blob: 49b4dd4720832772b5b08fc16eba84c1a07d7d09 [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;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +11002873 if(index >= (ftype->elements + 2)) {
Eric Biederman90089602004-05-28 14:11:54 +00002874 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
Eric Biederman90089602004-05-28 14:11:54 +00005649static struct type void_type = { .type = TYPE_VOID };
5650static struct type char_type = { .type = TYPE_CHAR };
5651static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005652#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005653static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005654#endif
Eric Biederman90089602004-05-28 14:11:54 +00005655static struct type ushort_type = { .type = TYPE_USHORT };
5656static struct type int_type = { .type = TYPE_INT };
5657static struct type uint_type = { .type = TYPE_UINT };
5658static struct type long_type = { .type = TYPE_LONG };
5659static struct type ulong_type = { .type = TYPE_ULONG };
5660static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005661
Eric Biederman5ade04a2003-10-22 04:03:46 +00005662static struct type void_ptr_type = {
5663 .type = TYPE_POINTER,
5664 .left = &void_type,
5665};
5666
Stefan Reinauer50542a82007-10-24 11:14:14 +00005667#if DEBUG_ROMCC_WARNING
Stefan Reinauer14e22772010-04-27 06:56:47 +00005668static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005669 .type = TYPE_FUNCTION,
5670 .left = &void_type,
5671 .right = &void_type,
5672};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005673#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005674
Eric Biederman90089602004-05-28 14:11:54 +00005675static size_t bits_to_bytes(size_t size)
5676{
5677 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5678}
5679
Eric Biedermanb138ac82003-04-22 18:44:01 +00005680static struct triple *variable(struct compile_state *state, struct type *type)
5681{
5682 struct triple *result;
5683 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005684 result = triple(state, OP_ADECL, type, 0, 0);
5685 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005686 }
5687 else {
5688 result = triple(state, OP_SDECL, type, 0, 0);
5689 }
5690 return result;
5691}
5692
5693static void stor_of(FILE *fp, struct type *type)
5694{
5695 switch(type->type & STOR_MASK) {
5696 case STOR_AUTO:
5697 fprintf(fp, "auto ");
5698 break;
5699 case STOR_STATIC:
5700 fprintf(fp, "static ");
5701 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005702 case STOR_LOCAL:
5703 fprintf(fp, "local ");
5704 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005705 case STOR_EXTERN:
5706 fprintf(fp, "extern ");
5707 break;
5708 case STOR_REGISTER:
5709 fprintf(fp, "register ");
5710 break;
5711 case STOR_TYPEDEF:
5712 fprintf(fp, "typedef ");
5713 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005714 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005715 fprintf(fp, "inline ");
5716 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005717 case STOR_INLINE | STOR_STATIC:
5718 fprintf(fp, "static inline");
5719 break;
5720 case STOR_INLINE | STOR_EXTERN:
5721 fprintf(fp, "extern inline");
5722 break;
5723 default:
5724 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5725 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005726 }
5727}
5728static void qual_of(FILE *fp, struct type *type)
5729{
5730 if (type->type & QUAL_CONST) {
5731 fprintf(fp, " const");
5732 }
5733 if (type->type & QUAL_VOLATILE) {
5734 fprintf(fp, " volatile");
5735 }
5736 if (type->type & QUAL_RESTRICT) {
5737 fprintf(fp, " restrict");
5738 }
5739}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005740
Eric Biedermanb138ac82003-04-22 18:44:01 +00005741static void name_of(FILE *fp, struct type *type)
5742{
Eric Biederman90089602004-05-28 14:11:54 +00005743 unsigned int base_type;
5744 base_type = type->type & TYPE_MASK;
5745 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5746 stor_of(fp, type);
5747 }
5748 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005749 case TYPE_VOID:
5750 fprintf(fp, "void");
5751 qual_of(fp, type);
5752 break;
5753 case TYPE_CHAR:
5754 fprintf(fp, "signed char");
5755 qual_of(fp, type);
5756 break;
5757 case TYPE_UCHAR:
5758 fprintf(fp, "unsigned char");
5759 qual_of(fp, type);
5760 break;
5761 case TYPE_SHORT:
5762 fprintf(fp, "signed short");
5763 qual_of(fp, type);
5764 break;
5765 case TYPE_USHORT:
5766 fprintf(fp, "unsigned short");
5767 qual_of(fp, type);
5768 break;
5769 case TYPE_INT:
5770 fprintf(fp, "signed int");
5771 qual_of(fp, type);
5772 break;
5773 case TYPE_UINT:
5774 fprintf(fp, "unsigned int");
5775 qual_of(fp, type);
5776 break;
5777 case TYPE_LONG:
5778 fprintf(fp, "signed long");
5779 qual_of(fp, type);
5780 break;
5781 case TYPE_ULONG:
5782 fprintf(fp, "unsigned long");
5783 qual_of(fp, type);
5784 break;
5785 case TYPE_POINTER:
5786 name_of(fp, type->left);
5787 fprintf(fp, " * ");
5788 qual_of(fp, type);
5789 break;
5790 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005791 name_of(fp, type->left);
5792 fprintf(fp, ", ");
5793 name_of(fp, type->right);
5794 break;
Eric Biederman90089602004-05-28 14:11:54 +00005795 case TYPE_OVERLAP:
5796 name_of(fp, type->left);
5797 fprintf(fp, ",| ");
5798 name_of(fp, type->right);
5799 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005800 case TYPE_ENUM:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005801 fprintf(fp, "enum %s",
Eric Biederman90089602004-05-28 14:11:54 +00005802 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005803 qual_of(fp, type);
5804 break;
5805 case TYPE_STRUCT:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005806 fprintf(fp, "struct %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005807 (type->type_ident)? type->type_ident->name : "");
5808 name_of(fp, type->left);
5809 fprintf(fp, " } ");
5810 qual_of(fp, type);
5811 break;
5812 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005813 fprintf(fp, "union %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005814 (type->type_ident)? type->type_ident->name : "");
5815 name_of(fp, type->left);
5816 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005817 qual_of(fp, type);
5818 break;
5819 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005820 name_of(fp, type->left);
5821 fprintf(fp, " (*)(");
5822 name_of(fp, type->right);
5823 fprintf(fp, ")");
5824 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005825 case TYPE_ARRAY:
5826 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005827 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005828 break;
Eric Biederman90089602004-05-28 14:11:54 +00005829 case TYPE_TUPLE:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005830 fprintf(fp, "tuple { ");
Eric Biederman90089602004-05-28 14:11:54 +00005831 name_of(fp, type->left);
5832 fprintf(fp, " } ");
5833 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005834 break;
Eric Biederman90089602004-05-28 14:11:54 +00005835 case TYPE_JOIN:
5836 fprintf(fp, "join { ");
5837 name_of(fp, type->left);
5838 fprintf(fp, " } ");
5839 qual_of(fp, type);
5840 break;
5841 case TYPE_BITFIELD:
5842 name_of(fp, type->left);
5843 fprintf(fp, " : %d ", type->elements);
5844 qual_of(fp, type);
5845 break;
5846 case TYPE_UNKNOWN:
5847 fprintf(fp, "unknown_t");
5848 break;
5849 default:
5850 fprintf(fp, "????: %x", base_type);
5851 break;
5852 }
5853 if (type->field_ident && type->field_ident->name) {
5854 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005855 }
5856}
5857
5858static size_t align_of(struct compile_state *state, struct type *type)
5859{
5860 size_t align;
5861 align = 0;
5862 switch(type->type & TYPE_MASK) {
5863 case TYPE_VOID:
5864 align = 1;
5865 break;
Eric Biederman90089602004-05-28 14:11:54 +00005866 case TYPE_BITFIELD:
5867 align = 1;
5868 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005869 case TYPE_CHAR:
5870 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005871 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005872 break;
5873 case TYPE_SHORT:
5874 case TYPE_USHORT:
5875 align = ALIGNOF_SHORT;
5876 break;
5877 case TYPE_INT:
5878 case TYPE_UINT:
5879 case TYPE_ENUM:
5880 align = ALIGNOF_INT;
5881 break;
5882 case TYPE_LONG:
5883 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005884 align = ALIGNOF_LONG;
5885 break;
Eric Biederman90089602004-05-28 14:11:54 +00005886 case TYPE_POINTER:
5887 align = ALIGNOF_POINTER;
5888 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005889 case TYPE_PRODUCT:
5890 case TYPE_OVERLAP:
5891 {
5892 size_t left_align, right_align;
5893 left_align = align_of(state, type->left);
5894 right_align = align_of(state, type->right);
5895 align = (left_align >= right_align) ? left_align : right_align;
5896 break;
5897 }
5898 case TYPE_ARRAY:
5899 align = align_of(state, type->left);
5900 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005901 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005902 case TYPE_TUPLE:
5903 case TYPE_UNION:
5904 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005905 align = align_of(state, type->left);
5906 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005907 default:
5908 error(state, 0, "alignof not yet defined for type\n");
5909 break;
5910 }
5911 return align;
5912}
5913
Eric Biederman90089602004-05-28 14:11:54 +00005914static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005915{
Eric Biederman90089602004-05-28 14:11:54 +00005916 size_t align;
5917 align = 0;
5918 switch(type->type & TYPE_MASK) {
5919 case TYPE_VOID:
5920 align = 1;
5921 break;
5922 case TYPE_BITFIELD:
5923 align = 1;
5924 break;
5925 case TYPE_CHAR:
5926 case TYPE_UCHAR:
5927 align = REG_ALIGNOF_CHAR;
5928 break;
5929 case TYPE_SHORT:
5930 case TYPE_USHORT:
5931 align = REG_ALIGNOF_SHORT;
5932 break;
5933 case TYPE_INT:
5934 case TYPE_UINT:
5935 case TYPE_ENUM:
5936 align = REG_ALIGNOF_INT;
5937 break;
5938 case TYPE_LONG:
5939 case TYPE_ULONG:
5940 align = REG_ALIGNOF_LONG;
5941 break;
5942 case TYPE_POINTER:
5943 align = REG_ALIGNOF_POINTER;
5944 break;
5945 case TYPE_PRODUCT:
5946 case TYPE_OVERLAP:
5947 {
5948 size_t left_align, right_align;
5949 left_align = reg_align_of(state, type->left);
5950 right_align = reg_align_of(state, type->right);
5951 align = (left_align >= right_align) ? left_align : right_align;
5952 break;
5953 }
5954 case TYPE_ARRAY:
5955 align = reg_align_of(state, type->left);
5956 break;
5957 case TYPE_STRUCT:
5958 case TYPE_UNION:
5959 case TYPE_TUPLE:
5960 case TYPE_JOIN:
5961 align = reg_align_of(state, type->left);
5962 break;
5963 default:
5964 error(state, 0, "alignof not yet defined for type\n");
5965 break;
5966 }
5967 return align;
5968}
5969
5970static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5971{
5972 return bits_to_bytes(align_of(state, type));
5973}
5974static size_t size_of(struct compile_state *state, struct type *type);
5975static size_t reg_size_of(struct compile_state *state, struct type *type);
5976
Stefan Reinauer14e22772010-04-27 06:56:47 +00005977static size_t needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00005978 struct type *type, size_t offset)
5979{
5980 size_t padding, align;
5981 align = align_of(state, type);
5982 /* Align to the next machine word if the bitfield does completely
5983 * fit into the current word.
5984 */
5985 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5986 size_t size;
5987 size = size_of(state, type);
5988 if ((offset + type->elements)/size != offset/size) {
5989 align = size;
5990 }
5991 }
Eric Biederman03b59862003-06-24 14:27:37 +00005992 padding = 0;
5993 if (offset % align) {
5994 padding = align - (offset % align);
5995 }
5996 return padding;
5997}
Eric Biederman90089602004-05-28 14:11:54 +00005998
Stefan Reinauer14e22772010-04-27 06:56:47 +00005999static size_t reg_needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006000 struct type *type, size_t offset)
6001{
6002 size_t padding, align;
6003 align = reg_align_of(state, type);
6004 /* Align to the next register word if the bitfield does completely
6005 * fit into the current register.
6006 */
6007 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00006008 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
Eric Biederman90089602004-05-28 14:11:54 +00006009 {
6010 align = REG_SIZEOF_REG;
6011 }
6012 padding = 0;
6013 if (offset % align) {
6014 padding = align - (offset % align);
6015 }
6016 return padding;
6017}
6018
Eric Biedermanb138ac82003-04-22 18:44:01 +00006019static size_t size_of(struct compile_state *state, struct type *type)
6020{
6021 size_t size;
6022 size = 0;
6023 switch(type->type & TYPE_MASK) {
6024 case TYPE_VOID:
6025 size = 0;
6026 break;
Eric Biederman90089602004-05-28 14:11:54 +00006027 case TYPE_BITFIELD:
6028 size = type->elements;
6029 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006030 case TYPE_CHAR:
6031 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006032 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006033 break;
6034 case TYPE_SHORT:
6035 case TYPE_USHORT:
6036 size = SIZEOF_SHORT;
6037 break;
6038 case TYPE_INT:
6039 case TYPE_UINT:
6040 case TYPE_ENUM:
6041 size = SIZEOF_INT;
6042 break;
6043 case TYPE_LONG:
6044 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006045 size = SIZEOF_LONG;
6046 break;
Eric Biederman90089602004-05-28 14:11:54 +00006047 case TYPE_POINTER:
6048 size = SIZEOF_POINTER;
6049 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006050 case TYPE_PRODUCT:
6051 {
Eric Biederman90089602004-05-28 14:11:54 +00006052 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006053 size = 0;
6054 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006055 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006056 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006057 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006058 }
Eric Biederman90089602004-05-28 14:11:54 +00006059 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006060 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006061 break;
6062 }
6063 case TYPE_OVERLAP:
6064 {
6065 size_t size_left, size_right;
6066 size_left = size_of(state, type->left);
6067 size_right = size_of(state, type->right);
6068 size = (size_left >= size_right)? size_left : size_right;
6069 break;
6070 }
6071 case TYPE_ARRAY:
6072 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6073 internal_error(state, 0, "Invalid array type");
6074 } else {
6075 size = size_of(state, type->left) * type->elements;
6076 }
6077 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006078 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006079 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006080 {
Eric Biederman90089602004-05-28 14:11:54 +00006081 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006082 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006083 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006084 pad = needed_padding(state, type, size);
6085 size = size + pad;
6086 break;
6087 }
6088 case TYPE_UNION:
6089 case TYPE_JOIN:
6090 {
6091 size_t pad;
6092 size = size_of(state, type->left);
6093 /* Pad unions so their size is a multiple of their alignment */
6094 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006095 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006096 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006097 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006098 default:
Eric Biederman90089602004-05-28 14:11:54 +00006099 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006100 break;
6101 }
6102 return size;
6103}
6104
Eric Biederman90089602004-05-28 14:11:54 +00006105static size_t reg_size_of(struct compile_state *state, struct type *type)
6106{
6107 size_t size;
6108 size = 0;
6109 switch(type->type & TYPE_MASK) {
6110 case TYPE_VOID:
6111 size = 0;
6112 break;
6113 case TYPE_BITFIELD:
6114 size = type->elements;
6115 break;
6116 case TYPE_CHAR:
6117 case TYPE_UCHAR:
6118 size = REG_SIZEOF_CHAR;
6119 break;
6120 case TYPE_SHORT:
6121 case TYPE_USHORT:
6122 size = REG_SIZEOF_SHORT;
6123 break;
6124 case TYPE_INT:
6125 case TYPE_UINT:
6126 case TYPE_ENUM:
6127 size = REG_SIZEOF_INT;
6128 break;
6129 case TYPE_LONG:
6130 case TYPE_ULONG:
6131 size = REG_SIZEOF_LONG;
6132 break;
6133 case TYPE_POINTER:
6134 size = REG_SIZEOF_POINTER;
6135 break;
6136 case TYPE_PRODUCT:
6137 {
6138 size_t pad;
6139 size = 0;
6140 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6141 pad = reg_needed_padding(state, type->left, size);
6142 size = size + pad + reg_size_of(state, type->left);
6143 type = type->right;
6144 }
6145 pad = reg_needed_padding(state, type, size);
6146 size = size + pad + reg_size_of(state, type);
6147 break;
6148 }
6149 case TYPE_OVERLAP:
6150 {
6151 size_t size_left, size_right;
6152 size_left = reg_size_of(state, type->left);
6153 size_right = reg_size_of(state, type->right);
6154 size = (size_left >= size_right)? size_left : size_right;
6155 break;
6156 }
6157 case TYPE_ARRAY:
6158 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6159 internal_error(state, 0, "Invalid array type");
6160 } else {
6161 size = reg_size_of(state, type->left) * type->elements;
6162 }
6163 break;
6164 case TYPE_STRUCT:
6165 case TYPE_TUPLE:
6166 {
6167 size_t pad;
6168 size = reg_size_of(state, type->left);
6169 /* Pad structures so their size is a multiples of their alignment */
6170 pad = reg_needed_padding(state, type, size);
6171 size = size + pad;
6172 break;
6173 }
6174 case TYPE_UNION:
6175 case TYPE_JOIN:
6176 {
6177 size_t pad;
6178 size = reg_size_of(state, type->left);
6179 /* Pad unions so their size is a multiple of their alignment */
6180 pad = reg_needed_padding(state, type, size);
6181 size = size + pad;
6182 break;
6183 }
6184 default:
6185 internal_error(state, 0, "sizeof not yet defined for type");
6186 break;
6187 }
6188 return size;
6189}
6190
6191static size_t registers_of(struct compile_state *state, struct type *type)
6192{
6193 size_t registers;
6194 registers = reg_size_of(state, type);
6195 registers += REG_SIZEOF_REG - 1;
6196 registers /= REG_SIZEOF_REG;
6197 return registers;
6198}
6199
6200static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6201{
6202 return bits_to_bytes(size_of(state, type));
6203}
6204
Stefan Reinauer14e22772010-04-27 06:56:47 +00006205static size_t field_offset(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006206 struct type *type, struct hash_entry *field)
6207{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006208 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006209 size_t size;
6210
Eric Biederman0babc1c2003-05-09 02:39:00 +00006211 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006212 member = 0;
6213 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6214 member = type->left;
6215 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6216 size += needed_padding(state, member->left, size);
6217 if (member->left->field_ident == field) {
6218 member = member->left;
6219 break;
6220 }
6221 size += size_of(state, member->left);
6222 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006223 }
Eric Biederman90089602004-05-28 14:11:54 +00006224 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006225 }
Eric Biederman90089602004-05-28 14:11:54 +00006226 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6227 member = type->left;
6228 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6229 if (member->left->field_ident == field) {
6230 member = member->left;
6231 break;
6232 }
6233 member = member->right;
6234 }
6235 }
6236 else {
6237 internal_error(state, 0, "field_offset only works on structures and unions");
6238 }
6239
6240 if (!member || (member->field_ident != field)) {
6241 error(state, 0, "member %s not present", field->name);
6242 }
6243 return size;
6244}
6245
Stefan Reinauer14e22772010-04-27 06:56:47 +00006246static size_t field_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006247 struct type *type, struct hash_entry *field)
6248{
6249 struct type *member;
6250 size_t size;
6251
6252 size = 0;
6253 member = 0;
6254 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6255 member = type->left;
6256 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6257 size += reg_needed_padding(state, member->left, size);
6258 if (member->left->field_ident == field) {
6259 member = member->left;
6260 break;
6261 }
6262 size += reg_size_of(state, member->left);
6263 member = member->right;
6264 }
6265 }
6266 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6267 member = type->left;
6268 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6269 if (member->left->field_ident == field) {
6270 member = member->left;
6271 break;
6272 }
6273 member = member->right;
6274 }
6275 }
6276 else {
6277 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6278 }
6279
6280 size += reg_needed_padding(state, member, size);
6281 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006282 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006283 }
6284 return size;
6285}
6286
Stefan Reinauer14e22772010-04-27 06:56:47 +00006287static struct type *field_type(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006288 struct type *type, struct hash_entry *field)
6289{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006290 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006291
6292 member = 0;
6293 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6294 member = type->left;
6295 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6296 if (member->left->field_ident == field) {
6297 member = member->left;
6298 break;
6299 }
6300 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006301 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006302 }
Eric Biederman90089602004-05-28 14:11:54 +00006303 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6304 member = type->left;
6305 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6306 if (member->left->field_ident == field) {
6307 member = member->left;
6308 break;
6309 }
6310 member = member->right;
6311 }
6312 }
6313 else {
6314 internal_error(state, 0, "field_type only works on structures and unions");
6315 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006316
Eric Biederman90089602004-05-28 14:11:54 +00006317 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006318 error(state, 0, "member %s not present", field->name);
6319 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006320 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006321}
6322
Stefan Reinauer14e22772010-04-27 06:56:47 +00006323static size_t index_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006324 struct type *type, ulong_t index)
6325{
6326 struct type *member;
6327 size_t size;
6328 size = 0;
6329 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6330 size = size_of(state, type->left) * index;
6331 }
6332 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6333 ulong_t i;
6334 member = type->left;
6335 i = 0;
6336 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6337 size += needed_padding(state, member->left, size);
6338 if (i == index) {
6339 member = member->left;
6340 break;
6341 }
6342 size += size_of(state, member->left);
6343 i++;
6344 member = member->right;
6345 }
6346 size += needed_padding(state, member, size);
6347 if (i != index) {
6348 internal_error(state, 0, "Missing member index: %u", index);
6349 }
6350 }
6351 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6352 ulong_t i;
6353 size = 0;
6354 member = type->left;
6355 i = 0;
6356 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6357 if (i == index) {
6358 member = member->left;
6359 break;
6360 }
6361 i++;
6362 member = member->right;
6363 }
6364 if (i != index) {
6365 internal_error(state, 0, "Missing member index: %u", index);
6366 }
6367 }
6368 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006369 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006370 "request for index %u in something not an array, tuple or join",
6371 index);
6372 }
6373 return size;
6374}
6375
Stefan Reinauer14e22772010-04-27 06:56:47 +00006376static size_t index_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006377 struct type *type, ulong_t index)
6378{
6379 struct type *member;
6380 size_t size;
6381 size = 0;
6382 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6383 size = reg_size_of(state, type->left) * index;
6384 }
6385 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6386 ulong_t i;
6387 member = type->left;
6388 i = 0;
6389 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6390 size += reg_needed_padding(state, member->left, size);
6391 if (i == index) {
6392 member = member->left;
6393 break;
6394 }
6395 size += reg_size_of(state, member->left);
6396 i++;
6397 member = member->right;
6398 }
6399 size += reg_needed_padding(state, member, size);
6400 if (i != index) {
6401 internal_error(state, 0, "Missing member index: %u", index);
6402 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006403
Eric Biederman90089602004-05-28 14:11:54 +00006404 }
6405 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6406 ulong_t i;
6407 size = 0;
6408 member = type->left;
6409 i = 0;
6410 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6411 if (i == index) {
6412 member = member->left;
6413 break;
6414 }
6415 i++;
6416 member = member->right;
6417 }
6418 if (i != index) {
6419 internal_error(state, 0, "Missing member index: %u", index);
6420 }
6421 }
6422 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006423 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006424 "request for index %u in something not an array, tuple or join",
6425 index);
6426 }
6427 return size;
6428}
6429
6430static struct type *index_type(struct compile_state *state,
6431 struct type *type, ulong_t index)
6432{
6433 struct type *member;
6434 if (index >= type->elements) {
6435 internal_error(state, 0, "Invalid element %u requested", index);
6436 }
6437 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6438 member = type->left;
6439 }
6440 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6441 ulong_t i;
6442 member = type->left;
6443 i = 0;
6444 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6445 if (i == index) {
6446 member = member->left;
6447 break;
6448 }
6449 i++;
6450 member = member->right;
6451 }
6452 if (i != index) {
6453 internal_error(state, 0, "Missing member index: %u", index);
6454 }
6455 }
6456 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6457 ulong_t i;
6458 member = type->left;
6459 i = 0;
6460 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6461 if (i == index) {
6462 member = member->left;
6463 break;
6464 }
6465 i++;
6466 member = member->right;
6467 }
6468 if (i != index) {
6469 internal_error(state, 0, "Missing member index: %u", index);
6470 }
6471 }
6472 else {
6473 member = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00006474 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006475 "request for index %u in something not an array, tuple or join",
6476 index);
6477 }
6478 return member;
6479}
6480
6481static struct type *unpack_type(struct compile_state *state, struct type *type)
6482{
6483 /* If I have a single register compound type not a bit-field
6484 * find the real type.
6485 */
6486 struct type *start_type;
6487 size_t size;
6488 /* Get out early if I need multiple registers for this type */
6489 size = reg_size_of(state, type);
6490 if (size > REG_SIZEOF_REG) {
6491 return type;
6492 }
6493 /* Get out early if I don't need any registers for this type */
6494 if (size == 0) {
6495 return &void_type;
6496 }
6497 /* Loop until I have no more layers I can remove */
6498 do {
6499 start_type = type;
6500 switch(type->type & TYPE_MASK) {
6501 case TYPE_ARRAY:
6502 /* If I have a single element the unpacked type
6503 * is that element.
6504 */
6505 if (type->elements == 1) {
6506 type = type->left;
6507 }
6508 break;
6509 case TYPE_STRUCT:
6510 case TYPE_TUPLE:
6511 /* If I have a single element the unpacked type
6512 * is that element.
6513 */
6514 if (type->elements == 1) {
6515 type = type->left;
6516 }
6517 /* If I have multiple elements the unpacked
6518 * type is the non-void element.
6519 */
6520 else {
6521 struct type *next, *member;
6522 struct type *sub_type;
6523 sub_type = 0;
6524 next = type->left;
6525 while(next) {
6526 member = next;
6527 next = 0;
6528 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6529 next = member->right;
6530 member = member->left;
6531 }
6532 if (reg_size_of(state, member) > 0) {
6533 if (sub_type) {
6534 internal_error(state, 0, "true compound type in a register");
6535 }
6536 sub_type = member;
6537 }
6538 }
6539 if (sub_type) {
6540 type = sub_type;
6541 }
6542 }
6543 break;
6544
6545 case TYPE_UNION:
6546 case TYPE_JOIN:
6547 /* If I have a single element the unpacked type
6548 * is that element.
6549 */
6550 if (type->elements == 1) {
6551 type = type->left;
6552 }
6553 /* I can't in general unpack union types */
6554 break;
6555 default:
6556 /* If I'm not a compound type I can't unpack it */
6557 break;
6558 }
6559 } while(start_type != type);
6560 switch(type->type & TYPE_MASK) {
6561 case TYPE_STRUCT:
6562 case TYPE_ARRAY:
6563 case TYPE_TUPLE:
6564 internal_error(state, 0, "irredicible type?");
6565 break;
6566 }
6567 return type;
6568}
6569
6570static int equiv_types(struct type *left, struct type *right);
6571static int is_compound_type(struct type *type);
6572
6573static struct type *reg_type(
6574 struct compile_state *state, struct type *type, int reg_offset)
6575{
6576 struct type *member;
6577 size_t size;
6578#if 1
6579 struct type *invalid;
6580 invalid = invalid_type(state, type);
6581 if (invalid) {
6582 fprintf(state->errout, "type: ");
6583 name_of(state->errout, type);
6584 fprintf(state->errout, "\n");
6585 fprintf(state->errout, "invalid: ");
6586 name_of(state->errout, invalid);
6587 fprintf(state->errout, "\n");
6588 internal_error(state, 0, "bad input type?");
6589 }
6590#endif
6591
6592 size = reg_size_of(state, type);
6593 if (reg_offset > size) {
6594 member = 0;
6595 fprintf(state->errout, "type: ");
6596 name_of(state->errout, type);
6597 fprintf(state->errout, "\n");
6598 internal_error(state, 0, "offset outside of type");
6599 }
6600 else {
6601 switch(type->type & TYPE_MASK) {
6602 /* Don't do anything with the basic types */
6603 case TYPE_VOID:
6604 case TYPE_CHAR: case TYPE_UCHAR:
6605 case TYPE_SHORT: case TYPE_USHORT:
6606 case TYPE_INT: case TYPE_UINT:
6607 case TYPE_LONG: case TYPE_ULONG:
6608 case TYPE_LLONG: case TYPE_ULLONG:
6609 case TYPE_FLOAT: case TYPE_DOUBLE:
6610 case TYPE_LDOUBLE:
6611 case TYPE_POINTER:
6612 case TYPE_ENUM:
6613 case TYPE_BITFIELD:
6614 member = type;
6615 break;
6616 case TYPE_ARRAY:
6617 member = type->left;
6618 size = reg_size_of(state, member);
6619 if (size > REG_SIZEOF_REG) {
6620 member = reg_type(state, member, reg_offset % size);
6621 }
6622 break;
6623 case TYPE_STRUCT:
6624 case TYPE_TUPLE:
6625 {
6626 size_t offset;
6627 offset = 0;
6628 member = type->left;
6629 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6630 size = reg_size_of(state, member->left);
6631 offset += reg_needed_padding(state, member->left, offset);
6632 if ((offset + size) > reg_offset) {
6633 member = member->left;
6634 break;
6635 }
6636 offset += size;
6637 member = member->right;
6638 }
6639 offset += reg_needed_padding(state, member, offset);
6640 member = reg_type(state, member, reg_offset - offset);
6641 break;
6642 }
6643 case TYPE_UNION:
6644 case TYPE_JOIN:
6645 {
6646 struct type *join, **jnext, *mnext;
6647 join = new_type(TYPE_JOIN, 0, 0);
6648 jnext = &join->left;
6649 mnext = type->left;
6650 while(mnext) {
6651 size_t size;
6652 member = mnext;
6653 mnext = 0;
6654 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6655 mnext = member->right;
6656 member = member->left;
6657 }
6658 size = reg_size_of(state, member);
6659 if (size > reg_offset) {
6660 struct type *part, *hunt;
6661 part = reg_type(state, member, reg_offset);
6662 /* See if this type is already in the union */
6663 hunt = join->left;
6664 while(hunt) {
6665 struct type *test = hunt;
6666 hunt = 0;
6667 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6668 hunt = test->right;
6669 test = test->left;
6670 }
6671 if (equiv_types(part, test)) {
6672 goto next;
6673 }
6674 }
6675 /* Nope add it */
6676 if (!*jnext) {
6677 *jnext = part;
6678 } else {
6679 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6680 jnext = &(*jnext)->right;
6681 }
6682 join->elements++;
6683 }
6684 next:
6685 ;
6686 }
6687 if (join->elements == 0) {
6688 internal_error(state, 0, "No elements?");
6689 }
6690 member = join;
6691 break;
6692 }
6693 default:
6694 member = 0;
6695 fprintf(state->errout, "type: ");
6696 name_of(state->errout, type);
6697 fprintf(state->errout, "\n");
6698 internal_error(state, 0, "reg_type not yet defined for type");
Stefan Reinauer14e22772010-04-27 06:56:47 +00006699
Eric Biederman90089602004-05-28 14:11:54 +00006700 }
6701 }
6702 /* If I have a single register compound type not a bit-field
6703 * find the real type.
6704 */
6705 member = unpack_type(state, member);
6706 ;
6707 size = reg_size_of(state, member);
6708 if (size > REG_SIZEOF_REG) {
6709 internal_error(state, 0, "Cannot find type of single register");
6710 }
6711#if 1
6712 invalid = invalid_type(state, member);
6713 if (invalid) {
6714 fprintf(state->errout, "type: ");
6715 name_of(state->errout, member);
6716 fprintf(state->errout, "\n");
6717 fprintf(state->errout, "invalid: ");
6718 name_of(state->errout, invalid);
6719 fprintf(state->errout, "\n");
6720 internal_error(state, 0, "returning bad type?");
6721 }
6722#endif
6723 return member;
6724}
6725
Eric Biederman03b59862003-06-24 14:27:37 +00006726static struct type *next_field(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006727 struct type *type, struct type *prev_member)
Eric Biederman03b59862003-06-24 14:27:37 +00006728{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006729 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006730 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6731 internal_error(state, 0, "next_field only works on structures");
6732 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006733 member = type->left;
6734 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006735 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006736 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006737 break;
6738 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006739 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006740 prev_member = 0;
6741 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006742 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006743 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006744 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006745 prev_member = 0;
6746 }
6747 if (prev_member) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006748 internal_error(state, 0, "prev_member %s not present",
Eric Biederman03b59862003-06-24 14:27:37 +00006749 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006750 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006751 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006752}
6753
Stefan Reinauer14e22772010-04-27 06:56:47 +00006754typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
Eric Biederman90089602004-05-28 14:11:54 +00006755 size_t ret_offset, size_t mem_offset, void *arg);
6756
6757static void walk_type_fields(struct compile_state *state,
6758 struct type *type, size_t reg_offset, size_t mem_offset,
6759 walk_type_fields_cb_t cb, void *arg);
6760
6761static void walk_struct_fields(struct compile_state *state,
6762 struct type *type, size_t reg_offset, size_t mem_offset,
6763 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006764{
Eric Biederman90089602004-05-28 14:11:54 +00006765 struct type *tptr;
6766 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006767 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006768 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006769 }
Eric Biederman90089602004-05-28 14:11:54 +00006770 tptr = type->left;
6771 for(i = 0; i < type->elements; i++) {
6772 struct type *mtype;
6773 mtype = tptr;
6774 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6775 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006776 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006777 walk_type_fields(state, mtype,
6778 reg_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006779 field_reg_offset(state, type, mtype->field_ident),
Stefan Reinauer14e22772010-04-27 06:56:47 +00006780 mem_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006781 field_offset(state, type, mtype->field_ident),
6782 cb, arg);
6783 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006784 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006785
Eric Biederman90089602004-05-28 14:11:54 +00006786}
6787
6788static void walk_type_fields(struct compile_state *state,
6789 struct type *type, size_t reg_offset, size_t mem_offset,
6790 walk_type_fields_cb_t cb, void *arg)
6791{
6792 switch(type->type & TYPE_MASK) {
6793 case TYPE_STRUCT:
6794 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6795 break;
6796 case TYPE_CHAR:
6797 case TYPE_UCHAR:
6798 case TYPE_SHORT:
6799 case TYPE_USHORT:
6800 case TYPE_INT:
6801 case TYPE_UINT:
6802 case TYPE_LONG:
6803 case TYPE_ULONG:
6804 cb(state, type, reg_offset, mem_offset, arg);
6805 break;
6806 case TYPE_VOID:
6807 break;
6808 default:
6809 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006810 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006811}
6812
Eric Biedermanb138ac82003-04-22 18:44:01 +00006813static void arrays_complete(struct compile_state *state, struct type *type)
6814{
6815 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6816 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6817 error(state, 0, "array size not specified");
6818 }
6819 arrays_complete(state, type->left);
6820 }
6821}
6822
Eric Biederman90089602004-05-28 14:11:54 +00006823static unsigned int get_basic_type(struct type *type)
6824{
6825 unsigned int basic;
6826 basic = type->type & TYPE_MASK;
6827 /* Convert enums to ints */
6828 if (basic == TYPE_ENUM) {
6829 basic = TYPE_INT;
6830 }
6831 /* Convert bitfields to standard types */
6832 else if (basic == TYPE_BITFIELD) {
6833 if (type->elements <= SIZEOF_CHAR) {
6834 basic = TYPE_CHAR;
6835 }
6836 else if (type->elements <= SIZEOF_SHORT) {
6837 basic = TYPE_SHORT;
6838 }
6839 else if (type->elements <= SIZEOF_INT) {
6840 basic = TYPE_INT;
6841 }
6842 else if (type->elements <= SIZEOF_LONG) {
6843 basic = TYPE_LONG;
6844 }
6845 if (!TYPE_SIGNED(type->left->type)) {
6846 basic += 1;
6847 }
6848 }
6849 return basic;
6850}
6851
Eric Biedermanb138ac82003-04-22 18:44:01 +00006852static unsigned int do_integral_promotion(unsigned int type)
6853{
Eric Biederman83b991a2003-10-11 06:20:25 +00006854 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006855 type = TYPE_INT;
6856 }
6857 return type;
6858}
6859
6860static unsigned int do_arithmetic_conversion(
6861 unsigned int left, unsigned int right)
6862{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006863 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6864 return TYPE_LDOUBLE;
6865 }
6866 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6867 return TYPE_DOUBLE;
6868 }
6869 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6870 return TYPE_FLOAT;
6871 }
6872 left = do_integral_promotion(left);
6873 right = do_integral_promotion(right);
6874 /* If both operands have the same size done */
6875 if (left == right) {
6876 return left;
6877 }
6878 /* If both operands have the same signedness pick the larger */
6879 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6880 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6881 }
6882 /* If the signed type can hold everything use it */
6883 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6884 return left;
6885 }
6886 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6887 return right;
6888 }
6889 /* Convert to the unsigned type with the same rank as the signed type */
6890 else if (TYPE_SIGNED(left)) {
6891 return TYPE_MKUNSIGNED(left);
6892 }
6893 else {
6894 return TYPE_MKUNSIGNED(right);
6895 }
6896}
6897
6898/* see if two types are the same except for qualifiers */
6899static int equiv_types(struct type *left, struct type *right)
6900{
6901 unsigned int type;
6902 /* Error if the basic types do not match */
6903 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6904 return 0;
6905 }
6906 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006907 /* If the basic types match and it is a void type we are done */
6908 if (type == TYPE_VOID) {
6909 return 1;
6910 }
Eric Biederman90089602004-05-28 14:11:54 +00006911 /* For bitfields we need to compare the sizes */
6912 else if (type == TYPE_BITFIELD) {
6913 return (left->elements == right->elements) &&
6914 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6915 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006916 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006917 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006918 return 1;
6919 }
6920 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006921 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006922 return equiv_types(left->left, right->left);
6923 }
6924 else if (type == TYPE_ARRAY) {
6925 return (left->elements == right->elements) &&
6926 equiv_types(left->left, right->left);
6927 }
Eric Biederman90089602004-05-28 14:11:54 +00006928 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006929 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006930 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006931 }
Eric Biederman90089602004-05-28 14:11:54 +00006932 /* test for union equality */
6933 else if (type == TYPE_UNION) {
6934 return left->type_ident == right->type_ident;
6935 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006936 /* Test for equivalent functions */
6937 else if (type == TYPE_FUNCTION) {
6938 return equiv_types(left->left, right->left) &&
6939 equiv_types(left->right, right->right);
6940 }
6941 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006942 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006943 else if (type == TYPE_PRODUCT) {
6944 return equiv_types(left->left, right->left) &&
6945 equiv_types(left->right, right->right);
6946 }
Eric Biederman90089602004-05-28 14:11:54 +00006947 /* We should see TYPE_OVERLAP when comparing joins */
6948 else if (type == TYPE_OVERLAP) {
6949 return equiv_types(left->left, right->left) &&
6950 equiv_types(left->right, right->right);
6951 }
6952 /* Test for equivalence of tuples */
6953 else if (type == TYPE_TUPLE) {
6954 return (left->elements == right->elements) &&
6955 equiv_types(left->left, right->left);
6956 }
6957 /* Test for equivalence of joins */
6958 else if (type == TYPE_JOIN) {
6959 return (left->elements == right->elements) &&
6960 equiv_types(left->left, right->left);
6961 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006962 else {
6963 return 0;
6964 }
6965}
6966
6967static int equiv_ptrs(struct type *left, struct type *right)
6968{
6969 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6970 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6971 return 0;
6972 }
6973 return equiv_types(left->left, right->left);
6974}
6975
6976static struct type *compatible_types(struct type *left, struct type *right)
6977{
6978 struct type *result;
6979 unsigned int type, qual_type;
6980 /* Error if the basic types do not match */
6981 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6982 return 0;
6983 }
6984 type = left->type & TYPE_MASK;
6985 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6986 result = 0;
6987 /* if the basic types match and it is an arithmetic type we are done */
6988 if (TYPE_ARITHMETIC(type)) {
6989 result = new_type(qual_type, 0, 0);
6990 }
6991 /* If it is a pointer type recurse and keep testing */
6992 else if (type == TYPE_POINTER) {
6993 result = compatible_types(left->left, right->left);
6994 if (result) {
6995 result = new_type(qual_type, result, 0);
6996 }
6997 }
Eric Biederman90089602004-05-28 14:11:54 +00006998 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006999 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007000 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007001 result = left;
7002 }
7003 }
Eric Biederman90089602004-05-28 14:11:54 +00007004 /* test for union equality */
7005 else if (type == TYPE_UNION) {
7006 if (left->type_ident == right->type_ident) {
7007 result = left;
7008 }
7009 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007010 /* Test for equivalent functions */
7011 else if (type == TYPE_FUNCTION) {
7012 struct type *lf, *rf;
7013 lf = compatible_types(left->left, right->left);
7014 rf = compatible_types(left->right, right->right);
7015 if (lf && rf) {
7016 result = new_type(qual_type, lf, rf);
7017 }
7018 }
7019 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7020 else if (type == TYPE_PRODUCT) {
7021 struct type *lf, *rf;
7022 lf = compatible_types(left->left, right->left);
7023 rf = compatible_types(left->right, right->right);
7024 if (lf && rf) {
7025 result = new_type(qual_type, lf, rf);
7026 }
7027 }
7028 else {
7029 /* Nothing else is compatible */
7030 }
7031 return result;
7032}
7033
Eric Biederman90089602004-05-28 14:11:54 +00007034/* See if left is a equivalent to right or right is a union member of left */
7035static int is_subset_type(struct type *left, struct type *right)
7036{
7037 if (equiv_types(left, right)) {
7038 return 1;
7039 }
7040 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7041 struct type *member, *mnext;
7042 mnext = left->left;
7043 while(mnext) {
7044 member = mnext;
7045 mnext = 0;
7046 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7047 mnext = member->right;
7048 member = member->left;
7049 }
7050 if (is_subset_type( member, right)) {
7051 return 1;
7052 }
7053 }
7054 }
7055 return 0;
7056}
7057
Eric Biedermanb138ac82003-04-22 18:44:01 +00007058static struct type *compatible_ptrs(struct type *left, struct type *right)
7059{
7060 struct type *result;
7061 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7062 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7063 return 0;
7064 }
7065 result = compatible_types(left->left, right->left);
7066 if (result) {
7067 unsigned int qual_type;
7068 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7069 result = new_type(qual_type, result, 0);
7070 }
7071 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007072
Eric Biedermanb138ac82003-04-22 18:44:01 +00007073}
7074static struct triple *integral_promotion(
7075 struct compile_state *state, struct triple *def)
7076{
7077 struct type *type;
7078 type = def->type;
7079 /* As all operations are carried out in registers
7080 * the values are converted on load I just convert
7081 * logical type of the operand.
7082 */
7083 if (TYPE_INTEGER(type->type)) {
7084 unsigned int int_type;
7085 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007086 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007087 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007088 if (def->op != OP_LOAD) {
7089 def->type = new_type(int_type, 0, 0);
7090 }
7091 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00007092 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007093 new_type(int_type, 0, 0), def, 0);
7094 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007095 }
7096 }
7097 return def;
7098}
7099
7100
7101static void arithmetic(struct compile_state *state, struct triple *def)
7102{
7103 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007104 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007105 }
7106}
7107
7108static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7109{
7110 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7111 error(state, def, "pointer or arithmetic type expected");
7112 }
7113}
7114
7115static int is_integral(struct triple *ins)
7116{
7117 return TYPE_INTEGER(ins->type->type);
7118}
7119
7120static void integral(struct compile_state *state, struct triple *def)
7121{
7122 if (!is_integral(def)) {
7123 error(state, 0, "integral type expected");
7124 }
7125}
7126
7127
7128static void bool(struct compile_state *state, struct triple *def)
7129{
7130 if (!TYPE_ARITHMETIC(def->type->type) &&
7131 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7132 error(state, 0, "arithmetic or pointer type expected");
7133 }
7134}
7135
7136static int is_signed(struct type *type)
7137{
Eric Biederman90089602004-05-28 14:11:54 +00007138 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7139 type = type->left;
7140 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007141 return !!TYPE_SIGNED(type->type);
7142}
Eric Biederman90089602004-05-28 14:11:54 +00007143static int is_compound_type(struct type *type)
7144{
7145 int is_compound;
7146 switch((type->type & TYPE_MASK)) {
7147 case TYPE_ARRAY:
7148 case TYPE_STRUCT:
7149 case TYPE_TUPLE:
7150 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00007151 case TYPE_JOIN:
Eric Biederman90089602004-05-28 14:11:54 +00007152 is_compound = 1;
7153 break;
7154 default:
7155 is_compound = 0;
7156 break;
7157 }
7158 return is_compound;
7159}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007160
Eric Biederman0babc1c2003-05-09 02:39:00 +00007161/* Is this value located in a register otherwise it must be in memory */
7162static int is_in_reg(struct compile_state *state, struct triple *def)
7163{
7164 int in_reg;
7165 if (def->op == OP_ADECL) {
7166 in_reg = 1;
7167 }
7168 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7169 in_reg = 0;
7170 }
Eric Biederman90089602004-05-28 14:11:54 +00007171 else if (triple_is_part(state, def)) {
7172 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007173 }
7174 else {
Eric Biederman90089602004-05-28 14:11:54 +00007175 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007176 in_reg = -1;
7177 }
7178 return in_reg;
7179}
7180
Eric Biederman90089602004-05-28 14:11:54 +00007181/* Is this an auto or static variable location? Something that can
7182 * be assigned to. Otherwise it must must be a pure value, a temporary.
7183 */
7184static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007185{
7186 int ret;
7187 ret = 0;
7188 if (!def) {
7189 return 0;
7190 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007191 if ((def->op == OP_ADECL) ||
7192 (def->op == OP_SDECL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +00007193 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007194 (def->op == OP_BLOBCONST) ||
7195 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007196 ret = 1;
7197 }
Eric Biederman90089602004-05-28 14:11:54 +00007198 else if (triple_is_part(state, def)) {
7199 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007200 }
7201 return ret;
7202}
7203
Eric Biederman00443072003-06-24 12:34:45 +00007204static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007205{
7206 if (!def) {
7207 internal_error(state, def, "nothing where lvalue expected?");
7208 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007209 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007210 error(state, def, "lvalue expected");
7211 }
7212}
Eric Biederman00443072003-06-24 12:34:45 +00007213static void lvalue(struct compile_state *state, struct triple *def)
7214{
7215 clvalue(state, def);
7216 if (def->type->type & QUAL_CONST) {
7217 error(state, def, "modifable lvalue expected");
7218 }
7219}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007220
7221static int is_pointer(struct triple *def)
7222{
7223 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7224}
7225
7226static void pointer(struct compile_state *state, struct triple *def)
7227{
7228 if (!is_pointer(def)) {
7229 error(state, def, "pointer expected");
7230 }
7231}
7232
7233static struct triple *int_const(
7234 struct compile_state *state, struct type *type, ulong_t value)
7235{
7236 struct triple *result;
7237 switch(type->type & TYPE_MASK) {
7238 case TYPE_CHAR:
7239 case TYPE_INT: case TYPE_UINT:
7240 case TYPE_LONG: case TYPE_ULONG:
7241 break;
7242 default:
Eric Biederman90089602004-05-28 14:11:54 +00007243 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007244 }
7245 result = triple(state, OP_INTCONST, type, 0, 0);
7246 result->u.cval = value;
7247 return result;
7248}
7249
7250
Eric Biederman83b991a2003-10-11 06:20:25 +00007251static struct triple *read_expr(struct compile_state *state, struct triple *def);
7252
Stefan Reinauer14e22772010-04-27 06:56:47 +00007253static struct triple *do_mk_addr_expr(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007254 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007255{
7256 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007257 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007258 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007259
Eric Biederman90089602004-05-28 14:11:54 +00007260 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007261
Stefan Reinauer14e22772010-04-27 06:56:47 +00007262
Eric Biedermanb138ac82003-04-22 18:44:01 +00007263 result = 0;
7264 if (expr->op == OP_ADECL) {
7265 error(state, expr, "address of auto variables not supported");
7266 }
7267 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007268 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007269 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007270 result->u.cval = offset;
7271 }
7272 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007273 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007274 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007275 int_const(state, &ulong_type, offset));
7276 }
Eric Biederman90089602004-05-28 14:11:54 +00007277 else if (expr->op == OP_BLOBCONST) {
7278 FINISHME();
7279 internal_error(state, expr, "not yet implemented");
7280 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007281 else if (expr->op == OP_LIST) {
7282 error(state, 0, "Function addresses not supported");
7283 }
Eric Biederman90089602004-05-28 14:11:54 +00007284 else if (triple_is_part(state, expr)) {
7285 struct triple *part;
7286 part = expr;
7287 expr = MISC(expr, 0);
7288 if (part->op == OP_DOT) {
7289 offset += bits_to_bytes(
7290 field_offset(state, expr->type, part->u.field));
7291 }
7292 else if (part->op == OP_INDEX) {
7293 offset += bits_to_bytes(
7294 index_offset(state, expr->type, part->u.cval));
7295 }
7296 else {
7297 internal_error(state, part, "unhandled part type");
7298 }
7299 result = do_mk_addr_expr(state, expr, type, offset);
7300 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007301 if (!result) {
7302 internal_error(state, expr, "cannot take address of expression");
7303 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007304 return result;
7305}
7306
Eric Biederman0babc1c2003-05-09 02:39:00 +00007307static struct triple *mk_addr_expr(
7308 struct compile_state *state, struct triple *expr, ulong_t offset)
7309{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007310 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007311}
7312
Eric Biedermanb138ac82003-04-22 18:44:01 +00007313static struct triple *mk_deref_expr(
7314 struct compile_state *state, struct triple *expr)
7315{
7316 struct type *base_type;
7317 pointer(state, expr);
7318 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007319 return triple(state, OP_DEREF, base_type, expr, 0);
7320}
7321
Eric Biederman90089602004-05-28 14:11:54 +00007322/* lvalue conversions always apply except when certain operators
7323 * are applied. So I apply apply it when I know no more
7324 * operators will be applied.
7325 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007326static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007327{
Eric Biederman5cd81732004-03-11 15:01:31 +00007328 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007329 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7330 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007331 type = new_type(
7332 TYPE_POINTER | (def->type->type & QUAL_MASK),
7333 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007334 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007335 struct triple *addrconst;
7336 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7337 internal_error(state, def, "bad array constant");
7338 }
7339 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7340 MISC(addrconst, 0) = def;
7341 def = addrconst;
7342 }
7343 else {
Eric Biederman90089602004-05-28 14:11:54 +00007344 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007345 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007346 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007347 /* Transform a function to a pointer to it */
7348 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7349 def = mk_addr_expr(state, def, 0);
7350 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007351 return def;
7352}
7353
Eric Biederman0babc1c2003-05-09 02:39:00 +00007354static struct triple *deref_field(
7355 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7356{
7357 struct triple *result;
7358 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007359 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007360 if (!field) {
7361 internal_error(state, 0, "No field passed to deref_field");
7362 }
7363 result = 0;
7364 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007365 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7366 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007367 error(state, 0, "request for member %s in something not a struct or union",
7368 field->name);
7369 }
Eric Biederman03b59862003-06-24 14:27:37 +00007370 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007371 if ((type->type & STOR_MASK) == STOR_PERM) {
7372 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007373 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007374 result = do_mk_addr_expr(state, expr, member, offset);
7375 result = mk_deref_expr(state, result);
7376 }
7377 else {
7378 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007379 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007380 result->u.field = field;
7381 }
7382 return result;
7383}
7384
Eric Biederman90089602004-05-28 14:11:54 +00007385static struct triple *deref_index(
7386 struct compile_state *state, struct triple *expr, size_t index)
7387{
7388 struct triple *result;
7389 struct type *type, *member;
7390 ulong_t offset;
7391
7392 result = 0;
7393 type = expr->type;
7394 member = index_type(state, type, index);
7395
7396 if ((type->type & STOR_MASK) == STOR_PERM) {
7397 offset = bits_to_bytes(index_offset(state, type, index));
7398 result = do_mk_addr_expr(state, expr, member, offset);
7399 result = mk_deref_expr(state, result);
7400 }
7401 else {
7402 result = triple(state, OP_INDEX, member, expr, 0);
7403 result->u.cval = index;
7404 }
7405 return result;
7406}
7407
Eric Biedermanb138ac82003-04-22 18:44:01 +00007408static struct triple *read_expr(struct compile_state *state, struct triple *def)
7409{
7410 int op;
7411 if (!def) {
7412 return 0;
7413 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007414#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007415#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007416#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007417 /* Transform lvalues into something we can read */
7418 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007419 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007420 return def;
7421 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007422 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007423 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007424 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007425 if (def->op == OP_SDECL) {
7426 def = mk_addr_expr(state, def, 0);
7427 def = mk_deref_expr(state, def);
7428 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007429 op = OP_LOAD;
7430 }
Eric Biederman90089602004-05-28 14:11:54 +00007431 def = triple(state, op, def->type, def, 0);
7432 if (def->type->type & QUAL_VOLATILE) {
7433 def->id |= TRIPLE_FLAG_VOLATILE;
7434 }
7435 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007436}
7437
Stefan Reinauer14e22772010-04-27 06:56:47 +00007438int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007439 struct type *dest, struct type *rval)
7440{
7441 int compatible = 0;
7442 /* Both operands have arithmetic type */
7443 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7444 compatible = 1;
7445 }
7446 /* One operand is a pointer and the other is a pointer to void */
7447 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7448 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7449 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7450 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7451 compatible = 1;
7452 }
7453 /* If both types are the same without qualifiers we are good */
7454 else if (equiv_ptrs(dest, rval)) {
7455 compatible = 1;
7456 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007457 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007458 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007459 compatible = 1;
7460 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007461 return compatible;
7462}
7463
Eric Biedermane058a1e2003-07-12 01:21:31 +00007464static void write_compatible(struct compile_state *state,
7465 struct type *dest, struct type *rval)
7466{
7467 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007468 FILE *fp = state->errout;
7469 fprintf(fp, "dest: ");
7470 name_of(fp, dest);
7471 fprintf(fp,"\nrval: ");
7472 name_of(fp, rval);
7473 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007474 error(state, 0, "Incompatible types in assignment");
7475 }
7476}
7477
Eric Biedermane058a1e2003-07-12 01:21:31 +00007478static int is_init_compatible(struct compile_state *state,
7479 struct type *dest, struct type *rval)
7480{
7481 int compatible = 0;
7482 if (is_write_compatible(state, dest, rval)) {
7483 compatible = 1;
7484 }
7485 else if (equiv_types(dest, rval)) {
7486 compatible = 1;
7487 }
7488 return compatible;
7489}
7490
Eric Biedermanb138ac82003-04-22 18:44:01 +00007491static struct triple *write_expr(
7492 struct compile_state *state, struct triple *dest, struct triple *rval)
7493{
7494 struct triple *def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007495
7496 def = 0;
7497 if (!rval) {
7498 internal_error(state, 0, "missing rval");
7499 }
7500
7501 if (rval->op == OP_LIST) {
7502 internal_error(state, 0, "expression of type OP_LIST?");
7503 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007504 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007505 internal_error(state, 0, "writing to a non lvalue?");
7506 }
Eric Biederman00443072003-06-24 12:34:45 +00007507 if (dest->type->type & QUAL_CONST) {
7508 internal_error(state, 0, "modifable lvalue expexted");
7509 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007510
7511 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007512 if (!equiv_types(dest->type, rval->type)) {
7513 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7514 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007515
7516 /* Now figure out which assignment operator to use */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007517 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007518 def = triple(state, OP_WRITE, dest->type, rval, dest);
7519 if (MISC(def, 0) != dest) {
7520 internal_error(state, def, "huh?");
7521 }
7522 if (RHS(def, 0) != rval) {
7523 internal_error(state, def, "huh?");
7524 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007525 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007526 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007527 }
Eric Biederman90089602004-05-28 14:11:54 +00007528 if (def->type->type & QUAL_VOLATILE) {
7529 def->id |= TRIPLE_FLAG_VOLATILE;
7530 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007531 return def;
7532}
7533
7534static struct triple *init_expr(
7535 struct compile_state *state, struct triple *dest, struct triple *rval)
7536{
7537 struct triple *def;
7538
7539 def = 0;
7540 if (!rval) {
7541 internal_error(state, 0, "missing rval");
7542 }
7543 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7544 rval = read_expr(state, rval);
7545 def = write_expr(state, dest, rval);
7546 }
7547 else {
7548 /* Fill in the array size if necessary */
7549 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7550 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7551 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7552 dest->type->elements = rval->type->elements;
7553 }
7554 }
7555 if (!equiv_types(dest->type, rval->type)) {
7556 error(state, 0, "Incompatible types in inializer");
7557 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007558 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007559 insert_triple(state, dest, rval);
7560 rval->id |= TRIPLE_FLAG_FLATTENED;
7561 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007562 }
7563 return def;
7564}
7565
7566struct type *arithmetic_result(
7567 struct compile_state *state, struct triple *left, struct triple *right)
7568{
7569 struct type *type;
7570 /* Sanity checks to ensure I am working with arithmetic types */
7571 arithmetic(state, left);
7572 arithmetic(state, right);
7573 type = new_type(
7574 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007575 get_basic_type(left->type),
7576 get_basic_type(right->type)),
7577 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007578 return type;
7579}
7580
7581struct type *ptr_arithmetic_result(
7582 struct compile_state *state, struct triple *left, struct triple *right)
7583{
7584 struct type *type;
7585 /* Sanity checks to ensure I am working with the proper types */
7586 ptr_arithmetic(state, left);
7587 arithmetic(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007588 if (TYPE_ARITHMETIC(left->type->type) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00007589 TYPE_ARITHMETIC(right->type->type)) {
7590 type = arithmetic_result(state, left, right);
7591 }
7592 else if (TYPE_PTR(left->type->type)) {
7593 type = left->type;
7594 }
7595 else {
7596 internal_error(state, 0, "huh?");
7597 type = 0;
7598 }
7599 return type;
7600}
7601
Eric Biedermanb138ac82003-04-22 18:44:01 +00007602/* boolean helper function */
7603
Stefan Reinauer14e22772010-04-27 06:56:47 +00007604static struct triple *ltrue_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007605 struct triple *expr)
7606{
7607 switch(expr->op) {
7608 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7609 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7610 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7611 /* If the expression is already boolean do nothing */
7612 break;
7613 default:
7614 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7615 break;
7616 }
7617 return expr;
7618}
7619
Stefan Reinauer14e22772010-04-27 06:56:47 +00007620static struct triple *lfalse_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007621 struct triple *expr)
7622{
7623 return triple(state, OP_LFALSE, &int_type, expr, 0);
7624}
7625
Eric Biederman90089602004-05-28 14:11:54 +00007626static struct triple *mkland_expr(
7627 struct compile_state *state,
7628 struct triple *left, struct triple *right)
7629{
7630 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007631 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007632
7633 /* Generate some intermediate triples */
7634 end = label(state);
7635 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007636
Eric Biederman90089602004-05-28 14:11:54 +00007637 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007638 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007639
7640 /* Jump if the value is false */
Stefan Reinauer14e22772010-04-27 06:56:47 +00007641 jmp = branch(state, end,
Eric Biederman90089602004-05-28 14:11:54 +00007642 lfalse_expr(state, read_expr(state, var)));
7643 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007644
Eric Biederman90089602004-05-28 14:11:54 +00007645 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007646 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007647
7648 /* An expression for the computed value */
7649 val = read_expr(state, var);
7650
7651 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007652 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007653
Eric Biederman90089602004-05-28 14:11:54 +00007654 return def;
7655}
7656
7657static struct triple *mklor_expr(
7658 struct compile_state *state,
7659 struct triple *left, struct triple *right)
7660{
7661 struct triple *def, *val, *var, *jmp, *mid, *end;
7662
7663 /* Generate some intermediate triples */
7664 end = label(state);
7665 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007666
Eric Biederman90089602004-05-28 14:11:54 +00007667 /* Store the left hand side value */
7668 left = write_expr(state, var, left);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007669
Eric Biederman90089602004-05-28 14:11:54 +00007670 /* Jump if the value is true */
7671 jmp = branch(state, end, read_expr(state, var));
7672 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007673
Eric Biederman90089602004-05-28 14:11:54 +00007674 /* Store the right hand side value */
7675 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007676
Eric Biederman90089602004-05-28 14:11:54 +00007677 /* An expression for the computed value*/
7678 val = read_expr(state, var);
7679
7680 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007681 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007682
7683 return def;
7684}
7685
7686static struct triple *mkcond_expr(
Stefan Reinauer14e22772010-04-27 06:56:47 +00007687 struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007688 struct triple *test, struct triple *left, struct triple *right)
7689{
Eric Biederman90089602004-05-28 14:11:54 +00007690 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007691 struct type *result_type;
7692 unsigned int left_type, right_type;
7693 bool(state, test);
7694 left_type = left->type->type;
7695 right_type = right->type->type;
7696 result_type = 0;
7697 /* Both operands have arithmetic type */
7698 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7699 result_type = arithmetic_result(state, left, right);
7700 }
7701 /* Both operands have void type */
7702 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7703 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7704 result_type = &void_type;
7705 }
7706 /* pointers to the same type... */
7707 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7708 ;
7709 }
7710 /* Both operands are pointers and left is a pointer to void */
7711 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7712 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7713 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7714 result_type = right->type;
7715 }
7716 /* Both operands are pointers and right is a pointer to void */
7717 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7718 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7719 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7720 result_type = left->type;
7721 }
7722 if (!result_type) {
7723 error(state, 0, "Incompatible types in conditional expression");
7724 }
Eric Biederman90089602004-05-28 14:11:54 +00007725 /* Generate some intermediate triples */
7726 mid = label(state);
7727 end = label(state);
7728 var = variable(state, result_type);
7729
7730 /* Branch if the test is false */
7731 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7732 top = label(state);
7733
7734 /* Store the left hand side value */
7735 left = write_expr(state, var, left);
7736
7737 /* Branch to the end */
7738 jmp2 = branch(state, end, 0);
7739
7740 /* Store the right hand side value */
7741 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007742
Eric Biederman90089602004-05-28 14:11:54 +00007743 /* An expression for the computed value */
7744 val = read_expr(state, var);
7745
7746 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007747 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007748
Eric Biedermanb138ac82003-04-22 18:44:01 +00007749 return def;
7750}
7751
7752
Eric Biederman0babc1c2003-05-09 02:39:00 +00007753static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007754{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007755#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007756#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007757#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007758 int count;
7759 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007760 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7761 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007762 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007763 else if (ins->op == OP_DEREF) {
7764 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007765 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007766 else if (ins->op == OP_VAL) {
7767 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007768 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007769 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007770 /* Don't figure the depth of a call just guess it is huge */
7771 count = 1000;
7772 }
7773 else {
7774 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007775 expr = triple_rhs(state, ins, 0);
7776 for(;expr; expr = triple_rhs(state, ins, expr)) {
7777 if (*expr) {
7778 int depth;
7779 depth = expr_depth(state, *expr);
7780 if (depth > count) {
7781 count = depth;
7782 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007783 }
7784 }
7785 }
7786 return count + 1;
7787}
7788
Eric Biederman0babc1c2003-05-09 02:39:00 +00007789static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007790 struct compile_state *state, struct triple *first, struct triple *ptr,
7791 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007792{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007793 struct rhs_vector {
7794 int depth;
7795 struct triple **ins;
7796 } vector[MAX_RHS];
7797 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007798 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007799 rhs = ptr->rhs;
7800 lhs = ptr->lhs;
7801 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007802 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007803 ptr->op, tops(ptr->op));
7804 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007805 /* Find the depth of the rhs elements */
7806 for(i = 0; i < rhs; i++) {
7807 vector[i].ins = &RHS(ptr, i);
7808 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007809 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007810 /* Selection sort the rhs */
7811 for(i = 0; i < rhs; i++) {
7812 int j, max = i;
7813 for(j = i + 1; j < rhs; j++ ) {
7814 if (vector[j].depth > vector[max].depth) {
7815 max = j;
7816 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007817 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007818 if (max != i) {
7819 struct rhs_vector tmp;
7820 tmp = vector[i];
7821 vector[i] = vector[max];
7822 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007823 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007824 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007825 /* Now flatten the rhs elements */
7826 for(i = 0; i < rhs; i++) {
7827 *vector[i].ins = flatten(state, first, *vector[i].ins);
7828 use_triple(*vector[i].ins, ptr);
7829 }
Eric Biederman90089602004-05-28 14:11:54 +00007830 if (lhs) {
7831 insert_triple(state, first, ptr);
7832 ptr->id |= TRIPLE_FLAG_FLATTENED;
7833 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007834
Eric Biederman90089602004-05-28 14:11:54 +00007835 /* Now flatten the lhs elements */
7836 for(i = 0; i < lhs; i++) {
7837 struct triple **ins = &LHS(ptr, i);
7838 *ins = flatten(state, first, *ins);
7839 use_triple(*ins, ptr);
7840 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007841 }
7842 return ptr;
7843}
7844
Eric Biederman90089602004-05-28 14:11:54 +00007845static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007846 struct compile_state *state, struct triple *first, struct triple *ptr)
7847{
Eric Biederman90089602004-05-28 14:11:54 +00007848 struct triple *head, *body, *val;
7849 head = RHS(ptr, 0);
7850 RHS(ptr, 0) = 0;
7851 val = head->prev;
7852 body = head->next;
7853 release_triple(state, head);
7854 release_triple(state, ptr);
7855 val->next = first;
7856 body->prev = first->prev;
7857 body->prev->next = body;
7858 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007859
Eric Biederman90089602004-05-28 14:11:54 +00007860 if (triple_is_cbranch(state, body->prev) ||
7861 triple_is_call(state, body->prev)) {
7862 unuse_triple(first, body->prev);
7863 use_triple(body, body->prev);
7864 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007865
Eric Biederman90089602004-05-28 14:11:54 +00007866 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7867 internal_error(state, val, "val not flattened?");
7868 }
7869
7870 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007871}
7872
Eric Biederman90089602004-05-28 14:11:54 +00007873
7874static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007875 struct compile_state *state, struct triple *first, struct triple *ptr)
7876{
Eric Biederman90089602004-05-28 14:11:54 +00007877 if (!triple_is_part(state, ptr)) {
7878 internal_error(state, ptr, "not a part");
7879 }
7880 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7881 internal_error(state, ptr, "unexpected args for: %d %s",
7882 ptr->op, tops(ptr->op));
7883 }
7884 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7885 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007886 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007887}
7888
7889static struct triple *flatten(
7890 struct compile_state *state, struct triple *first, struct triple *ptr)
7891{
7892 struct triple *orig_ptr;
7893 if (!ptr)
7894 return 0;
7895 do {
7896 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007897 /* Only flatten triples once */
7898 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7899 return ptr;
7900 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007901 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007902 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007903 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7904 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007905 break;
Eric Biederman90089602004-05-28 14:11:54 +00007906 case OP_PROG:
7907 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007908 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007909 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007910 ptr = flatten_generic(state, first, ptr, 1);
7911 insert_triple(state, first, ptr);
7912 ptr->id |= TRIPLE_FLAG_FLATTENED;
7913 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7914 if (ptr->next != ptr) {
7915 use_triple(ptr->next, ptr);
7916 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007917 break;
7918 case OP_READ:
7919 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007920 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7921 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007922 break;
Eric Biederman90089602004-05-28 14:11:54 +00007923 case OP_WRITE:
7924 ptr = flatten_generic(state, first, ptr, 1);
7925 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7926 use_triple(MISC(ptr, 0), ptr);
7927 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007928 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007929 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007930 break;
7931 case OP_CBRANCH:
7932 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7933 use_triple(RHS(ptr, 0), ptr);
7934 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007935 insert_triple(state, first, ptr);
7936 ptr->id |= TRIPLE_FLAG_FLATTENED;
7937 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007938 if (ptr->next != ptr) {
7939 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007940 }
7941 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007942 case OP_CALL:
7943 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7944 use_triple(MISC(ptr, 0), ptr);
7945 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007946 insert_triple(state, first, ptr);
7947 ptr->id |= TRIPLE_FLAG_FLATTENED;
7948 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007949 if (ptr->next != ptr) {
7950 use_triple(ptr->next, ptr);
7951 }
7952 break;
7953 case OP_RET:
7954 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7955 use_triple(RHS(ptr, 0), ptr);
7956 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007957 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007958 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007959 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007960 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007961 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007962 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007963 break;
7964 case OP_DEREF:
7965 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007966 ptr = RHS(ptr, 0);
7967 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007968 free_triple(state, orig_ptr);
7969 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007970 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007971 if (RHS(ptr, 0)->op == OP_DEREF) {
7972 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007973 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007974 base = MISC(ptr, 0);
7975 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007976 left = RHS(base, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007977 ptr = triple(state, OP_ADD, left->type,
Eric Biederman03b59862003-06-24 14:27:37 +00007978 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007979 int_const(state, &ulong_type, offset));
7980 free_triple(state, base);
7981 }
Eric Biederman90089602004-05-28 14:11:54 +00007982 else {
7983 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007984 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007985 break;
Eric Biederman90089602004-05-28 14:11:54 +00007986 case OP_INDEX:
7987 if (RHS(ptr, 0)->op == OP_DEREF) {
7988 struct triple *base, *left;
7989 ulong_t offset;
7990 base = MISC(ptr, 0);
7991 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
7992 left = RHS(base, 0);
7993 ptr = triple(state, OP_ADD, left->type,
7994 read_expr(state, left),
7995 int_const(state, &long_type, offset));
7996 free_triple(state, base);
7997 }
7998 else {
7999 ptr = flatten_part(state, first, ptr);
8000 }
8001 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008002 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008003 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008004 use_triple(ptr, MISC(ptr, 0));
8005 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008006 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008007 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8008 use_triple(MISC(ptr, 0), ptr);
8009 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008010 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008011 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008012 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8013 use_triple(MISC(ptr, 0), ptr);
8014 insert_triple(state, first, ptr);
8015 ptr->id |= TRIPLE_FLAG_FLATTENED;
8016 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8017 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008018 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008019 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008020 break;
8021 default:
8022 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008023 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008024 break;
8025 }
8026 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008027 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008028 insert_triple(state, first, ptr);
8029 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008030 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008031 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008032 return ptr;
8033}
8034
8035static void release_expr(struct compile_state *state, struct triple *expr)
8036{
8037 struct triple *head;
8038 head = label(state);
8039 flatten(state, head, expr);
8040 while(head->next != head) {
8041 release_triple(state, head->next);
8042 }
8043 free_triple(state, head);
8044}
8045
8046static int replace_rhs_use(struct compile_state *state,
8047 struct triple *orig, struct triple *new, struct triple *use)
8048{
8049 struct triple **expr;
8050 int found;
8051 found = 0;
8052 expr = triple_rhs(state, use, 0);
8053 for(;expr; expr = triple_rhs(state, use, expr)) {
8054 if (*expr == orig) {
8055 *expr = new;
8056 found = 1;
8057 }
8058 }
8059 if (found) {
8060 unuse_triple(orig, use);
8061 use_triple(new, use);
8062 }
8063 return found;
8064}
8065
8066static int replace_lhs_use(struct compile_state *state,
8067 struct triple *orig, struct triple *new, struct triple *use)
8068{
8069 struct triple **expr;
8070 int found;
8071 found = 0;
8072 expr = triple_lhs(state, use, 0);
8073 for(;expr; expr = triple_lhs(state, use, expr)) {
8074 if (*expr == orig) {
8075 *expr = new;
8076 found = 1;
8077 }
8078 }
8079 if (found) {
8080 unuse_triple(orig, use);
8081 use_triple(new, use);
8082 }
8083 return found;
8084}
8085
Eric Biederman90089602004-05-28 14:11:54 +00008086static int replace_misc_use(struct compile_state *state,
8087 struct triple *orig, struct triple *new, struct triple *use)
8088{
8089 struct triple **expr;
8090 int found;
8091 found = 0;
8092 expr = triple_misc(state, use, 0);
8093 for(;expr; expr = triple_misc(state, use, expr)) {
8094 if (*expr == orig) {
8095 *expr = new;
8096 found = 1;
8097 }
8098 }
8099 if (found) {
8100 unuse_triple(orig, use);
8101 use_triple(new, use);
8102 }
8103 return found;
8104}
8105
8106static int replace_targ_use(struct compile_state *state,
8107 struct triple *orig, struct triple *new, struct triple *use)
8108{
8109 struct triple **expr;
8110 int found;
8111 found = 0;
8112 expr = triple_targ(state, use, 0);
8113 for(;expr; expr = triple_targ(state, use, expr)) {
8114 if (*expr == orig) {
8115 *expr = new;
8116 found = 1;
8117 }
8118 }
8119 if (found) {
8120 unuse_triple(orig, use);
8121 use_triple(new, use);
8122 }
8123 return found;
8124}
8125
8126static void replace_use(struct compile_state *state,
8127 struct triple *orig, struct triple *new, struct triple *use)
8128{
8129 int found;
8130 found = 0;
8131 found |= replace_rhs_use(state, orig, new, use);
8132 found |= replace_lhs_use(state, orig, new, use);
8133 found |= replace_misc_use(state, orig, new, use);
8134 found |= replace_targ_use(state, orig, new, use);
8135 if (!found) {
8136 internal_error(state, use, "use without use");
8137 }
8138}
8139
Eric Biedermanb138ac82003-04-22 18:44:01 +00008140static void propogate_use(struct compile_state *state,
8141 struct triple *orig, struct triple *new)
8142{
8143 struct triple_set *user, *next;
8144 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008145 /* Careful replace_use modifies the use chain and
8146 * removes use. So we must get a copy of the next
8147 * entry early.
8148 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008149 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008150 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008151 }
8152 if (orig->use) {
8153 internal_error(state, orig, "used after propogate_use");
8154 }
8155}
8156
8157/*
8158 * Code generators
8159 * ===========================
8160 */
8161
Eric Biederman90089602004-05-28 14:11:54 +00008162static struct triple *mk_cast_expr(
8163 struct compile_state *state, struct type *type, struct triple *expr)
8164{
8165 struct triple *def;
8166 def = read_expr(state, expr);
8167 def = triple(state, OP_CONVERT, type, def, 0);
8168 return def;
8169}
8170
Eric Biedermanb138ac82003-04-22 18:44:01 +00008171static struct triple *mk_add_expr(
8172 struct compile_state *state, struct triple *left, struct triple *right)
8173{
8174 struct type *result_type;
8175 /* Put pointer operands on the left */
8176 if (is_pointer(right)) {
8177 struct triple *tmp;
8178 tmp = left;
8179 left = right;
8180 right = tmp;
8181 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008182 left = read_expr(state, left);
8183 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008184 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008185 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008186 struct type *ptr_math;
8187 int op;
8188 if (is_signed(right->type)) {
8189 ptr_math = &long_type;
8190 op = OP_SMUL;
8191 } else {
8192 ptr_math = &ulong_type;
8193 op = OP_UMUL;
8194 }
8195 if (!equiv_types(right->type, ptr_math)) {
8196 right = mk_cast_expr(state, ptr_math, right);
8197 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008198 right = triple(state, op, ptr_math, right,
8199 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008200 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008201 }
8202 return triple(state, OP_ADD, result_type, left, right);
8203}
8204
8205static struct triple *mk_sub_expr(
8206 struct compile_state *state, struct triple *left, struct triple *right)
8207{
8208 struct type *result_type;
8209 result_type = ptr_arithmetic_result(state, left, right);
8210 left = read_expr(state, left);
8211 right = read_expr(state, right);
8212 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008213 struct type *ptr_math;
8214 int op;
8215 if (is_signed(right->type)) {
8216 ptr_math = &long_type;
8217 op = OP_SMUL;
8218 } else {
8219 ptr_math = &ulong_type;
8220 op = OP_UMUL;
8221 }
8222 if (!equiv_types(right->type, ptr_math)) {
8223 right = mk_cast_expr(state, ptr_math, right);
8224 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008225 right = triple(state, op, ptr_math, right,
8226 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008227 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008228 }
8229 return triple(state, OP_SUB, result_type, left, right);
8230}
8231
8232static struct triple *mk_pre_inc_expr(
8233 struct compile_state *state, struct triple *def)
8234{
8235 struct triple *val;
8236 lvalue(state, def);
8237 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8238 return triple(state, OP_VAL, def->type,
8239 write_expr(state, def, val),
8240 val);
8241}
8242
8243static struct triple *mk_pre_dec_expr(
8244 struct compile_state *state, struct triple *def)
8245{
8246 struct triple *val;
8247 lvalue(state, def);
8248 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8249 return triple(state, OP_VAL, def->type,
8250 write_expr(state, def, val),
8251 val);
8252}
8253
8254static struct triple *mk_post_inc_expr(
8255 struct compile_state *state, struct triple *def)
8256{
8257 struct triple *val;
8258 lvalue(state, def);
8259 val = read_expr(state, def);
8260 return triple(state, OP_VAL, def->type,
8261 write_expr(state, def,
8262 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8263 , val);
8264}
8265
8266static struct triple *mk_post_dec_expr(
8267 struct compile_state *state, struct triple *def)
8268{
8269 struct triple *val;
8270 lvalue(state, def);
8271 val = read_expr(state, def);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008272 return triple(state, OP_VAL, def->type,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008273 write_expr(state, def,
8274 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8275 , val);
8276}
8277
8278static struct triple *mk_subscript_expr(
8279 struct compile_state *state, struct triple *left, struct triple *right)
8280{
8281 left = read_expr(state, left);
8282 right = read_expr(state, right);
8283 if (!is_pointer(left) && !is_pointer(right)) {
8284 error(state, left, "subscripted value is not a pointer");
8285 }
8286 return mk_deref_expr(state, mk_add_expr(state, left, right));
8287}
8288
Eric Biedermane058a1e2003-07-12 01:21:31 +00008289
Eric Biedermanb138ac82003-04-22 18:44:01 +00008290/*
8291 * Compile time evaluation
8292 * ===========================
8293 */
8294static int is_const(struct triple *ins)
8295{
8296 return IS_CONST_OP(ins->op);
8297}
8298
Eric Biederman83b991a2003-10-11 06:20:25 +00008299static int is_simple_const(struct triple *ins)
8300{
Eric Biederman90089602004-05-28 14:11:54 +00008301 /* Is this a constant that u.cval has the value.
8302 * Or equivalently is this a constant that read_const
8303 * works on.
Stefan Reinauer14e22772010-04-27 06:56:47 +00008304 * So far only OP_INTCONST qualifies.
Eric Biederman90089602004-05-28 14:11:54 +00008305 */
8306 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008307}
8308
Stefan Reinauer14e22772010-04-27 06:56:47 +00008309static int constants_equal(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008310 struct triple *left, struct triple *right)
8311{
8312 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008313 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8314 equal = 0;
8315 }
8316 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008317 equal = 0;
8318 }
8319 else if (left->op != right->op) {
8320 equal = 0;
8321 }
8322 else if (!equiv_types(left->type, right->type)) {
8323 equal = 0;
8324 }
8325 else {
8326 equal = 0;
8327 switch(left->op) {
8328 case OP_INTCONST:
8329 if (left->u.cval == right->u.cval) {
8330 equal = 1;
8331 }
8332 break;
8333 case OP_BLOBCONST:
8334 {
Eric Biederman90089602004-05-28 14:11:54 +00008335 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008336 lsize = size_of(state, left->type);
8337 rsize = size_of(state, right->type);
8338 if (lsize != rsize) {
8339 break;
8340 }
Eric Biederman90089602004-05-28 14:11:54 +00008341 bytes = bits_to_bytes(lsize);
8342 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008343 equal = 1;
8344 }
8345 break;
8346 }
8347 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008348 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008349 (left->u.cval == right->u.cval)) {
8350 equal = 1;
8351 }
8352 break;
8353 default:
8354 internal_error(state, left, "uknown constant type");
8355 break;
8356 }
8357 }
8358 return equal;
8359}
8360
8361static int is_zero(struct triple *ins)
8362{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008363 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008364}
8365
8366static int is_one(struct triple *ins)
8367{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008368 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008369}
8370
Stefan Reinauer50542a82007-10-24 11:14:14 +00008371#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008372static long_t bit_count(ulong_t value)
8373{
8374 int count;
8375 int i;
8376 count = 0;
8377 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8378 ulong_t mask;
8379 mask = 1;
8380 mask <<= i;
8381 if (value & mask) {
8382 count++;
8383 }
8384 }
8385 return count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008386
Eric Biederman530b5192003-07-01 10:05:30 +00008387}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008388#endif
8389
Eric Biedermanb138ac82003-04-22 18:44:01 +00008390static long_t bsr(ulong_t value)
8391{
8392 int i;
8393 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8394 ulong_t mask;
8395 mask = 1;
8396 mask <<= i;
8397 if (value & mask) {
8398 return i;
8399 }
8400 }
8401 return -1;
8402}
8403
8404static long_t bsf(ulong_t value)
8405{
8406 int i;
8407 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8408 ulong_t mask;
8409 mask = 1;
8410 mask <<= 1;
8411 if (value & mask) {
8412 return i;
8413 }
8414 }
8415 return -1;
8416}
8417
Eric Biedermancb364952004-11-15 10:46:44 +00008418static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008419{
8420 return bsr(value);
8421}
8422
8423static long_t tlog2(struct triple *ins)
8424{
Eric Biedermancb364952004-11-15 10:46:44 +00008425 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008426}
8427
8428static int is_pow2(struct triple *ins)
8429{
8430 ulong_t value, mask;
8431 long_t log;
8432 if (!is_const(ins)) {
8433 return 0;
8434 }
8435 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008436 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008437 if (log == -1) {
8438 return 0;
8439 }
8440 mask = 1;
8441 mask <<= log;
8442 return ((value & mask) == value);
8443}
8444
8445static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008446 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008447{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008448 switch(rhs->type->type &TYPE_MASK) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00008449 case TYPE_CHAR:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008450 case TYPE_SHORT:
8451 case TYPE_INT:
8452 case TYPE_LONG:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008453 case TYPE_UCHAR:
8454 case TYPE_USHORT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008455 case TYPE_UINT:
8456 case TYPE_ULONG:
8457 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008458 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008459 break;
8460 default:
Eric Biederman90089602004-05-28 14:11:54 +00008461 fprintf(state->errout, "type: ");
8462 name_of(state->errout, rhs->type);
8463 fprintf(state->errout, "\n");
8464 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008465 break;
8466 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008467 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008468 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008469 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008470 return rhs->u.cval;
8471}
8472
Eric Biederman5ade04a2003-10-22 04:03:46 +00008473static long_t read_sconst(struct compile_state *state,
8474 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008475{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008476 return (long_t)(rhs->u.cval);
8477}
8478
Eric Biederman5ade04a2003-10-22 04:03:46 +00008479int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8480{
8481 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008482 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008483 }
8484 return !is_zero(rhs);
8485}
8486
8487int const_eq(struct compile_state *state, struct triple *ins,
8488 struct triple *left, struct triple *right)
8489{
8490 int result;
8491 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008492 internal_warning(state, ins, "non const passed to const_eq");
8493 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008494 }
8495 else if (left == right) {
8496 result = 1;
8497 }
8498 else if (is_simple_const(left) && is_simple_const(right)) {
8499 ulong_t lval, rval;
8500 lval = read_const(state, ins, left);
8501 rval = read_const(state, ins, right);
8502 result = (lval == rval);
8503 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008504 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008505 (right->op == OP_ADDRCONST)) {
8506 result = (MISC(left, 0) == MISC(right, 0)) &&
8507 (left->u.cval == right->u.cval);
8508 }
8509 else {
Eric Biederman90089602004-05-28 14:11:54 +00008510 internal_warning(state, ins, "incomparable constants passed to const_eq");
8511 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008512 }
8513 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008514
Eric Biederman5ade04a2003-10-22 04:03:46 +00008515}
8516
8517int const_ucmp(struct compile_state *state, struct triple *ins,
8518 struct triple *left, struct triple *right)
8519{
8520 int result;
8521 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008522 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008523 result = -2;
8524 }
8525 else if (left == right) {
8526 result = 0;
8527 }
8528 else if (is_simple_const(left) && is_simple_const(right)) {
8529 ulong_t lval, rval;
8530 lval = read_const(state, ins, left);
8531 rval = read_const(state, ins, right);
8532 result = 0;
8533 if (lval > rval) {
8534 result = 1;
8535 } else if (rval > lval) {
8536 result = -1;
8537 }
8538 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008539 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008540 (right->op == OP_ADDRCONST) &&
8541 (MISC(left, 0) == MISC(right, 0))) {
8542 result = 0;
8543 if (left->u.cval > right->u.cval) {
8544 result = 1;
8545 } else if (left->u.cval < right->u.cval) {
8546 result = -1;
8547 }
8548 }
8549 else {
Eric Biederman90089602004-05-28 14:11:54 +00008550 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008551 result = -2;
8552 }
8553 return result;
8554}
8555
8556int const_scmp(struct compile_state *state, struct triple *ins,
8557 struct triple *left, struct triple *right)
8558{
8559 int result;
8560 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008561 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008562 result = -2;
8563 }
8564 else if (left == right) {
8565 result = 0;
8566 }
8567 else if (is_simple_const(left) && is_simple_const(right)) {
8568 long_t lval, rval;
8569 lval = read_sconst(state, ins, left);
8570 rval = read_sconst(state, ins, right);
8571 result = 0;
8572 if (lval > rval) {
8573 result = 1;
8574 } else if (rval > lval) {
8575 result = -1;
8576 }
8577 }
8578 else {
Eric Biederman90089602004-05-28 14:11:54 +00008579 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008580 result = -2;
8581 }
8582 return result;
8583}
8584
Eric Biedermanb138ac82003-04-22 18:44:01 +00008585static void unuse_rhs(struct compile_state *state, struct triple *ins)
8586{
8587 struct triple **expr;
8588 expr = triple_rhs(state, ins, 0);
8589 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008590 if (*expr) {
8591 unuse_triple(*expr, ins);
8592 *expr = 0;
8593 }
8594 }
8595}
8596
8597static void unuse_lhs(struct compile_state *state, struct triple *ins)
8598{
8599 struct triple **expr;
8600 expr = triple_lhs(state, ins, 0);
8601 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008602 unuse_triple(*expr, ins);
8603 *expr = 0;
8604 }
8605}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008606
Stefan Reinauer50542a82007-10-24 11:14:14 +00008607#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008608static void unuse_misc(struct compile_state *state, struct triple *ins)
8609{
8610 struct triple **expr;
8611 expr = triple_misc(state, ins, 0);
8612 for(;expr;expr = triple_misc(state, ins, expr)) {
8613 unuse_triple(*expr, ins);
8614 *expr = 0;
8615 }
8616}
8617
8618static void unuse_targ(struct compile_state *state, struct triple *ins)
8619{
8620 int i;
8621 struct triple **slot;
8622 slot = &TARG(ins, 0);
8623 for(i = 0; i < ins->targ; i++) {
8624 unuse_triple(slot[i], ins);
8625 slot[i] = 0;
8626 }
8627}
8628
Eric Biedermanb138ac82003-04-22 18:44:01 +00008629static void check_lhs(struct compile_state *state, struct triple *ins)
8630{
8631 struct triple **expr;
8632 expr = triple_lhs(state, ins, 0);
8633 for(;expr;expr = triple_lhs(state, ins, expr)) {
8634 internal_error(state, ins, "unexpected lhs");
8635 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008636
Eric Biedermanb138ac82003-04-22 18:44:01 +00008637}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008638#endif
Eric Biederman90089602004-05-28 14:11:54 +00008639
8640static void check_misc(struct compile_state *state, struct triple *ins)
8641{
8642 struct triple **expr;
8643 expr = triple_misc(state, ins, 0);
8644 for(;expr;expr = triple_misc(state, ins, expr)) {
8645 if (*expr) {
8646 internal_error(state, ins, "unexpected misc");
8647 }
8648 }
8649}
8650
Eric Biedermanb138ac82003-04-22 18:44:01 +00008651static void check_targ(struct compile_state *state, struct triple *ins)
8652{
8653 struct triple **expr;
8654 expr = triple_targ(state, ins, 0);
8655 for(;expr;expr = triple_targ(state, ins, expr)) {
8656 internal_error(state, ins, "unexpected targ");
8657 }
8658}
8659
8660static void wipe_ins(struct compile_state *state, struct triple *ins)
8661{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008662 /* Becareful which instructions you replace the wiped
8663 * instruction with, as there are not enough slots
8664 * in all instructions to hold all others.
8665 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008666 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008667 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008668 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008669 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008670 ins->lhs = 0;
8671 ins->rhs = 0;
8672 ins->misc = 0;
8673 ins->targ = 0;
8674}
8675
Stefan Reinauer50542a82007-10-24 11:14:14 +00008676#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008677static void wipe_branch(struct compile_state *state, struct triple *ins)
8678{
8679 /* Becareful which instructions you replace the wiped
8680 * instruction with, as there are not enough slots
8681 * in all instructions to hold all others.
8682 */
8683 unuse_rhs(state, ins);
8684 unuse_lhs(state, ins);
8685 unuse_misc(state, ins);
8686 unuse_targ(state, ins);
8687 ins->lhs = 0;
8688 ins->rhs = 0;
8689 ins->misc = 0;
8690 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008691}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008692#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008693
Stefan Reinauer14e22772010-04-27 06:56:47 +00008694static void mkcopy(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008695 struct triple *ins, struct triple *rhs)
8696{
Eric Biederman83b991a2003-10-11 06:20:25 +00008697 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008698 if (!equiv_types(ins->type, rhs->type)) {
8699 FILE *fp = state->errout;
8700 fprintf(fp, "src type: ");
8701 name_of(fp, rhs->type);
8702 fprintf(fp, "\ndst type: ");
8703 name_of(fp, ins->type);
8704 fprintf(fp, "\n");
8705 internal_error(state, ins, "mkcopy type mismatch");
8706 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008707 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008708 wipe_ins(state, ins);
8709 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008710 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008711 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008712 RHS(ins, 0) = rhs;
8713 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008714}
8715
Stefan Reinauer14e22772010-04-27 06:56:47 +00008716static void mkconst(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008717 struct triple *ins, ulong_t value)
8718{
8719 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008720 fprintf(state->errout, "type: ");
8721 name_of(state->errout, ins->type);
8722 fprintf(state->errout, "\n");
8723 internal_error(state, ins, "unknown type to make constant value: %ld",
8724 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008725 }
8726 wipe_ins(state, ins);
8727 ins->op = OP_INTCONST;
8728 ins->u.cval = value;
8729}
8730
8731static void mkaddr_const(struct compile_state *state,
8732 struct triple *ins, struct triple *sdecl, ulong_t value)
8733{
Eric Biederman90089602004-05-28 14:11:54 +00008734 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008735 internal_error(state, ins, "bad base for addrconst");
8736 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008737 wipe_ins(state, ins);
8738 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008739 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008740 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008741 ins->u.cval = value;
8742 use_triple(sdecl, ins);
8743}
8744
Eric Biederman90089602004-05-28 14:11:54 +00008745#if DEBUG_DECOMPOSE_PRINT_TUPLES
Stefan Reinauer14e22772010-04-27 06:56:47 +00008746static void print_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008747 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008748{
Eric Biederman90089602004-05-28 14:11:54 +00008749 FILE *fp = state->dbgout;
8750 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8751 name_of(fp, tuple->type);
8752 if (tuple->lhs > 0) {
8753 fprintf(fp, " lhs: ");
8754 name_of(fp, LHS(tuple, 0)->type);
8755 }
8756 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +00008757
Eric Biederman90089602004-05-28 14:11:54 +00008758}
8759#endif
8760
Stefan Reinauer14e22772010-04-27 06:56:47 +00008761static struct triple *decompose_with_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008762 struct triple *ins, struct triple *tuple)
8763{
8764 struct triple *next;
8765 next = ins->next;
8766 flatten(state, next, tuple);
8767#if DEBUG_DECOMPOSE_PRINT_TUPLES
8768 print_tuple(state, ins, tuple);
8769#endif
8770
8771 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8772 struct triple *tmp;
8773 if (tuple->lhs != 1) {
8774 internal_error(state, tuple, "plain type in multiple registers?");
8775 }
8776 tmp = LHS(tuple, 0);
8777 release_triple(state, tuple);
8778 tuple = tmp;
8779 }
8780
8781 propogate_use(state, ins, tuple);
8782 release_triple(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008783
Eric Biederman90089602004-05-28 14:11:54 +00008784 return next;
8785}
8786
8787static struct triple *decompose_unknownval(struct compile_state *state,
8788 struct triple *ins)
8789{
8790 struct triple *tuple;
8791 ulong_t i;
8792
8793#if DEBUG_DECOMPOSE_HIRES
8794 FILE *fp = state->dbgout;
8795 fprintf(fp, "unknown type: ");
8796 name_of(fp, ins->type);
8797 fprintf(fp, "\n");
8798#endif
8799
8800 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008801 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008802 ins->occurance);
8803
8804 for(i = 0; i < tuple->lhs; i++) {
8805 struct type *piece_type;
8806 struct triple *unknown;
8807
8808 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8809 get_occurance(tuple->occurance);
8810 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8811 tuple->occurance);
8812 LHS(tuple, i) = unknown;
8813 }
8814 return decompose_with_tuple(state, ins, tuple);
8815}
8816
8817
Stefan Reinauer14e22772010-04-27 06:56:47 +00008818static struct triple *decompose_read(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008819 struct triple *ins)
8820{
8821 struct triple *tuple, *lval;
8822 ulong_t i;
8823
8824 lval = RHS(ins, 0);
8825
8826 if (lval->op == OP_PIECE) {
8827 return ins->next;
8828 }
8829 get_occurance(ins->occurance);
8830 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8831 ins->occurance);
8832
8833 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008834 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
Eric Biederman90089602004-05-28 14:11:54 +00008835 {
8836 internal_error(state, ins, "lhs size inconsistency?");
8837 }
8838 for(i = 0; i < tuple->lhs; i++) {
8839 struct triple *piece, *read, *bitref;
8840 if ((i != 0) || !triple_is_def(state, lval)) {
8841 piece = LHS(lval, i);
8842 } else {
8843 piece = lval;
8844 }
8845
8846 /* See if the piece is really a bitref */
8847 bitref = 0;
8848 if (piece->op == OP_BITREF) {
8849 bitref = piece;
8850 piece = RHS(bitref, 0);
8851 }
8852
8853 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008854 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008855 tuple->occurance);
8856 RHS(read, 0) = piece;
8857
8858 if (bitref) {
8859 struct triple *extract;
8860 int op;
8861 if (is_signed(bitref->type->left)) {
8862 op = OP_SEXTRACT;
8863 } else {
8864 op = OP_UEXTRACT;
8865 }
8866 get_occurance(tuple->occurance);
8867 extract = alloc_triple(state, op, bitref->type, -1, -1,
8868 tuple->occurance);
8869 RHS(extract, 0) = read;
8870 extract->u.bitfield.size = bitref->u.bitfield.size;
8871 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8872
8873 read = extract;
8874 }
8875
8876 LHS(tuple, i) = read;
8877 }
8878 return decompose_with_tuple(state, ins, tuple);
8879}
8880
Stefan Reinauer14e22772010-04-27 06:56:47 +00008881static struct triple *decompose_write(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008882 struct triple *ins)
8883{
8884 struct triple *tuple, *lval, *val;
8885 ulong_t i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008886
Eric Biederman90089602004-05-28 14:11:54 +00008887 lval = MISC(ins, 0);
8888 val = RHS(ins, 0);
8889 get_occurance(ins->occurance);
8890 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8891 ins->occurance);
8892
8893 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008894 (!triple_is_def(state, lval) || tuple->lhs != 1))
Eric Biederman90089602004-05-28 14:11:54 +00008895 {
8896 internal_error(state, ins, "lhs size inconsistency?");
8897 }
8898 for(i = 0; i < tuple->lhs; i++) {
8899 struct triple *piece, *write, *pval, *bitref;
8900 if ((i != 0) || !triple_is_def(state, lval)) {
8901 piece = LHS(lval, i);
8902 } else {
8903 piece = lval;
8904 }
8905 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8906 pval = val;
8907 }
8908 else {
8909 if (i > val->lhs) {
8910 internal_error(state, ins, "lhs size inconsistency?");
8911 }
8912 pval = LHS(val, i);
8913 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008914
Eric Biederman90089602004-05-28 14:11:54 +00008915 /* See if the piece is really a bitref */
8916 bitref = 0;
8917 if (piece->op == OP_BITREF) {
8918 struct triple *read, *deposit;
8919 bitref = piece;
8920 piece = RHS(bitref, 0);
8921
8922 /* Read the destination register */
8923 get_occurance(tuple->occurance);
8924 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8925 tuple->occurance);
8926 RHS(read, 0) = piece;
8927
8928 /* Deposit the new bitfield value */
8929 get_occurance(tuple->occurance);
8930 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8931 tuple->occurance);
8932 RHS(deposit, 0) = read;
8933 RHS(deposit, 1) = pval;
8934 deposit->u.bitfield.size = bitref->u.bitfield.size;
8935 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8936
8937 /* Now write the newly generated value */
8938 pval = deposit;
8939 }
8940
8941 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008942 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008943 tuple->occurance);
8944 MISC(write, 0) = piece;
8945 RHS(write, 0) = pval;
8946 LHS(tuple, i) = write;
8947 }
8948 return decompose_with_tuple(state, ins, tuple);
8949}
8950
8951struct decompose_load_info {
8952 struct occurance *occurance;
8953 struct triple *lval;
8954 struct triple *tuple;
8955};
8956static void decompose_load_cb(struct compile_state *state,
8957 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8958{
8959 struct decompose_load_info *info = arg;
8960 struct triple *load;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008961
Eric Biederman90089602004-05-28 14:11:54 +00008962 if (reg_offset > info->tuple->lhs) {
8963 internal_error(state, info->tuple, "lhs to small?");
8964 }
8965 get_occurance(info->occurance);
8966 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8967 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8968 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8969}
8970
Stefan Reinauer14e22772010-04-27 06:56:47 +00008971static struct triple *decompose_load(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008972 struct triple *ins)
8973{
8974 struct triple *tuple;
8975 struct decompose_load_info info;
8976
8977 if (!is_compound_type(ins->type)) {
8978 return ins->next;
8979 }
8980 get_occurance(ins->occurance);
8981 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8982 ins->occurance);
8983
8984 info.occurance = ins->occurance;
8985 info.lval = RHS(ins, 0);
8986 info.tuple = tuple;
8987 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
8988
8989 return decompose_with_tuple(state, ins, tuple);
8990}
8991
8992
8993struct decompose_store_info {
8994 struct occurance *occurance;
8995 struct triple *lval;
8996 struct triple *val;
8997 struct triple *tuple;
8998};
8999static void decompose_store_cb(struct compile_state *state,
9000 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9001{
9002 struct decompose_store_info *info = arg;
9003 struct triple *store;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009004
Eric Biederman90089602004-05-28 14:11:54 +00009005 if (reg_offset > info->tuple->lhs) {
9006 internal_error(state, info->tuple, "lhs to small?");
9007 }
9008 get_occurance(info->occurance);
9009 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9010 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9011 RHS(store, 1) = LHS(info->val, reg_offset);
9012 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9013}
9014
Stefan Reinauer14e22772010-04-27 06:56:47 +00009015static struct triple *decompose_store(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009016 struct triple *ins)
9017{
9018 struct triple *tuple;
9019 struct decompose_store_info info;
9020
9021 if (!is_compound_type(ins->type)) {
9022 return ins->next;
9023 }
9024 get_occurance(ins->occurance);
9025 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9026 ins->occurance);
9027
9028 info.occurance = ins->occurance;
9029 info.lval = RHS(ins, 0);
9030 info.val = RHS(ins, 1);
9031 info.tuple = tuple;
9032 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9033
9034 return decompose_with_tuple(state, ins, tuple);
9035}
9036
Stefan Reinauer14e22772010-04-27 06:56:47 +00009037static struct triple *decompose_dot(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009038 struct triple *ins)
9039{
9040 struct triple *tuple, *lval;
9041 struct type *type;
9042 size_t reg_offset;
9043 int i, idx;
9044
9045 lval = MISC(ins, 0);
9046 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9047 idx = reg_offset/REG_SIZEOF_REG;
9048 type = field_type(state, lval->type, ins->u.field);
9049#if DEBUG_DECOMPOSE_HIRES
9050 {
9051 FILE *fp = state->dbgout;
9052 fprintf(fp, "field type: ");
9053 name_of(fp, type);
9054 fprintf(fp, "\n");
9055 }
9056#endif
9057
9058 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009059 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009060 ins->occurance);
9061
9062 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9063 (tuple->lhs != 1))
9064 {
9065 internal_error(state, ins, "multi register bitfield?");
9066 }
9067
9068 for(i = 0; i < tuple->lhs; i++, idx++) {
9069 struct triple *piece;
9070 if (!triple_is_def(state, lval)) {
9071 if (idx > lval->lhs) {
9072 internal_error(state, ins, "inconsistent lhs count");
9073 }
9074 piece = LHS(lval, idx);
9075 } else {
9076 if (idx != 0) {
9077 internal_error(state, ins, "bad reg_offset into def");
9078 }
9079 if (i != 0) {
9080 internal_error(state, ins, "bad reg count from def");
9081 }
9082 piece = lval;
9083 }
9084
9085 /* Remember the offset of the bitfield */
9086 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9087 get_occurance(ins->occurance);
9088 piece = build_triple(state, OP_BITREF, type, piece, 0,
9089 ins->occurance);
9090 piece->u.bitfield.size = size_of(state, type);
9091 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9092 }
9093 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00009094 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +00009095 "request for a nonbitfield sub register?");
9096 }
9097
9098 LHS(tuple, i) = piece;
9099 }
9100
9101 return decompose_with_tuple(state, ins, tuple);
9102}
9103
Stefan Reinauer14e22772010-04-27 06:56:47 +00009104static struct triple *decompose_index(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009105 struct triple *ins)
9106{
9107 struct triple *tuple, *lval;
9108 struct type *type;
9109 int i, idx;
9110
9111 lval = MISC(ins, 0);
9112 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9113 type = index_type(state, lval->type, ins->u.cval);
9114#if DEBUG_DECOMPOSE_HIRES
9115{
9116 FILE *fp = state->dbgout;
9117 fprintf(fp, "index type: ");
9118 name_of(fp, type);
9119 fprintf(fp, "\n");
9120}
9121#endif
9122
9123 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009124 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009125 ins->occurance);
9126
9127 for(i = 0; i < tuple->lhs; i++, idx++) {
9128 struct triple *piece;
9129 if (!triple_is_def(state, lval)) {
9130 if (idx > lval->lhs) {
9131 internal_error(state, ins, "inconsistent lhs count");
9132 }
9133 piece = LHS(lval, idx);
9134 } else {
9135 if (idx != 0) {
9136 internal_error(state, ins, "bad reg_offset into def");
9137 }
9138 if (i != 0) {
9139 internal_error(state, ins, "bad reg count from def");
9140 }
9141 piece = lval;
9142 }
9143 LHS(tuple, i) = piece;
9144 }
9145
9146 return decompose_with_tuple(state, ins, tuple);
9147}
9148
9149static void decompose_compound_types(struct compile_state *state)
9150{
9151 struct triple *ins, *next, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00009152 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009153 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009154
9155 /* Pass one expand compound values into pseudo registers.
9156 */
9157 next = first;
9158 do {
9159 ins = next;
9160 next = ins->next;
9161 switch(ins->op) {
9162 case OP_UNKNOWNVAL:
9163 next = decompose_unknownval(state, ins);
9164 break;
9165
9166 case OP_READ:
9167 next = decompose_read(state, ins);
9168 break;
9169
9170 case OP_WRITE:
9171 next = decompose_write(state, ins);
9172 break;
9173
9174
9175 /* Be very careful with the load/store logic. These
9176 * operations must convert from the in register layout
9177 * to the in memory layout, which is nontrivial.
9178 */
9179 case OP_LOAD:
9180 next = decompose_load(state, ins);
9181 break;
9182 case OP_STORE:
9183 next = decompose_store(state, ins);
9184 break;
9185
9186 case OP_DOT:
9187 next = decompose_dot(state, ins);
9188 break;
9189 case OP_INDEX:
9190 next = decompose_index(state, ins);
9191 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009192
Eric Biederman90089602004-05-28 14:11:54 +00009193 }
9194#if DEBUG_DECOMPOSE_HIRES
9195 fprintf(fp, "decompose next: %p \n", next);
9196 fflush(fp);
9197 fprintf(fp, "next->op: %d %s\n",
9198 next->op, tops(next->op));
9199 /* High resolution debugging mode */
9200 print_triples(state);
9201#endif
9202 } while (next != first);
9203
9204 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009205 */
9206 ins = first;
9207 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009208 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009209 if (ins->op == OP_TUPLE) {
9210 if (ins->use) {
9211 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009212 }
Eric Biederman90089602004-05-28 14:11:54 +00009213 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009214 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009215 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00009216 }
Eric Biederman90089602004-05-28 14:11:54 +00009217 ins = next;
9218 } while(ins != first);
9219 ins = first;
9220 do {
9221 next = ins->next;
9222 if (ins->op == OP_BITREF) {
9223 if (ins->use) {
9224 internal_error(state, ins, "bitref used");
Stefan Reinauer14e22772010-04-27 06:56:47 +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 }
9229 }
9230 ins = next;
9231 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009232
Eric Biederman0babc1c2003-05-09 02:39:00 +00009233 /* Pass three verify the state and set ->id to 0.
9234 */
Eric Biederman90089602004-05-28 14:11:54 +00009235 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009236 do {
Eric Biederman90089602004-05-28 14:11:54 +00009237 ins = next;
9238 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009239 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009240 if (triple_stores_block(state, ins)) {
9241 ins->u.block = 0;
9242 }
9243 if (triple_is_def(state, ins)) {
9244 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9245 internal_error(state, ins, "multi register value remains?");
9246 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009247 }
9248 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009249 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009250 }
Eric Biederman90089602004-05-28 14:11:54 +00009251 if (ins->op == OP_INDEX) {
9252 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009253 }
Eric Biederman90089602004-05-28 14:11:54 +00009254 if (ins->op == OP_BITREF) {
9255 internal_error(state, ins, "OP_BITREF remains?");
9256 }
9257 if (ins->op == OP_TUPLE) {
9258 internal_error(state, ins, "OP_TUPLE remains?");
9259 }
9260 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009261}
9262
Eric Biedermanb138ac82003-04-22 18:44:01 +00009263/* For those operations that cannot be simplified */
9264static void simplify_noop(struct compile_state *state, struct triple *ins)
9265{
9266 return;
9267}
9268
9269static void simplify_smul(struct compile_state *state, struct triple *ins)
9270{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009271 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009272 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009273 tmp = RHS(ins, 0);
9274 RHS(ins, 0) = RHS(ins, 1);
9275 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009276 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009277 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009278 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009279 left = read_sconst(state, ins, RHS(ins, 0));
9280 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009281 mkconst(state, ins, left * right);
9282 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009283 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009284 mkconst(state, ins, 0);
9285 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009286 else if (is_one(RHS(ins, 1))) {
9287 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009288 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009289 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009290 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009291 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009292 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009293 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009294 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009295 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009296 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009297 }
9298}
9299
9300static void simplify_umul(struct compile_state *state, struct triple *ins)
9301{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009302 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009303 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009304 tmp = RHS(ins, 0);
9305 RHS(ins, 0) = RHS(ins, 1);
9306 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009307 }
Eric Biederman90089602004-05-28 14:11:54 +00009308 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009309 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009310 left = read_const(state, ins, RHS(ins, 0));
9311 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009312 mkconst(state, ins, left * right);
9313 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009314 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009315 mkconst(state, ins, 0);
9316 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009317 else if (is_one(RHS(ins, 1))) {
9318 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009319 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009320 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009321 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009322 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009323 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009324 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009325 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009326 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009327 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009328 }
9329}
9330
9331static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9332{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009333 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009334 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009335 left = read_sconst(state, ins, RHS(ins, 0));
9336 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009337 mkconst(state, ins, left / right);
9338 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009339 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009340 mkconst(state, ins, 0);
9341 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009342 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009343 error(state, ins, "division by zero");
9344 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009345 else if (is_one(RHS(ins, 1))) {
9346 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009347 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009348 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009349 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009350 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009351 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009352 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009353 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009354 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009355 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009356 }
9357}
9358
9359static void simplify_udiv(struct compile_state *state, struct triple *ins)
9360{
Eric Biederman90089602004-05-28 14:11:54 +00009361 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009362 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009363 left = read_const(state, ins, RHS(ins, 0));
9364 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009365 mkconst(state, ins, left / right);
9366 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009367 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009368 mkconst(state, ins, 0);
9369 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009370 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009371 error(state, ins, "division by zero");
9372 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009373 else if (is_one(RHS(ins, 1))) {
9374 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009375 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009376 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009377 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009378 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009379 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009380 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009381 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009382 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009383 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009384 }
9385}
9386
9387static void simplify_smod(struct compile_state *state, struct triple *ins)
9388{
Eric Biederman90089602004-05-28 14:11:54 +00009389 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009390 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009391 left = read_const(state, ins, RHS(ins, 0));
9392 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009393 mkconst(state, ins, left % right);
9394 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009395 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396 mkconst(state, ins, 0);
9397 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009398 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009399 error(state, ins, "division by zero");
9400 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009401 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 mkconst(state, ins, 0);
9403 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009404 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009405 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009406 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009407 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009408 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009409 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009410 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009411 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009412 }
9413}
Eric Biederman83b991a2003-10-11 06:20:25 +00009414
Eric Biedermanb138ac82003-04-22 18:44:01 +00009415static void simplify_umod(struct compile_state *state, struct triple *ins)
9416{
Eric Biederman90089602004-05-28 14:11:54 +00009417 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009418 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009419 left = read_const(state, ins, RHS(ins, 0));
9420 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009421 mkconst(state, ins, left % right);
9422 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009423 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009424 mkconst(state, ins, 0);
9425 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009426 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009427 error(state, ins, "division by zero");
9428 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009429 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 mkconst(state, ins, 0);
9431 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009432 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009433 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009434 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009435 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009436 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009437 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009438 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009439 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009440 }
9441}
9442
9443static void simplify_add(struct compile_state *state, struct triple *ins)
9444{
9445 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009446 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009447 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009448 tmp = RHS(ins, 0);
9449 RHS(ins, 0) = RHS(ins, 1);
9450 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009451 }
Eric Biederman90089602004-05-28 14:11:54 +00009452 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009453 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009454 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009455 left = read_const(state, ins, RHS(ins, 0));
9456 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009457 mkconst(state, ins, left + right);
9458 }
Eric Biederman530b5192003-07-01 10:05:30 +00009459 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009460 struct triple *sdecl;
9461 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009462 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009463 left = RHS(ins, 0)->u.cval;
9464 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009465 mkaddr_const(state, ins, sdecl, left + right);
9466 }
Eric Biederman530b5192003-07-01 10:05:30 +00009467 else {
9468 internal_warning(state, ins, "Optimize me!");
9469 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009470 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009471 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009473 tmp = RHS(ins, 1);
9474 RHS(ins, 1) = RHS(ins, 0);
9475 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009476 }
9477}
9478
9479static void simplify_sub(struct compile_state *state, struct triple *ins)
9480{
Eric Biederman90089602004-05-28 14:11:54 +00009481 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009482 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009483 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009484 left = read_const(state, ins, RHS(ins, 0));
9485 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009486 mkconst(state, ins, left - right);
9487 }
Eric Biederman530b5192003-07-01 10:05:30 +00009488 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009489 struct triple *sdecl;
9490 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009491 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009492 left = RHS(ins, 0)->u.cval;
9493 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009494 mkaddr_const(state, ins, sdecl, left - right);
9495 }
Eric Biederman530b5192003-07-01 10:05:30 +00009496 else {
9497 internal_warning(state, ins, "Optimize me!");
9498 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009499 }
9500}
9501
9502static void simplify_sl(struct compile_state *state, struct triple *ins)
9503{
Eric Biederman90089602004-05-28 14:11:54 +00009504 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009505 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009506 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009507 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009508 warning(state, ins, "left shift count >= width of type");
9509 }
9510 }
Eric Biederman90089602004-05-28 14:11:54 +00009511 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009512 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009513 left = read_const(state, ins, RHS(ins, 0));
9514 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009515 mkconst(state, ins, left << right);
9516 }
9517}
9518
9519static void simplify_usr(struct compile_state *state, struct triple *ins)
9520{
Eric Biederman90089602004-05-28 14:11:54 +00009521 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009522 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009523 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009524 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009525 warning(state, ins, "right shift count >= width of type");
9526 }
9527 }
Eric Biederman90089602004-05-28 14:11:54 +00009528 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009529 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009530 left = read_const(state, ins, RHS(ins, 0));
9531 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009532 mkconst(state, ins, left >> right);
9533 }
9534}
9535
9536static void simplify_ssr(struct compile_state *state, struct triple *ins)
9537{
Eric Biederman90089602004-05-28 14:11:54 +00009538 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009539 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009540 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009541 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009542 warning(state, ins, "right shift count >= width of type");
9543 }
9544 }
Eric Biederman90089602004-05-28 14:11:54 +00009545 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009546 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009547 left = read_sconst(state, ins, RHS(ins, 0));
9548 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009549 mkconst(state, ins, left >> right);
9550 }
9551}
9552
9553static void simplify_and(struct compile_state *state, struct triple *ins)
9554{
Eric Biederman90089602004-05-28 14:11:54 +00009555 struct triple *left, *right;
9556 left = RHS(ins, 0);
9557 right = RHS(ins, 1);
9558
9559 if (is_simple_const(left) && is_simple_const(right)) {
9560 ulong_t lval, rval;
9561 lval = read_const(state, ins, left);
9562 rval = read_const(state, ins, right);
9563 mkconst(state, ins, lval & rval);
9564 }
9565 else if (is_zero(right) || is_zero(left)) {
9566 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009567 }
9568}
9569
9570static void simplify_or(struct compile_state *state, struct triple *ins)
9571{
Eric Biederman90089602004-05-28 14:11:54 +00009572 struct triple *left, *right;
9573 left = RHS(ins, 0);
9574 right = RHS(ins, 1);
9575
9576 if (is_simple_const(left) && is_simple_const(right)) {
9577 ulong_t lval, rval;
9578 lval = read_const(state, ins, left);
9579 rval = read_const(state, ins, right);
9580 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009581 }
Eric Biederman90089602004-05-28 14:11:54 +00009582#if 0 /* I need to handle type mismatches here... */
9583 else if (is_zero(right)) {
9584 mkcopy(state, ins, left);
9585 }
9586 else if (is_zero(left)) {
9587 mkcopy(state, ins, right);
9588 }
9589#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009590}
9591
9592static void simplify_xor(struct compile_state *state, struct triple *ins)
9593{
Eric Biederman90089602004-05-28 14:11:54 +00009594 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009595 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009596 left = read_const(state, ins, RHS(ins, 0));
9597 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009598 mkconst(state, ins, left ^ right);
9599 }
9600}
9601
9602static void simplify_pos(struct compile_state *state, struct triple *ins)
9603{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009604 if (is_const(RHS(ins, 0))) {
9605 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009606 }
9607 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009608 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009609 }
9610}
9611
9612static void simplify_neg(struct compile_state *state, struct triple *ins)
9613{
Eric Biederman90089602004-05-28 14:11:54 +00009614 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009615 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009616 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009617 mkconst(state, ins, -left);
9618 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009619 else if (RHS(ins, 0)->op == OP_NEG) {
9620 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009621 }
9622}
9623
9624static void simplify_invert(struct compile_state *state, struct triple *ins)
9625{
Eric Biederman90089602004-05-28 14:11:54 +00009626 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009627 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009628 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009629 mkconst(state, ins, ~left);
9630 }
9631}
9632
9633static void simplify_eq(struct compile_state *state, struct triple *ins)
9634{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009635 struct triple *left, *right;
9636 left = RHS(ins, 0);
9637 right = RHS(ins, 1);
9638
9639 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009640 int val;
9641 val = const_eq(state, ins, left, right);
9642 if (val >= 0) {
9643 mkconst(state, ins, val == 1);
9644 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009645 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009646 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009647 mkconst(state, ins, 1);
9648 }
9649}
9650
9651static void simplify_noteq(struct compile_state *state, struct triple *ins)
9652{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009653 struct triple *left, *right;
9654 left = RHS(ins, 0);
9655 right = RHS(ins, 1);
9656
9657 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009658 int val;
9659 val = const_eq(state, ins, left, right);
9660 if (val >= 0) {
9661 mkconst(state, ins, val != 1);
9662 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009663 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009664 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009665 mkconst(state, ins, 0);
9666 }
9667}
9668
9669static void simplify_sless(struct compile_state *state, struct triple *ins)
9670{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009671 struct triple *left, *right;
9672 left = RHS(ins, 0);
9673 right = RHS(ins, 1);
9674
9675 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009676 int val;
9677 val = const_scmp(state, ins, left, right);
9678 if ((val >= -1) && (val <= 1)) {
9679 mkconst(state, ins, val < 0);
9680 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009681 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009682 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009683 mkconst(state, ins, 0);
9684 }
9685}
9686
9687static void simplify_uless(struct compile_state *state, struct triple *ins)
9688{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009689 struct triple *left, *right;
9690 left = RHS(ins, 0);
9691 right = RHS(ins, 1);
9692
9693 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009694 int val;
9695 val = const_ucmp(state, ins, left, right);
9696 if ((val >= -1) && (val <= 1)) {
9697 mkconst(state, ins, val < 0);
9698 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009699 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009700 else if (is_zero(right)) {
9701 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009702 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009703 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009704 mkconst(state, ins, 0);
9705 }
9706}
9707
9708static void simplify_smore(struct compile_state *state, struct triple *ins)
9709{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009710 struct triple *left, *right;
9711 left = RHS(ins, 0);
9712 right = RHS(ins, 1);
9713
9714 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009715 int val;
9716 val = const_scmp(state, ins, left, right);
9717 if ((val >= -1) && (val <= 1)) {
9718 mkconst(state, ins, val > 0);
9719 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009720 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009721 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009722 mkconst(state, ins, 0);
9723 }
9724}
9725
9726static void simplify_umore(struct compile_state *state, struct triple *ins)
9727{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009728 struct triple *left, *right;
9729 left = RHS(ins, 0);
9730 right = RHS(ins, 1);
9731
9732 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009733 int val;
9734 val = const_ucmp(state, ins, left, right);
9735 if ((val >= -1) && (val <= 1)) {
9736 mkconst(state, ins, val > 0);
9737 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009738 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009739 else if (is_zero(left)) {
9740 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009741 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009742 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009743 mkconst(state, ins, 0);
9744 }
9745}
9746
9747
9748static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9749{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009750 struct triple *left, *right;
9751 left = RHS(ins, 0);
9752 right = RHS(ins, 1);
9753
9754 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009755 int val;
9756 val = const_scmp(state, ins, left, right);
9757 if ((val >= -1) && (val <= 1)) {
9758 mkconst(state, ins, val <= 0);
9759 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009760 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009761 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009762 mkconst(state, ins, 1);
9763 }
9764}
9765
9766static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9767{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009768 struct triple *left, *right;
9769 left = RHS(ins, 0);
9770 right = RHS(ins, 1);
9771
9772 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009773 int val;
9774 val = const_ucmp(state, ins, left, right);
9775 if ((val >= -1) && (val <= 1)) {
9776 mkconst(state, ins, val <= 0);
9777 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009778 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009779 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009780 mkconst(state, ins, 1);
9781 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009782 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009783 mkconst(state, ins, 1);
9784 }
9785}
9786
9787static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9788{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009789 struct triple *left, *right;
9790 left = RHS(ins, 0);
9791 right = RHS(ins, 1);
9792
9793 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009794 int val;
9795 val = const_scmp(state, ins, left, right);
9796 if ((val >= -1) && (val <= 1)) {
9797 mkconst(state, ins, val >= 0);
9798 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009799 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009800 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009801 mkconst(state, ins, 1);
9802 }
9803}
9804
9805static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9806{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009807 struct triple *left, *right;
9808 left = RHS(ins, 0);
9809 right = RHS(ins, 1);
9810
9811 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009812 int val;
9813 val = const_ucmp(state, ins, left, right);
9814 if ((val >= -1) && (val <= 1)) {
9815 mkconst(state, ins, val >= 0);
9816 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009817 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009818 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009819 mkconst(state, ins, 1);
9820 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009821 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009822 mkconst(state, ins, 1);
9823 }
9824}
9825
9826static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9827{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009828 struct triple *rhs;
9829 rhs = RHS(ins, 0);
9830
9831 if (is_const(rhs)) {
9832 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009833 }
9834 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009835 else if ((rhs->use) &&
9836 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009837 int need_copy = 1;
9838 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009839 switch(rhs->op) {
9840 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9841 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9842 case OP_EQ: rhs->op = OP_NOTEQ; break;
9843 case OP_NOTEQ: rhs->op = OP_EQ; break;
9844 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9845 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9846 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9847 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9848 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9849 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9850 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9851 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009852 default:
9853 need_copy = 0;
9854 break;
9855 }
9856 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009857 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009858 }
9859 }
9860}
9861
9862static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9863{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009864 struct triple *rhs;
9865 rhs = RHS(ins, 0);
9866
9867 if (is_const(rhs)) {
9868 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009869 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009870 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009871 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9872 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9873 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009874 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009875 }
9876
9877}
9878
Eric Biederman90089602004-05-28 14:11:54 +00009879static void simplify_load(struct compile_state *state, struct triple *ins)
9880{
9881 struct triple *addr, *sdecl, *blob;
9882
9883 /* If I am doing a load with a constant pointer from a constant
9884 * table get the value.
9885 */
9886 addr = RHS(ins, 0);
9887 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9888 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9889 (blob->op == OP_BLOBCONST)) {
9890 unsigned char buffer[SIZEOF_WORD];
9891 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009892 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009893 ulong_t val;
9894 reg_size = reg_size_of(state, ins->type);
9895 if (reg_size > REG_SIZEOF_REG) {
9896 internal_error(state, ins, "load size greater than register");
9897 }
9898 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009899 end = blob->u.blob;
9900 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009901 src = blob->u.blob;
9902 src += addr->u.cval;
9903
Eric Biederman7dea9552004-06-29 05:38:37 +00009904 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009905 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009906 }
9907
Eric Biederman90089602004-05-28 14:11:54 +00009908 memset(buffer, 0, sizeof(buffer));
9909 memcpy(buffer, src, bits_to_bytes(mem_size));
9910
9911 switch(mem_size) {
9912 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9913 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9914 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9915 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9916 default:
9917 internal_error(state, ins, "mem_size: %d not handled",
9918 mem_size);
9919 val = 0;
9920 break;
9921 }
9922 mkconst(state, ins, val);
9923 }
9924}
9925
9926static void simplify_uextract(struct compile_state *state, struct triple *ins)
9927{
9928 if (is_simple_const(RHS(ins, 0))) {
9929 ulong_t val;
9930 ulong_t mask;
9931 val = read_const(state, ins, RHS(ins, 0));
9932 mask = 1;
9933 mask <<= ins->u.bitfield.size;
9934 mask -= 1;
9935 val >>= ins->u.bitfield.offset;
9936 val &= mask;
9937 mkconst(state, ins, val);
9938 }
9939}
9940
9941static void simplify_sextract(struct compile_state *state, struct triple *ins)
9942{
9943 if (is_simple_const(RHS(ins, 0))) {
9944 ulong_t val;
9945 ulong_t mask;
9946 long_t sval;
9947 val = read_const(state, ins, RHS(ins, 0));
9948 mask = 1;
9949 mask <<= ins->u.bitfield.size;
9950 mask -= 1;
9951 val >>= ins->u.bitfield.offset;
9952 val &= mask;
9953 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9954 sval = val;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009955 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
Eric Biederman90089602004-05-28 14:11:54 +00009956 mkconst(state, ins, sval);
9957 }
9958}
9959
9960static void simplify_deposit(struct compile_state *state, struct triple *ins)
9961{
9962 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9963 ulong_t targ, val;
9964 ulong_t mask;
9965 targ = read_const(state, ins, RHS(ins, 0));
9966 val = read_const(state, ins, RHS(ins, 1));
9967 mask = 1;
9968 mask <<= ins->u.bitfield.size;
9969 mask -= 1;
9970 mask <<= ins->u.bitfield.offset;
9971 targ &= ~mask;
9972 val <<= ins->u.bitfield.offset;
9973 val &= mask;
9974 targ |= val;
9975 mkconst(state, ins, targ);
9976 }
9977}
9978
Eric Biedermanb138ac82003-04-22 18:44:01 +00009979static void simplify_copy(struct compile_state *state, struct triple *ins)
9980{
Eric Biederman90089602004-05-28 14:11:54 +00009981 struct triple *right;
9982 right = RHS(ins, 0);
9983 if (is_subset_type(ins->type, right->type)) {
9984 ins->type = right->type;
9985 }
9986 if (equiv_types(ins->type, right->type)) {
9987 ins->op = OP_COPY;/* I don't need to convert if the types match */
9988 } else {
9989 if (ins->op == OP_COPY) {
9990 internal_error(state, ins, "type mismatch on copy");
9991 }
9992 }
9993 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
9994 struct triple *sdecl;
9995 ulong_t offset;
9996 sdecl = MISC(right, 0);
9997 offset = right->u.cval;
9998 mkaddr_const(state, ins, sdecl, offset);
9999 }
10000 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10001 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010002 case OP_INTCONST:
10003 {
10004 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010005 left = read_const(state, ins, right);
10006 /* Ensure I have not overflowed the destination. */
10007 if (size_of(state, right->type) > size_of(state, ins->type)) {
10008 ulong_t mask;
10009 mask = 1;
10010 mask <<= size_of(state, ins->type);
10011 mask -= 1;
10012 left &= mask;
10013 }
10014 /* Ensure I am properly sign extended */
10015 if (size_of(state, right->type) < size_of(state, ins->type) &&
10016 is_signed(right->type)) {
10017 long_t val;
10018 int shift;
10019 shift = SIZEOF_LONG - size_of(state, right->type);
10020 val = left;
10021 val <<= shift;
10022 val >>= shift;
10023 left = val;
10024 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010025 mkconst(state, ins, left);
10026 break;
10027 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010028 default:
10029 internal_error(state, ins, "uknown constant");
10030 break;
10031 }
10032 }
10033}
10034
Eric Biederman83b991a2003-10-11 06:20:25 +000010035static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010036{
10037 struct triple *ptr;
10038 if (!block) {
10039 return 0;
10040 }
10041 ptr = block->first;
10042 do {
10043 if (ptr->op == OP_PHI) {
10044 return 1;
10045 }
10046 ptr = ptr->next;
10047 } while(ptr != block->last);
10048 return 0;
10049}
10050
Eric Biederman83b991a2003-10-11 06:20:25 +000010051static int phi_dependency(struct block *block)
10052{
10053 /* A block has a phi dependency if a phi function
10054 * depends on that block to exist, and makes a block
10055 * that is otherwise useless unsafe to remove.
10056 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010057 if (block) {
10058 struct block_set *edge;
10059 for(edge = block->edges; edge; edge = edge->next) {
10060 if (phi_present(edge->member)) {
10061 return 1;
10062 }
10063 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010064 }
10065 return 0;
10066}
10067
10068static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10069{
10070 struct triple *targ;
10071 targ = TARG(ins, 0);
10072 /* During scc_transform temporary triples are allocated that
10073 * loop back onto themselves. If I see one don't advance the
10074 * target.
10075 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000010076 while(triple_is_structural(state, targ) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010077 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010078 targ = targ->next;
10079 }
10080 return targ;
10081}
10082
10083
10084static void simplify_branch(struct compile_state *state, struct triple *ins)
10085{
Eric Biederman90089602004-05-28 14:11:54 +000010086 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010087 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010088 internal_error(state, ins, "not branch");
10089 }
10090 if (ins->use != 0) {
10091 internal_error(state, ins, "branch use");
10092 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010093 /* The challenge here with simplify branch is that I need to
Eric Biederman83b991a2003-10-11 06:20:25 +000010094 * make modifications to the control flow graph as well
10095 * as to the branch instruction itself. That is handled
10096 * by rebuilding the basic blocks after simplify all is called.
10097 */
10098
10099 /* If we have a branch to an unconditional branch update
10100 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010101 * functions.
10102 * Also only do this a limited number of times so
10103 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010104 */
Eric Biederman90089602004-05-28 14:11:54 +000010105 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010106 do {
10107 struct triple *targ;
10108 simplified = 0;
10109 targ = branch_target(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010110 if ((targ != ins) && (targ->op == OP_BRANCH) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010111 !phi_dependency(targ->u.block))
10112 {
10113 unuse_triple(TARG(ins, 0), ins);
10114 TARG(ins, 0) = TARG(targ, 0);
10115 use_triple(TARG(ins, 0), ins);
10116 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010117 }
Eric Biederman90089602004-05-28 14:11:54 +000010118 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010119
10120 /* If we have a conditional branch with a constant condition
10121 * make it an unconditional branch.
10122 */
Eric Biederman90089602004-05-28 14:11:54 +000010123 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010124 struct triple *targ;
10125 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010126 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010127 unuse_triple(RHS(ins, 0), ins);
10128 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010129 ins->rhs = 0;
10130 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010131 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010132 if (value) {
10133 unuse_triple(ins->next, ins);
10134 TARG(ins, 0) = targ;
10135 }
10136 else {
10137 unuse_triple(targ, ins);
10138 TARG(ins, 0) = ins->next;
10139 }
10140 }
Eric Biederman90089602004-05-28 14:11:54 +000010141
10142 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010143 * make it a noop.
10144 */
10145 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010146 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010147 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010148 unuse_triple(RHS(ins, 0), ins);
10149 unuse_triple(ins->next, ins);
10150 }
Eric Biederman90089602004-05-28 14:11:54 +000010151 ins->lhs = 0;
10152 ins->rhs = 0;
10153 ins->misc = 0;
10154 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010155 ins->op = OP_NOOP;
10156 if (ins->use) {
10157 internal_error(state, ins, "noop use != 0");
10158 }
10159 }
10160}
10161
Eric Biederman530b5192003-07-01 10:05:30 +000010162static void simplify_label(struct compile_state *state, struct triple *ins)
10163{
Eric Biederman83b991a2003-10-11 06:20:25 +000010164 /* Ignore volatile labels */
10165 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010166 return;
10167 }
10168 if (ins->use == 0) {
10169 ins->op = OP_NOOP;
10170 }
10171 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010172 /* In general it is not safe to merge one label that
10173 * imediately follows another. The problem is that the empty
10174 * looking block may have phi functions that depend on it.
10175 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010176 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010177 struct triple_set *user, *next;
10178 ins->op = OP_NOOP;
10179 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010180 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010181 next = user->next;
10182 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010183 expr = triple_targ(state, use, 0);
10184 for(;expr; expr = triple_targ(state, use, expr)) {
10185 if (*expr == ins) {
10186 *expr = ins->prev;
10187 unuse_triple(ins, use);
10188 use_triple(ins->prev, use);
10189 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010190
Eric Biederman530b5192003-07-01 10:05:30 +000010191 }
10192 }
10193 if (ins->use) {
10194 internal_error(state, ins, "noop use != 0");
10195 }
10196 }
10197 }
10198}
10199
Eric Biedermanb138ac82003-04-22 18:44:01 +000010200static void simplify_phi(struct compile_state *state, struct triple *ins)
10201{
Eric Biederman83b991a2003-10-11 06:20:25 +000010202 struct triple **slot;
10203 struct triple *value;
10204 int zrhs, i;
10205 ulong_t cvalue;
10206 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010207 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010208 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010209 return;
10210 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010211 /* See if all of the rhs members of a phi have the same value */
10212 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010213 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010214 for(i = 1; i < zrhs; i++) {
10215 if ( !slot[i] ||
10216 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010217 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010218 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010219 break;
10220 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010221 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010222 if (i == zrhs) {
10223 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010224 return;
10225 }
10226 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010227
Eric Biederman83b991a2003-10-11 06:20:25 +000010228 /* See if all of rhs members of a phi are the same */
10229 value = slot[0];
10230 for(i = 1; i < zrhs; i++) {
10231 if (slot[i] != value) {
10232 break;
10233 }
10234 }
10235 if (i == zrhs) {
10236 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010237 if (!is_subset_type(ins->type, value->type)) {
10238 internal_error(state, ins, "bad input type to phi");
10239 }
10240 /* Make the types match */
10241 if (!equiv_types(ins->type, value->type)) {
10242 ins->type = value->type;
10243 }
10244 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010245 mkcopy(state, ins, value);
10246 return;
10247 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010248}
10249
10250
10251static void simplify_bsf(struct compile_state *state, struct triple *ins)
10252{
Eric Biederman90089602004-05-28 14:11:54 +000010253 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010254 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010255 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010256 mkconst(state, ins, bsf(left));
10257 }
10258}
10259
10260static void simplify_bsr(struct compile_state *state, struct triple *ins)
10261{
Eric Biederman90089602004-05-28 14:11:54 +000010262 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010263 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010264 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010265 mkconst(state, ins, bsr(left));
10266 }
10267}
10268
10269
10270typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010271static const struct simplify_table {
10272 simplify_t func;
10273 unsigned long flag;
10274} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010275#define simplify_sdivt simplify_noop
10276#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010277#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010278
Eric Biederman5ade04a2003-10-22 04:03:46 +000010279[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10280[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10281[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10282[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10283[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10284[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10285[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10286[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10287[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10288[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10289[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10290[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10291[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10292[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10293[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10294[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10295[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10296[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10297[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010298
Eric Biederman5ade04a2003-10-22 04:03:46 +000010299[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10300[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10301[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10302[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10303[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10304[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10305[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10306[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10307[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10308[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10309[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10310[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010311
Eric Biederman90089602004-05-28 14:11:54 +000010312[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010313[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010314
Eric Biederman90089602004-05-28 14:11:54 +000010315[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10316[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10317[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10318
Eric Biederman5ade04a2003-10-22 04:03:46 +000010319[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010320
Eric Biederman5ade04a2003-10-22 04:03:46 +000010321[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10322[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10323[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010324[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010325
Eric Biederman5ade04a2003-10-22 04:03:46 +000010326[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10327[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10328[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010329[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010330[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10331[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010332
Eric Biederman5ade04a2003-10-22 04:03:46 +000010333[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010334[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010335
Eric Biederman5ade04a2003-10-22 04:03:46 +000010336[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10337[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10338[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10339[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10340[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10341[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10342[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10343[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10344[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010345
Eric Biederman5ade04a2003-10-22 04:03:46 +000010346[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10347[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10348[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10349[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10350[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10351[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10352[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10353[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10354[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Stefan Reinauer14e22772010-04-27 06:56:47 +000010355[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010356[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010357};
10358
Stefan Reinauer14e22772010-04-27 06:56:47 +000010359static inline void debug_simplify(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000010360 simplify_t do_simplify, struct triple *ins)
10361{
10362#if DEBUG_SIMPLIFY_HIRES
10363 if (state->functions_joined && (do_simplify != simplify_noop)) {
10364 /* High resolution debugging mode */
10365 fprintf(state->dbgout, "simplifing: ");
10366 display_triple(state->dbgout, ins);
10367 }
10368#endif
10369 do_simplify(state, ins);
10370#if DEBUG_SIMPLIFY_HIRES
10371 if (state->functions_joined && (do_simplify != simplify_noop)) {
10372 /* High resolution debugging mode */
10373 fprintf(state->dbgout, "simplified: ");
10374 display_triple(state->dbgout, ins);
10375 }
10376#endif
10377}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010378static void simplify(struct compile_state *state, struct triple *ins)
10379{
10380 int op;
10381 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010382 if (ins == &unknown_triple) {
10383 internal_error(state, ins, "simplifying the unknown triple?");
10384 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010385 do {
10386 op = ins->op;
10387 do_simplify = 0;
Patrick Georgi0dde01c2013-11-11 15:57:09 +010010388 if ((op < 0) || (op >= sizeof(table_simplify)/sizeof(table_simplify[0]))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010389 do_simplify = 0;
10390 }
Eric Biederman90089602004-05-28 14:11:54 +000010391 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010392 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010393 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010394 if (do_simplify &&
Eric Biederman90089602004-05-28 14:11:54 +000010395 !(state->compiler->flags & table_simplify[op].flag)) {
10396 do_simplify = simplify_noop;
10397 }
10398 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010399 do_simplify = simplify_noop;
10400 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010401
Eric Biedermanb138ac82003-04-22 18:44:01 +000010402 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010403 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010404 op, tops(op));
10405 return;
10406 }
Eric Biederman90089602004-05-28 14:11:54 +000010407 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010408 } while(ins->op != op);
10409}
10410
Eric Biederman5ade04a2003-10-22 04:03:46 +000010411static void rebuild_ssa_form(struct compile_state *state);
10412
Eric Biedermanb138ac82003-04-22 18:44:01 +000010413static void simplify_all(struct compile_state *state)
10414{
10415 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010416 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10417 return;
10418 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010419 first = state->first;
10420 ins = first->prev;
10421 do {
10422 simplify(state, ins);
10423 ins = ins->prev;
10424 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010425 ins = first;
10426 do {
10427 simplify(state, ins);
10428 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010429 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010430 rebuild_ssa_form(state);
10431
Eric Biederman90089602004-05-28 14:11:54 +000010432 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010433}
10434
10435/*
10436 * Builtins....
10437 * ============================
10438 */
10439
Eric Biederman0babc1c2003-05-09 02:39:00 +000010440static void register_builtin_function(struct compile_state *state,
10441 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010442{
Eric Biederman90089602004-05-28 14:11:54 +000010443 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010444 struct triple *def, *result, *work, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010445 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010446 struct file_state file;
10447 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010448 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010449 va_list args;
10450 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010451
10452 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010453 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010454 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010455 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010456 file.report_line = 1;
10457 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010458 file.prev = state->file;
10459 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010460 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010461
10462 /* Find the Parameter count */
10463 valid_op(state, op);
10464 parameters = table_ops[op].rhs;
10465 if (parameters < 0 ) {
10466 internal_error(state, 0, "Invalid builtin parameter count");
10467 }
10468
10469 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010470 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010471 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010472 next = &ftype->right;
10473 va_start(args, rtype);
10474 for(i = 0; i < parameters; i++) {
10475 atype = va_arg(args, struct type *);
10476 if (!*next) {
10477 *next = atype;
10478 } else {
10479 *next = new_type(TYPE_PRODUCT, *next, atype);
10480 next = &((*next)->right);
10481 }
10482 }
10483 if (!*next) {
10484 *next = &void_type;
10485 }
10486 va_end(args);
10487
Eric Biederman90089602004-05-28 14:11:54 +000010488 /* Get the initial closure type */
10489 ctype = new_type(TYPE_JOIN, &void_type, 0);
10490 ctype->elements = 1;
10491
10492 /* Get the return type */
10493 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10494 crtype->elements = 2;
10495
Eric Biedermanb138ac82003-04-22 18:44:01 +000010496 /* Generate the needed triples */
10497 def = triple(state, OP_LIST, ftype, 0, 0);
10498 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010499 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010500 result = flatten(state, first, variable(state, crtype));
10501 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010502 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010503
10504 /* Now string them together */
10505 param = ftype->right;
10506 for(i = 0; i < parameters; i++) {
10507 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10508 atype = param->left;
10509 } else {
10510 atype = param;
10511 }
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010512 flatten(state, first, variable(state, atype));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010513 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010514 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010515 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010516 generate_lhs_pieces(state, work);
10517 for(i = 0; i < parameters; i++) {
10518 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010519 }
Eric Biederman90089602004-05-28 14:11:54 +000010520 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10521 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010522 }
10523 work = flatten(state, first, work);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010524 flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010525 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010526 name_len = strlen(name);
10527 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010528 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010529 symbol(state, ident, &ident->sym_ident, def, ftype);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010530
Eric Biedermanb138ac82003-04-22 18:44:01 +000010531 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010532 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010533 state->main_function = 0;
10534
Eric Biederman5ade04a2003-10-22 04:03:46 +000010535 if (!state->functions) {
10536 state->functions = def;
10537 } else {
10538 insert_triple(state, state->functions, def);
10539 }
10540 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010541 FILE *fp = state->dbgout;
10542 fprintf(fp, "\n");
10543 loc(fp, state, 0);
10544 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10545 display_func(state, fp, def);
10546 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010547 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010548}
10549
Eric Biederman0babc1c2003-05-09 02:39:00 +000010550static struct type *partial_struct(struct compile_state *state,
10551 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010552{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010553 struct hash_entry *field_ident;
10554 struct type *result;
10555 int field_name_len;
10556
10557 field_name_len = strlen(field_name);
10558 field_ident = lookup(state, field_name, field_name_len);
10559
10560 result = clone_type(0, type);
10561 result->field_ident = field_ident;
10562
10563 if (rest) {
10564 result = new_type(TYPE_PRODUCT, result, rest);
10565 }
10566 return result;
10567}
10568
10569static struct type *register_builtin_type(struct compile_state *state,
10570 const char *name, struct type *type)
10571{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010572 struct hash_entry *ident;
10573 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010574
Eric Biedermanb138ac82003-04-22 18:44:01 +000010575 name_len = strlen(name);
10576 ident = lookup(state, name, name_len);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010577
Eric Biederman0babc1c2003-05-09 02:39:00 +000010578 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10579 ulong_t elements = 0;
10580 struct type *field;
10581 type = new_type(TYPE_STRUCT, type, 0);
10582 field = type->left;
10583 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10584 elements++;
10585 field = field->right;
10586 }
10587 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010588 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010589 type->type_ident = ident;
10590 type->elements = elements;
10591 }
10592 symbol(state, ident, &ident->sym_ident, 0, type);
10593 ident->tok = TOK_TYPE_NAME;
10594 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010595}
10596
Eric Biederman0babc1c2003-05-09 02:39:00 +000010597
Eric Biedermanb138ac82003-04-22 18:44:01 +000010598static void register_builtins(struct compile_state *state)
10599{
Eric Biederman530b5192003-07-01 10:05:30 +000010600 struct type *div_type, *ldiv_type;
10601 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010602 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010603
Eric Biederman530b5192003-07-01 10:05:30 +000010604 div_type = register_builtin_type(state, "__builtin_div_t",
10605 partial_struct(state, "quot", &int_type,
10606 partial_struct(state, "rem", &int_type, 0)));
10607 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10608 partial_struct(state, "quot", &long_type,
10609 partial_struct(state, "rem", &long_type, 0)));
10610 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10611 partial_struct(state, "quot", &uint_type,
10612 partial_struct(state, "rem", &uint_type, 0)));
10613 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10614 partial_struct(state, "quot", &ulong_type,
10615 partial_struct(state, "rem", &ulong_type, 0)));
10616
10617 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10618 &int_type, &int_type);
10619 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10620 &long_type, &long_type);
10621 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10622 &uint_type, &uint_type);
10623 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10624 &ulong_type, &ulong_type);
10625
Stefan Reinauer14e22772010-04-27 06:56:47 +000010626 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010627 &ushort_type);
10628 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10629 &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010630 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010631 &ushort_type);
10632
Stefan Reinauer14e22772010-04-27 06:56:47 +000010633 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010634 &uchar_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010635 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010636 &ushort_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010637 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010638 &uint_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010639
10640 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010641 &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010642 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010643 &int_type);
10644
10645 msr_type = register_builtin_type(state, "__builtin_msr_t",
10646 partial_struct(state, "lo", &ulong_type,
10647 partial_struct(state, "hi", &ulong_type, 0)));
10648
10649 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10650 &ulong_type);
10651 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10652 &ulong_type, &ulong_type, &ulong_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010653
10654 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010655 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010656}
10657
10658static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000010659 struct compile_state *state, struct type *type,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010660 struct hash_entry **ident, int need_ident);
10661static void decl(struct compile_state *state, struct triple *first);
10662static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010663#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010664static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010665#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010666static struct type *decl_specifiers(struct compile_state *state);
10667static int istype(int tok);
10668static struct triple *expr(struct compile_state *state);
10669static struct triple *assignment_expr(struct compile_state *state);
10670static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010671static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010672
10673static struct triple *call_expr(
10674 struct compile_state *state, struct triple *func)
10675{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010676 struct triple *def;
10677 struct type *param, *type;
10678 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010679
10680 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10681 error(state, 0, "Called object is not a function");
10682 }
10683 if (func->op != OP_LIST) {
10684 internal_error(state, 0, "improper function");
10685 }
10686 eat(state, TOK_LPAREN);
10687 /* Find the return type without any specifiers */
10688 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010689 /* Count the number of rhs entries for OP_FCALL */
10690 param = func->type->right;
10691 pvals = 0;
10692 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10693 pvals++;
10694 param = param->right;
10695 }
10696 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10697 pvals++;
10698 }
10699 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010700 MISC(def, 0) = func;
10701
10702 param = func->type->right;
10703 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010704 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010705 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010706 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010707 arg_type = param;
10708 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10709 arg_type = param->left;
10710 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010711 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010712 RHS(def, index) = val;
10713 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010714 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010715 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010716 }
10717 }
10718 eat(state, TOK_RPAREN);
10719 return def;
10720}
10721
10722
10723static struct triple *character_constant(struct compile_state *state)
10724{
10725 struct triple *def;
10726 struct token *tk;
10727 const signed char *str, *end;
10728 int c;
10729 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010730 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010731 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010732 str_len = tk->str_len - 2;
10733 if (str_len <= 0) {
10734 error(state, 0, "empty character constant");
10735 }
10736 end = str + str_len;
10737 c = char_value(state, &str, end);
10738 if (str != end) {
10739 error(state, 0, "multibyte character constant not supported");
10740 }
10741 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10742 return def;
10743}
10744
10745static struct triple *string_constant(struct compile_state *state)
10746{
10747 struct triple *def;
10748 struct token *tk;
10749 struct type *type;
10750 const signed char *str, *end;
10751 signed char *buf, *ptr;
10752 int str_len;
10753
10754 buf = 0;
10755 type = new_type(TYPE_ARRAY, &char_type, 0);
10756 type->elements = 0;
10757 /* The while loop handles string concatenation */
10758 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010759 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010760 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010761 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010762 if (str_len < 0) {
10763 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010764 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010765 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010766 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010767 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010768 end = str + str_len;
10769 ptr = buf;
10770 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10771 memcpy(buf, ptr, type->elements);
10772 ptr = buf + type->elements;
10773 do {
10774 *ptr++ = char_value(state, &str, end);
10775 } while(str < end);
10776 type->elements = ptr - buf;
10777 } while(peek(state) == TOK_LIT_STRING);
10778 *ptr = '\0';
10779 type->elements += 1;
10780 def = triple(state, OP_BLOBCONST, type, 0, 0);
10781 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010782
Eric Biedermanb138ac82003-04-22 18:44:01 +000010783 return def;
10784}
10785
10786
10787static struct triple *integer_constant(struct compile_state *state)
10788{
10789 struct triple *def;
10790 unsigned long val;
10791 struct token *tk;
10792 char *end;
10793 int u, l, decimal;
10794 struct type *type;
10795
Eric Biederman41203d92004-11-08 09:31:09 +000010796 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010797 errno = 0;
10798 decimal = (tk->val.str[0] != '0');
10799 val = strtoul(tk->val.str, &end, 0);
Patrick Georgib6239b82014-08-09 19:42:08 +020010800 if (errno == ERANGE) {
10801 error(state, 0, "Integer constant out of range");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010802 }
10803 u = l = 0;
10804 if ((*end == 'u') || (*end == 'U')) {
10805 u = 1;
10806 end++;
10807 }
10808 if ((*end == 'l') || (*end == 'L')) {
10809 l = 1;
10810 end++;
10811 }
10812 if ((*end == 'u') || (*end == 'U')) {
10813 u = 1;
10814 end++;
10815 }
10816 if (*end) {
10817 error(state, 0, "Junk at end of integer constant");
10818 }
10819 if (u && l) {
10820 type = &ulong_type;
10821 }
10822 else if (l) {
10823 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010824 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010825 type = &ulong_type;
10826 }
10827 }
10828 else if (u) {
10829 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010830 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010831 type = &ulong_type;
10832 }
10833 }
10834 else {
10835 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010836 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010837 type = &uint_type;
10838 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010839 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010840 type = &ulong_type;
10841 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010842 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010843 type = &long_type;
10844 }
10845 }
10846 def = int_const(state, type, val);
10847 return def;
10848}
10849
10850static struct triple *primary_expr(struct compile_state *state)
10851{
10852 struct triple *def;
10853 int tok;
10854 tok = peek(state);
10855 switch(tok) {
10856 case TOK_IDENT:
10857 {
10858 struct hash_entry *ident;
10859 /* Here ident is either:
10860 * a varable name
10861 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010862 */
Eric Biederman41203d92004-11-08 09:31:09 +000010863 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010864 if (!ident->sym_ident) {
10865 error(state, 0, "%s undeclared", ident->name);
10866 }
10867 def = ident->sym_ident->def;
10868 break;
10869 }
10870 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010871 {
10872 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010873 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010874 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010875 if (!ident->sym_ident) {
10876 error(state, 0, "%s undeclared", ident->name);
10877 }
10878 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010879 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010880 }
Eric Biederman41203d92004-11-08 09:31:09 +000010881 case TOK_MIDENT:
10882 {
10883 struct hash_entry *ident;
10884 ident = eat(state, TOK_MIDENT)->ident;
10885 warning(state, 0, "Replacing undefined macro: %s with 0",
10886 ident->name);
10887 def = int_const(state, &int_type, 0);
10888 break;
10889 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010890 case TOK_LPAREN:
10891 eat(state, TOK_LPAREN);
10892 def = expr(state);
10893 eat(state, TOK_RPAREN);
10894 break;
10895 case TOK_LIT_INT:
10896 def = integer_constant(state);
10897 break;
10898 case TOK_LIT_FLOAT:
10899 eat(state, TOK_LIT_FLOAT);
10900 error(state, 0, "Floating point constants not supported");
10901 def = 0;
10902 FINISHME();
10903 break;
10904 case TOK_LIT_CHAR:
10905 def = character_constant(state);
10906 break;
10907 case TOK_LIT_STRING:
10908 def = string_constant(state);
10909 break;
10910 default:
10911 def = 0;
10912 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10913 }
10914 return def;
10915}
10916
10917static struct triple *postfix_expr(struct compile_state *state)
10918{
10919 struct triple *def;
10920 int postfix;
10921 def = primary_expr(state);
10922 do {
10923 struct triple *left;
10924 int tok;
10925 postfix = 1;
10926 left = def;
10927 switch((tok = peek(state))) {
10928 case TOK_LBRACKET:
10929 eat(state, TOK_LBRACKET);
10930 def = mk_subscript_expr(state, left, expr(state));
10931 eat(state, TOK_RBRACKET);
10932 break;
10933 case TOK_LPAREN:
10934 def = call_expr(state, def);
10935 break;
10936 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010937 {
10938 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010939 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010940 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010941 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010942 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010943 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010944 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010945 {
10946 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010947 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010948 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010949 def = mk_deref_expr(state, read_expr(state, def));
10950 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010951 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010952 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010953 case TOK_PLUSPLUS:
10954 eat(state, TOK_PLUSPLUS);
10955 def = mk_post_inc_expr(state, left);
10956 break;
10957 case TOK_MINUSMINUS:
10958 eat(state, TOK_MINUSMINUS);
10959 def = mk_post_dec_expr(state, left);
10960 break;
10961 default:
10962 postfix = 0;
10963 break;
10964 }
10965 } while(postfix);
10966 return def;
10967}
10968
10969static struct triple *cast_expr(struct compile_state *state);
10970
10971static struct triple *unary_expr(struct compile_state *state)
10972{
10973 struct triple *def, *right;
10974 int tok;
10975 switch((tok = peek(state))) {
10976 case TOK_PLUSPLUS:
10977 eat(state, TOK_PLUSPLUS);
10978 def = mk_pre_inc_expr(state, unary_expr(state));
10979 break;
10980 case TOK_MINUSMINUS:
10981 eat(state, TOK_MINUSMINUS);
10982 def = mk_pre_dec_expr(state, unary_expr(state));
10983 break;
10984 case TOK_AND:
10985 eat(state, TOK_AND);
10986 def = mk_addr_expr(state, cast_expr(state), 0);
10987 break;
10988 case TOK_STAR:
10989 eat(state, TOK_STAR);
10990 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
10991 break;
10992 case TOK_PLUS:
10993 eat(state, TOK_PLUS);
10994 right = read_expr(state, cast_expr(state));
10995 arithmetic(state, right);
10996 def = integral_promotion(state, right);
10997 break;
10998 case TOK_MINUS:
10999 eat(state, TOK_MINUS);
11000 right = read_expr(state, cast_expr(state));
11001 arithmetic(state, right);
11002 def = integral_promotion(state, right);
11003 def = triple(state, OP_NEG, def->type, def, 0);
11004 break;
11005 case TOK_TILDE:
11006 eat(state, TOK_TILDE);
11007 right = read_expr(state, cast_expr(state));
11008 integral(state, right);
11009 def = integral_promotion(state, right);
11010 def = triple(state, OP_INVERT, def->type, def, 0);
11011 break;
11012 case TOK_BANG:
11013 eat(state, TOK_BANG);
11014 right = read_expr(state, cast_expr(state));
11015 bool(state, right);
11016 def = lfalse_expr(state, right);
11017 break;
11018 case TOK_SIZEOF:
11019 {
11020 struct type *type;
11021 int tok1, tok2;
11022 eat(state, TOK_SIZEOF);
11023 tok1 = peek(state);
11024 tok2 = peek2(state);
11025 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11026 eat(state, TOK_LPAREN);
11027 type = type_name(state);
11028 eat(state, TOK_RPAREN);
11029 }
11030 else {
11031 struct triple *expr;
11032 expr = unary_expr(state);
11033 type = expr->type;
11034 release_expr(state, expr);
11035 }
Eric Biederman90089602004-05-28 14:11:54 +000011036 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011037 break;
11038 }
11039 case TOK_ALIGNOF:
11040 {
11041 struct type *type;
11042 int tok1, tok2;
11043 eat(state, TOK_ALIGNOF);
11044 tok1 = peek(state);
11045 tok2 = peek2(state);
11046 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11047 eat(state, TOK_LPAREN);
11048 type = type_name(state);
11049 eat(state, TOK_RPAREN);
11050 }
11051 else {
11052 struct triple *expr;
11053 expr = unary_expr(state);
11054 type = expr->type;
11055 release_expr(state, expr);
11056 }
Eric Biederman90089602004-05-28 14:11:54 +000011057 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011058 break;
11059 }
Eric Biederman41203d92004-11-08 09:31:09 +000011060 case TOK_MDEFINED:
11061 {
11062 /* We only come here if we are called from the preprocessor */
11063 struct hash_entry *ident;
11064 int parens;
11065 eat(state, TOK_MDEFINED);
11066 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011067 if (pp_peek(state) == TOK_LPAREN) {
11068 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011069 parens = 1;
11070 }
Eric Biedermancb364952004-11-15 10:46:44 +000011071 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011072 if (parens) {
11073 eat(state, TOK_RPAREN);
11074 }
11075 def = int_const(state, &int_type, ident->sym_define != 0);
11076 break;
11077 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011078 default:
11079 def = postfix_expr(state);
11080 break;
11081 }
11082 return def;
11083}
11084
11085static struct triple *cast_expr(struct compile_state *state)
11086{
11087 struct triple *def;
11088 int tok1, tok2;
11089 tok1 = peek(state);
11090 tok2 = peek2(state);
11091 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11092 struct type *type;
11093 eat(state, TOK_LPAREN);
11094 type = type_name(state);
11095 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011096 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011097 }
11098 else {
11099 def = unary_expr(state);
11100 }
11101 return def;
11102}
11103
11104static struct triple *mult_expr(struct compile_state *state)
11105{
11106 struct triple *def;
11107 int done;
11108 def = cast_expr(state);
11109 do {
11110 struct triple *left, *right;
11111 struct type *result_type;
11112 int tok, op, sign;
11113 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011114 tok = peek(state);
11115 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011116 case TOK_STAR:
11117 case TOK_DIV:
11118 case TOK_MOD:
11119 left = read_expr(state, def);
11120 arithmetic(state, left);
11121
11122 eat(state, tok);
11123
11124 right = read_expr(state, cast_expr(state));
11125 arithmetic(state, right);
11126
11127 result_type = arithmetic_result(state, left, right);
11128 sign = is_signed(result_type);
11129 op = -1;
11130 switch(tok) {
11131 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11132 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11133 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11134 }
11135 def = triple(state, op, result_type, left, right);
11136 break;
11137 default:
11138 done = 1;
11139 break;
11140 }
11141 } while(!done);
11142 return def;
11143}
11144
11145static struct triple *add_expr(struct compile_state *state)
11146{
11147 struct triple *def;
11148 int done;
11149 def = mult_expr(state);
11150 do {
11151 done = 0;
11152 switch( peek(state)) {
11153 case TOK_PLUS:
11154 eat(state, TOK_PLUS);
11155 def = mk_add_expr(state, def, mult_expr(state));
11156 break;
11157 case TOK_MINUS:
11158 eat(state, TOK_MINUS);
11159 def = mk_sub_expr(state, def, mult_expr(state));
11160 break;
11161 default:
11162 done = 1;
11163 break;
11164 }
11165 } while(!done);
11166 return def;
11167}
11168
11169static struct triple *shift_expr(struct compile_state *state)
11170{
11171 struct triple *def;
11172 int done;
11173 def = add_expr(state);
11174 do {
11175 struct triple *left, *right;
11176 int tok, op;
11177 done = 0;
11178 switch((tok = peek(state))) {
11179 case TOK_SL:
11180 case TOK_SR:
11181 left = read_expr(state, def);
11182 integral(state, left);
11183 left = integral_promotion(state, left);
11184
11185 eat(state, tok);
11186
11187 right = read_expr(state, add_expr(state));
11188 integral(state, right);
11189 right = integral_promotion(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011190
11191 op = (tok == TOK_SL)? OP_SL :
Eric Biedermanb138ac82003-04-22 18:44:01 +000011192 is_signed(left->type)? OP_SSR: OP_USR;
11193
11194 def = triple(state, op, left->type, left, right);
11195 break;
11196 default:
11197 done = 1;
11198 break;
11199 }
11200 } while(!done);
11201 return def;
11202}
11203
11204static struct triple *relational_expr(struct compile_state *state)
11205{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011206#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011207#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011208#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011209 struct triple *def;
11210 int done;
11211 def = shift_expr(state);
11212 do {
11213 struct triple *left, *right;
11214 struct type *arg_type;
11215 int tok, op, sign;
11216 done = 0;
11217 switch((tok = peek(state))) {
11218 case TOK_LESS:
11219 case TOK_MORE:
11220 case TOK_LESSEQ:
11221 case TOK_MOREEQ:
11222 left = read_expr(state, def);
11223 arithmetic(state, left);
11224
11225 eat(state, tok);
11226
11227 right = read_expr(state, shift_expr(state));
11228 arithmetic(state, right);
11229
11230 arg_type = arithmetic_result(state, left, right);
11231 sign = is_signed(arg_type);
11232 op = -1;
11233 switch(tok) {
11234 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11235 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11236 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11237 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11238 }
11239 def = triple(state, op, &int_type, left, right);
11240 break;
11241 default:
11242 done = 1;
11243 break;
11244 }
11245 } while(!done);
11246 return def;
11247}
11248
11249static struct triple *equality_expr(struct compile_state *state)
11250{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011251#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011252#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011253#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011254 struct triple *def;
11255 int done;
11256 def = relational_expr(state);
11257 do {
11258 struct triple *left, *right;
11259 int tok, op;
11260 done = 0;
11261 switch((tok = peek(state))) {
11262 case TOK_EQEQ:
11263 case TOK_NOTEQ:
11264 left = read_expr(state, def);
11265 arithmetic(state, left);
11266 eat(state, tok);
11267 right = read_expr(state, relational_expr(state));
11268 arithmetic(state, right);
11269 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11270 def = triple(state, op, &int_type, left, right);
11271 break;
11272 default:
11273 done = 1;
11274 break;
11275 }
11276 } while(!done);
11277 return def;
11278}
11279
11280static struct triple *and_expr(struct compile_state *state)
11281{
11282 struct triple *def;
11283 def = equality_expr(state);
11284 while(peek(state) == TOK_AND) {
11285 struct triple *left, *right;
11286 struct type *result_type;
11287 left = read_expr(state, def);
11288 integral(state, left);
11289 eat(state, TOK_AND);
11290 right = read_expr(state, equality_expr(state));
11291 integral(state, right);
11292 result_type = arithmetic_result(state, left, right);
11293 def = triple(state, OP_AND, result_type, left, right);
11294 }
11295 return def;
11296}
11297
11298static struct triple *xor_expr(struct compile_state *state)
11299{
11300 struct triple *def;
11301 def = and_expr(state);
11302 while(peek(state) == TOK_XOR) {
11303 struct triple *left, *right;
11304 struct type *result_type;
11305 left = read_expr(state, def);
11306 integral(state, left);
11307 eat(state, TOK_XOR);
11308 right = read_expr(state, and_expr(state));
11309 integral(state, right);
11310 result_type = arithmetic_result(state, left, right);
11311 def = triple(state, OP_XOR, result_type, left, right);
11312 }
11313 return def;
11314}
11315
11316static struct triple *or_expr(struct compile_state *state)
11317{
11318 struct triple *def;
11319 def = xor_expr(state);
11320 while(peek(state) == TOK_OR) {
11321 struct triple *left, *right;
11322 struct type *result_type;
11323 left = read_expr(state, def);
11324 integral(state, left);
11325 eat(state, TOK_OR);
11326 right = read_expr(state, xor_expr(state));
11327 integral(state, right);
11328 result_type = arithmetic_result(state, left, right);
11329 def = triple(state, OP_OR, result_type, left, right);
11330 }
11331 return def;
11332}
11333
11334static struct triple *land_expr(struct compile_state *state)
11335{
11336 struct triple *def;
11337 def = or_expr(state);
11338 while(peek(state) == TOK_LOGAND) {
11339 struct triple *left, *right;
11340 left = read_expr(state, def);
11341 bool(state, left);
11342 eat(state, TOK_LOGAND);
11343 right = read_expr(state, or_expr(state));
11344 bool(state, right);
11345
Eric Biederman90089602004-05-28 14:11:54 +000011346 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011347 ltrue_expr(state, left),
11348 ltrue_expr(state, right));
11349 }
11350 return def;
11351}
11352
11353static struct triple *lor_expr(struct compile_state *state)
11354{
11355 struct triple *def;
11356 def = land_expr(state);
11357 while(peek(state) == TOK_LOGOR) {
11358 struct triple *left, *right;
11359 left = read_expr(state, def);
11360 bool(state, left);
11361 eat(state, TOK_LOGOR);
11362 right = read_expr(state, land_expr(state));
11363 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011364
Stefan Reinauer14e22772010-04-27 06:56:47 +000011365 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011366 ltrue_expr(state, left),
11367 ltrue_expr(state, right));
11368 }
11369 return def;
11370}
11371
11372static struct triple *conditional_expr(struct compile_state *state)
11373{
11374 struct triple *def;
11375 def = lor_expr(state);
11376 if (peek(state) == TOK_QUEST) {
11377 struct triple *test, *left, *right;
11378 bool(state, def);
11379 test = ltrue_expr(state, read_expr(state, def));
11380 eat(state, TOK_QUEST);
11381 left = read_expr(state, expr(state));
11382 eat(state, TOK_COLON);
11383 right = read_expr(state, conditional_expr(state));
11384
Eric Biederman90089602004-05-28 14:11:54 +000011385 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011386 }
11387 return def;
11388}
11389
Eric Biederman41203d92004-11-08 09:31:09 +000011390struct cv_triple {
11391 struct triple *val;
11392 int id;
11393};
11394
11395static void set_cv(struct compile_state *state, struct cv_triple *cv,
11396 struct triple *dest, struct triple *val)
11397{
11398 if (cv[dest->id].val) {
11399 free_triple(state, cv[dest->id].val);
11400 }
11401 cv[dest->id].val = val;
11402}
11403static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11404 struct triple *src)
11405{
11406 return cv[src->id].val;
11407}
11408
Eric Biedermanb138ac82003-04-22 18:44:01 +000011409static struct triple *eval_const_expr(
11410 struct compile_state *state, struct triple *expr)
11411{
11412 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011413 if (is_const(expr)) {
11414 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011415 }
Eric Biederman00443072003-06-24 12:34:45 +000011416 else {
11417 /* If we don't start out as a constant simplify into one */
11418 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011419 struct cv_triple *cv;
11420 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011421 head = label(state); /* dummy initial triple */
11422 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011423 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011424 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011425 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011426 }
Eric Biederman41203d92004-11-08 09:31:09 +000011427 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11428 i = 1;
11429 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11430 cv[i].val = 0;
11431 cv[i].id = ptr->id;
11432 ptr->id = i;
11433 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011434 }
Eric Biederman41203d92004-11-08 09:31:09 +000011435 ptr = head->next;
11436 do {
11437 valid_ins(state, ptr);
11438 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011439 internal_error(state, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011440 "unexpected %s in constant expression",
11441 tops(ptr->op));
11442 }
11443 else if (ptr->op == OP_LIST) {
11444 }
11445 else if (triple_is_structural(state, ptr)) {
11446 ptr = ptr->next;
11447 }
11448 else if (triple_is_ubranch(state, ptr)) {
11449 ptr = TARG(ptr, 0);
11450 }
11451 else if (triple_is_cbranch(state, ptr)) {
11452 struct triple *cond_val;
11453 cond_val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011454 if (!cond_val || !is_const(cond_val) ||
11455 (cond_val->op != OP_INTCONST))
Eric Biederman41203d92004-11-08 09:31:09 +000011456 {
11457 internal_error(state, ptr, "bad branch condition");
11458 }
11459 if (cond_val->u.cval == 0) {
11460 ptr = ptr->next;
11461 } else {
11462 ptr = TARG(ptr, 0);
11463 }
11464 }
11465 else if (triple_is_branch(state, ptr)) {
11466 error(state, ptr, "bad branch type in constant expression");
11467 }
11468 else if (ptr->op == OP_WRITE) {
11469 struct triple *val;
11470 val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011471
11472 set_cv(state, cv, MISC(ptr, 0),
Eric Biederman41203d92004-11-08 09:31:09 +000011473 copy_triple(state, val));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011474 set_cv(state, cv, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011475 copy_triple(state, val));
11476 ptr = ptr->next;
11477 }
11478 else if (ptr->op == OP_READ) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011479 set_cv(state, cv, ptr,
11480 copy_triple(state,
Eric Biederman41203d92004-11-08 09:31:09 +000011481 get_cv(state, cv, RHS(ptr, 0))));
11482 ptr = ptr->next;
11483 }
11484 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11485 struct triple *val, **rhs;
11486 val = copy_triple(state, ptr);
11487 rhs = triple_rhs(state, val, 0);
11488 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11489 if (!*rhs) {
11490 internal_error(state, ptr, "Missing rhs");
11491 }
11492 *rhs = get_cv(state, cv, *rhs);
11493 }
11494 simplify(state, val);
11495 set_cv(state, cv, ptr, val);
11496 ptr = ptr->next;
11497 }
11498 else {
11499 error(state, ptr, "impure operation in constant expression");
11500 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000011501
Eric Biederman41203d92004-11-08 09:31:09 +000011502 } while(ptr != head);
11503
11504 /* Get the result value */
11505 def = get_cv(state, cv, head->prev);
11506 cv[head->prev->id].val = 0;
11507
11508 /* Free the temporary values */
11509 for(i = 0; i < count; i++) {
11510 if (cv[i].val) {
11511 free_triple(state, cv[i].val);
11512 cv[i].val = 0;
11513 }
11514 }
11515 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011516 /* Free the intermediate expressions */
11517 while(head->next != head) {
11518 release_triple(state, head->next);
11519 }
11520 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011521 }
Eric Biederman41203d92004-11-08 09:31:09 +000011522 if (!is_const(def)) {
11523 error(state, expr, "Not a constant expression");
11524 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011525 return def;
11526}
11527
11528static struct triple *constant_expr(struct compile_state *state)
11529{
11530 return eval_const_expr(state, conditional_expr(state));
11531}
11532
11533static struct triple *assignment_expr(struct compile_state *state)
11534{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011535 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011536 int tok, op, sign;
11537 /* The C grammer in K&R shows assignment expressions
11538 * only taking unary expressions as input on their
11539 * left hand side. But specifies the precedence of
11540 * assignemnt as the lowest operator except for comma.
11541 *
11542 * Allowing conditional expressions on the left hand side
11543 * of an assignement results in a grammar that accepts
11544 * a larger set of statements than standard C. As long
11545 * as the subset of the grammar that is standard C behaves
11546 * correctly this should cause no problems.
Stefan Reinauer14e22772010-04-27 06:56:47 +000011547 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000011548 * For the extra token strings accepted by the grammar
11549 * none of them should produce a valid lvalue, so they
11550 * should not produce functioning programs.
11551 *
11552 * GCC has this bug as well, so surprises should be minimal.
11553 */
11554 def = conditional_expr(state);
11555 left = def;
11556 switch((tok = peek(state))) {
11557 case TOK_EQ:
11558 lvalue(state, left);
11559 eat(state, TOK_EQ);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011560 def = write_expr(state, left,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011561 read_expr(state, assignment_expr(state)));
11562 break;
11563 case TOK_TIMESEQ:
11564 case TOK_DIVEQ:
11565 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011566 lvalue(state, left);
11567 arithmetic(state, left);
11568 eat(state, tok);
11569 right = read_expr(state, assignment_expr(state));
11570 arithmetic(state, right);
11571
11572 sign = is_signed(left->type);
11573 op = -1;
11574 switch(tok) {
11575 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11576 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11577 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011578 }
11579 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011580 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011581 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011582 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011583 case TOK_PLUSEQ:
11584 lvalue(state, left);
11585 eat(state, TOK_PLUSEQ);
11586 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011587 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011588 break;
11589 case TOK_MINUSEQ:
11590 lvalue(state, left);
11591 eat(state, TOK_MINUSEQ);
11592 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011593 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011594 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011595 case TOK_SLEQ:
11596 case TOK_SREQ:
11597 case TOK_ANDEQ:
11598 case TOK_XOREQ:
11599 case TOK_OREQ:
11600 lvalue(state, left);
11601 integral(state, left);
11602 eat(state, tok);
11603 right = read_expr(state, assignment_expr(state));
11604 integral(state, right);
11605 right = integral_promotion(state, right);
11606 sign = is_signed(left->type);
11607 op = -1;
11608 switch(tok) {
11609 case TOK_SLEQ: op = OP_SL; break;
11610 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11611 case TOK_ANDEQ: op = OP_AND; break;
11612 case TOK_XOREQ: op = OP_XOR; break;
11613 case TOK_OREQ: op = OP_OR; break;
11614 }
11615 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011616 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011617 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011618 break;
11619 }
11620 return def;
11621}
11622
11623static struct triple *expr(struct compile_state *state)
11624{
11625 struct triple *def;
11626 def = assignment_expr(state);
11627 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011628 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011629 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011630 }
11631 return def;
11632}
11633
11634static void expr_statement(struct compile_state *state, struct triple *first)
11635{
11636 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011637 /* lvalue conversions always apply except when certian operators
11638 * are applied. I apply the lvalue conversions here
11639 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011640 */
11641 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011642 }
11643 eat(state, TOK_SEMI);
11644}
11645
11646static void if_statement(struct compile_state *state, struct triple *first)
11647{
11648 struct triple *test, *jmp1, *jmp2, *middle, *end;
11649
11650 jmp1 = jmp2 = middle = 0;
11651 eat(state, TOK_IF);
11652 eat(state, TOK_LPAREN);
11653 test = expr(state);
11654 bool(state, test);
11655 /* Cleanup and invert the test */
11656 test = lfalse_expr(state, read_expr(state, test));
11657 eat(state, TOK_RPAREN);
11658 /* Generate the needed pieces */
11659 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011660 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011661 /* Thread the pieces together */
11662 flatten(state, first, test);
11663 flatten(state, first, jmp1);
11664 flatten(state, first, label(state));
11665 statement(state, first);
11666 if (peek(state) == TOK_ELSE) {
11667 eat(state, TOK_ELSE);
11668 /* Generate the rest of the pieces */
11669 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011670 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011671 /* Thread them together */
11672 flatten(state, first, jmp2);
11673 flatten(state, first, middle);
11674 statement(state, first);
11675 flatten(state, first, end);
11676 }
11677 else {
11678 flatten(state, first, middle);
11679 }
11680}
11681
11682static void for_statement(struct compile_state *state, struct triple *first)
11683{
11684 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11685 struct triple *label1, *label2, *label3;
11686 struct hash_entry *ident;
11687
11688 eat(state, TOK_FOR);
11689 eat(state, TOK_LPAREN);
11690 head = test = tail = jmp1 = jmp2 = 0;
11691 if (peek(state) != TOK_SEMI) {
11692 head = expr(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011693 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011694 eat(state, TOK_SEMI);
11695 if (peek(state) != TOK_SEMI) {
11696 test = expr(state);
11697 bool(state, test);
11698 test = ltrue_expr(state, read_expr(state, test));
11699 }
11700 eat(state, TOK_SEMI);
11701 if (peek(state) != TOK_RPAREN) {
11702 tail = expr(state);
11703 }
11704 eat(state, TOK_RPAREN);
11705 /* Generate the needed pieces */
11706 label1 = label(state);
11707 label2 = label(state);
11708 label3 = label(state);
11709 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011710 jmp1 = branch(state, label3, 0);
11711 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011712 }
11713 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011714 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011715 }
11716 end = label(state);
11717 /* Remember where break and continue go */
11718 start_scope(state);
11719 ident = state->i_break;
11720 symbol(state, ident, &ident->sym_ident, end, end->type);
11721 ident = state->i_continue;
11722 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11723 /* Now include the body */
11724 flatten(state, first, head);
11725 flatten(state, first, jmp1);
11726 flatten(state, first, label1);
11727 statement(state, first);
11728 flatten(state, first, label2);
11729 flatten(state, first, tail);
11730 flatten(state, first, label3);
11731 flatten(state, first, test);
11732 flatten(state, first, jmp2);
11733 flatten(state, first, end);
11734 /* Cleanup the break/continue scope */
11735 end_scope(state);
11736}
11737
11738static void while_statement(struct compile_state *state, struct triple *first)
11739{
11740 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11741 struct hash_entry *ident;
11742 eat(state, TOK_WHILE);
11743 eat(state, TOK_LPAREN);
11744 test = expr(state);
11745 bool(state, test);
11746 test = ltrue_expr(state, read_expr(state, test));
11747 eat(state, TOK_RPAREN);
11748 /* Generate the needed pieces */
11749 label1 = label(state);
11750 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011751 jmp1 = branch(state, label2, 0);
11752 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011753 end = label(state);
11754 /* Remember where break and continue go */
11755 start_scope(state);
11756 ident = state->i_break;
11757 symbol(state, ident, &ident->sym_ident, end, end->type);
11758 ident = state->i_continue;
11759 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11760 /* Thread them together */
11761 flatten(state, first, jmp1);
11762 flatten(state, first, label1);
11763 statement(state, first);
11764 flatten(state, first, label2);
11765 flatten(state, first, test);
11766 flatten(state, first, jmp2);
11767 flatten(state, first, end);
11768 /* Cleanup the break/continue scope */
11769 end_scope(state);
11770}
11771
11772static void do_statement(struct compile_state *state, struct triple *first)
11773{
11774 struct triple *label1, *label2, *test, *end;
11775 struct hash_entry *ident;
11776 eat(state, TOK_DO);
11777 /* Generate the needed pieces */
11778 label1 = label(state);
11779 label2 = label(state);
11780 end = label(state);
11781 /* Remember where break and continue go */
11782 start_scope(state);
11783 ident = state->i_break;
11784 symbol(state, ident, &ident->sym_ident, end, end->type);
11785 ident = state->i_continue;
11786 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11787 /* Now include the body */
11788 flatten(state, first, label1);
11789 statement(state, first);
11790 /* Cleanup the break/continue scope */
11791 end_scope(state);
11792 /* Eat the rest of the loop */
11793 eat(state, TOK_WHILE);
11794 eat(state, TOK_LPAREN);
11795 test = read_expr(state, expr(state));
11796 bool(state, test);
11797 eat(state, TOK_RPAREN);
11798 eat(state, TOK_SEMI);
11799 /* Thread the pieces together */
11800 test = ltrue_expr(state, test);
11801 flatten(state, first, label2);
11802 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011803 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011804 flatten(state, first, end);
11805}
11806
11807
11808static void return_statement(struct compile_state *state, struct triple *first)
11809{
11810 struct triple *jmp, *mv, *dest, *var, *val;
11811 int last;
11812 eat(state, TOK_RETURN);
11813
Stefan Reinauer50542a82007-10-24 11:14:14 +000011814#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011815#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011816#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011817 val = 0;
11818 /* If we have a return value do some more work */
11819 if (peek(state) != TOK_SEMI) {
11820 val = read_expr(state, expr(state));
11821 }
11822 eat(state, TOK_SEMI);
11823
11824 /* See if this last statement in a function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000011825 last = ((peek(state) == TOK_RBRACE) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000011826 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11827
11828 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011829 var = fresult(state, state->main_function);
11830
Eric Biedermanb138ac82003-04-22 18:44:01 +000011831 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011832 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011833 mv = jmp = 0;
11834 /* If needed generate a jump instruction */
11835 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011836 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011837 }
11838 /* If needed generate an assignment instruction */
11839 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011840 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011841 }
11842 /* Now put the code together */
11843 if (mv) {
11844 flatten(state, first, mv);
11845 flatten(state, first, jmp);
11846 }
11847 else if (jmp) {
11848 flatten(state, first, jmp);
11849 }
11850}
11851
11852static void break_statement(struct compile_state *state, struct triple *first)
11853{
11854 struct triple *dest;
11855 eat(state, TOK_BREAK);
11856 eat(state, TOK_SEMI);
11857 if (!state->i_break->sym_ident) {
11858 error(state, 0, "break statement not within loop or switch");
11859 }
11860 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011861 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011862}
11863
11864static void continue_statement(struct compile_state *state, struct triple *first)
11865{
11866 struct triple *dest;
11867 eat(state, TOK_CONTINUE);
11868 eat(state, TOK_SEMI);
11869 if (!state->i_continue->sym_ident) {
11870 error(state, 0, "continue statement outside of a loop");
11871 }
11872 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011873 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011874}
11875
11876static void goto_statement(struct compile_state *state, struct triple *first)
11877{
Eric Biederman153ea352003-06-20 14:43:20 +000011878 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011879 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011880 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011881 if (!ident->sym_label) {
11882 /* If this is a forward branch allocate the label now,
11883 * it will be flattend in the appropriate location later.
11884 */
11885 struct triple *ins;
11886 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011887 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011888 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011889 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011890
11891 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011892}
11893
11894static void labeled_statement(struct compile_state *state, struct triple *first)
11895{
Eric Biederman153ea352003-06-20 14:43:20 +000011896 struct triple *ins;
11897 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011898
Eric Biederman41203d92004-11-08 09:31:09 +000011899 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011900 if (ident->sym_label && ident->sym_label->def) {
11901 ins = ident->sym_label->def;
11902 put_occurance(ins->occurance);
11903 ins->occurance = new_occurance(state);
11904 }
11905 else {
11906 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011907 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011908 }
11909 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11910 error(state, 0, "label %s already defined", ident->name);
11911 }
11912 flatten(state, first, ins);
11913
Eric Biedermanb138ac82003-04-22 18:44:01 +000011914 eat(state, TOK_COLON);
11915 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011916}
11917
11918static void switch_statement(struct compile_state *state, struct triple *first)
11919{
Eric Biederman83b991a2003-10-11 06:20:25 +000011920 struct triple *value, *top, *end, *dbranch;
11921 struct hash_entry *ident;
11922
11923 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011924 eat(state, TOK_SWITCH);
11925 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011926 value = expr(state);
11927 integral(state, value);
11928 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011929 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011930 /* Generate the needed pieces */
11931 top = label(state);
11932 end = label(state);
11933 dbranch = branch(state, end, 0);
11934 /* Remember where case branches and break goes */
11935 start_scope(state);
11936 ident = state->i_switch;
11937 symbol(state, ident, &ident->sym_ident, value, value->type);
11938 ident = state->i_case;
11939 symbol(state, ident, &ident->sym_ident, top, top->type);
11940 ident = state->i_break;
11941 symbol(state, ident, &ident->sym_ident, end, end->type);
11942 ident = state->i_default;
11943 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11944 /* Thread them together */
11945 flatten(state, first, value);
11946 flatten(state, first, top);
11947 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011948 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011949 flatten(state, first, end);
11950 /* Cleanup the switch scope */
11951 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011952}
11953
11954static void case_statement(struct compile_state *state, struct triple *first)
11955{
Eric Biederman83b991a2003-10-11 06:20:25 +000011956 struct triple *cvalue, *dest, *test, *jmp;
11957 struct triple *ptr, *value, *top, *dbranch;
11958
11959 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011960 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011961 cvalue = constant_expr(state);
11962 integral(state, cvalue);
11963 if (cvalue->op != OP_INTCONST) {
11964 error(state, 0, "integer constant expected");
11965 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011966 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011967 if (!state->i_case->sym_ident) {
11968 error(state, 0, "case statement not within a switch");
11969 }
11970
11971 /* Lookup the interesting pieces */
11972 top = state->i_case->sym_ident->def;
11973 value = state->i_switch->sym_ident->def;
11974 dbranch = state->i_default->sym_ident->def;
11975
11976 /* See if this case label has already been used */
11977 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11978 if (ptr->op != OP_EQ) {
11979 continue;
11980 }
11981 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11982 error(state, 0, "duplicate case %d statement",
11983 cvalue->u.cval);
11984 }
11985 }
11986 /* Generate the needed pieces */
11987 dest = label(state);
11988 test = triple(state, OP_EQ, &int_type, value, cvalue);
11989 jmp = branch(state, dest, test);
11990 /* Thread the pieces together */
11991 flatten(state, dbranch, test);
11992 flatten(state, dbranch, jmp);
11993 flatten(state, dbranch, label(state));
11994 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011995 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011996}
11997
11998static void default_statement(struct compile_state *state, struct triple *first)
11999{
Eric Biederman83b991a2003-10-11 06:20:25 +000012000 struct triple *dest;
12001 struct triple *dbranch, *end;
12002
12003 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012004 eat(state, TOK_DEFAULT);
12005 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012006
12007 if (!state->i_case->sym_ident) {
12008 error(state, 0, "default statement not within a switch");
12009 }
12010
12011 /* Lookup the interesting pieces */
12012 dbranch = state->i_default->sym_ident->def;
12013 end = state->i_break->sym_ident->def;
12014
12015 /* See if a default statement has already happened */
12016 if (TARG(dbranch, 0) != end) {
12017 error(state, 0, "duplicate default statement");
12018 }
12019
12020 /* Generate the needed pieces */
12021 dest = label(state);
12022
Eric Biederman90089602004-05-28 14:11:54 +000012023 /* Blame the branch on the default statement */
12024 put_occurance(dbranch->occurance);
12025 dbranch->occurance = new_occurance(state);
12026
Eric Biederman83b991a2003-10-11 06:20:25 +000012027 /* Thread the pieces together */
12028 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012029 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012030 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012031 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012032}
12033
12034static void asm_statement(struct compile_state *state, struct triple *first)
12035{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012036 struct asm_info *info;
12037 struct {
12038 struct triple *constraint;
12039 struct triple *expr;
12040 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12041 struct triple *def, *asm_str;
12042 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012043 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012044
Eric Biederman90089602004-05-28 14:11:54 +000012045 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012046 eat(state, TOK_ASM);
12047 /* For now ignore the qualifiers */
12048 switch(peek(state)) {
12049 case TOK_CONST:
12050 eat(state, TOK_CONST);
12051 break;
12052 case TOK_VOLATILE:
12053 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012054 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012055 break;
12056 }
12057 eat(state, TOK_LPAREN);
12058 asm_str = string_constant(state);
12059
12060 colons = 0;
12061 out = in = clobbers = 0;
12062 /* Outputs */
12063 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12064 eat(state, TOK_COLON);
12065 colons++;
12066 more = (peek(state) == TOK_LIT_STRING);
12067 while(more) {
12068 struct triple *var;
12069 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012070 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012071 more = 0;
12072 if (out > MAX_LHS) {
12073 error(state, 0, "Maximum output count exceeded.");
12074 }
12075 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012076 str = constraint->u.blob;
12077 if (str[0] != '=') {
12078 error(state, 0, "Output constraint does not start with =");
12079 }
12080 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012081 eat(state, TOK_LPAREN);
12082 var = conditional_expr(state);
12083 eat(state, TOK_RPAREN);
12084
12085 lvalue(state, var);
12086 out_param[out].constraint = constraint;
12087 out_param[out].expr = var;
12088 if (peek(state) == TOK_COMMA) {
12089 eat(state, TOK_COMMA);
12090 more = 1;
12091 }
12092 out++;
12093 }
12094 }
12095 /* Inputs */
12096 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12097 eat(state, TOK_COLON);
12098 colons++;
12099 more = (peek(state) == TOK_LIT_STRING);
12100 while(more) {
12101 struct triple *val;
12102 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012103 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012104 more = 0;
12105 if (in > MAX_RHS) {
12106 error(state, 0, "Maximum input count exceeded.");
12107 }
12108 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012109 str = constraint->u.blob;
12110 if (digitp(str[0] && str[1] == '\0')) {
12111 int val;
12112 val = digval(str[0]);
12113 if ((val < 0) || (val >= out)) {
12114 error(state, 0, "Invalid input constraint %d", val);
12115 }
12116 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012117 eat(state, TOK_LPAREN);
12118 val = conditional_expr(state);
12119 eat(state, TOK_RPAREN);
12120
12121 in_param[in].constraint = constraint;
12122 in_param[in].expr = val;
12123 if (peek(state) == TOK_COMMA) {
12124 eat(state, TOK_COMMA);
12125 more = 1;
12126 }
12127 in++;
12128 }
12129 }
12130
12131 /* Clobber */
12132 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12133 eat(state, TOK_COLON);
12134 colons++;
12135 more = (peek(state) == TOK_LIT_STRING);
12136 while(more) {
12137 struct triple *clobber;
12138 more = 0;
12139 if ((clobbers + out) > MAX_LHS) {
12140 error(state, 0, "Maximum clobber limit exceeded.");
12141 }
12142 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012143
12144 clob_param[clobbers].constraint = clobber;
12145 if (peek(state) == TOK_COMMA) {
12146 eat(state, TOK_COMMA);
12147 more = 1;
12148 }
12149 clobbers++;
12150 }
12151 }
12152 eat(state, TOK_RPAREN);
12153 eat(state, TOK_SEMI);
12154
12155
12156 info = xcmalloc(sizeof(*info), "asm_info");
12157 info->str = asm_str->u.blob;
12158 free_triple(state, asm_str);
12159
12160 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12161 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012162 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012163
12164 /* Find the register constraints */
12165 for(i = 0; i < out; i++) {
12166 struct triple *constraint;
12167 constraint = out_param[i].constraint;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012168 info->tmpl.lhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012169 out_param[i].expr->type, constraint->u.blob);
12170 free_triple(state, constraint);
12171 }
12172 for(; i - out < clobbers; i++) {
12173 struct triple *constraint;
12174 constraint = clob_param[i - out].constraint;
12175 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12176 free_triple(state, constraint);
12177 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012178 for(i = 0; i < in; i++) {
12179 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012180 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012181 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012182 str = constraint->u.blob;
12183 if (digitp(str[0]) && str[1] == '\0') {
12184 struct reg_info cinfo;
12185 int val;
12186 val = digval(str[0]);
12187 cinfo.reg = info->tmpl.lhs[val].reg;
12188 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12189 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12190 if (cinfo.reg == REG_UNSET) {
12191 cinfo.reg = REG_VIRT0 + val;
12192 }
12193 if (cinfo.regcm == 0) {
12194 error(state, 0, "No registers for %d", val);
12195 }
12196 info->tmpl.lhs[val] = cinfo;
12197 info->tmpl.rhs[i] = cinfo;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012198
Eric Biederman8d9c1232003-06-17 08:42:17 +000012199 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012200 info->tmpl.rhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012201 in_param[i].expr->type, str);
12202 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012203 free_triple(state, constraint);
12204 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012205
12206 /* Now build the helper expressions */
12207 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012208 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012209 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012210 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012211 for(i = 0; i < (out + clobbers); i++) {
12212 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012213 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012214 if (i < out) {
12215 type = out_param[i].expr->type;
12216 } else {
12217 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12218 if (size >= SIZEOF_LONG) {
12219 type = &ulong_type;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012220 }
Eric Biederman90089602004-05-28 14:11:54 +000012221 else if (size >= SIZEOF_INT) {
12222 type = &uint_type;
12223 }
12224 else if (size >= SIZEOF_SHORT) {
12225 type = &ushort_type;
12226 }
12227 else {
12228 type = &uchar_type;
12229 }
12230 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012231 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012232 piece->u.cval = i;
12233 LHS(def, i) = piece;
12234 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012235 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012236 /* And write the helpers to their destinations */
12237 for(i = 0; i < out; i++) {
12238 struct triple *piece;
12239 piece = LHS(def, i);
12240 flatten(state, first,
12241 write_expr(state, out_param[i].expr, piece));
12242 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012243}
12244
12245
12246static int isdecl(int tok)
12247{
12248 switch(tok) {
12249 case TOK_AUTO:
12250 case TOK_REGISTER:
12251 case TOK_STATIC:
12252 case TOK_EXTERN:
12253 case TOK_TYPEDEF:
12254 case TOK_CONST:
12255 case TOK_RESTRICT:
12256 case TOK_VOLATILE:
12257 case TOK_VOID:
12258 case TOK_CHAR:
12259 case TOK_SHORT:
12260 case TOK_INT:
12261 case TOK_LONG:
12262 case TOK_FLOAT:
12263 case TOK_DOUBLE:
12264 case TOK_SIGNED:
12265 case TOK_UNSIGNED:
12266 case TOK_STRUCT:
12267 case TOK_UNION:
12268 case TOK_ENUM:
12269 case TOK_TYPE_NAME: /* typedef name */
12270 return 1;
12271 default:
12272 return 0;
12273 }
12274}
12275
12276static void compound_statement(struct compile_state *state, struct triple *first)
12277{
12278 eat(state, TOK_LBRACE);
12279 start_scope(state);
12280
12281 /* statement-list opt */
12282 while (peek(state) != TOK_RBRACE) {
12283 statement(state, first);
12284 }
12285 end_scope(state);
12286 eat(state, TOK_RBRACE);
12287}
12288
12289static void statement(struct compile_state *state, struct triple *first)
12290{
12291 int tok;
12292 tok = peek(state);
12293 if (tok == TOK_LBRACE) {
12294 compound_statement(state, first);
12295 }
12296 else if (tok == TOK_IF) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012297 if_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012298 }
12299 else if (tok == TOK_FOR) {
12300 for_statement(state, first);
12301 }
12302 else if (tok == TOK_WHILE) {
12303 while_statement(state, first);
12304 }
12305 else if (tok == TOK_DO) {
12306 do_statement(state, first);
12307 }
12308 else if (tok == TOK_RETURN) {
12309 return_statement(state, first);
12310 }
12311 else if (tok == TOK_BREAK) {
12312 break_statement(state, first);
12313 }
12314 else if (tok == TOK_CONTINUE) {
12315 continue_statement(state, first);
12316 }
12317 else if (tok == TOK_GOTO) {
12318 goto_statement(state, first);
12319 }
12320 else if (tok == TOK_SWITCH) {
12321 switch_statement(state, first);
12322 }
12323 else if (tok == TOK_ASM) {
12324 asm_statement(state, first);
12325 }
12326 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012327 labeled_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012328 }
12329 else if (tok == TOK_CASE) {
12330 case_statement(state, first);
12331 }
12332 else if (tok == TOK_DEFAULT) {
12333 default_statement(state, first);
12334 }
12335 else if (isdecl(tok)) {
12336 /* This handles C99 intermixing of statements and decls */
12337 decl(state, first);
12338 }
12339 else {
12340 expr_statement(state, first);
12341 }
12342}
12343
12344static struct type *param_decl(struct compile_state *state)
12345{
12346 struct type *type;
12347 struct hash_entry *ident;
12348 /* Cheat so the declarator will know we are not global */
Stefan Reinauer14e22772010-04-27 06:56:47 +000012349 start_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012350 ident = 0;
12351 type = decl_specifiers(state);
12352 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012353 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012354 end_scope(state);
12355 return type;
12356}
12357
12358static struct type *param_type_list(struct compile_state *state, struct type *type)
12359{
12360 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012361 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012362 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012363 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012364 while(peek(state) == TOK_COMMA) {
12365 eat(state, TOK_COMMA);
12366 if (peek(state) == TOK_DOTS) {
12367 eat(state, TOK_DOTS);
12368 error(state, 0, "variadic functions not supported");
12369 }
12370 else {
12371 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12372 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012373 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012374 }
12375 }
12376 return ftype;
12377}
12378
Eric Biedermanb138ac82003-04-22 18:44:01 +000012379static struct type *type_name(struct compile_state *state)
12380{
12381 struct type *type;
12382 type = specifier_qualifier_list(state);
12383 /* abstract-declarator (may consume no tokens) */
12384 type = declarator(state, type, 0, 0);
12385 return type;
12386}
12387
12388static struct type *direct_declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012389 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012390 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012391{
Eric Biederman41203d92004-11-08 09:31:09 +000012392 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012393 struct type *outer;
12394 int op;
12395 outer = 0;
12396 arrays_complete(state, type);
12397 switch(peek(state)) {
12398 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012399 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012400 if (!ident) {
12401 error(state, 0, "Unexpected identifier found");
12402 }
12403 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012404 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012405 break;
12406 case TOK_LPAREN:
12407 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012408 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012409 eat(state, TOK_RPAREN);
12410 break;
12411 default:
12412 if (need_ident) {
12413 error(state, 0, "Identifier expected");
12414 }
12415 break;
12416 }
12417 do {
12418 op = 1;
12419 arrays_complete(state, type);
12420 switch(peek(state)) {
12421 case TOK_LPAREN:
12422 eat(state, TOK_LPAREN);
12423 type = param_type_list(state, type);
12424 eat(state, TOK_RPAREN);
12425 break;
12426 case TOK_LBRACKET:
12427 {
12428 unsigned int qualifiers;
12429 struct triple *value;
12430 value = 0;
12431 eat(state, TOK_LBRACKET);
12432 if (peek(state) != TOK_RBRACKET) {
12433 value = constant_expr(state);
12434 integral(state, value);
12435 }
12436 eat(state, TOK_RBRACKET);
12437
12438 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12439 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12440 if (value) {
12441 type->elements = value->u.cval;
12442 free_triple(state, value);
12443 } else {
12444 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12445 op = 0;
12446 }
12447 }
12448 break;
12449 default:
12450 op = 0;
12451 break;
12452 }
12453 } while(op);
12454 if (outer) {
12455 struct type *inner;
12456 arrays_complete(state, type);
12457 FINISHME();
12458 for(inner = outer; inner->left; inner = inner->left)
12459 ;
12460 inner->left = type;
12461 type = outer;
12462 }
12463 return type;
12464}
12465
12466static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012467 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012468 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012469{
12470 while(peek(state) == TOK_STAR) {
12471 eat(state, TOK_STAR);
12472 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12473 }
Eric Biederman41203d92004-11-08 09:31:09 +000012474 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012475 return type;
12476}
12477
Eric Biedermanb138ac82003-04-22 18:44:01 +000012478static struct type *typedef_name(
12479 struct compile_state *state, unsigned int specifiers)
12480{
12481 struct hash_entry *ident;
12482 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012483 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012484 type = ident->sym_ident->type;
12485 specifiers |= type->type & QUAL_MASK;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012486 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
Eric Biedermanb138ac82003-04-22 18:44:01 +000012487 (type->type & (STOR_MASK | QUAL_MASK))) {
12488 type = clone_type(specifiers, type);
12489 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012490 return type;
12491}
12492
12493static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012494 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012495{
Eric Biederman83b991a2003-10-11 06:20:25 +000012496 struct hash_entry *ident;
12497 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012498 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012499 struct type *enum_type;
12500 enum_type = 0;
12501 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012502 eat(state, TOK_ENUM);
12503 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012504 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012505 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012506 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012507 base = 0;
12508 if (!ident || (peek(state) == TOK_LBRACE)) {
12509 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012510 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012511 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12512 enum_type->type_ident = ident;
12513 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012514 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012515 struct hash_entry *eident;
12516 struct triple *value;
12517 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012518 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012519 if (eident->sym_ident) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012520 error(state, 0, "%s already declared",
Eric Biederman83b991a2003-10-11 06:20:25 +000012521 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012522 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012523 eident->tok = TOK_ENUM_CONST;
12524 if (peek(state) == TOK_EQ) {
12525 struct triple *val;
12526 eat(state, TOK_EQ);
12527 val = constant_expr(state);
12528 integral(state, val);
12529 base = val->u.cval;
12530 }
12531 value = int_const(state, &int_type, base);
12532 symbol(state, eident, &eident->sym_ident, value, &int_type);
12533 entry = new_type(TYPE_LIST, 0, 0);
12534 entry->field_ident = eident;
12535 *next = entry;
12536 next = &entry->right;
12537 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012538 if (peek(state) == TOK_COMMA) {
12539 eat(state, TOK_COMMA);
12540 }
12541 } while(peek(state) != TOK_RBRACE);
12542 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012543 if (ident) {
12544 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12545 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012546 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012547 if (ident && ident->sym_tag &&
12548 ident->sym_tag->type &&
12549 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12550 enum_type = clone_type(spec, ident->sym_tag->type);
12551 }
12552 else if (ident && !enum_type) {
12553 error(state, 0, "enum %s undeclared", ident->name);
12554 }
12555 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012556}
12557
Eric Biedermanb138ac82003-04-22 18:44:01 +000012558static struct type *struct_declarator(
12559 struct compile_state *state, struct type *type, struct hash_entry **ident)
12560{
Eric Biederman90089602004-05-28 14:11:54 +000012561 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012562 type = declarator(state, type, ident, 1);
12563 }
Eric Biederman90089602004-05-28 14:11:54 +000012564 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012565 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012566 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012567 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012568 if (value->op != OP_INTCONST) {
12569 error(state, 0, "Invalid constant expression");
12570 }
12571 if (value->u.cval > size_of(state, type)) {
12572 error(state, 0, "bitfield larger than base type");
12573 }
12574 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12575 error(state, 0, "bitfield base not an integer type");
12576 }
12577 type = new_type(TYPE_BITFIELD, type, 0);
12578 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012579 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012580 return type;
12581}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012582
12583static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012584 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012585{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012586 struct type *struct_type;
12587 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012588 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012589 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012590 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012591 struct_type = 0;
12592 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012593 switch(peek(state)) {
12594 case TOK_STRUCT:
12595 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012596 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012597 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012598 break;
12599 case TOK_UNION:
12600 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012601 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012602 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012603 break;
12604 default:
12605 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012606 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012607 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012608 break;
12609 }
12610 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012611 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012612 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012613 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012614 if (!ident || (peek(state) == TOK_LBRACE)) {
12615 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012616 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012617 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012618 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012619 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012620 do {
12621 struct type *base_type;
12622 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012623 base_type = specifier_qualifier_list(state);
12624 do {
12625 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012626 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012627 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012628 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012629 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012630 if (peek(state) == TOK_COMMA) {
12631 done = 0;
12632 eat(state, TOK_COMMA);
12633 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012634 type = clone_type(0, type);
12635 type->field_ident = fident;
12636 if (*next) {
12637 *next = new_type(type_join, *next, type);
12638 next = &((*next)->right);
12639 } else {
12640 *next = type;
12641 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012642 } while(!done);
12643 eat(state, TOK_SEMI);
12644 } while(peek(state) != TOK_RBRACE);
12645 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012646 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012647 struct_type->type_ident = ident;
12648 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012649 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012650 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012651 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012652 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000012653 if (ident && ident->sym_tag &&
12654 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012655 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012656 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012657 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012658 else if (ident && !struct_type) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012659 error(state, 0, "%s %s undeclared",
Eric Biederman90089602004-05-28 14:11:54 +000012660 (type_main == TYPE_STRUCT)?"struct" : "union",
12661 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012662 }
12663 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012664}
12665
12666static unsigned int storage_class_specifier_opt(struct compile_state *state)
12667{
12668 unsigned int specifiers;
12669 switch(peek(state)) {
12670 case TOK_AUTO:
12671 eat(state, TOK_AUTO);
12672 specifiers = STOR_AUTO;
12673 break;
12674 case TOK_REGISTER:
12675 eat(state, TOK_REGISTER);
12676 specifiers = STOR_REGISTER;
12677 break;
12678 case TOK_STATIC:
12679 eat(state, TOK_STATIC);
12680 specifiers = STOR_STATIC;
12681 break;
12682 case TOK_EXTERN:
12683 eat(state, TOK_EXTERN);
12684 specifiers = STOR_EXTERN;
12685 break;
12686 case TOK_TYPEDEF:
12687 eat(state, TOK_TYPEDEF);
12688 specifiers = STOR_TYPEDEF;
12689 break;
12690 default:
12691 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012692 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012693 }
12694 else {
12695 specifiers = STOR_AUTO;
12696 }
12697 }
12698 return specifiers;
12699}
12700
12701static unsigned int function_specifier_opt(struct compile_state *state)
12702{
12703 /* Ignore the inline keyword */
12704 unsigned int specifiers;
12705 specifiers = 0;
12706 switch(peek(state)) {
12707 case TOK_INLINE:
12708 eat(state, TOK_INLINE);
12709 specifiers = STOR_INLINE;
12710 }
12711 return specifiers;
12712}
12713
Eric Biederman90089602004-05-28 14:11:54 +000012714static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12715{
12716 int tok = peek(state);
12717 switch(tok) {
12718 case TOK_COMMA:
12719 case TOK_LPAREN:
12720 /* The empty attribute ignore it */
12721 break;
12722 case TOK_IDENT:
12723 case TOK_ENUM_CONST:
12724 case TOK_TYPE_NAME:
12725 {
12726 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012727 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012728
12729 if (ident == state->i_noinline) {
12730 if (attributes & ATTRIB_ALWAYS_INLINE) {
12731 error(state, 0, "both always_inline and noinline attribtes");
12732 }
12733 attributes |= ATTRIB_NOINLINE;
12734 }
12735 else if (ident == state->i_always_inline) {
12736 if (attributes & ATTRIB_NOINLINE) {
12737 error(state, 0, "both noinline and always_inline attribtes");
12738 }
12739 attributes |= ATTRIB_ALWAYS_INLINE;
12740 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012741 else if (ident == state->i_noreturn) {
12742 // attribute((noreturn)) does nothing (yet?)
12743 }
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -070012744 else if (ident == state->i_unused) {
12745 // attribute((unused)) does nothing (yet?)
12746 }
Stefan Reinauerec664bc2013-05-09 14:06:04 -070012747 else if (ident == state->i_packed) {
12748 // attribute((packed)) does nothing (yet?)
12749 }
Eric Biederman90089602004-05-28 14:11:54 +000012750 else {
12751 error(state, 0, "Unknown attribute:%s", ident->name);
12752 }
12753 break;
12754 }
12755 default:
12756 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12757 break;
12758 }
12759 return attributes;
12760}
12761
12762static unsigned int attribute_list(struct compile_state *state, unsigned type)
12763{
12764 type = attrib(state, type);
12765 while(peek(state) == TOK_COMMA) {
12766 eat(state, TOK_COMMA);
12767 type = attrib(state, type);
12768 }
12769 return type;
12770}
12771
12772static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12773{
12774 if (peek(state) == TOK_ATTRIBUTE) {
12775 eat(state, TOK_ATTRIBUTE);
12776 eat(state, TOK_LPAREN);
12777 eat(state, TOK_LPAREN);
12778 type = attribute_list(state, type);
12779 eat(state, TOK_RPAREN);
12780 eat(state, TOK_RPAREN);
12781 }
12782 return type;
12783}
12784
Eric Biedermanb138ac82003-04-22 18:44:01 +000012785static unsigned int type_qualifiers(struct compile_state *state)
12786{
12787 unsigned int specifiers;
12788 int done;
12789 done = 0;
12790 specifiers = QUAL_NONE;
12791 do {
12792 switch(peek(state)) {
12793 case TOK_CONST:
12794 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012795 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012796 break;
12797 case TOK_VOLATILE:
12798 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012799 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012800 break;
12801 case TOK_RESTRICT:
12802 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012803 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012804 break;
12805 default:
12806 done = 1;
12807 break;
12808 }
12809 } while(!done);
12810 return specifiers;
12811}
12812
12813static struct type *type_specifier(
12814 struct compile_state *state, unsigned int spec)
12815{
12816 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012817 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012818 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012819 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012820 case TOK_VOID:
12821 eat(state, TOK_VOID);
12822 type = new_type(TYPE_VOID | spec, 0, 0);
12823 break;
12824 case TOK_CHAR:
12825 eat(state, TOK_CHAR);
12826 type = new_type(TYPE_CHAR | spec, 0, 0);
12827 break;
12828 case TOK_SHORT:
12829 eat(state, TOK_SHORT);
12830 if (peek(state) == TOK_INT) {
12831 eat(state, TOK_INT);
12832 }
12833 type = new_type(TYPE_SHORT | spec, 0, 0);
12834 break;
12835 case TOK_INT:
12836 eat(state, TOK_INT);
12837 type = new_type(TYPE_INT | spec, 0, 0);
12838 break;
12839 case TOK_LONG:
12840 eat(state, TOK_LONG);
12841 switch(peek(state)) {
12842 case TOK_LONG:
12843 eat(state, TOK_LONG);
12844 error(state, 0, "long long not supported");
12845 break;
12846 case TOK_DOUBLE:
12847 eat(state, TOK_DOUBLE);
12848 error(state, 0, "long double not supported");
12849 break;
12850 case TOK_INT:
12851 eat(state, TOK_INT);
12852 type = new_type(TYPE_LONG | spec, 0, 0);
12853 break;
12854 default:
12855 type = new_type(TYPE_LONG | spec, 0, 0);
12856 break;
12857 }
12858 break;
12859 case TOK_FLOAT:
12860 eat(state, TOK_FLOAT);
12861 error(state, 0, "type float not supported");
12862 break;
12863 case TOK_DOUBLE:
12864 eat(state, TOK_DOUBLE);
12865 error(state, 0, "type double not supported");
12866 break;
12867 case TOK_SIGNED:
12868 eat(state, TOK_SIGNED);
12869 switch(peek(state)) {
12870 case TOK_LONG:
12871 eat(state, TOK_LONG);
12872 switch(peek(state)) {
12873 case TOK_LONG:
12874 eat(state, TOK_LONG);
12875 error(state, 0, "type long long not supported");
12876 break;
12877 case TOK_INT:
12878 eat(state, TOK_INT);
12879 type = new_type(TYPE_LONG | spec, 0, 0);
12880 break;
12881 default:
12882 type = new_type(TYPE_LONG | spec, 0, 0);
12883 break;
12884 }
12885 break;
12886 case TOK_INT:
12887 eat(state, TOK_INT);
12888 type = new_type(TYPE_INT | spec, 0, 0);
12889 break;
12890 case TOK_SHORT:
12891 eat(state, TOK_SHORT);
12892 type = new_type(TYPE_SHORT | spec, 0, 0);
12893 break;
12894 case TOK_CHAR:
12895 eat(state, TOK_CHAR);
12896 type = new_type(TYPE_CHAR | spec, 0, 0);
12897 break;
12898 default:
12899 type = new_type(TYPE_INT | spec, 0, 0);
12900 break;
12901 }
12902 break;
12903 case TOK_UNSIGNED:
12904 eat(state, TOK_UNSIGNED);
12905 switch(peek(state)) {
12906 case TOK_LONG:
12907 eat(state, TOK_LONG);
12908 switch(peek(state)) {
12909 case TOK_LONG:
12910 eat(state, TOK_LONG);
12911 error(state, 0, "unsigned long long not supported");
12912 break;
12913 case TOK_INT:
12914 eat(state, TOK_INT);
12915 type = new_type(TYPE_ULONG | spec, 0, 0);
12916 break;
12917 default:
12918 type = new_type(TYPE_ULONG | spec, 0, 0);
12919 break;
12920 }
12921 break;
12922 case TOK_INT:
12923 eat(state, TOK_INT);
12924 type = new_type(TYPE_UINT | spec, 0, 0);
12925 break;
12926 case TOK_SHORT:
12927 eat(state, TOK_SHORT);
12928 type = new_type(TYPE_USHORT | spec, 0, 0);
12929 break;
12930 case TOK_CHAR:
12931 eat(state, TOK_CHAR);
12932 type = new_type(TYPE_UCHAR | spec, 0, 0);
12933 break;
12934 default:
12935 type = new_type(TYPE_UINT | spec, 0, 0);
12936 break;
12937 }
12938 break;
12939 /* struct or union specifier */
12940 case TOK_STRUCT:
12941 case TOK_UNION:
12942 type = struct_or_union_specifier(state, spec);
12943 break;
12944 /* enum-spefifier */
12945 case TOK_ENUM:
12946 type = enum_specifier(state, spec);
12947 break;
12948 /* typedef name */
12949 case TOK_TYPE_NAME:
12950 type = typedef_name(state, spec);
12951 break;
12952 default:
Stefan Reinauer14e22772010-04-27 06:56:47 +000012953 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012954 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012955 break;
12956 }
12957 return type;
12958}
12959
12960static int istype(int tok)
12961{
12962 switch(tok) {
12963 case TOK_CONST:
12964 case TOK_RESTRICT:
12965 case TOK_VOLATILE:
12966 case TOK_VOID:
12967 case TOK_CHAR:
12968 case TOK_SHORT:
12969 case TOK_INT:
12970 case TOK_LONG:
12971 case TOK_FLOAT:
12972 case TOK_DOUBLE:
12973 case TOK_SIGNED:
12974 case TOK_UNSIGNED:
12975 case TOK_STRUCT:
12976 case TOK_UNION:
12977 case TOK_ENUM:
12978 case TOK_TYPE_NAME:
12979 return 1;
12980 default:
12981 return 0;
12982 }
12983}
12984
12985
12986static struct type *specifier_qualifier_list(struct compile_state *state)
12987{
12988 struct type *type;
12989 unsigned int specifiers = 0;
12990
12991 /* type qualifiers */
12992 specifiers |= type_qualifiers(state);
12993
12994 /* type specifier */
12995 type = type_specifier(state, specifiers);
12996
12997 return type;
12998}
12999
Stefan Reinauer50542a82007-10-24 11:14:14 +000013000#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013001static int isdecl_specifier(int tok)
13002{
13003 switch(tok) {
13004 /* storage class specifier */
13005 case TOK_AUTO:
13006 case TOK_REGISTER:
13007 case TOK_STATIC:
13008 case TOK_EXTERN:
13009 case TOK_TYPEDEF:
13010 /* type qualifier */
13011 case TOK_CONST:
13012 case TOK_RESTRICT:
13013 case TOK_VOLATILE:
13014 /* type specifiers */
13015 case TOK_VOID:
13016 case TOK_CHAR:
13017 case TOK_SHORT:
13018 case TOK_INT:
13019 case TOK_LONG:
13020 case TOK_FLOAT:
13021 case TOK_DOUBLE:
13022 case TOK_SIGNED:
13023 case TOK_UNSIGNED:
13024 /* struct or union specifier */
13025 case TOK_STRUCT:
13026 case TOK_UNION:
13027 /* enum-spefifier */
13028 case TOK_ENUM:
13029 /* typedef name */
13030 case TOK_TYPE_NAME:
13031 /* function specifiers */
13032 case TOK_INLINE:
13033 return 1;
13034 default:
13035 return 0;
13036 }
13037}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013038#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013039
13040static struct type *decl_specifiers(struct compile_state *state)
13041{
13042 struct type *type;
13043 unsigned int specifiers;
13044 /* I am overly restrictive in the arragement of specifiers supported.
13045 * C is overly flexible in this department it makes interpreting
13046 * the parse tree difficult.
13047 */
13048 specifiers = 0;
13049
13050 /* storage class specifier */
13051 specifiers |= storage_class_specifier_opt(state);
13052
13053 /* function-specifier */
13054 specifiers |= function_specifier_opt(state);
13055
Eric Biederman90089602004-05-28 14:11:54 +000013056 /* attributes */
13057 specifiers |= attributes_opt(state, 0);
13058
Eric Biedermanb138ac82003-04-22 18:44:01 +000013059 /* type qualifier */
13060 specifiers |= type_qualifiers(state);
13061
13062 /* type specifier */
13063 type = type_specifier(state, specifiers);
13064 return type;
13065}
13066
Eric Biederman00443072003-06-24 12:34:45 +000013067struct field_info {
13068 struct type *type;
13069 size_t offset;
13070};
13071
13072static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013073{
13074 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013075 struct field_info info;
13076 info.offset = ~0U;
13077 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013078 do {
13079 switch(peek(state)) {
13080 case TOK_LBRACKET:
13081 {
13082 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013083 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13084 error(state, 0, "Array designator not in array initializer");
13085 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013086 eat(state, TOK_LBRACKET);
13087 value = constant_expr(state);
13088 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013089
13090 info.type = type->left;
13091 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013092 break;
13093 }
13094 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013095 {
13096 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013097 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13098 ((type->type & TYPE_MASK) != TYPE_UNION))
13099 {
Eric Biederman00443072003-06-24 12:34:45 +000013100 error(state, 0, "Struct designator not in struct initializer");
13101 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013102 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013103 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013104 info.offset = field_offset(state, type, field);
13105 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013106 break;
Eric Biederman00443072003-06-24 12:34:45 +000013107 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013108 default:
13109 error(state, 0, "Invalid designator");
13110 }
13111 tok = peek(state);
13112 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13113 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013114 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013115}
13116
13117static struct triple *initializer(
13118 struct compile_state *state, struct type *type)
13119{
13120 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013121#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013122#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013123#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013124 if (peek(state) != TOK_LBRACE) {
13125 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013126 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13127 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13128 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13129 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13130 (equiv_types(type->left, result->type->left))) {
13131 type->elements = result->type->elements;
13132 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013133 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013134 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13135 (type->type & TYPE_MASK) != TYPE_ARRAY)
13136 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013137 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013138 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013139 if (!is_init_compatible(state, type, result->type)) {
13140 error(state, 0, "Incompatible types in initializer");
13141 }
13142 if (!equiv_types(type, result->type)) {
13143 result = mk_cast_expr(state, type, result);
13144 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013145 }
13146 else {
13147 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013148 size_t max_offset;
13149 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013150 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013151 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13152 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13153 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013154 }
Eric Biederman00443072003-06-24 12:34:45 +000013155 info.offset = 0;
13156 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013157 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13158 info.type = next_field(state, type, 0);
13159 }
Eric Biederman00443072003-06-24 12:34:45 +000013160 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13161 max_offset = 0;
13162 } else {
13163 max_offset = size_of(state, type);
13164 }
Eric Biederman90089602004-05-28 14:11:54 +000013165 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013166 eat(state, TOK_LBRACE);
13167 do {
13168 struct triple *value;
13169 struct type *value_type;
13170 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013171 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013172 int tok;
13173 comma = 0;
13174 tok = peek(state);
13175 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013176 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013177 }
Eric Biederman00443072003-06-24 12:34:45 +000013178 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13179 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013180 error(state, 0, "element beyond bounds");
13181 }
Eric Biederman00443072003-06-24 12:34:45 +000013182 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013183 value = eval_const_expr(state, initializer(state, value_type));
13184 value_size = size_of(state, value_type);
13185 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013186 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13187 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013188 void *old_buf;
13189 size_t old_size;
13190 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013191 old_size = max_offset;
13192 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013193 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13194 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013195 xfree(old_buf);
13196 }
Eric Biederman90089602004-05-28 14:11:54 +000013197 dest = ((char *)buf) + bits_to_bytes(info.offset);
13198#if DEBUG_INITIALIZER
Stefan Reinauer14e22772010-04-27 06:56:47 +000013199 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000013200 dest - buf,
13201 bits_to_bytes(max_offset),
13202 bits_to_bytes(value_size),
13203 value->op);
13204#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013205 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013206 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013207 }
Eric Biederman90089602004-05-28 14:11:54 +000013208 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13209#if DEBUG_INITIALIZER
13210 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13211#endif
Eric Biederman00443072003-06-24 12:34:45 +000013212 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013213 }
Eric Biederman90089602004-05-28 14:11:54 +000013214 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013215 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013216 }
Eric Biederman90089602004-05-28 14:11:54 +000013217 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013218 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013219 }
13220 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013221 internal_error(state, 0, "unhandled constant initializer");
13222 }
Eric Biederman00443072003-06-24 12:34:45 +000013223 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013224 if (peek(state) == TOK_COMMA) {
13225 eat(state, TOK_COMMA);
13226 comma = 1;
13227 }
Eric Biederman00443072003-06-24 12:34:45 +000013228 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013229 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13230 info.type = next_field(state, type, info.type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013231 info.offset = field_offset(state, type,
Eric Biederman03b59862003-06-24 14:27:37 +000013232 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013233 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013234 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013235 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13236 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13237 type->elements = max_offset / size_of(state, type->left);
13238 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013239 eat(state, TOK_RBRACE);
13240 result = triple(state, OP_BLOBCONST, type, 0, 0);
13241 result->u.blob = buf;
13242 }
13243 return result;
13244}
13245
Eric Biederman90089602004-05-28 14:11:54 +000013246static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013247{
13248 /* Make a second pass and finish anything outstanding
13249 * with respect to branches. The only outstanding item
13250 * is to see if there are goto to labels that have not
13251 * been defined and to error about them.
13252 */
13253 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013254 struct triple *ins;
13255 /* Also error on branches that do not use their targets */
13256 ins = first;
13257 do {
13258 if (!triple_is_ret(state, ins)) {
13259 struct triple **expr ;
13260 struct triple_set *set;
13261 expr = triple_targ(state, ins, 0);
13262 for(; expr; expr = triple_targ(state, ins, expr)) {
13263 struct triple *targ;
13264 targ = *expr;
13265 for(set = targ?targ->use:0; set; set = set->next) {
13266 if (set->member == ins) {
13267 break;
13268 }
13269 }
13270 if (!set) {
13271 internal_error(state, ins, "targ not used");
13272 }
13273 }
13274 }
13275 ins = ins->next;
13276 } while(ins != first);
13277 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013278 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13279 struct hash_entry *entry;
13280 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13281 struct triple *ins;
13282 if (!entry->sym_label) {
13283 continue;
13284 }
13285 ins = entry->sym_label->def;
13286 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13287 error(state, ins, "label `%s' used but not defined",
13288 entry->name);
13289 }
13290 }
13291 }
13292}
13293
Eric Biedermanb138ac82003-04-22 18:44:01 +000013294static struct triple *function_definition(
13295 struct compile_state *state, struct type *type)
13296{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013297 struct triple *def, *tmp, *first, *end, *retvar, *ret;
Eric Biederman90089602004-05-28 14:11:54 +000013298 struct triple *fname;
13299 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013300 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013301 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013302 int i;
13303 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13304 error(state, 0, "Invalid function header");
13305 }
13306
13307 /* Verify the function type */
13308 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13309 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013310 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013311 error(state, 0, "Invalid function parameters");
13312 }
13313 param = type->right;
13314 i = 0;
13315 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13316 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013317 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013318 error(state, 0, "No identifier for parameter %d\n", i);
13319 }
13320 param = param->right;
13321 }
13322 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013323 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013324 error(state, 0, "No identifier for paramter %d\n", i);
13325 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013326
Eric Biedermanb138ac82003-04-22 18:44:01 +000013327 /* Get a list of statements for this function. */
13328 def = triple(state, OP_LIST, type, 0, 0);
13329
13330 /* Start a new scope for the passed parameters */
13331 start_scope(state);
13332
13333 /* Put a label at the very start of a function */
13334 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013335 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013336
13337 /* Put a label at the very end of a function */
13338 end = label(state);
13339 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013340 /* Remember where return goes */
13341 ident = state->i_return;
13342 symbol(state, ident, &ident->sym_ident, end, end->type);
13343
Eric Biederman90089602004-05-28 14:11:54 +000013344 /* Get the initial closure type */
13345 ctype = new_type(TYPE_JOIN, &void_type, 0);
13346 ctype->elements = 1;
13347
13348 /* Add a variable for the return value */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013349 crtype = new_type(TYPE_TUPLE,
Eric Biederman90089602004-05-28 14:11:54 +000013350 /* Remove all type qualifiers from the return type */
13351 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13352 crtype->elements = 2;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013353 flatten(state, end, variable(state, crtype));
Eric Biederman90089602004-05-28 14:11:54 +000013354
Eric Biederman5ade04a2003-10-22 04:03:46 +000013355 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013356 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013357
13358 /* Add in the return instruction */
13359 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13360 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013361
13362 /* Walk through the parameters and create symbol table entries
13363 * for them.
13364 */
13365 param = type->right;
13366 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013367 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013368 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013369 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013370 flatten(state, end, tmp);
13371 param = param->right;
13372 }
13373 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13374 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013375 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013376 tmp = variable(state, param);
13377 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13378 flatten(state, end, tmp);
13379 }
Eric Biederman90089602004-05-28 14:11:54 +000013380
13381 /* Add the declaration static const char __func__ [] = "func-name" */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013382 fname_type = new_type(TYPE_ARRAY,
Eric Biederman90089602004-05-28 14:11:54 +000013383 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13384 fname_type->type |= QUAL_CONST | STOR_STATIC;
13385 fname_type->elements = strlen(state->function) + 1;
13386
13387 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13388 fname->u.blob = (void *)state->function;
13389 fname = flatten(state, end, fname);
13390
13391 ident = state->i___func__;
13392 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013393
13394 /* Remember which function I am compiling.
13395 * Also assume the last defined function is the main function.
13396 */
13397 state->main_function = def;
13398
13399 /* Now get the actual function definition */
13400 compound_statement(state, end);
13401
Eric Biederman153ea352003-06-20 14:43:20 +000013402 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013403 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013404
Eric Biedermanb138ac82003-04-22 18:44:01 +000013405 /* Remove the parameter scope */
13406 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013407
Eric Biederman5ade04a2003-10-22 04:03:46 +000013408
13409 /* Remember I have defined a function */
13410 if (!state->functions) {
13411 state->functions = def;
13412 } else {
13413 insert_triple(state, state->functions, def);
13414 }
13415 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013416 FILE *fp = state->dbgout;
13417 fprintf(fp, "\n");
13418 loc(fp, state, 0);
13419 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13420 display_func(state, fp, def);
13421 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013422 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013423
13424 return def;
13425}
13426
Stefan Reinauer14e22772010-04-27 06:56:47 +000013427static struct triple *do_decl(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013428 struct type *type, struct hash_entry *ident)
13429{
13430 struct triple *def;
13431 def = 0;
13432 /* Clean up the storage types used */
13433 switch (type->type & STOR_MASK) {
13434 case STOR_AUTO:
13435 case STOR_STATIC:
13436 /* These are the good types I am aiming for */
13437 break;
13438 case STOR_REGISTER:
13439 type->type &= ~STOR_MASK;
13440 type->type |= STOR_AUTO;
13441 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013442 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013443 case STOR_EXTERN:
13444 type->type &= ~STOR_MASK;
13445 type->type |= STOR_STATIC;
13446 break;
13447 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013448 if (!ident) {
13449 error(state, 0, "typedef without name");
13450 }
13451 symbol(state, ident, &ident->sym_ident, 0, type);
13452 ident->tok = TOK_TYPE_NAME;
13453 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013454 break;
13455 default:
13456 internal_error(state, 0, "Undefined storage class");
13457 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013458 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
Patrick Georgi11a7db32013-02-16 20:16:34 +010013459 // ignore function prototypes
13460 return def;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013461 }
Eric W. Biederman8483b702010-03-17 00:23:34 +000013462 if (ident &&
13463 ((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13464 ((type->type & STOR_MASK) != STOR_STATIC))
13465 error(state, 0, "non static arrays not supported");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013466 if (ident &&
Eric Biederman00443072003-06-24 12:34:45 +000013467 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013468 ((type->type & QUAL_CONST) == 0)) {
13469 error(state, 0, "non const static variables not supported");
13470 }
13471 if (ident) {
13472 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013473 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013474 }
13475 return def;
13476}
13477
13478static void decl(struct compile_state *state, struct triple *first)
13479{
13480 struct type *base_type, *type;
13481 struct hash_entry *ident;
13482 struct triple *def;
13483 int global;
13484 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13485 base_type = decl_specifiers(state);
13486 ident = 0;
13487 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013488 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013489 if (global && ident && (peek(state) == TOK_LBRACE)) {
13490 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013491 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013492 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013493 def = function_definition(state, type);
13494 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013495 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013496 }
13497 else {
13498 int done;
13499 flatten(state, first, do_decl(state, type, ident));
13500 /* type or variable definition */
13501 do {
13502 done = 1;
13503 if (peek(state) == TOK_EQ) {
13504 if (!ident) {
13505 error(state, 0, "cannot assign to a type");
13506 }
13507 eat(state, TOK_EQ);
13508 flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013509 init_expr(state,
13510 ident->sym_ident->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013511 initializer(state, type)));
13512 }
13513 arrays_complete(state, type);
13514 if (peek(state) == TOK_COMMA) {
13515 eat(state, TOK_COMMA);
13516 ident = 0;
13517 type = declarator(state, base_type, &ident, 0);
13518 flatten(state, first, do_decl(state, type, ident));
13519 done = 0;
13520 }
13521 } while(!done);
13522 eat(state, TOK_SEMI);
13523 }
13524}
13525
13526static void decls(struct compile_state *state)
13527{
13528 struct triple *list;
13529 int tok;
13530 list = label(state);
13531 while(1) {
13532 tok = peek(state);
13533 if (tok == TOK_EOF) {
13534 return;
13535 }
13536 if (tok == TOK_SPACE) {
13537 eat(state, TOK_SPACE);
13538 }
13539 decl(state, list);
13540 if (list->next != list) {
13541 error(state, 0, "global variables not supported");
13542 }
13543 }
13544}
13545
Stefan Reinauer14e22772010-04-27 06:56:47 +000013546/*
Eric Biederman5ade04a2003-10-22 04:03:46 +000013547 * Function inlining
13548 */
Eric Biederman90089602004-05-28 14:11:54 +000013549struct triple_reg_set {
13550 struct triple_reg_set *next;
13551 struct triple *member;
13552 struct triple *new;
13553};
13554struct reg_block {
13555 struct block *block;
13556 struct triple_reg_set *in;
13557 struct triple_reg_set *out;
13558 int vertex;
13559};
13560static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13561static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13562static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13563static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13564static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13565 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13566 void *arg);
13567static void print_block(
13568 struct compile_state *state, struct block *block, void *arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013569static int do_triple_set(struct triple_reg_set **head,
Eric Biederman90089602004-05-28 14:11:54 +000013570 struct triple *member, struct triple *new_member);
13571static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13572static struct reg_block *compute_variable_lifetimes(
13573 struct compile_state *state, struct basic_blocks *bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013574static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013575 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013576#if DEBUG_EXPLICIT_CLOSURES
Stefan Reinauer14e22772010-04-27 06:56:47 +000013577static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013578 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013579#endif
Eric Biederman90089602004-05-28 14:11:54 +000013580
Eric Biederman5ade04a2003-10-22 04:03:46 +000013581
13582static struct triple *call(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013583 struct triple *retvar, struct triple *ret_addr,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013584 struct triple *targ, struct triple *ret)
13585{
13586 struct triple *call;
13587
13588 if (!retvar || !is_lvalue(state, retvar)) {
13589 internal_error(state, 0, "writing to a non lvalue?");
13590 }
13591 write_compatible(state, retvar->type, &void_ptr_type);
13592
13593 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13594 TARG(call, 0) = targ;
13595 MISC(call, 0) = ret;
13596 if (!targ || (targ->op != OP_LABEL)) {
13597 internal_error(state, 0, "call not to a label");
13598 }
13599 if (!ret || (ret->op != OP_RET)) {
13600 internal_error(state, 0, "call not matched with return");
13601 }
13602 return call;
13603}
13604
Eric Biederman5ade04a2003-10-22 04:03:46 +000013605static void walk_functions(struct compile_state *state,
13606 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13607 void *arg)
13608{
13609 struct triple *func, *first;
13610 func = first = state->functions;
13611 do {
13612 cb(state, func, arg);
13613 func = func->next;
13614 } while(func != first);
13615}
13616
Eric Biederman90089602004-05-28 14:11:54 +000013617static void reverse_walk_functions(struct compile_state *state,
13618 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13619 void *arg)
13620{
13621 struct triple *func, *first;
13622 func = first = state->functions;
13623 do {
13624 func = func->prev;
13625 cb(state, func, arg);
13626 } while(func != first);
13627}
13628
13629
13630static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13631{
13632 struct triple *ptr, *first;
13633 if (func->u.cval == 0) {
13634 return;
13635 }
13636 ptr = first = RHS(func, 0);
13637 do {
13638 if (ptr->op == OP_FCALL) {
13639 struct triple *called_func;
13640 called_func = MISC(ptr, 0);
13641 /* Mark the called function as used */
13642 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13643 called_func->u.cval++;
13644 }
13645 /* Remove the called function from the list */
13646 called_func->prev->next = called_func->next;
13647 called_func->next->prev = called_func->prev;
13648
13649 /* Place the called function before me on the list */
13650 called_func->next = func;
13651 called_func->prev = func->prev;
13652 called_func->prev->next = called_func;
13653 called_func->next->prev = called_func;
13654 }
13655 ptr = ptr->next;
13656 } while(ptr != first);
13657 func->id |= TRIPLE_FLAG_FLATTENED;
13658}
13659
13660static void mark_live_functions(struct compile_state *state)
13661{
Stefan Reinauer14e22772010-04-27 06:56:47 +000013662 /* Ensure state->main_function is the last function in
Eric Biederman90089602004-05-28 14:11:54 +000013663 * the list of functions.
13664 */
13665 if ((state->main_function->next != state->functions) ||
13666 (state->functions->prev != state->main_function)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013667 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +000013668 "state->main_function is not at the end of the function list ");
13669 }
13670 state->main_function->u.cval = 1;
13671 reverse_walk_functions(state, mark_live, 0);
13672}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013673
Stefan Reinauer14e22772010-04-27 06:56:47 +000013674static int local_triple(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013675 struct triple *func, struct triple *ins)
13676{
13677 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13678#if 0
13679 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013680 FILE *fp = state->errout;
13681 fprintf(fp, "global: ");
13682 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013683 }
13684#endif
13685 return local;
13686}
13687
Stefan Reinauer14e22772010-04-27 06:56:47 +000013688struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013689 struct occurance *base_occurance)
13690{
13691 struct triple *nfunc;
13692 struct triple *nfirst, *ofirst;
13693 struct triple *new, *old;
13694
13695 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013696 FILE *fp = state->dbgout;
13697 fprintf(fp, "\n");
13698 loc(fp, state, 0);
13699 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13700 display_func(state, fp, ofunc);
13701 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013702 }
13703
13704 /* Make a new copy of the old function */
13705 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13706 nfirst = 0;
13707 ofirst = old = RHS(ofunc, 0);
13708 do {
13709 struct triple *new;
13710 struct occurance *occurance;
13711 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013712 old_lhs = old->lhs;
13713 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013714 occurance = inline_occurance(state, base_occurance, old->occurance);
13715 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13716 MISC(old, 0)->u.cval += 1;
13717 }
13718 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13719 occurance);
13720 if (!triple_stores_block(state, new)) {
13721 memcpy(&new->u, &old->u, sizeof(new->u));
13722 }
13723 if (!nfirst) {
13724 RHS(nfunc, 0) = nfirst = new;
13725 }
13726 else {
13727 insert_triple(state, nfirst, new);
13728 }
13729 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013730 new->id |= old->id & TRIPLE_FLAG_COPY;
Stefan Reinauer14e22772010-04-27 06:56:47 +000013731
Eric Biederman5ade04a2003-10-22 04:03:46 +000013732 /* During the copy remember new as user of old */
13733 use_triple(old, new);
13734
Eric Biederman5ade04a2003-10-22 04:03:46 +000013735 /* Remember which instructions are local */
13736 old->id |= TRIPLE_FLAG_LOCAL;
13737 old = old->next;
13738 } while(old != ofirst);
13739
13740 /* Make a second pass to fix up any unresolved references */
13741 old = ofirst;
13742 new = nfirst;
13743 do {
13744 struct triple **oexpr, **nexpr;
13745 int count, i;
13746 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013747 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013748 for(i = 0; i < count; i++) {
13749 oexpr = &old->param[i];
13750 nexpr = &new->param[i];
13751 if (*oexpr && !*nexpr) {
13752 if (!local_triple(state, ofunc, *oexpr)) {
13753 *nexpr = *oexpr;
13754 }
13755 else if ((*oexpr)->use) {
13756 *nexpr = (*oexpr)->use->member;
13757 }
13758 if (*nexpr == old) {
13759 internal_error(state, 0, "new == old?");
13760 }
13761 use_triple(*nexpr, new);
13762 }
13763 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013764 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013765 }
13766 }
13767 old = old->next;
13768 new = new->next;
13769 } while((old != ofirst) && (new != nfirst));
Stefan Reinauer14e22772010-04-27 06:56:47 +000013770
Eric Biederman5ade04a2003-10-22 04:03:46 +000013771 /* Make a third pass to cleanup the extra useses */
13772 old = ofirst;
13773 new = nfirst;
13774 do {
13775 unuse_triple(old, new);
13776 /* Forget which instructions are local */
13777 old->id &= ~TRIPLE_FLAG_LOCAL;
13778 old = old->next;
13779 new = new->next;
13780 } while ((old != ofirst) && (new != nfirst));
13781 return nfunc;
13782}
13783
Eric Biederman90089602004-05-28 14:11:54 +000013784static void expand_inline_call(
13785 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013786{
13787 /* Inline the function call */
13788 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013789 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013790 struct triple *end, *nend;
13791 int pvals, i;
13792
13793 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013794 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013795 if (ofunc->op != OP_LIST) {
13796 internal_error(state, 0, "improper function");
13797 }
Eric Biederman90089602004-05-28 14:11:54 +000013798 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013799 /* Prepend the parameter reading into the new function list */
13800 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013801 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013802 for(i = 0; i < pvals; i++) {
13803 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013804 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013805 atype = ptype;
13806 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13807 atype = ptype->left;
13808 }
Eric Biederman90089602004-05-28 14:11:54 +000013809 param = farg(state, nfunc, i);
13810 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13811 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013812 }
Eric Biederman90089602004-05-28 14:11:54 +000013813 arg = RHS(fcall, i);
13814 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013815 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013816 }
13817 result = 0;
13818 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013819 result = read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000013820 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013821 }
13822 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013823 FILE *fp = state->dbgout;
13824 fprintf(fp, "\n");
13825 loc(fp, state, 0);
13826 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13827 display_func(state, fp, nfunc);
13828 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013829 }
13830
Stefan Reinauer14e22772010-04-27 06:56:47 +000013831 /*
13832 * Get rid of the extra triples
Eric Biederman90089602004-05-28 14:11:54 +000013833 */
13834 /* Remove the read of the return address */
13835 ins = RHS(nfunc, 0)->prev->prev;
13836 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13837 internal_error(state, ins, "Not return addres read?");
13838 }
13839 release_triple(state, ins);
13840 /* Remove the return instruction */
13841 ins = RHS(nfunc, 0)->prev;
13842 if (ins->op != OP_RET) {
13843 internal_error(state, ins, "Not return?");
13844 }
13845 release_triple(state, ins);
13846 /* Remove the retaddres variable */
13847 retvar = fretaddr(state, nfunc);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013848 if ((retvar->lhs != 1) ||
Eric Biederman90089602004-05-28 14:11:54 +000013849 (retvar->op != OP_ADECL) ||
13850 (retvar->next->op != OP_PIECE) ||
13851 (MISC(retvar->next, 0) != retvar)) {
13852 internal_error(state, retvar, "Not the return address?");
13853 }
13854 release_triple(state, retvar->next);
13855 release_triple(state, retvar);
13856
13857 /* Remove the label at the start of the function */
13858 ins = RHS(nfunc, 0);
13859 if (ins->op != OP_LABEL) {
13860 internal_error(state, ins, "Not label?");
13861 }
13862 nfirst = ins->next;
13863 free_triple(state, ins);
13864 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013865 RHS(nfunc, 0) = 0;
13866 free_triple(state, nfunc);
13867
13868 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013869 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013870 nend = nfirst->prev;
13871 end->next = nfirst;
13872 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013873 nend->next = fcall;
13874 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013875
Eric Biederman90089602004-05-28 14:11:54 +000013876 /* Now the result reading code */
13877 if (result) {
13878 result = flatten(state, fcall, result);
13879 propogate_use(state, fcall, result);
13880 }
13881
13882 /* Release the original fcall instruction */
13883 release_triple(state, fcall);
13884
13885 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013886}
13887
Eric Biederman90089602004-05-28 14:11:54 +000013888/*
13889 *
13890 * Type of the result variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +000013891 *
Eric Biederman90089602004-05-28 14:11:54 +000013892 * result
13893 * |
13894 * +----------+------------+
13895 * | |
13896 * union of closures result_type
13897 * |
13898 * +------------------+---------------+
13899 * | |
13900 * closure1 ... closuerN
Stefan Reinauer14e22772010-04-27 06:56:47 +000013901 * | |
Eric Biederman90089602004-05-28 14:11:54 +000013902 * +----+--+-+--------+-----+ +----+----+---+-----+
13903 * | | | | | | | | |
13904 * var1 var2 var3 ... varN result var1 var2 ... varN result
13905 * |
13906 * +--------+---------+
13907 * | |
13908 * union of closures result_type
13909 * |
13910 * +-----+-------------------+
13911 * | |
13912 * closure1 ... closureN
13913 * | |
13914 * +-----+---+----+----+ +----+---+----+-----+
13915 * | | | | | | | |
13916 * var1 var2 ... varN result var1 var2 ... varN result
13917 */
13918
Stefan Reinauer14e22772010-04-27 06:56:47 +000013919static int add_closure_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013920 struct triple *func, struct type *closure_type)
13921{
13922 struct type *type, *ctype, **next;
13923 struct triple *var, *new_var;
13924 int i;
13925
13926#if 0
13927 FILE *fp = state->errout;
13928 fprintf(fp, "original_type: ");
13929 name_of(fp, fresult(state, func)->type);
13930 fprintf(fp, "\n");
13931#endif
13932 /* find the original type */
13933 var = fresult(state, func);
13934 type = var->type;
13935 if (type->elements != 2) {
13936 internal_error(state, var, "bad return type");
13937 }
13938
13939 /* Find the complete closure type and update it */
13940 ctype = type->left->left;
13941 next = &ctype->left;
13942 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13943 next = &(*next)->right;
13944 }
13945 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13946 ctype->elements += 1;
13947
13948#if 0
13949 fprintf(fp, "new_type: ");
13950 name_of(fp, type);
13951 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013952 fprintf(fp, "ctype: %p %d bits: %d ",
Eric Biederman90089602004-05-28 14:11:54 +000013953 ctype, ctype->elements, reg_size_of(state, ctype));
13954 name_of(fp, ctype);
13955 fprintf(fp, "\n");
13956#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000013957
Eric Biederman90089602004-05-28 14:11:54 +000013958 /* Regenerate the variable with the new type definition */
13959 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13960 new_var->id |= TRIPLE_FLAG_FLATTENED;
13961 for(i = 0; i < new_var->lhs; i++) {
13962 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13963 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013964
Eric Biederman90089602004-05-28 14:11:54 +000013965 /* Point everyone at the new variable */
13966 propogate_use(state, var, new_var);
13967
13968 /* Release the original variable */
13969 for(i = 0; i < var->lhs; i++) {
13970 release_triple(state, LHS(var, i));
13971 }
13972 release_triple(state, var);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013973
Eric Biederman90089602004-05-28 14:11:54 +000013974 /* Return the index of the added closure type */
13975 return ctype->elements - 1;
13976}
13977
13978static struct triple *closure_expr(struct compile_state *state,
13979 struct triple *func, int closure_idx, int var_idx)
13980{
13981 return deref_index(state,
13982 deref_index(state,
13983 deref_index(state, fresult(state, func), 0),
13984 closure_idx),
13985 var_idx);
13986}
13987
13988
13989static void insert_triple_set(
13990 struct triple_reg_set **head, struct triple *member)
13991{
13992 struct triple_reg_set *new;
13993 new = xcmalloc(sizeof(*new), "triple_set");
13994 new->member = member;
13995 new->new = 0;
13996 new->next = *head;
13997 *head = new;
13998}
13999
14000static int ordered_triple_set(
14001 struct triple_reg_set **head, struct triple *member)
14002{
14003 struct triple_reg_set **ptr;
14004 if (!member)
14005 return 0;
14006 ptr = head;
14007 while(*ptr) {
14008 if (member == (*ptr)->member) {
14009 return 0;
14010 }
14011 /* keep the list ordered */
14012 if (member->id < (*ptr)->member->id) {
14013 break;
14014 }
14015 ptr = &(*ptr)->next;
14016 }
14017 insert_triple_set(ptr, member);
14018 return 1;
14019}
14020
14021
14022static void free_closure_variables(struct compile_state *state,
14023 struct triple_reg_set **enclose)
14024{
14025 struct triple_reg_set *entry, *next;
14026 for(entry = *enclose; entry; entry = next) {
14027 next = entry->next;
14028 do_triple_unset(enclose, entry->member);
14029 }
14030}
14031
14032static int lookup_closure_index(struct compile_state *state,
14033 struct triple *me, struct triple *val)
14034{
14035 struct triple *first, *ins, *next;
14036 first = RHS(me, 0);
14037 ins = next = first;
14038 do {
14039 struct triple *result;
14040 struct triple *index0, *index1, *index2, *read, *write;
14041 ins = next;
14042 next = ins->next;
14043 if (ins->op != OP_CALL) {
14044 continue;
14045 }
14046 /* I am at a previous call point examine it closely */
14047 if (ins->next->op != OP_LABEL) {
14048 internal_error(state, ins, "call not followed by label");
14049 }
14050 /* Does this call does not enclose any variables? */
14051 if ((ins->next->next->op != OP_INDEX) ||
14052 (ins->next->next->u.cval != 0) ||
14053 (result = MISC(ins->next->next, 0)) ||
14054 (result->id & TRIPLE_FLAG_LOCAL)) {
14055 continue;
14056 }
14057 index0 = ins->next->next;
14058 /* The pattern is:
14059 * 0 index result < 0 >
14060 * 1 index 0 < ? >
14061 * 2 index 1 < ? >
14062 * 3 read 2
14063 * 4 write 3 var
14064 */
14065 for(index0 = ins->next->next;
14066 (index0->op == OP_INDEX) &&
14067 (MISC(index0, 0) == result) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000014068 (index0->u.cval == 0) ;
Eric Biederman90089602004-05-28 14:11:54 +000014069 index0 = write->next)
14070 {
14071 index1 = index0->next;
14072 index2 = index1->next;
14073 read = index2->next;
14074 write = read->next;
14075 if ((index0->op != OP_INDEX) ||
14076 (index1->op != OP_INDEX) ||
14077 (index2->op != OP_INDEX) ||
14078 (read->op != OP_READ) ||
14079 (write->op != OP_WRITE) ||
14080 (MISC(index1, 0) != index0) ||
14081 (MISC(index2, 0) != index1) ||
14082 (RHS(read, 0) != index2) ||
14083 (RHS(write, 0) != read)) {
14084 internal_error(state, index0, "bad var read");
14085 }
14086 if (MISC(write, 0) == val) {
14087 return index2->u.cval;
14088 }
14089 }
14090 } while(next != first);
14091 return -1;
14092}
14093
14094static inline int enclose_triple(struct triple *ins)
14095{
14096 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14097}
14098
14099static void compute_closure_variables(struct compile_state *state,
14100 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14101{
14102 struct triple_reg_set *set, *vars, **last_var;
14103 struct basic_blocks bb;
14104 struct reg_block *rb;
14105 struct block *block;
14106 struct triple *old_result, *first, *ins;
14107 size_t count, idx;
14108 unsigned long used_indicies;
14109 int i, max_index;
14110#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14111#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
Stefan Reinauer14e22772010-04-27 06:56:47 +000014112 struct {
Eric Biederman90089602004-05-28 14:11:54 +000014113 unsigned id;
14114 int index;
14115 } *info;
14116
Stefan Reinauer14e22772010-04-27 06:56:47 +000014117
Eric Biederman90089602004-05-28 14:11:54 +000014118 /* Find the basic blocks of this function */
14119 bb.func = me;
14120 bb.first = RHS(me, 0);
14121 old_result = 0;
14122 if (!triple_is_ret(state, bb.first->prev)) {
14123 bb.func = 0;
14124 } else {
14125 old_result = fresult(state, me);
14126 }
14127 analyze_basic_blocks(state, &bb);
14128
14129 /* Find which variables are currently alive in a given block */
14130 rb = compute_variable_lifetimes(state, &bb);
14131
14132 /* Find the variables that are currently alive */
14133 block = block_of_triple(state, fcall);
14134 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14135 internal_error(state, fcall, "No reg block? block: %p", block);
14136 }
14137
14138#if DEBUG_EXPLICIT_CLOSURES
14139 print_live_variables(state, &bb, rb, state->dbgout);
14140 fflush(state->dbgout);
14141#endif
14142
14143 /* Count the number of triples in the function */
14144 first = RHS(me, 0);
14145 ins = first;
14146 count = 0;
14147 do {
14148 count++;
14149 ins = ins->next;
14150 } while(ins != first);
14151
14152 /* Allocate some memory to temorary hold the id info */
14153 info = xcmalloc(sizeof(*info) * (count +1), "info");
14154
14155 /* Mark the local function */
14156 first = RHS(me, 0);
14157 ins = first;
14158 idx = 1;
14159 do {
14160 info[idx].id = ins->id;
14161 ins->id = TRIPLE_FLAG_LOCAL | idx;
14162 idx++;
14163 ins = ins->next;
14164 } while(ins != first);
14165
Stefan Reinauer14e22772010-04-27 06:56:47 +000014166 /*
Eric Biederman90089602004-05-28 14:11:54 +000014167 * Build the list of variables to enclose.
14168 *
14169 * A target it to put the same variable in the
14170 * same slot for ever call of a given function.
14171 * After coloring this removes all of the variable
14172 * manipulation code.
14173 *
14174 * The list of variables to enclose is built ordered
14175 * program order because except in corner cases this
14176 * gives me the stability of assignment I need.
14177 *
14178 * To gurantee that stability I lookup the variables
14179 * to see where they have been used before and
14180 * I build my final list with the assigned indicies.
14181 */
14182 vars = 0;
14183 if (enclose_triple(old_result)) {
14184 ordered_triple_set(&vars, old_result);
14185 }
14186 for(set = rb[block->vertex].out; set; set = set->next) {
14187 if (!enclose_triple(set->member)) {
14188 continue;
14189 }
14190 if ((set->member == fcall) || (set->member == old_result)) {
14191 continue;
14192 }
14193 if (!local_triple(state, me, set->member)) {
14194 internal_error(state, set->member, "not local?");
14195 }
14196 ordered_triple_set(&vars, set->member);
14197 }
14198
14199 /* Lookup the current indicies of the live varialbe */
14200 used_indicies = 0;
14201 max_index = -1;
14202 for(set = vars; set ; set = set->next) {
14203 struct triple *ins;
14204 int index;
14205 ins = set->member;
14206 index = lookup_closure_index(state, me, ins);
14207 info[ID_BITS(ins->id)].index = index;
14208 if (index < 0) {
14209 continue;
14210 }
14211 if (index >= MAX_INDICIES) {
14212 internal_error(state, ins, "index unexpectedly large");
14213 }
14214 if (used_indicies & (1 << index)) {
14215 internal_error(state, ins, "index previously used?");
14216 }
14217 /* Remember which indicies have been used */
14218 used_indicies |= (1 << index);
14219 if (index > max_index) {
14220 max_index = index;
14221 }
14222 }
14223
14224 /* Walk through the live variables and make certain
14225 * everything is assigned an index.
14226 */
14227 for(set = vars; set; set = set->next) {
14228 struct triple *ins;
14229 int index;
14230 ins = set->member;
14231 index = info[ID_BITS(ins->id)].index;
14232 if (index >= 0) {
14233 continue;
14234 }
14235 /* Find the lowest unused index value */
14236 for(index = 0; index < MAX_INDICIES; index++) {
14237 if (!(used_indicies & (1 << index))) {
14238 break;
14239 }
14240 }
14241 if (index == MAX_INDICIES) {
14242 internal_error(state, ins, "no free indicies?");
14243 }
14244 info[ID_BITS(ins->id)].index = index;
14245 /* Remember which indicies have been used */
14246 used_indicies |= (1 << index);
14247 if (index > max_index) {
14248 max_index = index;
14249 }
14250 }
14251
14252 /* Build the return list of variables with positions matching
14253 * their indicies.
14254 */
14255 *enclose = 0;
14256 last_var = enclose;
14257 for(i = 0; i <= max_index; i++) {
14258 struct triple *var;
14259 var = 0;
14260 if (used_indicies & (1 << i)) {
14261 for(set = vars; set; set = set->next) {
14262 int index;
14263 index = info[ID_BITS(set->member->id)].index;
14264 if (index == i) {
14265 var = set->member;
14266 break;
14267 }
14268 }
14269 if (!var) {
14270 internal_error(state, me, "missing variable");
14271 }
14272 }
14273 insert_triple_set(last_var, var);
14274 last_var = &(*last_var)->next;
14275 }
14276
14277#if DEBUG_EXPLICIT_CLOSURES
14278 /* Print out the variables to be enclosed */
14279 loc(state->dbgout, state, fcall);
14280 fprintf(state->dbgout, "Alive: \n");
14281 for(set = *enclose; set; set = set->next) {
14282 display_triple(state->dbgout, set->member);
14283 }
14284 fflush(state->dbgout);
14285#endif
14286
14287 /* Clear the marks */
14288 ins = first;
14289 do {
14290 ins->id = info[ID_BITS(ins->id)].id;
14291 ins = ins->next;
14292 } while(ins != first);
14293
14294 /* Release the ordered list of live variables */
14295 free_closure_variables(state, &vars);
14296
14297 /* Release the storage of the old ids */
14298 xfree(info);
14299
14300 /* Release the variable lifetime information */
14301 free_variable_lifetimes(state, &bb, rb);
14302
14303 /* Release the basic blocks of this function */
14304 free_basic_blocks(state, &bb);
14305}
14306
14307static void expand_function_call(
14308 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014309{
14310 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014311 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014312 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014313 struct triple *first;
14314 struct type *ptype, *rtype;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014315 struct triple *ret_addr, *ret_loc;
Eric Biederman90089602004-05-28 14:11:54 +000014316 struct triple_reg_set *enclose, *set;
14317 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014318
Eric Biederman90089602004-05-28 14:11:54 +000014319#if DEBUG_EXPLICIT_CLOSURES
14320 FILE *fp = state->dbgout;
14321 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14322 display_func(state, fp, MISC(fcall, 0));
14323 display_func(state, fp, me);
14324 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14325#endif
14326
Eric Biederman5ade04a2003-10-22 04:03:46 +000014327 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014328 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014329 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014330 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014331 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014332 first = fcall->next;
14333
14334 /* Find what I need to enclose */
14335 compute_closure_variables(state, me, fcall, &enclose);
14336
14337 /* Compute the closure type */
14338 closure_type = new_type(TYPE_TUPLE, 0, 0);
14339 closure_type->elements = 0;
14340 closure_next = &closure_type->left;
14341 for(set = enclose; set ; set = set->next) {
14342 struct type *type;
14343 type = &void_type;
14344 if (set->member) {
14345 type = set->member->type;
14346 }
14347 if (!*closure_next) {
14348 *closure_next = type;
14349 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014350 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
Eric Biederman90089602004-05-28 14:11:54 +000014351 type);
14352 closure_next = &(*closure_next)->right;
14353 }
14354 closure_type->elements += 1;
14355 }
14356 if (closure_type->elements == 0) {
14357 closure_type->type = TYPE_VOID;
14358 }
14359
14360
14361#if DEBUG_EXPLICIT_CLOSURES
14362 fprintf(state->dbgout, "closure type: ");
14363 name_of(state->dbgout, closure_type);
14364 fprintf(state->dbgout, "\n");
14365#endif
14366
14367 /* Update the called functions closure variable */
14368 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014369
14370 /* Generate some needed triples */
14371 ret_loc = label(state);
14372 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14373
14374 /* Pass the parameters to the new function */
14375 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014376 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014377 for(i = 0; i < pvals; i++) {
14378 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014379 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014380 atype = ptype;
14381 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14382 atype = ptype->left;
14383 }
Eric Biederman90089602004-05-28 14:11:54 +000014384 param = farg(state, func, i);
14385 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14386 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014387 }
Eric Biederman90089602004-05-28 14:11:54 +000014388 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014389 flatten(state, first, write_expr(state, param, arg));
14390 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014391 }
Eric Biederman90089602004-05-28 14:11:54 +000014392 rtype = func->type->left;
14393
Eric Biederman5ade04a2003-10-22 04:03:46 +000014394 /* Thread the triples together */
14395 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014396
14397 /* Save the active variables in the result variable */
14398 for(i = 0, set = enclose; set ; set = set->next, i++) {
14399 if (!set->member) {
14400 continue;
14401 }
14402 flatten(state, ret_loc,
14403 write_expr(state,
14404 closure_expr(state, func, closure_idx, i),
14405 read_expr(state, set->member)));
14406 }
14407
14408 /* Initialize the return value */
14409 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014410 flatten(state, ret_loc,
14411 write_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014412 deref_index(state, fresult(state, func), 1),
14413 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14414 }
14415
Eric Biederman5ade04a2003-10-22 04:03:46 +000014416 ret_addr = flatten(state, ret_loc, ret_addr);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014417 flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14418 flatten(state, ret_loc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000014419 call(state, retvar, ret_addr, func_first, func_last));
14420
Eric Biederman7dea9552004-06-29 05:38:37 +000014421 /* Find the result */
14422 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14423 struct triple * result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014424 result = flatten(state, first,
14425 read_expr(state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014426 deref_index(state, fresult(state, func), 1)));
14427
14428 propogate_use(state, fcall, result);
14429 }
14430
14431 /* Release the original fcall instruction */
14432 release_triple(state, fcall);
14433
Eric Biederman90089602004-05-28 14:11:54 +000014434 /* Restore the active variables from the result variable */
14435 for(i = 0, set = enclose; set ; set = set->next, i++) {
14436 struct triple_set *use, *next;
14437 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014438 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014439 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014440 continue;
14441 }
Eric Biederman90089602004-05-28 14:11:54 +000014442 /* Generate an expression for the value */
14443 new = flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014444 read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014445 closure_expr(state, func, closure_idx, i)));
14446
14447
14448 /* If the original is an lvalue restore the preserved value */
14449 if (is_lvalue(state, set->member)) {
14450 flatten(state, first,
14451 write_expr(state, set->member, new));
14452 continue;
14453 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014454 /*
14455 * If the original is a value update the dominated uses.
14456 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014457
Eric Biederman7dea9552004-06-29 05:38:37 +000014458 /* Analyze the basic blocks so I can see who dominates whom */
14459 bb.func = me;
14460 bb.first = RHS(me, 0);
14461 if (!triple_is_ret(state, bb.first->prev)) {
14462 bb.func = 0;
14463 }
14464 analyze_basic_blocks(state, &bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000014465
Eric Biederman90089602004-05-28 14:11:54 +000014466
14467#if DEBUG_EXPLICIT_CLOSURES
14468 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14469 set->member, new);
14470#endif
14471 /* If fcall dominates the use update the expression */
14472 for(use = set->member->use; use; use = next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014473 /* Replace use modifies the use chain and
Eric Biederman90089602004-05-28 14:11:54 +000014474 * removes use, so I must take a copy of the
14475 * next entry early.
14476 */
14477 next = use->next;
14478 if (!tdominates(state, fcall, use->member)) {
14479 continue;
14480 }
14481 replace_use(state, set->member, new, use->member);
14482 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014483
14484 /* Release the basic blocks, the instructions will be
14485 * different next time, and flatten/insert_triple does
14486 * not update the block values so I can't cache the analysis.
14487 */
14488 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014489 }
14490
Eric Biederman90089602004-05-28 14:11:54 +000014491 /* Release the closure variable list */
14492 free_closure_variables(state, &enclose);
14493
14494 if (state->compiler->debug & DEBUG_INLINE) {
14495 FILE *fp = state->dbgout;
14496 fprintf(fp, "\n");
14497 loc(fp, state, 0);
14498 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14499 display_func(state, fp, func);
14500 display_func(state, fp, me);
14501 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14502 }
14503
14504 return;
14505}
14506
14507static int do_inline(struct compile_state *state, struct triple *func)
14508{
14509 int do_inline;
14510 int policy;
14511
14512 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14513 switch(policy) {
14514 case COMPILER_INLINE_ALWAYS:
14515 do_inline = 1;
14516 if (func->type->type & ATTRIB_NOINLINE) {
14517 error(state, func, "noinline with always_inline compiler option");
14518 }
14519 break;
14520 case COMPILER_INLINE_NEVER:
14521 do_inline = 0;
14522 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14523 error(state, func, "always_inline with noinline compiler option");
14524 }
14525 break;
14526 case COMPILER_INLINE_DEFAULTON:
14527 switch(func->type->type & STOR_MASK) {
14528 case STOR_STATIC | STOR_INLINE:
14529 case STOR_LOCAL | STOR_INLINE:
14530 case STOR_EXTERN | STOR_INLINE:
14531 do_inline = 1;
14532 break;
14533 default:
14534 do_inline = 1;
14535 break;
14536 }
14537 break;
14538 case COMPILER_INLINE_DEFAULTOFF:
14539 switch(func->type->type & STOR_MASK) {
14540 case STOR_STATIC | STOR_INLINE:
14541 case STOR_LOCAL | STOR_INLINE:
14542 case STOR_EXTERN | STOR_INLINE:
14543 do_inline = 1;
14544 break;
14545 default:
14546 do_inline = 0;
14547 break;
14548 }
14549 break;
14550 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014551 switch(func->type->type & STOR_MASK) {
14552 case STOR_STATIC | STOR_INLINE:
14553 case STOR_LOCAL | STOR_INLINE:
14554 case STOR_EXTERN | STOR_INLINE:
14555 do_inline = 1;
14556 break;
14557 default:
14558 do_inline = (func->u.cval == 1);
14559 break;
14560 }
Eric Biederman90089602004-05-28 14:11:54 +000014561 break;
14562 default:
14563 do_inline = 0;
14564 internal_error(state, 0, "Unimplemented inline policy");
14565 break;
14566 }
14567 /* Force inlining */
14568 if (func->type->type & ATTRIB_NOINLINE) {
14569 do_inline = 0;
14570 }
14571 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14572 do_inline = 1;
14573 }
14574 return do_inline;
14575}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014576
Eric Biederman90089602004-05-28 14:11:54 +000014577static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14578{
14579 struct triple *first, *ptr, *next;
14580 /* If the function is not used don't bother */
14581 if (me->u.cval <= 0) {
14582 return;
14583 }
14584 if (state->compiler->debug & DEBUG_CALLS2) {
14585 FILE *fp = state->dbgout;
14586 fprintf(fp, "in: %s\n",
14587 me->type->type_ident->name);
14588 }
14589
14590 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014591 ptr = next = first;
14592 do {
Eric Biederman90089602004-05-28 14:11:54 +000014593 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014594 ptr = next;
14595 prev = ptr->prev;
14596 next = ptr->next;
14597 if (ptr->op != OP_FCALL) {
14598 continue;
14599 }
14600 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014601 /* See if the function should be inlined */
14602 if (!do_inline(state, func)) {
14603 /* Put a label after the fcall */
14604 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14605 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014606 }
Eric Biederman90089602004-05-28 14:11:54 +000014607 if (state->compiler->debug & DEBUG_CALLS) {
14608 FILE *fp = state->dbgout;
14609 if (state->compiler->debug & DEBUG_CALLS2) {
14610 loc(fp, state, ptr);
14611 }
14612 fprintf(fp, "inlining %s\n",
14613 func->type->type_ident->name);
14614 fflush(fp);
14615 }
14616
14617 /* Update the function use counts */
14618 func->u.cval -= 1;
14619
14620 /* Replace the fcall with the called function */
14621 expand_inline_call(state, me, ptr);
14622
14623 next = prev->next;
14624 } while (next != first);
14625
14626 ptr = next = first;
14627 do {
14628 struct triple *prev, *func;
14629 ptr = next;
14630 prev = ptr->prev;
14631 next = ptr->next;
14632 if (ptr->op != OP_FCALL) {
14633 continue;
14634 }
14635 func = MISC(ptr, 0);
14636 if (state->compiler->debug & DEBUG_CALLS) {
14637 FILE *fp = state->dbgout;
14638 if (state->compiler->debug & DEBUG_CALLS2) {
14639 loc(fp, state, ptr);
14640 }
14641 fprintf(fp, "calling %s\n",
14642 func->type->type_ident->name);
14643 fflush(fp);
14644 }
14645 /* Replace the fcall with the instruction sequence
14646 * needed to make the call.
14647 */
14648 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014649 next = prev->next;
14650 } while(next != first);
14651}
Eric Biederman90089602004-05-28 14:11:54 +000014652
14653static void inline_functions(struct compile_state *state, struct triple *func)
14654{
14655 inline_function(state, func, 0);
14656 reverse_walk_functions(state, inline_function, 0);
14657}
14658
Eric Biederman5ade04a2003-10-22 04:03:46 +000014659static void insert_function(struct compile_state *state,
14660 struct triple *func, void *arg)
14661{
14662 struct triple *first, *end, *ffirst, *fend;
14663
14664 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014665 FILE *fp = state->errout;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014666 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014667 func->type->type_ident->name, func->u.cval);
14668 }
14669 if (func->u.cval == 0) {
14670 return;
14671 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014672
14673 /* Find the end points of the lists */
14674 first = arg;
14675 end = first->prev;
14676 ffirst = RHS(func, 0);
14677 fend = ffirst->prev;
14678
14679 /* splice the lists together */
14680 end->next = ffirst;
14681 ffirst->prev = end;
14682 fend->next = first;
14683 first->prev = fend;
14684}
14685
Eric Biederman90089602004-05-28 14:11:54 +000014686struct triple *input_asm(struct compile_state *state)
14687{
14688 struct asm_info *info;
14689 struct triple *def;
14690 int i, out;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014691
Eric Biederman90089602004-05-28 14:11:54 +000014692 info = xcmalloc(sizeof(*info), "asm_info");
14693 info->str = "";
14694
14695 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14696 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14697
14698 def = new_triple(state, OP_ASM, &void_type, out, 0);
14699 def->u.ainfo = info;
14700 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014701
Eric Biederman90089602004-05-28 14:11:54 +000014702 for(i = 0; i < out; i++) {
14703 struct triple *piece;
14704 piece = triple(state, OP_PIECE, &int_type, def, 0);
14705 piece->u.cval = i;
14706 LHS(def, i) = piece;
14707 }
14708
14709 return def;
14710}
14711
14712struct triple *output_asm(struct compile_state *state)
14713{
14714 struct asm_info *info;
14715 struct triple *def;
14716 int in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014717
Eric Biederman90089602004-05-28 14:11:54 +000014718 info = xcmalloc(sizeof(*info), "asm_info");
14719 info->str = "";
14720
14721 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14722 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14723
14724 def = new_triple(state, OP_ASM, &void_type, 0, in);
14725 def->u.ainfo = info;
14726 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014727
Eric Biederman90089602004-05-28 14:11:54 +000014728 return def;
14729}
14730
Eric Biederman5ade04a2003-10-22 04:03:46 +000014731static void join_functions(struct compile_state *state)
14732{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014733 struct triple *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014734 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014735 struct type *pnext, *param;
14736 struct type *result_type, *args_type;
14737 int idx;
14738
14739 /* Be clear the functions have not been joined yet */
14740 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014741
14742 /* Dummy file state to get debug handing right */
14743 memset(&file, 0, sizeof(file));
14744 file.basename = "";
14745 file.line = 0;
14746 file.report_line = 0;
14747 file.report_name = file.basename;
14748 file.prev = state->file;
14749 state->file = &file;
14750 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014751
Eric Biederman41203d92004-11-08 09:31:09 +000014752 if (!state->main_function) {
14753 error(state, 0, "No functions to compile\n");
14754 }
14755
Eric Biederman90089602004-05-28 14:11:54 +000014756 /* The type of arguments */
14757 args_type = state->main_function->type->right;
14758 /* The return type without any specifiers */
14759 result_type = clone_type(0, state->main_function->type->left);
14760
14761
14762 /* Verify the external arguments */
14763 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014764 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014765 "Too many external input arguments");
14766 }
14767 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014768 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014769 "Too many external output arguments");
14770 }
14771
Eric Biederman5ade04a2003-10-22 04:03:46 +000014772 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014773 end = label(state);
14774 start = label(state);
14775 start = flatten(state, state->first, start);
14776 end = flatten(state, state->first, end);
14777 in = input_asm(state);
14778 out = output_asm(state);
14779 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014780 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014781 in = flatten(state, state->first, in);
14782 call = flatten(state, state->first, call);
14783 out = flatten(state, state->first, out);
14784
14785
14786 /* Read the external input arguments */
14787 pnext = args_type;
14788 idx = 0;
14789 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14790 struct triple *expr;
14791 param = pnext;
14792 pnext = 0;
14793 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14794 pnext = param->right;
14795 param = param->left;
14796 }
14797 if (registers_of(state, param) != 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014798 error(state, state->main_function,
14799 "Arg: %d %s requires multiple registers",
Eric Biederman90089602004-05-28 14:11:54 +000014800 idx + 1, param->field_ident->name);
14801 }
14802 expr = read_expr(state, LHS(in, idx));
14803 RHS(call, idx) = expr;
14804 expr = flatten(state, call, expr);
14805 use_triple(expr, call);
14806
Stefan Reinauer14e22772010-04-27 06:56:47 +000014807 idx++;
Eric Biederman90089602004-05-28 14:11:54 +000014808 }
14809
14810
14811 /* Write the external output arguments */
14812 pnext = result_type;
14813 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14814 pnext = result_type->left;
14815 }
14816 for(idx = 0; idx < out->rhs; idx++) {
14817 struct triple *expr;
14818 param = pnext;
14819 pnext = 0;
14820 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14821 pnext = param->right;
14822 param = param->left;
14823 }
14824 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14825 param = 0;
14826 }
14827 if (param) {
14828 if (registers_of(state, param) != 1) {
14829 error(state, state->main_function,
14830 "Result: %d %s requires multiple registers",
14831 idx, param->field_ident->name);
14832 }
14833 expr = read_expr(state, call);
14834 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14835 expr = deref_field(state, expr, param->field_ident);
14836 }
14837 } else {
14838 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14839 }
14840 flatten(state, out, expr);
14841 RHS(out, idx) = expr;
14842 use_triple(expr, out);
14843 }
14844
14845 /* Allocate a dummy containing function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014846 func = triple(state, OP_LIST,
Eric Biederman90089602004-05-28 14:11:54 +000014847 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14848 func->type->type_ident = lookup(state, "", 0);
14849 RHS(func, 0) = state->first;
14850 func->u.cval = 1;
14851
Eric Biederman5ade04a2003-10-22 04:03:46 +000014852 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014853 mark_live_functions(state);
14854 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014855 walk_functions(state, insert_function, end);
14856
14857 if (start->next != end) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014858 flatten(state, start, branch(state, end, 0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014859 }
14860
Eric Biederman90089602004-05-28 14:11:54 +000014861 /* OK now the functions have been joined. */
14862 state->functions_joined = 1;
14863
Eric Biederman5ade04a2003-10-22 04:03:46 +000014864 /* Done now cleanup */
14865 state->file = file.prev;
14866 state->function = 0;
14867}
14868
Eric Biedermanb138ac82003-04-22 18:44:01 +000014869/*
14870 * Data structurs for optimation.
14871 */
14872
Eric Biederman5ade04a2003-10-22 04:03:46 +000014873
Eric Biederman83b991a2003-10-11 06:20:25 +000014874static int do_use_block(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014875 struct block *used, struct block_set **head, struct block *user,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014876 int front)
14877{
14878 struct block_set **ptr, *new;
14879 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014880 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014881 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014882 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014883 ptr = head;
14884 while(*ptr) {
14885 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014886 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014887 }
14888 ptr = &(*ptr)->next;
14889 }
14890 new = xcmalloc(sizeof(*new), "block_set");
14891 new->member = user;
14892 if (front) {
14893 new->next = *head;
14894 *head = new;
14895 }
14896 else {
14897 new->next = 0;
14898 *ptr = new;
14899 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014900 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014901}
Eric Biederman83b991a2003-10-11 06:20:25 +000014902static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014903 struct block *used, struct block_set **head, struct block *unuser)
14904{
14905 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014906 int count;
14907 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014908 ptr = head;
14909 while(*ptr) {
14910 use = *ptr;
14911 if (use->member == unuser) {
14912 *ptr = use->next;
14913 memset(use, -1, sizeof(*use));
14914 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014915 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014916 }
14917 else {
14918 ptr = &use->next;
14919 }
14920 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014921 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014922}
14923
14924static void use_block(struct block *used, struct block *user)
14925{
Eric Biederman83b991a2003-10-11 06:20:25 +000014926 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014927 /* Append new to the head of the list, print_block
14928 * depends on this.
14929 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014930 count = do_use_block(used, &used->use, user, 1);
Eric Biederman83b991a2003-10-11 06:20:25 +000014931 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014932}
14933static void unuse_block(struct block *used, struct block *unuser)
14934{
Eric Biederman83b991a2003-10-11 06:20:25 +000014935 int count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014936 count = do_unuse_block(used, &used->use, unuser);
Eric Biederman83b991a2003-10-11 06:20:25 +000014937 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014938}
14939
Eric Biederman5ade04a2003-10-22 04:03:46 +000014940static void add_block_edge(struct block *block, struct block *edge, int front)
14941{
14942 int count;
14943 count = do_use_block(block, &block->edges, edge, front);
14944 block->edge_count += count;
14945}
14946
14947static void remove_block_edge(struct block *block, struct block *edge)
14948{
14949 int count;
14950 count = do_unuse_block(block, &block->edges, edge);
14951 block->edge_count -= count;
14952}
14953
Eric Biedermanb138ac82003-04-22 18:44:01 +000014954static void idom_block(struct block *idom, struct block *user)
14955{
14956 do_use_block(idom, &idom->idominates, user, 0);
14957}
14958
14959static void unidom_block(struct block *idom, struct block *unuser)
14960{
14961 do_unuse_block(idom, &idom->idominates, unuser);
14962}
14963
14964static void domf_block(struct block *block, struct block *domf)
14965{
14966 do_use_block(block, &block->domfrontier, domf, 0);
14967}
14968
14969static void undomf_block(struct block *block, struct block *undomf)
14970{
14971 do_unuse_block(block, &block->domfrontier, undomf);
14972}
14973
14974static void ipdom_block(struct block *ipdom, struct block *user)
14975{
14976 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14977}
14978
14979static void unipdom_block(struct block *ipdom, struct block *unuser)
14980{
14981 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14982}
14983
14984static void ipdomf_block(struct block *block, struct block *ipdomf)
14985{
14986 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14987}
14988
14989static void unipdomf_block(struct block *block, struct block *unipdomf)
14990{
14991 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
14992}
14993
Eric Biederman83b991a2003-10-11 06:20:25 +000014994static int walk_triples(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014995 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000014996 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
14997 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000014998{
Eric Biederman83b991a2003-10-11 06:20:25 +000014999 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015000 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015001 ptr = state->first;
15002 do {
Eric Biederman90089602004-05-28 14:11:54 +000015003 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015004 if (ptr->next->prev != ptr) {
15005 internal_error(state, ptr->next, "bad prev");
15006 }
15007 ptr = ptr->next;
15008 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015009 return result;
15010}
15011
Eric Biedermanb138ac82003-04-22 18:44:01 +000015012#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015013static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015014{
Eric Biederman90089602004-05-28 14:11:54 +000015015 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015016 int op;
15017 op = ins->op;
15018 if (op == OP_LIST) {
15019#if !PRINT_LIST
15020 return 0;
15021#endif
15022 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015023 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015024 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015025 }
Eric Biederman90089602004-05-28 14:11:54 +000015026 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015027
Stefan Reinauer14e22772010-04-27 06:56:47 +000015028 if (triple_is_branch(state, ins) && ins->use &&
Eric Biederman90089602004-05-28 14:11:54 +000015029 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015030 internal_error(state, ins, "branch used?");
15031 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015032 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015033 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015034 }
15035 return 0;
15036}
15037
Eric Biedermanb138ac82003-04-22 18:44:01 +000015038static void print_triples(struct compile_state *state)
15039{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015040 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015041 FILE *fp = state->dbgout;
15042 fprintf(fp, "--------------- triples ---------------\n");
15043 walk_triples(state, do_print_triple, fp);
15044 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015045 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015046}
15047
15048struct cf_block {
15049 struct block *block;
15050};
15051static void find_cf_blocks(struct cf_block *cf, struct block *block)
15052{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015053 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015054 if (!block || (cf[block->vertex].block == block)) {
15055 return;
15056 }
15057 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015058 for(edge = block->edges; edge; edge = edge->next) {
15059 find_cf_blocks(cf, edge->member);
15060 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015061}
15062
Eric Biederman90089602004-05-28 14:11:54 +000015063static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015064 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015065{
15066 struct cf_block *cf;
15067 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015068 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015069 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15070 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015071
Eric Biederman90089602004-05-28 14:11:54 +000015072 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015073 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015074 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015075 block = cf[i].block;
15076 if (!block)
15077 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015078 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015079 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015080 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015081 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015082 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015083 }
15084
15085 xfree(cf);
15086}
15087
Eric Biedermanb138ac82003-04-22 18:44:01 +000015088static void free_basic_block(struct compile_state *state, struct block *block)
15089{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015090 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015091 struct block *child;
15092 if (!block) {
15093 return;
15094 }
15095 if (block->vertex == -1) {
15096 return;
15097 }
15098 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015099 for(edge = block->edges; edge; edge = edge->next) {
15100 if (edge->member) {
15101 unuse_block(edge->member, block);
15102 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015103 }
15104 if (block->idom) {
15105 unidom_block(block->idom, block);
15106 }
15107 block->idom = 0;
15108 if (block->ipdom) {
15109 unipdom_block(block->ipdom, block);
15110 }
15111 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015112 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015113 child = entry->member;
15114 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015115 if (child && (child->vertex != -1)) {
15116 for(edge = child->edges; edge; edge = edge->next) {
15117 edge->member = 0;
15118 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015119 }
15120 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015121 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015122 child = entry->member;
15123 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015124 if (child && (child->vertex != -1)) {
15125 child->idom = 0;
15126 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015127 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015128 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015129 child = entry->member;
15130 undomf_block(block, child);
15131 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015132 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015133 child = entry->member;
15134 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015135 if (child && (child->vertex != -1)) {
15136 child->ipdom = 0;
15137 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015138 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015139 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015140 child = entry->member;
15141 unipdomf_block(block, child);
15142 }
15143 if (block->users != 0) {
15144 internal_error(state, 0, "block still has users");
15145 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015146 while((edge = block->edges)) {
15147 child = edge->member;
15148 remove_block_edge(block, child);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015149
Eric Biederman5ade04a2003-10-22 04:03:46 +000015150 if (child && (child->vertex != -1)) {
15151 free_basic_block(state, child);
15152 }
15153 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015154 memset(block, -1, sizeof(*block));
Patrick Georgi26774f22009-11-21 19:54:02 +000015155#ifndef WIN32
Eric Biedermanb138ac82003-04-22 18:44:01 +000015156 xfree(block);
Patrick Georgi26774f22009-11-21 19:54:02 +000015157#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015158}
15159
Stefan Reinauer14e22772010-04-27 06:56:47 +000015160static void free_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015161 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015162{
15163 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015164 free_basic_block(state, bb->first_block);
15165 bb->last_vertex = 0;
15166 bb->first_block = bb->last_block = 0;
15167 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015168 ins = first;
15169 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015170 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015171 ins->u.block = 0;
15172 }
15173 ins = ins->next;
15174 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015175
Eric Biedermanb138ac82003-04-22 18:44:01 +000015176}
15177
Stefan Reinauer14e22772010-04-27 06:56:47 +000015178static struct block *basic_block(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015179 struct basic_blocks *bb, struct triple *first)
15180{
15181 struct block *block;
15182 struct triple *ptr;
15183 if (!triple_is_label(state, first)) {
15184 internal_error(state, first, "block does not start with a label");
15185 }
15186 /* See if this basic block has already been setup */
15187 if (first->u.block != 0) {
15188 return first->u.block;
15189 }
15190 /* Allocate another basic block structure */
15191 bb->last_vertex += 1;
15192 block = xcmalloc(sizeof(*block), "block");
15193 block->first = block->last = first;
15194 block->vertex = bb->last_vertex;
15195 ptr = first;
15196 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015197 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015198 break;
15199 }
15200 block->last = ptr;
15201 /* If ptr->u is not used remember where the baic block is */
15202 if (triple_stores_block(state, ptr)) {
15203 ptr->u.block = block;
15204 }
15205 if (triple_is_branch(state, ptr)) {
15206 break;
15207 }
15208 ptr = ptr->next;
15209 } while (ptr != bb->first);
15210 if ((ptr == bb->first) ||
15211 ((ptr->next == bb->first) && (
Stefan Reinauer14e22772010-04-27 06:56:47 +000015212 triple_is_end(state, ptr) ||
Eric Biederman90089602004-05-28 14:11:54 +000015213 triple_is_ret(state, ptr))))
15214 {
15215 /* The block has no outflowing edges */
15216 }
15217 else if (triple_is_label(state, ptr)) {
15218 struct block *next;
15219 next = basic_block(state, bb, ptr);
15220 add_block_edge(block, next, 0);
15221 use_block(next, block);
15222 }
15223 else if (triple_is_branch(state, ptr)) {
15224 struct triple **expr, *first;
15225 struct block *child;
15226 /* Find the branch targets.
15227 * I special case the first branch as that magically
15228 * avoids some difficult cases for the register allocator.
15229 */
15230 expr = triple_edge_targ(state, ptr, 0);
15231 if (!expr) {
15232 internal_error(state, ptr, "branch without targets");
15233 }
15234 first = *expr;
15235 expr = triple_edge_targ(state, ptr, expr);
15236 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15237 if (!*expr) continue;
15238 child = basic_block(state, bb, *expr);
15239 use_block(child, block);
15240 add_block_edge(block, child, 0);
15241 }
15242 if (first) {
15243 child = basic_block(state, bb, first);
15244 use_block(child, block);
15245 add_block_edge(block, child, 1);
15246
15247 /* Be certain the return block of a call is
15248 * in a basic block. When it is not find
15249 * start of the block, insert a label if
15250 * necessary and build the basic block.
15251 * Then add a fake edge from the start block
15252 * to the return block of the function.
15253 */
15254 if (state->functions_joined && triple_is_call(state, ptr)
15255 && !block_of_triple(state, MISC(ptr, 0))) {
15256 struct block *tail;
15257 struct triple *start;
15258 start = triple_to_block_start(state, MISC(ptr, 0));
15259 if (!triple_is_label(state, start)) {
15260 start = pre_triple(state,
15261 start, OP_LABEL, &void_type, 0, 0);
15262 }
15263 tail = basic_block(state, bb, start);
15264 add_block_edge(child, tail, 0);
15265 use_block(tail, child);
15266 }
15267 }
15268 }
15269 else {
15270 internal_error(state, 0, "Bad basic block split");
15271 }
15272#if 0
15273{
15274 struct block_set *edge;
15275 FILE *fp = state->errout;
15276 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015277 block, block->vertex,
Eric Biederman90089602004-05-28 14:11:54 +000015278 block->first, block->last);
15279 for(edge = block->edges; edge; edge = edge->next) {
15280 fprintf(fp, " %10p [%2d]",
15281 edge->member ? edge->member->first : 0,
15282 edge->member ? edge->member->vertex : -1);
15283 }
15284 fprintf(fp, "\n");
15285}
15286#endif
15287 return block;
15288}
15289
15290
15291static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15292 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15293 void *arg)
15294{
15295 struct triple *ptr, *first;
15296 struct block *last_block;
15297 last_block = 0;
15298 first = bb->first;
15299 ptr = first;
15300 do {
15301 if (triple_stores_block(state, ptr)) {
15302 struct block *block;
15303 block = ptr->u.block;
15304 if (block && (block != last_block)) {
15305 cb(state, block, arg);
15306 }
15307 last_block = block;
15308 }
15309 ptr = ptr->next;
15310 } while(ptr != first);
15311}
15312
15313static void print_block(
15314 struct compile_state *state, struct block *block, void *arg)
15315{
15316 struct block_set *user, *edge;
15317 struct triple *ptr;
15318 FILE *fp = arg;
15319
15320 fprintf(fp, "\nblock: %p (%d) ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015321 block,
Eric Biederman90089602004-05-28 14:11:54 +000015322 block->vertex);
15323
15324 for(edge = block->edges; edge; edge = edge->next) {
15325 fprintf(fp, " %p<-%p",
15326 edge->member,
15327 (edge->member && edge->member->use)?
15328 edge->member->use->member : 0);
15329 }
15330 fprintf(fp, "\n");
15331 if (block->first->op == OP_LABEL) {
15332 fprintf(fp, "%p:\n", block->first);
15333 }
15334 for(ptr = block->first; ; ) {
15335 display_triple(fp, ptr);
15336 if (ptr == block->last)
15337 break;
15338 ptr = ptr->next;
15339 if (ptr == block->first) {
15340 internal_error(state, 0, "missing block last?");
15341 }
15342 }
15343 fprintf(fp, "users %d: ", block->users);
15344 for(user = block->use; user; user = user->next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015345 fprintf(fp, "%p (%d) ",
Eric Biederman90089602004-05-28 14:11:54 +000015346 user->member,
15347 user->member->vertex);
15348 }
15349 fprintf(fp,"\n\n");
15350}
15351
15352
15353static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15354{
15355 fprintf(fp, "--------------- blocks ---------------\n");
15356 walk_blocks(state, &state->bb, print_block, fp);
15357}
15358static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15359{
15360 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15361 fprintf(fp, "After %s\n", func);
15362 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015363 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15364 print_dominators(state, fp, &state->bb);
15365 print_dominance_frontiers(state, fp, &state->bb);
15366 }
15367 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015368 }
15369}
15370
Stefan Reinauer14e22772010-04-27 06:56:47 +000015371static void prune_nonblock_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015372 struct basic_blocks *bb)
15373{
15374 struct block *block;
15375 struct triple *first, *ins, *next;
15376 /* Delete the triples not in a basic block */
15377 block = 0;
15378 first = bb->first;
15379 ins = first;
15380 do {
15381 next = ins->next;
15382 if (ins->op == OP_LABEL) {
15383 block = ins->u.block;
15384 }
15385 if (!block) {
15386 struct triple_set *use;
15387 for(use = ins->use; use; use = use->next) {
15388 struct block *block;
15389 block = block_of_triple(state, use->member);
15390 if (block != 0) {
15391 internal_error(state, ins, "pruning used ins?");
15392 }
15393 }
15394 release_triple(state, ins);
15395 }
15396 if (block && block->last == ins) {
15397 block = 0;
15398 }
15399 ins = next;
15400 } while(ins != first);
15401}
15402
Stefan Reinauer14e22772010-04-27 06:56:47 +000015403static void setup_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015404 struct basic_blocks *bb)
15405{
15406 if (!triple_stores_block(state, bb->first)) {
15407 internal_error(state, 0, "ins will not store block?");
15408 }
15409 /* Initialize the state */
15410 bb->first_block = bb->last_block = 0;
15411 bb->last_vertex = 0;
15412 free_basic_blocks(state, bb);
15413
15414 /* Find the basic blocks */
15415 bb->first_block = basic_block(state, bb, bb->first);
15416
15417 /* Be certain the last instruction of a function, or the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015418 * entire program is in a basic block. When it is not find
15419 * the start of the block, insert a label if necessary and build
Eric Biederman90089602004-05-28 14:11:54 +000015420 * basic block. Then add a fake edge from the start block
15421 * to the final block.
15422 */
15423 if (!block_of_triple(state, bb->first->prev)) {
15424 struct triple *start;
15425 struct block *tail;
15426 start = triple_to_block_start(state, bb->first->prev);
15427 if (!triple_is_label(state, start)) {
15428 start = pre_triple(state,
15429 start, OP_LABEL, &void_type, 0, 0);
15430 }
15431 tail = basic_block(state, bb, start);
15432 add_block_edge(bb->first_block, tail, 0);
15433 use_block(tail, bb->first_block);
15434 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015435
Eric Biederman90089602004-05-28 14:11:54 +000015436 /* Find the last basic block.
15437 */
15438 bb->last_block = block_of_triple(state, bb->first->prev);
15439
15440 /* Delete the triples not in a basic block */
15441 prune_nonblock_triples(state, bb);
15442
15443#if 0
15444 /* If we are debugging print what I have just done */
15445 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15446 print_blocks(state, state->dbgout);
15447 print_control_flow(state, bb);
15448 }
15449#endif
15450}
15451
15452
Eric Biedermanb138ac82003-04-22 18:44:01 +000015453struct sdom_block {
15454 struct block *block;
15455 struct sdom_block *sdominates;
15456 struct sdom_block *sdom_next;
15457 struct sdom_block *sdom;
15458 struct sdom_block *label;
15459 struct sdom_block *parent;
15460 struct sdom_block *ancestor;
15461 int vertex;
15462};
15463
15464
15465static void unsdom_block(struct sdom_block *block)
15466{
15467 struct sdom_block **ptr;
15468 if (!block->sdom_next) {
15469 return;
15470 }
15471 ptr = &block->sdom->sdominates;
15472 while(*ptr) {
15473 if ((*ptr) == block) {
15474 *ptr = block->sdom_next;
15475 return;
15476 }
15477 ptr = &(*ptr)->sdom_next;
15478 }
15479}
15480
15481static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15482{
15483 unsdom_block(block);
15484 block->sdom = sdom;
15485 block->sdom_next = sdom->sdominates;
15486 sdom->sdominates = block;
15487}
15488
15489
15490
15491static int initialize_sdblock(struct sdom_block *sd,
15492 struct block *parent, struct block *block, int vertex)
15493{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015494 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015495 if (!block || (sd[block->vertex].block == block)) {
15496 return vertex;
15497 }
15498 vertex += 1;
15499 /* Renumber the blocks in a convinient fashion */
15500 block->vertex = vertex;
15501 sd[vertex].block = block;
15502 sd[vertex].sdom = &sd[vertex];
15503 sd[vertex].label = &sd[vertex];
15504 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15505 sd[vertex].ancestor = 0;
15506 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015507 for(edge = block->edges; edge; edge = edge->next) {
15508 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15509 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015510 return vertex;
15511}
15512
Eric Biederman83b991a2003-10-11 06:20:25 +000015513static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015514 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015515 struct block *parent, struct block *block, int vertex)
15516{
15517 struct block_set *user;
15518 if (!block || (sd[block->vertex].block == block)) {
15519 return vertex;
15520 }
15521 vertex += 1;
15522 /* Renumber the blocks in a convinient fashion */
15523 block->vertex = vertex;
15524 sd[vertex].block = block;
15525 sd[vertex].sdom = &sd[vertex];
15526 sd[vertex].label = &sd[vertex];
15527 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15528 sd[vertex].ancestor = 0;
15529 sd[vertex].vertex = vertex;
15530 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015531 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015532 }
15533 return vertex;
15534}
15535
Stefan Reinauer14e22772010-04-27 06:56:47 +000015536static int setup_spdblocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015537 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015538{
15539 struct block *block;
15540 int vertex;
15541 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015542 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015543
Eric Biederman5ade04a2003-10-22 04:03:46 +000015544 /* Walk through the graph and find unconnected blocks. Add a
15545 * fake edge from the unconnected blocks to the end of the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015546 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015547 */
Eric Biederman90089602004-05-28 14:11:54 +000015548 block = bb->first_block->last->next->u.block;
15549 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015550 if (sd[block->vertex].block == block) {
15551 continue;
15552 }
Eric Biederman530b5192003-07-01 10:05:30 +000015553#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015554 {
15555 FILE *fp = state->errout;
15556 fprintf(fp, "Adding %d\n", vertex +1);
15557 }
Eric Biederman530b5192003-07-01 10:05:30 +000015558#endif
Eric Biederman90089602004-05-28 14:11:54 +000015559 add_block_edge(block, bb->last_block, 0);
15560 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015561
Eric Biederman90089602004-05-28 14:11:54 +000015562 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015563 }
15564 return vertex;
15565}
15566
15567static void compress_ancestors(struct sdom_block *v)
15568{
15569 /* This procedure assumes ancestor(v) != 0 */
15570 /* if (ancestor(ancestor(v)) != 0) {
15571 * compress(ancestor(ancestor(v)));
15572 * if (semi(label(ancestor(v))) < semi(label(v))) {
15573 * label(v) = label(ancestor(v));
15574 * }
15575 * ancestor(v) = ancestor(ancestor(v));
15576 * }
15577 */
15578 if (!v->ancestor) {
15579 return;
15580 }
15581 if (v->ancestor->ancestor) {
15582 compress_ancestors(v->ancestor->ancestor);
15583 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15584 v->label = v->ancestor->label;
15585 }
15586 v->ancestor = v->ancestor->ancestor;
15587 }
15588}
15589
Stefan Reinauer14e22772010-04-27 06:56:47 +000015590static void compute_sdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015591 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015592{
15593 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015594 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015595 * for each v <= pred(w) {
15596 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015597 * if (semi[u] < semi[w] {
15598 * semi[w] = semi[u];
15599 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015600 * }
15601 * add w to bucket(vertex(semi[w]));
15602 * LINK(parent(w), w);
15603 *
15604 * // step 3
15605 * for each v <= bucket(parent(w)) {
15606 * delete v from bucket(parent(w));
15607 * u = EVAL(v);
15608 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15609 * }
15610 */
Eric Biederman90089602004-05-28 14:11:54 +000015611 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015612 struct sdom_block *v, *parent, *next;
15613 struct block_set *user;
15614 struct block *block;
15615 block = sd[i].block;
15616 parent = sd[i].parent;
15617 /* Step 2 */
15618 for(user = block->use; user; user = user->next) {
15619 struct sdom_block *v, *u;
15620 v = &sd[user->member->vertex];
15621 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15622 if (u->sdom->vertex < sd[i].sdom->vertex) {
15623 sd[i].sdom = u->sdom;
15624 }
15625 }
15626 sdom_block(sd[i].sdom, &sd[i]);
15627 sd[i].ancestor = parent;
15628 /* Step 3 */
15629 for(v = parent->sdominates; v; v = next) {
15630 struct sdom_block *u;
15631 next = v->sdom_next;
15632 unsdom_block(v);
15633 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015634 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015635 u->block : parent->block;
15636 }
15637 }
15638}
15639
Stefan Reinauer14e22772010-04-27 06:56:47 +000015640static void compute_spdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015641 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015642{
15643 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015644 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015645 * for each v <= pred(w) {
15646 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015647 * if (semi[u] < semi[w] {
15648 * semi[w] = semi[u];
15649 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015650 * }
15651 * add w to bucket(vertex(semi[w]));
15652 * LINK(parent(w), w);
15653 *
15654 * // step 3
15655 * for each v <= bucket(parent(w)) {
15656 * delete v from bucket(parent(w));
15657 * u = EVAL(v);
15658 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15659 * }
15660 */
Eric Biederman90089602004-05-28 14:11:54 +000015661 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015662 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015663 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015664 struct block *block;
15665 block = sd[i].block;
15666 parent = sd[i].parent;
15667 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015668 for(edge = block->edges; edge; edge = edge->next) {
15669 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015670 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15671 if (u->sdom->vertex < sd[i].sdom->vertex) {
15672 sd[i].sdom = u->sdom;
15673 }
15674 }
15675 sdom_block(sd[i].sdom, &sd[i]);
15676 sd[i].ancestor = parent;
15677 /* Step 3 */
15678 for(v = parent->sdominates; v; v = next) {
15679 struct sdom_block *u;
15680 next = v->sdom_next;
15681 unsdom_block(v);
15682 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015683 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015684 u->block : parent->block;
15685 }
15686 }
15687}
15688
Stefan Reinauer14e22772010-04-27 06:56:47 +000015689static void compute_idom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015690 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015691{
15692 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015693 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015694 struct block *block;
15695 block = sd[i].block;
15696 if (block->idom->vertex != sd[i].sdom->vertex) {
15697 block->idom = block->idom->idom;
15698 }
15699 idom_block(block->idom, block);
15700 }
15701 sd[1].block->idom = 0;
15702}
15703
Stefan Reinauer14e22772010-04-27 06:56:47 +000015704static void compute_ipdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015705 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015706{
15707 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015708 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015709 struct block *block;
15710 block = sd[i].block;
15711 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15712 block->ipdom = block->ipdom->ipdom;
15713 }
15714 ipdom_block(block->ipdom, block);
15715 }
15716 sd[1].block->ipdom = 0;
15717}
15718
15719 /* Theorem 1:
15720 * Every vertex of a flowgraph G = (V, E, r) except r has
Stefan Reinauer14e22772010-04-27 06:56:47 +000015721 * a unique immediate dominator.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015722 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
Stefan Reinauer14e22772010-04-27 06:56:47 +000015723 * rooted at r, called the dominator tree of G, such that
Eric Biedermanb138ac82003-04-22 18:44:01 +000015724 * v dominates w if and only if v is a proper ancestor of w in
15725 * the dominator tree.
15726 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000015727 /* Lemma 1:
Eric Biedermanb138ac82003-04-22 18:44:01 +000015728 * If v and w are vertices of G such that v <= w,
15729 * than any path from v to w must contain a common ancestor
15730 * of v and w in T.
15731 */
15732 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15733 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15734 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15735 /* Theorem 2:
15736 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15737 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15738 */
15739 /* Theorem 3:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015740 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015741 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15742 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15743 */
15744 /* Lemma 5: Let vertices v,w satisfy v -> w.
15745 * Then v -> idom(w) or idom(w) -> idom(v)
15746 */
15747
Eric Biederman90089602004-05-28 14:11:54 +000015748static void find_immediate_dominators(struct compile_state *state,
15749 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015750{
15751 struct sdom_block *sd;
15752 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15753 * vi > w for (1 <= i <= k - 1}
15754 */
15755 /* Theorem 4:
15756 * For any vertex w != r.
15757 * sdom(w) = min(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015758 * {v|(v,w) <= E and v < w } U
Eric Biedermanb138ac82003-04-22 18:44:01 +000015759 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15760 */
15761 /* Corollary 1:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015762 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015763 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15764 * Then:
15765 * { sdom(w) if sdom(w) = sdom(u),
15766 * idom(w) = {
15767 * { idom(u) otherwise
15768 */
15769 /* The algorithm consists of the following 4 steps.
Stefan Reinauer14e22772010-04-27 06:56:47 +000015770 * Step 1. Carry out a depth-first search of the problem graph.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015771 * Number the vertices from 1 to N as they are reached during
15772 * the search. Initialize the variables used in succeeding steps.
15773 * Step 2. Compute the semidominators of all vertices by applying
15774 * theorem 4. Carry out the computation vertex by vertex in
15775 * decreasing order by number.
15776 * Step 3. Implicitly define the immediate dominator of each vertex
15777 * by applying Corollary 1.
15778 * Step 4. Explicitly define the immediate dominator of each vertex,
15779 * carrying out the computation vertex by vertex in increasing order
15780 * by number.
15781 */
15782 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015783 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15784 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015785#if 0
15786 sd[1].size = 0;
15787 sd[1].label = 0;
15788 sd[1].sdom = 0;
15789#endif
15790 /* Step 2 compute the semidominators */
15791 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015792 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015793 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015794 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015795 xfree(sd);
15796}
15797
Eric Biederman90089602004-05-28 14:11:54 +000015798static void find_post_dominators(struct compile_state *state,
15799 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015800{
15801 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015802 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015803 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015804 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015805
Eric Biederman90089602004-05-28 14:11:54 +000015806 vertex = setup_spdblocks(state, bb, sd);
15807 if (vertex != bb->last_vertex) {
15808 internal_error(state, 0, "missing %d blocks",
15809 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015810 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015811
15812 /* Step 2 compute the semidominators */
15813 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015814 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015815 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015816 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015817 xfree(sd);
15818}
15819
15820
15821
15822static void find_block_domf(struct compile_state *state, struct block *block)
15823{
15824 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015825 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015826 if (block->domfrontier != 0) {
15827 internal_error(state, block->first, "domfrontier present?");
15828 }
15829 for(user = block->idominates; user; user = user->next) {
15830 child = user->member;
15831 if (child->idom != block) {
15832 internal_error(state, block->first, "bad idom");
15833 }
15834 find_block_domf(state, child);
15835 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015836 for(edge = block->edges; edge; edge = edge->next) {
15837 if (edge->member->idom != block) {
15838 domf_block(block, edge->member);
15839 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015840 }
15841 for(user = block->idominates; user; user = user->next) {
15842 struct block_set *frontier;
15843 child = user->member;
15844 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15845 if (frontier->member->idom != block) {
15846 domf_block(block, frontier->member);
15847 }
15848 }
15849 }
15850}
15851
15852static void find_block_ipdomf(struct compile_state *state, struct block *block)
15853{
15854 struct block *child;
15855 struct block_set *user;
15856 if (block->ipdomfrontier != 0) {
15857 internal_error(state, block->first, "ipdomfrontier present?");
15858 }
15859 for(user = block->ipdominates; user; user = user->next) {
15860 child = user->member;
15861 if (child->ipdom != block) {
15862 internal_error(state, block->first, "bad ipdom");
15863 }
15864 find_block_ipdomf(state, child);
15865 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015866 for(user = block->use; user; user = user->next) {
15867 if (user->member->ipdom != block) {
15868 ipdomf_block(block, user->member);
15869 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015870 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015871 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015872 struct block_set *frontier;
15873 child = user->member;
15874 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15875 if (frontier->member->ipdom != block) {
15876 ipdomf_block(block, frontier->member);
15877 }
15878 }
15879 }
15880}
15881
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015882static void print_dominated(
15883 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015884{
15885 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015886 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015887
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015888 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015889 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015890 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015891 if (user->member->idom != block) {
15892 internal_error(state, user->member->first, "bad idom");
15893 }
15894 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015896}
15897
Eric Biederman5ade04a2003-10-22 04:03:46 +000015898static void print_dominated2(
15899 struct compile_state *state, FILE *fp, int depth, struct block *block)
15900{
15901 struct block_set *user;
15902 struct triple *ins;
15903 struct occurance *ptr, *ptr2;
15904 const char *filename1, *filename2;
15905 int equal_filenames;
15906 int i;
15907 for(i = 0; i < depth; i++) {
15908 fprintf(fp, " ");
15909 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015910 fprintf(fp, "%3d: %p (%p - %p) @",
Eric Biederman5ade04a2003-10-22 04:03:46 +000015911 block->vertex, block, block->first, block->last);
15912 ins = block->first;
15913 while(ins != block->last && (ins->occurance->line == 0)) {
15914 ins = ins->next;
15915 }
15916 ptr = ins->occurance;
15917 ptr2 = block->last->occurance;
15918 filename1 = ptr->filename? ptr->filename : "";
15919 filename2 = ptr2->filename? ptr2->filename : "";
15920 equal_filenames = (strcmp(filename1, filename2) == 0);
15921 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15922 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15923 } else if (equal_filenames) {
15924 fprintf(fp, " %s:(%d - %d)",
15925 ptr->filename, ptr->line, ptr2->line);
15926 } else {
15927 fprintf(fp, " (%s:%d - %s:%d)",
15928 ptr->filename, ptr->line,
15929 ptr2->filename, ptr2->line);
15930 }
15931 fprintf(fp, "\n");
15932 for(user = block->idominates; user; user = user->next) {
15933 print_dominated2(state, fp, depth + 1, user->member);
15934 }
15935}
15936
Eric Biederman90089602004-05-28 14:11:54 +000015937static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015938{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015939 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015940 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015941 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015942 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015943}
15944
15945
15946static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015947 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015948{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015949 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015950
15951 if (!block || (block->vertex != vertex + 1)) {
15952 return vertex;
15953 }
15954 vertex += 1;
15955
Eric Biederman7dea9552004-06-29 05:38:37 +000015956 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015957 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015958 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015959 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015960 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000015961
Eric Biederman5ade04a2003-10-22 04:03:46 +000015962 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015963 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015964 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015965 return vertex;
15966}
Eric Biederman90089602004-05-28 14:11:54 +000015967static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015968 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015969{
Eric Biederman7dea9552004-06-29 05:38:37 +000015970 fprintf(fp, "\ndominance frontiers\n");
15971 print_frontiers(state, fp, bb->first_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015972
Eric Biedermanb138ac82003-04-22 18:44:01 +000015973}
15974
Eric Biederman90089602004-05-28 14:11:54 +000015975static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015976{
15977 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015978 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015979 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015980 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015981 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015982 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015983 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015984 print_dominance_frontiers(state, state->dbgout, bb);
15985 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015986 }
15987}
15988
15989
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015990static void print_ipdominated(
15991 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015992{
15993 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015994 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015995
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015996 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015997 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015998 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015999 if (user->member->ipdom != block) {
16000 internal_error(state, user->member->first, "bad ipdom");
16001 }
16002 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016003 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016004}
16005
Eric Biederman90089602004-05-28 14:11:54 +000016006static void print_ipdominators(struct compile_state *state, FILE *fp,
16007 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016008{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016009 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016010 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016011}
16012
16013static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016014 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016015{
16016 struct block_set *user;
16017
16018 if (!block || (block->vertex != vertex + 1)) {
16019 return vertex;
16020 }
16021 vertex += 1;
16022
Eric Biederman7dea9552004-06-29 05:38:37 +000016023 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016024 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016025 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016026 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016027 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016028 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016029 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016030 }
16031 return vertex;
16032}
Eric Biederman90089602004-05-28 14:11:54 +000016033static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016034 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016035{
Eric Biederman7dea9552004-06-29 05:38:37 +000016036 fprintf(fp, "\nipdominance frontiers\n");
16037 print_pfrontiers(state, fp, bb->last_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016038
Eric Biedermanb138ac82003-04-22 18:44:01 +000016039}
16040
Eric Biederman90089602004-05-28 14:11:54 +000016041static void analyze_ipdominators(struct compile_state *state,
16042 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016043{
16044 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016045 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016046 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016047 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016048 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016049 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016050 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016051 print_ipdominance_frontiers(state, state->dbgout, bb);
16052 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016053 }
16054}
16055
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016056static int bdominates(struct compile_state *state,
16057 struct block *dom, struct block *sub)
16058{
16059 while(sub && (sub != dom)) {
16060 sub = sub->idom;
16061 }
16062 return sub == dom;
16063}
16064
16065static int tdominates(struct compile_state *state,
16066 struct triple *dom, struct triple *sub)
16067{
16068 struct block *bdom, *bsub;
16069 int result;
16070 bdom = block_of_triple(state, dom);
16071 bsub = block_of_triple(state, sub);
16072 if (bdom != bsub) {
16073 result = bdominates(state, bdom, bsub);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016074 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016075 else {
16076 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016077 if (!bdom || !bsub) {
16078 internal_error(state, dom, "huh?");
16079 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016080 ins = sub;
16081 while((ins != bsub->first) && (ins != dom)) {
16082 ins = ins->prev;
16083 }
16084 result = (ins == dom);
16085 }
16086 return result;
16087}
16088
Eric Biederman90089602004-05-28 14:11:54 +000016089static void analyze_basic_blocks(
16090 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016091{
Eric Biederman90089602004-05-28 14:11:54 +000016092 setup_basic_blocks(state, bb);
16093 analyze_idominators(state, bb);
16094 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016095}
16096
Eric Biedermanb138ac82003-04-22 18:44:01 +000016097static void insert_phi_operations(struct compile_state *state)
16098{
16099 size_t size;
16100 struct triple *first;
16101 int *has_already, *work;
16102 struct block *work_list, **work_list_tail;
16103 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016104 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016105
Eric Biederman90089602004-05-28 14:11:54 +000016106 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016107 has_already = xcmalloc(size, "has_already");
16108 work = xcmalloc(size, "work");
16109 iter = 0;
16110
Eric Biederman83b991a2003-10-11 06:20:25 +000016111 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016112 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016113 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016114 struct triple_set *user, *unext;
16115 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016116
16117 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016118 continue;
16119 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016120
Eric Biedermanb138ac82003-04-22 18:44:01 +000016121 iter += 1;
16122 work_list = 0;
16123 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016124 for(user = var->use; user; user = unext) {
16125 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016126 if (MISC(var, 0) == user->member) {
16127 continue;
16128 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016129 if (user->member->op == OP_READ) {
16130 continue;
16131 }
16132 if (user->member->op != OP_WRITE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000016133 internal_error(state, user->member,
Eric Biedermanb138ac82003-04-22 18:44:01 +000016134 "bad variable access");
16135 }
16136 block = user->member->u.block;
16137 if (!block) {
16138 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016139 release_triple(state, user->member);
16140 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016141 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016142 if (work[block->vertex] >= iter) {
16143 continue;
16144 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016145 work[block->vertex] = iter;
16146 *work_list_tail = block;
16147 block->work_next = 0;
16148 work_list_tail = &block->work_next;
16149 }
16150 for(block = work_list; block; block = block->work_next) {
16151 struct block_set *df;
16152 for(df = block->domfrontier; df; df = df->next) {
16153 struct triple *phi;
16154 struct block *front;
16155 int in_edges;
16156 front = df->member;
16157
16158 if (has_already[front->vertex] >= iter) {
16159 continue;
16160 }
16161 /* Count how many edges flow into this block */
16162 in_edges = front->users;
16163 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016164 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016165 phi = alloc_triple(
Stefan Reinauer14e22772010-04-27 06:56:47 +000016166 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016167 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016168 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016169 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016170 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016171#if 1
16172 if (phi->rhs != in_edges) {
16173 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16174 phi->rhs, in_edges);
16175 }
16176#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016177 /* Insert the phi functions immediately after the label */
16178 insert_triple(state, front->first->next, phi);
16179 if (front->first == front->last) {
16180 front->last = front->first->next;
16181 }
16182 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016183 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016184
Eric Biedermanb138ac82003-04-22 18:44:01 +000016185 /* If necessary plan to visit the basic block */
16186 if (work[front->vertex] >= iter) {
16187 continue;
16188 }
16189 work[front->vertex] = iter;
16190 *work_list_tail = front;
16191 front->work_next = 0;
16192 work_list_tail = &front->work_next;
16193 }
16194 }
16195 }
16196 xfree(has_already);
16197 xfree(work);
16198}
16199
Eric Biederman66fe2222003-07-04 15:14:04 +000016200
Eric Biederman83b991a2003-10-11 06:20:25 +000016201struct stack {
16202 struct triple_set *top;
16203 unsigned orig_id;
16204};
16205
Eric Biederman90089602004-05-28 14:11:54 +000016206static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016207{
16208 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016209 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016210 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016211 ins = first;
16212 do {
Eric Biederman90089602004-05-28 14:11:54 +000016213 if (triple_is_auto_var(state, ins)) {
16214 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016215 }
16216 ins = ins->next;
16217 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016218 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016219}
16220
Eric Biederman90089602004-05-28 14:11:54 +000016221static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016222{
16223 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016224 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016225 first = state->first;
16226 ins = first;
16227 do {
Eric Biederman90089602004-05-28 14:11:54 +000016228 if (triple_is_auto_var(state, ins)) {
16229 auto_vars += 1;
16230 stacks[auto_vars].orig_id = ins->id;
16231 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016232 }
16233 ins = ins->next;
16234 } while(ins != first);
16235}
16236
Eric Biederman90089602004-05-28 14:11:54 +000016237static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016238{
16239 struct triple *first, *ins;
16240 first = state->first;
16241 ins = first;
16242 do {
Eric Biederman90089602004-05-28 14:11:54 +000016243 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016244 ins->id = stacks[ins->id].orig_id;
16245 }
16246 ins = ins->next;
16247 } while(ins != first);
16248}
16249
16250static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016251{
16252 struct triple_set *head;
16253 struct triple *top_val;
16254 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016255 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016256 if (head) {
16257 top_val = head->member;
16258 }
16259 return top_val;
16260}
16261
Eric Biederman83b991a2003-10-11 06:20:25 +000016262static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016263{
16264 struct triple_set *new;
16265 /* Append new to the head of the list,
16266 * it's the only sensible behavoir for a stack.
16267 */
16268 new = xcmalloc(sizeof(*new), "triple_set");
16269 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016270 new->next = stacks[var->id].top;
16271 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016272}
16273
Eric Biederman83b991a2003-10-11 06:20:25 +000016274static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016275{
16276 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016277 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016278 while(*ptr) {
16279 set = *ptr;
16280 if (set->member == oldval) {
16281 *ptr = set->next;
16282 xfree(set);
16283 /* Only free one occurance from the stack */
16284 return;
16285 }
16286 else {
16287 ptr = &set->next;
16288 }
16289 }
16290}
16291
Eric Biedermanb138ac82003-04-22 18:44:01 +000016292/*
16293 * C(V)
16294 * S(V)
16295 */
16296static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016297 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016298{
16299 struct block_set *set;
16300 struct triple *ptr;
16301 int edge;
16302 if (!parent || !block)
16303 return;
16304 /* Find the edge I am coming in on */
16305 edge = 0;
16306 for(set = block->use; set; set = set->next, edge++) {
16307 if (set->member == parent) {
16308 break;
16309 }
16310 }
16311 if (!set) {
16312 internal_error(state, 0, "phi input is not on a control predecessor");
16313 }
16314 for(ptr = block->first; ; ptr = ptr->next) {
16315 if (ptr->op == OP_PHI) {
16316 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016317 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016318 if (!var) {
16319 internal_error(state, ptr, "no var???");
16320 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016321 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016322 val = peek_triple(stacks, var);
16323 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016324 internal_error(state, val, "bad value in phi");
16325 }
Eric Biederman90089602004-05-28 14:11:54 +000016326 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016327 internal_error(state, ptr, "edges > phi rhs");
16328 }
16329 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016330 if ((*slot != 0) && (*slot != val)) {
16331 internal_error(state, ptr, "phi already bound on this edge");
16332 }
16333 *slot = val;
16334 use_triple(val, ptr);
16335 }
16336 if (ptr == block->last) {
16337 break;
16338 }
16339 }
16340}
16341
16342
16343static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016344 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016345{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016346 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016347 struct triple *ptr, *next, *last;
16348 int done;
16349 if (!block)
16350 return;
16351 last = block->first;
16352 done = 0;
16353 for(ptr = block->first; !done; ptr = next) {
16354 next = ptr->next;
16355 if (ptr == block->last) {
16356 done = 1;
16357 }
16358 /* RHS(A) */
16359 if (ptr->op == OP_READ) {
16360 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016361 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016362 if (!triple_is_auto_var(state, var)) {
16363 internal_error(state, ptr, "read of non auto var!");
16364 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016365 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016366 /* Find the current value of the variable */
16367 val = peek_triple(stacks, var);
16368 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016369 /* Let the optimizer at variables that are not initially
16370 * set. But give it a bogus value so things seem to
16371 * work by accident. This is useful for bitfields because
16372 * setting them always involves a read-modify-write.
16373 */
16374 if (TYPE_ARITHMETIC(ptr->type->type)) {
16375 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16376 val->u.cval = 0xdeadbeaf;
16377 } else {
16378 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16379 }
16380 }
16381 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016382 error(state, ptr, "variable used without being set");
16383 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016384 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16385 internal_error(state, val, "bad value in read");
16386 }
16387 propogate_use(state, ptr, val);
16388 release_triple(state, ptr);
16389 continue;
16390 }
16391 /* LHS(A) */
16392 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016393 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016394 var = MISC(ptr, 0);
16395 if (!triple_is_auto_var(state, var)) {
16396 internal_error(state, ptr, "write to non auto var!");
16397 }
16398 tval = val = RHS(ptr, 0);
16399 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16400 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016401 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016402 }
Eric Biederman90089602004-05-28 14:11:54 +000016403 /* Insert a cast if the types differ */
16404 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016405 if (val->op == OP_INTCONST) {
16406 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16407 tval->u.cval = val->u.cval;
16408 }
16409 else {
Eric Biederman90089602004-05-28 14:11:54 +000016410 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016411 use_triple(val, tval);
16412 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016413 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016414 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016415 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016416 use_triple(tval, ptr);
16417 }
16418 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016419 unuse_triple(var, ptr);
16420 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016421 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016422 }
16423 if (ptr->op == OP_PHI) {
16424 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016425 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016426 if (!triple_is_auto_var(state, var)) {
16427 internal_error(state, ptr, "phi references non auto var!");
16428 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016429 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016430 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016431 }
16432 last = ptr;
16433 }
16434 block->last = last;
16435
16436 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016437 for(edge = block->edges; edge; edge = edge->next) {
16438 fixup_block_phi_variables(state, stacks, block, edge->member);
16439 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016440 /* rename variables in the dominated nodes */
16441 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016442 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016443 }
16444 /* pop the renamed variable stack */
16445 last = block->first;
16446 done = 0;
16447 for(ptr = block->first; !done ; ptr = next) {
16448 next = ptr->next;
16449 if (ptr == block->last) {
16450 done = 1;
16451 }
16452 if (ptr->op == OP_WRITE) {
16453 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016454 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016455 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016456 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016457 release_triple(state, ptr);
16458 continue;
16459 }
16460 if (ptr->op == OP_PHI) {
16461 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016462 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016463 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016464 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016465 }
16466 last = ptr;
16467 }
16468 block->last = last;
16469}
16470
Eric Biederman83b991a2003-10-11 06:20:25 +000016471static void rename_variables(struct compile_state *state)
16472{
16473 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016474 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016475
16476 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016477 auto_vars = count_auto_vars(state);
16478 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016479
Eric Biederman90089602004-05-28 14:11:54 +000016480 /* Give each auto_var a stack */
16481 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016482
16483 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016484 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016485
Eric Biederman90089602004-05-28 14:11:54 +000016486 /* Remove the stacks from the auto_vars */
16487 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016488 xfree(stacks);
16489}
16490
Eric Biedermanb138ac82003-04-22 18:44:01 +000016491static void prune_block_variables(struct compile_state *state,
16492 struct block *block)
16493{
16494 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016495 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016496 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016497
Eric Biedermanb138ac82003-04-22 18:44:01 +000016498 done = 0;
16499 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016500 /* Be extremely careful I am deleting the list
16501 * as I walk trhough it.
16502 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016503 next = ptr->next;
16504 if (ptr == block->last) {
16505 done = 1;
16506 }
Eric Biederman90089602004-05-28 14:11:54 +000016507 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016508 struct triple_set *user, *next;
16509 for(user = ptr->use; user; user = next) {
16510 struct triple *use;
16511 next = user->next;
16512 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016513 if (MISC(ptr, 0) == user->member) {
16514 continue;
16515 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016516 if (use->op != OP_PHI) {
16517 internal_error(state, use, "decl still used");
16518 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016519 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016520 internal_error(state, use, "bad phi use of decl");
16521 }
16522 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016523 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016524 }
Eric Biederman90089602004-05-28 14:11:54 +000016525 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16526 /* Delete the adecl */
16527 release_triple(state, MISC(ptr, 0));
16528 /* And the piece */
16529 release_triple(state, ptr);
16530 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016531 continue;
16532 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016533 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016534 for(user = block->idominates; user; user = user->next) {
16535 prune_block_variables(state, user->member);
16536 }
16537}
16538
Eric Biederman66fe2222003-07-04 15:14:04 +000016539struct phi_triple {
16540 struct triple *phi;
16541 unsigned orig_id;
16542 int alive;
16543};
16544
16545static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16546{
16547 struct triple **slot;
16548 int zrhs, i;
16549 if (live[phi->id].alive) {
16550 return;
16551 }
16552 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016553 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016554 slot = &RHS(phi, 0);
16555 for(i = 0; i < zrhs; i++) {
16556 struct triple *used;
16557 used = slot[i];
16558 if (used && (used->op == OP_PHI)) {
16559 keep_phi(state, live, used);
16560 }
16561 }
16562}
16563
16564static void prune_unused_phis(struct compile_state *state)
16565{
16566 struct triple *first, *phi;
16567 struct phi_triple *live;
16568 int phis, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016569
Eric Biederman66fe2222003-07-04 15:14:04 +000016570 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016571 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016572
16573 /* Count how many phi functions I need to process */
16574 phis = 0;
16575 for(phi = first->next; phi != first; phi = phi->next) {
16576 if (phi->op == OP_PHI) {
16577 phis += 1;
16578 }
16579 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016580
Eric Biederman66fe2222003-07-04 15:14:04 +000016581 /* Mark them all dead */
16582 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16583 phis = 0;
16584 for(phi = first->next; phi != first; phi = phi->next) {
16585 if (phi->op != OP_PHI) {
16586 continue;
16587 }
16588 live[phis].alive = 0;
16589 live[phis].orig_id = phi->id;
16590 live[phis].phi = phi;
16591 phi->id = phis;
16592 phis += 1;
16593 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016594
Eric Biederman66fe2222003-07-04 15:14:04 +000016595 /* Mark phis alive that are used by non phis */
16596 for(i = 0; i < phis; i++) {
16597 struct triple_set *set;
16598 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16599 if (set->member->op != OP_PHI) {
16600 keep_phi(state, live, live[i].phi);
16601 break;
16602 }
16603 }
16604 }
16605
16606 /* Delete the extraneous phis */
16607 for(i = 0; i < phis; i++) {
16608 struct triple **slot;
16609 int zrhs, j;
16610 if (!live[i].alive) {
16611 release_triple(state, live[i].phi);
16612 continue;
16613 }
16614 phi = live[i].phi;
16615 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016616 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016617 for(j = 0; j < zrhs; j++) {
16618 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016619 struct triple *unknown;
16620 get_occurance(phi->occurance);
16621 unknown = flatten(state, state->global_pool,
16622 alloc_triple(state, OP_UNKNOWNVAL,
16623 phi->type, 0, 0, phi->occurance));
16624 slot[j] = unknown;
16625 use_triple(unknown, phi);
16626 transform_to_arch_instruction(state, unknown);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016627#if 0
Eric Biederman90089602004-05-28 14:11:54 +000016628 warning(state, phi, "variable not set at index %d on all paths to use", j);
16629#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016630 }
16631 }
16632 }
16633 xfree(live);
16634}
16635
Eric Biedermanb138ac82003-04-22 18:44:01 +000016636static void transform_to_ssa_form(struct compile_state *state)
16637{
16638 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016639 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016640
Eric Biederman90089602004-05-28 14:11:54 +000016641 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016642 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016643
Eric Biederman90089602004-05-28 14:11:54 +000016644 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016645}
16646
16647
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016648static void clear_vertex(
16649 struct compile_state *state, struct block *block, void *arg)
16650{
Eric Biederman83b991a2003-10-11 06:20:25 +000016651 /* Clear the current blocks vertex and the vertex of all
16652 * of the current blocks neighbors in case there are malformed
16653 * blocks with now instructions at this point.
16654 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016655 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016656 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016657 for(edge = block->edges; edge; edge = edge->next) {
16658 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016659 }
16660 for(user = block->use; user; user = user->next) {
16661 user->member->vertex = 0;
16662 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016663}
16664
16665static void mark_live_block(
16666 struct compile_state *state, struct block *block, int *next_vertex)
16667{
16668 /* See if this is a block that has not been marked */
16669 if (block->vertex != 0) {
16670 return;
16671 }
16672 block->vertex = *next_vertex;
16673 *next_vertex += 1;
16674 if (triple_is_branch(state, block->last)) {
16675 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016676 targ = triple_edge_targ(state, block->last, 0);
16677 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016678 if (!*targ) {
16679 continue;
16680 }
16681 if (!triple_stores_block(state, *targ)) {
16682 internal_error(state, 0, "bad targ");
16683 }
16684 mark_live_block(state, (*targ)->u.block, next_vertex);
16685 }
Eric Biederman90089602004-05-28 14:11:54 +000016686 /* Ensure the last block of a function remains alive */
16687 if (triple_is_call(state, block->last)) {
16688 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16689 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016690 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016691 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016692 struct triple *ins;
16693 ins = block->last->next;
16694 if (!triple_stores_block(state, ins)) {
16695 internal_error(state, 0, "bad block start");
16696 }
16697 mark_live_block(state, ins->u.block, next_vertex);
16698 }
16699}
16700
Eric Biedermanb138ac82003-04-22 18:44:01 +000016701static void transform_from_ssa_form(struct compile_state *state)
16702{
16703 /* To get out of ssa form we insert moves on the incoming
16704 * edges to blocks containting phi functions.
16705 */
16706 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016707 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016708 int next_vertex;
16709
16710 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016711 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016712 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016713 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016714
16715 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016716 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016717 for(phi = first->next; phi != first ; phi = next) {
16718 struct block_set *set;
16719 struct block *block;
16720 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016721 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016722 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016723 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016724 next = phi->next;
16725 if (phi->op != OP_PHI) {
16726 continue;
16727 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016728
Eric Biedermanb138ac82003-04-22 18:44:01 +000016729 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016730 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016731
Eric Biederman83b991a2003-10-11 06:20:25 +000016732 /* If this phi is in a dead block just forget it */
16733 if (block->vertex == 0) {
16734 release_triple(state, phi);
16735 continue;
16736 }
16737
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016738 /* Forget uses from code in dead blocks */
16739 for(use = phi->use; use; use = use_next) {
16740 struct block *ublock;
16741 struct triple **expr;
16742 use_next = use->next;
16743 ublock = block_of_triple(state, use->member);
16744 if ((use->member == phi) || (ublock->vertex != 0)) {
16745 continue;
16746 }
16747 expr = triple_rhs(state, use->member, 0);
16748 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16749 if (*expr == phi) {
16750 *expr = 0;
16751 }
16752 }
16753 unuse_triple(phi, use->member);
16754 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016755 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016756 if (registers_of(state, phi->type) != 1) {
16757 internal_error(state, phi, "phi->type does not fit in a single register!");
16758 }
16759 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16760 var = var->next; /* point at the var */
Stefan Reinauer14e22772010-04-27 06:56:47 +000016761
Eric Biederman83b991a2003-10-11 06:20:25 +000016762 /* Replaces use of phi with var */
16763 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016764
Eric Biederman90089602004-05-28 14:11:54 +000016765 /* Count the readers */
16766 readers = 0;
16767 for(use = var->use; use; use = use->next) {
16768 if (use->member != MISC(var, 0)) {
16769 readers++;
16770 }
16771 }
16772
Eric Biedermanb138ac82003-04-22 18:44:01 +000016773 /* Walk all of the incoming edges/blocks and insert moves.
16774 */
Eric Biederman90089602004-05-28 14:11:54 +000016775 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016776 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016777 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016778 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016779 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016780 eblock = set->member;
16781 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016782 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016783 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016784 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016785
Eric Biederman83b991a2003-10-11 06:20:25 +000016786 /* If we don't have a value that belongs in an OP_WRITE
16787 * continue on.
16788 */
Eric Biederman90089602004-05-28 14:11:54 +000016789 if (!val || (val == &unknown_triple) || (val == phi)
16790 || (vblock && (vblock->vertex == 0))) {
16791 continue;
16792 }
16793 /* If the value should never occur error */
16794 if (!vblock) {
16795 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016796 continue;
16797 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016798
16799 /* If the value occurs in a dead block see if a replacement
16800 * block can be found.
16801 */
16802 while(eblock && (eblock->vertex == 0)) {
16803 eblock = eblock->idom;
16804 }
16805 /* If not continue on with the next value. */
16806 if (!eblock || (eblock->vertex == 0)) {
16807 continue;
16808 }
16809
16810 /* If we have an empty incoming block ignore it. */
16811 if (!eblock->first) {
16812 internal_error(state, 0, "empty block?");
16813 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016814
Eric Biederman530b5192003-07-01 10:05:30 +000016815 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016816 /* Walk through the edge block backwards to find an
16817 * appropriate location for the OP_WRITE.
16818 */
16819 for(base = eblock->last; base != eblock->first; base = base->prev) {
16820 struct triple **expr;
16821 if (base->op == OP_PIECE) {
16822 base = MISC(base, 0);
16823 }
16824 if ((base == var) || (base == val)) {
16825 goto out;
16826 }
16827 expr = triple_lhs(state, base, 0);
16828 for(; expr; expr = triple_lhs(state, base, expr)) {
16829 if ((*expr) == val) {
16830 goto out;
16831 }
16832 }
16833 expr = triple_rhs(state, base, 0);
16834 for(; expr; expr = triple_rhs(state, base, expr)) {
16835 if ((*expr) == var) {
16836 goto out;
16837 }
16838 }
Eric Biederman530b5192003-07-01 10:05:30 +000016839 }
Eric Biederman90089602004-05-28 14:11:54 +000016840 out:
16841 if (triple_is_branch(state, base)) {
16842 internal_error(state, base,
16843 "Could not insert write to phi");
16844 }
16845 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016846 use_triple(val, move);
16847 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016848 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016849 }
Eric Biederman90089602004-05-28 14:11:54 +000016850 if (!writers && readers) {
16851 internal_error(state, var, "no value written to in use phi?");
16852 }
16853 /* If var is not used free it */
16854 if (!writers) {
16855 release_triple(state, MISC(var, 0));
16856 release_triple(state, var);
16857 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016858 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016859 release_triple(state, phi);
16860 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016861
Eric Biederman83b991a2003-10-11 06:20:25 +000016862 /* Walk all of the operations to find the adecls */
16863 for(var = first->next; var != first ; var = var->next) {
16864 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016865 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016866 continue;
16867 }
16868
16869 /* Walk through all of the rhs uses of var and
16870 * replace them with read of var.
16871 */
16872 for(use = var->use; use; use = use_next) {
16873 struct triple *read, *user;
16874 struct triple **slot;
16875 int zrhs, i, used;
16876 use_next = use->next;
16877 user = use->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016878
Eric Biederman83b991a2003-10-11 06:20:25 +000016879 /* Generate a read of var */
16880 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16881 use_triple(var, read);
16882
16883 /* Find the rhs uses and see if they need to be replaced */
16884 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016885 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016886 slot = &RHS(user, 0);
16887 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016888 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016889 slot[i] = read;
16890 used = 1;
16891 }
16892 }
16893 /* If we did use it cleanup the uses */
16894 if (used) {
16895 unuse_triple(var, user);
16896 use_triple(read, user);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016897 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016898 /* If we didn't use it release the extra triple */
16899 else {
16900 release_triple(state, read);
16901 }
16902 }
16903 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016904}
16905
Eric Biederman5ade04a2003-10-22 04:03:46 +000016906#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016907 FILE *fp = state->dbgout; \
16908 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000016909 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000016910
Eric Biederman83b991a2003-10-11 06:20:25 +000016911static void rebuild_ssa_form(struct compile_state *state)
16912{
16913HI();
16914 transform_from_ssa_form(state);
16915HI();
Eric Biederman90089602004-05-28 14:11:54 +000016916 state->bb.first = state->first;
16917 free_basic_blocks(state, &state->bb);
16918 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016919HI();
16920 insert_phi_operations(state);
16921HI();
16922 rename_variables(state);
16923HI();
Stefan Reinauer14e22772010-04-27 06:56:47 +000016924
Eric Biederman90089602004-05-28 14:11:54 +000016925 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016926HI();
16927 prune_unused_phis(state);
16928HI();
16929}
16930#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016931
Stefan Reinauer14e22772010-04-27 06:56:47 +000016932/*
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016933 * Register conflict resolution
16934 * =========================================================
16935 */
16936
16937static struct reg_info find_def_color(
16938 struct compile_state *state, struct triple *def)
16939{
16940 struct triple_set *set;
16941 struct reg_info info;
16942 info.reg = REG_UNSET;
16943 info.regcm = 0;
16944 if (!triple_is_def(state, def)) {
16945 return info;
16946 }
16947 info = arch_reg_lhs(state, def, 0);
16948 if (info.reg >= MAX_REGISTERS) {
16949 info.reg = REG_UNSET;
16950 }
16951 for(set = def->use; set; set = set->next) {
16952 struct reg_info tinfo;
16953 int i;
16954 i = find_rhs_use(state, set->member, def);
16955 if (i < 0) {
16956 continue;
16957 }
16958 tinfo = arch_reg_rhs(state, set->member, i);
16959 if (tinfo.reg >= MAX_REGISTERS) {
16960 tinfo.reg = REG_UNSET;
16961 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016962 if ((tinfo.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016963 (info.reg != REG_UNSET) &&
16964 (tinfo.reg != info.reg)) {
16965 internal_error(state, def, "register conflict");
16966 }
16967 if ((info.regcm & tinfo.regcm) == 0) {
16968 internal_error(state, def, "regcm conflict %x & %x == 0",
16969 info.regcm, tinfo.regcm);
16970 }
16971 if (info.reg == REG_UNSET) {
16972 info.reg = tinfo.reg;
16973 }
16974 info.regcm &= tinfo.regcm;
16975 }
16976 if (info.reg >= MAX_REGISTERS) {
16977 internal_error(state, def, "register out of range");
16978 }
16979 return info;
16980}
16981
16982static struct reg_info find_lhs_pre_color(
16983 struct compile_state *state, struct triple *ins, int index)
16984{
16985 struct reg_info info;
16986 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016987 zrhs = ins->rhs;
16988 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016989 if (!zlhs && triple_is_def(state, ins)) {
16990 zlhs = 1;
16991 }
16992 if (index >= zlhs) {
16993 internal_error(state, ins, "Bad lhs %d", index);
16994 }
16995 info = arch_reg_lhs(state, ins, index);
16996 for(i = 0; i < zrhs; i++) {
16997 struct reg_info rinfo;
16998 rinfo = arch_reg_rhs(state, ins, i);
16999 if ((info.reg == rinfo.reg) &&
17000 (rinfo.reg >= MAX_REGISTERS)) {
17001 struct reg_info tinfo;
17002 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17003 info.reg = tinfo.reg;
17004 info.regcm &= tinfo.regcm;
17005 break;
17006 }
17007 }
17008 if (info.reg >= MAX_REGISTERS) {
17009 info.reg = REG_UNSET;
17010 }
17011 return info;
17012}
17013
17014static struct reg_info find_rhs_post_color(
17015 struct compile_state *state, struct triple *ins, int index);
17016
17017static struct reg_info find_lhs_post_color(
17018 struct compile_state *state, struct triple *ins, int index)
17019{
17020 struct triple_set *set;
17021 struct reg_info info;
17022 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017023#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017024 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017025 ins, index);
17026#endif
17027 if ((index == 0) && triple_is_def(state, ins)) {
17028 lhs = ins;
17029 }
Eric Biederman90089602004-05-28 14:11:54 +000017030 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017031 lhs = LHS(ins, index);
17032 }
17033 else {
17034 internal_error(state, ins, "Bad lhs %d", index);
17035 lhs = 0;
17036 }
17037 info = arch_reg_lhs(state, ins, index);
17038 if (info.reg >= MAX_REGISTERS) {
17039 info.reg = REG_UNSET;
17040 }
17041 for(set = lhs->use; set; set = set->next) {
17042 struct reg_info rinfo;
17043 struct triple *user;
17044 int zrhs, i;
17045 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017046 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017047 for(i = 0; i < zrhs; i++) {
17048 if (RHS(user, i) != lhs) {
17049 continue;
17050 }
17051 rinfo = find_rhs_post_color(state, user, i);
17052 if ((info.reg != REG_UNSET) &&
17053 (rinfo.reg != REG_UNSET) &&
17054 (info.reg != rinfo.reg)) {
17055 internal_error(state, ins, "register conflict");
17056 }
17057 if ((info.regcm & rinfo.regcm) == 0) {
17058 internal_error(state, ins, "regcm conflict %x & %x == 0",
17059 info.regcm, rinfo.regcm);
17060 }
17061 if (info.reg == REG_UNSET) {
17062 info.reg = rinfo.reg;
17063 }
17064 info.regcm &= rinfo.regcm;
17065 }
17066 }
Eric Biederman530b5192003-07-01 10:05:30 +000017067#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017068 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017069 ins, index, info.reg, info.regcm);
17070#endif
17071 return info;
17072}
17073
17074static struct reg_info find_rhs_post_color(
17075 struct compile_state *state, struct triple *ins, int index)
17076{
17077 struct reg_info info, rinfo;
17078 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017079#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017080 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017081 ins, index);
17082#endif
17083 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017084 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017085 if (!zlhs && triple_is_def(state, ins)) {
17086 zlhs = 1;
17087 }
17088 info = rinfo;
17089 if (info.reg >= MAX_REGISTERS) {
17090 info.reg = REG_UNSET;
17091 }
17092 for(i = 0; i < zlhs; i++) {
17093 struct reg_info linfo;
17094 linfo = arch_reg_lhs(state, ins, i);
17095 if ((linfo.reg == rinfo.reg) &&
17096 (linfo.reg >= MAX_REGISTERS)) {
17097 struct reg_info tinfo;
17098 tinfo = find_lhs_post_color(state, ins, i);
17099 if (tinfo.reg >= MAX_REGISTERS) {
17100 tinfo.reg = REG_UNSET;
17101 }
Eric Biederman530b5192003-07-01 10:05:30 +000017102 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017103 info.regcm &= tinfo.regcm;
17104 if (info.reg != REG_UNSET) {
17105 internal_error(state, ins, "register conflict");
17106 }
17107 if (info.regcm == 0) {
17108 internal_error(state, ins, "regcm conflict");
17109 }
17110 info.reg = tinfo.reg;
17111 }
17112 }
Eric Biederman530b5192003-07-01 10:05:30 +000017113#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017114 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017115 ins, index, info.reg, info.regcm);
17116#endif
17117 return info;
17118}
17119
17120static struct reg_info find_lhs_color(
17121 struct compile_state *state, struct triple *ins, int index)
17122{
17123 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017124#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017125 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017126 ins, index);
17127#endif
17128 pre = find_lhs_pre_color(state, ins, index);
17129 post = find_lhs_post_color(state, ins, index);
17130 if ((pre.reg != post.reg) &&
17131 (pre.reg != REG_UNSET) &&
17132 (post.reg != REG_UNSET)) {
17133 internal_error(state, ins, "register conflict");
17134 }
17135 info.regcm = pre.regcm & post.regcm;
17136 info.reg = pre.reg;
17137 if (info.reg == REG_UNSET) {
17138 info.reg = post.reg;
17139 }
Eric Biederman530b5192003-07-01 10:05:30 +000017140#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017141 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017142 ins, index, info.reg, info.regcm,
17143 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017144#endif
17145 return info;
17146}
17147
17148static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17149{
17150 struct triple_set *entry, *next;
17151 struct triple *out;
17152 struct reg_info info, rinfo;
17153
17154 info = arch_reg_lhs(state, ins, 0);
17155 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17156 use_triple(RHS(out, 0), out);
17157 /* Get the users of ins to use out instead */
17158 for(entry = ins->use; entry; entry = next) {
17159 int i;
17160 next = entry->next;
17161 if (entry->member == out) {
17162 continue;
17163 }
17164 i = find_rhs_use(state, entry->member, ins);
17165 if (i < 0) {
17166 continue;
17167 }
17168 rinfo = arch_reg_rhs(state, entry->member, i);
17169 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17170 continue;
17171 }
17172 replace_rhs_use(state, ins, out, entry->member);
17173 }
17174 transform_to_arch_instruction(state, out);
17175 return out;
17176}
17177
Eric Biedermand1ea5392003-06-28 06:49:45 +000017178static struct triple *typed_pre_copy(
17179 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017180{
17181 /* Carefully insert enough operations so that I can
17182 * enter any operation with a GPR32.
17183 */
17184 struct triple *in;
17185 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017186 unsigned classes;
17187 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017188 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017189 if (ins->op == OP_PHI) {
17190 internal_error(state, ins, "pre_copy on a phi?");
17191 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017192 classes = arch_type_to_regcm(state, type);
17193 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017194 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017195 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017196 FILE *fp = state->errout;
17197 fprintf(fp, "src_type: ");
17198 name_of(fp, ins->type);
17199 fprintf(fp, "\ndst_type: ");
17200 name_of(fp, type);
17201 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017202 internal_error(state, ins, "pre_copy with no register classes");
17203 }
Eric Biederman90089602004-05-28 14:11:54 +000017204 op = OP_COPY;
17205 if (!equiv_types(type, (*expr)->type)) {
17206 op = OP_CONVERT;
17207 }
17208 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017209 unuse_triple(*expr, ins);
17210 *expr = in;
17211 use_triple(RHS(in, 0), in);
17212 use_triple(in, ins);
17213 transform_to_arch_instruction(state, in);
17214 return in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017215
Eric Biedermand1ea5392003-06-28 06:49:45 +000017216}
17217static struct triple *pre_copy(
17218 struct compile_state *state, struct triple *ins, int index)
17219{
17220 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017221}
17222
17223
Eric Biedermanb138ac82003-04-22 18:44:01 +000017224static void insert_copies_to_phi(struct compile_state *state)
17225{
17226 /* To get out of ssa form we insert moves on the incoming
17227 * edges to blocks containting phi functions.
17228 */
17229 struct triple *first;
17230 struct triple *phi;
17231
17232 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017233 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017234 for(phi = first->next; phi != first ; phi = phi->next) {
17235 struct block_set *set;
17236 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017237 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017238 int edge;
17239 if (phi->op != OP_PHI) {
17240 continue;
17241 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017242 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017243 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017244 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017245 /* Phi's that feed into mandatory live range joins
17246 * cause nasty complications. Insert a copy of
17247 * the phi value so I never have to deal with
17248 * that in the rest of the code.
17249 */
17250 copy = post_copy(state, phi);
17251 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017252 /* Walk all of the incoming edges/blocks and insert moves.
17253 */
17254 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17255 struct block *eblock;
17256 struct triple *move;
17257 struct triple *val;
17258 struct triple *ptr;
17259 eblock = set->member;
17260 val = slot[edge];
17261
17262 if (val == phi) {
17263 continue;
17264 }
17265
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017266 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017267 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017268 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017269 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017270 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017271 use_triple(val, move);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017272
Eric Biedermanb138ac82003-04-22 18:44:01 +000017273 slot[edge] = move;
17274 unuse_triple(val, phi);
17275 use_triple(move, phi);
17276
Eric Biederman66fe2222003-07-04 15:14:04 +000017277 /* Walk up the dominator tree until I have found the appropriate block */
17278 while(eblock && !tdominates(state, val, eblock->last)) {
17279 eblock = eblock->idom;
17280 }
17281 if (!eblock) {
17282 internal_error(state, phi, "Cannot find block dominated by %p",
17283 val);
17284 }
17285
Eric Biedermanb138ac82003-04-22 18:44:01 +000017286 /* Walk through the block backwards to find
17287 * an appropriate location for the OP_COPY.
17288 */
17289 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17290 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017291 if (ptr->op == OP_PIECE) {
17292 ptr = MISC(ptr, 0);
17293 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017294 if ((ptr == phi) || (ptr == val)) {
17295 goto out;
17296 }
Eric Biederman90089602004-05-28 14:11:54 +000017297 expr = triple_lhs(state, ptr, 0);
17298 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17299 if ((*expr) == val) {
17300 goto out;
17301 }
17302 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017303 expr = triple_rhs(state, ptr, 0);
17304 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17305 if ((*expr) == phi) {
17306 goto out;
17307 }
17308 }
17309 }
17310 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017311 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017312 internal_error(state, ptr,
17313 "Could not insert write to phi");
17314 }
Eric Biederman90089602004-05-28 14:11:54 +000017315 insert_triple(state, after_lhs(state, ptr), move);
17316 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017317 eblock->last = move;
17318 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017319 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017320 }
17321 }
Eric Biederman90089602004-05-28 14:11:54 +000017322 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017323}
17324
Eric Biederman90089602004-05-28 14:11:54 +000017325struct triple_reg_set;
17326struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017327
Eric Biedermanb138ac82003-04-22 18:44:01 +000017328
Stefan Reinauer14e22772010-04-27 06:56:47 +000017329static int do_triple_set(struct triple_reg_set **head,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017330 struct triple *member, struct triple *new_member)
17331{
17332 struct triple_reg_set **ptr, *new;
17333 if (!member)
17334 return 0;
17335 ptr = head;
17336 while(*ptr) {
17337 if ((*ptr)->member == member) {
17338 return 0;
17339 }
17340 ptr = &(*ptr)->next;
17341 }
17342 new = xcmalloc(sizeof(*new), "triple_set");
17343 new->member = member;
17344 new->new = new_member;
17345 new->next = *head;
17346 *head = new;
17347 return 1;
17348}
17349
17350static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17351{
17352 struct triple_reg_set *entry, **ptr;
17353 ptr = head;
17354 while(*ptr) {
17355 entry = *ptr;
17356 if (entry->member == member) {
17357 *ptr = entry->next;
17358 xfree(entry);
17359 return;
17360 }
17361 else {
17362 ptr = &entry->next;
17363 }
17364 }
17365}
17366
17367static int in_triple(struct reg_block *rb, struct triple *in)
17368{
17369 return do_triple_set(&rb->in, in, 0);
17370}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017371
17372#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017373static void unin_triple(struct reg_block *rb, struct triple *unin)
17374{
17375 do_triple_unset(&rb->in, unin);
17376}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017377#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017378
17379static int out_triple(struct reg_block *rb, struct triple *out)
17380{
17381 return do_triple_set(&rb->out, out, 0);
17382}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017383#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017384static void unout_triple(struct reg_block *rb, struct triple *unout)
17385{
17386 do_triple_unset(&rb->out, unout);
17387}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017388#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017389
17390static int initialize_regblock(struct reg_block *blocks,
17391 struct block *block, int vertex)
17392{
17393 struct block_set *user;
17394 if (!block || (blocks[block->vertex].block == block)) {
17395 return vertex;
17396 }
17397 vertex += 1;
17398 /* Renumber the blocks in a convinient fashion */
17399 block->vertex = vertex;
17400 blocks[vertex].block = block;
17401 blocks[vertex].vertex = vertex;
17402 for(user = block->use; user; user = user->next) {
17403 vertex = initialize_regblock(blocks, user->member, vertex);
17404 }
17405 return vertex;
17406}
17407
Eric Biederman90089602004-05-28 14:11:54 +000017408static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17409{
17410/* Part to piece is a best attempt and it cannot be correct all by
17411 * itself. If various values are read as different sizes in different
17412 * parts of the code this function cannot work. Or rather it cannot
17413 * work in conjunction with compute_variable_liftimes. As the
17414 * analysis will get confused.
17415 */
17416 struct triple *base;
17417 unsigned reg;
17418 if (!is_lvalue(state, ins)) {
17419 return ins;
17420 }
17421 base = 0;
17422 reg = 0;
17423 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17424 base = MISC(ins, 0);
17425 switch(ins->op) {
17426 case OP_INDEX:
17427 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17428 break;
17429 case OP_DOT:
17430 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17431 break;
17432 default:
17433 internal_error(state, ins, "unhandled part");
17434 break;
17435 }
17436 ins = base;
17437 }
17438 if (base) {
17439 if (reg > base->lhs) {
17440 internal_error(state, base, "part out of range?");
17441 }
17442 ins = LHS(base, reg);
17443 }
17444 return ins;
17445}
17446
Stefan Reinauer14e22772010-04-27 06:56:47 +000017447static int this_def(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017448 struct triple *ins, struct triple *other)
17449{
17450 if (ins == other) {
17451 return 1;
17452 }
17453 if (ins->op == OP_WRITE) {
17454 ins = part_to_piece(state, MISC(ins, 0));
17455 }
17456 return ins == other;
17457}
17458
Eric Biedermanb138ac82003-04-22 18:44:01 +000017459static int phi_in(struct compile_state *state, struct reg_block *blocks,
17460 struct reg_block *rb, struct block *suc)
17461{
17462 /* Read the conditional input set of a successor block
17463 * (i.e. the input to the phi nodes) and place it in the
17464 * current blocks output set.
17465 */
17466 struct block_set *set;
17467 struct triple *ptr;
17468 int edge;
17469 int done, change;
17470 change = 0;
17471 /* Find the edge I am coming in on */
17472 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17473 if (set->member == rb->block) {
17474 break;
17475 }
17476 }
17477 if (!set) {
17478 internal_error(state, 0, "Not coming on a control edge?");
17479 }
17480 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17481 struct triple **slot, *expr, *ptr2;
17482 int out_change, done2;
17483 done = (ptr == suc->last);
17484 if (ptr->op != OP_PHI) {
17485 continue;
17486 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017487 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017488 expr = slot[edge];
17489 out_change = out_triple(rb, expr);
17490 if (!out_change) {
17491 continue;
17492 }
17493 /* If we don't define the variable also plast it
17494 * in the current blocks input set.
17495 */
17496 ptr2 = rb->block->first;
17497 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017498 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017499 break;
17500 }
17501 done2 = (ptr2 == rb->block->last);
17502 }
17503 if (!done2) {
17504 continue;
17505 }
17506 change |= in_triple(rb, expr);
17507 }
17508 return change;
17509}
17510
17511static int reg_in(struct compile_state *state, struct reg_block *blocks,
17512 struct reg_block *rb, struct block *suc)
17513{
17514 struct triple_reg_set *in_set;
17515 int change;
17516 change = 0;
17517 /* Read the input set of a successor block
17518 * and place it in the current blocks output set.
17519 */
17520 in_set = blocks[suc->vertex].in;
17521 for(; in_set; in_set = in_set->next) {
17522 int out_change, done;
17523 struct triple *first, *last, *ptr;
17524 out_change = out_triple(rb, in_set->member);
17525 if (!out_change) {
17526 continue;
17527 }
17528 /* If we don't define the variable also place it
17529 * in the current blocks input set.
17530 */
17531 first = rb->block->first;
17532 last = rb->block->last;
17533 done = 0;
17534 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017535 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017536 break;
17537 }
17538 done = (ptr == last);
17539 }
17540 if (!done) {
17541 continue;
17542 }
17543 change |= in_triple(rb, in_set->member);
17544 }
17545 change |= phi_in(state, blocks, rb, suc);
17546 return change;
17547}
17548
Eric Biedermanb138ac82003-04-22 18:44:01 +000017549static int use_in(struct compile_state *state, struct reg_block *rb)
17550{
17551 /* Find the variables we use but don't define and add
17552 * it to the current blocks input set.
17553 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017554#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017555#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017556#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017557 struct block *block;
17558 struct triple *ptr;
17559 int done;
17560 int change;
17561 block = rb->block;
17562 change = 0;
17563 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17564 struct triple **expr;
17565 done = (ptr == block->first);
17566 /* The variable a phi function uses depends on the
17567 * control flow, and is handled in phi_in, not
17568 * here.
17569 */
17570 if (ptr->op == OP_PHI) {
17571 continue;
17572 }
17573 expr = triple_rhs(state, ptr, 0);
17574 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17575 struct triple *rhs, *test;
17576 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017577 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017578 if (!rhs) {
17579 continue;
17580 }
Eric Biederman90089602004-05-28 14:11:54 +000017581
17582 /* See if rhs is defined in this block.
17583 * A write counts as a definition.
17584 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017585 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17586 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017587 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017588 rhs = 0;
17589 break;
17590 }
17591 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017592 /* If I still have a valid rhs add it to in */
17593 change |= in_triple(rb, rhs);
17594 }
17595 }
17596 return change;
17597}
17598
17599static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017600 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017601{
17602 struct reg_block *blocks;
17603 int change;
17604 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017605 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17606 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017607 do {
17608 int i;
17609 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017610 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017611 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017612 struct reg_block *rb;
17613 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017614 /* Add the all successor's input set to in */
17615 for(edge = rb->block->edges; edge; edge = edge->next) {
17616 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017617 }
17618 /* Add use to in... */
17619 change |= use_in(state, rb);
17620 }
17621 } while(change);
17622 return blocks;
17623}
17624
Stefan Reinauer14e22772010-04-27 06:56:47 +000017625static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017626 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017627{
17628 int i;
17629 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017630 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017631 struct triple_reg_set *entry, *next;
17632 struct reg_block *rb;
17633 rb = &blocks[i];
17634 for(entry = rb->in; entry ; entry = next) {
17635 next = entry->next;
17636 do_triple_unset(&rb->in, entry->member);
17637 }
17638 for(entry = rb->out; entry; entry = next) {
17639 next = entry->next;
17640 do_triple_unset(&rb->out, entry->member);
17641 }
17642 }
17643 xfree(blocks);
17644
17645}
17646
Eric Biedermanf96a8102003-06-16 16:57:34 +000017647typedef void (*wvl_cb_t)(
Stefan Reinauer14e22772010-04-27 06:56:47 +000017648 struct compile_state *state,
17649 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017650 struct reg_block *rb, struct triple *ins, void *arg);
17651
17652static void walk_variable_lifetimes(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000017653 struct basic_blocks *bb, struct reg_block *blocks,
Eric Biederman90089602004-05-28 14:11:54 +000017654 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017655{
17656 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017657
Eric Biederman90089602004-05-28 14:11:54 +000017658 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017659 struct triple_reg_set *live;
17660 struct triple_reg_set *entry, *next;
17661 struct triple *ptr, *prev;
17662 struct reg_block *rb;
17663 struct block *block;
17664 int done;
17665
17666 /* Get the blocks */
17667 rb = &blocks[i];
17668 block = rb->block;
17669
17670 /* Copy out into live */
17671 live = 0;
17672 for(entry = rb->out; entry; entry = next) {
17673 next = entry->next;
17674 do_triple_set(&live, entry->member, entry->new);
17675 }
17676 /* Walk through the basic block calculating live */
17677 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017678 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017679
17680 prev = ptr->prev;
17681 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017682
17683 /* Ensure the current definition is in live */
17684 if (triple_is_def(state, ptr)) {
17685 do_triple_set(&live, ptr, 0);
17686 }
17687
17688 /* Inform the callback function of what is
17689 * going on.
17690 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017691 cb(state, blocks, live, rb, ptr, arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017692
Eric Biedermanb138ac82003-04-22 18:44:01 +000017693 /* Remove the current definition from live */
17694 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017695
Eric Biedermanb138ac82003-04-22 18:44:01 +000017696 /* Add the current uses to live.
17697 *
17698 * It is safe to skip phi functions because they do
17699 * not have any block local uses, and the block
17700 * output sets already properly account for what
17701 * control flow depedent uses phi functions do have.
17702 */
17703 if (ptr->op == OP_PHI) {
17704 continue;
17705 }
17706 expr = triple_rhs(state, ptr, 0);
17707 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17708 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017709 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017710 continue;
17711 }
17712 do_triple_set(&live, *expr, 0);
17713 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017714 }
17715 /* Free live */
17716 for(entry = live; entry; entry = next) {
17717 next = entry->next;
17718 do_triple_unset(&live, entry->member);
17719 }
17720 }
17721}
17722
Eric Biederman90089602004-05-28 14:11:54 +000017723struct print_live_variable_info {
17724 struct reg_block *rb;
17725 FILE *fp;
17726};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017727#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017728static void print_live_variables_block(
17729 struct compile_state *state, struct block *block, void *arg)
17730
17731{
17732 struct print_live_variable_info *info = arg;
17733 struct block_set *edge;
17734 FILE *fp = info->fp;
17735 struct reg_block *rb;
17736 struct triple *ptr;
17737 int phi_present;
17738 int done;
17739 rb = &info->rb[block->vertex];
17740
17741 fprintf(fp, "\nblock: %p (%d),",
17742 block, block->vertex);
17743 for(edge = block->edges; edge; edge = edge->next) {
17744 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000017745 edge->member,
Eric Biederman90089602004-05-28 14:11:54 +000017746 edge->member && edge->member->use?edge->member->use->member : 0);
17747 }
17748 fprintf(fp, "\n");
17749 if (rb->in) {
17750 struct triple_reg_set *in_set;
17751 fprintf(fp, " in:");
17752 for(in_set = rb->in; in_set; in_set = in_set->next) {
17753 fprintf(fp, " %-10p", in_set->member);
17754 }
17755 fprintf(fp, "\n");
17756 }
17757 phi_present = 0;
17758 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17759 done = (ptr == block->last);
17760 if (ptr->op == OP_PHI) {
17761 phi_present = 1;
17762 break;
17763 }
17764 }
17765 if (phi_present) {
17766 int edge;
17767 for(edge = 0; edge < block->users; edge++) {
17768 fprintf(fp, " in(%d):", edge);
17769 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17770 struct triple **slot;
17771 done = (ptr == block->last);
17772 if (ptr->op != OP_PHI) {
17773 continue;
17774 }
17775 slot = &RHS(ptr, 0);
17776 fprintf(fp, " %-10p", slot[edge]);
17777 }
17778 fprintf(fp, "\n");
17779 }
17780 }
17781 if (block->first->op == OP_LABEL) {
17782 fprintf(fp, "%p:\n", block->first);
17783 }
17784 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17785 done = (ptr == block->last);
17786 display_triple(fp, ptr);
17787 }
17788 if (rb->out) {
17789 struct triple_reg_set *out_set;
17790 fprintf(fp, " out:");
17791 for(out_set = rb->out; out_set; out_set = out_set->next) {
17792 fprintf(fp, " %-10p", out_set->member);
17793 }
17794 fprintf(fp, "\n");
17795 }
17796 fprintf(fp, "\n");
17797}
17798
Stefan Reinauer14e22772010-04-27 06:56:47 +000017799static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017800 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17801{
17802 struct print_live_variable_info info;
17803 info.rb = rb;
17804 info.fp = fp;
17805 fprintf(fp, "\nlive variables by block\n");
17806 walk_blocks(state, bb, print_live_variables_block, &info);
17807
17808}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017809#endif
Eric Biederman90089602004-05-28 14:11:54 +000017810
Eric Biedermanb138ac82003-04-22 18:44:01 +000017811static int count_triples(struct compile_state *state)
17812{
17813 struct triple *first, *ins;
17814 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017815 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017816 ins = first;
17817 do {
17818 triples++;
17819 ins = ins->next;
17820 } while (ins != first);
17821 return triples;
17822}
Eric Biederman66fe2222003-07-04 15:14:04 +000017823
17824
Eric Biedermanb138ac82003-04-22 18:44:01 +000017825struct dead_triple {
17826 struct triple *triple;
17827 struct dead_triple *work_next;
17828 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017829 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017830 int flags;
17831#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017832#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017833};
17834
Stefan Reinauer14e22772010-04-27 06:56:47 +000017835static void print_dead_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017836 struct dead_triple *dtriple)
17837{
17838 struct triple *first, *ins;
17839 struct dead_triple *dt;
17840 FILE *fp;
17841 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17842 return;
17843 }
17844 fp = state->dbgout;
17845 fprintf(fp, "--------------- dtriples ---------------\n");
17846 first = state->first;
17847 ins = first;
17848 do {
17849 dt = &dtriple[ins->id];
17850 if ((ins->op == OP_LABEL) && (ins->use)) {
17851 fprintf(fp, "\n%p:\n", ins);
17852 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000017853 fprintf(fp, "%c",
Eric Biederman90089602004-05-28 14:11:54 +000017854 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17855 display_triple(fp, ins);
17856 if (triple_is_branch(state, ins)) {
17857 fprintf(fp, "\n");
17858 }
17859 ins = ins->next;
17860 } while(ins != first);
17861 fprintf(fp, "\n");
17862}
17863
Eric Biedermanb138ac82003-04-22 18:44:01 +000017864
17865static void awaken(
17866 struct compile_state *state,
17867 struct dead_triple *dtriple, struct triple **expr,
17868 struct dead_triple ***work_list_tail)
17869{
17870 struct triple *triple;
17871 struct dead_triple *dt;
17872 if (!expr) {
17873 return;
17874 }
17875 triple = *expr;
17876 if (!triple) {
17877 return;
17878 }
17879 if (triple->id <= 0) {
17880 internal_error(state, triple, "bad triple id: %d",
17881 triple->id);
17882 }
17883 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017884 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017885 return;
17886 }
17887 dt = &dtriple[triple->id];
17888 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17889 dt->flags |= TRIPLE_FLAG_ALIVE;
17890 if (!dt->work_next) {
17891 **work_list_tail = dt;
17892 *work_list_tail = &dt->work_next;
17893 }
17894 }
17895}
17896
17897static void eliminate_inefectual_code(struct compile_state *state)
17898{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017899 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17900 int triples, i;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010017901 struct triple *first, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017902
Eric Biederman5ade04a2003-10-22 04:03:46 +000017903 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17904 return;
17905 }
17906
Eric Biedermanb138ac82003-04-22 18:44:01 +000017907 /* Setup the work list */
17908 work_list = 0;
17909 work_list_tail = &work_list;
17910
Eric Biederman83b991a2003-10-11 06:20:25 +000017911 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017912
17913 /* Count how many triples I have */
17914 triples = count_triples(state);
17915
17916 /* Now put then in an array and mark all of the triples dead */
17917 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
Stefan Reinauer14e22772010-04-27 06:56:47 +000017918
Eric Biedermanb138ac82003-04-22 18:44:01 +000017919 ins = first;
17920 i = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017921 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017922 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017923 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017924 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017925 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017926 ins->id = i;
17927 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017928 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017929 awaken(state, dtriple, &ins, &work_list_tail);
17930 }
17931 i++;
17932 ins = ins->next;
17933 } while(ins != first);
17934 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017935 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017936 struct dead_triple *dt;
17937 struct block_set *user;
17938 struct triple **expr;
17939 dt = work_list;
17940 work_list = dt->work_next;
17941 if (!work_list) {
17942 work_list_tail = &work_list;
17943 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017944 /* Make certain the block the current instruction is in lives */
17945 block = block_of_triple(state, dt->triple);
17946 awaken(state, dtriple, &block->first, &work_list_tail);
17947 if (triple_is_branch(state, block->last)) {
17948 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017949 } else {
17950 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017951 }
17952
Eric Biedermanb138ac82003-04-22 18:44:01 +000017953 /* Wake up the data depencencies of this triple */
17954 expr = 0;
17955 do {
17956 expr = triple_rhs(state, dt->triple, expr);
17957 awaken(state, dtriple, expr, &work_list_tail);
17958 } while(expr);
17959 do {
17960 expr = triple_lhs(state, dt->triple, expr);
17961 awaken(state, dtriple, expr, &work_list_tail);
17962 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017963 do {
17964 expr = triple_misc(state, dt->triple, expr);
17965 awaken(state, dtriple, expr, &work_list_tail);
17966 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017967 /* Wake up the forward control dependencies */
17968 do {
17969 expr = triple_targ(state, dt->triple, expr);
17970 awaken(state, dtriple, expr, &work_list_tail);
17971 } while(expr);
17972 /* Wake up the reverse control dependencies of this triple */
17973 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017974 struct triple *last;
17975 last = user->member->last;
17976 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017977#if DEBUG_ROMCC_WARNINGS
17978#warning "Should we bring the awakening noops back?"
17979#endif
17980 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017981 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017982 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017983 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017984 }
17985 }
Eric Biederman90089602004-05-28 14:11:54 +000017986 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017987 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000017988 if ((dt->triple->op == OP_NOOP) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000017989 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17990 internal_error(state, dt->triple, "noop effective?");
17991 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017992 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017993 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017994 release_triple(state, dt->triple);
17995 }
17996 }
17997 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000017998
17999 rebuild_ssa_form(state);
18000
Eric Biederman90089602004-05-28 14:11:54 +000018001 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018002}
18003
18004
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018005static void insert_mandatory_copies(struct compile_state *state)
18006{
18007 struct triple *ins, *first;
18008
18009 /* The object is with a minimum of inserted copies,
18010 * to resolve in fundamental register conflicts between
18011 * register value producers and consumers.
18012 * Theoretically we may be greater than minimal when we
18013 * are inserting copies before instructions but that
18014 * case should be rare.
18015 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018016 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018017 ins = first;
18018 do {
18019 struct triple_set *entry, *next;
18020 struct triple *tmp;
18021 struct reg_info info;
18022 unsigned reg, regcm;
18023 int do_post_copy, do_pre_copy;
18024 tmp = 0;
18025 if (!triple_is_def(state, ins)) {
18026 goto next;
18027 }
18028 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018029 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018030 if (info.reg >= MAX_REGISTERS) {
18031 info.reg = REG_UNSET;
18032 }
Eric Biederman90089602004-05-28 14:11:54 +000018033
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018034 reg = REG_UNSET;
18035 regcm = arch_type_to_regcm(state, ins->type);
18036 do_post_copy = do_pre_copy = 0;
18037
18038 /* Walk through the uses of ins and check for conflicts */
18039 for(entry = ins->use; entry; entry = next) {
18040 struct reg_info rinfo;
18041 int i;
18042 next = entry->next;
18043 i = find_rhs_use(state, entry->member, ins);
18044 if (i < 0) {
18045 continue;
18046 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018047
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018048 /* Find the users color requirements */
18049 rinfo = arch_reg_rhs(state, entry->member, i);
18050 if (rinfo.reg >= MAX_REGISTERS) {
18051 rinfo.reg = REG_UNSET;
18052 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018053
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018054 /* See if I need a pre_copy */
18055 if (rinfo.reg != REG_UNSET) {
18056 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18057 do_pre_copy = 1;
18058 }
18059 reg = rinfo.reg;
18060 }
18061 regcm &= rinfo.regcm;
18062 regcm = arch_regcm_normalize(state, regcm);
18063 if (regcm == 0) {
18064 do_pre_copy = 1;
18065 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018066 /* Always use pre_copies for constants.
18067 * They do not take up any registers until a
18068 * copy places them in one.
18069 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000018070 if ((info.reg == REG_UNNEEDED) &&
Eric Biedermand1ea5392003-06-28 06:49:45 +000018071 (rinfo.reg != REG_UNNEEDED)) {
18072 do_pre_copy = 1;
18073 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018074 }
18075 do_post_copy =
18076 !do_pre_copy &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000018077 (((info.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018078 (reg != REG_UNSET) &&
18079 (info.reg != reg)) ||
18080 ((info.regcm & regcm) == 0));
18081
18082 reg = info.reg;
18083 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018084 /* 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 +000018085 for(entry = ins->use; entry; entry = next) {
18086 struct reg_info rinfo;
18087 int i;
18088 next = entry->next;
18089 i = find_rhs_use(state, entry->member, ins);
18090 if (i < 0) {
18091 continue;
18092 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018093
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018094 /* Find the users color requirements */
18095 rinfo = arch_reg_rhs(state, entry->member, i);
18096 if (rinfo.reg >= MAX_REGISTERS) {
18097 rinfo.reg = REG_UNSET;
18098 }
18099
18100 /* Now see if it is time to do the pre_copy */
18101 if (rinfo.reg != REG_UNSET) {
18102 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18103 ((regcm & rinfo.regcm) == 0) ||
18104 /* Don't let a mandatory coalesce sneak
18105 * into a operation that is marked to prevent
18106 * coalescing.
18107 */
18108 ((reg != REG_UNNEEDED) &&
18109 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18110 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18111 ) {
18112 if (do_pre_copy) {
18113 struct triple *user;
18114 user = entry->member;
18115 if (RHS(user, i) != ins) {
18116 internal_error(state, user, "bad rhs");
18117 }
18118 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018119 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018120 continue;
18121 } else {
18122 do_post_copy = 1;
18123 }
18124 }
18125 reg = rinfo.reg;
18126 }
18127 if ((regcm & rinfo.regcm) == 0) {
18128 if (do_pre_copy) {
18129 struct triple *user;
18130 user = entry->member;
18131 if (RHS(user, i) != ins) {
18132 internal_error(state, user, "bad rhs");
18133 }
18134 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018135 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018136 continue;
18137 } else {
18138 do_post_copy = 1;
18139 }
18140 }
18141 regcm &= rinfo.regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018142
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018143 }
18144 if (do_post_copy) {
18145 struct reg_info pre, post;
18146 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018147 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018148 pre = arch_reg_lhs(state, ins, 0);
18149 post = arch_reg_lhs(state, tmp, 0);
18150 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18151 internal_error(state, tmp, "useless copy");
18152 }
18153 }
18154 next:
18155 ins = ins->next;
18156 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018157
Eric Biederman90089602004-05-28 14:11:54 +000018158 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018159}
18160
18161
Eric Biedermanb138ac82003-04-22 18:44:01 +000018162struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018163struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018164struct live_range {
18165 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018166 struct live_range_def *defs;
18167/* Note. The list pointed to by defs is kept in order.
18168 * That is baring splits in the flow control
18169 * defs dominates defs->next wich dominates defs->next->next
18170 * etc.
18171 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018172 unsigned color;
18173 unsigned classes;
18174 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018175 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018176 struct live_range *group_next, **group_prev;
18177};
18178
18179struct live_range_edge {
18180 struct live_range_edge *next;
18181 struct live_range *node;
18182};
18183
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018184struct live_range_def {
18185 struct live_range_def *next;
18186 struct live_range_def *prev;
18187 struct live_range *lr;
18188 struct triple *def;
18189 unsigned orig_id;
18190};
18191
Eric Biedermanb138ac82003-04-22 18:44:01 +000018192#define LRE_HASH_SIZE 2048
18193struct lre_hash {
18194 struct lre_hash *next;
18195 struct live_range *left;
18196 struct live_range *right;
18197};
18198
18199
18200struct reg_state {
18201 struct lre_hash *hash[LRE_HASH_SIZE];
18202 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018203 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018204 struct live_range *lr;
18205 struct live_range *low, **low_tail;
18206 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018207 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018208 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018209 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018210};
18211
18212
Eric Biedermand1ea5392003-06-28 06:49:45 +000018213struct print_interference_block_info {
18214 struct reg_state *rstate;
18215 FILE *fp;
18216 int need_edges;
18217};
18218static void print_interference_block(
18219 struct compile_state *state, struct block *block, void *arg)
18220
18221{
18222 struct print_interference_block_info *info = arg;
18223 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018224 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018225 FILE *fp = info->fp;
18226 struct reg_block *rb;
18227 struct triple *ptr;
18228 int phi_present;
18229 int done;
18230 rb = &rstate->blocks[block->vertex];
18231
Eric Biederman5ade04a2003-10-22 04:03:46 +000018232 fprintf(fp, "\nblock: %p (%d),",
18233 block, block->vertex);
18234 for(edge = block->edges; edge; edge = edge->next) {
18235 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000018236 edge->member,
Eric Biederman5ade04a2003-10-22 04:03:46 +000018237 edge->member && edge->member->use?edge->member->use->member : 0);
18238 }
18239 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018240 if (rb->in) {
18241 struct triple_reg_set *in_set;
18242 fprintf(fp, " in:");
18243 for(in_set = rb->in; in_set; in_set = in_set->next) {
18244 fprintf(fp, " %-10p", in_set->member);
18245 }
18246 fprintf(fp, "\n");
18247 }
18248 phi_present = 0;
18249 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18250 done = (ptr == block->last);
18251 if (ptr->op == OP_PHI) {
18252 phi_present = 1;
18253 break;
18254 }
18255 }
18256 if (phi_present) {
18257 int edge;
18258 for(edge = 0; edge < block->users; edge++) {
18259 fprintf(fp, " in(%d):", edge);
18260 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18261 struct triple **slot;
18262 done = (ptr == block->last);
18263 if (ptr->op != OP_PHI) {
18264 continue;
18265 }
18266 slot = &RHS(ptr, 0);
18267 fprintf(fp, " %-10p", slot[edge]);
18268 }
18269 fprintf(fp, "\n");
18270 }
18271 }
18272 if (block->first->op == OP_LABEL) {
18273 fprintf(fp, "%p:\n", block->first);
18274 }
18275 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018276 struct live_range *lr;
18277 unsigned id;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018278 done = (ptr == block->last);
18279 lr = rstate->lrd[ptr->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018280
Eric Biedermand1ea5392003-06-28 06:49:45 +000018281 id = ptr->id;
18282 ptr->id = rstate->lrd[id].orig_id;
18283 SET_REG(ptr->id, lr->color);
18284 display_triple(fp, ptr);
18285 ptr->id = id;
18286
18287 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18288 internal_error(state, ptr, "lr has no defs!");
18289 }
18290 if (info->need_edges) {
18291 if (lr->defs) {
18292 struct live_range_def *lrd;
18293 fprintf(fp, " range:");
18294 lrd = lr->defs;
18295 do {
18296 fprintf(fp, " %-10p", lrd->def);
18297 lrd = lrd->next;
18298 } while(lrd != lr->defs);
18299 fprintf(fp, "\n");
18300 }
18301 if (lr->edges > 0) {
18302 struct live_range_edge *edge;
18303 fprintf(fp, " edges:");
18304 for(edge = lr->edges; edge; edge = edge->next) {
18305 struct live_range_def *lrd;
18306 lrd = edge->node->defs;
18307 do {
18308 fprintf(fp, " %-10p", lrd->def);
18309 lrd = lrd->next;
18310 } while(lrd != edge->node->defs);
18311 fprintf(fp, "|");
18312 }
18313 fprintf(fp, "\n");
18314 }
18315 }
18316 /* Do a bunch of sanity checks */
18317 valid_ins(state, ptr);
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018318 if (ptr->id > rstate->defs) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018319 internal_error(state, ptr, "Invalid triple id: %d",
18320 ptr->id);
18321 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018322 }
18323 if (rb->out) {
18324 struct triple_reg_set *out_set;
18325 fprintf(fp, " out:");
18326 for(out_set = rb->out; out_set; out_set = out_set->next) {
18327 fprintf(fp, " %-10p", out_set->member);
18328 }
18329 fprintf(fp, "\n");
18330 }
18331 fprintf(fp, "\n");
18332}
18333
18334static void print_interference_blocks(
18335 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18336{
18337 struct print_interference_block_info info;
18338 info.rstate = rstate;
18339 info.fp = fp;
18340 info.need_edges = need_edges;
18341 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018342 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018343
18344}
18345
Eric Biedermanb138ac82003-04-22 18:44:01 +000018346static unsigned regc_max_size(struct compile_state *state, int classes)
18347{
18348 unsigned max_size;
18349 int i;
18350 max_size = 0;
18351 for(i = 0; i < MAX_REGC; i++) {
18352 if (classes & (1 << i)) {
18353 unsigned size;
18354 size = arch_regc_size(state, i);
18355 if (size > max_size) {
18356 max_size = size;
18357 }
18358 }
18359 }
18360 return max_size;
18361}
18362
18363static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18364{
18365 unsigned equivs[MAX_REG_EQUIVS];
18366 int i;
18367 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18368 internal_error(state, 0, "invalid register");
18369 }
18370 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18371 internal_error(state, 0, "invalid register");
18372 }
18373 arch_reg_equivs(state, equivs, reg1);
18374 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18375 if (equivs[i] == reg2) {
18376 return 1;
18377 }
18378 }
18379 return 0;
18380}
18381
18382static void reg_fill_used(struct compile_state *state, char *used, int reg)
18383{
18384 unsigned equivs[MAX_REG_EQUIVS];
18385 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018386 if (reg == REG_UNNEEDED) {
18387 return;
18388 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018389 arch_reg_equivs(state, equivs, reg);
18390 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18391 used[equivs[i]] = 1;
18392 }
18393 return;
18394}
18395
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018396static void reg_inc_used(struct compile_state *state, char *used, int reg)
18397{
18398 unsigned equivs[MAX_REG_EQUIVS];
18399 int i;
18400 if (reg == REG_UNNEEDED) {
18401 return;
18402 }
18403 arch_reg_equivs(state, equivs, reg);
18404 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18405 used[equivs[i]] += 1;
18406 }
18407 return;
18408}
18409
Eric Biedermanb138ac82003-04-22 18:44:01 +000018410static unsigned int hash_live_edge(
18411 struct live_range *left, struct live_range *right)
18412{
18413 unsigned int hash, val;
18414 unsigned long lval, rval;
18415 lval = ((unsigned long)left)/sizeof(struct live_range);
18416 rval = ((unsigned long)right)/sizeof(struct live_range);
18417 hash = 0;
18418 while(lval) {
18419 val = lval & 0xff;
18420 lval >>= 8;
18421 hash = (hash *263) + val;
18422 }
18423 while(rval) {
18424 val = rval & 0xff;
18425 rval >>= 8;
18426 hash = (hash *263) + val;
18427 }
18428 hash = hash & (LRE_HASH_SIZE - 1);
18429 return hash;
18430}
18431
18432static struct lre_hash **lre_probe(struct reg_state *rstate,
18433 struct live_range *left, struct live_range *right)
18434{
18435 struct lre_hash **ptr;
18436 unsigned int index;
18437 /* Ensure left <= right */
18438 if (left > right) {
18439 struct live_range *tmp;
18440 tmp = left;
18441 left = right;
18442 right = tmp;
18443 }
18444 index = hash_live_edge(left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000018445
Eric Biedermanb138ac82003-04-22 18:44:01 +000018446 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018447 while(*ptr) {
18448 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18449 break;
18450 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018451 ptr = &(*ptr)->next;
18452 }
18453 return ptr;
18454}
18455
18456static int interfere(struct reg_state *rstate,
18457 struct live_range *left, struct live_range *right)
18458{
18459 struct lre_hash **ptr;
18460 ptr = lre_probe(rstate, left, right);
18461 return ptr && *ptr;
18462}
18463
Stefan Reinauer14e22772010-04-27 06:56:47 +000018464static void add_live_edge(struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018465 struct live_range *left, struct live_range *right)
18466{
18467 /* FIXME the memory allocation overhead is noticeable here... */
18468 struct lre_hash **ptr, *new_hash;
18469 struct live_range_edge *edge;
18470
18471 if (left == right) {
18472 return;
18473 }
18474 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18475 return;
18476 }
18477 /* Ensure left <= right */
18478 if (left > right) {
18479 struct live_range *tmp;
18480 tmp = left;
18481 left = right;
18482 right = tmp;
18483 }
18484 ptr = lre_probe(rstate, left, right);
18485 if (*ptr) {
18486 return;
18487 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018488#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018489 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018490 left, right);
18491#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018492 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18493 new_hash->next = *ptr;
18494 new_hash->left = left;
18495 new_hash->right = right;
18496 *ptr = new_hash;
18497
18498 edge = xmalloc(sizeof(*edge), "live_range_edge");
18499 edge->next = left->edges;
18500 edge->node = right;
18501 left->edges = edge;
18502 left->degree += 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018503
Eric Biedermanb138ac82003-04-22 18:44:01 +000018504 edge = xmalloc(sizeof(*edge), "live_range_edge");
18505 edge->next = right->edges;
18506 edge->node = left;
18507 right->edges = edge;
18508 right->degree += 1;
18509}
18510
18511static void remove_live_edge(struct reg_state *rstate,
18512 struct live_range *left, struct live_range *right)
18513{
18514 struct live_range_edge *edge, **ptr;
18515 struct lre_hash **hptr, *entry;
18516 hptr = lre_probe(rstate, left, right);
18517 if (!hptr || !*hptr) {
18518 return;
18519 }
18520 entry = *hptr;
18521 *hptr = entry->next;
18522 xfree(entry);
18523
18524 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18525 edge = *ptr;
18526 if (edge->node == right) {
18527 *ptr = edge->next;
18528 memset(edge, 0, sizeof(*edge));
18529 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018530 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018531 break;
18532 }
18533 }
18534 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18535 edge = *ptr;
18536 if (edge->node == left) {
18537 *ptr = edge->next;
18538 memset(edge, 0, sizeof(*edge));
18539 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018540 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018541 break;
18542 }
18543 }
18544}
18545
18546static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18547{
18548 struct live_range_edge *edge, *next;
18549 for(edge = range->edges; edge; edge = next) {
18550 next = edge->next;
18551 remove_live_edge(rstate, range, edge->node);
18552 }
18553}
18554
Stefan Reinauer14e22772010-04-27 06:56:47 +000018555static void transfer_live_edges(struct reg_state *rstate,
Eric Biederman153ea352003-06-20 14:43:20 +000018556 struct live_range *dest, struct live_range *src)
18557{
18558 struct live_range_edge *edge, *next;
18559 for(edge = src->edges; edge; edge = next) {
18560 struct live_range *other;
18561 next = edge->next;
18562 other = edge->node;
18563 remove_live_edge(rstate, src, other);
18564 add_live_edge(rstate, dest, other);
18565 }
18566}
18567
Eric Biedermanb138ac82003-04-22 18:44:01 +000018568
18569/* Interference graph...
Stefan Reinauer14e22772010-04-27 06:56:47 +000018570 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000018571 * new(n) --- Return a graph with n nodes but no edges.
18572 * add(g,x,y) --- Return a graph including g with an between x and y
18573 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18574 * x and y in the graph g
18575 * degree(g, x) --- Return the degree of the node x in the graph g
18576 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18577 *
18578 * Implement with a hash table && a set of adjcency vectors.
18579 * The hash table supports constant time implementations of add and interfere.
18580 * The adjacency vectors support an efficient implementation of neighbors.
18581 */
18582
Stefan Reinauer14e22772010-04-27 06:56:47 +000018583/*
Eric Biedermanb138ac82003-04-22 18:44:01 +000018584 * +---------------------------------------------------+
18585 * | +--------------+ |
18586 * v v | |
Stefan Reinauer14e22772010-04-27 06:56:47 +000018587 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
Eric Biedermanb138ac82003-04-22 18:44:01 +000018588 *
18589 * -- In simplify implment optimistic coloring... (No backtracking)
18590 * -- Implement Rematerialization it is the only form of spilling we can perform
18591 * Essentially this means dropping a constant from a register because
18592 * we can regenerate it later.
18593 *
18594 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18595 * coalesce at phi points...
18596 * --- Bias coloring if at all possible do the coalesing a compile time.
18597 *
18598 *
18599 */
18600
Stefan Reinauer50542a82007-10-24 11:14:14 +000018601#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018602static void different_colored(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018603 struct compile_state *state, struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018604 struct triple *parent, struct triple *ins)
18605{
18606 struct live_range *lr;
18607 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018608 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018609 expr = triple_rhs(state, ins, 0);
18610 for(;expr; expr = triple_rhs(state, ins, expr)) {
18611 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018612 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018613 continue;
18614 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018615 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018616 if (lr->color == lr2->color) {
18617 internal_error(state, ins, "live range too big");
18618 }
18619 }
18620}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018621#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018622
18623static struct live_range *coalesce_ranges(
18624 struct compile_state *state, struct reg_state *rstate,
18625 struct live_range *lr1, struct live_range *lr2)
18626{
18627 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18628 unsigned color;
18629 unsigned classes;
18630 if (lr1 == lr2) {
18631 return lr1;
18632 }
18633 if (!lr1->defs || !lr2->defs) {
18634 internal_error(state, 0,
18635 "cannot coalese dead live ranges");
18636 }
18637 if ((lr1->color == REG_UNNEEDED) ||
18638 (lr2->color == REG_UNNEEDED)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018639 internal_error(state, 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018640 "cannot coalesce live ranges without a possible color");
18641 }
18642 if ((lr1->color != lr2->color) &&
18643 (lr1->color != REG_UNSET) &&
18644 (lr2->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018645 internal_error(state, lr1->defs->def,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018646 "cannot coalesce live ranges of different colors");
18647 }
18648 color = lr1->color;
18649 if (color == REG_UNSET) {
18650 color = lr2->color;
18651 }
18652 classes = lr1->classes & lr2->classes;
18653 if (!classes) {
18654 internal_error(state, lr1->defs->def,
18655 "cannot coalesce live ranges with dissimilar register classes");
18656 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018657 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018658 FILE *fp = state->errout;
18659 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018660 lrd = lr1->defs;
18661 do {
Eric Biederman90089602004-05-28 14:11:54 +000018662 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018663 lrd = lrd->next;
18664 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018665 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018666 lrd = lr2->defs;
18667 do {
Eric Biederman90089602004-05-28 14:11:54 +000018668 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018669 lrd = lrd->next;
18670 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018671 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018672 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018673 /* If there is a clear dominate live range put it in lr1,
18674 * For purposes of this test phi functions are
18675 * considered dominated by the definitions that feed into
Stefan Reinauer14e22772010-04-27 06:56:47 +000018676 * them.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018677 */
18678 if ((lr1->defs->prev->def->op == OP_PHI) ||
18679 ((lr2->defs->prev->def->op != OP_PHI) &&
18680 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18681 struct live_range *tmp;
18682 tmp = lr1;
18683 lr1 = lr2;
18684 lr2 = tmp;
18685 }
18686#if 0
18687 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018688 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018689 }
18690 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018691 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018692 }
18693 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018694 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018695 }
18696 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018697 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018698 }
18699#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018700#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018701 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018702 lr1->defs->def,
18703 lr1->color,
18704 lr2->defs->def,
18705 lr2->color);
18706#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018707
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018708 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018709#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018710#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018711#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018712 /* This FIXME item applies to the correctness of live_range_end
Eric Biederman153ea352003-06-20 14:43:20 +000018713 * and to the necessity of making multiple passes of coalesce_live_ranges.
18714 * A failure to find some coalesce opportunities in coaleace_live_ranges
18715 * does not impact the correct of the compiler just the efficiency with
18716 * which registers are allocated.
18717 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018718 head = lr1->defs;
18719 mid1 = lr1->defs->prev;
18720 mid2 = lr2->defs;
18721 end = lr2->defs->prev;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018722
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018723 head->prev = end;
18724 end->next = head;
18725
18726 mid1->next = mid2;
18727 mid2->prev = mid1;
18728
18729 /* Fixup the live range in the added live range defs */
18730 lrd = head;
18731 do {
18732 lrd->lr = lr1;
18733 lrd = lrd->next;
18734 } while(lrd != head);
18735
18736 /* Mark lr2 as free. */
18737 lr2->defs = 0;
18738 lr2->color = REG_UNNEEDED;
18739 lr2->classes = 0;
18740
18741 if (!lr1->defs) {
18742 internal_error(state, 0, "lr1->defs == 0 ?");
18743 }
18744
18745 lr1->color = color;
18746 lr1->classes = classes;
18747
Eric Biederman153ea352003-06-20 14:43:20 +000018748 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18749 transfer_live_edges(rstate, lr1, lr2);
18750
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018751 return lr1;
18752}
18753
18754static struct live_range_def *live_range_head(
18755 struct compile_state *state, struct live_range *lr,
18756 struct live_range_def *last)
18757{
18758 struct live_range_def *result;
18759 result = 0;
18760 if (last == 0) {
18761 result = lr->defs;
18762 }
18763 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18764 result = last->next;
18765 }
18766 return result;
18767}
18768
18769static struct live_range_def *live_range_end(
18770 struct compile_state *state, struct live_range *lr,
18771 struct live_range_def *last)
18772{
18773 struct live_range_def *result;
18774 result = 0;
18775 if (last == 0) {
18776 result = lr->defs->prev;
18777 }
18778 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18779 result = last->prev;
18780 }
18781 return result;
18782}
18783
18784
Eric Biedermanb138ac82003-04-22 18:44:01 +000018785static void initialize_live_ranges(
18786 struct compile_state *state, struct reg_state *rstate)
18787{
18788 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018789 size_t count, size;
18790 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018791
Eric Biederman83b991a2003-10-11 06:20:25 +000018792 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018793 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018794 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018795 count = count_triples(state);
18796 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018797 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018798 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018799 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018800 /* Potentially I need one live range for each instruction
18801 * plus an extra for the dummy live range.
18802 */
18803 rstate->ranges = count + 1;
18804 size = sizeof(rstate->lrd[0]) * rstate->defs;
18805 rstate->lrd = xcmalloc(size, "live_range_def");
18806 size = sizeof(rstate->lr[0]) * rstate->ranges;
18807 rstate->lr = xcmalloc(size, "live_range");
18808
Eric Biedermanb138ac82003-04-22 18:44:01 +000018809 /* Setup the dummy live range */
18810 rstate->lr[0].classes = 0;
18811 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018812 rstate->lr[0].defs = 0;
18813 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018814 ins = first;
18815 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018816 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018817 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018818 struct reg_info info;
18819 /* Find the architecture specific color information */
18820 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018821 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018822 rstate->lr[i].defs = &rstate->lrd[j];
18823 rstate->lr[i].color = info.reg;
18824 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018825 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018826 rstate->lrd[j].lr = &rstate->lr[i];
Stefan Reinauer14e22772010-04-27 06:56:47 +000018827 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018828 /* Otherwise give the triple the dummy live range. */
18829 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018830 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018831 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018832
18833 /* Initalize the live_range_def */
18834 rstate->lrd[j].next = &rstate->lrd[j];
18835 rstate->lrd[j].prev = &rstate->lrd[j];
18836 rstate->lrd[j].def = ins;
18837 rstate->lrd[j].orig_id = ins->id;
18838 ins->id = j;
18839
18840 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018841 ins = ins->next;
18842 } while(ins != first);
18843 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018844
Eric Biedermanb138ac82003-04-22 18:44:01 +000018845 /* Make a second pass to handle achitecture specific register
18846 * constraints.
18847 */
18848 ins = first;
18849 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018850 int zlhs, zrhs, i, j;
18851 if (ins->id > rstate->defs) {
18852 internal_error(state, ins, "bad id");
18853 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018854
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018855 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018856 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018857 if ((zlhs == 0) && triple_is_def(state, ins)) {
18858 zlhs = 1;
18859 }
Eric Biederman90089602004-05-28 14:11:54 +000018860 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018861
Eric Biederman5ade04a2003-10-22 04:03:46 +000018862 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018863 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018864 ins, zlhs, zrhs);
18865 }
18866
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018867 for(i = 0; i < zlhs; i++) {
18868 struct reg_info linfo;
18869 struct live_range_def *lhs;
18870 linfo = arch_reg_lhs(state, ins, i);
18871 if (linfo.reg < MAX_REGISTERS) {
18872 continue;
18873 }
18874 if (triple_is_def(state, ins)) {
18875 lhs = &rstate->lrd[ins->id];
18876 } else {
18877 lhs = &rstate->lrd[LHS(ins, i)->id];
18878 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018879
18880 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018881 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018882 i, lhs, linfo.reg);
18883 }
18884
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018885 for(j = 0; j < zrhs; j++) {
18886 struct reg_info rinfo;
18887 struct live_range_def *rhs;
18888 rinfo = arch_reg_rhs(state, ins, j);
18889 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018890 continue;
18891 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018892 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018893
18894 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018895 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018896 j, rhs, rinfo.reg);
18897 }
18898
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018899 if (rinfo.reg == linfo.reg) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018900 coalesce_ranges(state, rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018901 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018902 }
18903 }
18904 }
18905 ins = ins->next;
18906 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018907}
18908
Eric Biedermanf96a8102003-06-16 16:57:34 +000018909static void graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018910 struct compile_state *state,
18911 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018912 struct reg_block *rb, struct triple *ins, void *arg)
18913{
18914 struct reg_state *rstate = arg;
18915 struct live_range *def;
18916 struct triple_reg_set *entry;
18917
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018918 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018919 * we do not have a definition to add to
18920 * the interference graph.
18921 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018922 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018923 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018924 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018925 def = rstate->lrd[ins->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018926
Eric Biedermanb138ac82003-04-22 18:44:01 +000018927 /* Create an edge between ins and everything that is
18928 * alive, unless the live_range cannot share
18929 * a physical register with ins.
18930 */
18931 for(entry = live; entry; entry = entry->next) {
18932 struct live_range *lr;
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110018933 if (entry->member->id > rstate->defs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018934 internal_error(state, 0, "bad entry?");
18935 }
18936 lr = rstate->lrd[entry->member->id].lr;
18937 if (def == lr) {
18938 continue;
18939 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018940 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18941 continue;
18942 }
18943 add_live_edge(rstate, def, lr);
18944 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018945 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018946}
18947
Stefan Reinauer50542a82007-10-24 11:14:14 +000018948#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018949static struct live_range *get_verify_live_range(
18950 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18951{
18952 struct live_range *lr;
18953 struct live_range_def *lrd;
18954 int ins_found;
18955 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18956 internal_error(state, ins, "bad ins?");
18957 }
18958 lr = rstate->lrd[ins->id].lr;
18959 ins_found = 0;
18960 lrd = lr->defs;
18961 do {
18962 if (lrd->def == ins) {
18963 ins_found = 1;
18964 }
18965 lrd = lrd->next;
18966 } while(lrd != lr->defs);
18967 if (!ins_found) {
18968 internal_error(state, ins, "ins not in live range");
18969 }
18970 return lr;
18971}
18972
18973static void verify_graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018974 struct compile_state *state,
18975 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018976 struct reg_block *rb, struct triple *ins, void *arg)
18977{
18978 struct reg_state *rstate = arg;
18979 struct triple_reg_set *entry1, *entry2;
18980
18981
18982 /* Compare live against edges and make certain the code is working */
18983 for(entry1 = live; entry1; entry1 = entry1->next) {
18984 struct live_range *lr1;
18985 lr1 = get_verify_live_range(state, rstate, entry1->member);
18986 for(entry2 = live; entry2; entry2 = entry2->next) {
18987 struct live_range *lr2;
18988 struct live_range_edge *edge2;
18989 int lr1_found;
18990 int lr2_degree;
18991 if (entry2 == entry1) {
18992 continue;
18993 }
18994 lr2 = get_verify_live_range(state, rstate, entry2->member);
18995 if (lr1 == lr2) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018996 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018997 "live range with 2 values simultaneously alive");
18998 }
18999 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19000 continue;
19001 }
19002 if (!interfere(rstate, lr1, lr2)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019003 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019004 "edges don't interfere?");
19005 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019006
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019007 lr1_found = 0;
19008 lr2_degree = 0;
19009 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19010 lr2_degree++;
19011 if (edge2->node == lr1) {
19012 lr1_found = 1;
19013 }
19014 }
19015 if (lr2_degree != lr2->degree) {
19016 internal_error(state, entry2->member,
19017 "computed degree: %d does not match reported degree: %d\n",
19018 lr2_degree, lr2->degree);
19019 }
19020 if (!lr1_found) {
19021 internal_error(state, entry2->member, "missing edge");
19022 }
19023 }
19024 }
19025 return;
19026}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019027#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019028
Eric Biedermanf96a8102003-06-16 16:57:34 +000019029static void print_interference_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019030 struct compile_state *state,
19031 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019032 struct reg_block *rb, struct triple *ins, void *arg)
19033{
19034 struct reg_state *rstate = arg;
19035 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019036 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019037 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019038
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019039 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019040 id = ins->id;
19041 ins->id = rstate->lrd[id].orig_id;
19042 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019043 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019044 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019045
19046 if (lr->defs) {
19047 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019048 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019049 lrd = lr->defs;
19050 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019051 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019052 lrd = lrd->next;
19053 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019054 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019055 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019056 if (live) {
19057 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019058 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019059 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019060 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019061 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019062 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019063 }
19064 if (lr->edges) {
19065 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019066 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019067 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019068 struct live_range_def *lrd;
19069 lrd = entry->node->defs;
19070 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019071 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019072 lrd = lrd->next;
19073 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019074 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019075 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019076 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019077 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019078 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019079 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019080 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019081 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019082}
19083
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019084static int coalesce_live_ranges(
19085 struct compile_state *state, struct reg_state *rstate)
19086{
19087 /* At the point where a value is moved from one
19088 * register to another that value requires two
19089 * registers, thus increasing register pressure.
19090 * Live range coaleescing reduces the register
19091 * pressure by keeping a value in one register
19092 * longer.
19093 *
19094 * In the case of a phi function all paths leading
19095 * into it must be allocated to the same register
19096 * otherwise the phi function may not be removed.
19097 *
19098 * Forcing a value to stay in a single register
19099 * for an extended period of time does have
19100 * limitations when applied to non homogenous
Stefan Reinauer14e22772010-04-27 06:56:47 +000019101 * register pool.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019102 *
19103 * The two cases I have identified are:
19104 * 1) Two forced register assignments may
19105 * collide.
19106 * 2) Registers may go unused because they
19107 * are only good for storing the value
19108 * and not manipulating it.
19109 *
19110 * Because of this I need to split live ranges,
19111 * even outside of the context of coalesced live
19112 * ranges. The need to split live ranges does
19113 * impose some constraints on live range coalescing.
19114 *
19115 * - Live ranges may not be coalesced across phi
19116 * functions. This creates a 2 headed live
19117 * range that cannot be sanely split.
19118 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000019119 * - phi functions (coalesced in initialize_live_ranges)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019120 * are handled as pre split live ranges so we will
19121 * never attempt to split them.
19122 */
19123 int coalesced;
19124 int i;
19125
19126 coalesced = 0;
19127 for(i = 0; i <= rstate->ranges; i++) {
19128 struct live_range *lr1;
19129 struct live_range_def *lrd1;
19130 lr1 = &rstate->lr[i];
19131 if (!lr1->defs) {
19132 continue;
19133 }
19134 lrd1 = live_range_end(state, lr1, 0);
19135 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19136 struct triple_set *set;
19137 if (lrd1->def->op != OP_COPY) {
19138 continue;
19139 }
19140 /* Skip copies that are the result of a live range split. */
19141 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19142 continue;
19143 }
19144 for(set = lrd1->def->use; set; set = set->next) {
19145 struct live_range_def *lrd2;
19146 struct live_range *lr2, *res;
19147
19148 lrd2 = &rstate->lrd[set->member->id];
19149
19150 /* Don't coalesce with instructions
19151 * that are the result of a live range
19152 * split.
19153 */
19154 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19155 continue;
19156 }
19157 lr2 = rstate->lrd[set->member->id].lr;
19158 if (lr1 == lr2) {
19159 continue;
19160 }
19161 if ((lr1->color != lr2->color) &&
19162 (lr1->color != REG_UNSET) &&
19163 (lr2->color != REG_UNSET)) {
19164 continue;
19165 }
19166 if ((lr1->classes & lr2->classes) == 0) {
19167 continue;
19168 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019169
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019170 if (interfere(rstate, lr1, lr2)) {
19171 continue;
19172 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019173
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019174 res = coalesce_ranges(state, rstate, lr1, lr2);
19175 coalesced += 1;
19176 if (res != lr1) {
19177 goto next;
19178 }
19179 }
19180 }
19181 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019182 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019183 }
19184 return coalesced;
19185}
19186
19187
Eric Biedermanf96a8102003-06-16 16:57:34 +000019188static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019189 struct reg_block *blocks, struct triple_reg_set *live,
19190 struct reg_block *rb, struct triple *ins, void *arg)
19191{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019192 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019193 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019194
19195 /* See if we have a mandatory coalesce operation between
19196 * a lhs and a rhs value. If so and the rhs value is also
19197 * alive then this triple needs to be pre copied. Otherwise
19198 * we would have two definitions in the same live range simultaneously
19199 * alive.
19200 */
Eric Biederman90089602004-05-28 14:11:54 +000019201 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019202 if ((zlhs == 0) && triple_is_def(state, ins)) {
19203 zlhs = 1;
19204 }
Eric Biederman90089602004-05-28 14:11:54 +000019205 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019206 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019207 struct reg_info linfo;
19208 linfo = arch_reg_lhs(state, ins, i);
19209 if (linfo.reg < MAX_REGISTERS) {
19210 continue;
19211 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019212 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019213 struct reg_info rinfo;
19214 struct triple *rhs;
19215 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019216 int found;
19217 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019218 rinfo = arch_reg_rhs(state, ins, j);
19219 if (rinfo.reg != linfo.reg) {
19220 continue;
19221 }
19222 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019223 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019224 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019225 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019226 }
19227 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019228 if (found) {
19229 struct triple *copy;
19230 copy = pre_copy(state, ins, j);
19231 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019232 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019233 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019234 }
19235 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019236 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019237}
19238
Eric Biedermand1ea5392003-06-28 06:49:45 +000019239static int correct_coalesce_conflicts(
19240 struct compile_state *state, struct reg_block *blocks)
19241{
19242 int conflicts;
19243 conflicts = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019244 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019245 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019246 return conflicts;
19247}
19248
Eric Biedermanf96a8102003-06-16 16:57:34 +000019249static void replace_set_use(struct compile_state *state,
19250 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019251{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019252 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019253 for(set = head; set; set = set->next) {
19254 if (set->member == orig) {
19255 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019256 }
19257 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019258}
19259
Stefan Reinauer14e22772010-04-27 06:56:47 +000019260static void replace_block_use(struct compile_state *state,
Eric Biedermanf96a8102003-06-16 16:57:34 +000019261 struct reg_block *blocks, struct triple *orig, struct triple *new)
19262{
19263 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019264#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019265#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019266#endif
Eric Biederman90089602004-05-28 14:11:54 +000019267 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019268 struct reg_block *rb;
19269 rb = &blocks[i];
19270 replace_set_use(state, rb->in, orig, new);
19271 replace_set_use(state, rb->out, orig, new);
19272 }
19273}
19274
19275static void color_instructions(struct compile_state *state)
19276{
19277 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019278 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019279 ins = first;
19280 do {
19281 if (triple_is_def(state, ins)) {
19282 struct reg_info info;
19283 info = find_lhs_color(state, ins, 0);
19284 if (info.reg >= MAX_REGISTERS) {
19285 info.reg = REG_UNSET;
19286 }
19287 SET_INFO(ins->id, info);
19288 }
19289 ins = ins->next;
19290 } while(ins != first);
19291}
19292
19293static struct reg_info read_lhs_color(
19294 struct compile_state *state, struct triple *ins, int index)
19295{
19296 struct reg_info info;
19297 if ((index == 0) && triple_is_def(state, ins)) {
19298 info.reg = ID_REG(ins->id);
19299 info.regcm = ID_REGCM(ins->id);
19300 }
Eric Biederman90089602004-05-28 14:11:54 +000019301 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019302 info = read_lhs_color(state, LHS(ins, index), 0);
19303 }
19304 else {
19305 internal_error(state, ins, "Bad lhs %d", index);
19306 info.reg = REG_UNSET;
19307 info.regcm = 0;
19308 }
19309 return info;
19310}
19311
19312static struct triple *resolve_tangle(
19313 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019314{
19315 struct reg_info info, uinfo;
19316 struct triple_set *set, *next;
19317 struct triple *copy;
19318
Stefan Reinauer50542a82007-10-24 11:14:14 +000019319#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019320#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019321#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019322 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019323 for(set = tangle->use; set; set = next) {
19324 struct triple *user;
19325 int i, zrhs;
19326 next = set->next;
19327 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019328 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019329 for(i = 0; i < zrhs; i++) {
19330 if (RHS(user, i) != tangle) {
19331 continue;
19332 }
19333 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019334 if (uinfo.reg == info.reg) {
19335 copy = pre_copy(state, user, i);
19336 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019337 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019338 }
19339 }
19340 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019341 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019342 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019343 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019344 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019345 copy = post_copy(state, tangle);
19346 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019347 linfo = find_lhs_color(state, copy, 0);
19348 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019349 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019350 info = find_lhs_color(state, tangle, 0);
19351 SET_INFO(tangle->id, info);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019352
Eric Biedermanf96a8102003-06-16 16:57:34 +000019353 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019354}
19355
19356
Eric Biedermanf96a8102003-06-16 16:57:34 +000019357static void fix_tangles(struct compile_state *state,
19358 struct reg_block *blocks, struct triple_reg_set *live,
19359 struct reg_block *rb, struct triple *ins, void *arg)
19360{
Eric Biederman153ea352003-06-20 14:43:20 +000019361 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019362 struct triple *tangle;
19363 do {
19364 char used[MAX_REGISTERS];
19365 struct triple_reg_set *set;
19366 tangle = 0;
19367
19368 /* Find out which registers have multiple uses at this point */
19369 memset(used, 0, sizeof(used));
19370 for(set = live; set; set = set->next) {
19371 struct reg_info info;
19372 info = read_lhs_color(state, set->member, 0);
19373 if (info.reg == REG_UNSET) {
19374 continue;
19375 }
19376 reg_inc_used(state, used, info.reg);
19377 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019378
Eric Biedermanf96a8102003-06-16 16:57:34 +000019379 /* Now find the least dominated definition of a register in
19380 * conflict I have seen so far.
19381 */
19382 for(set = live; set; set = set->next) {
19383 struct reg_info info;
19384 info = read_lhs_color(state, set->member, 0);
19385 if (used[info.reg] < 2) {
19386 continue;
19387 }
Eric Biederman153ea352003-06-20 14:43:20 +000019388 /* Changing copies that feed into phi functions
19389 * is incorrect.
19390 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000019391 if (set->member->use &&
Eric Biederman153ea352003-06-20 14:43:20 +000019392 (set->member->use->member->op == OP_PHI)) {
19393 continue;
19394 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019395 if (!tangle || tdominates(state, set->member, tangle)) {
19396 tangle = set->member;
19397 }
19398 }
19399 /* If I have found a tangle resolve it */
19400 if (tangle) {
19401 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019402 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019403 post_copy = resolve_tangle(state, tangle);
19404 if (post_copy) {
19405 replace_block_use(state, blocks, tangle, post_copy);
19406 }
19407 if (post_copy && (tangle != ins)) {
19408 replace_set_use(state, live, tangle, post_copy);
19409 }
19410 }
19411 } while(tangle);
19412 return;
19413}
19414
Eric Biederman153ea352003-06-20 14:43:20 +000019415static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019416 struct compile_state *state, struct reg_block *blocks)
19417{
Eric Biederman153ea352003-06-20 14:43:20 +000019418 int tangles;
19419 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019420 color_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019421 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019422 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019423 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019424}
19425
Eric Biedermand1ea5392003-06-28 06:49:45 +000019426
19427static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19428static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19429
19430struct triple *find_constrained_def(
19431 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019432{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019433 struct live_range_def *lrd, *lrd_next;
19434 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019435 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019436 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019437 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019438
19439 lrd = lrd_next;
19440 lrd_next = lrd->next;
19441
Eric Biedermand1ea5392003-06-28 06:49:45 +000019442 regcm = arch_type_to_regcm(state, lrd->def->type);
19443 info = find_lhs_color(state, lrd->def, 0);
19444 regcm = arch_regcm_reg_normalize(state, regcm);
19445 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019446 /* If the 2 register class masks are equal then
19447 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019448 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019449 if (regcm == info.regcm) {
19450 continue;
19451 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019452
Eric Biederman5ade04a2003-10-22 04:03:46 +000019453 /* If there is just one use.
19454 * That use cannot accept a larger register class.
19455 * There are no intervening definitions except
19456 * definitions that feed into that use.
19457 * Then a triple is not constrained.
19458 * FIXME handle this case!
19459 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019460#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019461#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019462#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019463
Eric Biederman5ade04a2003-10-22 04:03:46 +000019464
Eric Biedermand1ea5392003-06-28 06:49:45 +000019465 /* Of the constrained live ranges deal with the
19466 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019467 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019468 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019469 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019470 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019471 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019472 if (!constrained ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000019473 tdominates(state, lrd->def, constrained))
19474 {
19475 constrained = lrd->def;
19476 }
19477 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019478 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019479}
19480
Eric Biedermand1ea5392003-06-28 06:49:45 +000019481static int split_constrained_ranges(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019482 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019483 struct live_range *range)
19484{
19485 /* Walk through the edges in conflict and our current live
19486 * range, and find definitions that are more severly constrained
19487 * than they type of data they contain require.
Stefan Reinauer14e22772010-04-27 06:56:47 +000019488 *
Eric Biedermand1ea5392003-06-28 06:49:45 +000019489 * Then pick one of those ranges and relax the constraints.
19490 */
19491 struct live_range_edge *edge;
19492 struct triple *constrained;
19493
19494 constrained = 0;
19495 for(edge = range->edges; edge; edge = edge->next) {
19496 constrained = find_constrained_def(state, edge->node, constrained);
19497 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019498#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019499#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019500#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019501 if (!constrained) {
19502 constrained = find_constrained_def(state, range, constrained);
19503 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019504
19505 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019506 fprintf(state->errout, "constrained: ");
19507 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019508 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019509 if (constrained) {
19510 ids_from_rstate(state, rstate);
19511 cleanup_rstate(state, rstate);
19512 resolve_tangle(state, constrained);
19513 }
19514 return !!constrained;
19515}
Stefan Reinauer14e22772010-04-27 06:56:47 +000019516
Eric Biedermand1ea5392003-06-28 06:49:45 +000019517static int split_ranges(
19518 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019519 char *used, struct live_range *range)
19520{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019521 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019522 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019523 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019524 rstate->passes, tops(range->defs->def->op), range->defs->def);
19525 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019526 if ((range->color == REG_UNNEEDED) ||
19527 (rstate->passes >= rstate->max_passes)) {
19528 return 0;
19529 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019530 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019531
Eric Biedermand1ea5392003-06-28 06:49:45 +000019532 /* Ideally I would split the live range that will not be used
Stefan Reinauer14e22772010-04-27 06:56:47 +000019533 * for the longest period of time in hopes that this will
Eric Biedermand1ea5392003-06-28 06:49:45 +000019534 * (a) allow me to spill a register or
19535 * (b) allow me to place a value in another register.
19536 *
19537 * So far I don't have a test case for this, the resolving
19538 * of mandatory constraints has solved all of my
19539 * know issues. So I have choosen not to write any
19540 * code until I cat get a better feel for cases where
19541 * it would be useful to have.
19542 *
19543 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019544#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019545#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019546#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019547
Eric Biederman5ade04a2003-10-22 04:03:46 +000019548 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019549 FILE *fp = state->errout;
19550 print_interference_blocks(state, rstate, fp, 0);
19551 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019552 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019553 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019554}
19555
Eric Biederman5ade04a2003-10-22 04:03:46 +000019556static FILE *cgdebug_fp(struct compile_state *state)
19557{
19558 FILE *fp;
19559 fp = 0;
19560 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019561 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019562 }
19563 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019564 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019565 }
19566 return fp;
19567}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019568
Eric Biederman5ade04a2003-10-22 04:03:46 +000019569static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19570{
19571 FILE *fp;
19572 fp = cgdebug_fp(state);
19573 if (fp) {
19574 va_list args;
19575 va_start(args, fmt);
19576 vfprintf(fp, fmt, args);
19577 va_end(args);
19578 }
19579}
19580
19581static void cgdebug_flush(struct compile_state *state)
19582{
19583 FILE *fp;
19584 fp = cgdebug_fp(state);
19585 if (fp) {
19586 fflush(fp);
19587 }
19588}
19589
19590static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19591{
19592 FILE *fp;
19593 fp = cgdebug_fp(state);
19594 if (fp) {
19595 loc(fp, state, ins);
19596 }
19597}
19598
Stefan Reinauer14e22772010-04-27 06:56:47 +000019599static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019600 struct reg_state *rstate, struct live_range *range)
19601{
19602 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019603 struct live_range_def *lrd;
19604 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019605 struct live_range_edge *edge;
19606 char used[MAX_REGISTERS];
19607 struct triple **expr;
19608
Eric Biedermanb138ac82003-04-22 18:44:01 +000019609 /* Instead of doing just the trivial color select here I try
19610 * a few extra things because a good color selection will help reduce
19611 * copies.
19612 */
19613
19614 /* Find the registers currently in use */
19615 memset(used, 0, sizeof(used));
19616 for(edge = range->edges; edge; edge = edge->next) {
19617 if (edge->node->color == REG_UNSET) {
19618 continue;
19619 }
19620 reg_fill_used(state, used, edge->node->color);
19621 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019622
19623 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019624 int i;
19625 i = 0;
19626 for(edge = range->edges; edge; edge = edge->next) {
19627 i++;
19628 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019629 cgdebug_printf(state, "\n%s edges: %d",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019630 tops(range->defs->def->op), i);
19631 cgdebug_loc(state, range->defs->def);
19632 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019633 for(i = 0; i < MAX_REGISTERS; i++) {
19634 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019635 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019636 arch_reg_str(i));
19637 }
19638 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019639 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019640
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019641 /* If a color is already assigned see if it will work */
19642 if (range->color != REG_UNSET) {
19643 struct live_range_def *lrd;
19644 if (!used[range->color]) {
19645 return 1;
19646 }
19647 for(edge = range->edges; edge; edge = edge->next) {
19648 if (edge->node->color != range->color) {
19649 continue;
19650 }
19651 warning(state, edge->node->defs->def, "edge: ");
19652 lrd = edge->node->defs;
19653 do {
19654 warning(state, lrd->def, " %p %s",
19655 lrd->def, tops(lrd->def->op));
19656 lrd = lrd->next;
19657 } while(lrd != edge->node->defs);
19658 }
19659 lrd = range->defs;
19660 warning(state, range->defs->def, "def: ");
19661 do {
19662 warning(state, lrd->def, " %p %s",
19663 lrd->def, tops(lrd->def->op));
19664 lrd = lrd->next;
19665 } while(lrd != range->defs);
19666 internal_error(state, range->defs->def,
19667 "live range with already used color %s",
19668 arch_reg_str(range->color));
19669 }
19670
Eric Biedermanb138ac82003-04-22 18:44:01 +000019671 /* If I feed into an expression reuse it's color.
19672 * This should help remove copies in the case of 2 register instructions
19673 * and phi functions.
19674 */
19675 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019676 lrd = live_range_end(state, range, 0);
19677 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19678 entry = lrd->def->use;
19679 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19680 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019681 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019682 insd = &rstate->lrd[entry->member->id];
19683 if (insd->lr->defs == 0) {
19684 continue;
19685 }
19686 if (!phi && (insd->def->op == OP_PHI) &&
19687 !interfere(rstate, range, insd->lr)) {
19688 phi = insd;
19689 }
Eric Biederman530b5192003-07-01 10:05:30 +000019690 if (insd->lr->color == REG_UNSET) {
19691 continue;
19692 }
19693 regcm = insd->lr->classes;
19694 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019695 (used[insd->lr->color])) {
19696 continue;
19697 }
19698 if (interfere(rstate, range, insd->lr)) {
19699 continue;
19700 }
19701 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019702 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019703 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019704 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019705 * of something else that feeds into the phi function.
19706 */
19707 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019708 if (phi->lr->color != REG_UNSET) {
19709 if (used[phi->lr->color]) {
19710 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019711 }
19712 }
19713 else {
19714 expr = triple_rhs(state, phi->def, 0);
19715 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19716 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019717 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019718 if (!*expr) {
19719 continue;
19720 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019721 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019722 if (lr->color == REG_UNSET) {
19723 continue;
19724 }
19725 regcm = lr->classes;
19726 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019727 (used[lr->color])) {
19728 continue;
19729 }
19730 if (interfere(rstate, range, lr)) {
19731 continue;
19732 }
19733 range->color = lr->color;
19734 }
19735 }
19736 }
19737 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019738 lrd = live_range_head(state, range, 0);
19739 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19740 expr = triple_rhs(state, lrd->def, 0);
19741 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019742 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019743 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019744 if (!*expr) {
19745 continue;
19746 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019747 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019748 if (lr->color == REG_UNSET) {
19749 continue;
19750 }
19751 regcm = lr->classes;
19752 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019753 (used[lr->color])) {
19754 continue;
19755 }
19756 if (interfere(rstate, range, lr)) {
19757 continue;
19758 }
19759 range->color = lr->color;
19760 break;
19761 }
19762 }
19763 /* If I have not opportunitically picked a useful color
19764 * pick the first color that is free.
19765 */
19766 if (range->color == REG_UNSET) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019767 range->color =
Eric Biedermanb138ac82003-04-22 18:44:01 +000019768 arch_select_free_register(state, used, range->classes);
19769 }
19770 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019771 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019772 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019773 if (split_ranges(state, rstate, used, range)) {
19774 return 0;
19775 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019776 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019777 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019778 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019779 lrd = edge->node->defs;
19780 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019781 warning(state, lrd->def, " %s %p",
19782 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019783 lrd = lrd->next;
19784 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019785 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019786 warning(state, range->defs->def, "range: ");
19787 lrd = range->defs;
19788 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019789 warning(state, lrd->def, " %s %p",
19790 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019791 lrd = lrd->next;
19792 } while(lrd != range->defs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019793
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019794 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019795 range->classes);
19796 for(i = 0; i < MAX_REGISTERS; i++) {
19797 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019798 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019799 arch_reg_str(i));
19800 }
19801 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019802 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019803 }
Eric Biederman530b5192003-07-01 10:05:30 +000019804 range->classes &= arch_reg_regcm(state, range->color);
19805 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019806 internal_error(state, range->defs->def, "select_free_color did not?");
19807 }
19808 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019809}
19810
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019811static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019812{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019813 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019814 struct live_range_edge *edge;
19815 struct live_range *range;
19816 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019817 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019818 range = rstate->low;
19819 if (*range->group_prev != range) {
19820 internal_error(state, 0, "lo: *prev != range?");
19821 }
19822 *range->group_prev = range->group_next;
19823 if (range->group_next) {
19824 range->group_next->group_prev = range->group_prev;
19825 }
19826 if (&range->group_next == rstate->low_tail) {
19827 rstate->low_tail = range->group_prev;
19828 }
19829 if (rstate->low == range) {
19830 internal_error(state, 0, "low: next != prev?");
19831 }
19832 }
19833 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019834 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019835 range = rstate->high;
19836 if (*range->group_prev != range) {
19837 internal_error(state, 0, "hi: *prev != range?");
19838 }
19839 *range->group_prev = range->group_next;
19840 if (range->group_next) {
19841 range->group_next->group_prev = range->group_prev;
19842 }
19843 if (&range->group_next == rstate->high_tail) {
19844 rstate->high_tail = range->group_prev;
19845 }
19846 if (rstate->high == range) {
19847 internal_error(state, 0, "high: next != prev?");
19848 }
19849 }
19850 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019851 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019852 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019853 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019854 range->group_prev = 0;
19855 for(edge = range->edges; edge; edge = edge->next) {
19856 struct live_range *node;
19857 node = edge->node;
19858 /* Move nodes from the high to the low list */
19859 if (node->group_prev && (node->color == REG_UNSET) &&
19860 (node->degree == regc_max_size(state, node->classes))) {
19861 if (*node->group_prev != node) {
19862 internal_error(state, 0, "move: *prev != node?");
19863 }
19864 *node->group_prev = node->group_next;
19865 if (node->group_next) {
19866 node->group_next->group_prev = node->group_prev;
19867 }
19868 if (&node->group_next == rstate->high_tail) {
19869 rstate->high_tail = node->group_prev;
19870 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019871 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019872 node->group_prev = rstate->low_tail;
19873 node->group_next = 0;
19874 *rstate->low_tail = node;
19875 rstate->low_tail = &node->group_next;
19876 if (*node->group_prev != node) {
19877 internal_error(state, 0, "move2: *prev != node?");
19878 }
19879 }
19880 node->degree -= 1;
19881 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019882 colored = color_graph(state, rstate);
19883 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019884 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019885 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019886 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019887 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019888 if (colored) {
19889 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19890 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019891 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019892 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019893}
19894
Eric Biedermana96d6a92003-05-13 20:45:19 +000019895static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19896{
19897 struct live_range *lr;
19898 struct live_range_edge *edge;
19899 struct triple *ins, *first;
19900 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019901 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019902 ins = first;
19903 do {
19904 if (triple_is_def(state, ins)) {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019905 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019906 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019907 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019908 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019909 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019910 if (lr->color == REG_UNSET) {
19911 internal_error(state, ins,
19912 "triple without a color");
19913 }
19914 /* Find the registers used by the edges */
19915 memset(used, 0, sizeof(used));
19916 for(edge = lr->edges; edge; edge = edge->next) {
19917 if (edge->node->color == REG_UNSET) {
19918 internal_error(state, 0,
19919 "live range without a color");
19920 }
19921 reg_fill_used(state, used, edge->node->color);
19922 }
19923 if (used[lr->color]) {
19924 internal_error(state, ins,
19925 "triple with already used color");
19926 }
19927 }
19928 ins = ins->next;
19929 } while(ins != first);
19930}
19931
Eric Biedermanb138ac82003-04-22 18:44:01 +000019932static void color_triples(struct compile_state *state, struct reg_state *rstate)
19933{
Eric Biederman90089602004-05-28 14:11:54 +000019934 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019935 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019936 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019937 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019938 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019939 do {
Edward O'Callaghan2cf97152014-02-20 20:06:42 +110019940 if (ins->id > rstate->defs) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019941 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019942 "triple without a live range");
19943 }
Eric Biederman90089602004-05-28 14:11:54 +000019944 lrd = &rstate->lrd[ins->id];
19945 lr = lrd->lr;
19946 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019947 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019948 ins = ins->next;
19949 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019950}
19951
Eric Biedermanb138ac82003-04-22 18:44:01 +000019952static struct live_range *merge_sort_lr(
19953 struct live_range *first, struct live_range *last)
19954{
19955 struct live_range *mid, *join, **join_tail, *pick;
19956 size_t size;
19957 size = (last - first) + 1;
19958 if (size >= 2) {
19959 mid = first + size/2;
19960 first = merge_sort_lr(first, mid -1);
19961 mid = merge_sort_lr(mid, last);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019962
Eric Biedermanb138ac82003-04-22 18:44:01 +000019963 join = 0;
19964 join_tail = &join;
19965 /* merge the two lists */
19966 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019967 if ((first->degree < mid->degree) ||
19968 ((first->degree == mid->degree) &&
19969 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019970 pick = first;
19971 first = first->group_next;
19972 if (first) {
19973 first->group_prev = 0;
19974 }
19975 }
19976 else {
19977 pick = mid;
19978 mid = mid->group_next;
19979 if (mid) {
19980 mid->group_prev = 0;
19981 }
19982 }
19983 pick->group_next = 0;
19984 pick->group_prev = join_tail;
19985 *join_tail = pick;
19986 join_tail = &pick->group_next;
19987 }
19988 /* Splice the remaining list */
19989 pick = (first)? first : mid;
19990 *join_tail = pick;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019991 if (pick) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019992 pick->group_prev = join_tail;
19993 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019994 }
19995 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019996 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019997 first = 0;
19998 }
19999 join = first;
20000 }
20001 return join;
20002}
20003
Stefan Reinauer14e22772010-04-27 06:56:47 +000020004static void ids_from_rstate(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020005 struct reg_state *rstate)
20006{
20007 struct triple *ins, *first;
20008 if (!rstate->defs) {
20009 return;
20010 }
20011 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020012 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020013 FILE *fp = state->dbgout;
20014 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020015 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020016 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020017 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020018 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020019 ins = first;
20020 do {
20021 if (ins->id) {
20022 struct live_range_def *lrd;
20023 lrd = &rstate->lrd[ins->id];
20024 ins->id = lrd->orig_id;
20025 }
20026 ins = ins->next;
20027 } while(ins != first);
20028}
20029
20030static void cleanup_live_edges(struct reg_state *rstate)
20031{
20032 int i;
20033 /* Free the edges on each node */
20034 for(i = 1; i <= rstate->ranges; i++) {
20035 remove_live_edges(rstate, &rstate->lr[i]);
20036 }
20037}
20038
20039static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20040{
20041 cleanup_live_edges(rstate);
20042 xfree(rstate->lrd);
20043 xfree(rstate->lr);
20044
20045 /* Free the variable lifetime information */
20046 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020047 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020048 }
20049 rstate->defs = 0;
20050 rstate->ranges = 0;
20051 rstate->lrd = 0;
20052 rstate->lr = 0;
20053 rstate->blocks = 0;
20054}
20055
Eric Biederman153ea352003-06-20 14:43:20 +000020056static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020057static void allocate_registers(struct compile_state *state)
20058{
20059 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020060 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020061
20062 /* Clear out the reg_state */
20063 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020064 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020065
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020066 do {
20067 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000020068 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020069 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020070
Eric Biederman5ade04a2003-10-22 04:03:46 +000020071 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020072 FILE *fp = state->errout;
20073 fprintf(fp, "pass: %d\n", rstate.passes);
20074 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020075 }
Eric Biederman153ea352003-06-20 14:43:20 +000020076
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020077 /* Restore ids */
20078 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020079
Eric Biedermanf96a8102003-06-16 16:57:34 +000020080 /* Cleanup the temporary data structures */
20081 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020082
Eric Biedermanf96a8102003-06-16 16:57:34 +000020083 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020084 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020085
Eric Biedermanf96a8102003-06-16 16:57:34 +000020086 /* Fix invalid mandatory live range coalesce conflicts */
Bernhard Urbanf31abe32012-02-01 16:30:30 +010020087 correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020088
Eric Biederman153ea352003-06-20 14:43:20 +000020089 /* Fix two simultaneous uses of the same register.
20090 * In a few pathlogical cases a partial untangle moves
20091 * the tangle to a part of the graph we won't revisit.
20092 * So we keep looping until we have no more tangle fixes
20093 * to apply.
20094 */
20095 do {
20096 tangles = correct_tangles(state, rstate.blocks);
20097 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020098
Stefan Reinauer14e22772010-04-27 06:56:47 +000020099
Eric Biederman90089602004-05-28 14:11:54 +000020100 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020101 verify_consistency(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020102
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020103 /* Allocate and initialize the live ranges */
20104 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020105
Stefan Reinauer14e22772010-04-27 06:56:47 +000020106 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020107 * buys me nothing. The code is left this way in case
20108 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020109 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020110 */
20111 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020112 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020113 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020114 }
20115
Eric Biederman153ea352003-06-20 14:43:20 +000020116 /* Remove any previous live edge calculations */
20117 cleanup_live_edges(&rstate);
20118
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020119 /* Compute the interference graph */
20120 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020121 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020122 graph_ins, &rstate);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020123
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020124 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020125 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020126 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020127 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020128 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020129 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020130 print_interference_ins, &rstate);
20131 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020132
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020133 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020134
Eric Biederman5ade04a2003-10-22 04:03:46 +000020135 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020136 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020137 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020138 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020139
Eric Biederman83b991a2003-10-11 06:20:25 +000020140#if DEBUG_CONSISTENCY > 1
20141# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020142 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020143# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020144 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020145 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020146 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020147 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020148# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020149 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020150#endif
20151#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000020152
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020153 /* Build the groups low and high. But with the nodes
20154 * first sorted by degree order.
20155 */
20156 rstate.low_tail = &rstate.low;
20157 rstate.high_tail = &rstate.high;
20158 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20159 if (rstate.high) {
20160 rstate.high->group_prev = &rstate.high;
20161 }
20162 for(point = &rstate.high; *point; point = &(*point)->group_next)
20163 ;
20164 rstate.high_tail = point;
20165 /* Walk through the high list and move everything that needs
20166 * to be onto low.
20167 */
20168 for(point = &rstate.high; *point; point = next) {
20169 struct live_range *range;
20170 next = &(*point)->group_next;
20171 range = *point;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020172
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020173 /* If it has a low degree or it already has a color
20174 * place the node in low.
20175 */
20176 if ((range->degree < regc_max_size(state, range->classes)) ||
20177 (range->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020178 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020179 range - rstate.lr, range->degree,
20180 (range->color != REG_UNSET) ? " (colored)": "");
20181 *range->group_prev = range->group_next;
20182 if (range->group_next) {
20183 range->group_next->group_prev = range->group_prev;
20184 }
20185 if (&range->group_next == rstate.high_tail) {
20186 rstate.high_tail = range->group_prev;
20187 }
20188 range->group_prev = rstate.low_tail;
20189 range->group_next = 0;
20190 *rstate.low_tail = range;
20191 rstate.low_tail = &range->group_next;
20192 next = point;
20193 }
20194 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020195 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020196 range - rstate.lr, range->degree,
20197 (range->color != REG_UNSET) ? " (colored)": "");
20198 }
20199 }
20200 /* Color the live_ranges */
20201 colored = color_graph(state, &rstate);
20202 rstate.passes++;
20203 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020204
Eric Biedermana96d6a92003-05-13 20:45:19 +000020205 /* Verify the graph was properly colored */
20206 verify_colors(state, &rstate);
20207
Eric Biedermanb138ac82003-04-22 18:44:01 +000020208 /* Move the colors from the graph to the triples */
20209 color_triples(state, &rstate);
20210
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020211 /* Cleanup the temporary data structures */
20212 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020213
20214 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020215 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020216}
20217
20218/* Sparce Conditional Constant Propogation
20219 * =========================================
20220 */
20221struct ssa_edge;
20222struct flow_block;
20223struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020224 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020225 struct triple *def;
20226 struct ssa_edge *out;
20227 struct flow_block *fblock;
20228 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020229 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020230 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020231 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020232 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020233};
20234struct ssa_edge {
20235 struct lattice_node *src;
20236 struct lattice_node *dst;
20237 struct ssa_edge *work_next;
20238 struct ssa_edge *work_prev;
20239 struct ssa_edge *out_next;
20240};
20241struct flow_edge {
20242 struct flow_block *src;
20243 struct flow_block *dst;
20244 struct flow_edge *work_next;
20245 struct flow_edge *work_prev;
20246 struct flow_edge *in_next;
20247 struct flow_edge *out_next;
20248 int executable;
20249};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020250#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020251struct flow_block {
20252 struct block *block;
20253 struct flow_edge *in;
20254 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020255 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020256};
20257
20258struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020259 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020260 struct lattice_node *lattice;
20261 struct ssa_edge *ssa_edges;
20262 struct flow_block *flow_blocks;
20263 struct flow_edge *flow_work_list;
20264 struct ssa_edge *ssa_work_list;
20265};
20266
20267
Eric Biederman90089602004-05-28 14:11:54 +000020268static int is_scc_const(struct compile_state *state, struct triple *ins)
20269{
20270 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20271}
20272
20273static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20274{
20275 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20276}
20277
20278static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20279{
20280 return is_scc_const(state, lnode->val);
20281}
20282
20283static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20284{
20285 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20286}
20287
Stefan Reinauer14e22772010-04-27 06:56:47 +000020288static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020289 struct flow_edge *fedge)
20290{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020291 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020292 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020293 fedge,
20294 fedge->src->block?fedge->src->block->last->id: 0,
20295 fedge->dst->block?fedge->dst->block->first->id: 0);
20296 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020297 if ((fedge == scc->flow_work_list) ||
20298 (fedge->work_next != fedge) ||
20299 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020300
20301 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020302 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020303 fedge);
20304 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020305 return;
20306 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020307 if (!scc->flow_work_list) {
20308 scc->flow_work_list = fedge;
20309 fedge->work_next = fedge->work_prev = fedge;
20310 }
20311 else {
20312 struct flow_edge *ftail;
20313 ftail = scc->flow_work_list->work_prev;
20314 fedge->work_next = ftail->work_next;
20315 fedge->work_prev = ftail;
20316 fedge->work_next->work_prev = fedge;
20317 fedge->work_prev->work_next = fedge;
20318 }
20319}
20320
20321static struct flow_edge *scc_next_fedge(
20322 struct compile_state *state, struct scc_state *scc)
20323{
20324 struct flow_edge *fedge;
20325 fedge = scc->flow_work_list;
20326 if (fedge) {
20327 fedge->work_next->work_prev = fedge->work_prev;
20328 fedge->work_prev->work_next = fedge->work_next;
20329 if (fedge->work_next != fedge) {
20330 scc->flow_work_list = fedge->work_next;
20331 } else {
20332 scc->flow_work_list = 0;
20333 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020334 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020335 }
20336 return fedge;
20337}
20338
20339static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20340 struct ssa_edge *sedge)
20341{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020342 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020343 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20344 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020345 sedge->src->def->id,
20346 sedge->dst->def->id);
20347 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020348 if ((sedge == scc->ssa_work_list) ||
20349 (sedge->work_next != sedge) ||
20350 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020351
20352 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020353 fprintf(state->errout, "dupped sedge: %5ld\n",
20354 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020355 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020356 return;
20357 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020358 if (!scc->ssa_work_list) {
20359 scc->ssa_work_list = sedge;
20360 sedge->work_next = sedge->work_prev = sedge;
20361 }
20362 else {
20363 struct ssa_edge *stail;
20364 stail = scc->ssa_work_list->work_prev;
20365 sedge->work_next = stail->work_next;
20366 sedge->work_prev = stail;
20367 sedge->work_next->work_prev = sedge;
20368 sedge->work_prev->work_next = sedge;
20369 }
20370}
20371
20372static struct ssa_edge *scc_next_sedge(
20373 struct compile_state *state, struct scc_state *scc)
20374{
20375 struct ssa_edge *sedge;
20376 sedge = scc->ssa_work_list;
20377 if (sedge) {
20378 sedge->work_next->work_prev = sedge->work_prev;
20379 sedge->work_prev->work_next = sedge->work_next;
20380 if (sedge->work_next != sedge) {
20381 scc->ssa_work_list = sedge->work_next;
20382 } else {
20383 scc->ssa_work_list = 0;
20384 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020385 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020386 }
20387 return sedge;
20388}
20389
20390static void initialize_scc_state(
20391 struct compile_state *state, struct scc_state *scc)
20392{
20393 int ins_count, ssa_edge_count;
20394 int ins_index, ssa_edge_index, fblock_index;
20395 struct triple *first, *ins;
20396 struct block *block;
20397 struct flow_block *fblock;
20398
20399 memset(scc, 0, sizeof(*scc));
20400
20401 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020402 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020403 ins = first;
20404 ins_count = ssa_edge_count = 0;
20405 do {
20406 struct triple_set *edge;
20407 ins_count += 1;
20408 for(edge = ins->use; edge; edge = edge->next) {
20409 ssa_edge_count++;
20410 }
20411 ins = ins->next;
20412 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020413 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020414 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20415 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020416 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020417 scc->ins_count = ins_count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020418 scc->lattice =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020419 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020420 scc->ssa_edges =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020421 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020422 scc->flow_blocks =
20423 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020424 "flow_blocks");
20425
20426 /* Initialize pass one collect up the nodes */
20427 fblock = 0;
20428 block = 0;
20429 ins_index = ssa_edge_index = fblock_index = 0;
20430 ins = first;
20431 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020432 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20433 block = ins->u.block;
20434 if (!block) {
20435 internal_error(state, ins, "label without block");
20436 }
20437 fblock_index += 1;
20438 block->vertex = fblock_index;
20439 fblock = &scc->flow_blocks[fblock_index];
20440 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020441 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20442 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020443 }
20444 {
20445 struct lattice_node *lnode;
20446 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020447 lnode = &scc->lattice[ins_index];
20448 lnode->def = ins;
20449 lnode->out = 0;
20450 lnode->fblock = fblock;
20451 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020452 if (lnode->val->op == OP_UNKNOWNVAL) {
20453 lnode->val = 0; /* LATTICE LOW by definition */
20454 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020455 lnode->old_id = ins->id;
20456 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020457 }
20458 ins = ins->next;
20459 } while(ins != first);
20460 /* Initialize pass two collect up the edges */
20461 block = 0;
20462 fblock = 0;
20463 ins = first;
20464 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020465 {
20466 struct triple_set *edge;
20467 struct ssa_edge **stail;
20468 struct lattice_node *lnode;
20469 lnode = &scc->lattice[ins->id];
20470 lnode->out = 0;
20471 stail = &lnode->out;
20472 for(edge = ins->use; edge; edge = edge->next) {
20473 struct ssa_edge *sedge;
20474 ssa_edge_index += 1;
20475 sedge = &scc->ssa_edges[ssa_edge_index];
20476 *stail = sedge;
20477 stail = &sedge->out_next;
20478 sedge->src = lnode;
20479 sedge->dst = &scc->lattice[edge->member->id];
20480 sedge->work_next = sedge->work_prev = sedge;
20481 sedge->out_next = 0;
20482 }
20483 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020484 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20485 struct flow_edge *fedge, **ftail;
20486 struct block_set *bedge;
20487 block = ins->u.block;
20488 fblock = &scc->flow_blocks[block->vertex];
20489 fblock->in = 0;
20490 fblock->out = 0;
20491 ftail = &fblock->out;
20492
20493 fedge = fblock->edges;
20494 bedge = block->edges;
20495 for(; bedge; bedge = bedge->next, fedge++) {
20496 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20497 if (fedge->dst->block != bedge->member) {
20498 internal_error(state, 0, "block mismatch");
20499 }
20500 *ftail = fedge;
20501 ftail = &fedge->out_next;
20502 fedge->out_next = 0;
20503 }
20504 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20505 fedge->src = fblock;
20506 fedge->work_next = fedge->work_prev = fedge;
20507 fedge->executable = 0;
20508 }
20509 }
20510 ins = ins->next;
20511 } while (ins != first);
20512 block = 0;
20513 fblock = 0;
20514 ins = first;
20515 do {
20516 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20517 struct flow_edge **ftail;
20518 struct block_set *bedge;
20519 block = ins->u.block;
20520 fblock = &scc->flow_blocks[block->vertex];
20521 ftail = &fblock->in;
20522 for(bedge = block->use; bedge; bedge = bedge->next) {
20523 struct block *src_block;
20524 struct flow_block *sfblock;
20525 struct flow_edge *sfedge;
20526 src_block = bedge->member;
20527 sfblock = &scc->flow_blocks[src_block->vertex];
20528 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20529 if (sfedge->dst == fblock) {
20530 break;
20531 }
20532 }
20533 if (!sfedge) {
20534 internal_error(state, 0, "edge mismatch");
20535 }
20536 *ftail = sfedge;
20537 ftail = &sfedge->in_next;
20538 sfedge->in_next = 0;
20539 }
20540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020541 ins = ins->next;
20542 } while(ins != first);
20543 /* Setup a dummy block 0 as a node above the start node */
20544 {
20545 struct flow_block *fblock, *dst;
20546 struct flow_edge *fedge;
20547 fblock = &scc->flow_blocks[0];
20548 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020549 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020550 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020551 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020552 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020553 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020554 fedge->src = fblock;
20555 fedge->dst = dst;
20556 fedge->work_next = fedge;
20557 fedge->work_prev = fedge;
20558 fedge->in_next = fedge->dst->in;
20559 fedge->out_next = 0;
20560 fedge->executable = 0;
20561 fedge->dst->in = fedge;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020562
Eric Biedermanb138ac82003-04-22 18:44:01 +000020563 /* Initialize the work lists */
20564 scc->flow_work_list = 0;
20565 scc->ssa_work_list = 0;
20566 scc_add_fedge(state, scc, fedge);
20567 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020568 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020569 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020570 ins_index, ssa_edge_index, fblock_index);
20571 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020572}
20573
Stefan Reinauer14e22772010-04-27 06:56:47 +000020574
Eric Biedermanb138ac82003-04-22 18:44:01 +000020575static void free_scc_state(
20576 struct compile_state *state, struct scc_state *scc)
20577{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020578 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020579 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020580 struct flow_block *fblock;
20581 fblock = &scc->flow_blocks[i];
20582 if (fblock->edges) {
20583 xfree(fblock->edges);
20584 fblock->edges = 0;
20585 }
20586 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020587 xfree(scc->flow_blocks);
20588 xfree(scc->ssa_edges);
20589 xfree(scc->lattice);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020590
Eric Biedermanb138ac82003-04-22 18:44:01 +000020591}
20592
20593static struct lattice_node *triple_to_lattice(
20594 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20595{
20596 if (ins->id <= 0) {
20597 internal_error(state, ins, "bad id");
20598 }
20599 return &scc->lattice[ins->id];
20600}
20601
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020602static struct triple *preserve_lval(
20603 struct compile_state *state, struct lattice_node *lnode)
20604{
20605 struct triple *old;
20606 /* Preserve the original value */
20607 if (lnode->val) {
20608 old = dup_triple(state, lnode->val);
20609 if (lnode->val != lnode->def) {
20610 xfree(lnode->val);
20611 }
20612 lnode->val = 0;
20613 } else {
20614 old = 0;
20615 }
20616 return old;
20617}
20618
Stefan Reinauer14e22772010-04-27 06:56:47 +000020619static int lval_changed(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020620 struct triple *old, struct lattice_node *lnode)
20621{
20622 int changed;
20623 /* See if the lattice value has changed */
20624 changed = 1;
20625 if (!old && !lnode->val) {
20626 changed = 0;
20627 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020628 if (changed &&
20629 lnode->val && old &&
20630 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020631 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020632 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20633 changed = 0;
20634 }
20635 if (old) {
20636 xfree(old);
20637 }
20638 return changed;
20639
20640}
20641
Eric Biederman5ade04a2003-10-22 04:03:46 +000020642static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020643 struct compile_state *state, struct scc_state *scc,
20644 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020645{
Eric Biederman90089602004-05-28 14:11:54 +000020646 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20647 display_triple_changes(state->errout, lnode->val, lnode->def);
20648 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020649 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020650 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020651 struct triple *val, **expr;
20652 val = lnode->val? lnode->val : lnode->def;
20653 fprintf(fp, "%p %s %3d %10s (",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020654 lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020655 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20656 lnode->def->id,
20657 tops(lnode->def->op));
20658 expr = triple_rhs(state, lnode->def, 0);
20659 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20660 if (*expr) {
20661 fprintf(fp, " %d", (*expr)->id);
20662 }
20663 }
20664 if (val->op == OP_INTCONST) {
20665 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20666 }
20667 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020668 (is_lattice_hi(state, lnode)? "hi":
20669 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020670 changed? "changed" : ""
20671 );
20672 }
20673}
20674
Eric Biedermanb138ac82003-04-22 18:44:01 +000020675static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20676 struct lattice_node *lnode)
20677{
20678 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020679 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020680 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020681 int count, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020682
Eric Biedermanb138ac82003-04-22 18:44:01 +000020683 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020684 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020685
Eric Biedermanb138ac82003-04-22 18:44:01 +000020686 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020687 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020688 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020689 scratch->next = scratch;
20690 scratch->prev = scratch;
20691 scratch->use = 0;
20692
Eric Biederman90089602004-05-28 14:11:54 +000020693 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020694 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020695 dexpr = &lnode->def->param[i];
20696 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020697 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020698 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20699 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020700 *dexpr) {
20701 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020702 tmp = triple_to_lattice(state, scc, *dexpr);
20703 *vexpr = (tmp->val)? tmp->val : tmp->def;
20704 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020705 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020706 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020707 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020708 }
20709 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020710#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020711#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020712#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020713 /* So far it looks like only the strength reduction
20714 * optimization are things I need to worry about.
20715 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020716 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020717 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020718 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020719 internal_error(state, lnode->def, "scratch used?");
20720 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020721 if ((scratch->prev != scratch) ||
20722 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020723 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020724 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020725 internal_error(state, lnode->def, "scratch in list?");
20726 }
20727 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020728 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020729 for(i = 0; i < count; i++) {
20730 vexpr = &scratch->param[i];
20731 if (*vexpr) {
20732 unuse_triple(*vexpr, scratch);
20733 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020734 }
Eric Biederman90089602004-05-28 14:11:54 +000020735 if (lnode->val->op == OP_UNKNOWNVAL) {
20736 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020737 }
Eric Biederman90089602004-05-28 14:11:54 +000020738 /* Find the case when I am lattice high */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020739 if (lnode->val &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000020740 (lnode->val->op == lnode->def->op) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000020741 (memcmp(lnode->val->param, lnode->def->param,
Eric Biederman0babc1c2003-05-09 02:39:00 +000020742 count * sizeof(lnode->val->param[0])) == 0) &&
20743 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020744 lnode->val = lnode->def;
20745 }
Eric Biederman90089602004-05-28 14:11:54 +000020746 /* Only allow lattice high when all of my inputs
20747 * are also lattice high. Occassionally I can
20748 * have constants with a lattice low input, so
20749 * I do not need to check that case.
20750 */
20751 if (is_lattice_hi(state, lnode)) {
20752 struct lattice_node *tmp;
20753 int rhs;
20754 rhs = lnode->val->rhs;
20755 for(i = 0; i < rhs; i++) {
20756 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20757 if (!is_lattice_hi(state, tmp)) {
20758 lnode->val = 0;
20759 break;
20760 }
20761 }
20762 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020763 /* Find the cases that are always lattice lo */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020764 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020765 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020766 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020767 lnode->val = 0;
20768 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020769 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020770 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020771 /* See if this value should not change */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020772 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020773 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020774 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020775 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020776#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020777#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020778#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020779 if (changed) {
20780 internal_warning(state, lnode->def, "non def changes value?");
20781 }
20782 lnode->val = 0;
20783 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020784
Eric Biederman83b991a2003-10-11 06:20:25 +000020785 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020786 if (lnode->val != scratch) {
20787 xfree(scratch);
20788 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020789
Eric Biedermanb138ac82003-04-22 18:44:01 +000020790 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020791}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020792
Eric Biederman90089602004-05-28 14:11:54 +000020793
20794static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020795 struct lattice_node *lnode)
20796{
20797 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020798 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020799 int changed;
20800
20801 /* Update the branch value */
20802 changed = compute_lnode_val(state, scc, lnode);
20803 scc_debug_lnode(state, scc, lnode, changed);
20804
20805 /* This only applies to conditional branches */
20806 if (!triple_is_cbranch(state, lnode->def)) {
20807 internal_error(state, lnode->def, "not a conditional branch");
20808 }
20809
Eric Biederman5ade04a2003-10-22 04:03:46 +000020810 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020811 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020812 FILE *fp = state->errout;
20813 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020814 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020815 lnode->def->id);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020816
Eric Biedermanb138ac82003-04-22 18:44:01 +000020817 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020818 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020819 }
Eric Biederman90089602004-05-28 14:11:54 +000020820 fprintf(fp, " )");
20821 if (lnode->def->rhs > 0) {
20822 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020823 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020824 }
Eric Biederman90089602004-05-28 14:11:54 +000020825 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020826 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020827 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020828 for(left = cond->fblock->out; left; left = left->out_next) {
20829 if (left->dst->block->first == lnode->def->next) {
20830 break;
20831 }
20832 }
20833 if (!left) {
20834 internal_error(state, lnode->def, "Cannot find left branch edge");
20835 }
20836 for(right = cond->fblock->out; right; right = right->out_next) {
20837 if (right->dst->block->first == TARG(lnode->def, 0)) {
20838 break;
20839 }
20840 }
20841 if (!right) {
20842 internal_error(state, lnode->def, "Cannot find right branch edge");
20843 }
Eric Biederman90089602004-05-28 14:11:54 +000020844 /* I should only come here if the controlling expressions value
20845 * has changed, which means it must be either a constant or lo.
20846 */
20847 if (is_lattice_hi(state, cond)) {
20848 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020849 return;
20850 }
Eric Biederman90089602004-05-28 14:11:54 +000020851 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020852 scc_add_fedge(state, scc, left);
20853 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020854 }
20855 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020856 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020857 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020858 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020859 }
20860
20861}
20862
Eric Biederman90089602004-05-28 14:11:54 +000020863
Stefan Reinauer14e22772010-04-27 06:56:47 +000020864static void scc_add_sedge_dst(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000020865 struct scc_state *scc, struct ssa_edge *sedge)
20866{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020867 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020868 scc_visit_cbranch(state, scc, sedge->dst);
20869 }
20870 else if (triple_is_def(state, sedge->dst->def)) {
20871 scc_add_sedge(state, scc, sedge);
20872 }
20873}
20874
Stefan Reinauer14e22772010-04-27 06:56:47 +000020875static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
Eric Biederman90089602004-05-28 14:11:54 +000020876 struct lattice_node *lnode)
20877{
20878 struct lattice_node *tmp;
20879 struct triple **slot, *old;
20880 struct flow_edge *fedge;
20881 int changed;
20882 int index;
20883 if (lnode->def->op != OP_PHI) {
20884 internal_error(state, lnode->def, "not phi");
20885 }
20886 /* Store the original value */
20887 old = preserve_lval(state, lnode);
20888
20889 /* default to lattice high */
20890 lnode->val = lnode->def;
20891 slot = &RHS(lnode->def, 0);
20892 index = 0;
20893 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20894 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020895 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000020896 index,
20897 fedge->dst->block->vertex,
20898 fedge->executable
20899 );
20900 }
20901 if (!fedge->executable) {
20902 continue;
20903 }
20904 if (!slot[index]) {
20905 internal_error(state, lnode->def, "no phi value");
20906 }
20907 tmp = triple_to_lattice(state, scc, slot[index]);
20908 /* meet(X, lattice low) = lattice low */
20909 if (is_lattice_lo(state, tmp)) {
20910 lnode->val = 0;
20911 }
20912 /* meet(X, lattice high) = X */
20913 else if (is_lattice_hi(state, tmp)) {
20914 lnode->val = lnode->val;
20915 }
20916 /* meet(lattice high, X) = X */
20917 else if (is_lattice_hi(state, lnode)) {
20918 lnode->val = dup_triple(state, tmp->val);
20919 /* Only change the type if necessary */
20920 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20921 lnode->val->type = lnode->def->type;
20922 }
20923 }
20924 /* meet(const, const) = const or lattice low */
20925 else if (!constants_equal(state, lnode->val, tmp->val)) {
20926 lnode->val = 0;
20927 }
20928
20929 /* meet(lattice low, X) = lattice low */
20930 if (is_lattice_lo(state, lnode)) {
20931 lnode->val = 0;
20932 break;
20933 }
20934 }
20935 changed = lval_changed(state, old, lnode);
20936 scc_debug_lnode(state, scc, lnode, changed);
20937
20938 /* If the lattice value has changed update the work lists. */
20939 if (changed) {
20940 struct ssa_edge *sedge;
20941 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20942 scc_add_sedge_dst(state, scc, sedge);
20943 }
20944 }
20945}
20946
20947
Eric Biedermanb138ac82003-04-22 18:44:01 +000020948static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20949 struct lattice_node *lnode)
20950{
20951 int changed;
20952
Eric Biederman90089602004-05-28 14:11:54 +000020953 if (!triple_is_def(state, lnode->def)) {
20954 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020955 }
Eric Biederman90089602004-05-28 14:11:54 +000020956 changed = compute_lnode_val(state, scc, lnode);
20957 scc_debug_lnode(state, scc, lnode, changed);
20958
20959 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020960 struct ssa_edge *sedge;
20961 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020962 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020963 }
20964 }
20965}
20966
20967static void scc_writeback_values(
20968 struct compile_state *state, struct scc_state *scc)
20969{
20970 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020971 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020972 ins = first;
20973 do {
20974 struct lattice_node *lnode;
20975 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020976 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020977 if (is_lattice_hi(state, lnode) &&
20978 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020979 {
20980 struct flow_edge *fedge;
20981 int executable;
20982 executable = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020983 for(fedge = lnode->fblock->in;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020984 !executable && fedge; fedge = fedge->in_next) {
20985 executable |= fedge->executable;
20986 }
20987 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020988 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020989 "lattice node %d %s->%s still high?",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020990 ins->id,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020991 tops(lnode->def->op),
20992 tops(lnode->val->op));
20993 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020994 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020995 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020996
Eric Biederman83b991a2003-10-11 06:20:25 +000020997 /* Restore id */
20998 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020999 if (lnode->val && (lnode->val != ins)) {
21000 /* See if it something I know how to write back */
21001 switch(lnode->val->op) {
21002 case OP_INTCONST:
21003 mkconst(state, ins, lnode->val->u.cval);
21004 break;
21005 case OP_ADDRCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021006 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021007 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021008 break;
21009 default:
21010 /* By default don't copy the changes,
21011 * recompute them in place instead.
21012 */
21013 simplify(state, ins);
21014 break;
21015 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021016 if (is_const(lnode->val) &&
21017 !constants_equal(state, lnode->val, ins)) {
21018 internal_error(state, 0, "constants not equal");
21019 }
21020 /* Free the lattice nodes */
21021 xfree(lnode->val);
21022 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021023 }
21024 ins = ins->next;
21025 } while(ins != first);
21026}
21027
21028static void scc_transform(struct compile_state *state)
21029{
21030 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021031 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21032 return;
21033 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021034
21035 initialize_scc_state(state, &scc);
21036
21037 while(scc.flow_work_list || scc.ssa_work_list) {
21038 struct flow_edge *fedge;
21039 struct ssa_edge *sedge;
21040 struct flow_edge *fptr;
21041 while((fedge = scc_next_fedge(state, &scc))) {
21042 struct block *block;
21043 struct triple *ptr;
21044 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021045 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021046 int done;
21047 if (fedge->executable) {
21048 continue;
21049 }
21050 if (!fedge->dst) {
21051 internal_error(state, 0, "fedge without dst");
21052 }
21053 if (!fedge->src) {
21054 internal_error(state, 0, "fedge without src");
21055 }
21056 fedge->executable = 1;
21057 fblock = fedge->dst;
21058 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021059 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021060 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21061 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021062 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021063 }
21064 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021065
Eric Biederman5ade04a2003-10-22 04:03:46 +000021066 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021067 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021068 block->vertex, reps);
21069 }
21070
Eric Biedermanb138ac82003-04-22 18:44:01 +000021071 done = 0;
21072 for(ptr = block->first; !done; ptr = ptr->next) {
21073 struct lattice_node *lnode;
21074 done = (ptr == block->last);
21075 lnode = &scc.lattice[ptr->id];
21076 if (ptr->op == OP_PHI) {
21077 scc_visit_phi(state, &scc, lnode);
21078 }
Eric Biederman90089602004-05-28 14:11:54 +000021079 else if ((reps == 1) && triple_is_def(state, ptr))
21080 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021081 scc_visit_expr(state, &scc, lnode);
21082 }
21083 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021084 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021085 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021086 struct flow_edge *out;
21087 for(out = fblock->out; out; out = out->out_next) {
21088 scc_add_fedge(state, &scc, out);
21089 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021090 }
21091 }
21092 while((sedge = scc_next_sedge(state, &scc))) {
21093 struct lattice_node *lnode;
21094 struct flow_block *fblock;
21095 lnode = sedge->dst;
21096 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021097
21098 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021099 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021100 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021101 sedge->src->def->id,
21102 sedge->dst->def->id);
21103 }
21104
Eric Biedermanb138ac82003-04-22 18:44:01 +000021105 if (lnode->def->op == OP_PHI) {
21106 scc_visit_phi(state, &scc, lnode);
21107 }
21108 else {
21109 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21110 if (fptr->executable) {
21111 break;
21112 }
21113 }
21114 if (fptr) {
21115 scc_visit_expr(state, &scc, lnode);
21116 }
21117 }
21118 }
21119 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021120
Eric Biedermanb138ac82003-04-22 18:44:01 +000021121 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021122 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021123 rebuild_ssa_form(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021124
Eric Biederman90089602004-05-28 14:11:54 +000021125 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021126}
21127
21128
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021129static void transform_to_arch_instructions(struct compile_state *state)
21130{
21131 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021132 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021133 ins = first;
21134 do {
21135 ins = transform_to_arch_instruction(state, ins);
21136 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021137
Eric Biederman90089602004-05-28 14:11:54 +000021138 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021139}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021140
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021141#if DEBUG_CONSISTENCY
21142static void verify_uses(struct compile_state *state)
21143{
21144 struct triple *first, *ins;
21145 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021146 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021147 ins = first;
21148 do {
21149 struct triple **expr;
21150 expr = triple_rhs(state, ins, 0);
21151 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021152 struct triple *rhs;
21153 rhs = *expr;
21154 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021155 if (set->member == ins) {
21156 break;
21157 }
21158 }
21159 if (!set) {
21160 internal_error(state, ins, "rhs not used");
21161 }
21162 }
21163 expr = triple_lhs(state, ins, 0);
21164 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021165 struct triple *lhs;
21166 lhs = *expr;
21167 for(set = lhs?lhs->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, "lhs not used");
21174 }
21175 }
Eric Biederman90089602004-05-28 14:11:54 +000021176 expr = triple_misc(state, ins, 0);
21177 if (ins->op != OP_PHI) {
21178 for(; expr; expr = triple_targ(state, ins, expr)) {
21179 struct triple *misc;
21180 misc = *expr;
21181 for(set = misc?misc->use:0; set; set = set->next) {
21182 if (set->member == ins) {
21183 break;
21184 }
21185 }
21186 if (!set) {
21187 internal_error(state, ins, "misc not used");
21188 }
21189 }
21190 }
21191 if (!triple_is_ret(state, ins)) {
21192 expr = triple_targ(state, ins, 0);
21193 for(; expr; expr = triple_targ(state, ins, expr)) {
21194 struct triple *targ;
21195 targ = *expr;
21196 for(set = targ?targ->use:0; set; set = set->next) {
21197 if (set->member == ins) {
21198 break;
21199 }
21200 }
21201 if (!set) {
21202 internal_error(state, ins, "targ not used");
21203 }
21204 }
21205 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021206 ins = ins->next;
21207 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021208
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021209}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021210static void verify_blocks_present(struct compile_state *state)
21211{
21212 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021213 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021214 return;
21215 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021216 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021217 ins = first;
21218 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021219 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021220 if (triple_stores_block(state, ins)) {
21221 if (!ins->u.block) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021222 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021223 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021224 }
21225 }
21226 ins = ins->next;
21227 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021228
21229
Eric Biedermand1ea5392003-06-28 06:49:45 +000021230}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021231
21232static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21233{
21234 struct block_set *bedge;
21235 struct block *targ;
21236 targ = block_of_triple(state, edge);
21237 for(bedge = block->edges; bedge; bedge = bedge->next) {
21238 if (bedge->member == targ) {
21239 return 1;
21240 }
21241 }
21242 return 0;
21243}
21244
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021245static void verify_blocks(struct compile_state *state)
21246{
21247 struct triple *ins;
21248 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021249 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021250 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021251 if (!block) {
21252 return;
21253 }
Eric Biederman530b5192003-07-01 10:05:30 +000021254 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021255 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021256 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021257 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021258 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021259 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021260 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021261 internal_error(state, ins, "inconsitent block specified");
21262 }
Eric Biederman530b5192003-07-01 10:05:30 +000021263 valid_ins(state, ins);
21264 }
21265 users = 0;
21266 for(user = block->use; user; user = user->next) {
21267 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021268 if (!user->member->first) {
21269 internal_error(state, block->first, "user is empty");
21270 }
Eric Biederman90089602004-05-28 14:11:54 +000021271 if ((block == state->bb.last_block) &&
21272 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021273 continue;
21274 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021275 for(edge = user->member->edges; edge; edge = edge->next) {
21276 if (edge->member == block) {
21277 break;
21278 }
21279 }
21280 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021281 internal_error(state, user->member->first,
21282 "user does not use block");
21283 }
21284 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021285 if (triple_is_branch(state, block->last)) {
21286 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021287 expr = triple_edge_targ(state, block->last, 0);
21288 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021289 if (*expr && !edge_present(state, block, *expr)) {
21290 internal_error(state, block->last, "no edge to targ");
21291 }
21292 }
Eric Biederman530b5192003-07-01 10:05:30 +000021293 }
Eric Biederman90089602004-05-28 14:11:54 +000021294 if (!triple_is_ubranch(state, block->last) &&
21295 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021296 !edge_present(state, block, block->last->next)) {
21297 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021298 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021299 for(edge = block->edges; edge; edge = edge->next) {
21300 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021301 if (user->member == block) {
21302 break;
21303 }
21304 }
21305 if (!user || user->member != block) {
21306 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021307 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021308 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021309 if (!edge->member->first) {
21310 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021311 }
Eric Biederman530b5192003-07-01 10:05:30 +000021312 }
21313 if (block->users != users) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021314 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021315 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021316 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021317 }
21318 if (!triple_stores_block(state, block->last->next)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021319 internal_error(state, block->last->next,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021320 "cannot find next block");
21321 }
21322 block = block->last->next->u.block;
21323 if (!block) {
21324 internal_error(state, block->last->next,
21325 "bad next block");
21326 }
Eric Biederman90089602004-05-28 14:11:54 +000021327 } while(block != state->bb.first_block);
21328 if (blocks != state->bb.last_vertex) {
21329 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21330 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021331 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021332}
21333
21334static void verify_domination(struct compile_state *state)
21335{
21336 struct triple *first, *ins;
21337 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021338 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021339 return;
21340 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021341
Eric Biederman83b991a2003-10-11 06:20:25 +000021342 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021343 ins = first;
21344 do {
21345 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021346 struct triple **slot;
21347 struct triple *use_point;
21348 int i, zrhs;
21349 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021350 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021351 slot = &RHS(set->member, 0);
21352 /* See if the use is on the right hand side */
21353 for(i = 0; i < zrhs; i++) {
21354 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021355 break;
21356 }
21357 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021358 if (i < zrhs) {
21359 use_point = set->member;
21360 if (set->member->op == OP_PHI) {
21361 struct block_set *bset;
21362 int edge;
21363 bset = set->member->u.block->use;
21364 for(edge = 0; bset && (edge < i); edge++) {
21365 bset = bset->next;
21366 }
21367 if (!bset) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021368 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021369 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021370 }
21371 use_point = bset->member->last;
21372 }
21373 }
21374 if (use_point &&
21375 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021376 if (is_const(ins)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021377 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021378 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021379 }
21380 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021381 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021382 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021383 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021384 }
21385 }
21386 ins = ins->next;
21387 } while(ins != first);
21388}
21389
Eric Biederman83b991a2003-10-11 06:20:25 +000021390static void verify_rhs(struct compile_state *state)
21391{
21392 struct triple *first, *ins;
21393 first = state->first;
21394 ins = first;
21395 do {
21396 struct triple **slot;
21397 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021398 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021399 slot = &RHS(ins, 0);
21400 for(i = 0; i < zrhs; i++) {
21401 if (slot[i] == 0) {
21402 internal_error(state, ins,
21403 "missing rhs %d on %s",
21404 i, tops(ins->op));
21405 }
21406 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21407 internal_error(state, ins,
21408 "ins == rhs[%d] on %s",
21409 i, tops(ins->op));
21410 }
21411 }
21412 ins = ins->next;
21413 } while(ins != first);
21414}
21415
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021416static void verify_piece(struct compile_state *state)
21417{
21418 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021419 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021420 ins = first;
21421 do {
21422 struct triple *ptr;
21423 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021424 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021425 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21426 if (ptr != LHS(ins, i)) {
21427 internal_error(state, ins, "malformed lhs on %s",
21428 tops(ins->op));
21429 }
21430 if (ptr->op != OP_PIECE) {
21431 internal_error(state, ins, "bad lhs op %s at %d on %s",
21432 tops(ptr->op), i, tops(ins->op));
21433 }
21434 if (ptr->u.cval != i) {
21435 internal_error(state, ins, "bad u.cval of %d %d expected",
21436 ptr->u.cval, i);
21437 }
21438 }
21439 ins = ins->next;
21440 } while(ins != first);
21441}
Eric Biederman83b991a2003-10-11 06:20:25 +000021442
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021443static void verify_ins_colors(struct compile_state *state)
21444{
21445 struct triple *first, *ins;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021446
Eric Biederman83b991a2003-10-11 06:20:25 +000021447 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021448 ins = first;
21449 do {
21450 ins = ins->next;
21451 } while(ins != first);
21452}
Eric Biederman90089602004-05-28 14:11:54 +000021453
21454static void verify_unknown(struct compile_state *state)
21455{
21456 struct triple *first, *ins;
21457 if ( (unknown_triple.next != &unknown_triple) ||
21458 (unknown_triple.prev != &unknown_triple) ||
21459#if 0
21460 (unknown_triple.use != 0) ||
21461#endif
21462 (unknown_triple.op != OP_UNKNOWNVAL) ||
21463 (unknown_triple.lhs != 0) ||
21464 (unknown_triple.rhs != 0) ||
21465 (unknown_triple.misc != 0) ||
21466 (unknown_triple.targ != 0) ||
21467 (unknown_triple.template_id != 0) ||
21468 (unknown_triple.id != -1) ||
21469 (unknown_triple.type != &unknown_type) ||
21470 (unknown_triple.occurance != &dummy_occurance) ||
21471 (unknown_triple.param[0] != 0) ||
21472 (unknown_triple.param[1] != 0)) {
21473 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21474 }
21475 if ( (dummy_occurance.count != 2) ||
21476 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21477 (strcmp(dummy_occurance.function, "") != 0) ||
21478 (dummy_occurance.col != 0) ||
21479 (dummy_occurance.parent != 0)) {
21480 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21481 }
21482 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21483 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21484 }
21485 first = state->first;
21486 ins = first;
21487 do {
21488 int params, i;
21489 if (ins == &unknown_triple) {
21490 internal_error(state, ins, "unknown triple in list");
21491 }
21492 params = TRIPLE_SIZE(ins);
21493 for(i = 0; i < params; i++) {
21494 if (ins->param[i] == &unknown_triple) {
21495 internal_error(state, ins, "unknown triple used!");
21496 }
21497 }
21498 ins = ins->next;
21499 } while(ins != first);
21500}
21501
21502static void verify_types(struct compile_state *state)
21503{
21504 struct triple *first, *ins;
21505 first = state->first;
21506 ins = first;
21507 do {
21508 struct type *invalid;
21509 invalid = invalid_type(state, ins->type);
21510 if (invalid) {
21511 FILE *fp = state->errout;
21512 fprintf(fp, "type: ");
21513 name_of(fp, ins->type);
21514 fprintf(fp, "\n");
21515 fprintf(fp, "invalid type: ");
21516 name_of(fp, invalid);
21517 fprintf(fp, "\n");
21518 internal_error(state, ins, "invalid ins type");
21519 }
21520 } while(ins != first);
21521}
21522
21523static void verify_copy(struct compile_state *state)
21524{
21525 struct triple *first, *ins, *next;
21526 first = state->first;
21527 next = ins = first;
21528 do {
21529 ins = next;
21530 next = ins->next;
21531 if (ins->op != OP_COPY) {
21532 continue;
21533 }
21534 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21535 FILE *fp = state->errout;
21536 fprintf(fp, "src type: ");
21537 name_of(fp, RHS(ins, 0)->type);
21538 fprintf(fp, "\n");
21539 fprintf(fp, "dst type: ");
21540 name_of(fp, ins->type);
21541 fprintf(fp, "\n");
21542 internal_error(state, ins, "type mismatch in copy");
21543 }
21544 } while(next != first);
21545}
21546
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021547static void verify_consistency(struct compile_state *state)
21548{
Eric Biederman90089602004-05-28 14:11:54 +000021549 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021550 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021551 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021552 verify_blocks(state);
21553 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021554 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021555 verify_piece(state);
21556 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021557 verify_types(state);
21558 verify_copy(state);
21559 if (state->compiler->debug & DEBUG_VERIFICATION) {
21560 fprintf(state->dbgout, "consistency verified\n");
21561 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021562}
Stefan Reinauer14e22772010-04-27 06:56:47 +000021563#else
Eric Biederman153ea352003-06-20 14:43:20 +000021564static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021565#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021566
21567static void optimize(struct compile_state *state)
21568{
Eric Biederman90089602004-05-28 14:11:54 +000021569 /* Join all of the functions into one giant function */
21570 join_functions(state);
21571
Eric Biederman5ade04a2003-10-22 04:03:46 +000021572 /* Dump what the instruction graph intially looks like */
21573 print_triples(state);
21574
Eric Biederman0babc1c2003-05-09 02:39:00 +000021575 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021576 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021577 print_triples(state);
21578
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021579 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021580 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021581 state->bb.first = state->first;
21582 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021583
Eric Biederman530b5192003-07-01 10:05:30 +000021584 /* Transform the code to ssa form. */
21585 /*
21586 * The transformation to ssa form puts a phi function
21587 * on each of edge of a dominance frontier where that
21588 * phi function might be needed. At -O2 if we don't
21589 * eleminate the excess phi functions we can get an
21590 * exponential code size growth. So I kill the extra
21591 * phi functions early and I kill them often.
21592 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021593 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021594 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021595
Eric Biederman83b991a2003-10-11 06:20:25 +000021596 /* Remove dead code */
21597 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021598 verify_consistency(state);
21599
Eric Biedermanb138ac82003-04-22 18:44:01 +000021600 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021601 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021602 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021603 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021604 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021605 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021606#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021607#warning "WISHLIST implement single use constants (least possible register pressure)"
21608#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021609#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021610 /* Select architecture instructions and an initial partial
21611 * coloring based on architecture constraints.
21612 */
21613 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021614 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021615
Eric Biederman83b991a2003-10-11 06:20:25 +000021616 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021617 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021618 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021619
Eric Biedermanb138ac82003-04-22 18:44:01 +000021620 /* Color all of the variables to see if they will fit in registers */
21621 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021622 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021623
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021624 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021625 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021626
Eric Biedermanb138ac82003-04-22 18:44:01 +000021627 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021628 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021629
Eric Biedermanb138ac82003-04-22 18:44:01 +000021630 /* Remove the optimization information.
21631 * This is more to check for memory consistency than to free memory.
21632 */
Eric Biederman90089602004-05-28 14:11:54 +000021633 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021634}
21635
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021636static void print_op_asm(struct compile_state *state,
21637 struct triple *ins, FILE *fp)
21638{
21639 struct asm_info *info;
21640 const char *ptr;
21641 unsigned lhs, rhs, i;
21642 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021643 lhs = ins->lhs;
21644 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021645 /* Don't count the clobbers in lhs */
21646 for(i = 0; i < lhs; i++) {
21647 if (LHS(ins, i)->type == &void_type) {
21648 break;
21649 }
21650 }
21651 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021652 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021653 fputc('\t', fp);
21654 for(ptr = info->str; *ptr; ptr++) {
21655 char *next;
21656 unsigned long param;
21657 struct triple *piece;
21658 if (*ptr != '%') {
21659 fputc(*ptr, fp);
21660 continue;
21661 }
21662 ptr++;
21663 if (*ptr == '%') {
21664 fputc('%', fp);
21665 continue;
21666 }
21667 param = strtoul(ptr, &next, 10);
21668 if (ptr == next) {
21669 error(state, ins, "Invalid asm template");
21670 }
21671 if (param >= (lhs + rhs)) {
21672 error(state, ins, "Invalid param %%%u in asm template",
21673 param);
21674 }
21675 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021676 fprintf(fp, "%s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021677 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021678 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021679 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021680 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021681}
21682
21683
21684/* Only use the low x86 byte registers. This allows me
21685 * allocate the entire register when a byte register is used.
21686 */
21687#define X86_4_8BIT_GPRS 1
21688
Eric Biederman83b991a2003-10-11 06:20:25 +000021689/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021690#define X86_MMX_REGS (1<<0)
21691#define X86_XMM_REGS (1<<1)
21692#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021693
Eric Biedermanb138ac82003-04-22 18:44:01 +000021694/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021695#define REGC_FLAGS 0
21696#define REGC_GPR8 1
21697#define REGC_GPR16 2
21698#define REGC_GPR32 3
21699#define REGC_DIVIDEND64 4
21700#define REGC_DIVIDEND32 5
21701#define REGC_MMX 6
21702#define REGC_XMM 7
21703#define REGC_GPR32_8 8
21704#define REGC_GPR16_8 9
21705#define REGC_GPR8_LO 10
21706#define REGC_IMM32 11
21707#define REGC_IMM16 12
21708#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021709#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021710#if LAST_REGC >= MAX_REGC
21711#error "MAX_REGC is to low"
21712#endif
21713
21714/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021715#define REGCM_FLAGS (1 << REGC_FLAGS)
21716#define REGCM_GPR8 (1 << REGC_GPR8)
21717#define REGCM_GPR16 (1 << REGC_GPR16)
21718#define REGCM_GPR32 (1 << REGC_GPR32)
21719#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21720#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21721#define REGCM_MMX (1 << REGC_MMX)
21722#define REGCM_XMM (1 << REGC_XMM)
21723#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21724#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21725#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21726#define REGCM_IMM32 (1 << REGC_IMM32)
21727#define REGCM_IMM16 (1 << REGC_IMM16)
21728#define REGCM_IMM8 (1 << REGC_IMM8)
21729#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021730#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021731
21732/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021733#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021734#define REGC_FLAGS_FIRST REG_EFLAGS
21735#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021736#define REG_AL 3
21737#define REG_BL 4
21738#define REG_CL 5
21739#define REG_DL 6
21740#define REG_AH 7
21741#define REG_BH 8
21742#define REG_CH 9
21743#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021744#define REGC_GPR8_LO_FIRST REG_AL
21745#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021746#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021747#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021748#define REG_AX 11
21749#define REG_BX 12
21750#define REG_CX 13
21751#define REG_DX 14
21752#define REG_SI 15
21753#define REG_DI 16
21754#define REG_BP 17
21755#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021756#define REGC_GPR16_FIRST REG_AX
21757#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021758#define REG_EAX 19
21759#define REG_EBX 20
21760#define REG_ECX 21
21761#define REG_EDX 22
21762#define REG_ESI 23
21763#define REG_EDI 24
21764#define REG_EBP 25
21765#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021766#define REGC_GPR32_FIRST REG_EAX
21767#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021768#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021769#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21770#define REGC_DIVIDEND64_LAST REG_EDXEAX
21771#define REG_DXAX 28
21772#define REGC_DIVIDEND32_FIRST REG_DXAX
21773#define REGC_DIVIDEND32_LAST REG_DXAX
21774#define REG_MMX0 29
21775#define REG_MMX1 30
21776#define REG_MMX2 31
21777#define REG_MMX3 32
21778#define REG_MMX4 33
21779#define REG_MMX5 34
21780#define REG_MMX6 35
21781#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021782#define REGC_MMX_FIRST REG_MMX0
21783#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021784#define REG_XMM0 37
21785#define REG_XMM1 38
21786#define REG_XMM2 39
21787#define REG_XMM3 40
21788#define REG_XMM4 41
21789#define REG_XMM5 42
21790#define REG_XMM6 43
21791#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021792#define REGC_XMM_FIRST REG_XMM0
21793#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021794
21795#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021796#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021797#endif
21798
Eric Biedermanb138ac82003-04-22 18:44:01 +000021799#define LAST_REG REG_XMM7
21800
21801#define REGC_GPR32_8_FIRST REG_EAX
21802#define REGC_GPR32_8_LAST REG_EDX
21803#define REGC_GPR16_8_FIRST REG_AX
21804#define REGC_GPR16_8_LAST REG_DX
21805
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021806#define REGC_IMM8_FIRST -1
21807#define REGC_IMM8_LAST -1
21808#define REGC_IMM16_FIRST -2
21809#define REGC_IMM16_LAST -1
21810#define REGC_IMM32_FIRST -4
21811#define REGC_IMM32_LAST -1
21812
Eric Biedermanb138ac82003-04-22 18:44:01 +000021813#if LAST_REG >= MAX_REGISTERS
21814#error "MAX_REGISTERS to low"
21815#endif
21816
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021817
21818static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021819 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21820 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21821 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21822 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21823 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21824 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21825 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21826 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21827 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21828 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21829 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21830 [REGC_IMM32] = 0,
21831 [REGC_IMM16] = 0,
21832 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021833};
21834
21835static const struct {
21836 int first, last;
21837} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021838 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21839 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21840 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21841 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21842 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21843 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21844 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21845 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21846 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21847 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21848 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21849 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21850 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21851 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021852};
21853
Eric Biederman90089602004-05-28 14:11:54 +000021854#if ARCH_INPUT_REGS != 4
21855#error ARCH_INPUT_REGS size mismatch
21856#endif
21857static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21858 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21859 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21860 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21861 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21862};
21863
21864#if ARCH_OUTPUT_REGS != 4
21865#error ARCH_INPUT_REGS size mismatch
21866#endif
21867static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21868 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21869 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21870 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21871 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21872};
21873
Eric Biederman5ade04a2003-10-22 04:03:46 +000021874static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021875{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021876 memset(arch, 0, sizeof(*arch));
21877 arch->features = 0;
21878}
21879
Eric Biederman90089602004-05-28 14:11:54 +000021880static const struct compiler_flag arch_flags[] = {
21881 { "mmx", X86_MMX_REGS },
21882 { "sse", X86_XMM_REGS },
21883 { "noop-copy", X86_NOOP_COPY },
21884 { 0, 0 },
21885};
21886static const struct compiler_flag arch_cpus[] = {
21887 { "i386", 0 },
21888 { "p2", X86_MMX_REGS },
21889 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21890 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21891 { "k7", X86_MMX_REGS },
21892 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21893 { "c3", X86_MMX_REGS },
21894 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21895 { 0, 0 }
21896};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021897static int arch_encode_flag(struct arch_state *arch, const char *flag)
21898{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021899 int result;
21900 int act;
21901
21902 act = 1;
21903 result = -1;
21904 if (strncmp(flag, "no-", 3) == 0) {
21905 flag += 3;
21906 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021907 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021908 if (act && strncmp(flag, "cpu=", 4) == 0) {
21909 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021910 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021911 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021912 else {
Eric Biederman90089602004-05-28 14:11:54 +000021913 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021914 }
21915 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021916}
21917
Eric Biederman90089602004-05-28 14:11:54 +000021918static void arch_usage(FILE *fp)
21919{
21920 flag_usage(fp, arch_flags, "-m", "-mno-");
21921 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21922}
21923
Eric Biedermanb138ac82003-04-22 18:44:01 +000021924static unsigned arch_regc_size(struct compile_state *state, int class)
21925{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021926 if ((class < 0) || (class > LAST_REGC)) {
21927 return 0;
21928 }
21929 return regc_size[class];
21930}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021931
Eric Biedermanb138ac82003-04-22 18:44:01 +000021932static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21933{
21934 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021935 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
Stefan Reinauer14e22772010-04-27 06:56:47 +000021936 REGCM_GPR32_8 | REGCM_GPR32 |
Eric Biederman530b5192003-07-01 10:05:30 +000021937 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021938
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021939 /* Special case for the immediates */
21940 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21941 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21942 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000021943 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021944 return 0;
21945 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021946 return (regcm1 & regcm2) ||
21947 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21948}
21949
21950static void arch_reg_equivs(
21951 struct compile_state *state, unsigned *equiv, int reg)
21952{
21953 if ((reg < 0) || (reg > LAST_REG)) {
21954 internal_error(state, 0, "invalid register");
21955 }
21956 *equiv++ = reg;
21957 switch(reg) {
21958 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021959#if X86_4_8BIT_GPRS
21960 *equiv++ = REG_AH;
21961#endif
21962 *equiv++ = REG_AX;
21963 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021964 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021965 *equiv++ = REG_EDXEAX;
21966 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021967 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021968#if X86_4_8BIT_GPRS
21969 *equiv++ = REG_AL;
21970#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021971 *equiv++ = REG_AX;
21972 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021973 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021974 *equiv++ = REG_EDXEAX;
21975 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021976 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021977#if X86_4_8BIT_GPRS
21978 *equiv++ = REG_BH;
21979#endif
21980 *equiv++ = REG_BX;
21981 *equiv++ = REG_EBX;
21982 break;
21983
Eric Biedermanb138ac82003-04-22 18:44:01 +000021984 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021985#if X86_4_8BIT_GPRS
21986 *equiv++ = REG_BL;
21987#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021988 *equiv++ = REG_BX;
21989 *equiv++ = REG_EBX;
21990 break;
21991 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021992#if X86_4_8BIT_GPRS
21993 *equiv++ = REG_CH;
21994#endif
21995 *equiv++ = REG_CX;
21996 *equiv++ = REG_ECX;
21997 break;
21998
Eric Biedermanb138ac82003-04-22 18:44:01 +000021999 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022000#if X86_4_8BIT_GPRS
22001 *equiv++ = REG_CL;
22002#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022003 *equiv++ = REG_CX;
22004 *equiv++ = REG_ECX;
22005 break;
22006 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022007#if X86_4_8BIT_GPRS
22008 *equiv++ = REG_DH;
22009#endif
22010 *equiv++ = REG_DX;
22011 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022012 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022013 *equiv++ = REG_EDXEAX;
22014 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022015 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022016#if X86_4_8BIT_GPRS
22017 *equiv++ = REG_DL;
22018#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022019 *equiv++ = REG_DX;
22020 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022021 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022022 *equiv++ = REG_EDXEAX;
22023 break;
22024 case REG_AX:
22025 *equiv++ = REG_AL;
22026 *equiv++ = REG_AH;
22027 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022028 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022029 *equiv++ = REG_EDXEAX;
22030 break;
22031 case REG_BX:
22032 *equiv++ = REG_BL;
22033 *equiv++ = REG_BH;
22034 *equiv++ = REG_EBX;
22035 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022036 case REG_CX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022037 *equiv++ = REG_CL;
22038 *equiv++ = REG_CH;
22039 *equiv++ = REG_ECX;
22040 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022041 case REG_DX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022042 *equiv++ = REG_DL;
22043 *equiv++ = REG_DH;
22044 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022045 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022046 *equiv++ = REG_EDXEAX;
22047 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022048 case REG_SI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022049 *equiv++ = REG_ESI;
22050 break;
22051 case REG_DI:
22052 *equiv++ = REG_EDI;
22053 break;
22054 case REG_BP:
22055 *equiv++ = REG_EBP;
22056 break;
22057 case REG_SP:
22058 *equiv++ = REG_ESP;
22059 break;
22060 case REG_EAX:
22061 *equiv++ = REG_AL;
22062 *equiv++ = REG_AH;
22063 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022064 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022065 *equiv++ = REG_EDXEAX;
22066 break;
22067 case REG_EBX:
22068 *equiv++ = REG_BL;
22069 *equiv++ = REG_BH;
22070 *equiv++ = REG_BX;
22071 break;
22072 case REG_ECX:
22073 *equiv++ = REG_CL;
22074 *equiv++ = REG_CH;
22075 *equiv++ = REG_CX;
22076 break;
22077 case REG_EDX:
22078 *equiv++ = REG_DL;
22079 *equiv++ = REG_DH;
22080 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022081 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022082 *equiv++ = REG_EDXEAX;
22083 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022084 case REG_ESI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022085 *equiv++ = REG_SI;
22086 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022087 case REG_EDI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022088 *equiv++ = REG_DI;
22089 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022090 case REG_EBP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022091 *equiv++ = REG_BP;
22092 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022093 case REG_ESP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022094 *equiv++ = REG_SP;
22095 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022096 case REG_DXAX:
Eric Biederman530b5192003-07-01 10:05:30 +000022097 *equiv++ = REG_AL;
22098 *equiv++ = REG_AH;
22099 *equiv++ = REG_DL;
22100 *equiv++ = REG_DH;
22101 *equiv++ = REG_AX;
22102 *equiv++ = REG_DX;
22103 *equiv++ = REG_EAX;
22104 *equiv++ = REG_EDX;
22105 *equiv++ = REG_EDXEAX;
22106 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022107 case REG_EDXEAX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022108 *equiv++ = REG_AL;
22109 *equiv++ = REG_AH;
22110 *equiv++ = REG_DL;
22111 *equiv++ = REG_DH;
22112 *equiv++ = REG_AX;
22113 *equiv++ = REG_DX;
22114 *equiv++ = REG_EAX;
22115 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022116 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022117 break;
22118 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000022119 *equiv++ = REG_UNSET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022120}
22121
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022122static unsigned arch_avail_mask(struct compile_state *state)
22123{
22124 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022125 /* REGCM_GPR8 is not available */
Stefan Reinauer14e22772010-04-27 06:56:47 +000022126 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22127 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022128 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022129 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022130 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022131 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022132 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022133 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022134 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022135 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022136 return avail_mask;
22137}
22138
22139static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22140{
22141 unsigned mask, result;
22142 int class, class2;
22143 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022144
22145 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22146 if ((result & mask) == 0) {
22147 continue;
22148 }
22149 if (class > LAST_REGC) {
22150 result &= ~mask;
22151 }
22152 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22153 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22154 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22155 result |= (1 << class2);
22156 }
22157 }
22158 }
Eric Biederman530b5192003-07-01 10:05:30 +000022159 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022160 return result;
22161}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022162
Eric Biedermand1ea5392003-06-28 06:49:45 +000022163static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22164{
22165 /* Like arch_regcm_normalize except immediate register classes are excluded */
22166 regcm = arch_regcm_normalize(state, regcm);
22167 /* Remove the immediate register classes */
22168 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22169 return regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022170
Eric Biedermand1ea5392003-06-28 06:49:45 +000022171}
22172
Eric Biedermanb138ac82003-04-22 18:44:01 +000022173static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22174{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022175 unsigned mask;
22176 int class;
22177 mask = 0;
22178 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022179 if ((reg >= regcm_bound[class].first) &&
22180 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022181 mask |= (1 << class);
22182 }
22183 }
22184 if (!mask) {
22185 internal_error(state, 0, "reg %d not in any class", reg);
22186 }
22187 return mask;
22188}
22189
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022190static struct reg_info arch_reg_constraint(
22191 struct compile_state *state, struct type *type, const char *constraint)
22192{
22193 static const struct {
22194 char class;
22195 unsigned int mask;
22196 unsigned int reg;
22197 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022198 { 'r', REGCM_GPR32, REG_UNSET },
22199 { 'g', REGCM_GPR32, REG_UNSET },
22200 { 'p', REGCM_GPR32, REG_UNSET },
22201 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022202 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022203 { 'x', REGCM_XMM, REG_UNSET },
22204 { 'y', REGCM_MMX, REG_UNSET },
22205 { 'a', REGCM_GPR32, REG_EAX },
22206 { 'b', REGCM_GPR32, REG_EBX },
22207 { 'c', REGCM_GPR32, REG_ECX },
22208 { 'd', REGCM_GPR32, REG_EDX },
22209 { 'D', REGCM_GPR32, REG_EDI },
22210 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022211 { '\0', 0, REG_UNSET },
22212 };
22213 unsigned int regcm;
22214 unsigned int mask, reg;
22215 struct reg_info result;
22216 const char *ptr;
22217 regcm = arch_type_to_regcm(state, type);
22218 reg = REG_UNSET;
22219 mask = 0;
22220 for(ptr = constraint; *ptr; ptr++) {
22221 int i;
22222 if (*ptr == ' ') {
22223 continue;
22224 }
22225 for(i = 0; constraints[i].class != '\0'; i++) {
22226 if (constraints[i].class == *ptr) {
22227 break;
22228 }
22229 }
22230 if (constraints[i].class == '\0') {
22231 error(state, 0, "invalid register constraint ``%c''", *ptr);
22232 break;
22233 }
22234 if ((constraints[i].mask & regcm) == 0) {
22235 error(state, 0, "invalid register class %c specified",
22236 *ptr);
22237 }
22238 mask |= constraints[i].mask;
22239 if (constraints[i].reg != REG_UNSET) {
22240 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22241 error(state, 0, "Only one register may be specified");
22242 }
22243 reg = constraints[i].reg;
22244 }
22245 }
22246 result.reg = reg;
22247 result.regcm = mask;
22248 return result;
22249}
22250
22251static struct reg_info arch_reg_clobber(
22252 struct compile_state *state, const char *clobber)
22253{
22254 struct reg_info result;
22255 if (strcmp(clobber, "memory") == 0) {
22256 result.reg = REG_UNSET;
22257 result.regcm = 0;
22258 }
Eric Biederman90089602004-05-28 14:11:54 +000022259 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022260 result.reg = REG_EAX;
22261 result.regcm = REGCM_GPR32;
22262 }
Eric Biederman90089602004-05-28 14:11:54 +000022263 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022264 result.reg = REG_EBX;
22265 result.regcm = REGCM_GPR32;
22266 }
Eric Biederman90089602004-05-28 14:11:54 +000022267 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022268 result.reg = REG_ECX;
22269 result.regcm = REGCM_GPR32;
22270 }
Eric Biederman90089602004-05-28 14:11:54 +000022271 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022272 result.reg = REG_EDX;
22273 result.regcm = REGCM_GPR32;
22274 }
Eric Biederman90089602004-05-28 14:11:54 +000022275 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022276 result.reg = REG_ESI;
22277 result.regcm = REGCM_GPR32;
22278 }
Eric Biederman90089602004-05-28 14:11:54 +000022279 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022280 result.reg = REG_EDI;
22281 result.regcm = REGCM_GPR32;
22282 }
Eric Biederman90089602004-05-28 14:11:54 +000022283 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022284 result.reg = REG_EBP;
22285 result.regcm = REGCM_GPR32;
22286 }
Eric Biederman90089602004-05-28 14:11:54 +000022287 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022288 result.reg = REG_ESP;
22289 result.regcm = REGCM_GPR32;
22290 }
22291 else if (strcmp(clobber, "cc") == 0) {
22292 result.reg = REG_EFLAGS;
22293 result.regcm = REGCM_FLAGS;
22294 }
22295 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22296 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22297 result.reg = REG_XMM0 + octdigval(clobber[3]);
22298 result.regcm = REGCM_XMM;
22299 }
Eric Biederman90089602004-05-28 14:11:54 +000022300 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022301 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22302 result.reg = REG_MMX0 + octdigval(clobber[3]);
22303 result.regcm = REGCM_MMX;
22304 }
22305 else {
Eric Biederman90089602004-05-28 14:11:54 +000022306 error(state, 0, "unknown register name `%s' in asm",
22307 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022308 result.reg = REG_UNSET;
22309 result.regcm = 0;
22310 }
22311 return result;
22312}
22313
Stefan Reinauer14e22772010-04-27 06:56:47 +000022314static int do_select_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000022315 char *used, int reg, unsigned classes)
22316{
22317 unsigned mask;
22318 if (used[reg]) {
22319 return REG_UNSET;
22320 }
22321 mask = arch_reg_regcm(state, reg);
22322 return (classes & mask) ? reg : REG_UNSET;
22323}
22324
22325static int arch_select_free_register(
22326 struct compile_state *state, char *used, int classes)
22327{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022328 /* Live ranges with the most neighbors are colored first.
22329 *
22330 * Generally it does not matter which colors are given
22331 * as the register allocator attempts to color live ranges
22332 * in an order where you are guaranteed not to run out of colors.
22333 *
22334 * Occasionally the register allocator cannot find an order
22335 * of register selection that will find a free color. To
22336 * increase the odds the register allocator will work when
22337 * it guesses first give out registers from register classes
22338 * least likely to run out of registers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000022339 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022340 */
22341 int i, reg;
22342 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022343 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022344 reg = do_select_reg(state, used, i, classes);
22345 }
22346 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22347 reg = do_select_reg(state, used, i, classes);
22348 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022349 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022350 reg = do_select_reg(state, used, i, classes);
22351 }
22352 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22353 reg = do_select_reg(state, used, i, classes);
22354 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022355 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22356 reg = do_select_reg(state, used, i, classes);
22357 }
Eric Biederman530b5192003-07-01 10:05:30 +000022358 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22359 reg = do_select_reg(state, used, i, classes);
22360 }
22361 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22362 reg = do_select_reg(state, used, i, classes);
22363 }
22364 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022365 reg = do_select_reg(state, used, i, classes);
22366 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022367 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22368 reg = do_select_reg(state, used, i, classes);
22369 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022370 return reg;
22371}
22372
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022373
Stefan Reinauer14e22772010-04-27 06:56:47 +000022374static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022375{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022376
22377#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022378#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022379#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022380 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022381 mask = 0;
22382 switch(type->type & TYPE_MASK) {
22383 case TYPE_ARRAY:
Stefan Reinauer14e22772010-04-27 06:56:47 +000022384 case TYPE_VOID:
22385 mask = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022386 break;
22387 case TYPE_CHAR:
22388 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022389 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Stefan Reinauer14e22772010-04-27 06:56:47 +000022390 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022391 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022392 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022393 REGCM_MMX | REGCM_XMM |
22394 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022395 break;
22396 case TYPE_SHORT:
22397 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022398 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022399 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022400 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022401 REGCM_MMX | REGCM_XMM |
22402 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022403 break;
Eric Biederman90089602004-05-28 14:11:54 +000022404 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022405 case TYPE_INT:
22406 case TYPE_UINT:
22407 case TYPE_LONG:
22408 case TYPE_ULONG:
22409 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022410 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022411 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22412 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022413 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022414 break;
Eric Biederman90089602004-05-28 14:11:54 +000022415 case TYPE_JOIN:
22416 case TYPE_UNION:
22417 mask = arch_type_to_regcm(state, type->left);
22418 break;
22419 case TYPE_OVERLAP:
22420 mask = arch_type_to_regcm(state, type->left) &
22421 arch_type_to_regcm(state, type->right);
22422 break;
22423 case TYPE_BITFIELD:
22424 mask = arch_type_to_regcm(state, type->left);
22425 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022426 default:
Eric Biederman90089602004-05-28 14:11:54 +000022427 fprintf(state->errout, "type: ");
22428 name_of(state->errout, type);
22429 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022430 internal_error(state, 0, "no register class for type");
22431 break;
22432 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022433 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022434 return mask;
22435}
22436
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022437static int is_imm32(struct triple *imm)
22438{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022439 // second condition commented out to prevent compiler warning:
22440 // imm->u.cval is always 32bit unsigned, so the comparison is
22441 // always true.
22442 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022443 (imm->op == OP_ADDRCONST);
Stefan Reinauer14e22772010-04-27 06:56:47 +000022444
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022445}
22446static int is_imm16(struct triple *imm)
22447{
22448 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22449}
22450static int is_imm8(struct triple *imm)
22451{
22452 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22453}
22454
22455static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022456{
22457 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022458 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022459 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022460 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022461 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022462 if (!is_imm32(imm)) {
22463 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022464 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022465 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022466 use_triple(imm, ins);
22467 *expr = imm;
22468 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022469}
22470
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022471static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022472{
22473 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022474 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022475 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022476 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022477 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022478 if (!is_imm8(imm)) {
22479 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022480 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022481 unuse_triple(*expr, ins);
22482 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022483 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022484 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022485}
22486
Eric Biederman530b5192003-07-01 10:05:30 +000022487#define TEMPLATE_NOP 0
22488#define TEMPLATE_INTCONST8 1
22489#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022490#define TEMPLATE_UNKNOWNVAL 3
22491#define TEMPLATE_COPY8_REG 5
22492#define TEMPLATE_COPY16_REG 6
22493#define TEMPLATE_COPY32_REG 7
22494#define TEMPLATE_COPY_IMM8 8
22495#define TEMPLATE_COPY_IMM16 9
22496#define TEMPLATE_COPY_IMM32 10
22497#define TEMPLATE_PHI8 11
22498#define TEMPLATE_PHI16 12
22499#define TEMPLATE_PHI32 13
22500#define TEMPLATE_STORE8 14
22501#define TEMPLATE_STORE16 15
22502#define TEMPLATE_STORE32 16
22503#define TEMPLATE_LOAD8 17
22504#define TEMPLATE_LOAD16 18
22505#define TEMPLATE_LOAD32 19
22506#define TEMPLATE_BINARY8_REG 20
22507#define TEMPLATE_BINARY16_REG 21
22508#define TEMPLATE_BINARY32_REG 22
22509#define TEMPLATE_BINARY8_IMM 23
22510#define TEMPLATE_BINARY16_IMM 24
22511#define TEMPLATE_BINARY32_IMM 25
22512#define TEMPLATE_SL8_CL 26
22513#define TEMPLATE_SL16_CL 27
22514#define TEMPLATE_SL32_CL 28
22515#define TEMPLATE_SL8_IMM 29
22516#define TEMPLATE_SL16_IMM 30
22517#define TEMPLATE_SL32_IMM 31
22518#define TEMPLATE_UNARY8 32
22519#define TEMPLATE_UNARY16 33
22520#define TEMPLATE_UNARY32 34
22521#define TEMPLATE_CMP8_REG 35
22522#define TEMPLATE_CMP16_REG 36
22523#define TEMPLATE_CMP32_REG 37
22524#define TEMPLATE_CMP8_IMM 38
22525#define TEMPLATE_CMP16_IMM 39
22526#define TEMPLATE_CMP32_IMM 40
22527#define TEMPLATE_TEST8 41
22528#define TEMPLATE_TEST16 42
22529#define TEMPLATE_TEST32 43
22530#define TEMPLATE_SET 44
22531#define TEMPLATE_JMP 45
22532#define TEMPLATE_RET 46
22533#define TEMPLATE_INB_DX 47
22534#define TEMPLATE_INB_IMM 48
22535#define TEMPLATE_INW_DX 49
22536#define TEMPLATE_INW_IMM 50
22537#define TEMPLATE_INL_DX 51
22538#define TEMPLATE_INL_IMM 52
22539#define TEMPLATE_OUTB_DX 53
22540#define TEMPLATE_OUTB_IMM 54
22541#define TEMPLATE_OUTW_DX 55
22542#define TEMPLATE_OUTW_IMM 56
22543#define TEMPLATE_OUTL_DX 57
22544#define TEMPLATE_OUTL_IMM 58
22545#define TEMPLATE_BSF 59
22546#define TEMPLATE_RDMSR 60
22547#define TEMPLATE_WRMSR 61
22548#define TEMPLATE_UMUL8 62
22549#define TEMPLATE_UMUL16 63
22550#define TEMPLATE_UMUL32 64
22551#define TEMPLATE_DIV8 65
22552#define TEMPLATE_DIV16 66
22553#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022554#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022555#if LAST_TEMPLATE >= MAX_TEMPLATES
22556#error "MAX_TEMPLATES to low"
22557#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022558
Eric Biederman530b5192003-07-01 10:05:30 +000022559#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 +000022560#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
Eric Biederman530b5192003-07-01 10:05:30 +000022561#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022562
Eric Biedermanb138ac82003-04-22 18:44:01 +000022563
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022564static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022565 [TEMPLATE_NOP] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022566 .lhs = {
Eric Biederman90089602004-05-28 14:11:54 +000022567 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22568 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22569 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22570 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22571 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22572 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22573 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22574 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22575 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22576 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22577 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22578 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22631 },
22632 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022633 [TEMPLATE_INTCONST8] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022634 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22635 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022636 [TEMPLATE_INTCONST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022637 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22638 },
Eric Biederman90089602004-05-28 14:11:54 +000022639 [TEMPLATE_UNKNOWNVAL] = {
22640 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22641 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022642 [TEMPLATE_COPY8_REG] = {
22643 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22644 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022645 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022646 [TEMPLATE_COPY16_REG] = {
22647 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22648 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22649 },
22650 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022651 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022652 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022653 },
22654 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022655 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022656 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22657 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022658 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022659 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022660 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22661 },
22662 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022663 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022664 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22665 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022666 [TEMPLATE_PHI8] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022667 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022668 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22669 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022670 [TEMPLATE_PHI16] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022671 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022672 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022673 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022674 [TEMPLATE_PHI32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022675 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022676 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022677 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022678 [TEMPLATE_STORE8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022679 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022680 [0] = { REG_UNSET, REGCM_GPR32 },
22681 [1] = { REG_UNSET, REGCM_GPR8_LO },
22682 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022683 },
22684 [TEMPLATE_STORE16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022685 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022686 [0] = { REG_UNSET, REGCM_GPR32 },
22687 [1] = { REG_UNSET, REGCM_GPR16 },
22688 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022689 },
22690 [TEMPLATE_STORE32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022691 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022692 [0] = { REG_UNSET, REGCM_GPR32 },
22693 [1] = { REG_UNSET, REGCM_GPR32 },
22694 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022695 },
22696 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022697 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022698 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22699 },
22700 [TEMPLATE_LOAD16] = {
22701 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22702 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22703 },
22704 [TEMPLATE_LOAD32] = {
22705 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22706 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22707 },
Eric Biederman530b5192003-07-01 10:05:30 +000022708 [TEMPLATE_BINARY8_REG] = {
22709 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022710 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022711 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22712 [1] = { REG_UNSET, REGCM_GPR8_LO },
22713 },
22714 },
22715 [TEMPLATE_BINARY16_REG] = {
22716 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022717 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022718 [0] = { REG_VIRT0, REGCM_GPR16 },
22719 [1] = { REG_UNSET, REGCM_GPR16 },
22720 },
22721 },
22722 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022723 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022724 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022725 [0] = { REG_VIRT0, REGCM_GPR32 },
22726 [1] = { REG_UNSET, REGCM_GPR32 },
22727 },
22728 },
Eric Biederman530b5192003-07-01 10:05:30 +000022729 [TEMPLATE_BINARY8_IMM] = {
22730 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022731 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022732 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22733 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22734 },
22735 },
22736 [TEMPLATE_BINARY16_IMM] = {
22737 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022738 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022739 [0] = { REG_VIRT0, REGCM_GPR16 },
22740 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22741 },
22742 },
22743 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022744 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022745 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022746 [0] = { REG_VIRT0, REGCM_GPR32 },
22747 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22748 },
22749 },
Eric Biederman530b5192003-07-01 10:05:30 +000022750 [TEMPLATE_SL8_CL] = {
22751 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022752 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022753 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22754 [1] = { REG_CL, REGCM_GPR8_LO },
22755 },
22756 },
22757 [TEMPLATE_SL16_CL] = {
22758 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022759 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022760 [0] = { REG_VIRT0, REGCM_GPR16 },
22761 [1] = { REG_CL, REGCM_GPR8_LO },
22762 },
22763 },
22764 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022765 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022766 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022767 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022768 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022769 },
22770 },
Eric Biederman530b5192003-07-01 10:05:30 +000022771 [TEMPLATE_SL8_IMM] = {
22772 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022773 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022774 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22775 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22776 },
22777 },
22778 [TEMPLATE_SL16_IMM] = {
22779 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022780 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022781 [0] = { REG_VIRT0, REGCM_GPR16 },
22782 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22783 },
22784 },
22785 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022786 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022787 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022788 [0] = { REG_VIRT0, REGCM_GPR32 },
22789 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22790 },
22791 },
Eric Biederman530b5192003-07-01 10:05:30 +000022792 [TEMPLATE_UNARY8] = {
22793 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22794 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22795 },
22796 [TEMPLATE_UNARY16] = {
22797 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22798 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22799 },
22800 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022801 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22802 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22803 },
Eric Biederman530b5192003-07-01 10:05:30 +000022804 [TEMPLATE_CMP8_REG] = {
22805 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22806 .rhs = {
22807 [0] = { REG_UNSET, REGCM_GPR8_LO },
22808 [1] = { REG_UNSET, REGCM_GPR8_LO },
22809 },
22810 },
22811 [TEMPLATE_CMP16_REG] = {
22812 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22813 .rhs = {
22814 [0] = { REG_UNSET, REGCM_GPR16 },
22815 [1] = { REG_UNSET, REGCM_GPR16 },
22816 },
22817 },
22818 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022819 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22820 .rhs = {
22821 [0] = { REG_UNSET, REGCM_GPR32 },
22822 [1] = { REG_UNSET, REGCM_GPR32 },
22823 },
22824 },
Eric Biederman530b5192003-07-01 10:05:30 +000022825 [TEMPLATE_CMP8_IMM] = {
22826 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22827 .rhs = {
22828 [0] = { REG_UNSET, REGCM_GPR8_LO },
22829 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22830 },
22831 },
22832 [TEMPLATE_CMP16_IMM] = {
22833 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22834 .rhs = {
22835 [0] = { REG_UNSET, REGCM_GPR16 },
22836 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22837 },
22838 },
22839 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022840 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22841 .rhs = {
22842 [0] = { REG_UNSET, REGCM_GPR32 },
22843 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22844 },
22845 },
Eric Biederman530b5192003-07-01 10:05:30 +000022846 [TEMPLATE_TEST8] = {
22847 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22848 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22849 },
22850 [TEMPLATE_TEST16] = {
22851 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22852 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22853 },
22854 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022855 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22856 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22857 },
22858 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022859 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022860 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22861 },
22862 [TEMPLATE_JMP] = {
22863 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22864 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022865 [TEMPLATE_RET] = {
22866 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22867 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022868 [TEMPLATE_INB_DX] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022869 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022870 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22871 },
22872 [TEMPLATE_INB_IMM] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022873 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022874 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22875 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022876 [TEMPLATE_INW_DX] = {
22877 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022878 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22879 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022880 [TEMPLATE_INW_IMM] = {
22881 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022882 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22883 },
22884 [TEMPLATE_INL_DX] = {
22885 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22886 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22887 },
22888 [TEMPLATE_INL_IMM] = {
22889 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22890 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22891 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022892 [TEMPLATE_OUTB_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022893 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022894 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022895 [1] = { REG_DX, REGCM_GPR16 },
22896 },
22897 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022898 [TEMPLATE_OUTB_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022899 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022900 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022901 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22902 },
22903 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022904 [TEMPLATE_OUTW_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022905 .rhs = {
22906 [0] = { REG_AX, REGCM_GPR16 },
22907 [1] = { REG_DX, REGCM_GPR16 },
22908 },
22909 },
22910 [TEMPLATE_OUTW_IMM] = {
22911 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022912 [0] = { REG_AX, REGCM_GPR16 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022913 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22914 },
22915 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022916 [TEMPLATE_OUTL_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022917 .rhs = {
22918 [0] = { REG_EAX, REGCM_GPR32 },
22919 [1] = { REG_DX, REGCM_GPR16 },
22920 },
22921 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022922 [TEMPLATE_OUTL_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022923 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022924 [0] = { REG_EAX, REGCM_GPR32 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022925 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22926 },
22927 },
22928 [TEMPLATE_BSF] = {
22929 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22930 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22931 },
22932 [TEMPLATE_RDMSR] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022933 .lhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022934 [0] = { REG_EAX, REGCM_GPR32 },
22935 [1] = { REG_EDX, REGCM_GPR32 },
22936 },
22937 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22938 },
22939 [TEMPLATE_WRMSR] = {
22940 .rhs = {
22941 [0] = { REG_ECX, REGCM_GPR32 },
22942 [1] = { REG_EAX, REGCM_GPR32 },
22943 [2] = { REG_EDX, REGCM_GPR32 },
22944 },
22945 },
Eric Biederman530b5192003-07-01 10:05:30 +000022946 [TEMPLATE_UMUL8] = {
22947 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022948 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022949 [0] = { REG_AL, REGCM_GPR8_LO },
22950 [1] = { REG_UNSET, REGCM_GPR8_LO },
22951 },
22952 },
22953 [TEMPLATE_UMUL16] = {
22954 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022955 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022956 [0] = { REG_AX, REGCM_GPR16 },
22957 [1] = { REG_UNSET, REGCM_GPR16 },
22958 },
22959 },
22960 [TEMPLATE_UMUL32] = {
22961 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022962 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022963 [0] = { REG_EAX, REGCM_GPR32 },
22964 [1] = { REG_UNSET, REGCM_GPR32 },
22965 },
22966 },
Eric Biederman530b5192003-07-01 10:05:30 +000022967 [TEMPLATE_DIV8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022968 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022969 [0] = { REG_AL, REGCM_GPR8_LO },
22970 [1] = { REG_AH, REGCM_GPR8 },
22971 },
22972 .rhs = {
22973 [0] = { REG_AX, REGCM_GPR16 },
22974 [1] = { REG_UNSET, REGCM_GPR8_LO },
22975 },
22976 },
22977 [TEMPLATE_DIV16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022978 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022979 [0] = { REG_AX, REGCM_GPR16 },
22980 [1] = { REG_DX, REGCM_GPR16 },
22981 },
22982 .rhs = {
22983 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22984 [1] = { REG_UNSET, REGCM_GPR16 },
22985 },
22986 },
22987 [TEMPLATE_DIV32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022988 .lhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022989 [0] = { REG_EAX, REGCM_GPR32 },
22990 [1] = { REG_EDX, REGCM_GPR32 },
22991 },
22992 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022993 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022994 [1] = { REG_UNSET, REGCM_GPR32 },
22995 },
22996 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022997};
Eric Biedermanb138ac82003-04-22 18:44:01 +000022998
Eric Biederman83b991a2003-10-11 06:20:25 +000022999static void fixup_branch(struct compile_state *state,
23000 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23001 struct triple *left, struct triple *right)
23002{
23003 struct triple *test;
23004 if (!left) {
23005 internal_error(state, branch, "no branch test?");
23006 }
23007 test = pre_triple(state, branch,
23008 cmp_op, cmp_type, left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000023009 test->template_id = TEMPLATE_TEST32;
Eric Biederman83b991a2003-10-11 06:20:25 +000023010 if (cmp_op == OP_CMP) {
23011 test->template_id = TEMPLATE_CMP32_REG;
23012 if (get_imm32(test, &RHS(test, 1))) {
23013 test->template_id = TEMPLATE_CMP32_IMM;
23014 }
23015 }
23016 use_triple(RHS(test, 0), test);
23017 use_triple(RHS(test, 1), test);
23018 unuse_triple(RHS(branch, 0), branch);
23019 RHS(branch, 0) = test;
23020 branch->op = jmp_op;
23021 branch->template_id = TEMPLATE_JMP;
23022 use_triple(RHS(branch, 0), branch);
23023}
23024
Eric Biedermanb138ac82003-04-22 18:44:01 +000023025static void fixup_branches(struct compile_state *state,
23026 struct triple *cmp, struct triple *use, int jmp_op)
23027{
23028 struct triple_set *entry, *next;
23029 for(entry = use->use; entry; entry = next) {
23030 next = entry->next;
23031 if (entry->member->op == OP_COPY) {
23032 fixup_branches(state, cmp, entry->member, jmp_op);
23033 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023034 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023035 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023036 struct triple *left, *right;
23037 left = right = 0;
23038 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023039 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023040 right = RHS(cmp, 1);
23041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023042 branch = entry->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023043 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023044 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023045 }
23046 }
23047}
23048
Stefan Reinauer14e22772010-04-27 06:56:47 +000023049static void bool_cmp(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023050 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23051{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023052 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023053 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023054
23055 /* Put a barrier up before the cmp which preceeds the
23056 * copy instruction. If a set actually occurs this gives
23057 * us a chance to move variables in registers out of the way.
23058 */
23059
23060 /* Modify the comparison operator */
23061 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023062 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023063 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023064 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023065 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023066 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023067 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023068 }
23069 /* Generate the instruction sequence that will transform the
23070 * result of the comparison into a logical value.
23071 */
Eric Biederman90089602004-05-28 14:11:54 +000023072 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023073 use_triple(ins, set);
23074 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023075
Eric Biederman90089602004-05-28 14:11:54 +000023076 convert = set;
23077 if (!equiv_types(ins->type, set->type)) {
23078 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23079 use_triple(set, convert);
23080 convert->template_id = TEMPLATE_COPY32_REG;
23081 }
23082
Eric Biedermanb138ac82003-04-22 18:44:01 +000023083 for(entry = ins->use; entry; entry = next) {
23084 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023085 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023086 continue;
23087 }
Eric Biederman90089602004-05-28 14:11:54 +000023088 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023089 }
Eric Biederman90089602004-05-28 14:11:54 +000023090 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023091}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023092
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023093struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23094{
23095 struct ins_template *template;
23096 struct reg_info result;
23097 int zlhs;
23098 if (ins->op == OP_PIECE) {
23099 index = ins->u.cval;
23100 ins = MISC(ins, 0);
23101 }
Eric Biederman90089602004-05-28 14:11:54 +000023102 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023103 if (triple_is_def(state, ins)) {
23104 zlhs = 1;
23105 }
23106 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023107 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023108 index, tops(ins->op));
23109 }
23110 switch(ins->op) {
23111 case OP_ASM:
23112 template = &ins->u.ainfo->tmpl;
23113 break;
23114 default:
23115 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023116 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023117 ins->template_id);
23118 }
23119 template = &templates[ins->template_id];
23120 break;
23121 }
23122 result = template->lhs[index];
23123 result.regcm = arch_regcm_normalize(state, result.regcm);
23124 if (result.reg != REG_UNNEEDED) {
23125 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23126 }
23127 if (result.regcm == 0) {
23128 internal_error(state, ins, "lhs %d regcm == 0", index);
23129 }
23130 return result;
23131}
23132
23133struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23134{
23135 struct reg_info result;
23136 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023137 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023138 (ins->op == OP_PIECE)) {
23139 internal_error(state, ins, "index %d out of range for %s\n",
23140 index, tops(ins->op));
23141 }
23142 switch(ins->op) {
23143 case OP_ASM:
23144 template = &ins->u.ainfo->tmpl;
23145 break;
Eric Biederman90089602004-05-28 14:11:54 +000023146 case OP_PHI:
23147 index = 0;
23148 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023149 default:
23150 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023151 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023152 ins->template_id);
23153 }
23154 template = &templates[ins->template_id];
23155 break;
23156 }
23157 result = template->rhs[index];
23158 result.regcm = arch_regcm_normalize(state, result.regcm);
23159 if (result.regcm == 0) {
23160 internal_error(state, ins, "rhs %d regcm == 0", index);
23161 }
23162 return result;
23163}
23164
Eric Biederman530b5192003-07-01 10:05:30 +000023165static struct triple *mod_div(struct compile_state *state,
23166 struct triple *ins, int div_op, int index)
23167{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010023168 struct triple *div, *piece1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023169
Eric Biederman530b5192003-07-01 10:05:30 +000023170 /* Generate the appropriate division instruction */
23171 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23172 RHS(div, 0) = RHS(ins, 0);
23173 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023174 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023175 div->template_id = TEMPLATE_DIV32;
23176 use_triple(RHS(div, 0), div);
23177 use_triple(RHS(div, 1), div);
23178 use_triple(LHS(div, 0), div);
23179 use_triple(LHS(div, 1), div);
23180
Eric Biederman530b5192003-07-01 10:05:30 +000023181 /* Replate uses of ins with the appropriate piece of the div */
23182 propogate_use(state, ins, LHS(div, index));
23183 release_triple(state, ins);
23184
23185 /* Return the address of the next instruction */
23186 return piece1->next;
23187}
23188
Eric Biederman90089602004-05-28 14:11:54 +000023189static int noop_adecl(struct triple *adecl)
23190{
23191 struct triple_set *use;
23192 /* It's a noop if it doesn't specify stoorage */
23193 if (adecl->lhs == 0) {
23194 return 1;
23195 }
23196 /* Is the adecl used? If not it's a noop */
23197 for(use = adecl->use; use ; use = use->next) {
23198 if ((use->member->op != OP_PIECE) ||
23199 (MISC(use->member, 0) != adecl)) {
23200 return 0;
23201 }
23202 }
23203 return 1;
23204}
23205
23206static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23207{
23208 struct triple *mask, *nmask, *shift;
23209 struct triple *val, *val_mask, *val_shift;
23210 struct triple *targ, *targ_mask;
23211 struct triple *new;
23212 ulong_t the_mask, the_nmask;
23213
23214 targ = RHS(ins, 0);
23215 val = RHS(ins, 1);
23216
23217 /* Get constant for the mask value */
23218 the_mask = 1;
23219 the_mask <<= ins->u.bitfield.size;
23220 the_mask -= 1;
23221 the_mask <<= ins->u.bitfield.offset;
23222 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23223 mask->u.cval = the_mask;
23224
23225 /* Get the inverted mask value */
23226 the_nmask = ~the_mask;
23227 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23228 nmask->u.cval = the_nmask;
23229
23230 /* Get constant for the shift value */
23231 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23232 shift->u.cval = ins->u.bitfield.offset;
23233
23234 /* Shift and mask the source value */
23235 val_shift = val;
23236 if (shift->u.cval != 0) {
23237 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23238 use_triple(val, val_shift);
23239 use_triple(shift, val_shift);
23240 }
23241 val_mask = val_shift;
23242 if (is_signed(val->type)) {
23243 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23244 use_triple(val_shift, val_mask);
23245 use_triple(mask, val_mask);
23246 }
23247
23248 /* Mask the target value */
23249 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23250 use_triple(targ, targ_mask);
23251 use_triple(nmask, targ_mask);
23252
23253 /* Now combined them together */
23254 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23255 use_triple(targ_mask, new);
23256 use_triple(val_mask, new);
23257
23258 /* Move all of the users over to the new expression */
23259 propogate_use(state, ins, new);
23260
23261 /* Delete the original triple */
23262 release_triple(state, ins);
23263
23264 /* Restart the transformation at mask */
23265 return mask;
23266}
23267
23268static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23269{
23270 struct triple *mask, *shift;
23271 struct triple *val, *val_mask, *val_shift;
23272 ulong_t the_mask;
23273
23274 val = RHS(ins, 0);
23275
23276 /* Get constant for the mask value */
23277 the_mask = 1;
23278 the_mask <<= ins->u.bitfield.size;
23279 the_mask -= 1;
23280 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23281 mask->u.cval = the_mask;
23282
23283 /* Get constant for the right shift value */
23284 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23285 shift->u.cval = ins->u.bitfield.offset;
23286
23287 /* Shift arithmetic right, to correct the sign */
23288 val_shift = val;
23289 if (shift->u.cval != 0) {
23290 int op;
23291 if (ins->op == OP_SEXTRACT) {
23292 op = OP_SSR;
23293 } else {
23294 op = OP_USR;
23295 }
23296 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23297 use_triple(val, val_shift);
23298 use_triple(shift, val_shift);
23299 }
23300
23301 /* Finally mask the value */
23302 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23303 use_triple(val_shift, val_mask);
23304 use_triple(mask, val_mask);
23305
23306 /* Move all of the users over to the new expression */
23307 propogate_use(state, ins, val_mask);
23308
23309 /* Release the original instruction */
23310 release_triple(state, ins);
23311
23312 return mask;
23313
23314}
23315
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023316static struct triple *transform_to_arch_instruction(
23317 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023318{
23319 /* Transform from generic 3 address instructions
23320 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023321 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023322 * Copies are inserted to preserve the register flexibility
23323 * of 3 address instructions.
23324 */
Eric Biederman90089602004-05-28 14:11:54 +000023325 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023326 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023327 next = ins->next;
23328 switch(ins->op) {
23329 case OP_INTCONST:
23330 ins->template_id = TEMPLATE_INTCONST32;
23331 if (ins->u.cval < 256) {
23332 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023333 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023334 break;
23335 case OP_ADDRCONST:
23336 ins->template_id = TEMPLATE_INTCONST32;
23337 break;
Eric Biederman90089602004-05-28 14:11:54 +000023338 case OP_UNKNOWNVAL:
23339 ins->template_id = TEMPLATE_UNKNOWNVAL;
23340 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023341 case OP_NOOP:
23342 case OP_SDECL:
23343 case OP_BLOBCONST:
23344 case OP_LABEL:
23345 ins->template_id = TEMPLATE_NOP;
23346 break;
23347 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023348 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023349 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023350 value = RHS(ins, 0);
23351 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023352 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023353 }
Eric Biederman90089602004-05-28 14:11:54 +000023354 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023355 ins->template_id = TEMPLATE_COPY_IMM16;
23356 }
Eric Biederman90089602004-05-28 14:11:54 +000023357 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023358 ins->template_id = TEMPLATE_COPY_IMM32;
23359 }
Eric Biederman90089602004-05-28 14:11:54 +000023360 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023361 internal_error(state, ins, "bad constant passed to copy");
23362 }
Eric Biederman90089602004-05-28 14:11:54 +000023363 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023364 ins->template_id = TEMPLATE_COPY8_REG;
23365 }
Eric Biederman90089602004-05-28 14:11:54 +000023366 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023367 ins->template_id = TEMPLATE_COPY16_REG;
23368 }
Eric Biederman90089602004-05-28 14:11:54 +000023369 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023370 ins->template_id = TEMPLATE_COPY32_REG;
23371 }
23372 else {
23373 internal_error(state, ins, "bad type passed to copy");
23374 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023375 break;
23376 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023377 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023378 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023379 ins->template_id = TEMPLATE_PHI8;
23380 }
Eric Biederman90089602004-05-28 14:11:54 +000023381 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023382 ins->template_id = TEMPLATE_PHI16;
23383 }
Eric Biederman90089602004-05-28 14:11:54 +000023384 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023385 ins->template_id = TEMPLATE_PHI32;
23386 }
23387 else {
23388 internal_error(state, ins, "bad type passed to phi");
23389 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023390 break;
Eric Biederman90089602004-05-28 14:11:54 +000023391 case OP_ADECL:
23392 /* Adecls should always be treated as dead code and
23393 * removed. If we are not optimizing they may linger.
23394 */
23395 if (!noop_adecl(ins)) {
23396 internal_error(state, ins, "adecl remains?");
23397 }
23398 ins->template_id = TEMPLATE_NOP;
23399 next = after_lhs(state, ins);
23400 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023401 case OP_STORE:
23402 switch(ins->type->type & TYPE_MASK) {
23403 case TYPE_CHAR: case TYPE_UCHAR:
23404 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023405 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023406 case TYPE_SHORT: case TYPE_USHORT:
23407 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023408 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023409 case TYPE_INT: case TYPE_UINT:
23410 case TYPE_LONG: case TYPE_ULONG:
23411 case TYPE_POINTER:
23412 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023413 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023414 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023415 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023416 break;
23417 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023418 break;
23419 case OP_LOAD:
23420 switch(ins->type->type & TYPE_MASK) {
23421 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023422 case TYPE_SHORT: case TYPE_USHORT:
23423 case TYPE_INT: case TYPE_UINT:
23424 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023425 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023426 break;
23427 default:
23428 internal_error(state, ins, "unknown type in load");
23429 break;
23430 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023431 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023432 break;
23433 case OP_ADD:
23434 case OP_SUB:
23435 case OP_AND:
23436 case OP_XOR:
23437 case OP_OR:
23438 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023439 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023440 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023441 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023442 }
23443 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023444 case OP_SDIVT:
23445 case OP_UDIVT:
23446 ins->template_id = TEMPLATE_DIV32;
23447 next = after_lhs(state, ins);
23448 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023449 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023450 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023451 break;
23452 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023453 next = mod_div(state, ins, OP_UDIVT, 0);
23454 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023455 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023456 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023457 break;
23458 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023459 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023460 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023461 case OP_SMOD:
23462 next = mod_div(state, ins, OP_SDIVT, 1);
23463 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023464 case OP_SL:
23465 case OP_SSR:
23466 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023467 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023468 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023469 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023470 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23471 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023472 }
23473 break;
23474 case OP_INVERT:
23475 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023476 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023477 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023478 case OP_EQ:
23479 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023480 break;
23481 case OP_NOTEQ:
23482 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23483 break;
23484 case OP_SLESS:
23485 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23486 break;
23487 case OP_ULESS:
23488 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23489 break;
23490 case OP_SMORE:
23491 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23492 break;
23493 case OP_UMORE:
23494 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23495 break;
23496 case OP_SLESSEQ:
23497 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23498 break;
23499 case OP_ULESSEQ:
23500 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23501 break;
23502 case OP_SMOREEQ:
23503 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23504 break;
23505 case OP_UMOREEQ:
23506 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23507 break;
23508 case OP_LTRUE:
23509 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23510 break;
23511 case OP_LFALSE:
23512 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23513 break;
23514 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023515 ins->op = OP_JMP;
23516 ins->template_id = TEMPLATE_NOP;
23517 break;
23518 case OP_CBRANCH:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023519 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023520 RHS(ins, 0)->type, RHS(ins, 0), 0);
23521 break;
23522 case OP_CALL:
23523 ins->template_id = TEMPLATE_NOP;
23524 break;
23525 case OP_RET:
23526 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023527 break;
23528 case OP_INB:
23529 case OP_INW:
23530 case OP_INL:
23531 switch(ins->op) {
23532 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23533 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23534 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23535 }
23536 if (get_imm8(ins, &RHS(ins, 0))) {
23537 ins->template_id += 1;
23538 }
23539 break;
23540 case OP_OUTB:
23541 case OP_OUTW:
23542 case OP_OUTL:
23543 switch(ins->op) {
23544 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23545 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23546 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23547 }
23548 if (get_imm8(ins, &RHS(ins, 1))) {
23549 ins->template_id += 1;
23550 }
23551 break;
23552 case OP_BSF:
23553 case OP_BSR:
23554 ins->template_id = TEMPLATE_BSF;
23555 break;
23556 case OP_RDMSR:
23557 ins->template_id = TEMPLATE_RDMSR;
23558 next = after_lhs(state, ins);
23559 break;
23560 case OP_WRMSR:
23561 ins->template_id = TEMPLATE_WRMSR;
23562 break;
23563 case OP_HLT:
23564 ins->template_id = TEMPLATE_NOP;
23565 break;
23566 case OP_ASM:
23567 ins->template_id = TEMPLATE_NOP;
23568 next = after_lhs(state, ins);
23569 break;
23570 /* Already transformed instructions */
23571 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023572 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023573 break;
23574 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023575 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023576 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023577 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023578 }
23579 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023580 case OP_JMP:
23581 ins->template_id = TEMPLATE_NOP;
23582 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023583 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23584 case OP_JMP_SLESS: case OP_JMP_ULESS:
23585 case OP_JMP_SMORE: case OP_JMP_UMORE:
23586 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23587 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23588 ins->template_id = TEMPLATE_JMP;
23589 break;
23590 case OP_SET_EQ: case OP_SET_NOTEQ:
23591 case OP_SET_SLESS: case OP_SET_ULESS:
23592 case OP_SET_SMORE: case OP_SET_UMORE:
23593 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23594 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23595 ins->template_id = TEMPLATE_SET;
23596 break;
Eric Biederman90089602004-05-28 14:11:54 +000023597 case OP_DEPOSIT:
23598 next = x86_deposit(state, ins);
23599 break;
23600 case OP_SEXTRACT:
23601 case OP_UEXTRACT:
23602 next = x86_extract(state, ins);
23603 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023604 /* Unhandled instructions */
23605 case OP_PIECE:
23606 default:
Eric Biederman90089602004-05-28 14:11:54 +000023607 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023608 ins->op, tops(ins->op));
23609 break;
23610 }
23611 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023612}
23613
Eric Biederman530b5192003-07-01 10:05:30 +000023614static long next_label(struct compile_state *state)
23615{
Eric Biederman90089602004-05-28 14:11:54 +000023616 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023617 return ++label_counter;
23618}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023619static void generate_local_labels(struct compile_state *state)
23620{
23621 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023622 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023623 label = first;
23624 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023625 if ((label->op == OP_LABEL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000023626 (label->op == OP_SDECL)) {
23627 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023628 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023629 } else {
23630 label->u.cval = 0;
23631 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023632
Eric Biedermanb138ac82003-04-22 18:44:01 +000023633 }
23634 label = label->next;
23635 } while(label != first);
23636}
23637
Stefan Reinauer14e22772010-04-27 06:56:47 +000023638static int check_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023639 struct triple *triple, int classes)
23640{
23641 unsigned mask;
23642 int reg;
23643 reg = ID_REG(triple->id);
23644 if (reg == REG_UNSET) {
23645 internal_error(state, triple, "register not set");
23646 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023647 mask = arch_reg_regcm(state, reg);
23648 if (!(classes & mask)) {
23649 internal_error(state, triple, "reg %d in wrong class",
23650 reg);
23651 }
23652 return reg;
23653}
23654
Eric Biederman90089602004-05-28 14:11:54 +000023655
Eric Biederman530b5192003-07-01 10:05:30 +000023656#if REG_XMM7 != 44
23657#error "Registers have renumberd fix arch_reg_str"
23658#endif
Eric Biederman90089602004-05-28 14:11:54 +000023659static const char *arch_regs[] = {
23660 "%unset",
23661 "%unneeded",
23662 "%eflags",
23663 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23664 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23665 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23666 "%edx:%eax",
23667 "%dx:%ax",
23668 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023669 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
Eric Biederman90089602004-05-28 14:11:54 +000023670 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23671};
23672static const char *arch_reg_str(int reg)
23673{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023674 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23675 reg = 0;
23676 }
Eric Biederman90089602004-05-28 14:11:54 +000023677 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023678}
23679
23680static const char *reg(struct compile_state *state, struct triple *triple,
23681 int classes)
23682{
23683 int reg;
23684 reg = check_reg(state, triple, classes);
23685 return arch_reg_str(reg);
23686}
23687
Eric Biederman90089602004-05-28 14:11:54 +000023688static int arch_reg_size(int reg)
23689{
23690 int size;
23691 size = 0;
23692 if (reg == REG_EFLAGS) {
23693 size = 32;
23694 }
23695 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23696 size = 8;
23697 }
23698 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23699 size = 16;
23700 }
23701 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23702 size = 32;
23703 }
23704 else if (reg == REG_EDXEAX) {
23705 size = 64;
23706 }
23707 else if (reg == REG_DXAX) {
23708 size = 32;
23709 }
23710 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23711 size = 64;
23712 }
23713 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23714 size = 128;
23715 }
23716 return size;
23717}
23718
23719static int reg_size(struct compile_state *state, struct triple *ins)
23720{
23721 int reg;
23722 reg = ID_REG(ins->id);
23723 if (reg == REG_UNSET) {
23724 internal_error(state, ins, "register not set");
23725 }
23726 return arch_reg_size(reg);
23727}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023728
Eric Biederman90089602004-05-28 14:11:54 +000023729
23730
Eric Biedermanb138ac82003-04-22 18:44:01 +000023731const char *type_suffix(struct compile_state *state, struct type *type)
23732{
23733 const char *suffix;
23734 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023735 case SIZEOF_I8: suffix = "b"; break;
23736 case SIZEOF_I16: suffix = "w"; break;
23737 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023738 default:
23739 internal_error(state, 0, "unknown suffix");
23740 suffix = 0;
23741 break;
23742 }
23743 return suffix;
23744}
23745
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023746static void print_const_val(
23747 struct compile_state *state, struct triple *ins, FILE *fp)
23748{
23749 switch(ins->op) {
23750 case OP_INTCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023751 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023752 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023753 break;
23754 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023755 if ((MISC(ins, 0)->op != OP_SDECL) &&
23756 (MISC(ins, 0)->op != OP_LABEL))
23757 {
Eric Biederman830c9882003-07-04 00:27:33 +000023758 internal_error(state, ins, "bad base for addrconst");
23759 }
23760 if (MISC(ins, 0)->u.cval <= 0) {
23761 internal_error(state, ins, "unlabeled constant");
23762 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023763 fprintf(fp, " $L%s%lu+%lu ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023764 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023765 (unsigned long)(MISC(ins, 0)->u.cval),
23766 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023767 break;
23768 default:
23769 internal_error(state, ins, "unknown constant type");
23770 break;
23771 }
23772}
23773
Eric Biederman530b5192003-07-01 10:05:30 +000023774static void print_const(struct compile_state *state,
23775 struct triple *ins, FILE *fp)
23776{
23777 switch(ins->op) {
23778 case OP_INTCONST:
23779 switch(ins->type->type & TYPE_MASK) {
23780 case TYPE_CHAR:
23781 case TYPE_UCHAR:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023782 fprintf(fp, ".byte 0x%02lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023783 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023784 break;
23785 case TYPE_SHORT:
23786 case TYPE_USHORT:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023787 fprintf(fp, ".short 0x%04lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023788 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023789 break;
23790 case TYPE_INT:
23791 case TYPE_UINT:
23792 case TYPE_LONG:
23793 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023794 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023795 fprintf(fp, ".int %lu\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023796 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023797 break;
23798 default:
Eric Biederman90089602004-05-28 14:11:54 +000023799 fprintf(state->errout, "type: ");
23800 name_of(state->errout, ins->type);
23801 fprintf(state->errout, "\n");
23802 internal_error(state, ins, "Unknown constant type. Val: %lu",
23803 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023804 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023805
Eric Biederman530b5192003-07-01 10:05:30 +000023806 break;
23807 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023808 if ((MISC(ins, 0)->op != OP_SDECL) &&
23809 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023810 internal_error(state, ins, "bad base for addrconst");
23811 }
23812 if (MISC(ins, 0)->u.cval <= 0) {
23813 internal_error(state, ins, "unlabeled constant");
23814 }
23815 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023816 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023817 (unsigned long)(MISC(ins, 0)->u.cval),
23818 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023819 break;
23820 case OP_BLOBCONST:
23821 {
23822 unsigned char *blob;
23823 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023824 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023825 blob = ins->u.blob;
23826 for(i = 0; i < size; i++) {
23827 fprintf(fp, ".byte 0x%02x\n",
23828 blob[i]);
23829 }
23830 break;
23831 }
23832 default:
23833 internal_error(state, ins, "Unknown constant type");
23834 break;
23835 }
23836}
23837
23838#define TEXT_SECTION ".rom.text"
23839#define DATA_SECTION ".rom.data"
23840
23841static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023842 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023843{
Eric Biederman90089602004-05-28 14:11:54 +000023844 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023845 long ref;
23846 ref = next_label(state);
23847 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023848 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023849 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023850 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023851 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23852 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023853 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023854 }
Eric Biederman530b5192003-07-01 10:05:30 +000023855 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23856 return ref;
23857}
23858
Eric Biederman90089602004-05-28 14:11:54 +000023859static long get_mask_pool_ref(
23860 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23861{
23862 long ref;
23863 if (mask == 0xff) {
23864 ref = 1;
23865 }
23866 else if (mask == 0xffff) {
23867 ref = 2;
23868 }
23869 else {
23870 ref = 0;
23871 internal_error(state, ins, "unhandled mask value");
23872 }
23873 return ref;
23874}
23875
Eric Biedermanb138ac82003-04-22 18:44:01 +000023876static void print_binary_op(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000023877 const char *op, struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023878{
23879 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023880 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023881 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023882 internal_error(state, ins, "invalid register assignment");
23883 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023884 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023885 fprintf(fp, "\t%s ", op);
23886 print_const_val(state, RHS(ins, 1), fp);
23887 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023888 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023889 }
23890 else {
23891 unsigned lmask, rmask;
23892 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023893 lreg = check_reg(state, RHS(ins, 0), mask);
23894 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023895 lmask = arch_reg_regcm(state, lreg);
23896 rmask = arch_reg_regcm(state, rreg);
23897 mask = lmask & rmask;
23898 fprintf(fp, "\t%s %s, %s\n",
23899 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023900 reg(state, RHS(ins, 1), mask),
23901 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023902 }
23903}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023904static void print_unary_op(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023905 const char *op, struct triple *ins, FILE *fp)
23906{
23907 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023908 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023909 fprintf(fp, "\t%s %s\n",
23910 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023911 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023912}
23913
23914static void print_op_shift(struct compile_state *state,
23915 const char *op, struct triple *ins, FILE *fp)
23916{
23917 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023918 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023919 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023920 internal_error(state, ins, "invalid register assignment");
23921 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023922 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023923 fprintf(fp, "\t%s ", op);
23924 print_const_val(state, RHS(ins, 1), fp);
23925 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023926 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023927 }
23928 else {
23929 fprintf(fp, "\t%s %s, %s\n",
23930 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023931 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023932 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023933 }
23934}
23935
23936static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23937{
23938 const char *op;
23939 int mask;
23940 int dreg;
23941 mask = 0;
23942 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023943 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023944 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23945 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23946 default:
23947 internal_error(state, ins, "not an in operation");
23948 op = 0;
23949 break;
23950 }
23951 dreg = check_reg(state, ins, mask);
23952 if (!reg_is_reg(state, dreg, REG_EAX)) {
23953 internal_error(state, ins, "dst != %%eax");
23954 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023955 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023956 fprintf(fp, "\t%s ", op);
23957 print_const_val(state, RHS(ins, 0), fp);
23958 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023959 reg(state, ins, mask));
23960 }
23961 else {
23962 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023963 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023964 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23965 internal_error(state, ins, "src != %%dx");
23966 }
23967 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023968 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023969 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023970 reg(state, ins, mask));
23971 }
23972}
23973
23974static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23975{
23976 const char *op;
23977 int mask;
23978 int lreg;
23979 mask = 0;
23980 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023981 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023982 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23983 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23984 default:
23985 internal_error(state, ins, "not an out operation");
23986 op = 0;
23987 break;
23988 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023989 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023990 if (!reg_is_reg(state, lreg, REG_EAX)) {
23991 internal_error(state, ins, "src != %%eax");
23992 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023993 if (is_const(RHS(ins, 1))) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023994 fprintf(fp, "\t%s %s,",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023995 op, reg(state, RHS(ins, 0), mask));
23996 print_const_val(state, RHS(ins, 1), fp);
23997 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023998 }
23999 else {
24000 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024001 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024002 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24003 internal_error(state, ins, "dst != %%dx");
24004 }
24005 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024006 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024007 reg(state, RHS(ins, 0), mask),
24008 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024009 }
24010}
24011
24012static void print_op_move(struct compile_state *state,
24013 struct triple *ins, FILE *fp)
24014{
24015 /* op_move is complex because there are many types
24016 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024017 * Because OP_COPY will be introduced in arbitrary locations
24018 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024019 * OP_CONVERT can change the flags and it is the only operation
24020 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024021 */
24022 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24023 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024024 if (state->arch->features & X86_NOOP_COPY) {
24025 omit_copy = 0;
24026 }
24027 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024028 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024029 dst = ins;
24030 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024031 else {
24032 internal_error(state, ins, "unknown move operation");
24033 src = dst = 0;
24034 }
Eric Biederman90089602004-05-28 14:11:54 +000024035 if (reg_size(state, dst) < size_of(state, dst->type)) {
24036 internal_error(state, ins, "Invalid destination register");
24037 }
24038 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24039 fprintf(state->errout, "src type: ");
24040 name_of(state->errout, src->type);
24041 fprintf(state->errout, "\n");
24042 fprintf(state->errout, "dst type: ");
24043 name_of(state->errout, dst->type);
24044 fprintf(state->errout, "\n");
24045 internal_error(state, ins, "Type mismatch for OP_COPY");
24046 }
24047
Eric Biederman0babc1c2003-05-09 02:39:00 +000024048 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024049 int src_reg, dst_reg;
24050 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024051 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024052 dst_reg = ID_REG(dst->id);
24053 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024054 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024055 /* If the class is the same just move the register */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024056 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024057 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024058 if ((src_reg != dst_reg) || !omit_copy) {
24059 fprintf(fp, "\tmov %s, %s\n",
24060 reg(state, src, src_regcm),
24061 reg(state, dst, dst_regcm));
24062 }
24063 }
24064 /* Move 32bit to 16bit */
24065 else if ((src_regcm & REGCM_GPR32) &&
24066 (dst_regcm & REGCM_GPR16)) {
24067 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24068 if ((src_reg != dst_reg) || !omit_copy) {
24069 fprintf(fp, "\tmovw %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024070 arch_reg_str(src_reg),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024071 arch_reg_str(dst_reg));
24072 }
24073 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024074 /* Move from 32bit gprs to 16bit gprs */
24075 else if ((src_regcm & REGCM_GPR32) &&
24076 (dst_regcm & REGCM_GPR16)) {
24077 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24078 if ((src_reg != dst_reg) || !omit_copy) {
24079 fprintf(fp, "\tmov %s, %s\n",
24080 arch_reg_str(src_reg),
24081 arch_reg_str(dst_reg));
24082 }
24083 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024084 /* Move 32bit to 8bit */
24085 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024086 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024087 {
24088 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24089 if ((src_reg != dst_reg) || !omit_copy) {
24090 fprintf(fp, "\tmovb %s, %s\n",
24091 arch_reg_str(src_reg),
24092 arch_reg_str(dst_reg));
24093 }
24094 }
24095 /* Move 16bit to 8bit */
24096 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024097 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024098 {
24099 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24100 if ((src_reg != dst_reg) || !omit_copy) {
24101 fprintf(fp, "\tmovb %s, %s\n",
24102 arch_reg_str(src_reg),
24103 arch_reg_str(dst_reg));
24104 }
24105 }
24106 /* Move 8/16bit to 16/32bit */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024107 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024108 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024109 const char *op;
24110 op = is_signed(src->type)? "movsx": "movzx";
24111 fprintf(fp, "\t%s %s, %s\n",
24112 op,
24113 reg(state, src, src_regcm),
24114 reg(state, dst, dst_regcm));
24115 }
24116 /* Move between sse registers */
24117 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24118 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024119 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024120 reg(state, src, src_regcm),
24121 reg(state, dst, dst_regcm));
24122 }
24123 }
Eric Biederman530b5192003-07-01 10:05:30 +000024124 /* Move between mmx registers */
24125 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024126 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024127 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024128 reg(state, src, src_regcm),
24129 reg(state, dst, dst_regcm));
24130 }
24131 }
Eric Biederman530b5192003-07-01 10:05:30 +000024132 /* Move from sse to mmx registers */
24133 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24134 fprintf(fp, "\tmovdq2q %s, %s\n",
24135 reg(state, src, src_regcm),
24136 reg(state, dst, dst_regcm));
24137 }
24138 /* Move from mmx to sse registers */
24139 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24140 fprintf(fp, "\tmovq2dq %s, %s\n",
24141 reg(state, src, src_regcm),
24142 reg(state, dst, dst_regcm));
24143 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024144 /* Move between 32bit gprs & mmx/sse registers */
24145 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24146 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24147 fprintf(fp, "\tmovd %s, %s\n",
24148 reg(state, src, src_regcm),
24149 reg(state, dst, dst_regcm));
24150 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024151 /* Move from 16bit gprs & mmx/sse registers */
24152 else if ((src_regcm & REGCM_GPR16) &&
24153 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24154 const char *op;
24155 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024156 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024157 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24158 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24159 op,
24160 arch_reg_str(src_reg),
24161 arch_reg_str(mid_reg),
24162 arch_reg_str(mid_reg),
24163 arch_reg_str(dst_reg));
24164 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024165 /* Move from mmx/sse registers to 16bit gprs */
24166 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24167 (dst_regcm & REGCM_GPR16)) {
24168 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24169 fprintf(fp, "\tmovd %s, %s\n",
24170 arch_reg_str(src_reg),
24171 arch_reg_str(dst_reg));
24172 }
Eric Biederman530b5192003-07-01 10:05:30 +000024173 /* Move from gpr to 64bit dividend */
24174 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24175 (dst_regcm & REGCM_DIVIDEND64)) {
24176 const char *extend;
24177 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24178 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024179 arch_reg_str(src_reg),
Eric Biederman530b5192003-07-01 10:05:30 +000024180 extend);
24181 }
24182 /* Move from 64bit gpr to gpr */
24183 else if ((src_regcm & REGCM_DIVIDEND64) &&
24184 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24185 if (dst_regcm & REGCM_GPR32) {
24186 src_reg = REG_EAX;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024187 }
Eric Biederman530b5192003-07-01 10:05:30 +000024188 else if (dst_regcm & REGCM_GPR16) {
24189 src_reg = REG_AX;
24190 }
24191 else if (dst_regcm & REGCM_GPR8_LO) {
24192 src_reg = REG_AL;
24193 }
24194 fprintf(fp, "\tmov %s, %s\n",
24195 arch_reg_str(src_reg),
24196 arch_reg_str(dst_reg));
24197 }
24198 /* Move from mmx/sse registers to 64bit gpr */
24199 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24200 (dst_regcm & REGCM_DIVIDEND64)) {
24201 const char *extend;
24202 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24203 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24204 arch_reg_str(src_reg),
24205 extend);
24206 }
24207 /* Move from 64bit gpr to mmx/sse register */
24208 else if ((src_regcm & REGCM_DIVIDEND64) &&
24209 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24210 fprintf(fp, "\tmovd %%eax, %s\n",
24211 arch_reg_str(dst_reg));
24212 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024213#if X86_4_8BIT_GPRS
24214 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024215 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024216 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24217 const char *op;
24218 int mid_reg;
24219 op = is_signed(src->type)? "movsx":"movzx";
24220 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24221 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24222 op,
24223 reg(state, src, src_regcm),
24224 arch_reg_str(mid_reg),
24225 arch_reg_str(mid_reg),
24226 reg(state, dst, dst_regcm));
24227 }
24228 /* Move from mmx/sse registers and 8bit gprs */
24229 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024230 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024231 int mid_reg;
24232 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24233 fprintf(fp, "\tmovd %s, %s\n",
24234 reg(state, src, src_regcm),
24235 arch_reg_str(mid_reg));
24236 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024237 /* Move from 32bit gprs to 8bit gprs */
24238 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024239 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024240 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24241 if ((src_reg != dst_reg) || !omit_copy) {
24242 fprintf(fp, "\tmov %s, %s\n",
24243 arch_reg_str(src_reg),
24244 arch_reg_str(dst_reg));
24245 }
24246 }
24247 /* Move from 16bit gprs to 8bit gprs */
24248 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024249 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024250 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24251 if ((src_reg != dst_reg) || !omit_copy) {
24252 fprintf(fp, "\tmov %s, %s\n",
24253 arch_reg_str(src_reg),
24254 arch_reg_str(dst_reg));
24255 }
24256 }
24257#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024258 /* Move from %eax:%edx to %eax:%edx */
24259 else if ((src_regcm & REGCM_DIVIDEND64) &&
24260 (dst_regcm & REGCM_DIVIDEND64) &&
24261 (src_reg == dst_reg)) {
24262 if (!omit_copy) {
24263 fprintf(fp, "\t/*mov %s, %s*/\n",
24264 arch_reg_str(src_reg),
24265 arch_reg_str(dst_reg));
24266 }
24267 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024268 else {
Eric Biederman90089602004-05-28 14:11:54 +000024269 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24270 internal_error(state, ins, "attempt to copy from %%eflags!");
24271 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024272 internal_error(state, ins, "unknown copy type");
24273 }
24274 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024275 else {
Eric Biederman90089602004-05-28 14:11:54 +000024276 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024277 int dst_reg;
24278 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024279 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024280 dst_reg = ID_REG(dst->id);
24281 dst_regcm = arch_reg_regcm(state, dst_reg);
24282 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24283 fprintf(fp, "\tmov ");
24284 print_const_val(state, src, fp);
24285 fprintf(fp, ", %s\n",
24286 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24287 }
24288 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024289 if (dst_size > SIZEOF_I32) {
24290 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024291 }
24292 fprintf(fp, "\tmov $0, %%edx\n");
24293 fprintf(fp, "\tmov ");
24294 print_const_val(state, src, fp);
24295 fprintf(fp, ", %%eax\n");
24296 }
24297 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024298 if (dst_size > SIZEOF_I16) {
24299 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024300 }
24301 fprintf(fp, "\tmov $0, %%dx\n");
24302 fprintf(fp, "\tmov ");
24303 print_const_val(state, src, fp);
24304 fprintf(fp, ", %%ax");
24305 }
24306 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24307 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024308 if (dst_size > SIZEOF_I32) {
24309 internal_error(state, ins, "%d bit constant...", dst_size);
24310 }
24311 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024312 fprintf(fp, "\tmovd L%s%lu, %s\n",
24313 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024314 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24315 }
24316 else {
24317 internal_error(state, ins, "unknown copy immediate type");
24318 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024319 }
Eric Biederman90089602004-05-28 14:11:54 +000024320 /* Leave now if this is not a type conversion */
24321 if (ins->op != OP_CONVERT) {
24322 return;
24323 }
24324 /* Now make certain I have not logically overflowed the destination */
24325 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24326 (size_of(state, dst->type) < reg_size(state, dst)))
24327 {
24328 unsigned long mask;
24329 int dst_reg;
24330 int dst_regcm;
24331 if (size_of(state, dst->type) >= 32) {
24332 fprintf(state->errout, "dst type: ");
24333 name_of(state->errout, dst->type);
24334 fprintf(state->errout, "\n");
24335 internal_error(state, dst, "unhandled dst type size");
24336 }
24337 mask = 1;
24338 mask <<= size_of(state, dst->type);
24339 mask -= 1;
24340
24341 dst_reg = ID_REG(dst->id);
24342 dst_regcm = arch_reg_regcm(state, dst_reg);
24343
24344 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24345 fprintf(fp, "\tand $0x%lx, %s\n",
24346 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24347 }
24348 else if (dst_regcm & REGCM_MMX) {
24349 long ref;
24350 ref = get_mask_pool_ref(state, dst, mask, fp);
24351 fprintf(fp, "\tpand L%s%lu, %s\n",
24352 state->compiler->label_prefix, ref,
24353 reg(state, dst, REGCM_MMX));
24354 }
24355 else if (dst_regcm & REGCM_XMM) {
24356 long ref;
24357 ref = get_mask_pool_ref(state, dst, mask, fp);
24358 fprintf(fp, "\tpand L%s%lu, %s\n",
24359 state->compiler->label_prefix, ref,
24360 reg(state, dst, REGCM_XMM));
24361 }
24362 else {
24363 fprintf(state->errout, "dst type: ");
24364 name_of(state->errout, dst->type);
24365 fprintf(state->errout, "\n");
24366 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24367 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24368 }
24369 }
24370 /* Make certain I am properly sign extended */
24371 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24372 (is_signed(src->type)))
24373 {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010024374 int reg_bits, shift_bits;
Eric Biederman90089602004-05-28 14:11:54 +000024375 int dst_reg;
24376 int dst_regcm;
24377
Eric Biederman90089602004-05-28 14:11:54 +000024378 reg_bits = reg_size(state, dst);
24379 if (reg_bits > 32) {
24380 reg_bits = 32;
24381 }
24382 shift_bits = reg_bits - size_of(state, src->type);
24383 dst_reg = ID_REG(dst->id);
24384 dst_regcm = arch_reg_regcm(state, dst_reg);
24385
24386 if (shift_bits < 0) {
24387 internal_error(state, dst, "negative shift?");
24388 }
24389
24390 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024391 fprintf(fp, "\tshl $%d, %s\n",
24392 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024393 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024394 fprintf(fp, "\tsar $%d, %s\n",
24395 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024396 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24397 }
24398 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24399 fprintf(fp, "\tpslld $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024400 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024401 reg(state, dst, REGCM_MMX | REGCM_XMM));
24402 fprintf(fp, "\tpsrad $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024403 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024404 reg(state, dst, REGCM_MMX | REGCM_XMM));
24405 }
24406 else {
24407 fprintf(state->errout, "dst type: ");
24408 name_of(state->errout, dst->type);
24409 fprintf(state->errout, "\n");
24410 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24411 internal_error(state, dst, "failed to signed extend value");
24412 }
24413 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024414}
24415
24416static void print_op_load(struct compile_state *state,
24417 struct triple *ins, FILE *fp)
24418{
24419 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024420 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024421 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024422 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024423 if (is_const(src) || is_const(dst)) {
24424 internal_error(state, ins, "unknown load operation");
24425 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024426 switch(ins->type->type & TYPE_MASK) {
24427 case TYPE_CHAR: op = "movsbl"; break;
24428 case TYPE_UCHAR: op = "movzbl"; break;
24429 case TYPE_SHORT: op = "movswl"; break;
24430 case TYPE_USHORT: op = "movzwl"; break;
24431 case TYPE_INT: case TYPE_UINT:
24432 case TYPE_LONG: case TYPE_ULONG:
24433 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024434 op = "movl";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024435 break;
24436 default:
24437 internal_error(state, ins, "unknown type in load");
24438 op = "<invalid opcode>";
24439 break;
24440 }
24441 fprintf(fp, "\t%s (%s), %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024442 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024443 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024444 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024445}
24446
24447
24448static void print_op_store(struct compile_state *state,
24449 struct triple *ins, FILE *fp)
24450{
24451 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024452 dst = RHS(ins, 0);
24453 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024454 if (is_const(src) && (src->op == OP_INTCONST)) {
24455 long_t value;
24456 value = (long_t)(src->u.cval);
24457 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24458 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024459 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024460 reg(state, dst, REGCM_GPR32));
24461 }
24462 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24463 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24464 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024465 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024466 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024467 }
24468 else {
24469 if (is_const(src) || is_const(dst)) {
24470 internal_error(state, ins, "unknown store operation");
24471 }
24472 fprintf(fp, "\tmov%s %s, (%s)\n",
24473 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024474 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024475 reg(state, dst, REGCM_GPR32));
24476 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024477
24478
Eric Biedermanb138ac82003-04-22 18:44:01 +000024479}
24480
24481static void print_op_smul(struct compile_state *state,
24482 struct triple *ins, FILE *fp)
24483{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024484 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024485 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024486 reg(state, RHS(ins, 1), REGCM_GPR32),
24487 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024488 }
24489 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024490 fprintf(fp, "\timul ");
24491 print_const_val(state, RHS(ins, 1), fp);
24492 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024493 }
24494}
24495
24496static void print_op_cmp(struct compile_state *state,
24497 struct triple *ins, FILE *fp)
24498{
24499 unsigned mask;
24500 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024501 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024502 dreg = check_reg(state, ins, REGCM_FLAGS);
24503 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24504 internal_error(state, ins, "bad dest register for cmp");
24505 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024506 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024507 fprintf(fp, "\tcmp ");
24508 print_const_val(state, RHS(ins, 1), fp);
24509 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024510 }
24511 else {
24512 unsigned lmask, rmask;
24513 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024514 lreg = check_reg(state, RHS(ins, 0), mask);
24515 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024516 lmask = arch_reg_regcm(state, lreg);
24517 rmask = arch_reg_regcm(state, rreg);
24518 mask = lmask & rmask;
24519 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024520 reg(state, RHS(ins, 1), mask),
24521 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024522 }
24523}
24524
24525static void print_op_test(struct compile_state *state,
24526 struct triple *ins, FILE *fp)
24527{
24528 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024529 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024530 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024531 reg(state, RHS(ins, 0), mask),
24532 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024533}
24534
24535static void print_op_branch(struct compile_state *state,
24536 struct triple *branch, FILE *fp)
24537{
24538 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024539 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024540 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024541 internal_error(state, branch, "jmp with condition?");
24542 }
24543 bop = "jmp";
24544 }
24545 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024546 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024547 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024548 internal_error(state, branch, "jmpcc without condition?");
24549 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024550 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24551 if ((RHS(branch, 0)->op != OP_CMP) &&
24552 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024553 internal_error(state, branch, "bad branch test");
24554 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024555#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024556#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024557#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024558 ptr = RHS(branch, 0);
24559 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24560 if (ptr->op != OP_COPY) {
24561 internal_error(state, branch, "branch does not follow test");
24562 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024563 }
24564 switch(branch->op) {
24565 case OP_JMP_EQ: bop = "jz"; break;
24566 case OP_JMP_NOTEQ: bop = "jnz"; break;
24567 case OP_JMP_SLESS: bop = "jl"; break;
24568 case OP_JMP_ULESS: bop = "jb"; break;
24569 case OP_JMP_SMORE: bop = "jg"; break;
24570 case OP_JMP_UMORE: bop = "ja"; break;
24571 case OP_JMP_SLESSEQ: bop = "jle"; break;
24572 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24573 case OP_JMP_SMOREEQ: bop = "jge"; break;
24574 case OP_JMP_UMOREEQ: bop = "jae"; break;
24575 default:
24576 internal_error(state, branch, "Invalid branch op");
24577 break;
24578 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024579
Eric Biedermanb138ac82003-04-22 18:44:01 +000024580 }
Eric Biederman90089602004-05-28 14:11:54 +000024581#if 1
24582 if (branch->op == OP_CALL) {
24583 fprintf(fp, "\t/* call */\n");
24584 }
24585#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024586 fprintf(fp, "\t%s L%s%lu\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024587 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024588 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024589 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024590}
24591
Eric Biederman5ade04a2003-10-22 04:03:46 +000024592static void print_op_ret(struct compile_state *state,
24593 struct triple *branch, FILE *fp)
24594{
24595 fprintf(fp, "\tjmp *%s\n",
24596 reg(state, RHS(branch, 0), REGCM_GPR32));
24597}
24598
Eric Biedermanb138ac82003-04-22 18:44:01 +000024599static void print_op_set(struct compile_state *state,
24600 struct triple *set, FILE *fp)
24601{
24602 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024603 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024604 internal_error(state, set, "setcc without condition?");
24605 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024606 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24607 if ((RHS(set, 0)->op != OP_CMP) &&
24608 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024609 internal_error(state, set, "bad set test");
24610 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024611 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024612 internal_error(state, set, "set does not follow test");
24613 }
24614 switch(set->op) {
24615 case OP_SET_EQ: sop = "setz"; break;
24616 case OP_SET_NOTEQ: sop = "setnz"; break;
24617 case OP_SET_SLESS: sop = "setl"; break;
24618 case OP_SET_ULESS: sop = "setb"; break;
24619 case OP_SET_SMORE: sop = "setg"; break;
24620 case OP_SET_UMORE: sop = "seta"; break;
24621 case OP_SET_SLESSEQ: sop = "setle"; break;
24622 case OP_SET_ULESSEQ: sop = "setbe"; break;
24623 case OP_SET_SMOREEQ: sop = "setge"; break;
24624 case OP_SET_UMOREEQ: sop = "setae"; break;
24625 default:
24626 internal_error(state, set, "Invalid set op");
24627 break;
24628 }
24629 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024630 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024631}
24632
Stefan Reinauer14e22772010-04-27 06:56:47 +000024633static void print_op_bit_scan(struct compile_state *state,
24634 struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024635{
24636 const char *op;
24637 switch(ins->op) {
24638 case OP_BSF: op = "bsf"; break;
24639 case OP_BSR: op = "bsr"; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024640 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024641 internal_error(state, ins, "unknown bit scan");
24642 op = 0;
24643 break;
24644 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024645 fprintf(fp,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024646 "\t%s %s, %s\n"
24647 "\tjnz 1f\n"
24648 "\tmovl $-1, %s\n"
24649 "1:\n",
24650 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024651 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024652 reg(state, ins, REGCM_GPR32),
24653 reg(state, ins, REGCM_GPR32));
24654}
24655
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024656
Eric Biedermanb138ac82003-04-22 18:44:01 +000024657static void print_sdecl(struct compile_state *state,
24658 struct triple *ins, FILE *fp)
24659{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024660 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024661 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024662 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024663 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024664 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024665 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000024666
Eric Biedermanb138ac82003-04-22 18:44:01 +000024667}
24668
24669static void print_instruction(struct compile_state *state,
24670 struct triple *ins, FILE *fp)
24671{
24672 /* Assumption: after I have exted the register allocator
Stefan Reinauer14e22772010-04-27 06:56:47 +000024673 * everything is in a valid register.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024674 */
24675 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024676 case OP_ASM:
24677 print_op_asm(state, ins, fp);
24678 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024679 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24680 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24681 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24682 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24683 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24684 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24685 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24686 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24687 case OP_POS: break;
24688 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24689 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024690 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024691 case OP_INTCONST:
24692 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024693 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024694 /* Don't generate anything here for constants */
24695 case OP_PHI:
24696 /* Don't generate anything for variable declarations. */
24697 break;
Eric Biederman90089602004-05-28 14:11:54 +000024698 case OP_UNKNOWNVAL:
24699 fprintf(fp, " /* unknown %s */\n",
24700 reg(state, ins, REGCM_ALL));
24701 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024702 case OP_SDECL:
24703 print_sdecl(state, ins, fp);
24704 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024705 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024706 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024707 print_op_move(state, ins, fp);
24708 break;
24709 case OP_LOAD:
24710 print_op_load(state, ins, fp);
24711 break;
24712 case OP_STORE:
24713 print_op_store(state, ins, fp);
24714 break;
24715 case OP_SMUL:
24716 print_op_smul(state, ins, fp);
24717 break;
24718 case OP_CMP: print_op_cmp(state, ins, fp); break;
24719 case OP_TEST: print_op_test(state, ins, fp); break;
24720 case OP_JMP:
24721 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24722 case OP_JMP_SLESS: case OP_JMP_ULESS:
24723 case OP_JMP_SMORE: case OP_JMP_UMORE:
24724 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24725 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024726 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024727 print_op_branch(state, ins, fp);
24728 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024729 case OP_RET:
24730 print_op_ret(state, ins, fp);
24731 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024732 case OP_SET_EQ: case OP_SET_NOTEQ:
24733 case OP_SET_SLESS: case OP_SET_ULESS:
24734 case OP_SET_SMORE: case OP_SET_UMORE:
24735 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24736 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24737 print_op_set(state, ins, fp);
24738 break;
24739 case OP_INB: case OP_INW: case OP_INL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024740 print_op_in(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024741 break;
24742 case OP_OUTB: case OP_OUTW: case OP_OUTL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024743 print_op_out(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024744 break;
24745 case OP_BSF:
24746 case OP_BSR:
24747 print_op_bit_scan(state, ins, fp);
24748 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024749 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024750 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024751 fprintf(fp, "\trdmsr\n");
24752 break;
24753 case OP_WRMSR:
24754 fprintf(fp, "\twrmsr\n");
24755 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024756 case OP_HLT:
24757 fprintf(fp, "\thlt\n");
24758 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024759 case OP_SDIVT:
24760 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24761 break;
24762 case OP_UDIVT:
24763 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24764 break;
24765 case OP_UMUL:
24766 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24767 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024768 case OP_LABEL:
24769 if (!ins->use) {
24770 return;
24771 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024772 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024773 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024774 break;
Eric Biederman90089602004-05-28 14:11:54 +000024775 case OP_ADECL:
24776 /* Ignore adecls with no registers error otherwise */
24777 if (!noop_adecl(ins)) {
24778 internal_error(state, ins, "adecl remains?");
24779 }
24780 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024781 /* Ignore OP_PIECE */
24782 case OP_PIECE:
24783 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024784 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024785 case OP_SDIV: case OP_UDIV:
24786 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024787 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24788 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24789 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24790 default:
24791 internal_error(state, ins, "unknown op: %d %s",
24792 ins->op, tops(ins->op));
24793 break;
24794 }
24795}
24796
24797static void print_instructions(struct compile_state *state)
24798{
24799 struct triple *first, *ins;
24800 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024801 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024802 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024803 int max_inline_depth;
24804 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024805 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024806 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024807 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024808 /* Masks for common sizes */
24809 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24810 fprintf(fp, ".balign 16\n");
24811 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24812 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24813 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24814 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024815 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024816 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024817 ins = first;
24818 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024819 if (print_location &&
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024820 last_occurance != ins->occurance) {
24821 if (!ins->occurance->parent) {
24822 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024823 ins->occurance->function?ins->occurance->function:"(null)",
24824 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024825 ins->occurance->line,
24826 ins->occurance->col);
24827 }
24828 else {
24829 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024830 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024831 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024832 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024833 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024834 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024835 fprintf(fp, "\t * %s,%s:%d.%d\n",
24836 ptr->function,
24837 ptr->filename,
24838 ptr->line,
24839 ptr->col);
24840 }
24841 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024842 if (inline_depth > max_inline_depth) {
24843 max_inline_depth = inline_depth;
24844 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024845 }
24846 if (last_occurance) {
24847 put_occurance(last_occurance);
24848 }
24849 get_occurance(ins->occurance);
24850 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024851 }
24852
24853 print_instruction(state, ins, fp);
24854 ins = ins->next;
24855 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024856 if (print_location) {
24857 fprintf(fp, "/* max inline depth %d */\n",
24858 max_inline_depth);
24859 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024860}
Eric Biederman530b5192003-07-01 10:05:30 +000024861
Eric Biedermanb138ac82003-04-22 18:44:01 +000024862static void generate_code(struct compile_state *state)
24863{
24864 generate_local_labels(state);
24865 print_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024866
Eric Biedermanb138ac82003-04-22 18:44:01 +000024867}
24868
Eric Biederman90089602004-05-28 14:11:54 +000024869static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024870{
Eric Biederman90089602004-05-28 14:11:54 +000024871 int tok;
24872 FILE *fp;
24873 int line;
24874 const char *filename;
24875 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024876 filename = 0;
24877 line = 0;
24878 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024879 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024880 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024881 const char *token_str;
24882 tok = peek(state);
24883 if (tok == TOK_EOF) {
24884 break;
24885 }
Eric Biederman41203d92004-11-08 09:31:09 +000024886 tk = eat(state, tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024887 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024888 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024889 tk->str_len ? tk->val.str :
24890 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024891
24892 file = state->file;
24893 while(file->macro && file->prev) {
24894 file = file->prev;
24895 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024896 if (!file->macro &&
24897 ((file->line != line) || (file->basename != filename)))
Eric Biederman132368b2004-11-09 08:59:23 +000024898 {
Eric Biederman90089602004-05-28 14:11:54 +000024899 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024900 if ((file->basename == filename) &&
24901 (line < file->line)) {
24902 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024903 fprintf(fp, "\n");
24904 line++;
24905 }
24906 }
24907 else {
24908 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024909 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024910 }
Eric Biederman132368b2004-11-09 08:59:23 +000024911 line = file->line;
24912 filename = file->basename;
24913 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024914 for(i = 0; i < col; i++) {
24915 fprintf(fp, " ");
24916 }
24917 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024918
Eric Biederman90089602004-05-28 14:11:54 +000024919 fprintf(fp, "%s ", token_str);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024920
Eric Biederman90089602004-05-28 14:11:54 +000024921 if (state->compiler->debug & DEBUG_TOKENS) {
24922 loc(state->dbgout, state, 0);
24923 fprintf(state->dbgout, "%s <- `%s'\n",
24924 tokens[tok], token_str);
24925 }
24926 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024927}
24928
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024929static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024930 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024931{
24932 int i;
24933 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024934 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024935 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024936 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024937 state.compiler = compiler;
24938 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024939 state.file = 0;
24940 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24941 memset(&state.token[i], 0, sizeof(state.token[i]));
24942 state.token[i].tok = -1;
24943 }
Eric Biederman90089602004-05-28 14:11:54 +000024944 /* Remember the output descriptors */
24945 state.errout = stderr;
24946 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024947 /* Remember the output filename */
Patrick Georgi516a2a72010-03-25 21:45:25 +000024948 if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) {
24949 state.output = stdout;
24950 } else {
24951 state.output = fopen(state.compiler->ofilename, "w");
24952 if (!state.output) {
24953 error(&state, 0, "Cannot open output file %s\n",
24954 state.compiler->ofilename);
24955 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024956 }
Eric Biederman90089602004-05-28 14:11:54 +000024957 /* Make certain a good cleanup happens */
24958 exit_state = &state;
24959 atexit(exit_cleanup);
24960
Eric Biedermanb138ac82003-04-22 18:44:01 +000024961 /* Prep the preprocessor */
24962 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024963 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024964 /* register the C keywords */
24965 register_keywords(&state);
24966 /* register the keywords the macro preprocessor knows */
24967 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024968 /* generate some builtin macros */
24969 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024970 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024971 state.i_switch = lookup(&state, "switch", 6);
24972 state.i_case = lookup(&state, "case", 4);
24973 state.i_continue = lookup(&state, "continue", 8);
24974 state.i_break = lookup(&state, "break", 5);
24975 state.i_default = lookup(&state, "default", 7);
24976 state.i_return = lookup(&state, "return", 6);
24977 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024978 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24979 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24980 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24981 /* Memorize where predefined identifiers are. */
24982 state.i___func__ = lookup(&state, "__func__", 8);
24983 /* Memorize where some attribute keywords are. */
24984 state.i_noinline = lookup(&state, "noinline", 8);
24985 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000024986 state.i_noreturn = lookup(&state, "noreturn", 8);
Sven Schnelleefb479c2012-06-22 08:53:36 +020024987 state.i_unused = lookup(&state, "unused", 6);
Stefan Reinauerec664bc2013-05-09 14:06:04 -070024988 state.i_packed = lookup(&state, "packed", 6);
Eric Biederman90089602004-05-28 14:11:54 +000024989
24990 /* Process the command line macros */
24991 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000024992
24993 /* Allocate beginning bounding labels for the function list */
24994 state.first = label(&state);
24995 state.first->id |= TRIPLE_FLAG_VOLATILE;
24996 use_triple(state.first, state.first);
24997 ptr = label(&state);
24998 ptr->id |= TRIPLE_FLAG_VOLATILE;
24999 use_triple(ptr, ptr);
25000 flatten(&state, state.first, ptr);
25001
Eric Biederman5ade04a2003-10-22 04:03:46 +000025002 /* Allocate a label for the pool of global variables */
25003 state.global_pool = label(&state);
25004 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25005 flatten(&state, state.first, state.global_pool);
25006
Eric Biedermanb138ac82003-04-22 18:44:01 +000025007 /* Enter the globl definition scope */
25008 start_scope(&state);
25009 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025010
Eric Biedermanb138ac82003-04-22 18:44:01 +000025011 compile_file(&state, filename, 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +000025012
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025013 while (includes) {
25014 compile_file(&state, includes->filename, 1);
25015 includes=includes->next;
25016 }
Eric Biederman90089602004-05-28 14:11:54 +000025017
25018 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025019 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025020 print_preprocessed_tokens(&state);
25021 return;
25022 }
25023
Eric Biedermanb138ac82003-04-22 18:44:01 +000025024 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025025
Eric Biedermanb138ac82003-04-22 18:44:01 +000025026 /* Exit the global definition scope */
25027 end_scope(&state);
25028
Stefan Reinauer14e22772010-04-27 06:56:47 +000025029 /* Now that basic compilation has happened
25030 * optimize the intermediate code
Eric Biedermanb138ac82003-04-22 18:44:01 +000025031 */
25032 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025033
Eric Biedermanb138ac82003-04-22 18:44:01 +000025034 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025035 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025036 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025037 }
Eric Biederman90089602004-05-28 14:11:54 +000025038 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025039}
25040
Eric Biederman90089602004-05-28 14:11:54 +000025041static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025042{
Eric Biederman90089602004-05-28 14:11:54 +000025043 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025044}
25045
25046static void usage(void)
25047{
Eric Biederman90089602004-05-28 14:11:54 +000025048 FILE *fp = stdout;
25049 version(fp);
25050 fprintf(fp,
25051 "\nUsage: romcc [options] <source>.c\n"
25052 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25053 "Options: \n"
25054 "-o <output file name>\n"
25055 "-f<option> Specify a generic compiler option\n"
25056 "-m<option> Specify a arch dependent option\n"
25057 "-- Specify this is the last option\n"
25058 "\nGeneric compiler options:\n"
25059 );
25060 compiler_usage(fp);
25061 fprintf(fp,
25062 "\nArchitecture compiler options:\n"
25063 );
25064 arch_usage(fp);
25065 fprintf(fp,
25066 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025067 );
25068}
25069
25070static void arg_error(char *fmt, ...)
25071{
25072 va_list args;
25073 va_start(args, fmt);
25074 vfprintf(stderr, fmt, args);
25075 va_end(args);
25076 usage();
25077 exit(1);
25078}
25079
25080int main(int argc, char **argv)
25081{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025082 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025083 struct compiler_state compiler;
25084 struct arch_state arch;
25085 int all_opts;
Stefan Reinauer14e22772010-04-27 06:56:47 +000025086
25087
Eric Biederman90089602004-05-28 14:11:54 +000025088 /* I don't want any surprises */
25089 setlocale(LC_ALL, "C");
25090
Eric Biederman5ade04a2003-10-22 04:03:46 +000025091 init_compiler_state(&compiler);
25092 init_arch_state(&arch);
25093 filename = 0;
25094 all_opts = 0;
25095 while(argc > 1) {
25096 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25097 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025098 argv += 2;
25099 argc -= 2;
25100 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025101 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025102 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025103 result = -1;
25104 if (strcmp(argv[1], "--") == 0) {
25105 result = 0;
25106 all_opts = 1;
25107 }
Eric Biederman90089602004-05-28 14:11:54 +000025108 else if (strncmp(argv[1], "-E", 2) == 0) {
25109 result = compiler_encode_flag(&compiler, argv[1]);
25110 }
25111 else if (strncmp(argv[1], "-O", 2) == 0) {
25112 result = compiler_encode_flag(&compiler, argv[1]);
25113 }
25114 else if (strncmp(argv[1], "-I", 2) == 0) {
25115 result = compiler_encode_flag(&compiler, argv[1]);
25116 }
25117 else if (strncmp(argv[1], "-D", 2) == 0) {
25118 result = compiler_encode_flag(&compiler, argv[1]);
25119 }
25120 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025121 result = compiler_encode_flag(&compiler, argv[1]);
25122 }
25123 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25124 result = compiler_encode_flag(&compiler, argv[1]+2);
25125 }
25126 else if (strncmp(argv[1], "-f", 2) == 0) {
25127 result = compiler_encode_flag(&compiler, argv[1]+2);
25128 }
25129 else if (strncmp(argv[1], "-m", 2) == 0) {
25130 result = arch_encode_flag(&arch, argv[1]+2);
25131 }
Patrick Georgi516a2a72010-03-25 21:45:25 +000025132 else if (strncmp(argv[1], "-c", 2) == 0) {
25133 result = 0;
25134 }
25135 else if (strncmp(argv[1], "-S", 2) == 0) {
25136 result = 0;
25137 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025138 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025139 struct filelist *old_head = include_filelist;
25140 include_filelist = malloc(sizeof(struct filelist));
25141 if (!include_filelist) {
25142 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025143 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025144 argv++;
25145 argc--;
Patrick Georgic2d0b622010-03-25 15:49:40 +000025146 include_filelist->filename = strdup(argv[1]);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025147 include_filelist->next = old_head;
25148 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025149 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025150 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025151 arg_error("Invalid option specified: %s\n",
25152 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025153 }
25154 argv++;
25155 argc--;
25156 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025157 else {
25158 if (filename) {
25159 arg_error("Only one filename may be specified\n");
25160 }
25161 filename = argv[1];
25162 argv++;
25163 argc--;
25164 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025165 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025166 if (!filename) {
25167 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025168 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025169 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025170
25171 return 0;
25172}