blob: 15e950cc67591b877be1a5dfcc70790679e7f2a8 [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 Reinauerb4d3af82010-02-11 03:21:29 +00006#define VERSION_MINOR "72"
7#define RELEASE_DATE "10 February 2010"
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 }
901 if (index > OP_MAX) {
902 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;
Eric Biederman90089602004-05-28 14:11:54 +00001088 /* Additional hash entries for predefined macros */
1089 struct hash_entry *i_defined;
1090 struct hash_entry *i___VA_ARGS__;
1091 struct hash_entry *i___FILE__;
1092 struct hash_entry *i___LINE__;
1093 /* Additional hash entries for predefined identifiers */
1094 struct hash_entry *i___func__;
1095 /* Additional hash entries for attributes */
1096 struct hash_entry *i_noinline;
1097 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001098 int scope_depth;
Eric Biedermancb364952004-11-15 10:46:44 +00001099 unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
Eric Biederman90089602004-05-28 14:11:54 +00001100 int if_depth;
1101 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001102 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001103 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001104 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001105 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001106 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001107 struct basic_blocks bb;
1108 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001109};
1110
Eric Biederman0babc1c2003-05-09 02:39:00 +00001111/* visibility global/local */
1112/* static/auto duration */
1113/* typedef, register, inline */
1114#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001115#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001116/* Visibility */
1117#define STOR_GLOBAL 0x0001
1118/* Duration */
1119#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001120/* Definition locality */
1121#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001122/* Storage specifiers */
1123#define STOR_AUTO 0x0000
1124#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001125#define STOR_LOCAL 0x0003
1126#define STOR_EXTERN 0x0007
1127#define STOR_INLINE 0x0008
1128#define STOR_REGISTER 0x0010
1129#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001130
Eric Biederman5ade04a2003-10-22 04:03:46 +00001131#define QUAL_SHIFT 5
1132#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001133#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001134#define QUAL_CONST 0x0020
1135#define QUAL_VOLATILE 0x0040
1136#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001137
1138#define TYPE_SHIFT 8
1139#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001140#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1141#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001142#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1143#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001144#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1145#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001146#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1147#define TYPE_DEFAULT 0x0000
1148#define TYPE_VOID 0x0100
1149#define TYPE_CHAR 0x0200
1150#define TYPE_UCHAR 0x0300
1151#define TYPE_SHORT 0x0400
1152#define TYPE_USHORT 0x0500
1153#define TYPE_INT 0x0600
1154#define TYPE_UINT 0x0700
1155#define TYPE_LONG 0x0800
1156#define TYPE_ULONG 0x0900
1157#define TYPE_LLONG 0x0a00 /* long long */
1158#define TYPE_ULLONG 0x0b00
1159#define TYPE_FLOAT 0x0c00
1160#define TYPE_DOUBLE 0x0d00
1161#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001162
1163/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1164#define TYPE_ENUM 0x1600
1165#define TYPE_LIST 0x1700
1166/* TYPE_LIST is a basic building block when defining enumerations
1167 * type->field_ident holds the name of this enumeration entry.
1168 * type->right holds the entry in the list.
1169 */
1170
Eric Biederman0babc1c2003-05-09 02:39:00 +00001171#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001172/* For TYPE_STRUCT
1173 * type->left holds the link list of TYPE_PRODUCT entries that
1174 * make up the structure.
1175 * type->elements hold the length of the linked list
1176 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001177#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001178/* For TYPE_UNION
1179 * type->left holds the link list of TYPE_OVERLAP entries that
1180 * make up the union.
1181 * type->elements hold the length of the linked list
1182 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001183#define TYPE_POINTER 0x1200
Eric Biederman0babc1c2003-05-09 02:39:00 +00001184/* For TYPE_POINTER:
1185 * type->left holds the type pointed to.
1186 */
Stefan Reinauer14e22772010-04-27 06:56:47 +00001187#define TYPE_FUNCTION 0x1300
Eric Biederman0babc1c2003-05-09 02:39:00 +00001188/* For TYPE_FUNCTION:
1189 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001190 * type->right holds the type of the arguments
1191 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001192 */
1193#define TYPE_PRODUCT 0x1400
1194/* TYPE_PRODUCT is a basic building block when defining structures
1195 * type->left holds the type that appears first in memory.
1196 * type->right holds the type that appears next in memory.
1197 */
1198#define TYPE_OVERLAP 0x1500
1199/* TYPE_OVERLAP is a basic building block when defining unions
1200 * type->left and type->right holds to types that overlap
1201 * each other in memory.
1202 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001203#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001204/* TYPE_ARRAY is a basic building block when definitng arrays.
1205 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001206 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001207 */
Eric Biederman90089602004-05-28 14:11:54 +00001208#define TYPE_TUPLE 0x1900
Stefan Reinauer14e22772010-04-27 06:56:47 +00001209/* TYPE_TUPLE is a basic building block when defining
Eric Biederman90089602004-05-28 14:11:54 +00001210 * positionally reference type conglomerations. (i.e. closures)
1211 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1212 * except it has no field names.
1213 * type->left holds the liked list of TYPE_PRODUCT entries that
1214 * make up the closure type.
1215 * type->elements hold the number of elements in the closure.
1216 */
1217#define TYPE_JOIN 0x1a00
Stefan Reinauer14e22772010-04-27 06:56:47 +00001218/* TYPE_JOIN is a basic building block when defining
Eric Biederman90089602004-05-28 14:11:54 +00001219 * positionally reference type conglomerations. (i.e. closures)
1220 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1221 * except it has no field names.
1222 * type->left holds the liked list of TYPE_OVERLAP entries that
1223 * make up the closure type.
1224 * type->elements hold the number of elements in the closure.
1225 */
1226#define TYPE_BITFIELD 0x1b00
1227/* TYPE_BITFIED is the type of a bitfield.
1228 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1229 * type->elements holds the number of bits in the bitfield.
1230 */
1231#define TYPE_UNKNOWN 0x1c00
1232/* TYPE_UNKNOWN is the type of an unknown value.
1233 * Used on unknown consts and other places where I don't know the type.
1234 */
1235
1236#define ATTRIB_SHIFT 16
1237#define ATTRIB_MASK 0xffff0000
1238#define ATTRIB_NOINLINE 0x00010000
1239#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001240
Eric Biederman83b991a2003-10-11 06:20:25 +00001241#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001242
1243struct type {
1244 unsigned int type;
1245 struct type *left, *right;
1246 ulong_t elements;
1247 struct hash_entry *field_ident;
1248 struct hash_entry *type_ident;
1249};
1250
Eric Biederman530b5192003-07-01 10:05:30 +00001251#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001252#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001253#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001254#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001255#define MAX_REGISTERS 75
1256#define REGISTER_BITS 7
1257#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001258#define REG_ERROR 0
1259#define REG_UNSET 1
1260#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001261#define REG_VIRT0 (MAX_REGISTERS + 0)
1262#define REG_VIRT1 (MAX_REGISTERS + 1)
1263#define REG_VIRT2 (MAX_REGISTERS + 2)
1264#define REG_VIRT3 (MAX_REGISTERS + 3)
1265#define REG_VIRT4 (MAX_REGISTERS + 4)
1266#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001267#define REG_VIRT6 (MAX_REGISTERS + 6)
1268#define REG_VIRT7 (MAX_REGISTERS + 7)
1269#define REG_VIRT8 (MAX_REGISTERS + 8)
1270#define REG_VIRT9 (MAX_REGISTERS + 9)
1271
1272#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1273#error "MAX_VIRT_REGISTERS to small"
1274#endif
Eric Biederman90089602004-05-28 14:11:54 +00001275#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001276#error "Too many id bits used"
1277#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001278
1279/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001280#define REG_SHIFT 0
1281#define REGC_SHIFT REGISTER_BITS
1282#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001283#define REG_MASK (MAX_VIRT_REGISTERS -1)
1284#define ID_REG(ID) ((ID) & REG_MASK)
1285#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001286#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1287#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1288#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1289 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001290
Eric Biederman90089602004-05-28 14:11:54 +00001291#define ARCH_INPUT_REGS 4
1292#define ARCH_OUTPUT_REGS 4
1293
1294static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1295static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001296static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001297static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001298static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001299static void arch_reg_equivs(
1300 struct compile_state *state, unsigned *equiv, int reg);
1301static int arch_select_free_register(
1302 struct compile_state *state, char *used, int classes);
1303static unsigned arch_regc_size(struct compile_state *state, int class);
1304static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1305static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1306static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001307static struct reg_info arch_reg_constraint(
1308 struct compile_state *state, struct type *type, const char *constraint);
1309static struct reg_info arch_reg_clobber(
1310 struct compile_state *state, const char *clobber);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001311static struct reg_info arch_reg_lhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001312 struct triple *ins, int index);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001313static struct reg_info arch_reg_rhs(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001314 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001315static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001316static struct triple *transform_to_arch_instruction(
1317 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001318static struct triple *flatten(
1319 struct compile_state *state, struct triple *first, struct triple *ptr);
Jason Schildt27b85112005-08-10 14:31:52 +00001320static void print_dominators(struct compile_state *state,
1321 FILE *fp, struct basic_blocks *bb);
1322static void print_dominance_frontiers(struct compile_state *state,
1323 FILE *fp, struct basic_blocks *bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001324
1325
Eric Biedermanb138ac82003-04-22 18:44:01 +00001326
Eric Biederman5ade04a2003-10-22 04:03:46 +00001327#define DEBUG_ABORT_ON_ERROR 0x00000001
1328#define DEBUG_BASIC_BLOCKS 0x00000002
1329#define DEBUG_FDOMINATORS 0x00000004
1330#define DEBUG_RDOMINATORS 0x00000008
1331#define DEBUG_TRIPLES 0x00000010
1332#define DEBUG_INTERFERENCE 0x00000020
1333#define DEBUG_SCC_TRANSFORM 0x00000040
1334#define DEBUG_SCC_TRANSFORM2 0x00000080
1335#define DEBUG_REBUILD_SSA_FORM 0x00000100
1336#define DEBUG_INLINE 0x00000200
1337#define DEBUG_RANGE_CONFLICTS 0x00000400
1338#define DEBUG_RANGE_CONFLICTS2 0x00000800
1339#define DEBUG_COLOR_GRAPH 0x00001000
1340#define DEBUG_COLOR_GRAPH2 0x00002000
1341#define DEBUG_COALESCING 0x00004000
1342#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001343#define DEBUG_VERIFICATION 0x00010000
1344#define DEBUG_CALLS 0x00020000
1345#define DEBUG_CALLS2 0x00040000
1346#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001347
1348#define DEBUG_DEFAULT ( \
1349 DEBUG_ABORT_ON_ERROR | \
1350 DEBUG_BASIC_BLOCKS | \
1351 DEBUG_FDOMINATORS | \
1352 DEBUG_RDOMINATORS | \
1353 DEBUG_TRIPLES | \
1354 0 )
1355
Eric Biederman90089602004-05-28 14:11:54 +00001356#define DEBUG_ALL ( \
1357 DEBUG_ABORT_ON_ERROR | \
1358 DEBUG_BASIC_BLOCKS | \
1359 DEBUG_FDOMINATORS | \
1360 DEBUG_RDOMINATORS | \
1361 DEBUG_TRIPLES | \
1362 DEBUG_INTERFERENCE | \
1363 DEBUG_SCC_TRANSFORM | \
1364 DEBUG_SCC_TRANSFORM2 | \
1365 DEBUG_REBUILD_SSA_FORM | \
1366 DEBUG_INLINE | \
1367 DEBUG_RANGE_CONFLICTS | \
1368 DEBUG_RANGE_CONFLICTS2 | \
1369 DEBUG_COLOR_GRAPH | \
1370 DEBUG_COLOR_GRAPH2 | \
1371 DEBUG_COALESCING | \
1372 DEBUG_COALESCING2 | \
1373 DEBUG_VERIFICATION | \
1374 DEBUG_CALLS | \
1375 DEBUG_CALLS2 | \
1376 DEBUG_TOKENS | \
1377 0 )
1378
1379#define COMPILER_INLINE_MASK 0x00000007
1380#define COMPILER_INLINE_ALWAYS 0x00000000
1381#define COMPILER_INLINE_NEVER 0x00000001
1382#define COMPILER_INLINE_DEFAULTON 0x00000002
1383#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1384#define COMPILER_INLINE_NOPENALTY 0x00000004
1385#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1386#define COMPILER_SIMPLIFY 0x00000010
1387#define COMPILER_SCC_TRANSFORM 0x00000020
1388#define COMPILER_SIMPLIFY_OP 0x00000040
1389#define COMPILER_SIMPLIFY_PHI 0x00000080
1390#define COMPILER_SIMPLIFY_LABEL 0x00000100
1391#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1392#define COMPILER_SIMPLIFY_COPY 0x00000400
1393#define COMPILER_SIMPLIFY_ARITH 0x00000800
1394#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1395#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1396#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1397#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1398
Eric Biedermancb364952004-11-15 10:46:44 +00001399#define COMPILER_TRIGRAPHS 0x40000000
1400#define COMPILER_PP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001401
1402#define COMPILER_DEFAULT_FLAGS ( \
Eric Biedermancb364952004-11-15 10:46:44 +00001403 COMPILER_TRIGRAPHS | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001404 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001405 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001406 COMPILER_SIMPLIFY_OP | \
1407 COMPILER_SIMPLIFY_PHI | \
1408 COMPILER_SIMPLIFY_LABEL | \
1409 COMPILER_SIMPLIFY_BRANCH | \
1410 COMPILER_SIMPLIFY_COPY | \
1411 COMPILER_SIMPLIFY_ARITH | \
1412 COMPILER_SIMPLIFY_SHIFT | \
1413 COMPILER_SIMPLIFY_BITWISE | \
1414 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001415 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001416 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001417
Eric Biederman153ea352003-06-20 14:43:20 +00001418#define GLOBAL_SCOPE_DEPTH 1
1419#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001420
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001421static void compile_file(struct compile_state *old_state, const char *filename, int local);
1422
Eric Biederman5ade04a2003-10-22 04:03:46 +00001423
1424
1425static void init_compiler_state(struct compiler_state *compiler)
1426{
1427 memset(compiler, 0, sizeof(*compiler));
1428 compiler->label_prefix = "";
1429 compiler->ofilename = "auto.inc";
1430 compiler->flags = COMPILER_DEFAULT_FLAGS;
1431 compiler->debug = 0;
1432 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001433 compiler->include_path_count = 1;
1434 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1435 compiler->define_count = 1;
1436 compiler->defines = xcmalloc(sizeof(char *), "defines");
1437 compiler->undef_count = 1;
1438 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001439}
1440
1441struct compiler_flag {
1442 const char *name;
1443 unsigned long flag;
1444};
Eric Biederman90089602004-05-28 14:11:54 +00001445
1446struct compiler_arg {
1447 const char *name;
1448 unsigned long mask;
1449 struct compiler_flag flags[16];
1450};
1451
Eric Biederman5ade04a2003-10-22 04:03:46 +00001452static int set_flag(
1453 const struct compiler_flag *ptr, unsigned long *flags,
1454 int act, const char *flag)
1455{
1456 int result = -1;
1457 for(; ptr->name; ptr++) {
1458 if (strcmp(ptr->name, flag) == 0) {
1459 break;
1460 }
1461 }
1462 if (ptr->name) {
1463 result = 0;
1464 *flags &= ~(ptr->flag);
1465 if (act) {
1466 *flags |= ptr->flag;
1467 }
1468 }
1469 return result;
1470}
1471
Eric Biederman90089602004-05-28 14:11:54 +00001472static int set_arg(
1473 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1474{
1475 const char *val;
1476 int result = -1;
1477 int len;
1478 val = strchr(arg, '=');
1479 if (val) {
1480 len = val - arg;
1481 val++;
1482 for(; ptr->name; ptr++) {
1483 if (strncmp(ptr->name, arg, len) == 0) {
1484 break;
1485 }
1486 }
1487 if (ptr->name) {
1488 *flags &= ~ptr->mask;
1489 result = set_flag(&ptr->flags[0], flags, 1, val);
1490 }
1491 }
1492 return result;
1493}
Eric Biederman90089602004-05-28 14:11:54 +00001494
Stefan Reinauer14e22772010-04-27 06:56:47 +00001495
1496static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001497 const char *prefix, const char *invert_prefix)
1498{
1499 for(;ptr->name; ptr++) {
1500 fprintf(fp, "%s%s\n", prefix, ptr->name);
1501 if (invert_prefix) {
1502 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1503 }
1504 }
1505}
1506
1507static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1508 const char *prefix)
1509{
1510 for(;ptr->name; ptr++) {
1511 const struct compiler_flag *flag;
1512 for(flag = &ptr->flags[0]; flag->name; flag++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001513 fprintf(fp, "%s%s=%s\n",
Eric Biederman90089602004-05-28 14:11:54 +00001514 prefix, ptr->name, flag->name);
1515 }
1516 }
1517}
1518
1519static int append_string(size_t *max, const char ***vec, const char *str,
1520 const char *name)
1521{
1522 size_t count;
1523 count = ++(*max);
1524 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1525 (*vec)[count -1] = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001526 (*vec)[count -2] = str;
Eric Biederman90089602004-05-28 14:11:54 +00001527 return 0;
1528}
1529
1530static void arg_error(char *fmt, ...);
1531static const char *identifier(const char *str, const char *end);
1532
1533static int append_include_path(struct compiler_state *compiler, const char *str)
1534{
1535 int result;
1536 if (!exists(str, ".")) {
1537 arg_error("Nonexistent include path: `%s'\n",
1538 str);
1539 }
1540 result = append_string(&compiler->include_path_count,
1541 &compiler->include_paths, str, "include_paths");
1542 return result;
1543}
1544
1545static int append_define(struct compiler_state *compiler, const char *str)
1546{
1547 const char *end, *rest;
1548 int result;
1549
1550 end = strchr(str, '=');
1551 if (!end) {
1552 end = str + strlen(str);
1553 }
1554 rest = identifier(str, end);
1555 if (rest != end) {
1556 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001557 arg_error("Invalid name cannot define macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001558 len, len, str);
1559 }
1560 result = append_string(&compiler->define_count,
1561 &compiler->defines, str, "defines");
1562 return result;
1563}
1564
1565static int append_undef(struct compiler_state *compiler, const char *str)
1566{
1567 const char *end, *rest;
1568 int result;
1569
1570 end = str + strlen(str);
1571 rest = identifier(str, end);
1572 if (rest != end) {
1573 int len = end - str - 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001574 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
Eric Biederman90089602004-05-28 14:11:54 +00001575 len, len, str);
1576 }
1577 result = append_string(&compiler->undef_count,
1578 &compiler->undefs, str, "undefs");
1579 return result;
1580}
1581
1582static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001583 { "trigraphs", COMPILER_TRIGRAPHS },
1584 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001585 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1586 { "simplify", COMPILER_SIMPLIFY },
1587 { "scc-transform", COMPILER_SCC_TRANSFORM },
1588 { "simplify-op", COMPILER_SIMPLIFY_OP },
1589 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1590 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1591 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1592 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1593 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1594 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1595 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1596 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1597 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1598 { 0, 0 },
1599};
1600static const struct compiler_arg romcc_args[] = {
1601 { "inline-policy", COMPILER_INLINE_MASK,
1602 {
1603 { "always", COMPILER_INLINE_ALWAYS, },
1604 { "never", COMPILER_INLINE_NEVER, },
1605 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1606 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1607 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1608 { 0, 0 },
1609 },
1610 },
1611 { 0, 0 },
1612};
1613static const struct compiler_flag romcc_opt_flags[] = {
1614 { "-O", COMPILER_SIMPLIFY },
1615 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001616 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001617 { 0, 0, },
1618};
1619static const struct compiler_flag romcc_debug_flags[] = {
1620 { "all", DEBUG_ALL },
1621 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1622 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1623 { "fdominators", DEBUG_FDOMINATORS },
1624 { "rdominators", DEBUG_RDOMINATORS },
1625 { "triples", DEBUG_TRIPLES },
1626 { "interference", DEBUG_INTERFERENCE },
1627 { "scc-transform", DEBUG_SCC_TRANSFORM },
1628 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1629 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1630 { "inline", DEBUG_INLINE },
1631 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1632 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1633 { "color-graph", DEBUG_COLOR_GRAPH },
1634 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1635 { "coalescing", DEBUG_COALESCING },
1636 { "coalescing2", DEBUG_COALESCING2 },
1637 { "verification", DEBUG_VERIFICATION },
1638 { "calls", DEBUG_CALLS },
1639 { "calls2", DEBUG_CALLS2 },
1640 { "tokens", DEBUG_TOKENS },
1641 { 0, 0 },
1642};
1643
Eric Biederman5ade04a2003-10-22 04:03:46 +00001644static int compiler_encode_flag(
1645 struct compiler_state *compiler, const char *flag)
1646{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001647 int act;
1648 int result;
1649
1650 act = 1;
1651 result = -1;
1652 if (strncmp(flag, "no-", 3) == 0) {
1653 flag += 3;
1654 act = 0;
1655 }
1656 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001657 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1658 }
1659 else if (strncmp(flag, "-E", 2) == 0) {
1660 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1661 }
1662 else if (strncmp(flag, "-I", 2) == 0) {
1663 result = append_include_path(compiler, flag + 2);
1664 }
1665 else if (strncmp(flag, "-D", 2) == 0) {
1666 result = append_define(compiler, flag + 2);
1667 }
1668 else if (strncmp(flag, "-U", 2) == 0) {
1669 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001670 }
1671 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1672 result = 0;
1673 compiler->label_prefix = flag + 13;
1674 }
1675 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1676 unsigned long max_passes;
1677 char *end;
1678 max_passes = strtoul(flag + 22, &end, 10);
1679 if (end[0] == '\0') {
1680 result = 0;
1681 compiler->max_allocation_passes = max_passes;
1682 }
1683 }
1684 else if (act && strcmp(flag, "debug") == 0) {
1685 result = 0;
1686 compiler->debug |= DEBUG_DEFAULT;
1687 }
1688 else if (strncmp(flag, "debug-", 6) == 0) {
1689 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001690 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001691 }
1692 else {
Eric Biederman90089602004-05-28 14:11:54 +00001693 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1694 if (result < 0) {
1695 result = set_arg(romcc_args, &compiler->flags, flag);
1696 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001697 }
1698 return result;
1699}
1700
Eric Biederman90089602004-05-28 14:11:54 +00001701static void compiler_usage(FILE *fp)
1702{
1703 flag_usage(fp, romcc_opt_flags, "", 0);
1704 flag_usage(fp, romcc_flags, "-f", "-fno-");
1705 arg_usage(fp, romcc_args, "-f");
1706 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1707 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1708 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1709 fprintf(fp, "-I<include path>\n");
1710 fprintf(fp, "-D<macro>[=defn]\n");
1711 fprintf(fp, "-U<macro>\n");
1712}
1713
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001714static void do_cleanup(struct compile_state *state)
1715{
1716 if (state->output) {
1717 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001718 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001719 state->output = 0;
1720 }
1721 if (state->dbgout) {
1722 fflush(state->dbgout);
1723 }
1724 if (state->errout) {
1725 fflush(state->errout);
1726 }
1727}
1728
1729static struct compile_state *exit_state;
1730static void exit_cleanup(void)
1731{
1732 if (exit_state) {
1733 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001734 }
1735}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001736
1737static int get_col(struct file_state *file)
1738{
1739 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001740 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001741 ptr = file->line_start;
1742 end = file->pos;
1743 for(col = 0; ptr < end; ptr++) {
1744 if (*ptr != '\t') {
1745 col++;
Stefan Reinauer14e22772010-04-27 06:56:47 +00001746 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001747 else {
1748 col = (col & ~7) + 8;
1749 }
1750 }
1751 return col;
1752}
1753
1754static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1755{
1756 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001757 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001758 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001759 for(spot = triple->occurance; spot; spot = spot->parent) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00001760 fprintf(fp, "%s:%d.%d: ",
Eric Biederman5ade04a2003-10-22 04:03:46 +00001761 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001762 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001763 return;
1764 }
1765 if (!state->file) {
1766 return;
1767 }
1768 col = get_col(state->file);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001769 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001770 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001771}
1772
Stefan Reinauer14e22772010-04-27 06:56:47 +00001773static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001774 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001775{
Eric Biederman90089602004-05-28 14:11:54 +00001776 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001777 va_list args;
1778 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001779 loc(fp, state, ptr);
1780 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001781 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001782 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001783 }
Eric Biederman90089602004-05-28 14:11:54 +00001784 fprintf(fp, "Internal compiler error: ");
1785 vfprintf(fp, fmt, args);
1786 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001787 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001788 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001789 abort();
1790}
1791
1792
Stefan Reinauer14e22772010-04-27 06:56:47 +00001793static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001794 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001795{
Eric Biederman90089602004-05-28 14:11:54 +00001796 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001797 va_list args;
1798 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001799 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001800 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001801 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001802 }
Eric Biederman90089602004-05-28 14:11:54 +00001803 fprintf(fp, "Internal compiler warning: ");
1804 vfprintf(fp, fmt, args);
1805 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001806 va_end(args);
1807}
1808
1809
1810
Stefan Reinauer14e22772010-04-27 06:56:47 +00001811static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001812 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001813{
Eric Biederman90089602004-05-28 14:11:54 +00001814 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001815 va_list args;
1816 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001817 loc(fp, state, ptr);
1818 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001819 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001820 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001821 }
Eric Biederman90089602004-05-28 14:11:54 +00001822 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001823 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001824 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001825 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001826 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001827 abort();
1828 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001829 exit(1);
1830}
1831
Stefan Reinauer14e22772010-04-27 06:56:47 +00001832static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001833 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001834{
Eric Biederman90089602004-05-28 14:11:54 +00001835 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001836 va_list args;
1837 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001838 loc(fp, state, ptr);
Stefan Reinauer14e22772010-04-27 06:56:47 +00001839 fprintf(fp, "warning: ");
Eric Biederman90089602004-05-28 14:11:54 +00001840 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1841 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1842 }
1843 vfprintf(fp, fmt, args);
1844 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001845 va_end(args);
1846}
1847
Eric Biedermanb138ac82003-04-22 18:44:01 +00001848#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1849
Eric Biederman0babc1c2003-05-09 02:39:00 +00001850static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001851{
1852 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001853 if (op >= OP_MAX) {
1854 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001855 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001856 if (op < 0) {
1857 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001858 }
1859}
1860
Eric Biederman0babc1c2003-05-09 02:39:00 +00001861static void valid_ins(struct compile_state *state, struct triple *ptr)
1862{
1863 valid_op(state, ptr->op);
1864}
1865
Stefan Reinauer50542a82007-10-24 11:14:14 +00001866#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001867static void valid_param_count(struct compile_state *state, struct triple *ins)
1868{
1869 int lhs, rhs, misc, targ;
1870 valid_ins(state, ins);
1871 lhs = table_ops[ins->op].lhs;
1872 rhs = table_ops[ins->op].rhs;
1873 misc = table_ops[ins->op].misc;
1874 targ = table_ops[ins->op].targ;
1875
1876 if ((lhs >= 0) && (ins->lhs != lhs)) {
1877 internal_error(state, ins, "Bad lhs count");
1878 }
1879 if ((rhs >= 0) && (ins->rhs != rhs)) {
1880 internal_error(state, ins, "Bad rhs count");
1881 }
1882 if ((misc >= 0) && (ins->misc != misc)) {
1883 internal_error(state, ins, "Bad misc count");
1884 }
1885 if ((targ >= 0) && (ins->targ != targ)) {
1886 internal_error(state, ins, "Bad targ count");
1887 }
1888}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001889#endif
Eric Biederman90089602004-05-28 14:11:54 +00001890
Eric Biedermanb138ac82003-04-22 18:44:01 +00001891static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001892static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001893static void use_triple(struct triple *used, struct triple *user)
1894{
1895 struct triple_set **ptr, *new;
1896 if (!used)
1897 return;
1898 if (!user)
1899 return;
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001900 ptr = &used->use;
1901 while(*ptr) {
1902 if ((*ptr)->member == user) {
1903 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001904 }
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001905 ptr = &(*ptr)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001906 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00001907 /* Append new to the head of the list,
Eric Biedermanb138ac82003-04-22 18:44:01 +00001908 * copy_func and rename_block_variables
1909 * depends on this.
1910 */
1911 new = xcmalloc(sizeof(*new), "triple_set");
1912 new->member = user;
1913 new->next = used->use;
1914 used->use = new;
1915}
1916
1917static void unuse_triple(struct triple *used, struct triple *unuser)
1918{
1919 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001920 if (!used) {
1921 return;
1922 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001923 ptr = &used->use;
1924 while(*ptr) {
1925 use = *ptr;
1926 if (use->member == unuser) {
1927 *ptr = use->next;
1928 xfree(use);
1929 }
1930 else {
1931 ptr = &use->next;
1932 }
1933 }
1934}
1935
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001936static void put_occurance(struct occurance *occurance)
1937{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001938 if (occurance) {
1939 occurance->count -= 1;
1940 if (occurance->count <= 0) {
1941 if (occurance->parent) {
1942 put_occurance(occurance->parent);
1943 }
1944 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001945 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001946 }
1947}
1948
1949static void get_occurance(struct occurance *occurance)
1950{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001951 if (occurance) {
1952 occurance->count += 1;
1953 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001954}
1955
1956
1957static struct occurance *new_occurance(struct compile_state *state)
1958{
1959 struct occurance *result, *last;
1960 const char *filename;
1961 const char *function;
1962 int line, col;
1963
1964 function = "";
1965 filename = 0;
1966 line = 0;
1967 col = 0;
1968 if (state->file) {
1969 filename = state->file->report_name;
1970 line = state->file->report_line;
1971 col = get_col(state->file);
1972 }
1973 if (state->function) {
1974 function = state->function;
1975 }
1976 last = state->last_occurance;
1977 if (last &&
1978 (last->col == col) &&
1979 (last->line == line) &&
1980 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001981 ((last->filename == filename) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00001982 (strcmp(last->filename, filename) == 0)))
Eric Biederman83b991a2003-10-11 06:20:25 +00001983 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001984 get_occurance(last);
1985 return last;
1986 }
1987 if (last) {
1988 state->last_occurance = 0;
1989 put_occurance(last);
1990 }
1991 result = xmalloc(sizeof(*result), "occurance");
1992 result->count = 2;
1993 result->filename = filename;
1994 result->function = function;
1995 result->line = line;
1996 result->col = col;
1997 result->parent = 0;
1998 state->last_occurance = result;
1999 return result;
2000}
2001
2002static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002003 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002004{
2005 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002006 if (top->parent) {
2007 internal_error(state, 0, "inlining an already inlined function?");
2008 }
2009 /* If I have a null base treat it that way */
2010 if ((base->parent == 0) &&
2011 (base->col == 0) &&
2012 (base->line == 0) &&
2013 (base->function[0] == '\0') &&
2014 (base->filename[0] == '\0')) {
2015 base = 0;
2016 }
2017 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002018 last = state->last_occurance;
2019 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002020 (last->parent == base) &&
2021 (last->col == top->col) &&
2022 (last->line == top->line) &&
2023 (last->function == top->function) &&
2024 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002025 get_occurance(last);
2026 return last;
2027 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002028 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002029 if (last) {
2030 state->last_occurance = 0;
2031 put_occurance(last);
2032 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002033 /* Generate a new occurance structure */
2034 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002035 result = xmalloc(sizeof(*result), "occurance");
2036 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002037 result->filename = top->filename;
2038 result->function = top->function;
2039 result->line = top->line;
2040 result->col = top->col;
2041 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002042 state->last_occurance = result;
2043 return result;
2044}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002045
2046static struct occurance dummy_occurance = {
2047 .count = 2,
2048 .filename = __FILE__,
2049 .function = "",
2050 .line = __LINE__,
2051 .col = 0,
2052 .parent = 0,
2053};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002054
Eric Biederman90089602004-05-28 14:11:54 +00002055/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002056 * from a triple. Having allows certain sanity checks to pass even
2057 * when the original triple that was pointed to is gone.
2058 */
Eric Biederman90089602004-05-28 14:11:54 +00002059static struct triple unknown_triple = {
2060 .next = &unknown_triple,
2061 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002062 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002063 .op = OP_UNKNOWNVAL,
2064 .lhs = 0,
2065 .rhs = 0,
2066 .misc = 0,
2067 .targ = 0,
2068 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002069 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002070 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002071 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002072 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002073};
2074
Eric Biederman0babc1c2003-05-09 02:39:00 +00002075
Eric Biederman90089602004-05-28 14:11:54 +00002076static size_t registers_of(struct compile_state *state, struct type *type);
2077
Stefan Reinauer14e22772010-04-27 06:56:47 +00002078static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002079 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2080 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002081{
Eric Biederman90089602004-05-28 14:11:54 +00002082 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002083 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002084 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002085 dummy.op = op;
2086 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002087 valid_op(state, op);
2088 lhs = table_ops[op].lhs;
2089 rhs = table_ops[op].rhs;
2090 misc = table_ops[op].misc;
2091 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002092
2093 switch(op) {
2094 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002095 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002096 break;
2097 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002098 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002099 break;
2100 case OP_ADECL:
2101 lhs = registers_of(state, type);
2102 break;
2103 case OP_TUPLE:
2104 lhs = registers_of(state, type);
2105 break;
2106 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002107 rhs = rhs_wanted;
2108 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002109 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002110 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002111 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002112 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002113 }
2114 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002115 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002116 }
2117 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002118 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002119 }
2120 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002121 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002122 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002123
2124 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002125 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002126 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2127
2128 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2129 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002130 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002131 ret->lhs = lhs;
2132 ret->rhs = rhs;
2133 ret->misc = misc;
2134 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002135 ret->type = type;
2136 ret->next = ret;
2137 ret->prev = ret;
2138 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002139 /* A simple sanity check */
2140 if ((ret->op != op) ||
2141 (ret->lhs != lhs) ||
2142 (ret->rhs != rhs) ||
2143 (ret->misc != misc) ||
2144 (ret->targ != targ) ||
2145 (ret->type != type) ||
2146 (ret->next != ret) ||
2147 (ret->prev != ret) ||
2148 (ret->occurance != occurance)) {
2149 internal_error(state, ret, "huh?");
2150 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002151 return ret;
2152}
2153
Eric Biederman0babc1c2003-05-09 02:39:00 +00002154struct triple *dup_triple(struct compile_state *state, struct triple *src)
2155{
2156 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002157 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002158 src_lhs = src->lhs;
2159 src_rhs = src->rhs;
2160 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002161 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002162 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002163 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002164 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002165 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002166 return dup;
2167}
2168
Eric Biederman41203d92004-11-08 09:31:09 +00002169static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2170{
2171 struct triple *copy;
2172 copy = dup_triple(state, src);
2173 copy->use = 0;
2174 copy->next = copy->prev = copy;
2175 return copy;
2176}
2177
Stefan Reinauer14e22772010-04-27 06:56:47 +00002178static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002179 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002180{
2181 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002182 struct occurance *occurance;
2183 occurance = new_occurance(state);
2184 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002185 return ret;
2186}
2187
Stefan Reinauer14e22772010-04-27 06:56:47 +00002188static struct triple *build_triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002189 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002190 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002191{
2192 struct triple *ret;
2193 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002194 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002195 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002196 if (count > 0) {
2197 ret->param[0] = left;
2198 }
2199 if (count > 1) {
2200 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002201 }
2202 return ret;
2203}
2204
Stefan Reinauer14e22772010-04-27 06:56:47 +00002205static struct triple *triple(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002206 int op, struct type *type, struct triple *left, struct triple *right)
2207{
2208 struct triple *ret;
2209 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002210 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002211 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002212 if (count >= 1) {
2213 ret->param[0] = left;
2214 }
2215 if (count >= 2) {
2216 ret->param[1] = right;
2217 }
2218 return ret;
2219}
2220
Stefan Reinauer14e22772010-04-27 06:56:47 +00002221static struct triple *branch(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002222 struct triple *targ, struct triple *test)
2223{
2224 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002225 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002226 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002227 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002228 } else {
2229 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002230 }
2231 TARG(ret, 0) = targ;
2232 /* record the branch target was used */
2233 if (!targ || (targ->op != OP_LABEL)) {
2234 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002235 }
2236 return ret;
2237}
2238
Eric Biederman90089602004-05-28 14:11:54 +00002239static int triple_is_label(struct compile_state *state, struct triple *ins);
2240static int triple_is_call(struct compile_state *state, struct triple *ins);
2241static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002242static void insert_triple(struct compile_state *state,
2243 struct triple *first, struct triple *ptr)
2244{
2245 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002246 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002247 internal_error(state, ptr, "expression already used");
2248 }
2249 ptr->next = first;
2250 ptr->prev = first->prev;
2251 ptr->prev->next = ptr;
2252 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002253
2254 if (triple_is_cbranch(state, ptr->prev) ||
2255 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002256 unuse_triple(first, ptr->prev);
2257 use_triple(ptr, ptr->prev);
2258 }
2259 }
2260}
2261
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002262static int triple_stores_block(struct compile_state *state, struct triple *ins)
2263{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002264 /* This function is used to determine if u.block
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002265 * is utilized to store the current block number.
2266 */
2267 int stores_block;
2268 valid_ins(state, ins);
2269 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2270 return stores_block;
2271}
2272
Eric Biederman90089602004-05-28 14:11:54 +00002273static int triple_is_branch(struct compile_state *state, struct triple *ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002274static struct block *block_of_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002275 struct triple *ins)
2276{
2277 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002278 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002279 return 0;
2280 }
2281 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002282 while(ins != first && !triple_is_branch(state, ins->prev) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00002283 !triple_stores_block(state, ins))
2284 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002285 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002286 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002287 }
2288 ins = ins->prev;
2289 }
Eric Biederman90089602004-05-28 14:11:54 +00002290 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002291}
2292
Eric Biederman90089602004-05-28 14:11:54 +00002293static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002294static struct triple *pre_triple(struct compile_state *state,
2295 struct triple *base,
2296 int op, struct type *type, struct triple *left, struct triple *right)
2297{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002298 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002299 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002300 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002301 /* If I am an OP_PIECE jump to the real instruction */
2302 if (base->op == OP_PIECE) {
2303 base = MISC(base, 0);
2304 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002305 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002306 get_occurance(base->occurance);
2307 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002308 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002309 if (triple_stores_block(state, ret)) {
2310 ret->u.block = block;
2311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002312 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002313 for(i = 0; i < ret->lhs; i++) {
2314 struct triple *piece;
2315 piece = LHS(ret, i);
2316 insert_triple(state, base, piece);
2317 use_triple(ret, piece);
2318 use_triple(piece, ret);
2319 }
2320 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002321 block->first = ret;
2322 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002323 return ret;
2324}
2325
2326static struct triple *post_triple(struct compile_state *state,
2327 struct triple *base,
2328 int op, struct type *type, struct triple *left, struct triple *right)
2329{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002330 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002331 struct triple *ret, *next;
2332 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002333 /* If I am an OP_PIECE jump to the real instruction */
2334 if (base->op == OP_PIECE) {
2335 base = MISC(base, 0);
2336 }
2337 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002338 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002339 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002340 base = LHS(base, zlhs - 1);
2341 }
2342
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002343 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002344 get_occurance(base->occurance);
2345 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002346 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002347 if (triple_stores_block(state, ret)) {
2348 ret->u.block = block;
2349 }
Eric Biederman90089602004-05-28 14:11:54 +00002350 next = base->next;
2351 insert_triple(state, next, ret);
2352 zlhs = ret->lhs;
2353 for(i = 0; i < zlhs; i++) {
2354 struct triple *piece;
2355 piece = LHS(ret, i);
2356 insert_triple(state, next, piece);
2357 use_triple(ret, piece);
2358 use_triple(piece, ret);
2359 }
2360 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002361 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002362 if (zlhs) {
2363 block->last = LHS(ret, zlhs - 1);
2364 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002365 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002366 return ret;
2367}
2368
Eric Biederman90089602004-05-28 14:11:54 +00002369static struct type *reg_type(
2370 struct compile_state *state, struct type *type, int reg);
2371
2372static void generate_lhs_piece(
2373 struct compile_state *state, struct triple *ins, int index)
2374{
2375 struct type *piece_type;
2376 struct triple *piece;
2377 get_occurance(ins->occurance);
2378 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2379
2380 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2381 piece_type = piece_type->left;
2382 }
2383#if 0
2384{
2385 static void name_of(FILE *fp, struct type *type);
2386 FILE * fp = state->errout;
2387 fprintf(fp, "piece_type(%d): ", index);
2388 name_of(fp, piece_type);
2389 fprintf(fp, "\n");
2390}
2391#endif
2392 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2393 piece->u.cval = index;
2394 LHS(ins, piece->u.cval) = piece;
2395 MISC(piece, 0) = ins;
2396}
2397
2398static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2399{
2400 int i, zlhs;
2401 zlhs = ins->lhs;
2402 for(i = 0; i < zlhs; i++) {
2403 generate_lhs_piece(state, ins, i);
2404 }
2405}
2406
Eric Biedermanb138ac82003-04-22 18:44:01 +00002407static struct triple *label(struct compile_state *state)
2408{
2409 /* Labels don't get a type */
2410 struct triple *result;
2411 result = triple(state, OP_LABEL, &void_type, 0, 0);
2412 return result;
2413}
2414
Eric Biederman90089602004-05-28 14:11:54 +00002415static struct triple *mkprog(struct compile_state *state, ...)
2416{
2417 struct triple *prog, *head, *arg;
2418 va_list args;
2419 int i;
2420
2421 head = label(state);
2422 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2423 RHS(prog, 0) = head;
2424 va_start(args, state);
2425 i = 0;
2426 while((arg = va_arg(args, struct triple *)) != 0) {
2427 if (++i >= 100) {
2428 internal_error(state, 0, "too many arguments to mkprog");
2429 }
2430 flatten(state, head, arg);
2431 }
2432 va_end(args);
2433 prog->type = head->prev->type;
2434 return prog;
2435}
2436static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002437static void display_triple(FILE *fp, struct triple *ins)
2438{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002439 struct occurance *ptr;
2440 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002441 char pre, post, vol;
2442 pre = post = vol = ' ';
2443 if (ins) {
2444 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2445 pre = '^';
2446 }
2447 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2448 post = ',';
2449 }
2450 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2451 vol = 'v';
2452 }
2453 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002454 }
Eric Biederman90089602004-05-28 14:11:54 +00002455 if (ins == 0) {
2456 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002457 }
Eric Biederman90089602004-05-28 14:11:54 +00002458 else if (ins->op == OP_INTCONST) {
2459 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002460 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002461 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002462 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002463 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002464 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002465 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002466 MISC(ins, 0), (unsigned long)(ins->u.cval));
2467 }
2468 else if (ins->op == OP_INDEX) {
2469 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002470 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman90089602004-05-28 14:11:54 +00002471 RHS(ins, 0), (unsigned long)(ins->u.cval));
2472 }
2473 else if (ins->op == OP_PIECE) {
2474 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002475 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002476 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002477 }
2478 else {
2479 int i, count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002480 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
Eric Biederman90089602004-05-28 14:11:54 +00002481 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2482 if (table_ops[ins->op].flags & BITFIELD) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002483 fprintf(fp, " <%2d-%2d:%2d>",
Eric Biederman90089602004-05-28 14:11:54 +00002484 ins->u.bitfield.offset,
2485 ins->u.bitfield.offset + ins->u.bitfield.size,
2486 ins->u.bitfield.size);
2487 }
2488 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002489 for(i = 0; i < count; i++) {
2490 fprintf(fp, " %-10p", ins->param[i]);
2491 }
2492 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002493 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002494 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002495 }
Eric Biederman90089602004-05-28 14:11:54 +00002496 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002497 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002498#if DEBUG_DISPLAY_TYPES
2499 fprintf(fp, " <");
2500 name_of(fp, ins->type);
2501 fprintf(fp, "> ");
2502#endif
2503#if DEBUG_DISPLAY_USES
2504 fprintf(fp, " [");
2505 for(user = ins->use; user; user = user->next) {
2506 fprintf(fp, " %-10p", user->member);
2507 }
2508 fprintf(fp, " ]");
2509#endif
2510 fprintf(fp, " @");
2511 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2512 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002513 ptr->function,
Eric Biederman90089602004-05-28 14:11:54 +00002514 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002515 ptr->line,
Eric Biederman90089602004-05-28 14:11:54 +00002516 ptr->col);
2517 }
2518 if (ins->op == OP_ASM) {
2519 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002520 }
2521 }
Eric Biederman90089602004-05-28 14:11:54 +00002522 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002523 fflush(fp);
2524}
2525
Eric Biederman90089602004-05-28 14:11:54 +00002526static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002527static void display_triple_changes(
2528 FILE *fp, const struct triple *new, const struct triple *orig)
2529{
2530
2531 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002532 new_count = TRIPLE_SIZE(new);
2533 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002534 if ((new->op != orig->op) ||
2535 (new_count != orig_count) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002536 (memcmp(orig->param, new->param,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002537 orig_count * sizeof(orig->param[0])) != 0) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00002538 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
Eric Biederman5ade04a2003-10-22 04:03:46 +00002539 {
2540 struct occurance *ptr;
2541 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002542 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002543 if (orig->op == new->op) {
2544 fprintf(fp, " %-11s", tops(orig->op));
2545 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002546 fprintf(fp, " [%-10s %-10s]",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002547 tops(new->op), tops(orig->op));
2548 }
2549 min_count = new_count;
2550 if (min_count > orig_count) {
2551 min_count = orig_count;
2552 }
2553 for(indent = i = 0; i < min_count; i++) {
2554 if (orig->param[i] == new->param[i]) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002555 fprintf(fp, " %-11p",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002556 orig->param[i]);
2557 indent += 12;
2558 } else {
2559 fprintf(fp, " [%-10p %-10p]",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002560 new->param[i],
Eric Biederman5ade04a2003-10-22 04:03:46 +00002561 orig->param[i]);
2562 indent += 24;
2563 }
2564 }
2565 for(; i < orig_count; i++) {
2566 fprintf(fp, " [%-9p]", orig->param[i]);
2567 indent += 12;
2568 }
2569 for(; i < new_count; i++) {
2570 fprintf(fp, " [%-9p]", new->param[i]);
2571 indent += 12;
2572 }
2573 if ((new->op == OP_INTCONST)||
2574 (new->op == OP_ADDRCONST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00002575 fprintf(fp, " <0x%08lx>",
Eric Biederman5ade04a2003-10-22 04:03:46 +00002576 (unsigned long)(new->u.cval));
2577 indent += 13;
2578 }
2579 for(;indent < 36; indent++) {
2580 putc(' ', fp);
2581 }
Eric Biederman90089602004-05-28 14:11:54 +00002582
2583#if DEBUG_DISPLAY_TYPES
2584 fprintf(fp, " <");
2585 name_of(fp, new->type);
2586 if (!equiv_types(new->type, orig->type)) {
2587 fprintf(fp, " -- ");
2588 name_of(fp, orig->type);
2589 }
2590 fprintf(fp, "> ");
2591#endif
2592
Eric Biederman5ade04a2003-10-22 04:03:46 +00002593 fprintf(fp, " @");
2594 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2595 fprintf(fp, " %s,%s:%d.%d",
Stefan Reinauer14e22772010-04-27 06:56:47 +00002596 ptr->function,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002597 ptr->filename,
Stefan Reinauer14e22772010-04-27 06:56:47 +00002598 ptr->line,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002599 ptr->col);
Stefan Reinauer14e22772010-04-27 06:56:47 +00002600
Eric Biederman5ade04a2003-10-22 04:03:46 +00002601 }
2602 fprintf(fp, "\n");
2603 fflush(fp);
2604 }
2605}
2606
Eric Biederman83b991a2003-10-11 06:20:25 +00002607static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002608{
2609 /* Does the triple have no side effects.
Stefan Reinauer14e22772010-04-27 06:56:47 +00002610 * I.e. Rexecuting the triple with the same arguments
Eric Biedermanb138ac82003-04-22 18:44:01 +00002611 * gives the same value.
2612 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002613 unsigned pure;
2614 valid_ins(state, ins);
2615 pure = PURE_BITS(table_ops[ins->op].flags);
2616 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002617 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002618 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002619 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002620 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002621}
2622
Stefan Reinauer14e22772010-04-27 06:56:47 +00002623static int triple_is_branch_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002624 struct triple *ins, unsigned type)
2625{
2626 /* Is this one of the passed branch types? */
2627 valid_ins(state, ins);
2628 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2629}
2630
Eric Biederman0babc1c2003-05-09 02:39:00 +00002631static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002632{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002633 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002634 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002635 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002636}
2637
Eric Biederman90089602004-05-28 14:11:54 +00002638static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002639{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002640 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002641 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002642}
2643
Eric Biederman90089602004-05-28 14:11:54 +00002644static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002645{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002646 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002647 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002648 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002649 type = BRANCH_BITS(table_ops[ins->op].flags);
2650 return (type != 0) && (type != CBRANCH);
2651}
2652
2653static int triple_is_call(struct compile_state *state, struct triple *ins)
2654{
2655 /* Is this triple a call instruction? */
2656 return triple_is_branch_type(state, ins, CALLBRANCH);
2657}
2658
2659static int triple_is_ret(struct compile_state *state, struct triple *ins)
2660{
2661 /* Is this triple a return instruction? */
2662 return triple_is_branch_type(state, ins, RETBRANCH);
2663}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002664
Stefan Reinauer50542a82007-10-24 11:14:14 +00002665#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002666static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2667{
2668 /* Is this triple an unconditional branch and not a call or a
2669 * return? */
2670 return triple_is_branch_type(state, ins, UBRANCH);
2671}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002672#endif
Eric Biederman90089602004-05-28 14:11:54 +00002673
2674static int triple_is_end(struct compile_state *state, struct triple *ins)
2675{
2676 return triple_is_branch_type(state, ins, ENDBRANCH);
2677}
2678
2679static int triple_is_label(struct compile_state *state, struct triple *ins)
2680{
2681 valid_ins(state, ins);
2682 return (ins->op == OP_LABEL);
2683}
2684
2685static struct triple *triple_to_block_start(
2686 struct compile_state *state, struct triple *start)
2687{
2688 while(!triple_is_branch(state, start->prev) &&
2689 (!triple_is_label(state, start) || !start->use)) {
2690 start = start->prev;
2691 }
2692 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002693}
2694
Eric Biederman0babc1c2003-05-09 02:39:00 +00002695static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002696{
2697 /* This function is used to determine which triples need
2698 * a register.
2699 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002700 int is_def;
2701 valid_ins(state, ins);
2702 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002703 if (ins->lhs >= 1) {
2704 is_def = 0;
2705 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002706 return is_def;
2707}
2708
Eric Biederman83b991a2003-10-11 06:20:25 +00002709static int triple_is_structural(struct compile_state *state, struct triple *ins)
2710{
2711 int is_structural;
2712 valid_ins(state, ins);
2713 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2714 return is_structural;
2715}
2716
Eric Biederman90089602004-05-28 14:11:54 +00002717static int triple_is_part(struct compile_state *state, struct triple *ins)
2718{
2719 int is_part;
2720 valid_ins(state, ins);
2721 is_part = (table_ops[ins->op].flags & PART) == PART;
2722 return is_part;
2723}
2724
2725static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2726{
2727 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2728}
2729
Eric Biederman0babc1c2003-05-09 02:39:00 +00002730static struct triple **triple_iter(struct compile_state *state,
2731 size_t count, struct triple **vector,
2732 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002733{
2734 struct triple **ret;
2735 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002736 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002737 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002738 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002739 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002740 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002741 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002742 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002743 }
2744 return ret;
Stefan Reinauer14e22772010-04-27 06:56:47 +00002745
Eric Biedermanb138ac82003-04-22 18:44:01 +00002746}
2747
2748static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002749 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002750{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002751 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002752 ins, last);
2753}
2754
2755static struct triple **triple_rhs(struct compile_state *state,
2756 struct triple *ins, struct triple **last)
2757{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002758 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002759 ins, last);
2760}
2761
2762static struct triple **triple_misc(struct compile_state *state,
2763 struct triple *ins, struct triple **last)
2764{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002765 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002766 ins, last);
2767}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002768
Eric Biederman90089602004-05-28 14:11:54 +00002769static struct triple **do_triple_targ(struct compile_state *state,
2770 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002771{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002772 size_t count;
2773 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002774 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002775 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002776 count = ins->targ;
2777 next_is_targ = 0;
2778 if (triple_is_cbranch(state, ins)) {
2779 next_is_targ = 1;
2780 }
2781 if (!call_edges && triple_is_call(state, ins)) {
2782 count = 0;
2783 }
2784 if (next_edges && triple_is_call(state, ins)) {
2785 next_is_targ = 1;
2786 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002787 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002788 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002789 if (!last) {
2790 ret = &ins->next;
2791 } else if (last == &ins->next) {
2792 last = 0;
2793 }
2794 }
2795 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002796 if (!last) {
2797 ret = vector;
2798 }
2799 else if ((last >= vector) && (last < (vector + count - 1))) {
2800 ret = last + 1;
2801 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002802 else if (last == vector + count - 1) {
2803 last = 0;
2804 }
2805 }
Eric Biederman90089602004-05-28 14:11:54 +00002806 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002807 struct triple_set *use;
2808 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002809 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002810 continue;
2811 }
2812 if (!last) {
2813 ret = &use->member->next;
2814 break;
2815 }
2816 else if (last == &use->member->next) {
2817 last = 0;
2818 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002819 }
2820 }
2821 return ret;
2822}
2823
Eric Biederman90089602004-05-28 14:11:54 +00002824static struct triple **triple_targ(struct compile_state *state,
2825 struct triple *ins, struct triple **last)
2826{
2827 return do_triple_targ(state, ins, last, 1, 1);
2828}
2829
2830static struct triple **triple_edge_targ(struct compile_state *state,
2831 struct triple *ins, struct triple **last)
2832{
Stefan Reinauer14e22772010-04-27 06:56:47 +00002833 return do_triple_targ(state, ins, last,
Eric Biederman90089602004-05-28 14:11:54 +00002834 state->functions_joined, !state->functions_joined);
2835}
2836
2837static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2838{
2839 struct triple *next;
2840 int lhs, i;
2841 lhs = ins->lhs;
2842 next = ins->next;
2843 for(i = 0; i < lhs; i++) {
2844 struct triple *piece;
2845 piece = LHS(ins, i);
2846 if (next != piece) {
2847 internal_error(state, ins, "malformed lhs on %s",
2848 tops(ins->op));
2849 }
2850 if (next->op != OP_PIECE) {
2851 internal_error(state, ins, "bad lhs op %s at %d on %s",
2852 tops(next->op), i, tops(ins->op));
2853 }
2854 if (next->u.cval != i) {
2855 internal_error(state, ins, "bad u.cval of %d %d expected",
2856 next->u.cval, i);
2857 }
2858 next = next->next;
2859 }
2860 return next;
2861}
2862
2863/* Function piece accessor functions */
Stefan Reinauer14e22772010-04-27 06:56:47 +00002864static struct triple *do_farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002865 struct triple *func, unsigned index)
2866{
2867 struct type *ftype;
2868 struct triple *first, *arg;
2869 unsigned i;
2870
2871 ftype = func->type;
2872 if((index < 0) || (index >= (ftype->elements + 2))) {
2873 internal_error(state, func, "bad argument index: %d", index);
2874 }
2875 first = RHS(func, 0);
2876 arg = first->next;
2877 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2878 /* do nothing */
2879 }
2880 if (arg->op != OP_ADECL) {
2881 internal_error(state, 0, "arg not adecl?");
2882 }
2883 return arg;
2884}
2885static struct triple *fresult(struct compile_state *state, struct triple *func)
2886{
2887 return do_farg(state, func, 0);
2888}
2889static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2890{
2891 return do_farg(state, func, 1);
2892}
Stefan Reinauer14e22772010-04-27 06:56:47 +00002893static struct triple *farg(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00002894 struct triple *func, unsigned index)
2895{
2896 return do_farg(state, func, index + 2);
2897}
2898
2899
2900static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2901{
2902 struct triple *first, *ins;
2903 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2904 first = ins = RHS(func, 0);
2905 do {
2906 if (triple_is_label(state, ins) && ins->use) {
2907 fprintf(fp, "%p:\n", ins);
2908 }
2909 display_triple(fp, ins);
2910
2911 if (triple_is_branch(state, ins)) {
2912 fprintf(fp, "\n");
2913 }
2914 if (ins->next->prev != ins) {
2915 internal_error(state, ins->next, "bad prev");
2916 }
2917 ins = ins->next;
2918 } while(ins != first);
2919}
2920
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002921static void verify_use(struct compile_state *state,
2922 struct triple *user, struct triple *used)
2923{
2924 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002925 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002926 for(i = 0; i < size; i++) {
2927 if (user->param[i] == used) {
2928 break;
2929 }
2930 }
2931 if (triple_is_branch(state, user)) {
2932 if (user->next == used) {
2933 i = -1;
2934 }
2935 }
2936 if (i == size) {
2937 internal_error(state, user, "%s(%p) does not use %s(%p)",
2938 tops(user->op), user, tops(used->op), used);
2939 }
2940}
2941
Stefan Reinauer14e22772010-04-27 06:56:47 +00002942static int find_rhs_use(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002943 struct triple *user, struct triple *used)
2944{
2945 struct triple **param;
2946 int size, i;
2947 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002948
2949#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002950#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002951#endif
Eric Biederman90089602004-05-28 14:11:54 +00002952 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002953 param = &RHS(user, 0);
2954 for(i = 0; i < size; i++) {
2955 if (param[i] == used) {
2956 return i;
2957 }
2958 }
2959 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002960}
2961
2962static void free_triple(struct compile_state *state, struct triple *ptr)
2963{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002964 size_t size;
2965 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002966 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002967 ptr->prev->next = ptr->next;
2968 ptr->next->prev = ptr->prev;
2969 if (ptr->use) {
2970 internal_error(state, ptr, "ptr->use != 0");
2971 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002972 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002973 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002974 xfree(ptr);
2975}
2976
2977static void release_triple(struct compile_state *state, struct triple *ptr)
2978{
2979 struct triple_set *set, *next;
2980 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002981 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002982 if (ptr == &unknown_triple) {
2983 return;
2984 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002985 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002986 /* Make certain the we are not the first or last element of a block */
2987 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002988 if (block) {
2989 if ((block->last == ptr) && (block->first == ptr)) {
2990 block->last = block->first = 0;
2991 }
2992 else if (block->last == ptr) {
2993 block->last = ptr->prev;
2994 }
2995 else if (block->first == ptr) {
2996 block->first = ptr->next;
2997 }
Eric Biederman66fe2222003-07-04 15:14:04 +00002998 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002999 /* Remove ptr from use chains where it is the user */
3000 expr = triple_rhs(state, ptr, 0);
3001 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3002 if (*expr) {
3003 unuse_triple(*expr, ptr);
3004 }
3005 }
3006 expr = triple_lhs(state, ptr, 0);
3007 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3008 if (*expr) {
3009 unuse_triple(*expr, ptr);
3010 }
3011 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003012 expr = triple_misc(state, ptr, 0);
3013 for(; expr; expr = triple_misc(state, ptr, expr)) {
3014 if (*expr) {
3015 unuse_triple(*expr, ptr);
3016 }
3017 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003018 expr = triple_targ(state, ptr, 0);
3019 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003020 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003021 unuse_triple(*expr, ptr);
3022 }
3023 }
3024 /* Reomve ptr from use chains where it is used */
3025 for(set = ptr->use; set; set = next) {
3026 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003027 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003028 expr = triple_rhs(state, set->member, 0);
3029 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3030 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003031 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003032 }
3033 }
3034 expr = triple_lhs(state, set->member, 0);
3035 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3036 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003037 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003038 }
3039 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003040 expr = triple_misc(state, set->member, 0);
3041 for(; expr; expr = triple_misc(state, set->member, expr)) {
3042 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003043 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003044 }
3045 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003046 expr = triple_targ(state, set->member, 0);
3047 for(; expr; expr = triple_targ(state, set->member, expr)) {
3048 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003049 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003050 }
3051 }
3052 unuse_triple(ptr, set->member);
3053 }
3054 free_triple(state, ptr);
3055}
3056
Eric Biederman5ade04a2003-10-22 04:03:46 +00003057static void print_triples(struct compile_state *state);
3058static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003059
Jason Schildt27b85112005-08-10 14:31:52 +00003060#define TOK_UNKNOWN 0
3061#define TOK_SPACE 1
3062#define TOK_SEMI 2
3063#define TOK_LBRACE 3
3064#define TOK_RBRACE 4
3065#define TOK_COMMA 5
3066#define TOK_EQ 6
3067#define TOK_COLON 7
3068#define TOK_LBRACKET 8
3069#define TOK_RBRACKET 9
3070#define TOK_LPAREN 10
3071#define TOK_RPAREN 11
3072#define TOK_STAR 12
3073#define TOK_DOTS 13
3074#define TOK_MORE 14
3075#define TOK_LESS 15
3076#define TOK_TIMESEQ 16
3077#define TOK_DIVEQ 17
3078#define TOK_MODEQ 18
3079#define TOK_PLUSEQ 19
3080#define TOK_MINUSEQ 20
3081#define TOK_SLEQ 21
3082#define TOK_SREQ 22
3083#define TOK_ANDEQ 23
3084#define TOK_XOREQ 24
3085#define TOK_OREQ 25
3086#define TOK_EQEQ 26
3087#define TOK_NOTEQ 27
3088#define TOK_QUEST 28
3089#define TOK_LOGOR 29
3090#define TOK_LOGAND 30
3091#define TOK_OR 31
3092#define TOK_AND 32
3093#define TOK_XOR 33
3094#define TOK_LESSEQ 34
3095#define TOK_MOREEQ 35
3096#define TOK_SL 36
3097#define TOK_SR 37
3098#define TOK_PLUS 38
3099#define TOK_MINUS 39
3100#define TOK_DIV 40
3101#define TOK_MOD 41
3102#define TOK_PLUSPLUS 42
3103#define TOK_MINUSMINUS 43
3104#define TOK_BANG 44
3105#define TOK_ARROW 45
3106#define TOK_DOT 46
3107#define TOK_TILDE 47
3108#define TOK_LIT_STRING 48
3109#define TOK_LIT_CHAR 49
3110#define TOK_LIT_INT 50
3111#define TOK_LIT_FLOAT 51
3112#define TOK_MACRO 52
3113#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003114
Jason Schildt27b85112005-08-10 14:31:52 +00003115#define TOK_IDENT 54
3116#define TOK_STRUCT_NAME 55
3117#define TOK_ENUM_CONST 56
3118#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003119
Jason Schildt27b85112005-08-10 14:31:52 +00003120#define TOK_AUTO 58
3121#define TOK_BREAK 59
3122#define TOK_CASE 60
3123#define TOK_CHAR 61
3124#define TOK_CONST 62
3125#define TOK_CONTINUE 63
3126#define TOK_DEFAULT 64
3127#define TOK_DO 65
3128#define TOK_DOUBLE 66
3129#define TOK_ELSE 67
3130#define TOK_ENUM 68
3131#define TOK_EXTERN 69
3132#define TOK_FLOAT 70
3133#define TOK_FOR 71
3134#define TOK_GOTO 72
3135#define TOK_IF 73
3136#define TOK_INLINE 74
3137#define TOK_INT 75
3138#define TOK_LONG 76
3139#define TOK_REGISTER 77
3140#define TOK_RESTRICT 78
3141#define TOK_RETURN 79
3142#define TOK_SHORT 80
3143#define TOK_SIGNED 81
3144#define TOK_SIZEOF 82
3145#define TOK_STATIC 83
3146#define TOK_STRUCT 84
3147#define TOK_SWITCH 85
3148#define TOK_TYPEDEF 86
3149#define TOK_UNION 87
3150#define TOK_UNSIGNED 88
3151#define TOK_VOID 89
3152#define TOK_VOLATILE 90
3153#define TOK_WHILE 91
3154#define TOK_ASM 92
3155#define TOK_ATTRIBUTE 93
3156#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003157#define TOK_FIRST_KEYWORD TOK_AUTO
3158#define TOK_LAST_KEYWORD TOK_ALIGNOF
3159
Eric Biedermancb364952004-11-15 10:46:44 +00003160#define TOK_MDEFINE 100
3161#define TOK_MDEFINED 101
3162#define TOK_MUNDEF 102
3163#define TOK_MINCLUDE 103
3164#define TOK_MLINE 104
3165#define TOK_MERROR 105
3166#define TOK_MWARNING 106
3167#define TOK_MPRAGMA 107
3168#define TOK_MIFDEF 108
3169#define TOK_MIFNDEF 109
3170#define TOK_MELIF 110
3171#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003172
Eric Biedermancb364952004-11-15 10:46:44 +00003173#define TOK_FIRST_MACRO TOK_MDEFINE
3174#define TOK_LAST_MACRO TOK_MENDIF
Stefan Reinauer14e22772010-04-27 06:56:47 +00003175
Eric Biedermancb364952004-11-15 10:46:44 +00003176#define TOK_MIF 112
3177#define TOK_MELSE 113
3178#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003179
Eric Biedermancb364952004-11-15 10:46:44 +00003180#define TOK_EOL 115
3181#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003182
3183static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003184[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003185[TOK_SPACE ] = ":space:",
3186[TOK_SEMI ] = ";",
3187[TOK_LBRACE ] = "{",
3188[TOK_RBRACE ] = "}",
3189[TOK_COMMA ] = ",",
3190[TOK_EQ ] = "=",
3191[TOK_COLON ] = ":",
3192[TOK_LBRACKET ] = "[",
3193[TOK_RBRACKET ] = "]",
3194[TOK_LPAREN ] = "(",
3195[TOK_RPAREN ] = ")",
3196[TOK_STAR ] = "*",
3197[TOK_DOTS ] = "...",
3198[TOK_MORE ] = ">",
3199[TOK_LESS ] = "<",
3200[TOK_TIMESEQ ] = "*=",
3201[TOK_DIVEQ ] = "/=",
3202[TOK_MODEQ ] = "%=",
3203[TOK_PLUSEQ ] = "+=",
3204[TOK_MINUSEQ ] = "-=",
3205[TOK_SLEQ ] = "<<=",
3206[TOK_SREQ ] = ">>=",
3207[TOK_ANDEQ ] = "&=",
3208[TOK_XOREQ ] = "^=",
3209[TOK_OREQ ] = "|=",
3210[TOK_EQEQ ] = "==",
3211[TOK_NOTEQ ] = "!=",
3212[TOK_QUEST ] = "?",
3213[TOK_LOGOR ] = "||",
3214[TOK_LOGAND ] = "&&",
3215[TOK_OR ] = "|",
3216[TOK_AND ] = "&",
3217[TOK_XOR ] = "^",
3218[TOK_LESSEQ ] = "<=",
3219[TOK_MOREEQ ] = ">=",
3220[TOK_SL ] = "<<",
3221[TOK_SR ] = ">>",
3222[TOK_PLUS ] = "+",
3223[TOK_MINUS ] = "-",
3224[TOK_DIV ] = "/",
3225[TOK_MOD ] = "%",
3226[TOK_PLUSPLUS ] = "++",
3227[TOK_MINUSMINUS ] = "--",
3228[TOK_BANG ] = "!",
3229[TOK_ARROW ] = "->",
3230[TOK_DOT ] = ".",
3231[TOK_TILDE ] = "~",
3232[TOK_LIT_STRING ] = ":string:",
3233[TOK_IDENT ] = ":ident:",
3234[TOK_TYPE_NAME ] = ":typename:",
3235[TOK_LIT_CHAR ] = ":char:",
3236[TOK_LIT_INT ] = ":integer:",
3237[TOK_LIT_FLOAT ] = ":float:",
3238[TOK_MACRO ] = "#",
3239[TOK_CONCATENATE ] = "##",
3240
3241[TOK_AUTO ] = "auto",
3242[TOK_BREAK ] = "break",
3243[TOK_CASE ] = "case",
3244[TOK_CHAR ] = "char",
3245[TOK_CONST ] = "const",
3246[TOK_CONTINUE ] = "continue",
3247[TOK_DEFAULT ] = "default",
3248[TOK_DO ] = "do",
3249[TOK_DOUBLE ] = "double",
3250[TOK_ELSE ] = "else",
3251[TOK_ENUM ] = "enum",
3252[TOK_EXTERN ] = "extern",
3253[TOK_FLOAT ] = "float",
3254[TOK_FOR ] = "for",
3255[TOK_GOTO ] = "goto",
3256[TOK_IF ] = "if",
3257[TOK_INLINE ] = "inline",
3258[TOK_INT ] = "int",
3259[TOK_LONG ] = "long",
3260[TOK_REGISTER ] = "register",
3261[TOK_RESTRICT ] = "restrict",
3262[TOK_RETURN ] = "return",
3263[TOK_SHORT ] = "short",
3264[TOK_SIGNED ] = "signed",
3265[TOK_SIZEOF ] = "sizeof",
3266[TOK_STATIC ] = "static",
3267[TOK_STRUCT ] = "struct",
3268[TOK_SWITCH ] = "switch",
3269[TOK_TYPEDEF ] = "typedef",
3270[TOK_UNION ] = "union",
3271[TOK_UNSIGNED ] = "unsigned",
3272[TOK_VOID ] = "void",
3273[TOK_VOLATILE ] = "volatile",
3274[TOK_WHILE ] = "while",
3275[TOK_ASM ] = "asm",
3276[TOK_ATTRIBUTE ] = "__attribute__",
3277[TOK_ALIGNOF ] = "__alignof__",
3278
Eric Biederman41203d92004-11-08 09:31:09 +00003279[TOK_MDEFINE ] = "#define",
3280[TOK_MDEFINED ] = "#defined",
3281[TOK_MUNDEF ] = "#undef",
3282[TOK_MINCLUDE ] = "#include",
3283[TOK_MLINE ] = "#line",
3284[TOK_MERROR ] = "#error",
3285[TOK_MWARNING ] = "#warning",
3286[TOK_MPRAGMA ] = "#pragma",
3287[TOK_MIFDEF ] = "#ifdef",
3288[TOK_MIFNDEF ] = "#ifndef",
3289[TOK_MELIF ] = "#elif",
3290[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003291
Eric Biederman41203d92004-11-08 09:31:09 +00003292[TOK_MIF ] = "#if",
3293[TOK_MELSE ] = "#else",
3294[TOK_MIDENT ] = "#:ident:",
Stefan Reinauer14e22772010-04-27 06:56:47 +00003295[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003296[TOK_EOF ] = "EOF",
3297};
3298
3299static unsigned int hash(const char *str, int str_len)
3300{
3301 unsigned int hash;
3302 const char *end;
3303 end = str + str_len;
3304 hash = 0;
3305 for(; str < end; str++) {
3306 hash = (hash *263) + *str;
3307 }
3308 hash = hash & (HASH_TABLE_SIZE -1);
3309 return hash;
3310}
3311
3312static struct hash_entry *lookup(
3313 struct compile_state *state, const char *name, int name_len)
3314{
3315 struct hash_entry *entry;
3316 unsigned int index;
3317 index = hash(name, name_len);
3318 entry = state->hash_table[index];
Stefan Reinauer14e22772010-04-27 06:56:47 +00003319 while(entry &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003320 ((entry->name_len != name_len) ||
3321 (memcmp(entry->name, name, name_len) != 0))) {
3322 entry = entry->next;
3323 }
3324 if (!entry) {
3325 char *new_name;
3326 /* Get a private copy of the name */
3327 new_name = xmalloc(name_len + 1, "hash_name");
3328 memcpy(new_name, name, name_len);
3329 new_name[name_len] = '\0';
3330
3331 /* Create a new hash entry */
3332 entry = xcmalloc(sizeof(*entry), "hash_entry");
3333 entry->next = state->hash_table[index];
3334 entry->name = new_name;
3335 entry->name_len = name_len;
3336
3337 /* Place the new entry in the hash table */
3338 state->hash_table[index] = entry;
3339 }
3340 return entry;
3341}
3342
3343static void ident_to_keyword(struct compile_state *state, struct token *tk)
3344{
3345 struct hash_entry *entry;
3346 entry = tk->ident;
3347 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3348 (entry->tok == TOK_ENUM_CONST) ||
Stefan Reinauer14e22772010-04-27 06:56:47 +00003349 ((entry->tok >= TOK_FIRST_KEYWORD) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00003350 (entry->tok <= TOK_LAST_KEYWORD)))) {
3351 tk->tok = entry->tok;
3352 }
3353}
3354
3355static void ident_to_macro(struct compile_state *state, struct token *tk)
3356{
3357 struct hash_entry *entry;
3358 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003359 if (!entry)
3360 return;
3361 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003362 tk->tok = entry->tok;
3363 }
Eric Biederman41203d92004-11-08 09:31:09 +00003364 else if (entry->tok == TOK_IF) {
3365 tk->tok = TOK_MIF;
3366 }
3367 else if (entry->tok == TOK_ELSE) {
3368 tk->tok = TOK_MELSE;
3369 }
3370 else {
3371 tk->tok = TOK_MIDENT;
3372 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003373}
3374
3375static void hash_keyword(
3376 struct compile_state *state, const char *keyword, int tok)
3377{
3378 struct hash_entry *entry;
3379 entry = lookup(state, keyword, strlen(keyword));
3380 if (entry && entry->tok != TOK_UNKNOWN) {
3381 die("keyword %s already hashed", keyword);
3382 }
3383 entry->tok = tok;
3384}
3385
Eric Biederman90089602004-05-28 14:11:54 +00003386static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003387 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003388 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003389{
3390 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003391 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003392 error(state, 0, "%s already defined", ident->name);
3393 }
3394 sym = xcmalloc(sizeof(*sym), "symbol");
3395 sym->ident = ident;
3396 sym->def = def;
3397 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003398 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003399 sym->next = *chain;
3400 *chain = sym;
3401}
3402
Eric Biederman90089602004-05-28 14:11:54 +00003403static void symbol(
3404 struct compile_state *state, struct hash_entry *ident,
3405 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003406{
Eric Biederman90089602004-05-28 14:11:54 +00003407 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3408}
3409
Stefan Reinauer14e22772010-04-27 06:56:47 +00003410static void var_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003411 struct hash_entry *ident, struct triple *def)
3412{
3413 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3414 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003415 }
Eric Biederman90089602004-05-28 14:11:54 +00003416 symbol(state, ident, &ident->sym_ident, def, def->type);
3417}
3418
Stefan Reinauer14e22772010-04-27 06:56:47 +00003419static void label_symbol(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00003420 struct hash_entry *ident, struct triple *label, int depth)
3421{
3422 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003423}
3424
Eric Biedermanb138ac82003-04-22 18:44:01 +00003425static void start_scope(struct compile_state *state)
3426{
3427 state->scope_depth++;
3428}
3429
Eric Biederman90089602004-05-28 14:11:54 +00003430static void end_scope_syms(struct compile_state *state,
3431 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003432{
3433 struct symbol *sym, *next;
3434 sym = *chain;
3435 while(sym && (sym->scope_depth == depth)) {
3436 next = sym->next;
3437 xfree(sym);
3438 sym = next;
3439 }
3440 *chain = sym;
3441}
3442
3443static void end_scope(struct compile_state *state)
3444{
3445 int i;
3446 int depth;
3447 /* Walk through the hash table and remove all symbols
Stefan Reinauer14e22772010-04-27 06:56:47 +00003448 * in the current scope.
Eric Biedermanb138ac82003-04-22 18:44:01 +00003449 */
3450 depth = state->scope_depth;
3451 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3452 struct hash_entry *entry;
3453 entry = state->hash_table[i];
3454 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003455 end_scope_syms(state, &entry->sym_label, depth);
3456 end_scope_syms(state, &entry->sym_tag, depth);
3457 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003458 entry = entry->next;
3459 }
3460 }
3461 state->scope_depth = depth - 1;
3462}
3463
3464static void register_keywords(struct compile_state *state)
3465{
3466 hash_keyword(state, "auto", TOK_AUTO);
3467 hash_keyword(state, "break", TOK_BREAK);
3468 hash_keyword(state, "case", TOK_CASE);
3469 hash_keyword(state, "char", TOK_CHAR);
3470 hash_keyword(state, "const", TOK_CONST);
3471 hash_keyword(state, "continue", TOK_CONTINUE);
3472 hash_keyword(state, "default", TOK_DEFAULT);
3473 hash_keyword(state, "do", TOK_DO);
3474 hash_keyword(state, "double", TOK_DOUBLE);
3475 hash_keyword(state, "else", TOK_ELSE);
3476 hash_keyword(state, "enum", TOK_ENUM);
3477 hash_keyword(state, "extern", TOK_EXTERN);
3478 hash_keyword(state, "float", TOK_FLOAT);
3479 hash_keyword(state, "for", TOK_FOR);
3480 hash_keyword(state, "goto", TOK_GOTO);
3481 hash_keyword(state, "if", TOK_IF);
3482 hash_keyword(state, "inline", TOK_INLINE);
3483 hash_keyword(state, "int", TOK_INT);
3484 hash_keyword(state, "long", TOK_LONG);
3485 hash_keyword(state, "register", TOK_REGISTER);
3486 hash_keyword(state, "restrict", TOK_RESTRICT);
3487 hash_keyword(state, "return", TOK_RETURN);
3488 hash_keyword(state, "short", TOK_SHORT);
3489 hash_keyword(state, "signed", TOK_SIGNED);
3490 hash_keyword(state, "sizeof", TOK_SIZEOF);
3491 hash_keyword(state, "static", TOK_STATIC);
3492 hash_keyword(state, "struct", TOK_STRUCT);
3493 hash_keyword(state, "switch", TOK_SWITCH);
3494 hash_keyword(state, "typedef", TOK_TYPEDEF);
3495 hash_keyword(state, "union", TOK_UNION);
3496 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3497 hash_keyword(state, "void", TOK_VOID);
3498 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003499 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003500 hash_keyword(state, "while", TOK_WHILE);
3501 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003502 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003503 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3504 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3505}
3506
3507static void register_macro_keywords(struct compile_state *state)
3508{
Eric Biederman41203d92004-11-08 09:31:09 +00003509 hash_keyword(state, "define", TOK_MDEFINE);
3510 hash_keyword(state, "defined", TOK_MDEFINED);
3511 hash_keyword(state, "undef", TOK_MUNDEF);
3512 hash_keyword(state, "include", TOK_MINCLUDE);
3513 hash_keyword(state, "line", TOK_MLINE);
3514 hash_keyword(state, "error", TOK_MERROR);
3515 hash_keyword(state, "warning", TOK_MWARNING);
3516 hash_keyword(state, "pragma", TOK_MPRAGMA);
3517 hash_keyword(state, "ifdef", TOK_MIFDEF);
3518 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3519 hash_keyword(state, "elif", TOK_MELIF);
3520 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003521}
3522
Eric Biederman90089602004-05-28 14:11:54 +00003523
3524static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3525{
3526 if (ident->sym_define != 0) {
3527 struct macro *macro;
3528 struct macro_arg *arg, *anext;
3529 macro = ident->sym_define;
3530 ident->sym_define = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003531
Eric Biederman90089602004-05-28 14:11:54 +00003532 /* Free the macro arguments... */
3533 anext = macro->args;
3534 while(anext) {
3535 arg = anext;
3536 anext = arg->next;
3537 xfree(arg);
3538 }
3539
3540 /* Free the macro buffer */
3541 xfree(macro->buf);
3542
3543 /* Now free the macro itself */
3544 xfree(macro);
3545 }
3546}
3547
Stefan Reinauer14e22772010-04-27 06:56:47 +00003548static void do_define_macro(struct compile_state *state,
3549 struct hash_entry *ident, const char *body,
Eric Biedermancb364952004-11-15 10:46:44 +00003550 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003551{
3552 struct macro *macro;
3553 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003554 size_t body_len;
3555
3556 /* Find the length of the body */
3557 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003558 macro = ident->sym_define;
3559 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003560 int identical_bodies, identical_args;
3561 struct macro_arg *oarg;
3562 /* Explicitly allow identical redfinitions of the same macro */
Stefan Reinauer14e22772010-04-27 06:56:47 +00003563 identical_bodies =
Eric Biedermancb364952004-11-15 10:46:44 +00003564 (macro->buf_len == body_len) &&
3565 (memcmp(macro->buf, body, body_len) == 0);
3566 identical_args = macro->argc == argc;
3567 oarg = macro->args;
3568 arg = args;
3569 while(identical_args && arg) {
3570 identical_args = oarg->ident == arg->ident;
3571 arg = arg->next;
3572 oarg = oarg->next;
3573 }
3574 if (identical_bodies && identical_args) {
3575 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003576 return;
3577 }
3578 error(state, 0, "macro %s already defined\n", ident->name);
3579 }
3580#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003581 fprintf(state->errout, "#define %s: `%*.*s'\n",
3582 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003583#endif
3584 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003585 macro->ident = ident;
3586 macro->buf = body;
3587 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003588 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003589 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003590
3591 ident->sym_define = macro;
3592}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003593
Eric Biedermancb364952004-11-15 10:46:44 +00003594static void define_macro(
3595 struct compile_state *state,
3596 struct hash_entry *ident,
3597 const char *body, int body_len,
3598 int argc, struct macro_arg *args)
3599{
3600 char *buf;
3601 buf = xmalloc(body_len + 1, "macro buf");
3602 memcpy(buf, body, body_len);
3603 buf[body_len] = '\0';
3604 do_define_macro(state, ident, buf, argc, args);
3605}
Eric Biederman90089602004-05-28 14:11:54 +00003606
3607static void register_builtin_macro(struct compile_state *state,
3608 const char *name, const char *value)
3609{
3610 struct hash_entry *ident;
3611
3612 if (value[0] == '(') {
3613 internal_error(state, 0, "Builtin macros with arguments not supported");
3614 }
3615 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003616 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003617}
3618
3619static void register_builtin_macros(struct compile_state *state)
3620{
3621 char buf[30];
3622 char scratch[30];
3623 time_t now;
3624 struct tm *tm;
3625 now = time(NULL);
3626 tm = localtime(&now);
3627
3628 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3629 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3630 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3631 register_builtin_macro(state, "__LINE__", "54321");
3632
3633 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3634 sprintf(buf, "\"%s\"", scratch);
3635 register_builtin_macro(state, "__DATE__", buf);
3636
3637 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3638 sprintf(buf, "\"%s\"", scratch);
3639 register_builtin_macro(state, "__TIME__", buf);
3640
3641 /* I can't be a conforming implementation of C :( */
3642 register_builtin_macro(state, "__STDC__", "0");
3643 /* In particular I don't conform to C99 */
3644 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
Stefan Reinauer14e22772010-04-27 06:56:47 +00003645
Eric Biederman90089602004-05-28 14:11:54 +00003646}
3647
3648static void process_cmdline_macros(struct compile_state *state)
3649{
3650 const char **macro, *name;
3651 struct hash_entry *ident;
3652 for(macro = state->compiler->defines; (name = *macro); macro++) {
3653 const char *body;
3654 size_t name_len;
3655
3656 name_len = strlen(name);
3657 body = strchr(name, '=');
3658 if (!body) {
3659 body = "\0";
3660 } else {
3661 name_len = body - name;
3662 body++;
3663 }
3664 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003665 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003666 }
3667 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3668 ident = lookup(state, name, strlen(name));
3669 undef_macro(state, ident);
3670 }
3671}
3672
Eric Biedermanb138ac82003-04-22 18:44:01 +00003673static int spacep(int c)
3674{
3675 int ret = 0;
3676 switch(c) {
3677 case ' ':
3678 case '\t':
3679 case '\f':
3680 case '\v':
3681 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003682 ret = 1;
3683 break;
3684 }
3685 return ret;
3686}
3687
Eric Biedermanb138ac82003-04-22 18:44:01 +00003688static int digitp(int c)
3689{
3690 int ret = 0;
3691 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003692 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003693 case '5': case '6': case '7': case '8': case '9':
3694 ret = 1;
3695 break;
3696 }
3697 return ret;
3698}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003699static int digval(int c)
3700{
3701 int val = -1;
3702 if ((c >= '0') && (c <= '9')) {
3703 val = c - '0';
3704 }
3705 return val;
3706}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003707
3708static int hexdigitp(int c)
3709{
3710 int ret = 0;
3711 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003712 case '0': case '1': case '2': case '3': case '4':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003713 case '5': case '6': case '7': case '8': case '9':
3714 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3715 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3716 ret = 1;
3717 break;
3718 }
3719 return ret;
3720}
Stefan Reinauer14e22772010-04-27 06:56:47 +00003721static int hexdigval(int c)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003722{
3723 int val = -1;
3724 if ((c >= '0') && (c <= '9')) {
3725 val = c - '0';
3726 }
3727 else if ((c >= 'A') && (c <= 'F')) {
3728 val = 10 + (c - 'A');
3729 }
3730 else if ((c >= 'a') && (c <= 'f')) {
3731 val = 10 + (c - 'a');
3732 }
3733 return val;
3734}
3735
3736static int octdigitp(int c)
3737{
3738 int ret = 0;
3739 switch(c) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00003740 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003741 case '4': case '5': case '6': case '7':
3742 ret = 1;
3743 break;
3744 }
3745 return ret;
3746}
3747static int octdigval(int c)
3748{
3749 int val = -1;
3750 if ((c >= '0') && (c <= '7')) {
3751 val = c - '0';
3752 }
3753 return val;
3754}
3755
3756static int letterp(int c)
3757{
3758 int ret = 0;
3759 switch(c) {
3760 case 'a': case 'b': case 'c': case 'd': case 'e':
3761 case 'f': case 'g': case 'h': case 'i': case 'j':
3762 case 'k': case 'l': case 'm': case 'n': case 'o':
3763 case 'p': case 'q': case 'r': case 's': case 't':
3764 case 'u': case 'v': case 'w': case 'x': case 'y':
3765 case 'z':
3766 case 'A': case 'B': case 'C': case 'D': case 'E':
3767 case 'F': case 'G': case 'H': case 'I': case 'J':
3768 case 'K': case 'L': case 'M': case 'N': case 'O':
3769 case 'P': case 'Q': case 'R': case 'S': case 'T':
3770 case 'U': case 'V': case 'W': case 'X': case 'Y':
3771 case 'Z':
3772 case '_':
3773 ret = 1;
3774 break;
3775 }
3776 return ret;
3777}
3778
Eric Biederman90089602004-05-28 14:11:54 +00003779static const char *identifier(const char *str, const char *end)
3780{
3781 if (letterp(*str)) {
3782 for(; str < end; str++) {
3783 int c;
3784 c = *str;
3785 if (!letterp(c) && !digitp(c)) {
3786 break;
3787 }
3788 }
3789 }
3790 return str;
3791}
3792
Eric Biedermanb138ac82003-04-22 18:44:01 +00003793static int char_value(struct compile_state *state,
3794 const signed char **strp, const signed char *end)
3795{
3796 const signed char *str;
3797 int c;
3798 str = *strp;
3799 c = *str++;
3800 if ((c == '\\') && (str < end)) {
3801 switch(*str) {
3802 case 'n': c = '\n'; str++; break;
3803 case 't': c = '\t'; str++; break;
3804 case 'v': c = '\v'; str++; break;
3805 case 'b': c = '\b'; str++; break;
3806 case 'r': c = '\r'; str++; break;
3807 case 'f': c = '\f'; str++; break;
3808 case 'a': c = '\a'; str++; break;
3809 case '\\': c = '\\'; str++; break;
3810 case '?': c = '?'; str++; break;
3811 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003812 case '"': c = '"'; str++; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003813 case 'x':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003814 c = 0;
3815 str++;
3816 while((str < end) && hexdigitp(*str)) {
3817 c <<= 4;
3818 c += hexdigval(*str);
3819 str++;
3820 }
3821 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00003822 case '0': case '1': case '2': case '3':
Eric Biedermanb138ac82003-04-22 18:44:01 +00003823 case '4': case '5': case '6': case '7':
3824 c = 0;
3825 while((str < end) && octdigitp(*str)) {
3826 c <<= 3;
3827 c += octdigval(*str);
3828 str++;
3829 }
3830 break;
3831 default:
3832 error(state, 0, "Invalid character constant");
3833 break;
3834 }
3835 }
3836 *strp = str;
3837 return c;
3838}
3839
Eric Biedermancb364952004-11-15 10:46:44 +00003840static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003841{
Eric Biedermancb364952004-11-15 10:46:44 +00003842 const char *end = file->buf + file->size;
3843 while(pos < end) {
3844 /* Lookup the character */
3845 int size = 1;
3846 int c = *pos;
3847 /* Is this a trigraph? */
3848 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003849 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003850 {
3851 switch(pos[2]) {
3852 case '=': c = '#'; break;
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 }
3862 if (c != '?') {
3863 size = 3;
3864 }
3865 }
3866 /* Is this an escaped newline? */
3867 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003868 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003869 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003870 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003871 /* At the start of a line just eat it */
3872 if (pos == file->pos) {
3873 file->line++;
3874 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003875 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003876 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003877 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003878 }
3879 /* Do I need to ga any farther? */
3880 else if (index == 0) {
3881 break;
3882 }
3883 /* Process a normal character */
3884 else {
3885 pos += size;
3886 index -= 1;
3887 }
3888 }
3889 return pos;
3890}
3891
3892static int get_char(struct file_state *file, const char *pos)
3893{
3894 const char *end = file->buf + file->size;
3895 int c;
3896 c = -1;
3897 pos = next_char(file, pos, 0);
3898 if (pos < end) {
3899 /* Lookup the character */
3900 c = *pos;
3901 /* If it is a trigraph get the trigraph value */
3902 if (file->trigraphs &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00003903 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
Eric Biedermancb364952004-11-15 10:46:44 +00003904 {
3905 switch(pos[2]) {
3906 case '=': c = '#'; break;
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 }
3916 }
3917 }
3918 return c;
3919}
3920
3921static void eat_chars(struct file_state *file, const char *targ)
3922{
3923 const char *pos = file->pos;
3924 while(pos < targ) {
3925 /* Do we have a newline? */
3926 if (pos[0] == '\n') {
3927 file->line++;
3928 file->report_line++;
3929 file->line_start = pos + 1;
3930 }
3931 pos++;
3932 }
3933 file->pos = pos;
3934}
3935
3936
3937static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3938{
3939 size_t len;
3940 len = 0;
3941 while(src < end) {
3942 src = next_char(file, src, 1);
3943 len++;
3944 }
3945 return len;
3946}
3947
Stefan Reinauer14e22772010-04-27 06:56:47 +00003948static void char_strcpy(char *dest,
Eric Biedermancb364952004-11-15 10:46:44 +00003949 struct file_state *file, const char *src, const char *end)
3950{
3951 while(src < end) {
3952 int c;
3953 c = get_char(file, src);
3954 src = next_char(file, src, 1);
3955 *dest++ = c;
3956 }
3957}
3958
Stefan Reinauer14e22772010-04-27 06:56:47 +00003959static char *char_strdup(struct file_state *file,
Eric Biedermancb364952004-11-15 10:46:44 +00003960 const char *start, const char *end, const char *id)
3961{
3962 char *str;
3963 size_t str_len;
3964 str_len = char_strlen(file, start, end);
3965 str = xcmalloc(str_len + 1, id);
3966 char_strcpy(str, file, start, end);
3967 str[str_len] = '\0';
3968 return str;
3969}
3970
3971static const char *after_digits(struct file_state *file, const char *ptr)
3972{
3973 while(digitp(get_char(file, ptr))) {
3974 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003975 }
3976 return ptr;
3977}
3978
Eric Biedermancb364952004-11-15 10:46:44 +00003979static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003980{
Eric Biedermancb364952004-11-15 10:46:44 +00003981 while(octdigitp(get_char(file, ptr))) {
3982 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003983 }
3984 return ptr;
3985}
3986
Eric Biedermancb364952004-11-15 10:46:44 +00003987static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003988{
Eric Biedermancb364952004-11-15 10:46:44 +00003989 while(hexdigitp(get_char(file, ptr))) {
3990 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003991 }
3992 return ptr;
3993}
3994
Eric Biedermancb364952004-11-15 10:46:44 +00003995static const char *after_alnums(struct file_state *file, const char *ptr)
3996{
3997 int c;
3998 c = get_char(file, ptr);
3999 while(letterp(c) || digitp(c)) {
4000 ptr = next_char(file, ptr, 1);
4001 c = get_char(file, ptr);
4002 }
4003 return ptr;
4004}
4005
4006static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004007 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004008{
4009 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004010
Eric Biedermanb138ac82003-04-22 18:44:01 +00004011 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004012 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004013
4014 /* Store the copy in the token */
4015 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004016 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004017}
4018
Stefan Reinauer14e22772010-04-27 06:56:47 +00004019static void raw_next_token(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004020 struct file_state *file, struct token *tk)
4021{
4022 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004023 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004024 const char *tokp;
4025 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004026 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004027
Eric Biedermanb138ac82003-04-22 18:44:01 +00004028 tk->str_len = 0;
4029 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004030 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004031 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004032 c = get_char(file, tokp);
4033 tokp = next_char(file, tokp, 1);
4034 eat = 0;
4035 c1 = get_char(file, tokp);
4036 c2 = get_char(file, next_char(file, tokp, 1));
4037 c3 = get_char(file, next_char(file, tokp, 2));
4038
4039 /* The end of the file */
4040 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004041 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004042 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004043 /* Whitespace */
4044 else if (spacep(c)) {
4045 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004046 while (spacep(get_char(file, tokp))) {
4047 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004048 }
4049 }
4050 /* EOL Comments */
4051 else if ((c == '/') && (c1 == '/')) {
4052 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004053 tokp = next_char(file, tokp, 1);
4054 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004055 /* Advance to the next character only after we verify
Stefan Reinauer14e22772010-04-27 06:56:47 +00004056 * the current character is not a newline.
Eric Biederman57183382006-12-02 16:48:48 +00004057 * EOL is special to the preprocessor so we don't
4058 * want to loose any.
4059 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004060 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004061 break;
4062 }
Eric Biederman57183382006-12-02 16:48:48 +00004063 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004064 }
4065 }
4066 /* Comments */
4067 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004068 tokp = next_char(file, tokp, 2);
4069 c = c2;
4070 while((c1 = get_char(file, tokp)) != -1) {
4071 tokp = next_char(file, tokp, 1);
4072 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004073 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004074 break;
4075 }
Eric Biedermancb364952004-11-15 10:46:44 +00004076 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004077 }
4078 if (tok == TOK_UNKNOWN) {
4079 error(state, 0, "unterminated comment");
4080 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004081 }
4082 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004083 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004084 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004085
Eric Biedermancb364952004-11-15 10:46:44 +00004086 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004087 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004088 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004089 }
Eric Biedermancb364952004-11-15 10:46:44 +00004090 while((c = get_char(file, tokp)) != -1) {
4091 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004092 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004093 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004094 }
Eric Biedermancb364952004-11-15 10:46:44 +00004095 else if (c == '\\') {
4096 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004097 }
4098 else if (c == '"') {
4099 tok = TOK_LIT_STRING;
4100 break;
4101 }
4102 }
4103 if (tok == TOK_UNKNOWN) {
4104 error(state, 0, "unterminated string constant");
4105 }
Eric Biedermancb364952004-11-15 10:46:44 +00004106 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004107 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004108 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004109
4110 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004111 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004112 }
4113 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004114 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +01004115 int multiline;
Eric Biedermancb364952004-11-15 10:46:44 +00004116
Eric Biedermancb364952004-11-15 10:46:44 +00004117 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004118 if (c == 'L') {
Eric Biedermancb364952004-11-15 10:46:44 +00004119 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004120 }
Eric Biedermancb364952004-11-15 10:46:44 +00004121 while((c = get_char(file, tokp)) != -1) {
4122 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004123 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004124 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004125 }
Eric Biedermancb364952004-11-15 10:46:44 +00004126 else if (c == '\\') {
4127 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004128 }
4129 else if (c == '\'') {
4130 tok = TOK_LIT_CHAR;
4131 break;
4132 }
4133 }
4134 if (tok == TOK_UNKNOWN) {
4135 error(state, 0, "unterminated character constant");
4136 }
Eric Biedermancb364952004-11-15 10:46:44 +00004137 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004138 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004139 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004140
4141 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004142 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004143 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004144 /* integer and floating constants
Eric Biedermanb138ac82003-04-22 18:44:01 +00004145 * Integer Constants
4146 * {digits}
4147 * 0[Xx]{hexdigits}
4148 * 0{octdigit}+
Stefan Reinauer14e22772010-04-27 06:56:47 +00004149 *
Eric Biedermanb138ac82003-04-22 18:44:01 +00004150 * Floating constants
4151 * {digits}.{digits}[Ee][+-]?{digits}
4152 * {digits}.{digits}
4153 * {digits}[Ee][+-]?{digits}
4154 * .{digits}[Ee][+-]?{digits}
4155 * .{digits}
4156 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004157 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004158 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004159 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004160 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004161 is_float = 0;
4162 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004163 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004164 }
4165 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004166 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004167 }
Eric Biedermancb364952004-11-15 10:46:44 +00004168 cn = get_char(file, next);
4169 if (cn == '.') {
4170 next = next_char(file, next, 1);
4171 next = after_digits(file, next);
4172 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004173 }
Eric Biedermancb364952004-11-15 10:46:44 +00004174 cn = get_char(file, next);
4175 if ((cn == 'e') || (cn == 'E')) {
4176 const char *new;
4177 next = next_char(file, next, 1);
4178 cn = get_char(file, next);
4179 if ((cn == '+') || (cn == '-')) {
4180 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004181 }
Eric Biedermancb364952004-11-15 10:46:44 +00004182 new = after_digits(file, next);
4183 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004184 next = new;
4185 }
4186 if (is_float) {
4187 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004188 cn = get_char(file, next);
4189 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4190 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004191 }
4192 }
4193 if (!is_float && digitp(c)) {
4194 tok = TOK_LIT_INT;
4195 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004196 next = next_char(file, tokp, 1);
4197 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004198 }
4199 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004200 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004201 }
4202 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004203 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004204 }
4205 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004206 cn = get_char(file, next);
4207 if ((cn == 'u') || (cn == 'U')) {
4208 next = next_char(file, next, 1);
4209 cn = get_char(file, next);
4210 if ((cn == 'l') || (cn == 'L')) {
4211 next = next_char(file, next, 1);
4212 cn = get_char(file, next);
4213 }
4214 if ((cn == 'l') || (cn == 'L')) {
4215 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004216 }
4217 }
Eric Biedermancb364952004-11-15 10:46:44 +00004218 else if ((cn == 'l') || (cn == 'L')) {
4219 next = next_char(file, next, 1);
4220 cn = get_char(file, next);
4221 if ((cn == 'l') || (cn == 'L')) {
4222 next = next_char(file, next, 1);
4223 cn = get_char(file, next);
4224 }
4225 if ((cn == 'u') || (cn == 'U')) {
4226 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004227 }
4228 }
4229 }
Eric Biedermancb364952004-11-15 10:46:44 +00004230 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004231
4232 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004233 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004234 }
4235 /* identifiers */
4236 else if (letterp(c)) {
4237 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004238
4239 /* Find and save the identifier string */
4240 tokp = after_alnums(file, tokp);
4241 save_string(file, tk, token, tokp, "identifier");
4242
4243 /* Look up to see which identifier it is */
4244 tk->ident = lookup(state, tk->val.str, tk->str_len);
4245
4246 /* Free the identifier string */
4247 tk->str_len = 0;
4248 xfree(tk->val.str);
4249
Eric Biederman90089602004-05-28 14:11:54 +00004250 /* See if this identifier can be macro expanded */
4251 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004252 c = get_char(file, tokp);
4253 if (c == '$') {
4254 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004255 tk->val.notmacro = 1;
4256 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004257 }
4258 /* C99 alternate macro characters */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004259 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004260 eat += 3;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004261 tok = TOK_CONCATENATE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004262 }
Eric Biedermancb364952004-11-15 10:46:44 +00004263 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4264 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4265 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4266 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4267 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4268 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4269 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4270 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4271 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4272 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4273 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4274 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4275 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4276 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4277 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4278 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4279 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4280 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4281 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4282 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4283 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4284 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4285 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4286 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4287 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4288 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4289 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4290 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004291 else if (c == ';') { tok = TOK_SEMI; }
4292 else if (c == '{') { tok = TOK_LBRACE; }
4293 else if (c == '}') { tok = TOK_RBRACE; }
4294 else if (c == ',') { tok = TOK_COMMA; }
4295 else if (c == '=') { tok = TOK_EQ; }
4296 else if (c == ':') { tok = TOK_COLON; }
4297 else if (c == '[') { tok = TOK_LBRACKET; }
4298 else if (c == ']') { tok = TOK_RBRACKET; }
4299 else if (c == '(') { tok = TOK_LPAREN; }
4300 else if (c == ')') { tok = TOK_RPAREN; }
4301 else if (c == '*') { tok = TOK_STAR; }
4302 else if (c == '>') { tok = TOK_MORE; }
4303 else if (c == '<') { tok = TOK_LESS; }
4304 else if (c == '?') { tok = TOK_QUEST; }
4305 else if (c == '|') { tok = TOK_OR; }
4306 else if (c == '&') { tok = TOK_AND; }
4307 else if (c == '^') { tok = TOK_XOR; }
4308 else if (c == '+') { tok = TOK_PLUS; }
4309 else if (c == '-') { tok = TOK_MINUS; }
4310 else if (c == '/') { tok = TOK_DIV; }
4311 else if (c == '%') { tok = TOK_MOD; }
4312 else if (c == '!') { tok = TOK_BANG; }
4313 else if (c == '.') { tok = TOK_DOT; }
4314 else if (c == '~') { tok = TOK_TILDE; }
4315 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004316 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004317
Eric Biedermancb364952004-11-15 10:46:44 +00004318 tokp = next_char(file, tokp, eat);
4319 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004320 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004321 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004322}
4323
4324static void check_tok(struct compile_state *state, struct token *tk, int tok)
4325{
4326 if (tk->tok != tok) {
4327 const char *name1, *name2;
4328 name1 = tokens[tk->tok];
4329 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004330 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004331 name2 = tk->ident->name;
4332 }
4333 error(state, 0, "\tfound %s %s expected %s",
4334 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004335 }
4336}
4337
Eric Biederman90089602004-05-28 14:11:54 +00004338struct macro_arg_value {
4339 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004340 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004341 size_t len;
4342};
4343static struct macro_arg_value *read_macro_args(
Stefan Reinauer14e22772010-04-27 06:56:47 +00004344 struct compile_state *state, struct macro *macro,
Eric Biederman90089602004-05-28 14:11:54 +00004345 struct file_state *file, struct token *tk)
4346{
4347 struct macro_arg_value *argv;
4348 struct macro_arg *arg;
4349 int paren_depth;
4350 int i;
4351
4352 if (macro->argc == 0) {
4353 do {
4354 raw_next_token(state, file, tk);
4355 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004356 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004357 }
4358 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4359 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4360 argv[i].value = 0;
4361 argv[i].len = 0;
4362 argv[i].ident = arg->ident;
4363 }
4364 paren_depth = 0;
4365 i = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004366
Eric Biederman90089602004-05-28 14:11:54 +00004367 for(;;) {
4368 const char *start;
4369 size_t len;
4370 start = file->pos;
4371 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004372
Eric Biederman90089602004-05-28 14:11:54 +00004373 if (!paren_depth && (tk->tok == TOK_COMMA) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004374 (argv[i].ident != state->i___VA_ARGS__))
Eric Biederman90089602004-05-28 14:11:54 +00004375 {
4376 i++;
4377 if (i >= macro->argc) {
4378 error(state, 0, "too many args to %s\n",
4379 macro->ident->name);
4380 }
4381 continue;
4382 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004383
Eric Biederman90089602004-05-28 14:11:54 +00004384 if (tk->tok == TOK_LPAREN) {
4385 paren_depth++;
4386 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004387
Eric Biederman90089602004-05-28 14:11:54 +00004388 if (tk->tok == TOK_RPAREN) {
4389 if (paren_depth == 0) {
4390 break;
4391 }
4392 paren_depth--;
4393 }
4394 if (tk->tok == TOK_EOF) {
4395 error(state, 0, "End of file encountered while parsing macro arguments");
4396 }
Eric Biedermancb364952004-11-15 10:46:44 +00004397
4398 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004399 argv[i].value = xrealloc(
4400 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004401 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004402 argv[i].len += len;
4403 }
4404 if (i != macro->argc -1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00004405 error(state, 0, "missing %s arg %d\n",
Eric Biederman90089602004-05-28 14:11:54 +00004406 macro->ident->name, i +2);
4407 }
4408 return argv;
4409}
4410
4411
4412static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4413{
4414 int i;
4415 for(i = 0; i < macro->argc; i++) {
4416 xfree(argv[i].value);
4417 }
4418 xfree(argv);
4419}
4420
4421struct macro_buf {
4422 char *str;
4423 size_t len, pos;
4424};
4425
Eric Biedermancb364952004-11-15 10:46:44 +00004426static void grow_macro_buf(struct compile_state *state,
4427 const char *id, struct macro_buf *buf,
4428 size_t grow)
4429{
4430 if ((buf->pos + grow) >= buf->len) {
4431 buf->str = xrealloc(buf->str, buf->len + grow, id);
4432 buf->len += grow;
4433 }
4434}
4435
Eric Biederman90089602004-05-28 14:11:54 +00004436static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004437 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004438 const char *fstart, size_t flen)
4439{
Eric Biedermancb364952004-11-15 10:46:44 +00004440 grow_macro_buf(state, id, buf, flen);
4441 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004442#if 0
4443 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4444 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004445 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004446#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004447 buf->pos += flen;
4448}
4449
4450
4451static void append_macro_chars(struct compile_state *state,
4452 const char *id, struct macro_buf *buf,
4453 struct file_state *file, const char *start, const char *end)
4454{
4455 size_t flen;
4456 flen = char_strlen(file, start, end);
4457 grow_macro_buf(state, id, buf, flen);
4458 char_strcpy(buf->str + buf->pos, file, start, end);
4459#if 0
4460 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4461 buf->pos, buf->pos, buf->str,
4462 flen, flen, buf->str + buf->pos);
4463#endif
Eric Biederman90089602004-05-28 14:11:54 +00004464 buf->pos += flen;
4465}
4466
Stefan Reinauer14e22772010-04-27 06:56:47 +00004467static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004468 struct file_state **filep, struct token *tk);
4469
Stefan Reinauer14e22772010-04-27 06:56:47 +00004470static void macro_expand_args(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004471 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4472{
Eric Biedermancb364952004-11-15 10:46:44 +00004473 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004474
Eric Biederman90089602004-05-28 14:11:54 +00004475 for(i = 0; i < macro->argc; i++) {
4476 struct file_state fmacro, *file;
4477 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004478
Eric Biedermancb364952004-11-15 10:46:44 +00004479 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004480 fmacro.basename = argv[i].ident->name;
4481 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004482 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004483 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004484 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004485 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004486 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004487 fmacro.report_line = 1;
4488 fmacro.report_name = fmacro.basename;
4489 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004490 fmacro.macro = 1;
4491 fmacro.trigraphs = 0;
4492 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004493
4494 buf.len = argv[i].len;
4495 buf.str = xmalloc(buf.len, argv[i].ident->name);
4496 buf.pos = 0;
4497
4498 file = &fmacro;
4499 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004500 raw_next_token(state, file, tk);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004501
Eric Biedermancb364952004-11-15 10:46:44 +00004502 /* If we have recursed into another macro body
4503 * get out of it.
4504 */
Eric Biederman90089602004-05-28 14:11:54 +00004505 if (tk->tok == TOK_EOF) {
4506 struct file_state *old;
4507 old = file;
4508 file = file->prev;
4509 if (!file) {
4510 break;
4511 }
4512 /* old->basename is used keep it */
4513 xfree(old->dirname);
4514 xfree(old->buf);
4515 xfree(old);
4516 continue;
4517 }
4518 else if (tk->ident && tk->ident->sym_define) {
4519 if (compile_macro(state, &file, tk)) {
4520 continue;
4521 }
4522 }
4523
Eric Biedermancb364952004-11-15 10:46:44 +00004524 append_macro_chars(state, macro->ident->name, &buf,
4525 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004526 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004527
Eric Biederman90089602004-05-28 14:11:54 +00004528 xfree(argv[i].value);
4529 argv[i].value = buf.str;
4530 argv[i].len = buf.pos;
4531 }
4532 return;
4533}
4534
4535static void expand_macro(struct compile_state *state,
4536 struct macro *macro, struct macro_buf *buf,
4537 struct macro_arg_value *argv, struct token *tk)
4538{
4539 struct file_state fmacro;
4540 const char space[] = " ";
4541 const char *fstart;
4542 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004543 int i, j;
4544
4545 /* Place the macro body in a dummy file */
4546 fmacro.prev = 0;
4547 fmacro.basename = macro->ident->name;
4548 fmacro.dirname = "";
4549 fmacro.buf = macro->buf;
4550 fmacro.size = macro->buf_len;
4551 fmacro.pos = fmacro.buf;
4552 fmacro.line = 1;
4553 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004554 fmacro.report_line = 1;
4555 fmacro.report_name = fmacro.basename;
4556 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004557 fmacro.macro = 1;
4558 fmacro.trigraphs = 0;
4559 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004560
Eric Biedermancb364952004-11-15 10:46:44 +00004561 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004562 buf->len = macro->buf_len + 3;
4563 buf->str = xmalloc(buf->len, macro->ident->name);
4564 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004565
Eric Biederman90089602004-05-28 14:11:54 +00004566 fstart = fmacro.pos;
4567 raw_next_token(state, &fmacro, tk);
4568 while(tk->tok != TOK_EOF) {
4569 flen = fmacro.pos - fstart;
4570 switch(tk->tok) {
4571 case TOK_IDENT:
4572 for(i = 0; i < macro->argc; i++) {
4573 if (argv[i].ident == tk->ident) {
4574 break;
4575 }
4576 }
4577 if (i >= macro->argc) {
4578 break;
4579 }
4580 /* Substitute macro parameter */
4581 fstart = argv[i].value;
4582 flen = argv[i].len;
4583 break;
4584 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004585 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004586 break;
4587 }
4588 do {
4589 raw_next_token(state, &fmacro, tk);
4590 } while(tk->tok == TOK_SPACE);
4591 check_tok(state, tk, TOK_IDENT);
4592 for(i = 0; i < macro->argc; i++) {
4593 if (argv[i].ident == tk->ident) {
4594 break;
4595 }
4596 }
4597 if (i >= macro->argc) {
4598 error(state, 0, "parameter `%s' not found",
4599 tk->ident->name);
4600 }
4601 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004602 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004603 for(j = 0; j < argv[i].len; j++) {
4604 char *str = argv[i].value + j;
4605 size_t len = 1;
4606 if (*str == '\\') {
4607 str = "\\";
4608 len = 2;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004609 }
Eric Biederman90089602004-05-28 14:11:54 +00004610 else if (*str == '"') {
4611 str = "\\\"";
4612 len = 2;
4613 }
Eric Biedermancb364952004-11-15 10:46:44 +00004614 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004615 }
Eric Biedermancb364952004-11-15 10:46:44 +00004616 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004617 fstart = 0;
4618 flen = 0;
4619 break;
4620 case TOK_CONCATENATE:
4621 /* Concatenate tokens */
4622 /* Delete the previous whitespace token */
4623 if (buf->str[buf->pos - 1] == ' ') {
4624 buf->pos -= 1;
4625 }
4626 /* Skip the next sequence of whitspace tokens */
4627 do {
4628 fstart = fmacro.pos;
4629 raw_next_token(state, &fmacro, tk);
4630 } while(tk->tok == TOK_SPACE);
4631 /* Restart at the top of the loop.
4632 * I need to process the non white space token.
4633 */
4634 continue;
4635 break;
4636 case TOK_SPACE:
4637 /* Collapse multiple spaces into one */
4638 if (buf->str[buf->pos - 1] != ' ') {
4639 fstart = space;
4640 flen = 1;
4641 } else {
4642 fstart = 0;
4643 flen = 0;
4644 }
4645 break;
4646 default:
4647 break;
4648 }
4649
Eric Biedermancb364952004-11-15 10:46:44 +00004650 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004651
Eric Biederman90089602004-05-28 14:11:54 +00004652 fstart = fmacro.pos;
4653 raw_next_token(state, &fmacro, tk);
4654 }
4655}
4656
4657static void tag_macro_name(struct compile_state *state,
4658 struct macro *macro, struct macro_buf *buf,
4659 struct token *tk)
4660{
4661 /* Guard all instances of the macro name in the replacement
4662 * text from further macro expansion.
4663 */
4664 struct file_state fmacro;
4665 const char *fstart;
4666 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004667
4668 /* Put the old macro expansion buffer in a file */
4669 fmacro.prev = 0;
4670 fmacro.basename = macro->ident->name;
4671 fmacro.dirname = "";
4672 fmacro.buf = buf->str;
4673 fmacro.size = buf->pos;
4674 fmacro.pos = fmacro.buf;
4675 fmacro.line = 1;
4676 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004677 fmacro.report_line = 1;
4678 fmacro.report_name = fmacro.basename;
4679 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004680 fmacro.macro = 1;
4681 fmacro.trigraphs = 0;
4682 fmacro.join_lines = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004683
Eric Biedermancb364952004-11-15 10:46:44 +00004684 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004685 buf->len = macro->buf_len + 3;
4686 buf->str = xmalloc(buf->len, macro->ident->name);
4687 buf->pos = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00004688
Eric Biederman90089602004-05-28 14:11:54 +00004689 fstart = fmacro.pos;
4690 raw_next_token(state, &fmacro, tk);
4691 while(tk->tok != TOK_EOF) {
4692 flen = fmacro.pos - fstart;
4693 if ((tk->tok == TOK_IDENT) &&
4694 (tk->ident == macro->ident) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00004695 (tk->val.notmacro == 0))
Eric Biedermancb364952004-11-15 10:46:44 +00004696 {
4697 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004698 fstart = "$";
4699 flen = 1;
4700 }
4701
Eric Biedermancb364952004-11-15 10:46:44 +00004702 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004703
Eric Biederman90089602004-05-28 14:11:54 +00004704 fstart = fmacro.pos;
4705 raw_next_token(state, &fmacro, tk);
4706 }
4707 xfree(fmacro.buf);
4708}
Eric Biedermancb364952004-11-15 10:46:44 +00004709
Stefan Reinauer14e22772010-04-27 06:56:47 +00004710static int compile_macro(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00004711 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004712{
4713 struct file_state *file;
4714 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004715 struct macro *macro;
4716 struct macro_arg_value *argv;
4717 struct macro_buf buf;
4718
4719#if 0
4720 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4721#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004722 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004723 macro = ident->sym_define;
4724
4725 /* If this token comes from a macro expansion ignore it */
4726 if (tk->val.notmacro) {
4727 return 0;
4728 }
4729 /* If I am a function like macro and the identifier is not followed
4730 * by a left parenthesis, do nothing.
4731 */
Eric Biedermancb364952004-11-15 10:46:44 +00004732 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004733 return 0;
4734 }
4735
4736 /* Read in the macro arguments */
4737 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004738 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004739 raw_next_token(state, *filep, tk);
4740 check_tok(state, tk, TOK_LPAREN);
4741
4742 argv = read_macro_args(state, macro, *filep, tk);
4743
4744 check_tok(state, tk, TOK_RPAREN);
4745 }
4746 /* Macro expand the macro arguments */
4747 macro_expand_args(state, macro, argv, tk);
4748
4749 buf.str = 0;
4750 buf.len = 0;
4751 buf.pos = 0;
4752 if (ident == state->i___FILE__) {
4753 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4754 buf.str = xmalloc(buf.len, ident->name);
4755 sprintf(buf.str, "\"%s\"", state->file->basename);
4756 buf.pos = strlen(buf.str);
4757 }
4758 else if (ident == state->i___LINE__) {
4759 buf.len = 30;
4760 buf.str = xmalloc(buf.len, ident->name);
4761 sprintf(buf.str, "%d", state->file->line);
4762 buf.pos = strlen(buf.str);
4763 }
4764 else {
4765 expand_macro(state, macro, &buf, argv, tk);
4766 }
4767 /* Tag the macro name with a $ so it will no longer
4768 * be regonized as a canidate for macro expansion.
4769 */
4770 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004771
4772#if 0
4773 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4774 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4775#endif
4776
4777 free_macro_args(macro, argv);
4778
Eric Biedermanb138ac82003-04-22 18:44:01 +00004779 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004780 file->prev = *filep;
4781 file->basename = xstrdup(ident->name);
4782 file->dirname = xstrdup("");
4783 file->buf = buf.str;
4784 file->size = buf.pos;
4785 file->pos = file->buf;
4786 file->line = 1;
4787 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004788 file->report_line = 1;
4789 file->report_name = file->basename;
4790 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004791 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004792 file->trigraphs = 0;
4793 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004794 *filep = file;
4795 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004796}
4797
Eric Biederman90089602004-05-28 14:11:54 +00004798static void eat_tokens(struct compile_state *state, int targ_tok)
4799{
4800 if (state->eat_depth > 0) {
4801 internal_error(state, 0, "Already eating...");
4802 }
4803 state->eat_depth = state->if_depth;
4804 state->eat_targ = targ_tok;
4805}
4806static int if_eat(struct compile_state *state)
4807{
4808 return state->eat_depth > 0;
4809}
4810static int if_value(struct compile_state *state)
4811{
4812 int index, offset;
4813 index = state->if_depth / CHAR_BIT;
4814 offset = state->if_depth % CHAR_BIT;
4815 return !!(state->if_bytes[index] & (1 << (offset)));
4816}
Stefan Reinauer14e22772010-04-27 06:56:47 +00004817static void set_if_value(struct compile_state *state, int value)
Eric Biederman90089602004-05-28 14:11:54 +00004818{
4819 int index, offset;
4820 index = state->if_depth / CHAR_BIT;
4821 offset = state->if_depth % CHAR_BIT;
4822
4823 state->if_bytes[index] &= ~(1 << offset);
4824 if (value) {
4825 state->if_bytes[index] |= (1 << offset);
4826 }
4827}
4828static void in_if(struct compile_state *state, const char *name)
4829{
4830 if (state->if_depth <= 0) {
4831 error(state, 0, "%s without #if", name);
4832 }
4833}
4834static void enter_if(struct compile_state *state)
4835{
4836 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004837 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004838 error(state, 0, "#if depth too great");
4839 }
4840}
4841static void reenter_if(struct compile_state *state, const char *name)
4842{
4843 in_if(state, name);
4844 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004845 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004846 state->eat_depth = 0;
4847 state->eat_targ = 0;
4848 }
4849}
4850static void enter_else(struct compile_state *state, const char *name)
4851{
4852 in_if(state, name);
4853 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004854 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004855 state->eat_depth = 0;
4856 state->eat_targ = 0;
4857 }
4858}
4859static void exit_if(struct compile_state *state, const char *name)
4860{
4861 in_if(state, name);
4862 if (state->eat_depth == state->if_depth) {
4863 state->eat_depth = 0;
4864 state->eat_targ = 0;
4865 }
4866 state->if_depth -= 1;
4867}
4868
Eric Biedermancb364952004-11-15 10:46:44 +00004869static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004870{
4871 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004872 int rescan;
4873
Eric Biedermancb364952004-11-15 10:46:44 +00004874 file = state->file;
4875 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004876 do {
4877 rescan = 0;
4878 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004879 /* Exit out of an include directive or macro call */
Stefan Reinauer14e22772010-04-27 06:56:47 +00004880 if ((tk->tok == TOK_EOF) &&
4881 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004882 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004883 state->file = file->prev;
4884 /* file->basename is used keep it */
4885 xfree(file->dirname);
4886 xfree(file->buf);
4887 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004888 file = 0;
4889 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004890 rescan = 1;
4891 }
Eric Biederman41203d92004-11-08 09:31:09 +00004892 } while(rescan);
4893}
4894
Eric Biedermancb364952004-11-15 10:46:44 +00004895static void pp_token(struct compile_state *state, struct token *tk)
4896{
Eric Biedermancb364952004-11-15 10:46:44 +00004897 int rescan;
4898
4899 raw_token(state, tk);
4900 do {
4901 rescan = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004902 if (tk->tok == TOK_SPACE) {
4903 raw_token(state, tk);
4904 rescan = 1;
4905 }
4906 else if (tk->tok == TOK_IDENT) {
4907 if (state->token_base == 0) {
4908 ident_to_keyword(state, tk);
4909 } else {
4910 ident_to_macro(state, tk);
4911 }
4912 }
4913 } while(rescan);
4914}
4915
Eric Biederman41203d92004-11-08 09:31:09 +00004916static void preprocess(struct compile_state *state, struct token *tk);
4917
4918static void token(struct compile_state *state, struct token *tk)
4919{
4920 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004921 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004922 do {
4923 rescan = 0;
4924 /* Process a macro directive */
4925 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004926 /* Only match preprocessor directives at the start of a line */
4927 const char *ptr;
4928 ptr = state->file->line_start;
4929 while((ptr < tk->pos)
4930 && spacep(get_char(state->file, ptr)))
4931 {
4932 ptr = next_char(state->file, ptr, 1);
4933 }
4934 if (ptr == tk->pos) {
4935 preprocess(state, tk);
4936 rescan = 1;
4937 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004938 }
Eric Biederman41203d92004-11-08 09:31:09 +00004939 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004940 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004941 rescan = compile_macro(state, &state->file, tk);
4942 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004943 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004944 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004945 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00004946 /* Eat tokens disabled by the preprocessor
4947 * (Unless we are parsing a preprocessor directive
Eric Biedermancb364952004-11-15 10:46:44 +00004948 */
Eric Biedermana649a412004-11-09 00:35:39 +00004949 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004950 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004951 rescan = 1;
4952 }
Eric Biedermana649a412004-11-09 00:35:39 +00004953 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004954 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004955 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004956 rescan = 1;
4957 }
Eric Biedermancb364952004-11-15 10:46:44 +00004958 /* Error on unknown tokens */
4959 else if (tk->tok == TOK_UNKNOWN) {
4960 error(state, 0, "unknown token");
4961 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004962 } while(rescan);
4963}
4964
Eric Biederman41203d92004-11-08 09:31:09 +00004965
4966static inline struct token *get_token(struct compile_state *state, int offset)
4967{
4968 int index;
4969 index = state->token_base + offset;
4970 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4971 internal_error(state, 0, "token array to small");
4972 }
4973 return &state->token[index];
4974}
4975
4976static struct token *do_eat_token(struct compile_state *state, int tok)
4977{
4978 struct token *tk;
4979 int i;
4980 check_tok(state, get_token(state, 1), tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +00004981
Eric Biederman41203d92004-11-08 09:31:09 +00004982 /* Free the old token value */
4983 tk = get_token(state, 0);
4984 if (tk->str_len) {
4985 memset((void *)tk->val.str, -1, tk->str_len);
4986 xfree(tk->val.str);
4987 }
4988 /* Overwrite the old token with newer tokens */
4989 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4990 state->token[i] = state->token[i + 1];
4991 }
4992 /* Clear the last token */
4993 memset(&state->token[i], 0, sizeof(state->token[i]));
4994 state->token[i].tok = -1;
4995
4996 /* Return the token */
4997 return tk;
4998}
4999
Eric Biedermancb364952004-11-15 10:46:44 +00005000static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005001{
5002 struct token *tk1;
5003 tk1 = get_token(state, 1);
5004 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005005 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005006 }
5007 return tk1->tok;
5008}
5009
Eric Biedermancb364952004-11-15 10:46:44 +00005010static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005011{
Eric Biedermancb364952004-11-15 10:46:44 +00005012 raw_peek(state);
5013 return do_eat_token(state, tok);
5014}
5015
5016static int pp_peek(struct compile_state *state)
5017{
5018 struct token *tk1;
5019 tk1 = get_token(state, 1);
5020 if (tk1->tok == -1) {
5021 pp_token(state, tk1);
5022 }
5023 return tk1->tok;
5024}
5025
5026static struct token *pp_eat(struct compile_state *state, int tok)
5027{
5028 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005029 return do_eat_token(state, tok);
5030}
5031
Eric Biedermanb138ac82003-04-22 18:44:01 +00005032static int peek(struct compile_state *state)
5033{
Eric Biederman41203d92004-11-08 09:31:09 +00005034 struct token *tk1;
5035 tk1 = get_token(state, 1);
5036 if (tk1->tok == -1) {
5037 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005038 }
Eric Biederman41203d92004-11-08 09:31:09 +00005039 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005040}
5041
5042static int peek2(struct compile_state *state)
5043{
Eric Biederman41203d92004-11-08 09:31:09 +00005044 struct token *tk1, *tk2;
5045 tk1 = get_token(state, 1);
5046 tk2 = get_token(state, 2);
5047 if (tk1->tok == -1) {
5048 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005049 }
Eric Biederman41203d92004-11-08 09:31:09 +00005050 if (tk2->tok == -1) {
5051 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005052 }
Eric Biederman41203d92004-11-08 09:31:09 +00005053 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005054}
5055
Eric Biederman41203d92004-11-08 09:31:09 +00005056static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005057{
Eric Biederman90089602004-05-28 14:11:54 +00005058 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005059 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005060}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005061
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005062static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005063{
Eric Biederman90089602004-05-28 14:11:54 +00005064 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005065 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005066 int subdir_len;
5067 struct file_state *file;
5068 char *basename;
5069 file = xmalloc(sizeof(*file), "file_state");
5070
5071 base = strrchr(filename, '/');
5072 subdir = filename;
5073 if (base != 0) {
5074 subdir_len = base - filename;
5075 base++;
5076 }
5077 else {
5078 base = filename;
5079 subdir_len = 0;
5080 }
5081 basename = xmalloc(strlen(base) +1, "basename");
5082 strcpy(basename, base);
5083 file->basename = basename;
5084
5085 if (getcwd(cwd, sizeof(cwd)) == 0) {
5086 die("cwd buffer to small");
5087 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005088 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005089 file->dirname = xmalloc(subdir_len + 1, "dirname");
5090 memcpy(file->dirname, subdir, subdir_len);
5091 file->dirname[subdir_len] = '\0';
5092 }
5093 else {
Eric Biederman90089602004-05-28 14:11:54 +00005094 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005095 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005096 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005097 /* Find the appropriate directory... */
5098 dir = 0;
5099 if (!state->file && exists(cwd, filename)) {
5100 dir = cwd;
5101 }
5102 if (local && state->file && exists(state->file->dirname, filename)) {
5103 dir = state->file->dirname;
5104 }
Eric Biederman90089602004-05-28 14:11:54 +00005105 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005106 if (exists(*path, filename)) {
5107 dir = *path;
5108 }
5109 }
5110 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005111 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005112 }
5113 dirlen = strlen(dir);
5114 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5115 memcpy(file->dirname, dir, dirlen);
5116 file->dirname[dirlen] = '/';
5117 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5118 file->dirname[dirlen + 1 + subdir_len] = '\0';
5119 }
5120 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005121
5122 file->pos = file->buf;
5123 file->line_start = file->pos;
5124 file->line = 1;
5125
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005126 file->report_line = 1;
5127 file->report_name = file->basename;
5128 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005129 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005130 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5131 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005132
Eric Biedermanb138ac82003-04-22 18:44:01 +00005133 file->prev = state->file;
5134 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005135}
5136
Eric Biederman41203d92004-11-08 09:31:09 +00005137static struct triple *constant_expr(struct compile_state *state);
5138static void integral(struct compile_state *state, struct triple *def);
5139
5140static int mcexpr(struct compile_state *state)
5141{
5142 struct triple *cvalue;
5143 cvalue = constant_expr(state);
5144 integral(state, cvalue);
5145 if (cvalue->op != OP_INTCONST) {
5146 error(state, 0, "integer constant expected");
5147 }
5148 return cvalue->u.cval != 0;
5149}
5150
5151static void preprocess(struct compile_state *state, struct token *current_token)
5152{
5153 /* Doing much more with the preprocessor would require
5154 * a parser and a major restructuring.
5155 * Postpone that for later.
5156 */
Eric Biederman41203d92004-11-08 09:31:09 +00005157 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005158 int tok;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005159
Eric Biedermancb364952004-11-15 10:46:44 +00005160 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005161
5162 old_token_base = state->token_base;
5163 state->token_base = current_token - state->token;
5164
Eric Biedermancb364952004-11-15 10:46:44 +00005165 tok = pp_peek(state);
5166 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005167 case TOK_LIT_INT:
5168 {
5169 struct token *tk;
5170 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005171 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005172 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005173 /* I have a preprocessor line marker parse it */
5174 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005175 const char *token, *base;
5176 char *name, *dir;
5177 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005178 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005179 name = xmalloc(tk->str_len, "report_name");
5180 token = tk->val.str + 1;
5181 base = strrchr(token, '/');
5182 name_len = tk->str_len -2;
5183 if (base != 0) {
5184 dir_len = base - token;
5185 base++;
5186 name_len -= base - token;
5187 } else {
5188 dir_len = 0;
5189 base = token;
5190 }
5191 memcpy(name, base, name_len);
5192 name[name_len] = '\0';
5193 dir = xmalloc(dir_len + 1, "report_dir");
5194 memcpy(dir, token, dir_len);
5195 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005196 state->file->report_line = override_line - 1;
5197 state->file->report_name = name;
5198 state->file->report_dir = dir;
5199 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005200 }
5201 break;
5202 }
5203 case TOK_MLINE:
5204 {
5205 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005206 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005207 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005208 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5209 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005210 const char *token, *base;
5211 char *name, *dir;
5212 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005213 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005214 name = xmalloc(tk->str_len, "report_name");
5215 token = tk->val.str + 1;
5216 base = strrchr(token, '/');
5217 name_len = tk->str_len - 2;
5218 if (base != 0) {
5219 dir_len = base - token;
5220 base++;
5221 name_len -= base - token;
5222 } else {
5223 dir_len = 0;
5224 base = token;
5225 }
5226 memcpy(name, base, name_len);
5227 name[name_len] = '\0';
5228 dir = xmalloc(dir_len + 1, "report_dir");
5229 memcpy(dir, token, dir_len);
5230 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005231 state->file->report_name = name;
5232 state->file->report_dir = dir;
5233 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005234 }
5235 break;
5236 }
5237 case TOK_MUNDEF:
5238 {
5239 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005240 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005241 if (if_eat(state)) /* quit early when #if'd out */
5242 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005243
Eric Biedermancb364952004-11-15 10:46:44 +00005244 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005245
5246 undef_macro(state, ident);
5247 break;
5248 }
5249 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005250 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005251 if (if_eat(state)) /* quit early when #if'd out */
5252 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005253 warning(state, 0, "Ignoring pragma");
Eric Biederman41203d92004-11-08 09:31:09 +00005254 break;
5255 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005256 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005257 reenter_if(state, "#elif");
5258 if (if_eat(state)) /* quit early when #if'd out */
5259 break;
5260 /* If the #if was taken the #elif just disables the following code */
5261 if (if_value(state)) {
5262 eat_tokens(state, TOK_MENDIF);
5263 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005264 /* If the previous #if was not taken see if the #elif enables the
Eric Biederman41203d92004-11-08 09:31:09 +00005265 * trailing code.
5266 */
5267 else {
5268 set_if_value(state, mcexpr(state));
5269 if (!if_value(state)) {
5270 eat_tokens(state, TOK_MELSE);
5271 }
5272 }
5273 break;
5274 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005275 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005276 enter_if(state);
5277 if (if_eat(state)) /* quit early when #if'd out */
5278 break;
5279 set_if_value(state, mcexpr(state));
5280 if (!if_value(state)) {
5281 eat_tokens(state, TOK_MELSE);
5282 }
5283 break;
5284 case TOK_MIFNDEF:
5285 {
5286 struct hash_entry *ident;
5287
Eric Biedermancb364952004-11-15 10:46:44 +00005288 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005289 enter_if(state);
5290 if (if_eat(state)) /* quit early when #if'd out */
5291 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005292 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005293 set_if_value(state, ident->sym_define == 0);
5294 if (!if_value(state)) {
5295 eat_tokens(state, TOK_MELSE);
5296 }
5297 break;
5298 }
5299 case TOK_MIFDEF:
5300 {
5301 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005302 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005303 enter_if(state);
5304 if (if_eat(state)) /* quit early when #if'd out */
5305 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005306 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005307 set_if_value(state, ident->sym_define != 0);
5308 if (!if_value(state)) {
5309 eat_tokens(state, TOK_MELSE);
5310 }
5311 break;
5312 }
5313 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005314 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005315 enter_else(state, "#else");
5316 if (!if_eat(state) && if_value(state)) {
5317 eat_tokens(state, TOK_MENDIF);
5318 }
5319 break;
5320 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005321 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005322 exit_if(state, "#endif");
5323 break;
5324 case TOK_MDEFINE:
5325 {
5326 struct hash_entry *ident;
5327 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005328 const char *mstart, *mend;
5329 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005330
Eric Biedermancb364952004-11-15 10:46:44 +00005331 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005332 if (if_eat(state)) /* quit early when #if'd out */
5333 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005334 ident = pp_eat(state, TOK_MIDENT)->ident;
5335 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005336 args = 0;
5337 larg = &args;
5338
Eric Biederman41203d92004-11-08 09:31:09 +00005339 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005340 if (raw_peek(state) == TOK_LPAREN) {
5341 raw_eat(state, TOK_LPAREN);
5342 argc += 1;
5343
Eric Biederman41203d92004-11-08 09:31:09 +00005344 for(;;) {
5345 struct macro_arg *narg, *arg;
5346 struct hash_entry *aident;
5347 int tok;
5348
Eric Biedermancb364952004-11-15 10:46:44 +00005349 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005350 if (!args && (tok == TOK_RPAREN)) {
5351 break;
5352 }
5353 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005354 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005355 aident = state->i___VA_ARGS__;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005356 }
Eric Biederman41203d92004-11-08 09:31:09 +00005357 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005358 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005359 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005360
Eric Biederman41203d92004-11-08 09:31:09 +00005361 narg = xcmalloc(sizeof(*arg), "macro arg");
5362 narg->ident = aident;
5363
5364 /* Verify I don't have a duplicate identifier */
5365 for(arg = args; arg; arg = arg->next) {
5366 if (arg->ident == narg->ident) {
5367 error(state, 0, "Duplicate macro arg `%s'",
5368 narg->ident->name);
5369 }
5370 }
5371 /* Add the new argument to the end of the list */
5372 *larg = narg;
5373 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005374 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005375
5376 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005377 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005378 break;
5379 }
Eric Biedermancb364952004-11-15 10:46:44 +00005380 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005381 }
Eric Biedermancb364952004-11-15 10:46:44 +00005382 pp_eat(state, TOK_RPAREN);
5383 }
5384 /* Remove leading whitespace */
5385 while(raw_peek(state) == TOK_SPACE) {
5386 raw_eat(state, TOK_SPACE);
5387 }
Eric Biederman41203d92004-11-08 09:31:09 +00005388
Eric Biedermancb364952004-11-15 10:46:44 +00005389 /* Remember the start of the macro body */
5390 tok = raw_peek(state);
5391 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005392
Eric Biedermancb364952004-11-15 10:46:44 +00005393 /* Find the end of the macro */
5394 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5395 raw_eat(state, tok);
5396 /* Remember the end of the last non space token */
5397 raw_peek(state);
5398 if (tok != TOK_SPACE) {
5399 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005400 }
5401 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005402
Eric Biedermancb364952004-11-15 10:46:44 +00005403 /* Now that I have found the body defined the token */
5404 do_define_macro(state, ident,
5405 char_strdup(state->file, mstart, mend, "macro buf"),
5406 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005407 break;
5408 }
5409 case TOK_MERROR:
5410 {
Eric Biedermancb364952004-11-15 10:46:44 +00005411 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005412 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005413
Eric Biedermancb364952004-11-15 10:46:44 +00005414 pp_eat(state, TOK_MERROR);
5415 /* Find the start of the line */
5416 raw_peek(state);
5417 start = get_token(state, 1)->pos;
5418
Eric Biederman41203d92004-11-08 09:31:09 +00005419 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005420 while((tok = raw_peek(state)) != TOK_EOL) {
5421 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005422 }
Eric Biedermancb364952004-11-15 10:46:44 +00005423 end = get_token(state, 1)->pos;
5424 len = end - start;
5425 if (!if_eat(state)) {
5426 error(state, 0, "%*.*s", len, len, start);
5427 }
Eric Biederman41203d92004-11-08 09:31:09 +00005428 break;
5429 }
5430 case TOK_MWARNING:
5431 {
Eric Biedermancb364952004-11-15 10:46:44 +00005432 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005433 int len;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005434
Eric Biedermancb364952004-11-15 10:46:44 +00005435 pp_eat(state, TOK_MWARNING);
5436
5437 /* Find the start of the line */
5438 raw_peek(state);
5439 start = get_token(state, 1)->pos;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005440
Eric Biederman41203d92004-11-08 09:31:09 +00005441 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005442 while((tok = raw_peek(state)) != TOK_EOL) {
5443 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005444 }
Eric Biedermancb364952004-11-15 10:46:44 +00005445 end = get_token(state, 1)->pos;
5446 len = end - start;
5447 if (!if_eat(state)) {
5448 warning(state, 0, "%*.*s", len, len, start);
5449 }
Eric Biederman41203d92004-11-08 09:31:09 +00005450 break;
5451 }
5452 case TOK_MINCLUDE:
5453 {
5454 char *name;
5455 int local;
5456 local = 0;
5457 name = 0;
5458
Eric Biedermancb364952004-11-15 10:46:44 +00005459 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005460 if (if_eat(state)) {
5461 /* Find the end of the line */
5462 while((tok = raw_peek(state)) != TOK_EOL) {
5463 raw_eat(state, tok);
5464 }
5465 break;
5466 }
Eric Biederman41203d92004-11-08 09:31:09 +00005467 tok = peek(state);
5468 if (tok == TOK_LIT_STRING) {
5469 struct token *tk;
5470 const char *token;
5471 int name_len;
5472 tk = eat(state, TOK_LIT_STRING);
5473 name = xmalloc(tk->str_len, "include");
5474 token = tk->val.str +1;
5475 name_len = tk->str_len -2;
5476 if (*token == '"') {
5477 token++;
5478 name_len--;
5479 }
5480 memcpy(name, token, name_len);
5481 name[name_len] = '\0';
5482 local = 1;
5483 }
5484 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005485 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005486 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005487
5488 buf.len = 40;
5489 buf.str = xmalloc(buf.len, "include");
5490 buf.pos = 0;
5491
5492 tok = peek(state);
5493 while((tok != TOK_MORE) &&
5494 (tok != TOK_EOL) && (tok != TOK_EOF))
5495 {
5496 struct token *tk;
5497 tk = eat(state, tok);
5498 append_macro_chars(state, "include", &buf,
5499 state->file, tk->pos, state->file->pos);
5500 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005501 }
Eric Biedermancb364952004-11-15 10:46:44 +00005502 append_macro_text(state, "include", &buf, "\0", 1);
5503 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005504 error(state, 0, "Unterminated include directive");
5505 }
Eric Biederman41203d92004-11-08 09:31:09 +00005506 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005507 local = 0;
5508 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005509 }
5510 else {
5511 error(state, 0, "Invalid include directive");
5512 }
5513 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005514 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005515 error(state, 0, "garbage after include directive");
5516 }
5517 if (!if_eat(state)) {
5518 compile_file(state, name, local);
5519 }
5520 xfree(name);
5521 break;
5522 }
5523 case TOK_EOL:
5524 /* Ignore # without a follwing ident */
5525 break;
5526 default:
5527 {
5528 const char *name1, *name2;
5529 name1 = tokens[tok];
5530 name2 = "";
5531 if (tok == TOK_MIDENT) {
5532 name2 = get_token(state, 1)->ident->name;
5533 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00005534 error(state, 0, "Invalid preprocessor directive: %s %s",
Eric Biederman41203d92004-11-08 09:31:09 +00005535 name1, name2);
5536 break;
5537 }
5538 }
5539 /* Consume the rest of the macro line */
5540 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005541 tok = pp_peek(state);
5542 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005543 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5544 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005545 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005546 return;
5547}
5548
Eric Biederman0babc1c2003-05-09 02:39:00 +00005549/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005550
5551static struct type *new_type(
5552 unsigned int type, struct type *left, struct type *right)
5553{
5554 struct type *result;
5555 result = xmalloc(sizeof(*result), "type");
5556 result->type = type;
5557 result->left = left;
5558 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005559 result->field_ident = 0;
5560 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005561 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005562 return result;
5563}
5564
5565static struct type *clone_type(unsigned int specifiers, struct type *old)
5566{
5567 struct type *result;
5568 result = xmalloc(sizeof(*result), "type");
5569 memcpy(result, old, sizeof(*result));
5570 result->type &= TYPE_MASK;
5571 result->type |= specifiers;
5572 return result;
5573}
5574
Eric Biederman90089602004-05-28 14:11:54 +00005575static struct type *dup_type(struct compile_state *state, struct type *orig)
5576{
5577 struct type *new;
5578 new = xcmalloc(sizeof(*new), "type");
5579 new->type = orig->type;
5580 new->field_ident = orig->field_ident;
5581 new->type_ident = orig->type_ident;
5582 new->elements = orig->elements;
5583 if (orig->left) {
5584 new->left = dup_type(state, orig->left);
5585 }
5586 if (orig->right) {
5587 new->right = dup_type(state, orig->right);
5588 }
5589 return new;
5590}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005591
Eric Biederman90089602004-05-28 14:11:54 +00005592
5593static struct type *invalid_type(struct compile_state *state, struct type *type)
5594{
5595 struct type *invalid, *member;
5596 invalid = 0;
5597 if (!type) {
5598 internal_error(state, 0, "type missing?");
5599 }
5600 switch(type->type & TYPE_MASK) {
5601 case TYPE_VOID:
5602 case TYPE_CHAR: case TYPE_UCHAR:
5603 case TYPE_SHORT: case TYPE_USHORT:
5604 case TYPE_INT: case TYPE_UINT:
5605 case TYPE_LONG: case TYPE_ULONG:
5606 case TYPE_LLONG: case TYPE_ULLONG:
5607 case TYPE_POINTER:
5608 case TYPE_ENUM:
5609 break;
5610 case TYPE_BITFIELD:
5611 invalid = invalid_type(state, type->left);
5612 break;
5613 case TYPE_ARRAY:
5614 invalid = invalid_type(state, type->left);
5615 break;
5616 case TYPE_STRUCT:
5617 case TYPE_TUPLE:
5618 member = type->left;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005619 while(member && (invalid == 0) &&
Eric Biederman90089602004-05-28 14:11:54 +00005620 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5621 invalid = invalid_type(state, member->left);
5622 member = member->right;
5623 }
5624 if (!invalid) {
5625 invalid = invalid_type(state, member);
5626 }
5627 break;
5628 case TYPE_UNION:
5629 case TYPE_JOIN:
5630 member = type->left;
5631 while(member && (invalid == 0) &&
5632 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5633 invalid = invalid_type(state, member->left);
5634 member = member->right;
5635 }
5636 if (!invalid) {
5637 invalid = invalid_type(state, member);
5638 }
5639 break;
5640 default:
5641 invalid = type;
5642 break;
5643 }
5644 return invalid;
Stefan Reinauer14e22772010-04-27 06:56:47 +00005645
Eric Biederman90089602004-05-28 14:11:54 +00005646}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005647
5648#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005649#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005650static inline ulong_t mask_uint(ulong_t x)
5651{
5652 if (SIZEOF_INT < SIZEOF_LONG) {
Stefan Reinauer50542a82007-10-24 11:14:14 +00005653 ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005654 x &= mask;
5655 }
5656 return x;
5657}
5658#define MASK_UINT(X) (mask_uint(X))
5659#define MASK_ULONG(X) (X)
5660
Eric Biederman90089602004-05-28 14:11:54 +00005661static struct type void_type = { .type = TYPE_VOID };
5662static struct type char_type = { .type = TYPE_CHAR };
5663static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005664#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005665static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005666#endif
Eric Biederman90089602004-05-28 14:11:54 +00005667static struct type ushort_type = { .type = TYPE_USHORT };
5668static struct type int_type = { .type = TYPE_INT };
5669static struct type uint_type = { .type = TYPE_UINT };
5670static struct type long_type = { .type = TYPE_LONG };
5671static struct type ulong_type = { .type = TYPE_ULONG };
5672static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005673
Eric Biederman5ade04a2003-10-22 04:03:46 +00005674static struct type void_ptr_type = {
5675 .type = TYPE_POINTER,
5676 .left = &void_type,
5677};
5678
Stefan Reinauer50542a82007-10-24 11:14:14 +00005679#if DEBUG_ROMCC_WARNING
Stefan Reinauer14e22772010-04-27 06:56:47 +00005680static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005681 .type = TYPE_FUNCTION,
5682 .left = &void_type,
5683 .right = &void_type,
5684};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005685#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005686
Eric Biederman90089602004-05-28 14:11:54 +00005687static size_t bits_to_bytes(size_t size)
5688{
5689 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5690}
5691
Eric Biedermanb138ac82003-04-22 18:44:01 +00005692static struct triple *variable(struct compile_state *state, struct type *type)
5693{
5694 struct triple *result;
5695 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005696 result = triple(state, OP_ADECL, type, 0, 0);
5697 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005698 }
5699 else {
5700 result = triple(state, OP_SDECL, type, 0, 0);
5701 }
5702 return result;
5703}
5704
5705static void stor_of(FILE *fp, struct type *type)
5706{
5707 switch(type->type & STOR_MASK) {
5708 case STOR_AUTO:
5709 fprintf(fp, "auto ");
5710 break;
5711 case STOR_STATIC:
5712 fprintf(fp, "static ");
5713 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005714 case STOR_LOCAL:
5715 fprintf(fp, "local ");
5716 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005717 case STOR_EXTERN:
5718 fprintf(fp, "extern ");
5719 break;
5720 case STOR_REGISTER:
5721 fprintf(fp, "register ");
5722 break;
5723 case STOR_TYPEDEF:
5724 fprintf(fp, "typedef ");
5725 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005726 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005727 fprintf(fp, "inline ");
5728 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005729 case STOR_INLINE | STOR_STATIC:
5730 fprintf(fp, "static inline");
5731 break;
5732 case STOR_INLINE | STOR_EXTERN:
5733 fprintf(fp, "extern inline");
5734 break;
5735 default:
5736 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5737 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005738 }
5739}
5740static void qual_of(FILE *fp, struct type *type)
5741{
5742 if (type->type & QUAL_CONST) {
5743 fprintf(fp, " const");
5744 }
5745 if (type->type & QUAL_VOLATILE) {
5746 fprintf(fp, " volatile");
5747 }
5748 if (type->type & QUAL_RESTRICT) {
5749 fprintf(fp, " restrict");
5750 }
5751}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005752
Eric Biedermanb138ac82003-04-22 18:44:01 +00005753static void name_of(FILE *fp, struct type *type)
5754{
Eric Biederman90089602004-05-28 14:11:54 +00005755 unsigned int base_type;
5756 base_type = type->type & TYPE_MASK;
5757 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5758 stor_of(fp, type);
5759 }
5760 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005761 case TYPE_VOID:
5762 fprintf(fp, "void");
5763 qual_of(fp, type);
5764 break;
5765 case TYPE_CHAR:
5766 fprintf(fp, "signed char");
5767 qual_of(fp, type);
5768 break;
5769 case TYPE_UCHAR:
5770 fprintf(fp, "unsigned char");
5771 qual_of(fp, type);
5772 break;
5773 case TYPE_SHORT:
5774 fprintf(fp, "signed short");
5775 qual_of(fp, type);
5776 break;
5777 case TYPE_USHORT:
5778 fprintf(fp, "unsigned short");
5779 qual_of(fp, type);
5780 break;
5781 case TYPE_INT:
5782 fprintf(fp, "signed int");
5783 qual_of(fp, type);
5784 break;
5785 case TYPE_UINT:
5786 fprintf(fp, "unsigned int");
5787 qual_of(fp, type);
5788 break;
5789 case TYPE_LONG:
5790 fprintf(fp, "signed long");
5791 qual_of(fp, type);
5792 break;
5793 case TYPE_ULONG:
5794 fprintf(fp, "unsigned long");
5795 qual_of(fp, type);
5796 break;
5797 case TYPE_POINTER:
5798 name_of(fp, type->left);
5799 fprintf(fp, " * ");
5800 qual_of(fp, type);
5801 break;
5802 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005803 name_of(fp, type->left);
5804 fprintf(fp, ", ");
5805 name_of(fp, type->right);
5806 break;
Eric Biederman90089602004-05-28 14:11:54 +00005807 case TYPE_OVERLAP:
5808 name_of(fp, type->left);
5809 fprintf(fp, ",| ");
5810 name_of(fp, type->right);
5811 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005812 case TYPE_ENUM:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005813 fprintf(fp, "enum %s",
Eric Biederman90089602004-05-28 14:11:54 +00005814 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005815 qual_of(fp, type);
5816 break;
5817 case TYPE_STRUCT:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005818 fprintf(fp, "struct %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005819 (type->type_ident)? type->type_ident->name : "");
5820 name_of(fp, type->left);
5821 fprintf(fp, " } ");
5822 qual_of(fp, type);
5823 break;
5824 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005825 fprintf(fp, "union %s { ",
Eric Biederman90089602004-05-28 14:11:54 +00005826 (type->type_ident)? type->type_ident->name : "");
5827 name_of(fp, type->left);
5828 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005829 qual_of(fp, type);
5830 break;
5831 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005832 name_of(fp, type->left);
5833 fprintf(fp, " (*)(");
5834 name_of(fp, type->right);
5835 fprintf(fp, ")");
5836 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005837 case TYPE_ARRAY:
5838 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005839 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005840 break;
Eric Biederman90089602004-05-28 14:11:54 +00005841 case TYPE_TUPLE:
Stefan Reinauer14e22772010-04-27 06:56:47 +00005842 fprintf(fp, "tuple { ");
Eric Biederman90089602004-05-28 14:11:54 +00005843 name_of(fp, type->left);
5844 fprintf(fp, " } ");
5845 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005846 break;
Eric Biederman90089602004-05-28 14:11:54 +00005847 case TYPE_JOIN:
5848 fprintf(fp, "join { ");
5849 name_of(fp, type->left);
5850 fprintf(fp, " } ");
5851 qual_of(fp, type);
5852 break;
5853 case TYPE_BITFIELD:
5854 name_of(fp, type->left);
5855 fprintf(fp, " : %d ", type->elements);
5856 qual_of(fp, type);
5857 break;
5858 case TYPE_UNKNOWN:
5859 fprintf(fp, "unknown_t");
5860 break;
5861 default:
5862 fprintf(fp, "????: %x", base_type);
5863 break;
5864 }
5865 if (type->field_ident && type->field_ident->name) {
5866 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005867 }
5868}
5869
5870static size_t align_of(struct compile_state *state, struct type *type)
5871{
5872 size_t align;
5873 align = 0;
5874 switch(type->type & TYPE_MASK) {
5875 case TYPE_VOID:
5876 align = 1;
5877 break;
Eric Biederman90089602004-05-28 14:11:54 +00005878 case TYPE_BITFIELD:
5879 align = 1;
5880 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005881 case TYPE_CHAR:
5882 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005883 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005884 break;
5885 case TYPE_SHORT:
5886 case TYPE_USHORT:
5887 align = ALIGNOF_SHORT;
5888 break;
5889 case TYPE_INT:
5890 case TYPE_UINT:
5891 case TYPE_ENUM:
5892 align = ALIGNOF_INT;
5893 break;
5894 case TYPE_LONG:
5895 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005896 align = ALIGNOF_LONG;
5897 break;
Eric Biederman90089602004-05-28 14:11:54 +00005898 case TYPE_POINTER:
5899 align = ALIGNOF_POINTER;
5900 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005901 case TYPE_PRODUCT:
5902 case TYPE_OVERLAP:
5903 {
5904 size_t left_align, right_align;
5905 left_align = align_of(state, type->left);
5906 right_align = align_of(state, type->right);
5907 align = (left_align >= right_align) ? left_align : right_align;
5908 break;
5909 }
5910 case TYPE_ARRAY:
5911 align = align_of(state, type->left);
5912 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005913 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005914 case TYPE_TUPLE:
5915 case TYPE_UNION:
5916 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005917 align = align_of(state, type->left);
5918 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005919 default:
5920 error(state, 0, "alignof not yet defined for type\n");
5921 break;
5922 }
5923 return align;
5924}
5925
Eric Biederman90089602004-05-28 14:11:54 +00005926static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005927{
Eric Biederman90089602004-05-28 14:11:54 +00005928 size_t align;
5929 align = 0;
5930 switch(type->type & TYPE_MASK) {
5931 case TYPE_VOID:
5932 align = 1;
5933 break;
5934 case TYPE_BITFIELD:
5935 align = 1;
5936 break;
5937 case TYPE_CHAR:
5938 case TYPE_UCHAR:
5939 align = REG_ALIGNOF_CHAR;
5940 break;
5941 case TYPE_SHORT:
5942 case TYPE_USHORT:
5943 align = REG_ALIGNOF_SHORT;
5944 break;
5945 case TYPE_INT:
5946 case TYPE_UINT:
5947 case TYPE_ENUM:
5948 align = REG_ALIGNOF_INT;
5949 break;
5950 case TYPE_LONG:
5951 case TYPE_ULONG:
5952 align = REG_ALIGNOF_LONG;
5953 break;
5954 case TYPE_POINTER:
5955 align = REG_ALIGNOF_POINTER;
5956 break;
5957 case TYPE_PRODUCT:
5958 case TYPE_OVERLAP:
5959 {
5960 size_t left_align, right_align;
5961 left_align = reg_align_of(state, type->left);
5962 right_align = reg_align_of(state, type->right);
5963 align = (left_align >= right_align) ? left_align : right_align;
5964 break;
5965 }
5966 case TYPE_ARRAY:
5967 align = reg_align_of(state, type->left);
5968 break;
5969 case TYPE_STRUCT:
5970 case TYPE_UNION:
5971 case TYPE_TUPLE:
5972 case TYPE_JOIN:
5973 align = reg_align_of(state, type->left);
5974 break;
5975 default:
5976 error(state, 0, "alignof not yet defined for type\n");
5977 break;
5978 }
5979 return align;
5980}
5981
5982static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5983{
5984 return bits_to_bytes(align_of(state, type));
5985}
5986static size_t size_of(struct compile_state *state, struct type *type);
5987static size_t reg_size_of(struct compile_state *state, struct type *type);
5988
Stefan Reinauer14e22772010-04-27 06:56:47 +00005989static size_t needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00005990 struct type *type, size_t offset)
5991{
5992 size_t padding, align;
5993 align = align_of(state, type);
5994 /* Align to the next machine word if the bitfield does completely
5995 * fit into the current word.
5996 */
5997 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
5998 size_t size;
5999 size = size_of(state, type);
6000 if ((offset + type->elements)/size != offset/size) {
6001 align = size;
6002 }
6003 }
Eric Biederman03b59862003-06-24 14:27:37 +00006004 padding = 0;
6005 if (offset % align) {
6006 padding = align - (offset % align);
6007 }
6008 return padding;
6009}
Eric Biederman90089602004-05-28 14:11:54 +00006010
Stefan Reinauer14e22772010-04-27 06:56:47 +00006011static size_t reg_needed_padding(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006012 struct type *type, size_t offset)
6013{
6014 size_t padding, align;
6015 align = reg_align_of(state, type);
6016 /* Align to the next register word if the bitfield does completely
6017 * fit into the current register.
6018 */
6019 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00006020 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
Eric Biederman90089602004-05-28 14:11:54 +00006021 {
6022 align = REG_SIZEOF_REG;
6023 }
6024 padding = 0;
6025 if (offset % align) {
6026 padding = align - (offset % align);
6027 }
6028 return padding;
6029}
6030
Eric Biedermanb138ac82003-04-22 18:44:01 +00006031static size_t size_of(struct compile_state *state, struct type *type)
6032{
6033 size_t size;
6034 size = 0;
6035 switch(type->type & TYPE_MASK) {
6036 case TYPE_VOID:
6037 size = 0;
6038 break;
Eric Biederman90089602004-05-28 14:11:54 +00006039 case TYPE_BITFIELD:
6040 size = type->elements;
6041 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006042 case TYPE_CHAR:
6043 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006044 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006045 break;
6046 case TYPE_SHORT:
6047 case TYPE_USHORT:
6048 size = SIZEOF_SHORT;
6049 break;
6050 case TYPE_INT:
6051 case TYPE_UINT:
6052 case TYPE_ENUM:
6053 size = SIZEOF_INT;
6054 break;
6055 case TYPE_LONG:
6056 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006057 size = SIZEOF_LONG;
6058 break;
Eric Biederman90089602004-05-28 14:11:54 +00006059 case TYPE_POINTER:
6060 size = SIZEOF_POINTER;
6061 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006062 case TYPE_PRODUCT:
6063 {
Eric Biederman90089602004-05-28 14:11:54 +00006064 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006065 size = 0;
6066 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006067 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006068 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006069 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006070 }
Eric Biederman90089602004-05-28 14:11:54 +00006071 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006072 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006073 break;
6074 }
6075 case TYPE_OVERLAP:
6076 {
6077 size_t size_left, size_right;
6078 size_left = size_of(state, type->left);
6079 size_right = size_of(state, type->right);
6080 size = (size_left >= size_right)? size_left : size_right;
6081 break;
6082 }
6083 case TYPE_ARRAY:
6084 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6085 internal_error(state, 0, "Invalid array type");
6086 } else {
6087 size = size_of(state, type->left) * type->elements;
6088 }
6089 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006090 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006091 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006092 {
Eric Biederman90089602004-05-28 14:11:54 +00006093 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006094 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006095 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006096 pad = needed_padding(state, type, size);
6097 size = size + pad;
6098 break;
6099 }
6100 case TYPE_UNION:
6101 case TYPE_JOIN:
6102 {
6103 size_t pad;
6104 size = size_of(state, type->left);
6105 /* Pad unions so their size is a multiple of their alignment */
6106 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006107 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006108 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006109 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006110 default:
Eric Biederman90089602004-05-28 14:11:54 +00006111 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006112 break;
6113 }
6114 return size;
6115}
6116
Eric Biederman90089602004-05-28 14:11:54 +00006117static size_t reg_size_of(struct compile_state *state, struct type *type)
6118{
6119 size_t size;
6120 size = 0;
6121 switch(type->type & TYPE_MASK) {
6122 case TYPE_VOID:
6123 size = 0;
6124 break;
6125 case TYPE_BITFIELD:
6126 size = type->elements;
6127 break;
6128 case TYPE_CHAR:
6129 case TYPE_UCHAR:
6130 size = REG_SIZEOF_CHAR;
6131 break;
6132 case TYPE_SHORT:
6133 case TYPE_USHORT:
6134 size = REG_SIZEOF_SHORT;
6135 break;
6136 case TYPE_INT:
6137 case TYPE_UINT:
6138 case TYPE_ENUM:
6139 size = REG_SIZEOF_INT;
6140 break;
6141 case TYPE_LONG:
6142 case TYPE_ULONG:
6143 size = REG_SIZEOF_LONG;
6144 break;
6145 case TYPE_POINTER:
6146 size = REG_SIZEOF_POINTER;
6147 break;
6148 case TYPE_PRODUCT:
6149 {
6150 size_t pad;
6151 size = 0;
6152 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6153 pad = reg_needed_padding(state, type->left, size);
6154 size = size + pad + reg_size_of(state, type->left);
6155 type = type->right;
6156 }
6157 pad = reg_needed_padding(state, type, size);
6158 size = size + pad + reg_size_of(state, type);
6159 break;
6160 }
6161 case TYPE_OVERLAP:
6162 {
6163 size_t size_left, size_right;
6164 size_left = reg_size_of(state, type->left);
6165 size_right = reg_size_of(state, type->right);
6166 size = (size_left >= size_right)? size_left : size_right;
6167 break;
6168 }
6169 case TYPE_ARRAY:
6170 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6171 internal_error(state, 0, "Invalid array type");
6172 } else {
6173 size = reg_size_of(state, type->left) * type->elements;
6174 }
6175 break;
6176 case TYPE_STRUCT:
6177 case TYPE_TUPLE:
6178 {
6179 size_t pad;
6180 size = reg_size_of(state, type->left);
6181 /* Pad structures so their size is a multiples of their alignment */
6182 pad = reg_needed_padding(state, type, size);
6183 size = size + pad;
6184 break;
6185 }
6186 case TYPE_UNION:
6187 case TYPE_JOIN:
6188 {
6189 size_t pad;
6190 size = reg_size_of(state, type->left);
6191 /* Pad unions so their size is a multiple of their alignment */
6192 pad = reg_needed_padding(state, type, size);
6193 size = size + pad;
6194 break;
6195 }
6196 default:
6197 internal_error(state, 0, "sizeof not yet defined for type");
6198 break;
6199 }
6200 return size;
6201}
6202
6203static size_t registers_of(struct compile_state *state, struct type *type)
6204{
6205 size_t registers;
6206 registers = reg_size_of(state, type);
6207 registers += REG_SIZEOF_REG - 1;
6208 registers /= REG_SIZEOF_REG;
6209 return registers;
6210}
6211
6212static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6213{
6214 return bits_to_bytes(size_of(state, type));
6215}
6216
Stefan Reinauer14e22772010-04-27 06:56:47 +00006217static size_t field_offset(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006218 struct type *type, struct hash_entry *field)
6219{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006220 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006221 size_t size;
6222
Eric Biederman0babc1c2003-05-09 02:39:00 +00006223 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006224 member = 0;
6225 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6226 member = type->left;
6227 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6228 size += needed_padding(state, member->left, size);
6229 if (member->left->field_ident == field) {
6230 member = member->left;
6231 break;
6232 }
6233 size += size_of(state, member->left);
6234 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006235 }
Eric Biederman90089602004-05-28 14:11:54 +00006236 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006237 }
Eric Biederman90089602004-05-28 14:11:54 +00006238 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6239 member = type->left;
6240 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6241 if (member->left->field_ident == field) {
6242 member = member->left;
6243 break;
6244 }
6245 member = member->right;
6246 }
6247 }
6248 else {
6249 internal_error(state, 0, "field_offset only works on structures and unions");
6250 }
6251
6252 if (!member || (member->field_ident != field)) {
6253 error(state, 0, "member %s not present", field->name);
6254 }
6255 return size;
6256}
6257
Stefan Reinauer14e22772010-04-27 06:56:47 +00006258static size_t field_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006259 struct type *type, struct hash_entry *field)
6260{
6261 struct type *member;
6262 size_t size;
6263
6264 size = 0;
6265 member = 0;
6266 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6267 member = type->left;
6268 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6269 size += reg_needed_padding(state, member->left, size);
6270 if (member->left->field_ident == field) {
6271 member = member->left;
6272 break;
6273 }
6274 size += reg_size_of(state, member->left);
6275 member = member->right;
6276 }
6277 }
6278 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6279 member = type->left;
6280 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6281 if (member->left->field_ident == field) {
6282 member = member->left;
6283 break;
6284 }
6285 member = member->right;
6286 }
6287 }
6288 else {
6289 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6290 }
6291
6292 size += reg_needed_padding(state, member, size);
6293 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006294 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006295 }
6296 return size;
6297}
6298
Stefan Reinauer14e22772010-04-27 06:56:47 +00006299static struct type *field_type(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00006300 struct type *type, struct hash_entry *field)
6301{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006302 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006303
6304 member = 0;
6305 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6306 member = type->left;
6307 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6308 if (member->left->field_ident == field) {
6309 member = member->left;
6310 break;
6311 }
6312 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006313 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006314 }
Eric Biederman90089602004-05-28 14:11:54 +00006315 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6316 member = type->left;
6317 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6318 if (member->left->field_ident == field) {
6319 member = member->left;
6320 break;
6321 }
6322 member = member->right;
6323 }
6324 }
6325 else {
6326 internal_error(state, 0, "field_type only works on structures and unions");
6327 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006328
Eric Biederman90089602004-05-28 14:11:54 +00006329 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006330 error(state, 0, "member %s not present", field->name);
6331 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006332 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006333}
6334
Stefan Reinauer14e22772010-04-27 06:56:47 +00006335static size_t index_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006336 struct type *type, ulong_t index)
6337{
6338 struct type *member;
6339 size_t size;
6340 size = 0;
6341 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6342 size = size_of(state, type->left) * index;
6343 }
6344 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6345 ulong_t i;
6346 member = type->left;
6347 i = 0;
6348 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6349 size += needed_padding(state, member->left, size);
6350 if (i == index) {
6351 member = member->left;
6352 break;
6353 }
6354 size += size_of(state, member->left);
6355 i++;
6356 member = member->right;
6357 }
6358 size += needed_padding(state, member, size);
6359 if (i != index) {
6360 internal_error(state, 0, "Missing member index: %u", index);
6361 }
6362 }
6363 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6364 ulong_t i;
6365 size = 0;
6366 member = type->left;
6367 i = 0;
6368 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6369 if (i == index) {
6370 member = member->left;
6371 break;
6372 }
6373 i++;
6374 member = member->right;
6375 }
6376 if (i != index) {
6377 internal_error(state, 0, "Missing member index: %u", index);
6378 }
6379 }
6380 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006381 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006382 "request for index %u in something not an array, tuple or join",
6383 index);
6384 }
6385 return size;
6386}
6387
Stefan Reinauer14e22772010-04-27 06:56:47 +00006388static size_t index_reg_offset(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00006389 struct type *type, ulong_t index)
6390{
6391 struct type *member;
6392 size_t size;
6393 size = 0;
6394 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6395 size = reg_size_of(state, type->left) * index;
6396 }
6397 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6398 ulong_t i;
6399 member = type->left;
6400 i = 0;
6401 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6402 size += reg_needed_padding(state, member->left, size);
6403 if (i == index) {
6404 member = member->left;
6405 break;
6406 }
6407 size += reg_size_of(state, member->left);
6408 i++;
6409 member = member->right;
6410 }
6411 size += reg_needed_padding(state, member, size);
6412 if (i != index) {
6413 internal_error(state, 0, "Missing member index: %u", index);
6414 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006415
Eric Biederman90089602004-05-28 14:11:54 +00006416 }
6417 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6418 ulong_t i;
6419 size = 0;
6420 member = type->left;
6421 i = 0;
6422 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6423 if (i == index) {
6424 member = member->left;
6425 break;
6426 }
6427 i++;
6428 member = member->right;
6429 }
6430 if (i != index) {
6431 internal_error(state, 0, "Missing member index: %u", index);
6432 }
6433 }
6434 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006435 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006436 "request for index %u in something not an array, tuple or join",
6437 index);
6438 }
6439 return size;
6440}
6441
6442static struct type *index_type(struct compile_state *state,
6443 struct type *type, ulong_t index)
6444{
6445 struct type *member;
6446 if (index >= type->elements) {
6447 internal_error(state, 0, "Invalid element %u requested", index);
6448 }
6449 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6450 member = type->left;
6451 }
6452 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6453 ulong_t i;
6454 member = type->left;
6455 i = 0;
6456 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6457 if (i == index) {
6458 member = member->left;
6459 break;
6460 }
6461 i++;
6462 member = member->right;
6463 }
6464 if (i != index) {
6465 internal_error(state, 0, "Missing member index: %u", index);
6466 }
6467 }
6468 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6469 ulong_t i;
6470 member = type->left;
6471 i = 0;
6472 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6473 if (i == index) {
6474 member = member->left;
6475 break;
6476 }
6477 i++;
6478 member = member->right;
6479 }
6480 if (i != index) {
6481 internal_error(state, 0, "Missing member index: %u", index);
6482 }
6483 }
6484 else {
6485 member = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +00006486 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +00006487 "request for index %u in something not an array, tuple or join",
6488 index);
6489 }
6490 return member;
6491}
6492
6493static struct type *unpack_type(struct compile_state *state, struct type *type)
6494{
6495 /* If I have a single register compound type not a bit-field
6496 * find the real type.
6497 */
6498 struct type *start_type;
6499 size_t size;
6500 /* Get out early if I need multiple registers for this type */
6501 size = reg_size_of(state, type);
6502 if (size > REG_SIZEOF_REG) {
6503 return type;
6504 }
6505 /* Get out early if I don't need any registers for this type */
6506 if (size == 0) {
6507 return &void_type;
6508 }
6509 /* Loop until I have no more layers I can remove */
6510 do {
6511 start_type = type;
6512 switch(type->type & TYPE_MASK) {
6513 case TYPE_ARRAY:
6514 /* If I have a single element the unpacked type
6515 * is that element.
6516 */
6517 if (type->elements == 1) {
6518 type = type->left;
6519 }
6520 break;
6521 case TYPE_STRUCT:
6522 case TYPE_TUPLE:
6523 /* If I have a single element the unpacked type
6524 * is that element.
6525 */
6526 if (type->elements == 1) {
6527 type = type->left;
6528 }
6529 /* If I have multiple elements the unpacked
6530 * type is the non-void element.
6531 */
6532 else {
6533 struct type *next, *member;
6534 struct type *sub_type;
6535 sub_type = 0;
6536 next = type->left;
6537 while(next) {
6538 member = next;
6539 next = 0;
6540 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6541 next = member->right;
6542 member = member->left;
6543 }
6544 if (reg_size_of(state, member) > 0) {
6545 if (sub_type) {
6546 internal_error(state, 0, "true compound type in a register");
6547 }
6548 sub_type = member;
6549 }
6550 }
6551 if (sub_type) {
6552 type = sub_type;
6553 }
6554 }
6555 break;
6556
6557 case TYPE_UNION:
6558 case TYPE_JOIN:
6559 /* If I have a single element the unpacked type
6560 * is that element.
6561 */
6562 if (type->elements == 1) {
6563 type = type->left;
6564 }
6565 /* I can't in general unpack union types */
6566 break;
6567 default:
6568 /* If I'm not a compound type I can't unpack it */
6569 break;
6570 }
6571 } while(start_type != type);
6572 switch(type->type & TYPE_MASK) {
6573 case TYPE_STRUCT:
6574 case TYPE_ARRAY:
6575 case TYPE_TUPLE:
6576 internal_error(state, 0, "irredicible type?");
6577 break;
6578 }
6579 return type;
6580}
6581
6582static int equiv_types(struct type *left, struct type *right);
6583static int is_compound_type(struct type *type);
6584
6585static struct type *reg_type(
6586 struct compile_state *state, struct type *type, int reg_offset)
6587{
6588 struct type *member;
6589 size_t size;
6590#if 1
6591 struct type *invalid;
6592 invalid = invalid_type(state, type);
6593 if (invalid) {
6594 fprintf(state->errout, "type: ");
6595 name_of(state->errout, type);
6596 fprintf(state->errout, "\n");
6597 fprintf(state->errout, "invalid: ");
6598 name_of(state->errout, invalid);
6599 fprintf(state->errout, "\n");
6600 internal_error(state, 0, "bad input type?");
6601 }
6602#endif
6603
6604 size = reg_size_of(state, type);
6605 if (reg_offset > size) {
6606 member = 0;
6607 fprintf(state->errout, "type: ");
6608 name_of(state->errout, type);
6609 fprintf(state->errout, "\n");
6610 internal_error(state, 0, "offset outside of type");
6611 }
6612 else {
6613 switch(type->type & TYPE_MASK) {
6614 /* Don't do anything with the basic types */
6615 case TYPE_VOID:
6616 case TYPE_CHAR: case TYPE_UCHAR:
6617 case TYPE_SHORT: case TYPE_USHORT:
6618 case TYPE_INT: case TYPE_UINT:
6619 case TYPE_LONG: case TYPE_ULONG:
6620 case TYPE_LLONG: case TYPE_ULLONG:
6621 case TYPE_FLOAT: case TYPE_DOUBLE:
6622 case TYPE_LDOUBLE:
6623 case TYPE_POINTER:
6624 case TYPE_ENUM:
6625 case TYPE_BITFIELD:
6626 member = type;
6627 break;
6628 case TYPE_ARRAY:
6629 member = type->left;
6630 size = reg_size_of(state, member);
6631 if (size > REG_SIZEOF_REG) {
6632 member = reg_type(state, member, reg_offset % size);
6633 }
6634 break;
6635 case TYPE_STRUCT:
6636 case TYPE_TUPLE:
6637 {
6638 size_t offset;
6639 offset = 0;
6640 member = type->left;
6641 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6642 size = reg_size_of(state, member->left);
6643 offset += reg_needed_padding(state, member->left, offset);
6644 if ((offset + size) > reg_offset) {
6645 member = member->left;
6646 break;
6647 }
6648 offset += size;
6649 member = member->right;
6650 }
6651 offset += reg_needed_padding(state, member, offset);
6652 member = reg_type(state, member, reg_offset - offset);
6653 break;
6654 }
6655 case TYPE_UNION:
6656 case TYPE_JOIN:
6657 {
6658 struct type *join, **jnext, *mnext;
6659 join = new_type(TYPE_JOIN, 0, 0);
6660 jnext = &join->left;
6661 mnext = type->left;
6662 while(mnext) {
6663 size_t size;
6664 member = mnext;
6665 mnext = 0;
6666 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6667 mnext = member->right;
6668 member = member->left;
6669 }
6670 size = reg_size_of(state, member);
6671 if (size > reg_offset) {
6672 struct type *part, *hunt;
6673 part = reg_type(state, member, reg_offset);
6674 /* See if this type is already in the union */
6675 hunt = join->left;
6676 while(hunt) {
6677 struct type *test = hunt;
6678 hunt = 0;
6679 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6680 hunt = test->right;
6681 test = test->left;
6682 }
6683 if (equiv_types(part, test)) {
6684 goto next;
6685 }
6686 }
6687 /* Nope add it */
6688 if (!*jnext) {
6689 *jnext = part;
6690 } else {
6691 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6692 jnext = &(*jnext)->right;
6693 }
6694 join->elements++;
6695 }
6696 next:
6697 ;
6698 }
6699 if (join->elements == 0) {
6700 internal_error(state, 0, "No elements?");
6701 }
6702 member = join;
6703 break;
6704 }
6705 default:
6706 member = 0;
6707 fprintf(state->errout, "type: ");
6708 name_of(state->errout, type);
6709 fprintf(state->errout, "\n");
6710 internal_error(state, 0, "reg_type not yet defined for type");
Stefan Reinauer14e22772010-04-27 06:56:47 +00006711
Eric Biederman90089602004-05-28 14:11:54 +00006712 }
6713 }
6714 /* If I have a single register compound type not a bit-field
6715 * find the real type.
6716 */
6717 member = unpack_type(state, member);
6718 ;
6719 size = reg_size_of(state, member);
6720 if (size > REG_SIZEOF_REG) {
6721 internal_error(state, 0, "Cannot find type of single register");
6722 }
6723#if 1
6724 invalid = invalid_type(state, member);
6725 if (invalid) {
6726 fprintf(state->errout, "type: ");
6727 name_of(state->errout, member);
6728 fprintf(state->errout, "\n");
6729 fprintf(state->errout, "invalid: ");
6730 name_of(state->errout, invalid);
6731 fprintf(state->errout, "\n");
6732 internal_error(state, 0, "returning bad type?");
6733 }
6734#endif
6735 return member;
6736}
6737
Eric Biederman03b59862003-06-24 14:27:37 +00006738static struct type *next_field(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +00006739 struct type *type, struct type *prev_member)
Eric Biederman03b59862003-06-24 14:27:37 +00006740{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006741 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006742 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6743 internal_error(state, 0, "next_field only works on structures");
6744 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006745 member = type->left;
6746 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006747 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006748 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006749 break;
6750 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006751 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006752 prev_member = 0;
6753 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006754 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006755 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006756 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006757 prev_member = 0;
6758 }
6759 if (prev_member) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00006760 internal_error(state, 0, "prev_member %s not present",
Eric Biederman03b59862003-06-24 14:27:37 +00006761 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006762 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006763 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006764}
6765
Stefan Reinauer14e22772010-04-27 06:56:47 +00006766typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
Eric Biederman90089602004-05-28 14:11:54 +00006767 size_t ret_offset, size_t mem_offset, void *arg);
6768
6769static void walk_type_fields(struct compile_state *state,
6770 struct type *type, size_t reg_offset, size_t mem_offset,
6771 walk_type_fields_cb_t cb, void *arg);
6772
6773static void walk_struct_fields(struct compile_state *state,
6774 struct type *type, size_t reg_offset, size_t mem_offset,
6775 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006776{
Eric Biederman90089602004-05-28 14:11:54 +00006777 struct type *tptr;
6778 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006779 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006780 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006781 }
Eric Biederman90089602004-05-28 14:11:54 +00006782 tptr = type->left;
6783 for(i = 0; i < type->elements; i++) {
6784 struct type *mtype;
6785 mtype = tptr;
6786 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6787 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006788 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006789 walk_type_fields(state, mtype,
6790 reg_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006791 field_reg_offset(state, type, mtype->field_ident),
Stefan Reinauer14e22772010-04-27 06:56:47 +00006792 mem_offset +
Eric Biederman90089602004-05-28 14:11:54 +00006793 field_offset(state, type, mtype->field_ident),
6794 cb, arg);
6795 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006796 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00006797
Eric Biederman90089602004-05-28 14:11:54 +00006798}
6799
6800static void walk_type_fields(struct compile_state *state,
6801 struct type *type, size_t reg_offset, size_t mem_offset,
6802 walk_type_fields_cb_t cb, void *arg)
6803{
6804 switch(type->type & TYPE_MASK) {
6805 case TYPE_STRUCT:
6806 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6807 break;
6808 case TYPE_CHAR:
6809 case TYPE_UCHAR:
6810 case TYPE_SHORT:
6811 case TYPE_USHORT:
6812 case TYPE_INT:
6813 case TYPE_UINT:
6814 case TYPE_LONG:
6815 case TYPE_ULONG:
6816 cb(state, type, reg_offset, mem_offset, arg);
6817 break;
6818 case TYPE_VOID:
6819 break;
6820 default:
6821 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006822 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006823}
6824
Eric Biedermanb138ac82003-04-22 18:44:01 +00006825static void arrays_complete(struct compile_state *state, struct type *type)
6826{
6827 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6828 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6829 error(state, 0, "array size not specified");
6830 }
6831 arrays_complete(state, type->left);
6832 }
6833}
6834
Eric Biederman90089602004-05-28 14:11:54 +00006835static unsigned int get_basic_type(struct type *type)
6836{
6837 unsigned int basic;
6838 basic = type->type & TYPE_MASK;
6839 /* Convert enums to ints */
6840 if (basic == TYPE_ENUM) {
6841 basic = TYPE_INT;
6842 }
6843 /* Convert bitfields to standard types */
6844 else if (basic == TYPE_BITFIELD) {
6845 if (type->elements <= SIZEOF_CHAR) {
6846 basic = TYPE_CHAR;
6847 }
6848 else if (type->elements <= SIZEOF_SHORT) {
6849 basic = TYPE_SHORT;
6850 }
6851 else if (type->elements <= SIZEOF_INT) {
6852 basic = TYPE_INT;
6853 }
6854 else if (type->elements <= SIZEOF_LONG) {
6855 basic = TYPE_LONG;
6856 }
6857 if (!TYPE_SIGNED(type->left->type)) {
6858 basic += 1;
6859 }
6860 }
6861 return basic;
6862}
6863
Eric Biedermanb138ac82003-04-22 18:44:01 +00006864static unsigned int do_integral_promotion(unsigned int type)
6865{
Eric Biederman83b991a2003-10-11 06:20:25 +00006866 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006867 type = TYPE_INT;
6868 }
6869 return type;
6870}
6871
6872static unsigned int do_arithmetic_conversion(
6873 unsigned int left, unsigned int right)
6874{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006875 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6876 return TYPE_LDOUBLE;
6877 }
6878 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6879 return TYPE_DOUBLE;
6880 }
6881 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6882 return TYPE_FLOAT;
6883 }
6884 left = do_integral_promotion(left);
6885 right = do_integral_promotion(right);
6886 /* If both operands have the same size done */
6887 if (left == right) {
6888 return left;
6889 }
6890 /* If both operands have the same signedness pick the larger */
6891 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6892 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6893 }
6894 /* If the signed type can hold everything use it */
6895 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6896 return left;
6897 }
6898 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6899 return right;
6900 }
6901 /* Convert to the unsigned type with the same rank as the signed type */
6902 else if (TYPE_SIGNED(left)) {
6903 return TYPE_MKUNSIGNED(left);
6904 }
6905 else {
6906 return TYPE_MKUNSIGNED(right);
6907 }
6908}
6909
6910/* see if two types are the same except for qualifiers */
6911static int equiv_types(struct type *left, struct type *right)
6912{
6913 unsigned int type;
6914 /* Error if the basic types do not match */
6915 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6916 return 0;
6917 }
6918 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006919 /* If the basic types match and it is a void type we are done */
6920 if (type == TYPE_VOID) {
6921 return 1;
6922 }
Eric Biederman90089602004-05-28 14:11:54 +00006923 /* For bitfields we need to compare the sizes */
6924 else if (type == TYPE_BITFIELD) {
6925 return (left->elements == right->elements) &&
6926 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6927 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006928 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006929 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006930 return 1;
6931 }
6932 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006933 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006934 return equiv_types(left->left, right->left);
6935 }
6936 else if (type == TYPE_ARRAY) {
6937 return (left->elements == right->elements) &&
6938 equiv_types(left->left, right->left);
6939 }
Eric Biederman90089602004-05-28 14:11:54 +00006940 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006941 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006942 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006943 }
Eric Biederman90089602004-05-28 14:11:54 +00006944 /* test for union equality */
6945 else if (type == TYPE_UNION) {
6946 return left->type_ident == right->type_ident;
6947 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006948 /* Test for equivalent functions */
6949 else if (type == TYPE_FUNCTION) {
6950 return equiv_types(left->left, right->left) &&
6951 equiv_types(left->right, right->right);
6952 }
6953 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006954 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006955 else if (type == TYPE_PRODUCT) {
6956 return equiv_types(left->left, right->left) &&
6957 equiv_types(left->right, right->right);
6958 }
Eric Biederman90089602004-05-28 14:11:54 +00006959 /* We should see TYPE_OVERLAP when comparing joins */
6960 else if (type == TYPE_OVERLAP) {
6961 return equiv_types(left->left, right->left) &&
6962 equiv_types(left->right, right->right);
6963 }
6964 /* Test for equivalence of tuples */
6965 else if (type == TYPE_TUPLE) {
6966 return (left->elements == right->elements) &&
6967 equiv_types(left->left, right->left);
6968 }
6969 /* Test for equivalence of joins */
6970 else if (type == TYPE_JOIN) {
6971 return (left->elements == right->elements) &&
6972 equiv_types(left->left, right->left);
6973 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006974 else {
6975 return 0;
6976 }
6977}
6978
6979static int equiv_ptrs(struct type *left, struct type *right)
6980{
6981 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6982 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6983 return 0;
6984 }
6985 return equiv_types(left->left, right->left);
6986}
6987
6988static struct type *compatible_types(struct type *left, struct type *right)
6989{
6990 struct type *result;
6991 unsigned int type, qual_type;
6992 /* Error if the basic types do not match */
6993 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6994 return 0;
6995 }
6996 type = left->type & TYPE_MASK;
6997 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
6998 result = 0;
6999 /* if the basic types match and it is an arithmetic type we are done */
7000 if (TYPE_ARITHMETIC(type)) {
7001 result = new_type(qual_type, 0, 0);
7002 }
7003 /* If it is a pointer type recurse and keep testing */
7004 else if (type == TYPE_POINTER) {
7005 result = compatible_types(left->left, right->left);
7006 if (result) {
7007 result = new_type(qual_type, result, 0);
7008 }
7009 }
Eric Biederman90089602004-05-28 14:11:54 +00007010 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007011 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007012 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007013 result = left;
7014 }
7015 }
Eric Biederman90089602004-05-28 14:11:54 +00007016 /* test for union equality */
7017 else if (type == TYPE_UNION) {
7018 if (left->type_ident == right->type_ident) {
7019 result = left;
7020 }
7021 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007022 /* Test for equivalent functions */
7023 else if (type == TYPE_FUNCTION) {
7024 struct type *lf, *rf;
7025 lf = compatible_types(left->left, right->left);
7026 rf = compatible_types(left->right, right->right);
7027 if (lf && rf) {
7028 result = new_type(qual_type, lf, rf);
7029 }
7030 }
7031 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7032 else if (type == TYPE_PRODUCT) {
7033 struct type *lf, *rf;
7034 lf = compatible_types(left->left, right->left);
7035 rf = compatible_types(left->right, right->right);
7036 if (lf && rf) {
7037 result = new_type(qual_type, lf, rf);
7038 }
7039 }
7040 else {
7041 /* Nothing else is compatible */
7042 }
7043 return result;
7044}
7045
Eric Biederman90089602004-05-28 14:11:54 +00007046/* See if left is a equivalent to right or right is a union member of left */
7047static int is_subset_type(struct type *left, struct type *right)
7048{
7049 if (equiv_types(left, right)) {
7050 return 1;
7051 }
7052 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7053 struct type *member, *mnext;
7054 mnext = left->left;
7055 while(mnext) {
7056 member = mnext;
7057 mnext = 0;
7058 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7059 mnext = member->right;
7060 member = member->left;
7061 }
7062 if (is_subset_type( member, right)) {
7063 return 1;
7064 }
7065 }
7066 }
7067 return 0;
7068}
7069
Eric Biedermanb138ac82003-04-22 18:44:01 +00007070static struct type *compatible_ptrs(struct type *left, struct type *right)
7071{
7072 struct type *result;
7073 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7074 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7075 return 0;
7076 }
7077 result = compatible_types(left->left, right->left);
7078 if (result) {
7079 unsigned int qual_type;
7080 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7081 result = new_type(qual_type, result, 0);
7082 }
7083 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007084
Eric Biedermanb138ac82003-04-22 18:44:01 +00007085}
7086static struct triple *integral_promotion(
7087 struct compile_state *state, struct triple *def)
7088{
7089 struct type *type;
7090 type = def->type;
7091 /* As all operations are carried out in registers
7092 * the values are converted on load I just convert
7093 * logical type of the operand.
7094 */
7095 if (TYPE_INTEGER(type->type)) {
7096 unsigned int int_type;
7097 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007098 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007099 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007100 if (def->op != OP_LOAD) {
7101 def->type = new_type(int_type, 0, 0);
7102 }
7103 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +00007104 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007105 new_type(int_type, 0, 0), def, 0);
7106 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007107 }
7108 }
7109 return def;
7110}
7111
7112
7113static void arithmetic(struct compile_state *state, struct triple *def)
7114{
7115 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007116 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007117 }
7118}
7119
7120static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7121{
7122 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7123 error(state, def, "pointer or arithmetic type expected");
7124 }
7125}
7126
7127static int is_integral(struct triple *ins)
7128{
7129 return TYPE_INTEGER(ins->type->type);
7130}
7131
7132static void integral(struct compile_state *state, struct triple *def)
7133{
7134 if (!is_integral(def)) {
7135 error(state, 0, "integral type expected");
7136 }
7137}
7138
7139
7140static void bool(struct compile_state *state, struct triple *def)
7141{
7142 if (!TYPE_ARITHMETIC(def->type->type) &&
7143 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7144 error(state, 0, "arithmetic or pointer type expected");
7145 }
7146}
7147
7148static int is_signed(struct type *type)
7149{
Eric Biederman90089602004-05-28 14:11:54 +00007150 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7151 type = type->left;
7152 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007153 return !!TYPE_SIGNED(type->type);
7154}
Eric Biederman90089602004-05-28 14:11:54 +00007155static int is_compound_type(struct type *type)
7156{
7157 int is_compound;
7158 switch((type->type & TYPE_MASK)) {
7159 case TYPE_ARRAY:
7160 case TYPE_STRUCT:
7161 case TYPE_TUPLE:
7162 case TYPE_UNION:
Stefan Reinauer14e22772010-04-27 06:56:47 +00007163 case TYPE_JOIN:
Eric Biederman90089602004-05-28 14:11:54 +00007164 is_compound = 1;
7165 break;
7166 default:
7167 is_compound = 0;
7168 break;
7169 }
7170 return is_compound;
7171}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007172
Eric Biederman0babc1c2003-05-09 02:39:00 +00007173/* Is this value located in a register otherwise it must be in memory */
7174static int is_in_reg(struct compile_state *state, struct triple *def)
7175{
7176 int in_reg;
7177 if (def->op == OP_ADECL) {
7178 in_reg = 1;
7179 }
7180 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7181 in_reg = 0;
7182 }
Eric Biederman90089602004-05-28 14:11:54 +00007183 else if (triple_is_part(state, def)) {
7184 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007185 }
7186 else {
Eric Biederman90089602004-05-28 14:11:54 +00007187 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007188 in_reg = -1;
7189 }
7190 return in_reg;
7191}
7192
Eric Biederman90089602004-05-28 14:11:54 +00007193/* Is this an auto or static variable location? Something that can
7194 * be assigned to. Otherwise it must must be a pure value, a temporary.
7195 */
7196static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007197{
7198 int ret;
7199 ret = 0;
7200 if (!def) {
7201 return 0;
7202 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007203 if ((def->op == OP_ADECL) ||
7204 (def->op == OP_SDECL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +00007205 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007206 (def->op == OP_BLOBCONST) ||
7207 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007208 ret = 1;
7209 }
Eric Biederman90089602004-05-28 14:11:54 +00007210 else if (triple_is_part(state, def)) {
7211 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007212 }
7213 return ret;
7214}
7215
Eric Biederman00443072003-06-24 12:34:45 +00007216static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007217{
7218 if (!def) {
7219 internal_error(state, def, "nothing where lvalue expected?");
7220 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007221 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007222 error(state, def, "lvalue expected");
7223 }
7224}
Eric Biederman00443072003-06-24 12:34:45 +00007225static void lvalue(struct compile_state *state, struct triple *def)
7226{
7227 clvalue(state, def);
7228 if (def->type->type & QUAL_CONST) {
7229 error(state, def, "modifable lvalue expected");
7230 }
7231}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007232
7233static int is_pointer(struct triple *def)
7234{
7235 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7236}
7237
7238static void pointer(struct compile_state *state, struct triple *def)
7239{
7240 if (!is_pointer(def)) {
7241 error(state, def, "pointer expected");
7242 }
7243}
7244
7245static struct triple *int_const(
7246 struct compile_state *state, struct type *type, ulong_t value)
7247{
7248 struct triple *result;
7249 switch(type->type & TYPE_MASK) {
7250 case TYPE_CHAR:
7251 case TYPE_INT: case TYPE_UINT:
7252 case TYPE_LONG: case TYPE_ULONG:
7253 break;
7254 default:
Eric Biederman90089602004-05-28 14:11:54 +00007255 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007256 }
7257 result = triple(state, OP_INTCONST, type, 0, 0);
7258 result->u.cval = value;
7259 return result;
7260}
7261
7262
Eric Biederman83b991a2003-10-11 06:20:25 +00007263static struct triple *read_expr(struct compile_state *state, struct triple *def);
7264
Stefan Reinauer14e22772010-04-27 06:56:47 +00007265static struct triple *do_mk_addr_expr(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007266 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007267{
7268 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007269 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007270 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007271
Eric Biederman90089602004-05-28 14:11:54 +00007272 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007273
Stefan Reinauer14e22772010-04-27 06:56:47 +00007274
Eric Biedermanb138ac82003-04-22 18:44:01 +00007275 result = 0;
7276 if (expr->op == OP_ADECL) {
7277 error(state, expr, "address of auto variables not supported");
7278 }
7279 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007280 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007281 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007282 result->u.cval = offset;
7283 }
7284 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007285 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007286 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007287 int_const(state, &ulong_type, offset));
7288 }
Eric Biederman90089602004-05-28 14:11:54 +00007289 else if (expr->op == OP_BLOBCONST) {
7290 FINISHME();
7291 internal_error(state, expr, "not yet implemented");
7292 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007293 else if (expr->op == OP_LIST) {
7294 error(state, 0, "Function addresses not supported");
7295 }
Eric Biederman90089602004-05-28 14:11:54 +00007296 else if (triple_is_part(state, expr)) {
7297 struct triple *part;
7298 part = expr;
7299 expr = MISC(expr, 0);
7300 if (part->op == OP_DOT) {
7301 offset += bits_to_bytes(
7302 field_offset(state, expr->type, part->u.field));
7303 }
7304 else if (part->op == OP_INDEX) {
7305 offset += bits_to_bytes(
7306 index_offset(state, expr->type, part->u.cval));
7307 }
7308 else {
7309 internal_error(state, part, "unhandled part type");
7310 }
7311 result = do_mk_addr_expr(state, expr, type, offset);
7312 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007313 if (!result) {
7314 internal_error(state, expr, "cannot take address of expression");
7315 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007316 return result;
7317}
7318
Eric Biederman0babc1c2003-05-09 02:39:00 +00007319static struct triple *mk_addr_expr(
7320 struct compile_state *state, struct triple *expr, ulong_t offset)
7321{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007322 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007323}
7324
Eric Biedermanb138ac82003-04-22 18:44:01 +00007325static struct triple *mk_deref_expr(
7326 struct compile_state *state, struct triple *expr)
7327{
7328 struct type *base_type;
7329 pointer(state, expr);
7330 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007331 return triple(state, OP_DEREF, base_type, expr, 0);
7332}
7333
Eric Biederman90089602004-05-28 14:11:54 +00007334/* lvalue conversions always apply except when certain operators
7335 * are applied. So I apply apply it when I know no more
7336 * operators will be applied.
7337 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007338static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007339{
Eric Biederman5cd81732004-03-11 15:01:31 +00007340 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007341 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7342 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007343 type = new_type(
7344 TYPE_POINTER | (def->type->type & QUAL_MASK),
7345 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007346 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007347 struct triple *addrconst;
7348 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7349 internal_error(state, def, "bad array constant");
7350 }
7351 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7352 MISC(addrconst, 0) = def;
7353 def = addrconst;
7354 }
7355 else {
Eric Biederman90089602004-05-28 14:11:54 +00007356 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007357 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007358 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007359 /* Transform a function to a pointer to it */
7360 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7361 def = mk_addr_expr(state, def, 0);
7362 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007363 return def;
7364}
7365
Eric Biederman0babc1c2003-05-09 02:39:00 +00007366static struct triple *deref_field(
7367 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7368{
7369 struct triple *result;
7370 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007371 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007372 if (!field) {
7373 internal_error(state, 0, "No field passed to deref_field");
7374 }
7375 result = 0;
7376 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007377 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7378 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007379 error(state, 0, "request for member %s in something not a struct or union",
7380 field->name);
7381 }
Eric Biederman03b59862003-06-24 14:27:37 +00007382 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007383 if ((type->type & STOR_MASK) == STOR_PERM) {
7384 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007385 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007386 result = do_mk_addr_expr(state, expr, member, offset);
7387 result = mk_deref_expr(state, result);
7388 }
7389 else {
7390 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007391 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007392 result->u.field = field;
7393 }
7394 return result;
7395}
7396
Eric Biederman90089602004-05-28 14:11:54 +00007397static struct triple *deref_index(
7398 struct compile_state *state, struct triple *expr, size_t index)
7399{
7400 struct triple *result;
7401 struct type *type, *member;
7402 ulong_t offset;
7403
7404 result = 0;
7405 type = expr->type;
7406 member = index_type(state, type, index);
7407
7408 if ((type->type & STOR_MASK) == STOR_PERM) {
7409 offset = bits_to_bytes(index_offset(state, type, index));
7410 result = do_mk_addr_expr(state, expr, member, offset);
7411 result = mk_deref_expr(state, result);
7412 }
7413 else {
7414 result = triple(state, OP_INDEX, member, expr, 0);
7415 result->u.cval = index;
7416 }
7417 return result;
7418}
7419
Eric Biedermanb138ac82003-04-22 18:44:01 +00007420static struct triple *read_expr(struct compile_state *state, struct triple *def)
7421{
7422 int op;
7423 if (!def) {
7424 return 0;
7425 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007426#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007427#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007428#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007429 /* Transform lvalues into something we can read */
7430 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007431 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007432 return def;
7433 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007434 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007435 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007436 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007437 if (def->op == OP_SDECL) {
7438 def = mk_addr_expr(state, def, 0);
7439 def = mk_deref_expr(state, def);
7440 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007441 op = OP_LOAD;
7442 }
Eric Biederman90089602004-05-28 14:11:54 +00007443 def = triple(state, op, def->type, def, 0);
7444 if (def->type->type & QUAL_VOLATILE) {
7445 def->id |= TRIPLE_FLAG_VOLATILE;
7446 }
7447 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007448}
7449
Stefan Reinauer14e22772010-04-27 06:56:47 +00007450int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007451 struct type *dest, struct type *rval)
7452{
7453 int compatible = 0;
7454 /* Both operands have arithmetic type */
7455 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7456 compatible = 1;
7457 }
7458 /* One operand is a pointer and the other is a pointer to void */
7459 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7460 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7461 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7462 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7463 compatible = 1;
7464 }
7465 /* If both types are the same without qualifiers we are good */
7466 else if (equiv_ptrs(dest, rval)) {
7467 compatible = 1;
7468 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007469 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007470 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007471 compatible = 1;
7472 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007473 return compatible;
7474}
7475
Eric Biedermane058a1e2003-07-12 01:21:31 +00007476static void write_compatible(struct compile_state *state,
7477 struct type *dest, struct type *rval)
7478{
7479 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007480 FILE *fp = state->errout;
7481 fprintf(fp, "dest: ");
7482 name_of(fp, dest);
7483 fprintf(fp,"\nrval: ");
7484 name_of(fp, rval);
7485 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007486 error(state, 0, "Incompatible types in assignment");
7487 }
7488}
7489
Eric Biedermane058a1e2003-07-12 01:21:31 +00007490static int is_init_compatible(struct compile_state *state,
7491 struct type *dest, struct type *rval)
7492{
7493 int compatible = 0;
7494 if (is_write_compatible(state, dest, rval)) {
7495 compatible = 1;
7496 }
7497 else if (equiv_types(dest, rval)) {
7498 compatible = 1;
7499 }
7500 return compatible;
7501}
7502
Eric Biedermanb138ac82003-04-22 18:44:01 +00007503static struct triple *write_expr(
7504 struct compile_state *state, struct triple *dest, struct triple *rval)
7505{
7506 struct triple *def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007507
7508 def = 0;
7509 if (!rval) {
7510 internal_error(state, 0, "missing rval");
7511 }
7512
7513 if (rval->op == OP_LIST) {
7514 internal_error(state, 0, "expression of type OP_LIST?");
7515 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007516 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007517 internal_error(state, 0, "writing to a non lvalue?");
7518 }
Eric Biederman00443072003-06-24 12:34:45 +00007519 if (dest->type->type & QUAL_CONST) {
7520 internal_error(state, 0, "modifable lvalue expexted");
7521 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007522
7523 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007524 if (!equiv_types(dest->type, rval->type)) {
7525 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7526 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007527
7528 /* Now figure out which assignment operator to use */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007529 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007530 def = triple(state, OP_WRITE, dest->type, rval, dest);
7531 if (MISC(def, 0) != dest) {
7532 internal_error(state, def, "huh?");
7533 }
7534 if (RHS(def, 0) != rval) {
7535 internal_error(state, def, "huh?");
7536 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007537 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007538 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007539 }
Eric Biederman90089602004-05-28 14:11:54 +00007540 if (def->type->type & QUAL_VOLATILE) {
7541 def->id |= TRIPLE_FLAG_VOLATILE;
7542 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007543 return def;
7544}
7545
7546static struct triple *init_expr(
7547 struct compile_state *state, struct triple *dest, struct triple *rval)
7548{
7549 struct triple *def;
7550
7551 def = 0;
7552 if (!rval) {
7553 internal_error(state, 0, "missing rval");
7554 }
7555 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7556 rval = read_expr(state, rval);
7557 def = write_expr(state, dest, rval);
7558 }
7559 else {
7560 /* Fill in the array size if necessary */
7561 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7562 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7563 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7564 dest->type->elements = rval->type->elements;
7565 }
7566 }
7567 if (!equiv_types(dest->type, rval->type)) {
7568 error(state, 0, "Incompatible types in inializer");
7569 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007570 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007571 insert_triple(state, dest, rval);
7572 rval->id |= TRIPLE_FLAG_FLATTENED;
7573 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007574 }
7575 return def;
7576}
7577
7578struct type *arithmetic_result(
7579 struct compile_state *state, struct triple *left, struct triple *right)
7580{
7581 struct type *type;
7582 /* Sanity checks to ensure I am working with arithmetic types */
7583 arithmetic(state, left);
7584 arithmetic(state, right);
7585 type = new_type(
7586 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007587 get_basic_type(left->type),
7588 get_basic_type(right->type)),
7589 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007590 return type;
7591}
7592
7593struct type *ptr_arithmetic_result(
7594 struct compile_state *state, struct triple *left, struct triple *right)
7595{
7596 struct type *type;
7597 /* Sanity checks to ensure I am working with the proper types */
7598 ptr_arithmetic(state, left);
7599 arithmetic(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007600 if (TYPE_ARITHMETIC(left->type->type) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00007601 TYPE_ARITHMETIC(right->type->type)) {
7602 type = arithmetic_result(state, left, right);
7603 }
7604 else if (TYPE_PTR(left->type->type)) {
7605 type = left->type;
7606 }
7607 else {
7608 internal_error(state, 0, "huh?");
7609 type = 0;
7610 }
7611 return type;
7612}
7613
Eric Biedermanb138ac82003-04-22 18:44:01 +00007614/* boolean helper function */
7615
Stefan Reinauer14e22772010-04-27 06:56:47 +00007616static struct triple *ltrue_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007617 struct triple *expr)
7618{
7619 switch(expr->op) {
7620 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7621 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7622 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7623 /* If the expression is already boolean do nothing */
7624 break;
7625 default:
7626 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7627 break;
7628 }
7629 return expr;
7630}
7631
Stefan Reinauer14e22772010-04-27 06:56:47 +00007632static struct triple *lfalse_expr(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007633 struct triple *expr)
7634{
7635 return triple(state, OP_LFALSE, &int_type, expr, 0);
7636}
7637
Eric Biederman90089602004-05-28 14:11:54 +00007638static struct triple *mkland_expr(
7639 struct compile_state *state,
7640 struct triple *left, struct triple *right)
7641{
7642 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007643 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007644
7645 /* Generate some intermediate triples */
7646 end = label(state);
7647 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007648
Eric Biederman90089602004-05-28 14:11:54 +00007649 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007650 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007651
7652 /* Jump if the value is false */
Stefan Reinauer14e22772010-04-27 06:56:47 +00007653 jmp = branch(state, end,
Eric Biederman90089602004-05-28 14:11:54 +00007654 lfalse_expr(state, read_expr(state, var)));
7655 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007656
Eric Biederman90089602004-05-28 14:11:54 +00007657 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007658 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007659
7660 /* An expression for the computed value */
7661 val = read_expr(state, var);
7662
7663 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007664 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007665
Eric Biederman90089602004-05-28 14:11:54 +00007666 return def;
7667}
7668
7669static struct triple *mklor_expr(
7670 struct compile_state *state,
7671 struct triple *left, struct triple *right)
7672{
7673 struct triple *def, *val, *var, *jmp, *mid, *end;
7674
7675 /* Generate some intermediate triples */
7676 end = label(state);
7677 var = variable(state, &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007678
Eric Biederman90089602004-05-28 14:11:54 +00007679 /* Store the left hand side value */
7680 left = write_expr(state, var, left);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007681
Eric Biederman90089602004-05-28 14:11:54 +00007682 /* Jump if the value is true */
7683 jmp = branch(state, end, read_expr(state, var));
7684 mid = label(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007685
Eric Biederman90089602004-05-28 14:11:54 +00007686 /* Store the right hand side value */
7687 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007688
Eric Biederman90089602004-05-28 14:11:54 +00007689 /* An expression for the computed value*/
7690 val = read_expr(state, var);
7691
7692 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007693 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007694
7695 return def;
7696}
7697
7698static struct triple *mkcond_expr(
Stefan Reinauer14e22772010-04-27 06:56:47 +00007699 struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007700 struct triple *test, struct triple *left, struct triple *right)
7701{
Eric Biederman90089602004-05-28 14:11:54 +00007702 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007703 struct type *result_type;
7704 unsigned int left_type, right_type;
7705 bool(state, test);
7706 left_type = left->type->type;
7707 right_type = right->type->type;
7708 result_type = 0;
7709 /* Both operands have arithmetic type */
7710 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7711 result_type = arithmetic_result(state, left, right);
7712 }
7713 /* Both operands have void type */
7714 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7715 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7716 result_type = &void_type;
7717 }
7718 /* pointers to the same type... */
7719 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7720 ;
7721 }
7722 /* Both operands are pointers and left is a pointer to void */
7723 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7724 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7725 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7726 result_type = right->type;
7727 }
7728 /* Both operands are pointers and right is a pointer to void */
7729 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7730 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7731 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7732 result_type = left->type;
7733 }
7734 if (!result_type) {
7735 error(state, 0, "Incompatible types in conditional expression");
7736 }
Eric Biederman90089602004-05-28 14:11:54 +00007737 /* Generate some intermediate triples */
7738 mid = label(state);
7739 end = label(state);
7740 var = variable(state, result_type);
7741
7742 /* Branch if the test is false */
7743 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7744 top = label(state);
7745
7746 /* Store the left hand side value */
7747 left = write_expr(state, var, left);
7748
7749 /* Branch to the end */
7750 jmp2 = branch(state, end, 0);
7751
7752 /* Store the right hand side value */
7753 right = write_expr(state, var, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007754
Eric Biederman90089602004-05-28 14:11:54 +00007755 /* An expression for the computed value */
7756 val = read_expr(state, var);
7757
7758 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007759 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007760
Eric Biedermanb138ac82003-04-22 18:44:01 +00007761 return def;
7762}
7763
7764
Eric Biederman0babc1c2003-05-09 02:39:00 +00007765static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007766{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007767#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007768#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007769#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007770 int count;
7771 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007772 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7773 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007774 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007775 else if (ins->op == OP_DEREF) {
7776 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007777 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007778 else if (ins->op == OP_VAL) {
7779 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007780 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007781 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007782 /* Don't figure the depth of a call just guess it is huge */
7783 count = 1000;
7784 }
7785 else {
7786 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007787 expr = triple_rhs(state, ins, 0);
7788 for(;expr; expr = triple_rhs(state, ins, expr)) {
7789 if (*expr) {
7790 int depth;
7791 depth = expr_depth(state, *expr);
7792 if (depth > count) {
7793 count = depth;
7794 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007795 }
7796 }
7797 }
7798 return count + 1;
7799}
7800
Eric Biederman0babc1c2003-05-09 02:39:00 +00007801static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007802 struct compile_state *state, struct triple *first, struct triple *ptr,
7803 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007804{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007805 struct rhs_vector {
7806 int depth;
7807 struct triple **ins;
7808 } vector[MAX_RHS];
7809 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007810 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007811 rhs = ptr->rhs;
7812 lhs = ptr->lhs;
7813 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007814 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007815 ptr->op, tops(ptr->op));
7816 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007817 /* Find the depth of the rhs elements */
7818 for(i = 0; i < rhs; i++) {
7819 vector[i].ins = &RHS(ptr, i);
7820 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007821 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007822 /* Selection sort the rhs */
7823 for(i = 0; i < rhs; i++) {
7824 int j, max = i;
7825 for(j = i + 1; j < rhs; j++ ) {
7826 if (vector[j].depth > vector[max].depth) {
7827 max = j;
7828 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007829 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007830 if (max != i) {
7831 struct rhs_vector tmp;
7832 tmp = vector[i];
7833 vector[i] = vector[max];
7834 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007835 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007836 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007837 /* Now flatten the rhs elements */
7838 for(i = 0; i < rhs; i++) {
7839 *vector[i].ins = flatten(state, first, *vector[i].ins);
7840 use_triple(*vector[i].ins, ptr);
7841 }
Eric Biederman90089602004-05-28 14:11:54 +00007842 if (lhs) {
7843 insert_triple(state, first, ptr);
7844 ptr->id |= TRIPLE_FLAG_FLATTENED;
7845 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Stefan Reinauer14e22772010-04-27 06:56:47 +00007846
Eric Biederman90089602004-05-28 14:11:54 +00007847 /* Now flatten the lhs elements */
7848 for(i = 0; i < lhs; i++) {
7849 struct triple **ins = &LHS(ptr, i);
7850 *ins = flatten(state, first, *ins);
7851 use_triple(*ins, ptr);
7852 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007853 }
7854 return ptr;
7855}
7856
Eric Biederman90089602004-05-28 14:11:54 +00007857static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007858 struct compile_state *state, struct triple *first, struct triple *ptr)
7859{
Eric Biederman90089602004-05-28 14:11:54 +00007860 struct triple *head, *body, *val;
7861 head = RHS(ptr, 0);
7862 RHS(ptr, 0) = 0;
7863 val = head->prev;
7864 body = head->next;
7865 release_triple(state, head);
7866 release_triple(state, ptr);
7867 val->next = first;
7868 body->prev = first->prev;
7869 body->prev->next = body;
7870 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007871
Eric Biederman90089602004-05-28 14:11:54 +00007872 if (triple_is_cbranch(state, body->prev) ||
7873 triple_is_call(state, body->prev)) {
7874 unuse_triple(first, body->prev);
7875 use_triple(body, body->prev);
7876 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00007877
Eric Biederman90089602004-05-28 14:11:54 +00007878 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7879 internal_error(state, val, "val not flattened?");
7880 }
7881
7882 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007883}
7884
Eric Biederman90089602004-05-28 14:11:54 +00007885
7886static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007887 struct compile_state *state, struct triple *first, struct triple *ptr)
7888{
Eric Biederman90089602004-05-28 14:11:54 +00007889 if (!triple_is_part(state, ptr)) {
7890 internal_error(state, ptr, "not a part");
7891 }
7892 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7893 internal_error(state, ptr, "unexpected args for: %d %s",
7894 ptr->op, tops(ptr->op));
7895 }
7896 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7897 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007898 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007899}
7900
7901static struct triple *flatten(
7902 struct compile_state *state, struct triple *first, struct triple *ptr)
7903{
7904 struct triple *orig_ptr;
7905 if (!ptr)
7906 return 0;
7907 do {
7908 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007909 /* Only flatten triples once */
7910 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7911 return ptr;
7912 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007913 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007914 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007915 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7916 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007917 break;
Eric Biederman90089602004-05-28 14:11:54 +00007918 case OP_PROG:
7919 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007920 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007921 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007922 ptr = flatten_generic(state, first, ptr, 1);
7923 insert_triple(state, first, ptr);
7924 ptr->id |= TRIPLE_FLAG_FLATTENED;
7925 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7926 if (ptr->next != ptr) {
7927 use_triple(ptr->next, ptr);
7928 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007929 break;
7930 case OP_READ:
7931 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007932 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7933 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007934 break;
Eric Biederman90089602004-05-28 14:11:54 +00007935 case OP_WRITE:
7936 ptr = flatten_generic(state, first, ptr, 1);
7937 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7938 use_triple(MISC(ptr, 0), ptr);
7939 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007940 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007941 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007942 break;
7943 case OP_CBRANCH:
7944 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7945 use_triple(RHS(ptr, 0), ptr);
7946 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007947 insert_triple(state, first, ptr);
7948 ptr->id |= TRIPLE_FLAG_FLATTENED;
7949 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007950 if (ptr->next != ptr) {
7951 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007952 }
7953 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007954 case OP_CALL:
7955 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7956 use_triple(MISC(ptr, 0), ptr);
7957 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007958 insert_triple(state, first, ptr);
7959 ptr->id |= TRIPLE_FLAG_FLATTENED;
7960 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007961 if (ptr->next != ptr) {
7962 use_triple(ptr->next, ptr);
7963 }
7964 break;
7965 case OP_RET:
7966 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7967 use_triple(RHS(ptr, 0), ptr);
7968 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007969 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007970 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007971 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007972 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007973 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007974 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007975 break;
7976 case OP_DEREF:
7977 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007978 ptr = RHS(ptr, 0);
7979 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007980 free_triple(state, orig_ptr);
7981 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007982 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007983 if (RHS(ptr, 0)->op == OP_DEREF) {
7984 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007985 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007986 base = MISC(ptr, 0);
7987 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007988 left = RHS(base, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +00007989 ptr = triple(state, OP_ADD, left->type,
Eric Biederman03b59862003-06-24 14:27:37 +00007990 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007991 int_const(state, &ulong_type, offset));
7992 free_triple(state, base);
7993 }
Eric Biederman90089602004-05-28 14:11:54 +00007994 else {
7995 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007996 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007997 break;
Eric Biederman90089602004-05-28 14:11:54 +00007998 case OP_INDEX:
7999 if (RHS(ptr, 0)->op == OP_DEREF) {
8000 struct triple *base, *left;
8001 ulong_t offset;
8002 base = MISC(ptr, 0);
8003 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8004 left = RHS(base, 0);
8005 ptr = triple(state, OP_ADD, left->type,
8006 read_expr(state, left),
8007 int_const(state, &long_type, offset));
8008 free_triple(state, base);
8009 }
8010 else {
8011 ptr = flatten_part(state, first, ptr);
8012 }
8013 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008014 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008015 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008016 use_triple(ptr, MISC(ptr, 0));
8017 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008018 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008019 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8020 use_triple(MISC(ptr, 0), ptr);
8021 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008022 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008023 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008024 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8025 use_triple(MISC(ptr, 0), ptr);
8026 insert_triple(state, first, ptr);
8027 ptr->id |= TRIPLE_FLAG_FLATTENED;
8028 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8029 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008030 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008031 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008032 break;
8033 default:
8034 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008035 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008036 break;
8037 }
8038 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008039 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008040 insert_triple(state, first, ptr);
8041 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008042 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008044 return ptr;
8045}
8046
8047static void release_expr(struct compile_state *state, struct triple *expr)
8048{
8049 struct triple *head;
8050 head = label(state);
8051 flatten(state, head, expr);
8052 while(head->next != head) {
8053 release_triple(state, head->next);
8054 }
8055 free_triple(state, head);
8056}
8057
8058static int replace_rhs_use(struct compile_state *state,
8059 struct triple *orig, struct triple *new, struct triple *use)
8060{
8061 struct triple **expr;
8062 int found;
8063 found = 0;
8064 expr = triple_rhs(state, use, 0);
8065 for(;expr; expr = triple_rhs(state, use, expr)) {
8066 if (*expr == orig) {
8067 *expr = new;
8068 found = 1;
8069 }
8070 }
8071 if (found) {
8072 unuse_triple(orig, use);
8073 use_triple(new, use);
8074 }
8075 return found;
8076}
8077
8078static int replace_lhs_use(struct compile_state *state,
8079 struct triple *orig, struct triple *new, struct triple *use)
8080{
8081 struct triple **expr;
8082 int found;
8083 found = 0;
8084 expr = triple_lhs(state, use, 0);
8085 for(;expr; expr = triple_lhs(state, use, expr)) {
8086 if (*expr == orig) {
8087 *expr = new;
8088 found = 1;
8089 }
8090 }
8091 if (found) {
8092 unuse_triple(orig, use);
8093 use_triple(new, use);
8094 }
8095 return found;
8096}
8097
Eric Biederman90089602004-05-28 14:11:54 +00008098static int replace_misc_use(struct compile_state *state,
8099 struct triple *orig, struct triple *new, struct triple *use)
8100{
8101 struct triple **expr;
8102 int found;
8103 found = 0;
8104 expr = triple_misc(state, use, 0);
8105 for(;expr; expr = triple_misc(state, use, expr)) {
8106 if (*expr == orig) {
8107 *expr = new;
8108 found = 1;
8109 }
8110 }
8111 if (found) {
8112 unuse_triple(orig, use);
8113 use_triple(new, use);
8114 }
8115 return found;
8116}
8117
8118static int replace_targ_use(struct compile_state *state,
8119 struct triple *orig, struct triple *new, struct triple *use)
8120{
8121 struct triple **expr;
8122 int found;
8123 found = 0;
8124 expr = triple_targ(state, use, 0);
8125 for(;expr; expr = triple_targ(state, use, expr)) {
8126 if (*expr == orig) {
8127 *expr = new;
8128 found = 1;
8129 }
8130 }
8131 if (found) {
8132 unuse_triple(orig, use);
8133 use_triple(new, use);
8134 }
8135 return found;
8136}
8137
8138static void replace_use(struct compile_state *state,
8139 struct triple *orig, struct triple *new, struct triple *use)
8140{
8141 int found;
8142 found = 0;
8143 found |= replace_rhs_use(state, orig, new, use);
8144 found |= replace_lhs_use(state, orig, new, use);
8145 found |= replace_misc_use(state, orig, new, use);
8146 found |= replace_targ_use(state, orig, new, use);
8147 if (!found) {
8148 internal_error(state, use, "use without use");
8149 }
8150}
8151
Eric Biedermanb138ac82003-04-22 18:44:01 +00008152static void propogate_use(struct compile_state *state,
8153 struct triple *orig, struct triple *new)
8154{
8155 struct triple_set *user, *next;
8156 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008157 /* Careful replace_use modifies the use chain and
8158 * removes use. So we must get a copy of the next
8159 * entry early.
8160 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008161 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008162 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008163 }
8164 if (orig->use) {
8165 internal_error(state, orig, "used after propogate_use");
8166 }
8167}
8168
8169/*
8170 * Code generators
8171 * ===========================
8172 */
8173
Eric Biederman90089602004-05-28 14:11:54 +00008174static struct triple *mk_cast_expr(
8175 struct compile_state *state, struct type *type, struct triple *expr)
8176{
8177 struct triple *def;
8178 def = read_expr(state, expr);
8179 def = triple(state, OP_CONVERT, type, def, 0);
8180 return def;
8181}
8182
Eric Biedermanb138ac82003-04-22 18:44:01 +00008183static struct triple *mk_add_expr(
8184 struct compile_state *state, struct triple *left, struct triple *right)
8185{
8186 struct type *result_type;
8187 /* Put pointer operands on the left */
8188 if (is_pointer(right)) {
8189 struct triple *tmp;
8190 tmp = left;
8191 left = right;
8192 right = tmp;
8193 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008194 left = read_expr(state, left);
8195 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008196 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008197 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008198 struct type *ptr_math;
8199 int op;
8200 if (is_signed(right->type)) {
8201 ptr_math = &long_type;
8202 op = OP_SMUL;
8203 } else {
8204 ptr_math = &ulong_type;
8205 op = OP_UMUL;
8206 }
8207 if (!equiv_types(right->type, ptr_math)) {
8208 right = mk_cast_expr(state, ptr_math, right);
8209 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008210 right = triple(state, op, ptr_math, right,
8211 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008212 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008213 }
8214 return triple(state, OP_ADD, result_type, left, right);
8215}
8216
8217static struct triple *mk_sub_expr(
8218 struct compile_state *state, struct triple *left, struct triple *right)
8219{
8220 struct type *result_type;
8221 result_type = ptr_arithmetic_result(state, left, right);
8222 left = read_expr(state, left);
8223 right = read_expr(state, right);
8224 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008225 struct type *ptr_math;
8226 int op;
8227 if (is_signed(right->type)) {
8228 ptr_math = &long_type;
8229 op = OP_SMUL;
8230 } else {
8231 ptr_math = &ulong_type;
8232 op = OP_UMUL;
8233 }
8234 if (!equiv_types(right->type, ptr_math)) {
8235 right = mk_cast_expr(state, ptr_math, right);
8236 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008237 right = triple(state, op, ptr_math, right,
8238 int_const(state, ptr_math,
Eric Biederman90089602004-05-28 14:11:54 +00008239 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008240 }
8241 return triple(state, OP_SUB, result_type, left, right);
8242}
8243
8244static struct triple *mk_pre_inc_expr(
8245 struct compile_state *state, struct triple *def)
8246{
8247 struct triple *val;
8248 lvalue(state, def);
8249 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8250 return triple(state, OP_VAL, def->type,
8251 write_expr(state, def, val),
8252 val);
8253}
8254
8255static struct triple *mk_pre_dec_expr(
8256 struct compile_state *state, struct triple *def)
8257{
8258 struct triple *val;
8259 lvalue(state, def);
8260 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8261 return triple(state, OP_VAL, def->type,
8262 write_expr(state, def, val),
8263 val);
8264}
8265
8266static struct triple *mk_post_inc_expr(
8267 struct compile_state *state, struct triple *def)
8268{
8269 struct triple *val;
8270 lvalue(state, def);
8271 val = read_expr(state, def);
8272 return triple(state, OP_VAL, def->type,
8273 write_expr(state, def,
8274 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8275 , val);
8276}
8277
8278static struct triple *mk_post_dec_expr(
8279 struct compile_state *state, struct triple *def)
8280{
8281 struct triple *val;
8282 lvalue(state, def);
8283 val = read_expr(state, def);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008284 return triple(state, OP_VAL, def->type,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008285 write_expr(state, def,
8286 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8287 , val);
8288}
8289
8290static struct triple *mk_subscript_expr(
8291 struct compile_state *state, struct triple *left, struct triple *right)
8292{
8293 left = read_expr(state, left);
8294 right = read_expr(state, right);
8295 if (!is_pointer(left) && !is_pointer(right)) {
8296 error(state, left, "subscripted value is not a pointer");
8297 }
8298 return mk_deref_expr(state, mk_add_expr(state, left, right));
8299}
8300
Eric Biedermane058a1e2003-07-12 01:21:31 +00008301
Eric Biedermanb138ac82003-04-22 18:44:01 +00008302/*
8303 * Compile time evaluation
8304 * ===========================
8305 */
8306static int is_const(struct triple *ins)
8307{
8308 return IS_CONST_OP(ins->op);
8309}
8310
Eric Biederman83b991a2003-10-11 06:20:25 +00008311static int is_simple_const(struct triple *ins)
8312{
Eric Biederman90089602004-05-28 14:11:54 +00008313 /* Is this a constant that u.cval has the value.
8314 * Or equivalently is this a constant that read_const
8315 * works on.
Stefan Reinauer14e22772010-04-27 06:56:47 +00008316 * So far only OP_INTCONST qualifies.
Eric Biederman90089602004-05-28 14:11:54 +00008317 */
8318 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008319}
8320
Stefan Reinauer14e22772010-04-27 06:56:47 +00008321static int constants_equal(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008322 struct triple *left, struct triple *right)
8323{
8324 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008325 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8326 equal = 0;
8327 }
8328 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008329 equal = 0;
8330 }
8331 else if (left->op != right->op) {
8332 equal = 0;
8333 }
8334 else if (!equiv_types(left->type, right->type)) {
8335 equal = 0;
8336 }
8337 else {
8338 equal = 0;
8339 switch(left->op) {
8340 case OP_INTCONST:
8341 if (left->u.cval == right->u.cval) {
8342 equal = 1;
8343 }
8344 break;
8345 case OP_BLOBCONST:
8346 {
Eric Biederman90089602004-05-28 14:11:54 +00008347 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008348 lsize = size_of(state, left->type);
8349 rsize = size_of(state, right->type);
8350 if (lsize != rsize) {
8351 break;
8352 }
Eric Biederman90089602004-05-28 14:11:54 +00008353 bytes = bits_to_bytes(lsize);
8354 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008355 equal = 1;
8356 }
8357 break;
8358 }
8359 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008360 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008361 (left->u.cval == right->u.cval)) {
8362 equal = 1;
8363 }
8364 break;
8365 default:
8366 internal_error(state, left, "uknown constant type");
8367 break;
8368 }
8369 }
8370 return equal;
8371}
8372
8373static int is_zero(struct triple *ins)
8374{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008375 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008376}
8377
8378static int is_one(struct triple *ins)
8379{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008380 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008381}
8382
Stefan Reinauer50542a82007-10-24 11:14:14 +00008383#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008384static long_t bit_count(ulong_t value)
8385{
8386 int count;
8387 int i;
8388 count = 0;
8389 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8390 ulong_t mask;
8391 mask = 1;
8392 mask <<= i;
8393 if (value & mask) {
8394 count++;
8395 }
8396 }
8397 return count;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008398
Eric Biederman530b5192003-07-01 10:05:30 +00008399}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008400#endif
8401
Eric Biedermanb138ac82003-04-22 18:44:01 +00008402static long_t bsr(ulong_t value)
8403{
8404 int i;
8405 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8406 ulong_t mask;
8407 mask = 1;
8408 mask <<= i;
8409 if (value & mask) {
8410 return i;
8411 }
8412 }
8413 return -1;
8414}
8415
8416static long_t bsf(ulong_t value)
8417{
8418 int i;
8419 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8420 ulong_t mask;
8421 mask = 1;
8422 mask <<= 1;
8423 if (value & mask) {
8424 return i;
8425 }
8426 }
8427 return -1;
8428}
8429
Eric Biedermancb364952004-11-15 10:46:44 +00008430static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008431{
8432 return bsr(value);
8433}
8434
8435static long_t tlog2(struct triple *ins)
8436{
Eric Biedermancb364952004-11-15 10:46:44 +00008437 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008438}
8439
8440static int is_pow2(struct triple *ins)
8441{
8442 ulong_t value, mask;
8443 long_t log;
8444 if (!is_const(ins)) {
8445 return 0;
8446 }
8447 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008448 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008449 if (log == -1) {
8450 return 0;
8451 }
8452 mask = 1;
8453 mask <<= log;
8454 return ((value & mask) == value);
8455}
8456
8457static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008458 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008459{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008460 switch(rhs->type->type &TYPE_MASK) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00008461 case TYPE_CHAR:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008462 case TYPE_SHORT:
8463 case TYPE_INT:
8464 case TYPE_LONG:
Stefan Reinauer14e22772010-04-27 06:56:47 +00008465 case TYPE_UCHAR:
8466 case TYPE_USHORT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008467 case TYPE_UINT:
8468 case TYPE_ULONG:
8469 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008470 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008471 break;
8472 default:
Eric Biederman90089602004-05-28 14:11:54 +00008473 fprintf(state->errout, "type: ");
8474 name_of(state->errout, rhs->type);
8475 fprintf(state->errout, "\n");
8476 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008477 break;
8478 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008479 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008480 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008481 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008482 return rhs->u.cval;
8483}
8484
Eric Biederman5ade04a2003-10-22 04:03:46 +00008485static long_t read_sconst(struct compile_state *state,
8486 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008487{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008488 return (long_t)(rhs->u.cval);
8489}
8490
Eric Biederman5ade04a2003-10-22 04:03:46 +00008491int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8492{
8493 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008494 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008495 }
8496 return !is_zero(rhs);
8497}
8498
8499int const_eq(struct compile_state *state, struct triple *ins,
8500 struct triple *left, struct triple *right)
8501{
8502 int result;
8503 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008504 internal_warning(state, ins, "non const passed to const_eq");
8505 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008506 }
8507 else if (left == right) {
8508 result = 1;
8509 }
8510 else if (is_simple_const(left) && is_simple_const(right)) {
8511 ulong_t lval, rval;
8512 lval = read_const(state, ins, left);
8513 rval = read_const(state, ins, right);
8514 result = (lval == rval);
8515 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008516 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008517 (right->op == OP_ADDRCONST)) {
8518 result = (MISC(left, 0) == MISC(right, 0)) &&
8519 (left->u.cval == right->u.cval);
8520 }
8521 else {
Eric Biederman90089602004-05-28 14:11:54 +00008522 internal_warning(state, ins, "incomparable constants passed to const_eq");
8523 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008524 }
8525 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008526
Eric Biederman5ade04a2003-10-22 04:03:46 +00008527}
8528
8529int const_ucmp(struct compile_state *state, struct triple *ins,
8530 struct triple *left, struct triple *right)
8531{
8532 int result;
8533 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008534 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008535 result = -2;
8536 }
8537 else if (left == right) {
8538 result = 0;
8539 }
8540 else if (is_simple_const(left) && is_simple_const(right)) {
8541 ulong_t lval, rval;
8542 lval = read_const(state, ins, left);
8543 rval = read_const(state, ins, right);
8544 result = 0;
8545 if (lval > rval) {
8546 result = 1;
8547 } else if (rval > lval) {
8548 result = -1;
8549 }
8550 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008551 else if ((left->op == OP_ADDRCONST) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00008552 (right->op == OP_ADDRCONST) &&
8553 (MISC(left, 0) == MISC(right, 0))) {
8554 result = 0;
8555 if (left->u.cval > right->u.cval) {
8556 result = 1;
8557 } else if (left->u.cval < right->u.cval) {
8558 result = -1;
8559 }
8560 }
8561 else {
Eric Biederman90089602004-05-28 14:11:54 +00008562 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008563 result = -2;
8564 }
8565 return result;
8566}
8567
8568int const_scmp(struct compile_state *state, struct triple *ins,
8569 struct triple *left, struct triple *right)
8570{
8571 int result;
8572 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008573 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008574 result = -2;
8575 }
8576 else if (left == right) {
8577 result = 0;
8578 }
8579 else if (is_simple_const(left) && is_simple_const(right)) {
8580 long_t lval, rval;
8581 lval = read_sconst(state, ins, left);
8582 rval = read_sconst(state, ins, right);
8583 result = 0;
8584 if (lval > rval) {
8585 result = 1;
8586 } else if (rval > lval) {
8587 result = -1;
8588 }
8589 }
8590 else {
Eric Biederman90089602004-05-28 14:11:54 +00008591 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008592 result = -2;
8593 }
8594 return result;
8595}
8596
Eric Biedermanb138ac82003-04-22 18:44:01 +00008597static void unuse_rhs(struct compile_state *state, struct triple *ins)
8598{
8599 struct triple **expr;
8600 expr = triple_rhs(state, ins, 0);
8601 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008602 if (*expr) {
8603 unuse_triple(*expr, ins);
8604 *expr = 0;
8605 }
8606 }
8607}
8608
8609static void unuse_lhs(struct compile_state *state, struct triple *ins)
8610{
8611 struct triple **expr;
8612 expr = triple_lhs(state, ins, 0);
8613 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008614 unuse_triple(*expr, ins);
8615 *expr = 0;
8616 }
8617}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008618
Stefan Reinauer50542a82007-10-24 11:14:14 +00008619#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008620static void unuse_misc(struct compile_state *state, struct triple *ins)
8621{
8622 struct triple **expr;
8623 expr = triple_misc(state, ins, 0);
8624 for(;expr;expr = triple_misc(state, ins, expr)) {
8625 unuse_triple(*expr, ins);
8626 *expr = 0;
8627 }
8628}
8629
8630static void unuse_targ(struct compile_state *state, struct triple *ins)
8631{
8632 int i;
8633 struct triple **slot;
8634 slot = &TARG(ins, 0);
8635 for(i = 0; i < ins->targ; i++) {
8636 unuse_triple(slot[i], ins);
8637 slot[i] = 0;
8638 }
8639}
8640
Eric Biedermanb138ac82003-04-22 18:44:01 +00008641static void check_lhs(struct compile_state *state, struct triple *ins)
8642{
8643 struct triple **expr;
8644 expr = triple_lhs(state, ins, 0);
8645 for(;expr;expr = triple_lhs(state, ins, expr)) {
8646 internal_error(state, ins, "unexpected lhs");
8647 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008648
Eric Biedermanb138ac82003-04-22 18:44:01 +00008649}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008650#endif
Eric Biederman90089602004-05-28 14:11:54 +00008651
8652static void check_misc(struct compile_state *state, struct triple *ins)
8653{
8654 struct triple **expr;
8655 expr = triple_misc(state, ins, 0);
8656 for(;expr;expr = triple_misc(state, ins, expr)) {
8657 if (*expr) {
8658 internal_error(state, ins, "unexpected misc");
8659 }
8660 }
8661}
8662
Eric Biedermanb138ac82003-04-22 18:44:01 +00008663static void check_targ(struct compile_state *state, struct triple *ins)
8664{
8665 struct triple **expr;
8666 expr = triple_targ(state, ins, 0);
8667 for(;expr;expr = triple_targ(state, ins, expr)) {
8668 internal_error(state, ins, "unexpected targ");
8669 }
8670}
8671
8672static void wipe_ins(struct compile_state *state, struct triple *ins)
8673{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008674 /* Becareful which instructions you replace the wiped
8675 * instruction with, as there are not enough slots
8676 * in all instructions to hold all others.
8677 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008678 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008679 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008680 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008681 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008682 ins->lhs = 0;
8683 ins->rhs = 0;
8684 ins->misc = 0;
8685 ins->targ = 0;
8686}
8687
Stefan Reinauer50542a82007-10-24 11:14:14 +00008688#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008689static void wipe_branch(struct compile_state *state, struct triple *ins)
8690{
8691 /* Becareful which instructions you replace the wiped
8692 * instruction with, as there are not enough slots
8693 * in all instructions to hold all others.
8694 */
8695 unuse_rhs(state, ins);
8696 unuse_lhs(state, ins);
8697 unuse_misc(state, ins);
8698 unuse_targ(state, ins);
8699 ins->lhs = 0;
8700 ins->rhs = 0;
8701 ins->misc = 0;
8702 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008703}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008704#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008705
Stefan Reinauer14e22772010-04-27 06:56:47 +00008706static void mkcopy(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008707 struct triple *ins, struct triple *rhs)
8708{
Eric Biederman83b991a2003-10-11 06:20:25 +00008709 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008710 if (!equiv_types(ins->type, rhs->type)) {
8711 FILE *fp = state->errout;
8712 fprintf(fp, "src type: ");
8713 name_of(fp, rhs->type);
8714 fprintf(fp, "\ndst type: ");
8715 name_of(fp, ins->type);
8716 fprintf(fp, "\n");
8717 internal_error(state, ins, "mkcopy type mismatch");
8718 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008719 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008720 wipe_ins(state, ins);
8721 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008722 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008723 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008724 RHS(ins, 0) = rhs;
8725 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008726}
8727
Stefan Reinauer14e22772010-04-27 06:56:47 +00008728static void mkconst(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00008729 struct triple *ins, ulong_t value)
8730{
8731 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008732 fprintf(state->errout, "type: ");
8733 name_of(state->errout, ins->type);
8734 fprintf(state->errout, "\n");
8735 internal_error(state, ins, "unknown type to make constant value: %ld",
8736 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008737 }
8738 wipe_ins(state, ins);
8739 ins->op = OP_INTCONST;
8740 ins->u.cval = value;
8741}
8742
8743static void mkaddr_const(struct compile_state *state,
8744 struct triple *ins, struct triple *sdecl, ulong_t value)
8745{
Eric Biederman90089602004-05-28 14:11:54 +00008746 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008747 internal_error(state, ins, "bad base for addrconst");
8748 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008749 wipe_ins(state, ins);
8750 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008751 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008752 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008753 ins->u.cval = value;
8754 use_triple(sdecl, ins);
8755}
8756
Eric Biederman90089602004-05-28 14:11:54 +00008757#if DEBUG_DECOMPOSE_PRINT_TUPLES
Stefan Reinauer14e22772010-04-27 06:56:47 +00008758static void print_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008759 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008760{
Eric Biederman90089602004-05-28 14:11:54 +00008761 FILE *fp = state->dbgout;
8762 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8763 name_of(fp, tuple->type);
8764 if (tuple->lhs > 0) {
8765 fprintf(fp, " lhs: ");
8766 name_of(fp, LHS(tuple, 0)->type);
8767 }
8768 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +00008769
Eric Biederman90089602004-05-28 14:11:54 +00008770}
8771#endif
8772
Stefan Reinauer14e22772010-04-27 06:56:47 +00008773static struct triple *decompose_with_tuple(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008774 struct triple *ins, struct triple *tuple)
8775{
8776 struct triple *next;
8777 next = ins->next;
8778 flatten(state, next, tuple);
8779#if DEBUG_DECOMPOSE_PRINT_TUPLES
8780 print_tuple(state, ins, tuple);
8781#endif
8782
8783 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8784 struct triple *tmp;
8785 if (tuple->lhs != 1) {
8786 internal_error(state, tuple, "plain type in multiple registers?");
8787 }
8788 tmp = LHS(tuple, 0);
8789 release_triple(state, tuple);
8790 tuple = tmp;
8791 }
8792
8793 propogate_use(state, ins, tuple);
8794 release_triple(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008795
Eric Biederman90089602004-05-28 14:11:54 +00008796 return next;
8797}
8798
8799static struct triple *decompose_unknownval(struct compile_state *state,
8800 struct triple *ins)
8801{
8802 struct triple *tuple;
8803 ulong_t i;
8804
8805#if DEBUG_DECOMPOSE_HIRES
8806 FILE *fp = state->dbgout;
8807 fprintf(fp, "unknown type: ");
8808 name_of(fp, ins->type);
8809 fprintf(fp, "\n");
8810#endif
8811
8812 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008813 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008814 ins->occurance);
8815
8816 for(i = 0; i < tuple->lhs; i++) {
8817 struct type *piece_type;
8818 struct triple *unknown;
8819
8820 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8821 get_occurance(tuple->occurance);
8822 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8823 tuple->occurance);
8824 LHS(tuple, i) = unknown;
8825 }
8826 return decompose_with_tuple(state, ins, tuple);
8827}
8828
8829
Stefan Reinauer14e22772010-04-27 06:56:47 +00008830static struct triple *decompose_read(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008831 struct triple *ins)
8832{
8833 struct triple *tuple, *lval;
8834 ulong_t i;
8835
8836 lval = RHS(ins, 0);
8837
8838 if (lval->op == OP_PIECE) {
8839 return ins->next;
8840 }
8841 get_occurance(ins->occurance);
8842 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8843 ins->occurance);
8844
8845 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008846 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
Eric Biederman90089602004-05-28 14:11:54 +00008847 {
8848 internal_error(state, ins, "lhs size inconsistency?");
8849 }
8850 for(i = 0; i < tuple->lhs; i++) {
8851 struct triple *piece, *read, *bitref;
8852 if ((i != 0) || !triple_is_def(state, lval)) {
8853 piece = LHS(lval, i);
8854 } else {
8855 piece = lval;
8856 }
8857
8858 /* See if the piece is really a bitref */
8859 bitref = 0;
8860 if (piece->op == OP_BITREF) {
8861 bitref = piece;
8862 piece = RHS(bitref, 0);
8863 }
8864
8865 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008866 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008867 tuple->occurance);
8868 RHS(read, 0) = piece;
8869
8870 if (bitref) {
8871 struct triple *extract;
8872 int op;
8873 if (is_signed(bitref->type->left)) {
8874 op = OP_SEXTRACT;
8875 } else {
8876 op = OP_UEXTRACT;
8877 }
8878 get_occurance(tuple->occurance);
8879 extract = alloc_triple(state, op, bitref->type, -1, -1,
8880 tuple->occurance);
8881 RHS(extract, 0) = read;
8882 extract->u.bitfield.size = bitref->u.bitfield.size;
8883 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8884
8885 read = extract;
8886 }
8887
8888 LHS(tuple, i) = read;
8889 }
8890 return decompose_with_tuple(state, ins, tuple);
8891}
8892
Stefan Reinauer14e22772010-04-27 06:56:47 +00008893static struct triple *decompose_write(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008894 struct triple *ins)
8895{
8896 struct triple *tuple, *lval, *val;
8897 ulong_t i;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008898
Eric Biederman90089602004-05-28 14:11:54 +00008899 lval = MISC(ins, 0);
8900 val = RHS(ins, 0);
8901 get_occurance(ins->occurance);
8902 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8903 ins->occurance);
8904
8905 if ((tuple->lhs != lval->lhs) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +00008906 (!triple_is_def(state, lval) || tuple->lhs != 1))
Eric Biederman90089602004-05-28 14:11:54 +00008907 {
8908 internal_error(state, ins, "lhs size inconsistency?");
8909 }
8910 for(i = 0; i < tuple->lhs; i++) {
8911 struct triple *piece, *write, *pval, *bitref;
8912 if ((i != 0) || !triple_is_def(state, lval)) {
8913 piece = LHS(lval, i);
8914 } else {
8915 piece = lval;
8916 }
8917 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8918 pval = val;
8919 }
8920 else {
8921 if (i > val->lhs) {
8922 internal_error(state, ins, "lhs size inconsistency?");
8923 }
8924 pval = LHS(val, i);
8925 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00008926
Eric Biederman90089602004-05-28 14:11:54 +00008927 /* See if the piece is really a bitref */
8928 bitref = 0;
8929 if (piece->op == OP_BITREF) {
8930 struct triple *read, *deposit;
8931 bitref = piece;
8932 piece = RHS(bitref, 0);
8933
8934 /* Read the destination register */
8935 get_occurance(tuple->occurance);
8936 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8937 tuple->occurance);
8938 RHS(read, 0) = piece;
8939
8940 /* Deposit the new bitfield value */
8941 get_occurance(tuple->occurance);
8942 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8943 tuple->occurance);
8944 RHS(deposit, 0) = read;
8945 RHS(deposit, 1) = pval;
8946 deposit->u.bitfield.size = bitref->u.bitfield.size;
8947 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8948
8949 /* Now write the newly generated value */
8950 pval = deposit;
8951 }
8952
8953 get_occurance(tuple->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00008954 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00008955 tuple->occurance);
8956 MISC(write, 0) = piece;
8957 RHS(write, 0) = pval;
8958 LHS(tuple, i) = write;
8959 }
8960 return decompose_with_tuple(state, ins, tuple);
8961}
8962
8963struct decompose_load_info {
8964 struct occurance *occurance;
8965 struct triple *lval;
8966 struct triple *tuple;
8967};
8968static void decompose_load_cb(struct compile_state *state,
8969 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8970{
8971 struct decompose_load_info *info = arg;
8972 struct triple *load;
Stefan Reinauer14e22772010-04-27 06:56:47 +00008973
Eric Biederman90089602004-05-28 14:11:54 +00008974 if (reg_offset > info->tuple->lhs) {
8975 internal_error(state, info->tuple, "lhs to small?");
8976 }
8977 get_occurance(info->occurance);
8978 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8979 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8980 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8981}
8982
Stefan Reinauer14e22772010-04-27 06:56:47 +00008983static struct triple *decompose_load(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00008984 struct triple *ins)
8985{
8986 struct triple *tuple;
8987 struct decompose_load_info info;
8988
8989 if (!is_compound_type(ins->type)) {
8990 return ins->next;
8991 }
8992 get_occurance(ins->occurance);
8993 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8994 ins->occurance);
8995
8996 info.occurance = ins->occurance;
8997 info.lval = RHS(ins, 0);
8998 info.tuple = tuple;
8999 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
9000
9001 return decompose_with_tuple(state, ins, tuple);
9002}
9003
9004
9005struct decompose_store_info {
9006 struct occurance *occurance;
9007 struct triple *lval;
9008 struct triple *val;
9009 struct triple *tuple;
9010};
9011static void decompose_store_cb(struct compile_state *state,
9012 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9013{
9014 struct decompose_store_info *info = arg;
9015 struct triple *store;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009016
Eric Biederman90089602004-05-28 14:11:54 +00009017 if (reg_offset > info->tuple->lhs) {
9018 internal_error(state, info->tuple, "lhs to small?");
9019 }
9020 get_occurance(info->occurance);
9021 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9022 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9023 RHS(store, 1) = LHS(info->val, reg_offset);
9024 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9025}
9026
Stefan Reinauer14e22772010-04-27 06:56:47 +00009027static struct triple *decompose_store(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009028 struct triple *ins)
9029{
9030 struct triple *tuple;
9031 struct decompose_store_info info;
9032
9033 if (!is_compound_type(ins->type)) {
9034 return ins->next;
9035 }
9036 get_occurance(ins->occurance);
9037 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9038 ins->occurance);
9039
9040 info.occurance = ins->occurance;
9041 info.lval = RHS(ins, 0);
9042 info.val = RHS(ins, 1);
9043 info.tuple = tuple;
9044 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9045
9046 return decompose_with_tuple(state, ins, tuple);
9047}
9048
Stefan Reinauer14e22772010-04-27 06:56:47 +00009049static struct triple *decompose_dot(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009050 struct triple *ins)
9051{
9052 struct triple *tuple, *lval;
9053 struct type *type;
9054 size_t reg_offset;
9055 int i, idx;
9056
9057 lval = MISC(ins, 0);
9058 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9059 idx = reg_offset/REG_SIZEOF_REG;
9060 type = field_type(state, lval->type, ins->u.field);
9061#if DEBUG_DECOMPOSE_HIRES
9062 {
9063 FILE *fp = state->dbgout;
9064 fprintf(fp, "field type: ");
9065 name_of(fp, type);
9066 fprintf(fp, "\n");
9067 }
9068#endif
9069
9070 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009071 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009072 ins->occurance);
9073
9074 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9075 (tuple->lhs != 1))
9076 {
9077 internal_error(state, ins, "multi register bitfield?");
9078 }
9079
9080 for(i = 0; i < tuple->lhs; i++, idx++) {
9081 struct triple *piece;
9082 if (!triple_is_def(state, lval)) {
9083 if (idx > lval->lhs) {
9084 internal_error(state, ins, "inconsistent lhs count");
9085 }
9086 piece = LHS(lval, idx);
9087 } else {
9088 if (idx != 0) {
9089 internal_error(state, ins, "bad reg_offset into def");
9090 }
9091 if (i != 0) {
9092 internal_error(state, ins, "bad reg count from def");
9093 }
9094 piece = lval;
9095 }
9096
9097 /* Remember the offset of the bitfield */
9098 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9099 get_occurance(ins->occurance);
9100 piece = build_triple(state, OP_BITREF, type, piece, 0,
9101 ins->occurance);
9102 piece->u.bitfield.size = size_of(state, type);
9103 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9104 }
9105 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
Stefan Reinauer14e22772010-04-27 06:56:47 +00009106 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +00009107 "request for a nonbitfield sub register?");
9108 }
9109
9110 LHS(tuple, i) = piece;
9111 }
9112
9113 return decompose_with_tuple(state, ins, tuple);
9114}
9115
Stefan Reinauer14e22772010-04-27 06:56:47 +00009116static struct triple *decompose_index(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +00009117 struct triple *ins)
9118{
9119 struct triple *tuple, *lval;
9120 struct type *type;
9121 int i, idx;
9122
9123 lval = MISC(ins, 0);
9124 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9125 type = index_type(state, lval->type, ins->u.cval);
9126#if DEBUG_DECOMPOSE_HIRES
9127{
9128 FILE *fp = state->dbgout;
9129 fprintf(fp, "index type: ");
9130 name_of(fp, type);
9131 fprintf(fp, "\n");
9132}
9133#endif
9134
9135 get_occurance(ins->occurance);
Stefan Reinauer14e22772010-04-27 06:56:47 +00009136 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
Eric Biederman90089602004-05-28 14:11:54 +00009137 ins->occurance);
9138
9139 for(i = 0; i < tuple->lhs; i++, idx++) {
9140 struct triple *piece;
9141 if (!triple_is_def(state, lval)) {
9142 if (idx > lval->lhs) {
9143 internal_error(state, ins, "inconsistent lhs count");
9144 }
9145 piece = LHS(lval, idx);
9146 } else {
9147 if (idx != 0) {
9148 internal_error(state, ins, "bad reg_offset into def");
9149 }
9150 if (i != 0) {
9151 internal_error(state, ins, "bad reg count from def");
9152 }
9153 piece = lval;
9154 }
9155 LHS(tuple, i) = piece;
9156 }
9157
9158 return decompose_with_tuple(state, ins, tuple);
9159}
9160
9161static void decompose_compound_types(struct compile_state *state)
9162{
9163 struct triple *ins, *next, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +00009164 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009165 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009166
9167 /* Pass one expand compound values into pseudo registers.
9168 */
9169 next = first;
9170 do {
9171 ins = next;
9172 next = ins->next;
9173 switch(ins->op) {
9174 case OP_UNKNOWNVAL:
9175 next = decompose_unknownval(state, ins);
9176 break;
9177
9178 case OP_READ:
9179 next = decompose_read(state, ins);
9180 break;
9181
9182 case OP_WRITE:
9183 next = decompose_write(state, ins);
9184 break;
9185
9186
9187 /* Be very careful with the load/store logic. These
9188 * operations must convert from the in register layout
9189 * to the in memory layout, which is nontrivial.
9190 */
9191 case OP_LOAD:
9192 next = decompose_load(state, ins);
9193 break;
9194 case OP_STORE:
9195 next = decompose_store(state, ins);
9196 break;
9197
9198 case OP_DOT:
9199 next = decompose_dot(state, ins);
9200 break;
9201 case OP_INDEX:
9202 next = decompose_index(state, ins);
9203 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009204
Eric Biederman90089602004-05-28 14:11:54 +00009205 }
9206#if DEBUG_DECOMPOSE_HIRES
9207 fprintf(fp, "decompose next: %p \n", next);
9208 fflush(fp);
9209 fprintf(fp, "next->op: %d %s\n",
9210 next->op, tops(next->op));
9211 /* High resolution debugging mode */
9212 print_triples(state);
9213#endif
9214 } while (next != first);
9215
9216 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009217 */
9218 ins = first;
9219 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009220 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009221 if (ins->op == OP_TUPLE) {
9222 if (ins->use) {
9223 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009224 }
Eric Biederman90089602004-05-28 14:11:54 +00009225 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009226 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009227 }
Stefan Reinauer14e22772010-04-27 06:56:47 +00009228 }
Eric Biederman90089602004-05-28 14:11:54 +00009229 ins = next;
9230 } while(ins != first);
9231 ins = first;
9232 do {
9233 next = ins->next;
9234 if (ins->op == OP_BITREF) {
9235 if (ins->use) {
9236 internal_error(state, ins, "bitref used");
Stefan Reinauer14e22772010-04-27 06:56:47 +00009237 }
Eric Biederman90089602004-05-28 14:11:54 +00009238 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009239 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009240 }
9241 }
9242 ins = next;
9243 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009244
Eric Biederman0babc1c2003-05-09 02:39:00 +00009245 /* Pass three verify the state and set ->id to 0.
9246 */
Eric Biederman90089602004-05-28 14:11:54 +00009247 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009248 do {
Eric Biederman90089602004-05-28 14:11:54 +00009249 ins = next;
9250 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009251 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009252 if (triple_stores_block(state, ins)) {
9253 ins->u.block = 0;
9254 }
9255 if (triple_is_def(state, ins)) {
9256 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9257 internal_error(state, ins, "multi register value remains?");
9258 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009259 }
9260 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009261 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009262 }
Eric Biederman90089602004-05-28 14:11:54 +00009263 if (ins->op == OP_INDEX) {
9264 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009265 }
Eric Biederman90089602004-05-28 14:11:54 +00009266 if (ins->op == OP_BITREF) {
9267 internal_error(state, ins, "OP_BITREF remains?");
9268 }
9269 if (ins->op == OP_TUPLE) {
9270 internal_error(state, ins, "OP_TUPLE remains?");
9271 }
9272 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009273}
9274
Eric Biedermanb138ac82003-04-22 18:44:01 +00009275/* For those operations that cannot be simplified */
9276static void simplify_noop(struct compile_state *state, struct triple *ins)
9277{
9278 return;
9279}
9280
9281static void simplify_smul(struct compile_state *state, struct triple *ins)
9282{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009283 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009284 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009285 tmp = RHS(ins, 0);
9286 RHS(ins, 0) = RHS(ins, 1);
9287 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009288 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009289 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009290 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009291 left = read_sconst(state, ins, RHS(ins, 0));
9292 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009293 mkconst(state, ins, left * right);
9294 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009295 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009296 mkconst(state, ins, 0);
9297 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009298 else if (is_one(RHS(ins, 1))) {
9299 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009300 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009301 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009302 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009303 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009304 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009305 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009306 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009307 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009308 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009309 }
9310}
9311
9312static void simplify_umul(struct compile_state *state, struct triple *ins)
9313{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009314 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009315 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009316 tmp = RHS(ins, 0);
9317 RHS(ins, 0) = RHS(ins, 1);
9318 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009319 }
Eric Biederman90089602004-05-28 14:11:54 +00009320 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009321 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009322 left = read_const(state, ins, RHS(ins, 0));
9323 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009324 mkconst(state, ins, left * right);
9325 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009326 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009327 mkconst(state, ins, 0);
9328 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009329 else if (is_one(RHS(ins, 1))) {
9330 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009331 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009332 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009333 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009334 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009335 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009336 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009337 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009338 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009339 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009340 }
9341}
9342
9343static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9344{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009345 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009346 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009347 left = read_sconst(state, ins, RHS(ins, 0));
9348 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009349 mkconst(state, ins, left / right);
9350 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009351 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009352 mkconst(state, ins, 0);
9353 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009354 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009355 error(state, ins, "division by zero");
9356 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009357 else if (is_one(RHS(ins, 1))) {
9358 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009359 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009360 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009361 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009362 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009363 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009364 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009365 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009366 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009367 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009368 }
9369}
9370
9371static void simplify_udiv(struct compile_state *state, struct triple *ins)
9372{
Eric Biederman90089602004-05-28 14:11:54 +00009373 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009374 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009375 left = read_const(state, ins, RHS(ins, 0));
9376 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009377 mkconst(state, ins, left / right);
9378 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009379 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009380 mkconst(state, ins, 0);
9381 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009382 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009383 error(state, ins, "division by zero");
9384 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009385 else if (is_one(RHS(ins, 1))) {
9386 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009387 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009388 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009389 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009390 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009391 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009392 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009393 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009394 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009395 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396 }
9397}
9398
9399static void simplify_smod(struct compile_state *state, struct triple *ins)
9400{
Eric Biederman90089602004-05-28 14:11:54 +00009401 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009403 left = read_const(state, ins, RHS(ins, 0));
9404 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009405 mkconst(state, ins, left % right);
9406 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009407 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009408 mkconst(state, ins, 0);
9409 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009410 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009411 error(state, ins, "division by zero");
9412 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009413 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009414 mkconst(state, ins, 0);
9415 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009416 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009417 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009418 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009419 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009420 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009421 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009423 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009424 }
9425}
Eric Biederman83b991a2003-10-11 06:20:25 +00009426
Eric Biedermanb138ac82003-04-22 18:44:01 +00009427static void simplify_umod(struct compile_state *state, struct triple *ins)
9428{
Eric Biederman90089602004-05-28 14:11:54 +00009429 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009431 left = read_const(state, ins, RHS(ins, 0));
9432 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009433 mkconst(state, ins, left % right);
9434 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009435 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009436 mkconst(state, ins, 0);
9437 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009438 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009439 error(state, ins, "division by zero");
9440 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009441 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 mkconst(state, ins, 0);
9443 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009444 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009445 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009446 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009447 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009448 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009449 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009450 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009451 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009452 }
9453}
9454
9455static void simplify_add(struct compile_state *state, struct triple *ins)
9456{
9457 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009458 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009459 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009460 tmp = RHS(ins, 0);
9461 RHS(ins, 0) = RHS(ins, 1);
9462 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009463 }
Eric Biederman90089602004-05-28 14:11:54 +00009464 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009465 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009466 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009467 left = read_const(state, ins, RHS(ins, 0));
9468 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009469 mkconst(state, ins, left + right);
9470 }
Eric Biederman530b5192003-07-01 10:05:30 +00009471 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 struct triple *sdecl;
9473 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009474 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009475 left = RHS(ins, 0)->u.cval;
9476 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009477 mkaddr_const(state, ins, sdecl, left + right);
9478 }
Eric Biederman530b5192003-07-01 10:05:30 +00009479 else {
9480 internal_warning(state, ins, "Optimize me!");
9481 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009482 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009483 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009484 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009485 tmp = RHS(ins, 1);
9486 RHS(ins, 1) = RHS(ins, 0);
9487 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009488 }
9489}
9490
9491static void simplify_sub(struct compile_state *state, struct triple *ins)
9492{
Eric Biederman90089602004-05-28 14:11:54 +00009493 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009494 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009495 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009496 left = read_const(state, ins, RHS(ins, 0));
9497 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009498 mkconst(state, ins, left - right);
9499 }
Eric Biederman530b5192003-07-01 10:05:30 +00009500 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009501 struct triple *sdecl;
9502 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009503 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009504 left = RHS(ins, 0)->u.cval;
9505 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009506 mkaddr_const(state, ins, sdecl, left - right);
9507 }
Eric Biederman530b5192003-07-01 10:05:30 +00009508 else {
9509 internal_warning(state, ins, "Optimize me!");
9510 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009511 }
9512}
9513
9514static void simplify_sl(struct compile_state *state, struct triple *ins)
9515{
Eric Biederman90089602004-05-28 14:11:54 +00009516 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009517 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009518 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009519 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009520 warning(state, ins, "left shift count >= width of type");
9521 }
9522 }
Eric Biederman90089602004-05-28 14:11:54 +00009523 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009524 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009525 left = read_const(state, ins, RHS(ins, 0));
9526 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009527 mkconst(state, ins, left << right);
9528 }
9529}
9530
9531static void simplify_usr(struct compile_state *state, struct triple *ins)
9532{
Eric Biederman90089602004-05-28 14:11:54 +00009533 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009534 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009535 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009536 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009537 warning(state, ins, "right shift count >= width of type");
9538 }
9539 }
Eric Biederman90089602004-05-28 14:11:54 +00009540 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009541 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009542 left = read_const(state, ins, RHS(ins, 0));
9543 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009544 mkconst(state, ins, left >> right);
9545 }
9546}
9547
9548static void simplify_ssr(struct compile_state *state, struct triple *ins)
9549{
Eric Biederman90089602004-05-28 14:11:54 +00009550 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009551 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009552 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009553 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009554 warning(state, ins, "right shift count >= width of type");
9555 }
9556 }
Eric Biederman90089602004-05-28 14:11:54 +00009557 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009558 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009559 left = read_sconst(state, ins, RHS(ins, 0));
9560 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009561 mkconst(state, ins, left >> right);
9562 }
9563}
9564
9565static void simplify_and(struct compile_state *state, struct triple *ins)
9566{
Eric Biederman90089602004-05-28 14:11:54 +00009567 struct triple *left, *right;
9568 left = RHS(ins, 0);
9569 right = RHS(ins, 1);
9570
9571 if (is_simple_const(left) && is_simple_const(right)) {
9572 ulong_t lval, rval;
9573 lval = read_const(state, ins, left);
9574 rval = read_const(state, ins, right);
9575 mkconst(state, ins, lval & rval);
9576 }
9577 else if (is_zero(right) || is_zero(left)) {
9578 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009579 }
9580}
9581
9582static void simplify_or(struct compile_state *state, struct triple *ins)
9583{
Eric Biederman90089602004-05-28 14:11:54 +00009584 struct triple *left, *right;
9585 left = RHS(ins, 0);
9586 right = RHS(ins, 1);
9587
9588 if (is_simple_const(left) && is_simple_const(right)) {
9589 ulong_t lval, rval;
9590 lval = read_const(state, ins, left);
9591 rval = read_const(state, ins, right);
9592 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009593 }
Eric Biederman90089602004-05-28 14:11:54 +00009594#if 0 /* I need to handle type mismatches here... */
9595 else if (is_zero(right)) {
9596 mkcopy(state, ins, left);
9597 }
9598 else if (is_zero(left)) {
9599 mkcopy(state, ins, right);
9600 }
9601#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009602}
9603
9604static void simplify_xor(struct compile_state *state, struct triple *ins)
9605{
Eric Biederman90089602004-05-28 14:11:54 +00009606 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009607 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009608 left = read_const(state, ins, RHS(ins, 0));
9609 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009610 mkconst(state, ins, left ^ right);
9611 }
9612}
9613
9614static void simplify_pos(struct compile_state *state, struct triple *ins)
9615{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009616 if (is_const(RHS(ins, 0))) {
9617 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009618 }
9619 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009620 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009621 }
9622}
9623
9624static void simplify_neg(struct compile_state *state, struct triple *ins)
9625{
Eric Biederman90089602004-05-28 14:11:54 +00009626 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009627 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009628 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009629 mkconst(state, ins, -left);
9630 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009631 else if (RHS(ins, 0)->op == OP_NEG) {
9632 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009633 }
9634}
9635
9636static void simplify_invert(struct compile_state *state, struct triple *ins)
9637{
Eric Biederman90089602004-05-28 14:11:54 +00009638 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009639 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009640 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009641 mkconst(state, ins, ~left);
9642 }
9643}
9644
9645static void simplify_eq(struct compile_state *state, struct triple *ins)
9646{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009647 struct triple *left, *right;
9648 left = RHS(ins, 0);
9649 right = RHS(ins, 1);
9650
9651 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009652 int val;
9653 val = const_eq(state, ins, left, right);
9654 if (val >= 0) {
9655 mkconst(state, ins, val == 1);
9656 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009657 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009658 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009659 mkconst(state, ins, 1);
9660 }
9661}
9662
9663static void simplify_noteq(struct compile_state *state, struct triple *ins)
9664{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009665 struct triple *left, *right;
9666 left = RHS(ins, 0);
9667 right = RHS(ins, 1);
9668
9669 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009670 int val;
9671 val = const_eq(state, ins, left, right);
9672 if (val >= 0) {
9673 mkconst(state, ins, val != 1);
9674 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009675 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009676 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009677 mkconst(state, ins, 0);
9678 }
9679}
9680
9681static void simplify_sless(struct compile_state *state, struct triple *ins)
9682{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009683 struct triple *left, *right;
9684 left = RHS(ins, 0);
9685 right = RHS(ins, 1);
9686
9687 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009688 int val;
9689 val = const_scmp(state, ins, left, right);
9690 if ((val >= -1) && (val <= 1)) {
9691 mkconst(state, ins, val < 0);
9692 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009693 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009694 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009695 mkconst(state, ins, 0);
9696 }
9697}
9698
9699static void simplify_uless(struct compile_state *state, struct triple *ins)
9700{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009701 struct triple *left, *right;
9702 left = RHS(ins, 0);
9703 right = RHS(ins, 1);
9704
9705 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009706 int val;
9707 val = const_ucmp(state, ins, left, right);
9708 if ((val >= -1) && (val <= 1)) {
9709 mkconst(state, ins, val < 0);
9710 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009711 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009712 else if (is_zero(right)) {
9713 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009714 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009715 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009716 mkconst(state, ins, 0);
9717 }
9718}
9719
9720static void simplify_smore(struct compile_state *state, struct triple *ins)
9721{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009722 struct triple *left, *right;
9723 left = RHS(ins, 0);
9724 right = RHS(ins, 1);
9725
9726 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009727 int val;
9728 val = const_scmp(state, ins, left, right);
9729 if ((val >= -1) && (val <= 1)) {
9730 mkconst(state, ins, val > 0);
9731 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009732 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009733 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009734 mkconst(state, ins, 0);
9735 }
9736}
9737
9738static void simplify_umore(struct compile_state *state, struct triple *ins)
9739{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009740 struct triple *left, *right;
9741 left = RHS(ins, 0);
9742 right = RHS(ins, 1);
9743
9744 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009745 int val;
9746 val = const_ucmp(state, ins, left, right);
9747 if ((val >= -1) && (val <= 1)) {
9748 mkconst(state, ins, val > 0);
9749 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009750 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009751 else if (is_zero(left)) {
9752 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009753 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009754 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009755 mkconst(state, ins, 0);
9756 }
9757}
9758
9759
9760static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9761{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009762 struct triple *left, *right;
9763 left = RHS(ins, 0);
9764 right = RHS(ins, 1);
9765
9766 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009767 int val;
9768 val = const_scmp(state, ins, left, right);
9769 if ((val >= -1) && (val <= 1)) {
9770 mkconst(state, ins, val <= 0);
9771 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009772 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009773 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009774 mkconst(state, ins, 1);
9775 }
9776}
9777
9778static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9779{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009780 struct triple *left, *right;
9781 left = RHS(ins, 0);
9782 right = RHS(ins, 1);
9783
9784 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009785 int val;
9786 val = const_ucmp(state, ins, left, right);
9787 if ((val >= -1) && (val <= 1)) {
9788 mkconst(state, ins, val <= 0);
9789 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009790 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009791 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009792 mkconst(state, ins, 1);
9793 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009794 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009795 mkconst(state, ins, 1);
9796 }
9797}
9798
9799static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9800{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009801 struct triple *left, *right;
9802 left = RHS(ins, 0);
9803 right = RHS(ins, 1);
9804
9805 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009806 int val;
9807 val = const_scmp(state, ins, left, right);
9808 if ((val >= -1) && (val <= 1)) {
9809 mkconst(state, ins, val >= 0);
9810 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009811 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009812 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009813 mkconst(state, ins, 1);
9814 }
9815}
9816
9817static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9818{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009819 struct triple *left, *right;
9820 left = RHS(ins, 0);
9821 right = RHS(ins, 1);
9822
9823 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009824 int val;
9825 val = const_ucmp(state, ins, left, right);
9826 if ((val >= -1) && (val <= 1)) {
9827 mkconst(state, ins, val >= 0);
9828 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009829 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009830 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009831 mkconst(state, ins, 1);
9832 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009833 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009834 mkconst(state, ins, 1);
9835 }
9836}
9837
9838static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9839{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009840 struct triple *rhs;
9841 rhs = RHS(ins, 0);
9842
9843 if (is_const(rhs)) {
9844 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009845 }
9846 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009847 else if ((rhs->use) &&
9848 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009849 int need_copy = 1;
9850 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009851 switch(rhs->op) {
9852 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9853 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9854 case OP_EQ: rhs->op = OP_NOTEQ; break;
9855 case OP_NOTEQ: rhs->op = OP_EQ; break;
9856 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9857 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9858 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9859 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9860 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9861 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9862 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9863 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009864 default:
9865 need_copy = 0;
9866 break;
9867 }
9868 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009869 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009870 }
9871 }
9872}
9873
9874static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9875{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009876 struct triple *rhs;
9877 rhs = RHS(ins, 0);
9878
9879 if (is_const(rhs)) {
9880 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009881 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009882 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009883 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9884 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9885 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009886 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009887 }
9888
9889}
9890
Eric Biederman90089602004-05-28 14:11:54 +00009891static void simplify_load(struct compile_state *state, struct triple *ins)
9892{
9893 struct triple *addr, *sdecl, *blob;
9894
9895 /* If I am doing a load with a constant pointer from a constant
9896 * table get the value.
9897 */
9898 addr = RHS(ins, 0);
9899 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9900 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9901 (blob->op == OP_BLOBCONST)) {
9902 unsigned char buffer[SIZEOF_WORD];
9903 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009904 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009905 ulong_t val;
9906 reg_size = reg_size_of(state, ins->type);
9907 if (reg_size > REG_SIZEOF_REG) {
9908 internal_error(state, ins, "load size greater than register");
9909 }
9910 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009911 end = blob->u.blob;
9912 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009913 src = blob->u.blob;
9914 src += addr->u.cval;
9915
Eric Biederman7dea9552004-06-29 05:38:37 +00009916 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009917 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009918 }
9919
Eric Biederman90089602004-05-28 14:11:54 +00009920 memset(buffer, 0, sizeof(buffer));
9921 memcpy(buffer, src, bits_to_bytes(mem_size));
9922
9923 switch(mem_size) {
9924 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9925 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9926 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9927 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9928 default:
9929 internal_error(state, ins, "mem_size: %d not handled",
9930 mem_size);
9931 val = 0;
9932 break;
9933 }
9934 mkconst(state, ins, val);
9935 }
9936}
9937
9938static void simplify_uextract(struct compile_state *state, struct triple *ins)
9939{
9940 if (is_simple_const(RHS(ins, 0))) {
9941 ulong_t val;
9942 ulong_t mask;
9943 val = read_const(state, ins, RHS(ins, 0));
9944 mask = 1;
9945 mask <<= ins->u.bitfield.size;
9946 mask -= 1;
9947 val >>= ins->u.bitfield.offset;
9948 val &= mask;
9949 mkconst(state, ins, val);
9950 }
9951}
9952
9953static void simplify_sextract(struct compile_state *state, struct triple *ins)
9954{
9955 if (is_simple_const(RHS(ins, 0))) {
9956 ulong_t val;
9957 ulong_t mask;
9958 long_t sval;
9959 val = read_const(state, ins, RHS(ins, 0));
9960 mask = 1;
9961 mask <<= ins->u.bitfield.size;
9962 mask -= 1;
9963 val >>= ins->u.bitfield.offset;
9964 val &= mask;
9965 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9966 sval = val;
Stefan Reinauer14e22772010-04-27 06:56:47 +00009967 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
Eric Biederman90089602004-05-28 14:11:54 +00009968 mkconst(state, ins, sval);
9969 }
9970}
9971
9972static void simplify_deposit(struct compile_state *state, struct triple *ins)
9973{
9974 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9975 ulong_t targ, val;
9976 ulong_t mask;
9977 targ = read_const(state, ins, RHS(ins, 0));
9978 val = read_const(state, ins, RHS(ins, 1));
9979 mask = 1;
9980 mask <<= ins->u.bitfield.size;
9981 mask -= 1;
9982 mask <<= ins->u.bitfield.offset;
9983 targ &= ~mask;
9984 val <<= ins->u.bitfield.offset;
9985 val &= mask;
9986 targ |= val;
9987 mkconst(state, ins, targ);
9988 }
9989}
9990
Eric Biedermanb138ac82003-04-22 18:44:01 +00009991static void simplify_copy(struct compile_state *state, struct triple *ins)
9992{
Eric Biederman90089602004-05-28 14:11:54 +00009993 struct triple *right;
9994 right = RHS(ins, 0);
9995 if (is_subset_type(ins->type, right->type)) {
9996 ins->type = right->type;
9997 }
9998 if (equiv_types(ins->type, right->type)) {
9999 ins->op = OP_COPY;/* I don't need to convert if the types match */
10000 } else {
10001 if (ins->op == OP_COPY) {
10002 internal_error(state, ins, "type mismatch on copy");
10003 }
10004 }
10005 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10006 struct triple *sdecl;
10007 ulong_t offset;
10008 sdecl = MISC(right, 0);
10009 offset = right->u.cval;
10010 mkaddr_const(state, ins, sdecl, offset);
10011 }
10012 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10013 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010014 case OP_INTCONST:
10015 {
10016 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010017 left = read_const(state, ins, right);
10018 /* Ensure I have not overflowed the destination. */
10019 if (size_of(state, right->type) > size_of(state, ins->type)) {
10020 ulong_t mask;
10021 mask = 1;
10022 mask <<= size_of(state, ins->type);
10023 mask -= 1;
10024 left &= mask;
10025 }
10026 /* Ensure I am properly sign extended */
10027 if (size_of(state, right->type) < size_of(state, ins->type) &&
10028 is_signed(right->type)) {
10029 long_t val;
10030 int shift;
10031 shift = SIZEOF_LONG - size_of(state, right->type);
10032 val = left;
10033 val <<= shift;
10034 val >>= shift;
10035 left = val;
10036 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010037 mkconst(state, ins, left);
10038 break;
10039 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010040 default:
10041 internal_error(state, ins, "uknown constant");
10042 break;
10043 }
10044 }
10045}
10046
Eric Biederman83b991a2003-10-11 06:20:25 +000010047static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010048{
10049 struct triple *ptr;
10050 if (!block) {
10051 return 0;
10052 }
10053 ptr = block->first;
10054 do {
10055 if (ptr->op == OP_PHI) {
10056 return 1;
10057 }
10058 ptr = ptr->next;
10059 } while(ptr != block->last);
10060 return 0;
10061}
10062
Eric Biederman83b991a2003-10-11 06:20:25 +000010063static int phi_dependency(struct block *block)
10064{
10065 /* A block has a phi dependency if a phi function
10066 * depends on that block to exist, and makes a block
10067 * that is otherwise useless unsafe to remove.
10068 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010069 if (block) {
10070 struct block_set *edge;
10071 for(edge = block->edges; edge; edge = edge->next) {
10072 if (phi_present(edge->member)) {
10073 return 1;
10074 }
10075 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010076 }
10077 return 0;
10078}
10079
10080static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10081{
10082 struct triple *targ;
10083 targ = TARG(ins, 0);
10084 /* During scc_transform temporary triples are allocated that
10085 * loop back onto themselves. If I see one don't advance the
10086 * target.
10087 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000010088 while(triple_is_structural(state, targ) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010089 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010090 targ = targ->next;
10091 }
10092 return targ;
10093}
10094
10095
10096static void simplify_branch(struct compile_state *state, struct triple *ins)
10097{
Eric Biederman90089602004-05-28 14:11:54 +000010098 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010099 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010100 internal_error(state, ins, "not branch");
10101 }
10102 if (ins->use != 0) {
10103 internal_error(state, ins, "branch use");
10104 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010105 /* The challenge here with simplify branch is that I need to
Eric Biederman83b991a2003-10-11 06:20:25 +000010106 * make modifications to the control flow graph as well
10107 * as to the branch instruction itself. That is handled
10108 * by rebuilding the basic blocks after simplify all is called.
10109 */
10110
10111 /* If we have a branch to an unconditional branch update
10112 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010113 * functions.
10114 * Also only do this a limited number of times so
10115 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010116 */
Eric Biederman90089602004-05-28 14:11:54 +000010117 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010118 do {
10119 struct triple *targ;
10120 simplified = 0;
10121 targ = branch_target(state, ins);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010122 if ((targ != ins) && (targ->op == OP_BRANCH) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000010123 !phi_dependency(targ->u.block))
10124 {
10125 unuse_triple(TARG(ins, 0), ins);
10126 TARG(ins, 0) = TARG(targ, 0);
10127 use_triple(TARG(ins, 0), ins);
10128 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010129 }
Eric Biederman90089602004-05-28 14:11:54 +000010130 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010131
10132 /* If we have a conditional branch with a constant condition
10133 * make it an unconditional branch.
10134 */
Eric Biederman90089602004-05-28 14:11:54 +000010135 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010136 struct triple *targ;
10137 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010138 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010139 unuse_triple(RHS(ins, 0), ins);
10140 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010141 ins->rhs = 0;
10142 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010143 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010144 if (value) {
10145 unuse_triple(ins->next, ins);
10146 TARG(ins, 0) = targ;
10147 }
10148 else {
10149 unuse_triple(targ, ins);
10150 TARG(ins, 0) = ins->next;
10151 }
10152 }
Eric Biederman90089602004-05-28 14:11:54 +000010153
10154 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010155 * make it a noop.
10156 */
10157 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010158 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010159 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010160 unuse_triple(RHS(ins, 0), ins);
10161 unuse_triple(ins->next, ins);
10162 }
Eric Biederman90089602004-05-28 14:11:54 +000010163 ins->lhs = 0;
10164 ins->rhs = 0;
10165 ins->misc = 0;
10166 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010167 ins->op = OP_NOOP;
10168 if (ins->use) {
10169 internal_error(state, ins, "noop use != 0");
10170 }
10171 }
10172}
10173
Eric Biederman530b5192003-07-01 10:05:30 +000010174static void simplify_label(struct compile_state *state, struct triple *ins)
10175{
Eric Biederman83b991a2003-10-11 06:20:25 +000010176 /* Ignore volatile labels */
10177 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010178 return;
10179 }
10180 if (ins->use == 0) {
10181 ins->op = OP_NOOP;
10182 }
10183 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010184 /* In general it is not safe to merge one label that
10185 * imediately follows another. The problem is that the empty
10186 * looking block may have phi functions that depend on it.
10187 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010188 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010189 struct triple_set *user, *next;
10190 ins->op = OP_NOOP;
10191 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010192 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010193 next = user->next;
10194 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010195 expr = triple_targ(state, use, 0);
10196 for(;expr; expr = triple_targ(state, use, expr)) {
10197 if (*expr == ins) {
10198 *expr = ins->prev;
10199 unuse_triple(ins, use);
10200 use_triple(ins->prev, use);
10201 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010202
Eric Biederman530b5192003-07-01 10:05:30 +000010203 }
10204 }
10205 if (ins->use) {
10206 internal_error(state, ins, "noop use != 0");
10207 }
10208 }
10209 }
10210}
10211
Eric Biedermanb138ac82003-04-22 18:44:01 +000010212static void simplify_phi(struct compile_state *state, struct triple *ins)
10213{
Eric Biederman83b991a2003-10-11 06:20:25 +000010214 struct triple **slot;
10215 struct triple *value;
10216 int zrhs, i;
10217 ulong_t cvalue;
10218 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010219 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010220 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010221 return;
10222 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010223 /* See if all of the rhs members of a phi have the same value */
10224 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010225 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010226 for(i = 1; i < zrhs; i++) {
10227 if ( !slot[i] ||
10228 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010229 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010230 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010231 break;
10232 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010233 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010234 if (i == zrhs) {
10235 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010236 return;
10237 }
10238 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010239
Eric Biederman83b991a2003-10-11 06:20:25 +000010240 /* See if all of rhs members of a phi are the same */
10241 value = slot[0];
10242 for(i = 1; i < zrhs; i++) {
10243 if (slot[i] != value) {
10244 break;
10245 }
10246 }
10247 if (i == zrhs) {
10248 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010249 if (!is_subset_type(ins->type, value->type)) {
10250 internal_error(state, ins, "bad input type to phi");
10251 }
10252 /* Make the types match */
10253 if (!equiv_types(ins->type, value->type)) {
10254 ins->type = value->type;
10255 }
10256 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010257 mkcopy(state, ins, value);
10258 return;
10259 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010260}
10261
10262
10263static void simplify_bsf(struct compile_state *state, struct triple *ins)
10264{
Eric Biederman90089602004-05-28 14:11:54 +000010265 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010266 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010267 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010268 mkconst(state, ins, bsf(left));
10269 }
10270}
10271
10272static void simplify_bsr(struct compile_state *state, struct triple *ins)
10273{
Eric Biederman90089602004-05-28 14:11:54 +000010274 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010275 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010276 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010277 mkconst(state, ins, bsr(left));
10278 }
10279}
10280
10281
10282typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010283static const struct simplify_table {
10284 simplify_t func;
10285 unsigned long flag;
10286} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010287#define simplify_sdivt simplify_noop
10288#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010289#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010290
Eric Biederman5ade04a2003-10-22 04:03:46 +000010291[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10292[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10293[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10294[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10295[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10296[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10297[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10298[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10299[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10300[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10301[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10302[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10303[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10304[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10305[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10306[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10307[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10308[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10309[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010310
Eric Biederman5ade04a2003-10-22 04:03:46 +000010311[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10312[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10313[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10314[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10315[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10316[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10317[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10318[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10319[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10320[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10321[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10322[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010323
Eric Biederman90089602004-05-28 14:11:54 +000010324[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010325[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010326
Eric Biederman90089602004-05-28 14:11:54 +000010327[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10328[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10329[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10330
Eric Biederman5ade04a2003-10-22 04:03:46 +000010331[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010332
Eric Biederman5ade04a2003-10-22 04:03:46 +000010333[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10334[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10335[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010336[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010337
Eric Biederman5ade04a2003-10-22 04:03:46 +000010338[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10339[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10340[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010341[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010342[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10343[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010344
Eric Biederman5ade04a2003-10-22 04:03:46 +000010345[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010346[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010347
Eric Biederman5ade04a2003-10-22 04:03:46 +000010348[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10349[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10350[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10351[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10352[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10353[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10354[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10355[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10356[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010357
Eric Biederman5ade04a2003-10-22 04:03:46 +000010358[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10359[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10360[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10361[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10362[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10363[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10364[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10365[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10366[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Stefan Reinauer14e22772010-04-27 06:56:47 +000010367[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010368[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010369};
10370
Stefan Reinauer14e22772010-04-27 06:56:47 +000010371static inline void debug_simplify(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000010372 simplify_t do_simplify, struct triple *ins)
10373{
10374#if DEBUG_SIMPLIFY_HIRES
10375 if (state->functions_joined && (do_simplify != simplify_noop)) {
10376 /* High resolution debugging mode */
10377 fprintf(state->dbgout, "simplifing: ");
10378 display_triple(state->dbgout, ins);
10379 }
10380#endif
10381 do_simplify(state, ins);
10382#if DEBUG_SIMPLIFY_HIRES
10383 if (state->functions_joined && (do_simplify != simplify_noop)) {
10384 /* High resolution debugging mode */
10385 fprintf(state->dbgout, "simplified: ");
10386 display_triple(state->dbgout, ins);
10387 }
10388#endif
10389}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010390static void simplify(struct compile_state *state, struct triple *ins)
10391{
10392 int op;
10393 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010394 if (ins == &unknown_triple) {
10395 internal_error(state, ins, "simplifying the unknown triple?");
10396 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010397 do {
10398 op = ins->op;
10399 do_simplify = 0;
10400 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10401 do_simplify = 0;
10402 }
Eric Biederman90089602004-05-28 14:11:54 +000010403 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010404 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010405 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010406 if (do_simplify &&
Eric Biederman90089602004-05-28 14:11:54 +000010407 !(state->compiler->flags & table_simplify[op].flag)) {
10408 do_simplify = simplify_noop;
10409 }
10410 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010411 do_simplify = simplify_noop;
10412 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000010413
Eric Biedermanb138ac82003-04-22 18:44:01 +000010414 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010415 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010416 op, tops(op));
10417 return;
10418 }
Eric Biederman90089602004-05-28 14:11:54 +000010419 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010420 } while(ins->op != op);
10421}
10422
Eric Biederman5ade04a2003-10-22 04:03:46 +000010423static void rebuild_ssa_form(struct compile_state *state);
10424
Eric Biedermanb138ac82003-04-22 18:44:01 +000010425static void simplify_all(struct compile_state *state)
10426{
10427 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010428 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10429 return;
10430 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010431 first = state->first;
10432 ins = first->prev;
10433 do {
10434 simplify(state, ins);
10435 ins = ins->prev;
10436 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010437 ins = first;
10438 do {
10439 simplify(state, ins);
10440 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010441 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010442 rebuild_ssa_form(state);
10443
Eric Biederman90089602004-05-28 14:11:54 +000010444 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010445}
10446
10447/*
10448 * Builtins....
10449 * ============================
10450 */
10451
Eric Biederman0babc1c2003-05-09 02:39:00 +000010452static void register_builtin_function(struct compile_state *state,
10453 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010454{
Eric Biederman90089602004-05-28 14:11:54 +000010455 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010456 struct triple *def, *result, *work, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010457 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010458 struct file_state file;
10459 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010460 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010461 va_list args;
10462 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010463
10464 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010465 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010466 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010467 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010468 file.report_line = 1;
10469 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010470 file.prev = state->file;
10471 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010472 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010473
10474 /* Find the Parameter count */
10475 valid_op(state, op);
10476 parameters = table_ops[op].rhs;
10477 if (parameters < 0 ) {
10478 internal_error(state, 0, "Invalid builtin parameter count");
10479 }
10480
10481 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010482 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010483 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010484 next = &ftype->right;
10485 va_start(args, rtype);
10486 for(i = 0; i < parameters; i++) {
10487 atype = va_arg(args, struct type *);
10488 if (!*next) {
10489 *next = atype;
10490 } else {
10491 *next = new_type(TYPE_PRODUCT, *next, atype);
10492 next = &((*next)->right);
10493 }
10494 }
10495 if (!*next) {
10496 *next = &void_type;
10497 }
10498 va_end(args);
10499
Eric Biederman90089602004-05-28 14:11:54 +000010500 /* Get the initial closure type */
10501 ctype = new_type(TYPE_JOIN, &void_type, 0);
10502 ctype->elements = 1;
10503
10504 /* Get the return type */
10505 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10506 crtype->elements = 2;
10507
Eric Biedermanb138ac82003-04-22 18:44:01 +000010508 /* Generate the needed triples */
10509 def = triple(state, OP_LIST, ftype, 0, 0);
10510 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010511 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010512 result = flatten(state, first, variable(state, crtype));
10513 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010514 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010515
10516 /* Now string them together */
10517 param = ftype->right;
10518 for(i = 0; i < parameters; i++) {
10519 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10520 atype = param->left;
10521 } else {
10522 atype = param;
10523 }
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010524 flatten(state, first, variable(state, atype));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010525 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010526 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010527 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010528 generate_lhs_pieces(state, work);
10529 for(i = 0; i < parameters; i++) {
10530 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010531 }
Eric Biederman90089602004-05-28 14:11:54 +000010532 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10533 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010534 }
10535 work = flatten(state, first, work);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010010536 flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010537 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010538 name_len = strlen(name);
10539 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010540 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010541 symbol(state, ident, &ident->sym_ident, def, ftype);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010542
Eric Biedermanb138ac82003-04-22 18:44:01 +000010543 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010544 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010545 state->main_function = 0;
10546
Eric Biederman5ade04a2003-10-22 04:03:46 +000010547 if (!state->functions) {
10548 state->functions = def;
10549 } else {
10550 insert_triple(state, state->functions, def);
10551 }
10552 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010553 FILE *fp = state->dbgout;
10554 fprintf(fp, "\n");
10555 loc(fp, state, 0);
10556 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10557 display_func(state, fp, def);
10558 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010559 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010560}
10561
Eric Biederman0babc1c2003-05-09 02:39:00 +000010562static struct type *partial_struct(struct compile_state *state,
10563 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010564{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010565 struct hash_entry *field_ident;
10566 struct type *result;
10567 int field_name_len;
10568
10569 field_name_len = strlen(field_name);
10570 field_ident = lookup(state, field_name, field_name_len);
10571
10572 result = clone_type(0, type);
10573 result->field_ident = field_ident;
10574
10575 if (rest) {
10576 result = new_type(TYPE_PRODUCT, result, rest);
10577 }
10578 return result;
10579}
10580
10581static struct type *register_builtin_type(struct compile_state *state,
10582 const char *name, struct type *type)
10583{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010584 struct hash_entry *ident;
10585 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010586
Eric Biedermanb138ac82003-04-22 18:44:01 +000010587 name_len = strlen(name);
10588 ident = lookup(state, name, name_len);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010589
Eric Biederman0babc1c2003-05-09 02:39:00 +000010590 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10591 ulong_t elements = 0;
10592 struct type *field;
10593 type = new_type(TYPE_STRUCT, type, 0);
10594 field = type->left;
10595 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10596 elements++;
10597 field = field->right;
10598 }
10599 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010600 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010601 type->type_ident = ident;
10602 type->elements = elements;
10603 }
10604 symbol(state, ident, &ident->sym_ident, 0, type);
10605 ident->tok = TOK_TYPE_NAME;
10606 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010607}
10608
Eric Biederman0babc1c2003-05-09 02:39:00 +000010609
Eric Biedermanb138ac82003-04-22 18:44:01 +000010610static void register_builtins(struct compile_state *state)
10611{
Eric Biederman530b5192003-07-01 10:05:30 +000010612 struct type *div_type, *ldiv_type;
10613 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010614 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010615
Eric Biederman530b5192003-07-01 10:05:30 +000010616 div_type = register_builtin_type(state, "__builtin_div_t",
10617 partial_struct(state, "quot", &int_type,
10618 partial_struct(state, "rem", &int_type, 0)));
10619 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10620 partial_struct(state, "quot", &long_type,
10621 partial_struct(state, "rem", &long_type, 0)));
10622 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10623 partial_struct(state, "quot", &uint_type,
10624 partial_struct(state, "rem", &uint_type, 0)));
10625 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10626 partial_struct(state, "quot", &ulong_type,
10627 partial_struct(state, "rem", &ulong_type, 0)));
10628
10629 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10630 &int_type, &int_type);
10631 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10632 &long_type, &long_type);
10633 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10634 &uint_type, &uint_type);
10635 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10636 &ulong_type, &ulong_type);
10637
Stefan Reinauer14e22772010-04-27 06:56:47 +000010638 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010639 &ushort_type);
10640 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10641 &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010642 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010643 &ushort_type);
10644
Stefan Reinauer14e22772010-04-27 06:56:47 +000010645 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010646 &uchar_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010647 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010648 &ushort_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010649 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010650 &uint_type, &ushort_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010651
10652 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010653 &int_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010654 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010655 &int_type);
10656
10657 msr_type = register_builtin_type(state, "__builtin_msr_t",
10658 partial_struct(state, "lo", &ulong_type,
10659 partial_struct(state, "hi", &ulong_type, 0)));
10660
10661 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10662 &ulong_type);
10663 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10664 &ulong_type, &ulong_type, &ulong_type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000010665
10666 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +000010667 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010668}
10669
10670static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000010671 struct compile_state *state, struct type *type,
Eric Biedermanb138ac82003-04-22 18:44:01 +000010672 struct hash_entry **ident, int need_ident);
10673static void decl(struct compile_state *state, struct triple *first);
10674static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010675#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010676static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010677#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010678static struct type *decl_specifiers(struct compile_state *state);
10679static int istype(int tok);
10680static struct triple *expr(struct compile_state *state);
10681static struct triple *assignment_expr(struct compile_state *state);
10682static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010683static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010684
10685static struct triple *call_expr(
10686 struct compile_state *state, struct triple *func)
10687{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010688 struct triple *def;
10689 struct type *param, *type;
10690 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010691
10692 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10693 error(state, 0, "Called object is not a function");
10694 }
10695 if (func->op != OP_LIST) {
10696 internal_error(state, 0, "improper function");
10697 }
10698 eat(state, TOK_LPAREN);
10699 /* Find the return type without any specifiers */
10700 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010701 /* Count the number of rhs entries for OP_FCALL */
10702 param = func->type->right;
10703 pvals = 0;
10704 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10705 pvals++;
10706 param = param->right;
10707 }
10708 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10709 pvals++;
10710 }
10711 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010712 MISC(def, 0) = func;
10713
10714 param = func->type->right;
10715 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010716 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010717 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010718 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010719 arg_type = param;
10720 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10721 arg_type = param->left;
10722 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010723 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010724 RHS(def, index) = val;
10725 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010726 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010727 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010728 }
10729 }
10730 eat(state, TOK_RPAREN);
10731 return def;
10732}
10733
10734
10735static struct triple *character_constant(struct compile_state *state)
10736{
10737 struct triple *def;
10738 struct token *tk;
10739 const signed char *str, *end;
10740 int c;
10741 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010742 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010743 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010744 str_len = tk->str_len - 2;
10745 if (str_len <= 0) {
10746 error(state, 0, "empty character constant");
10747 }
10748 end = str + str_len;
10749 c = char_value(state, &str, end);
10750 if (str != end) {
10751 error(state, 0, "multibyte character constant not supported");
10752 }
10753 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10754 return def;
10755}
10756
10757static struct triple *string_constant(struct compile_state *state)
10758{
10759 struct triple *def;
10760 struct token *tk;
10761 struct type *type;
10762 const signed char *str, *end;
10763 signed char *buf, *ptr;
10764 int str_len;
10765
10766 buf = 0;
10767 type = new_type(TYPE_ARRAY, &char_type, 0);
10768 type->elements = 0;
10769 /* The while loop handles string concatenation */
10770 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010771 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010772 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010773 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010774 if (str_len < 0) {
10775 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010776 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010777 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010778 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010779 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010780 end = str + str_len;
10781 ptr = buf;
10782 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10783 memcpy(buf, ptr, type->elements);
10784 ptr = buf + type->elements;
10785 do {
10786 *ptr++ = char_value(state, &str, end);
10787 } while(str < end);
10788 type->elements = ptr - buf;
10789 } while(peek(state) == TOK_LIT_STRING);
10790 *ptr = '\0';
10791 type->elements += 1;
10792 def = triple(state, OP_BLOBCONST, type, 0, 0);
10793 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010794
Eric Biedermanb138ac82003-04-22 18:44:01 +000010795 return def;
10796}
10797
10798
10799static struct triple *integer_constant(struct compile_state *state)
10800{
10801 struct triple *def;
10802 unsigned long val;
10803 struct token *tk;
10804 char *end;
10805 int u, l, decimal;
10806 struct type *type;
10807
Eric Biederman41203d92004-11-08 09:31:09 +000010808 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010809 errno = 0;
10810 decimal = (tk->val.str[0] != '0');
10811 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010812 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010813 error(state, 0, "Integer constant to large");
10814 }
10815 u = l = 0;
10816 if ((*end == 'u') || (*end == 'U')) {
10817 u = 1;
10818 end++;
10819 }
10820 if ((*end == 'l') || (*end == 'L')) {
10821 l = 1;
10822 end++;
10823 }
10824 if ((*end == 'u') || (*end == 'U')) {
10825 u = 1;
10826 end++;
10827 }
10828 if (*end) {
10829 error(state, 0, "Junk at end of integer constant");
10830 }
10831 if (u && l) {
10832 type = &ulong_type;
10833 }
10834 else if (l) {
10835 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010836 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010837 type = &ulong_type;
10838 }
10839 }
10840 else if (u) {
10841 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010842 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010843 type = &ulong_type;
10844 }
10845 }
10846 else {
10847 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010848 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010849 type = &uint_type;
10850 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010851 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010852 type = &ulong_type;
10853 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010854 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010855 type = &long_type;
10856 }
10857 }
10858 def = int_const(state, type, val);
10859 return def;
10860}
10861
10862static struct triple *primary_expr(struct compile_state *state)
10863{
10864 struct triple *def;
10865 int tok;
10866 tok = peek(state);
10867 switch(tok) {
10868 case TOK_IDENT:
10869 {
10870 struct hash_entry *ident;
10871 /* Here ident is either:
10872 * a varable name
10873 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010874 */
Eric Biederman41203d92004-11-08 09:31:09 +000010875 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010876 if (!ident->sym_ident) {
10877 error(state, 0, "%s undeclared", ident->name);
10878 }
10879 def = ident->sym_ident->def;
10880 break;
10881 }
10882 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010883 {
10884 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010885 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010886 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010887 if (!ident->sym_ident) {
10888 error(state, 0, "%s undeclared", ident->name);
10889 }
10890 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010891 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010892 }
Eric Biederman41203d92004-11-08 09:31:09 +000010893 case TOK_MIDENT:
10894 {
10895 struct hash_entry *ident;
10896 ident = eat(state, TOK_MIDENT)->ident;
10897 warning(state, 0, "Replacing undefined macro: %s with 0",
10898 ident->name);
10899 def = int_const(state, &int_type, 0);
10900 break;
10901 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010902 case TOK_LPAREN:
10903 eat(state, TOK_LPAREN);
10904 def = expr(state);
10905 eat(state, TOK_RPAREN);
10906 break;
10907 case TOK_LIT_INT:
10908 def = integer_constant(state);
10909 break;
10910 case TOK_LIT_FLOAT:
10911 eat(state, TOK_LIT_FLOAT);
10912 error(state, 0, "Floating point constants not supported");
10913 def = 0;
10914 FINISHME();
10915 break;
10916 case TOK_LIT_CHAR:
10917 def = character_constant(state);
10918 break;
10919 case TOK_LIT_STRING:
10920 def = string_constant(state);
10921 break;
10922 default:
10923 def = 0;
10924 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10925 }
10926 return def;
10927}
10928
10929static struct triple *postfix_expr(struct compile_state *state)
10930{
10931 struct triple *def;
10932 int postfix;
10933 def = primary_expr(state);
10934 do {
10935 struct triple *left;
10936 int tok;
10937 postfix = 1;
10938 left = def;
10939 switch((tok = peek(state))) {
10940 case TOK_LBRACKET:
10941 eat(state, TOK_LBRACKET);
10942 def = mk_subscript_expr(state, left, expr(state));
10943 eat(state, TOK_RBRACKET);
10944 break;
10945 case TOK_LPAREN:
10946 def = call_expr(state, def);
10947 break;
10948 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010949 {
10950 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010951 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010952 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010953 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010954 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010955 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010956 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010957 {
10958 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010959 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010960 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010961 def = mk_deref_expr(state, read_expr(state, def));
10962 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010963 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010964 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010965 case TOK_PLUSPLUS:
10966 eat(state, TOK_PLUSPLUS);
10967 def = mk_post_inc_expr(state, left);
10968 break;
10969 case TOK_MINUSMINUS:
10970 eat(state, TOK_MINUSMINUS);
10971 def = mk_post_dec_expr(state, left);
10972 break;
10973 default:
10974 postfix = 0;
10975 break;
10976 }
10977 } while(postfix);
10978 return def;
10979}
10980
10981static struct triple *cast_expr(struct compile_state *state);
10982
10983static struct triple *unary_expr(struct compile_state *state)
10984{
10985 struct triple *def, *right;
10986 int tok;
10987 switch((tok = peek(state))) {
10988 case TOK_PLUSPLUS:
10989 eat(state, TOK_PLUSPLUS);
10990 def = mk_pre_inc_expr(state, unary_expr(state));
10991 break;
10992 case TOK_MINUSMINUS:
10993 eat(state, TOK_MINUSMINUS);
10994 def = mk_pre_dec_expr(state, unary_expr(state));
10995 break;
10996 case TOK_AND:
10997 eat(state, TOK_AND);
10998 def = mk_addr_expr(state, cast_expr(state), 0);
10999 break;
11000 case TOK_STAR:
11001 eat(state, TOK_STAR);
11002 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11003 break;
11004 case TOK_PLUS:
11005 eat(state, TOK_PLUS);
11006 right = read_expr(state, cast_expr(state));
11007 arithmetic(state, right);
11008 def = integral_promotion(state, right);
11009 break;
11010 case TOK_MINUS:
11011 eat(state, TOK_MINUS);
11012 right = read_expr(state, cast_expr(state));
11013 arithmetic(state, right);
11014 def = integral_promotion(state, right);
11015 def = triple(state, OP_NEG, def->type, def, 0);
11016 break;
11017 case TOK_TILDE:
11018 eat(state, TOK_TILDE);
11019 right = read_expr(state, cast_expr(state));
11020 integral(state, right);
11021 def = integral_promotion(state, right);
11022 def = triple(state, OP_INVERT, def->type, def, 0);
11023 break;
11024 case TOK_BANG:
11025 eat(state, TOK_BANG);
11026 right = read_expr(state, cast_expr(state));
11027 bool(state, right);
11028 def = lfalse_expr(state, right);
11029 break;
11030 case TOK_SIZEOF:
11031 {
11032 struct type *type;
11033 int tok1, tok2;
11034 eat(state, TOK_SIZEOF);
11035 tok1 = peek(state);
11036 tok2 = peek2(state);
11037 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11038 eat(state, TOK_LPAREN);
11039 type = type_name(state);
11040 eat(state, TOK_RPAREN);
11041 }
11042 else {
11043 struct triple *expr;
11044 expr = unary_expr(state);
11045 type = expr->type;
11046 release_expr(state, expr);
11047 }
Eric Biederman90089602004-05-28 14:11:54 +000011048 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011049 break;
11050 }
11051 case TOK_ALIGNOF:
11052 {
11053 struct type *type;
11054 int tok1, tok2;
11055 eat(state, TOK_ALIGNOF);
11056 tok1 = peek(state);
11057 tok2 = peek2(state);
11058 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11059 eat(state, TOK_LPAREN);
11060 type = type_name(state);
11061 eat(state, TOK_RPAREN);
11062 }
11063 else {
11064 struct triple *expr;
11065 expr = unary_expr(state);
11066 type = expr->type;
11067 release_expr(state, expr);
11068 }
Eric Biederman90089602004-05-28 14:11:54 +000011069 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011070 break;
11071 }
Eric Biederman41203d92004-11-08 09:31:09 +000011072 case TOK_MDEFINED:
11073 {
11074 /* We only come here if we are called from the preprocessor */
11075 struct hash_entry *ident;
11076 int parens;
11077 eat(state, TOK_MDEFINED);
11078 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011079 if (pp_peek(state) == TOK_LPAREN) {
11080 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011081 parens = 1;
11082 }
Eric Biedermancb364952004-11-15 10:46:44 +000011083 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011084 if (parens) {
11085 eat(state, TOK_RPAREN);
11086 }
11087 def = int_const(state, &int_type, ident->sym_define != 0);
11088 break;
11089 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011090 default:
11091 def = postfix_expr(state);
11092 break;
11093 }
11094 return def;
11095}
11096
11097static struct triple *cast_expr(struct compile_state *state)
11098{
11099 struct triple *def;
11100 int tok1, tok2;
11101 tok1 = peek(state);
11102 tok2 = peek2(state);
11103 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11104 struct type *type;
11105 eat(state, TOK_LPAREN);
11106 type = type_name(state);
11107 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011108 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011109 }
11110 else {
11111 def = unary_expr(state);
11112 }
11113 return def;
11114}
11115
11116static struct triple *mult_expr(struct compile_state *state)
11117{
11118 struct triple *def;
11119 int done;
11120 def = cast_expr(state);
11121 do {
11122 struct triple *left, *right;
11123 struct type *result_type;
11124 int tok, op, sign;
11125 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011126 tok = peek(state);
11127 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011128 case TOK_STAR:
11129 case TOK_DIV:
11130 case TOK_MOD:
11131 left = read_expr(state, def);
11132 arithmetic(state, left);
11133
11134 eat(state, tok);
11135
11136 right = read_expr(state, cast_expr(state));
11137 arithmetic(state, right);
11138
11139 result_type = arithmetic_result(state, left, right);
11140 sign = is_signed(result_type);
11141 op = -1;
11142 switch(tok) {
11143 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11144 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11145 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11146 }
11147 def = triple(state, op, result_type, left, right);
11148 break;
11149 default:
11150 done = 1;
11151 break;
11152 }
11153 } while(!done);
11154 return def;
11155}
11156
11157static struct triple *add_expr(struct compile_state *state)
11158{
11159 struct triple *def;
11160 int done;
11161 def = mult_expr(state);
11162 do {
11163 done = 0;
11164 switch( peek(state)) {
11165 case TOK_PLUS:
11166 eat(state, TOK_PLUS);
11167 def = mk_add_expr(state, def, mult_expr(state));
11168 break;
11169 case TOK_MINUS:
11170 eat(state, TOK_MINUS);
11171 def = mk_sub_expr(state, def, mult_expr(state));
11172 break;
11173 default:
11174 done = 1;
11175 break;
11176 }
11177 } while(!done);
11178 return def;
11179}
11180
11181static struct triple *shift_expr(struct compile_state *state)
11182{
11183 struct triple *def;
11184 int done;
11185 def = add_expr(state);
11186 do {
11187 struct triple *left, *right;
11188 int tok, op;
11189 done = 0;
11190 switch((tok = peek(state))) {
11191 case TOK_SL:
11192 case TOK_SR:
11193 left = read_expr(state, def);
11194 integral(state, left);
11195 left = integral_promotion(state, left);
11196
11197 eat(state, tok);
11198
11199 right = read_expr(state, add_expr(state));
11200 integral(state, right);
11201 right = integral_promotion(state, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011202
11203 op = (tok == TOK_SL)? OP_SL :
Eric Biedermanb138ac82003-04-22 18:44:01 +000011204 is_signed(left->type)? OP_SSR: OP_USR;
11205
11206 def = triple(state, op, left->type, left, right);
11207 break;
11208 default:
11209 done = 1;
11210 break;
11211 }
11212 } while(!done);
11213 return def;
11214}
11215
11216static struct triple *relational_expr(struct compile_state *state)
11217{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011218#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011219#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011220#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011221 struct triple *def;
11222 int done;
11223 def = shift_expr(state);
11224 do {
11225 struct triple *left, *right;
11226 struct type *arg_type;
11227 int tok, op, sign;
11228 done = 0;
11229 switch((tok = peek(state))) {
11230 case TOK_LESS:
11231 case TOK_MORE:
11232 case TOK_LESSEQ:
11233 case TOK_MOREEQ:
11234 left = read_expr(state, def);
11235 arithmetic(state, left);
11236
11237 eat(state, tok);
11238
11239 right = read_expr(state, shift_expr(state));
11240 arithmetic(state, right);
11241
11242 arg_type = arithmetic_result(state, left, right);
11243 sign = is_signed(arg_type);
11244 op = -1;
11245 switch(tok) {
11246 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11247 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11248 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11249 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11250 }
11251 def = triple(state, op, &int_type, left, right);
11252 break;
11253 default:
11254 done = 1;
11255 break;
11256 }
11257 } while(!done);
11258 return def;
11259}
11260
11261static struct triple *equality_expr(struct compile_state *state)
11262{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011263#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011264#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011265#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011266 struct triple *def;
11267 int done;
11268 def = relational_expr(state);
11269 do {
11270 struct triple *left, *right;
11271 int tok, op;
11272 done = 0;
11273 switch((tok = peek(state))) {
11274 case TOK_EQEQ:
11275 case TOK_NOTEQ:
11276 left = read_expr(state, def);
11277 arithmetic(state, left);
11278 eat(state, tok);
11279 right = read_expr(state, relational_expr(state));
11280 arithmetic(state, right);
11281 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11282 def = triple(state, op, &int_type, left, right);
11283 break;
11284 default:
11285 done = 1;
11286 break;
11287 }
11288 } while(!done);
11289 return def;
11290}
11291
11292static struct triple *and_expr(struct compile_state *state)
11293{
11294 struct triple *def;
11295 def = equality_expr(state);
11296 while(peek(state) == TOK_AND) {
11297 struct triple *left, *right;
11298 struct type *result_type;
11299 left = read_expr(state, def);
11300 integral(state, left);
11301 eat(state, TOK_AND);
11302 right = read_expr(state, equality_expr(state));
11303 integral(state, right);
11304 result_type = arithmetic_result(state, left, right);
11305 def = triple(state, OP_AND, result_type, left, right);
11306 }
11307 return def;
11308}
11309
11310static struct triple *xor_expr(struct compile_state *state)
11311{
11312 struct triple *def;
11313 def = and_expr(state);
11314 while(peek(state) == TOK_XOR) {
11315 struct triple *left, *right;
11316 struct type *result_type;
11317 left = read_expr(state, def);
11318 integral(state, left);
11319 eat(state, TOK_XOR);
11320 right = read_expr(state, and_expr(state));
11321 integral(state, right);
11322 result_type = arithmetic_result(state, left, right);
11323 def = triple(state, OP_XOR, result_type, left, right);
11324 }
11325 return def;
11326}
11327
11328static struct triple *or_expr(struct compile_state *state)
11329{
11330 struct triple *def;
11331 def = xor_expr(state);
11332 while(peek(state) == TOK_OR) {
11333 struct triple *left, *right;
11334 struct type *result_type;
11335 left = read_expr(state, def);
11336 integral(state, left);
11337 eat(state, TOK_OR);
11338 right = read_expr(state, xor_expr(state));
11339 integral(state, right);
11340 result_type = arithmetic_result(state, left, right);
11341 def = triple(state, OP_OR, result_type, left, right);
11342 }
11343 return def;
11344}
11345
11346static struct triple *land_expr(struct compile_state *state)
11347{
11348 struct triple *def;
11349 def = or_expr(state);
11350 while(peek(state) == TOK_LOGAND) {
11351 struct triple *left, *right;
11352 left = read_expr(state, def);
11353 bool(state, left);
11354 eat(state, TOK_LOGAND);
11355 right = read_expr(state, or_expr(state));
11356 bool(state, right);
11357
Eric Biederman90089602004-05-28 14:11:54 +000011358 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011359 ltrue_expr(state, left),
11360 ltrue_expr(state, right));
11361 }
11362 return def;
11363}
11364
11365static struct triple *lor_expr(struct compile_state *state)
11366{
11367 struct triple *def;
11368 def = land_expr(state);
11369 while(peek(state) == TOK_LOGOR) {
11370 struct triple *left, *right;
11371 left = read_expr(state, def);
11372 bool(state, left);
11373 eat(state, TOK_LOGOR);
11374 right = read_expr(state, land_expr(state));
11375 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011376
Stefan Reinauer14e22772010-04-27 06:56:47 +000011377 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011378 ltrue_expr(state, left),
11379 ltrue_expr(state, right));
11380 }
11381 return def;
11382}
11383
11384static struct triple *conditional_expr(struct compile_state *state)
11385{
11386 struct triple *def;
11387 def = lor_expr(state);
11388 if (peek(state) == TOK_QUEST) {
11389 struct triple *test, *left, *right;
11390 bool(state, def);
11391 test = ltrue_expr(state, read_expr(state, def));
11392 eat(state, TOK_QUEST);
11393 left = read_expr(state, expr(state));
11394 eat(state, TOK_COLON);
11395 right = read_expr(state, conditional_expr(state));
11396
Eric Biederman90089602004-05-28 14:11:54 +000011397 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011398 }
11399 return def;
11400}
11401
Eric Biederman41203d92004-11-08 09:31:09 +000011402struct cv_triple {
11403 struct triple *val;
11404 int id;
11405};
11406
11407static void set_cv(struct compile_state *state, struct cv_triple *cv,
11408 struct triple *dest, struct triple *val)
11409{
11410 if (cv[dest->id].val) {
11411 free_triple(state, cv[dest->id].val);
11412 }
11413 cv[dest->id].val = val;
11414}
11415static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11416 struct triple *src)
11417{
11418 return cv[src->id].val;
11419}
11420
Eric Biedermanb138ac82003-04-22 18:44:01 +000011421static struct triple *eval_const_expr(
11422 struct compile_state *state, struct triple *expr)
11423{
11424 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011425 if (is_const(expr)) {
11426 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011427 }
Eric Biederman00443072003-06-24 12:34:45 +000011428 else {
11429 /* If we don't start out as a constant simplify into one */
11430 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011431 struct cv_triple *cv;
11432 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011433 head = label(state); /* dummy initial triple */
11434 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011435 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011436 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011437 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011438 }
Eric Biederman41203d92004-11-08 09:31:09 +000011439 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11440 i = 1;
11441 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11442 cv[i].val = 0;
11443 cv[i].id = ptr->id;
11444 ptr->id = i;
11445 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011446 }
Eric Biederman41203d92004-11-08 09:31:09 +000011447 ptr = head->next;
11448 do {
11449 valid_ins(state, ptr);
11450 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011451 internal_error(state, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011452 "unexpected %s in constant expression",
11453 tops(ptr->op));
11454 }
11455 else if (ptr->op == OP_LIST) {
11456 }
11457 else if (triple_is_structural(state, ptr)) {
11458 ptr = ptr->next;
11459 }
11460 else if (triple_is_ubranch(state, ptr)) {
11461 ptr = TARG(ptr, 0);
11462 }
11463 else if (triple_is_cbranch(state, ptr)) {
11464 struct triple *cond_val;
11465 cond_val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011466 if (!cond_val || !is_const(cond_val) ||
11467 (cond_val->op != OP_INTCONST))
Eric Biederman41203d92004-11-08 09:31:09 +000011468 {
11469 internal_error(state, ptr, "bad branch condition");
11470 }
11471 if (cond_val->u.cval == 0) {
11472 ptr = ptr->next;
11473 } else {
11474 ptr = TARG(ptr, 0);
11475 }
11476 }
11477 else if (triple_is_branch(state, ptr)) {
11478 error(state, ptr, "bad branch type in constant expression");
11479 }
11480 else if (ptr->op == OP_WRITE) {
11481 struct triple *val;
11482 val = get_cv(state, cv, RHS(ptr, 0));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011483
11484 set_cv(state, cv, MISC(ptr, 0),
Eric Biederman41203d92004-11-08 09:31:09 +000011485 copy_triple(state, val));
Stefan Reinauer14e22772010-04-27 06:56:47 +000011486 set_cv(state, cv, ptr,
Eric Biederman41203d92004-11-08 09:31:09 +000011487 copy_triple(state, val));
11488 ptr = ptr->next;
11489 }
11490 else if (ptr->op == OP_READ) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000011491 set_cv(state, cv, ptr,
11492 copy_triple(state,
Eric Biederman41203d92004-11-08 09:31:09 +000011493 get_cv(state, cv, RHS(ptr, 0))));
11494 ptr = ptr->next;
11495 }
11496 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11497 struct triple *val, **rhs;
11498 val = copy_triple(state, ptr);
11499 rhs = triple_rhs(state, val, 0);
11500 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11501 if (!*rhs) {
11502 internal_error(state, ptr, "Missing rhs");
11503 }
11504 *rhs = get_cv(state, cv, *rhs);
11505 }
11506 simplify(state, val);
11507 set_cv(state, cv, ptr, val);
11508 ptr = ptr->next;
11509 }
11510 else {
11511 error(state, ptr, "impure operation in constant expression");
11512 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000011513
Eric Biederman41203d92004-11-08 09:31:09 +000011514 } while(ptr != head);
11515
11516 /* Get the result value */
11517 def = get_cv(state, cv, head->prev);
11518 cv[head->prev->id].val = 0;
11519
11520 /* Free the temporary values */
11521 for(i = 0; i < count; i++) {
11522 if (cv[i].val) {
11523 free_triple(state, cv[i].val);
11524 cv[i].val = 0;
11525 }
11526 }
11527 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011528 /* Free the intermediate expressions */
11529 while(head->next != head) {
11530 release_triple(state, head->next);
11531 }
11532 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011533 }
Eric Biederman41203d92004-11-08 09:31:09 +000011534 if (!is_const(def)) {
11535 error(state, expr, "Not a constant expression");
11536 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011537 return def;
11538}
11539
11540static struct triple *constant_expr(struct compile_state *state)
11541{
11542 return eval_const_expr(state, conditional_expr(state));
11543}
11544
11545static struct triple *assignment_expr(struct compile_state *state)
11546{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011547 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011548 int tok, op, sign;
11549 /* The C grammer in K&R shows assignment expressions
11550 * only taking unary expressions as input on their
11551 * left hand side. But specifies the precedence of
11552 * assignemnt as the lowest operator except for comma.
11553 *
11554 * Allowing conditional expressions on the left hand side
11555 * of an assignement results in a grammar that accepts
11556 * a larger set of statements than standard C. As long
11557 * as the subset of the grammar that is standard C behaves
11558 * correctly this should cause no problems.
Stefan Reinauer14e22772010-04-27 06:56:47 +000011559 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000011560 * For the extra token strings accepted by the grammar
11561 * none of them should produce a valid lvalue, so they
11562 * should not produce functioning programs.
11563 *
11564 * GCC has this bug as well, so surprises should be minimal.
11565 */
11566 def = conditional_expr(state);
11567 left = def;
11568 switch((tok = peek(state))) {
11569 case TOK_EQ:
11570 lvalue(state, left);
11571 eat(state, TOK_EQ);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011572 def = write_expr(state, left,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011573 read_expr(state, assignment_expr(state)));
11574 break;
11575 case TOK_TIMESEQ:
11576 case TOK_DIVEQ:
11577 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011578 lvalue(state, left);
11579 arithmetic(state, left);
11580 eat(state, tok);
11581 right = read_expr(state, assignment_expr(state));
11582 arithmetic(state, right);
11583
11584 sign = is_signed(left->type);
11585 op = -1;
11586 switch(tok) {
11587 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11588 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11589 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011590 }
11591 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011592 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011593 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011594 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011595 case TOK_PLUSEQ:
11596 lvalue(state, left);
11597 eat(state, TOK_PLUSEQ);
11598 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011599 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011600 break;
11601 case TOK_MINUSEQ:
11602 lvalue(state, left);
11603 eat(state, TOK_MINUSEQ);
11604 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011605 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011606 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011607 case TOK_SLEQ:
11608 case TOK_SREQ:
11609 case TOK_ANDEQ:
11610 case TOK_XOREQ:
11611 case TOK_OREQ:
11612 lvalue(state, left);
11613 integral(state, left);
11614 eat(state, tok);
11615 right = read_expr(state, assignment_expr(state));
11616 integral(state, right);
11617 right = integral_promotion(state, right);
11618 sign = is_signed(left->type);
11619 op = -1;
11620 switch(tok) {
11621 case TOK_SLEQ: op = OP_SL; break;
11622 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11623 case TOK_ANDEQ: op = OP_AND; break;
11624 case TOK_XOREQ: op = OP_XOR; break;
11625 case TOK_OREQ: op = OP_OR; break;
11626 }
11627 def = write_expr(state, left,
Stefan Reinauer14e22772010-04-27 06:56:47 +000011628 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011629 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011630 break;
11631 }
11632 return def;
11633}
11634
11635static struct triple *expr(struct compile_state *state)
11636{
11637 struct triple *def;
11638 def = assignment_expr(state);
11639 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011640 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011641 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011642 }
11643 return def;
11644}
11645
11646static void expr_statement(struct compile_state *state, struct triple *first)
11647{
11648 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011649 /* lvalue conversions always apply except when certian operators
11650 * are applied. I apply the lvalue conversions here
11651 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011652 */
11653 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011654 }
11655 eat(state, TOK_SEMI);
11656}
11657
11658static void if_statement(struct compile_state *state, struct triple *first)
11659{
11660 struct triple *test, *jmp1, *jmp2, *middle, *end;
11661
11662 jmp1 = jmp2 = middle = 0;
11663 eat(state, TOK_IF);
11664 eat(state, TOK_LPAREN);
11665 test = expr(state);
11666 bool(state, test);
11667 /* Cleanup and invert the test */
11668 test = lfalse_expr(state, read_expr(state, test));
11669 eat(state, TOK_RPAREN);
11670 /* Generate the needed pieces */
11671 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011672 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011673 /* Thread the pieces together */
11674 flatten(state, first, test);
11675 flatten(state, first, jmp1);
11676 flatten(state, first, label(state));
11677 statement(state, first);
11678 if (peek(state) == TOK_ELSE) {
11679 eat(state, TOK_ELSE);
11680 /* Generate the rest of the pieces */
11681 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011682 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011683 /* Thread them together */
11684 flatten(state, first, jmp2);
11685 flatten(state, first, middle);
11686 statement(state, first);
11687 flatten(state, first, end);
11688 }
11689 else {
11690 flatten(state, first, middle);
11691 }
11692}
11693
11694static void for_statement(struct compile_state *state, struct triple *first)
11695{
11696 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11697 struct triple *label1, *label2, *label3;
11698 struct hash_entry *ident;
11699
11700 eat(state, TOK_FOR);
11701 eat(state, TOK_LPAREN);
11702 head = test = tail = jmp1 = jmp2 = 0;
11703 if (peek(state) != TOK_SEMI) {
11704 head = expr(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000011705 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011706 eat(state, TOK_SEMI);
11707 if (peek(state) != TOK_SEMI) {
11708 test = expr(state);
11709 bool(state, test);
11710 test = ltrue_expr(state, read_expr(state, test));
11711 }
11712 eat(state, TOK_SEMI);
11713 if (peek(state) != TOK_RPAREN) {
11714 tail = expr(state);
11715 }
11716 eat(state, TOK_RPAREN);
11717 /* Generate the needed pieces */
11718 label1 = label(state);
11719 label2 = label(state);
11720 label3 = label(state);
11721 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011722 jmp1 = branch(state, label3, 0);
11723 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011724 }
11725 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011726 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011727 }
11728 end = label(state);
11729 /* Remember where break and continue go */
11730 start_scope(state);
11731 ident = state->i_break;
11732 symbol(state, ident, &ident->sym_ident, end, end->type);
11733 ident = state->i_continue;
11734 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11735 /* Now include the body */
11736 flatten(state, first, head);
11737 flatten(state, first, jmp1);
11738 flatten(state, first, label1);
11739 statement(state, first);
11740 flatten(state, first, label2);
11741 flatten(state, first, tail);
11742 flatten(state, first, label3);
11743 flatten(state, first, test);
11744 flatten(state, first, jmp2);
11745 flatten(state, first, end);
11746 /* Cleanup the break/continue scope */
11747 end_scope(state);
11748}
11749
11750static void while_statement(struct compile_state *state, struct triple *first)
11751{
11752 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11753 struct hash_entry *ident;
11754 eat(state, TOK_WHILE);
11755 eat(state, TOK_LPAREN);
11756 test = expr(state);
11757 bool(state, test);
11758 test = ltrue_expr(state, read_expr(state, test));
11759 eat(state, TOK_RPAREN);
11760 /* Generate the needed pieces */
11761 label1 = label(state);
11762 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011763 jmp1 = branch(state, label2, 0);
11764 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011765 end = label(state);
11766 /* Remember where break and continue go */
11767 start_scope(state);
11768 ident = state->i_break;
11769 symbol(state, ident, &ident->sym_ident, end, end->type);
11770 ident = state->i_continue;
11771 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11772 /* Thread them together */
11773 flatten(state, first, jmp1);
11774 flatten(state, first, label1);
11775 statement(state, first);
11776 flatten(state, first, label2);
11777 flatten(state, first, test);
11778 flatten(state, first, jmp2);
11779 flatten(state, first, end);
11780 /* Cleanup the break/continue scope */
11781 end_scope(state);
11782}
11783
11784static void do_statement(struct compile_state *state, struct triple *first)
11785{
11786 struct triple *label1, *label2, *test, *end;
11787 struct hash_entry *ident;
11788 eat(state, TOK_DO);
11789 /* Generate the needed pieces */
11790 label1 = label(state);
11791 label2 = label(state);
11792 end = label(state);
11793 /* Remember where break and continue go */
11794 start_scope(state);
11795 ident = state->i_break;
11796 symbol(state, ident, &ident->sym_ident, end, end->type);
11797 ident = state->i_continue;
11798 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11799 /* Now include the body */
11800 flatten(state, first, label1);
11801 statement(state, first);
11802 /* Cleanup the break/continue scope */
11803 end_scope(state);
11804 /* Eat the rest of the loop */
11805 eat(state, TOK_WHILE);
11806 eat(state, TOK_LPAREN);
11807 test = read_expr(state, expr(state));
11808 bool(state, test);
11809 eat(state, TOK_RPAREN);
11810 eat(state, TOK_SEMI);
11811 /* Thread the pieces together */
11812 test = ltrue_expr(state, test);
11813 flatten(state, first, label2);
11814 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011815 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011816 flatten(state, first, end);
11817}
11818
11819
11820static void return_statement(struct compile_state *state, struct triple *first)
11821{
11822 struct triple *jmp, *mv, *dest, *var, *val;
11823 int last;
11824 eat(state, TOK_RETURN);
11825
Stefan Reinauer50542a82007-10-24 11:14:14 +000011826#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011827#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011828#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011829 val = 0;
11830 /* If we have a return value do some more work */
11831 if (peek(state) != TOK_SEMI) {
11832 val = read_expr(state, expr(state));
11833 }
11834 eat(state, TOK_SEMI);
11835
11836 /* See if this last statement in a function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000011837 last = ((peek(state) == TOK_RBRACE) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000011838 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11839
11840 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011841 var = fresult(state, state->main_function);
11842
Eric Biedermanb138ac82003-04-22 18:44:01 +000011843 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011844 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011845 mv = jmp = 0;
11846 /* If needed generate a jump instruction */
11847 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011848 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011849 }
11850 /* If needed generate an assignment instruction */
11851 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011852 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011853 }
11854 /* Now put the code together */
11855 if (mv) {
11856 flatten(state, first, mv);
11857 flatten(state, first, jmp);
11858 }
11859 else if (jmp) {
11860 flatten(state, first, jmp);
11861 }
11862}
11863
11864static void break_statement(struct compile_state *state, struct triple *first)
11865{
11866 struct triple *dest;
11867 eat(state, TOK_BREAK);
11868 eat(state, TOK_SEMI);
11869 if (!state->i_break->sym_ident) {
11870 error(state, 0, "break statement not within loop or switch");
11871 }
11872 dest = state->i_break->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 continue_statement(struct compile_state *state, struct triple *first)
11877{
11878 struct triple *dest;
11879 eat(state, TOK_CONTINUE);
11880 eat(state, TOK_SEMI);
11881 if (!state->i_continue->sym_ident) {
11882 error(state, 0, "continue statement outside of a loop");
11883 }
11884 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011885 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011886}
11887
11888static void goto_statement(struct compile_state *state, struct triple *first)
11889{
Eric Biederman153ea352003-06-20 14:43:20 +000011890 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011891 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011892 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011893 if (!ident->sym_label) {
11894 /* If this is a forward branch allocate the label now,
11895 * it will be flattend in the appropriate location later.
11896 */
11897 struct triple *ins;
11898 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011899 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011900 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011901 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011902
11903 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011904}
11905
11906static void labeled_statement(struct compile_state *state, struct triple *first)
11907{
Eric Biederman153ea352003-06-20 14:43:20 +000011908 struct triple *ins;
11909 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011910
Eric Biederman41203d92004-11-08 09:31:09 +000011911 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011912 if (ident->sym_label && ident->sym_label->def) {
11913 ins = ident->sym_label->def;
11914 put_occurance(ins->occurance);
11915 ins->occurance = new_occurance(state);
11916 }
11917 else {
11918 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011919 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011920 }
11921 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11922 error(state, 0, "label %s already defined", ident->name);
11923 }
11924 flatten(state, first, ins);
11925
Eric Biedermanb138ac82003-04-22 18:44:01 +000011926 eat(state, TOK_COLON);
11927 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011928}
11929
11930static void switch_statement(struct compile_state *state, struct triple *first)
11931{
Eric Biederman83b991a2003-10-11 06:20:25 +000011932 struct triple *value, *top, *end, *dbranch;
11933 struct hash_entry *ident;
11934
11935 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011936 eat(state, TOK_SWITCH);
11937 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011938 value = expr(state);
11939 integral(state, value);
11940 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011941 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011942 /* Generate the needed pieces */
11943 top = label(state);
11944 end = label(state);
11945 dbranch = branch(state, end, 0);
11946 /* Remember where case branches and break goes */
11947 start_scope(state);
11948 ident = state->i_switch;
11949 symbol(state, ident, &ident->sym_ident, value, value->type);
11950 ident = state->i_case;
11951 symbol(state, ident, &ident->sym_ident, top, top->type);
11952 ident = state->i_break;
11953 symbol(state, ident, &ident->sym_ident, end, end->type);
11954 ident = state->i_default;
11955 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11956 /* Thread them together */
11957 flatten(state, first, value);
11958 flatten(state, first, top);
11959 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011960 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011961 flatten(state, first, end);
11962 /* Cleanup the switch scope */
11963 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011964}
11965
11966static void case_statement(struct compile_state *state, struct triple *first)
11967{
Eric Biederman83b991a2003-10-11 06:20:25 +000011968 struct triple *cvalue, *dest, *test, *jmp;
11969 struct triple *ptr, *value, *top, *dbranch;
11970
11971 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011972 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011973 cvalue = constant_expr(state);
11974 integral(state, cvalue);
11975 if (cvalue->op != OP_INTCONST) {
11976 error(state, 0, "integer constant expected");
11977 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011978 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011979 if (!state->i_case->sym_ident) {
11980 error(state, 0, "case statement not within a switch");
11981 }
11982
11983 /* Lookup the interesting pieces */
11984 top = state->i_case->sym_ident->def;
11985 value = state->i_switch->sym_ident->def;
11986 dbranch = state->i_default->sym_ident->def;
11987
11988 /* See if this case label has already been used */
11989 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11990 if (ptr->op != OP_EQ) {
11991 continue;
11992 }
11993 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
11994 error(state, 0, "duplicate case %d statement",
11995 cvalue->u.cval);
11996 }
11997 }
11998 /* Generate the needed pieces */
11999 dest = label(state);
12000 test = triple(state, OP_EQ, &int_type, value, cvalue);
12001 jmp = branch(state, dest, test);
12002 /* Thread the pieces together */
12003 flatten(state, dbranch, test);
12004 flatten(state, dbranch, jmp);
12005 flatten(state, dbranch, label(state));
12006 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012007 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012008}
12009
12010static void default_statement(struct compile_state *state, struct triple *first)
12011{
Eric Biederman83b991a2003-10-11 06:20:25 +000012012 struct triple *dest;
12013 struct triple *dbranch, *end;
12014
12015 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012016 eat(state, TOK_DEFAULT);
12017 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012018
12019 if (!state->i_case->sym_ident) {
12020 error(state, 0, "default statement not within a switch");
12021 }
12022
12023 /* Lookup the interesting pieces */
12024 dbranch = state->i_default->sym_ident->def;
12025 end = state->i_break->sym_ident->def;
12026
12027 /* See if a default statement has already happened */
12028 if (TARG(dbranch, 0) != end) {
12029 error(state, 0, "duplicate default statement");
12030 }
12031
12032 /* Generate the needed pieces */
12033 dest = label(state);
12034
Eric Biederman90089602004-05-28 14:11:54 +000012035 /* Blame the branch on the default statement */
12036 put_occurance(dbranch->occurance);
12037 dbranch->occurance = new_occurance(state);
12038
Eric Biederman83b991a2003-10-11 06:20:25 +000012039 /* Thread the pieces together */
12040 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012041 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012042 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012043 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012044}
12045
12046static void asm_statement(struct compile_state *state, struct triple *first)
12047{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012048 struct asm_info *info;
12049 struct {
12050 struct triple *constraint;
12051 struct triple *expr;
12052 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12053 struct triple *def, *asm_str;
12054 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012055 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012056
Eric Biederman90089602004-05-28 14:11:54 +000012057 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012058 eat(state, TOK_ASM);
12059 /* For now ignore the qualifiers */
12060 switch(peek(state)) {
12061 case TOK_CONST:
12062 eat(state, TOK_CONST);
12063 break;
12064 case TOK_VOLATILE:
12065 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012066 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012067 break;
12068 }
12069 eat(state, TOK_LPAREN);
12070 asm_str = string_constant(state);
12071
12072 colons = 0;
12073 out = in = clobbers = 0;
12074 /* Outputs */
12075 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12076 eat(state, TOK_COLON);
12077 colons++;
12078 more = (peek(state) == TOK_LIT_STRING);
12079 while(more) {
12080 struct triple *var;
12081 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012082 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012083 more = 0;
12084 if (out > MAX_LHS) {
12085 error(state, 0, "Maximum output count exceeded.");
12086 }
12087 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012088 str = constraint->u.blob;
12089 if (str[0] != '=') {
12090 error(state, 0, "Output constraint does not start with =");
12091 }
12092 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012093 eat(state, TOK_LPAREN);
12094 var = conditional_expr(state);
12095 eat(state, TOK_RPAREN);
12096
12097 lvalue(state, var);
12098 out_param[out].constraint = constraint;
12099 out_param[out].expr = var;
12100 if (peek(state) == TOK_COMMA) {
12101 eat(state, TOK_COMMA);
12102 more = 1;
12103 }
12104 out++;
12105 }
12106 }
12107 /* Inputs */
12108 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12109 eat(state, TOK_COLON);
12110 colons++;
12111 more = (peek(state) == TOK_LIT_STRING);
12112 while(more) {
12113 struct triple *val;
12114 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012115 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012116 more = 0;
12117 if (in > MAX_RHS) {
12118 error(state, 0, "Maximum input count exceeded.");
12119 }
12120 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012121 str = constraint->u.blob;
12122 if (digitp(str[0] && str[1] == '\0')) {
12123 int val;
12124 val = digval(str[0]);
12125 if ((val < 0) || (val >= out)) {
12126 error(state, 0, "Invalid input constraint %d", val);
12127 }
12128 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012129 eat(state, TOK_LPAREN);
12130 val = conditional_expr(state);
12131 eat(state, TOK_RPAREN);
12132
12133 in_param[in].constraint = constraint;
12134 in_param[in].expr = val;
12135 if (peek(state) == TOK_COMMA) {
12136 eat(state, TOK_COMMA);
12137 more = 1;
12138 }
12139 in++;
12140 }
12141 }
12142
12143 /* Clobber */
12144 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12145 eat(state, TOK_COLON);
12146 colons++;
12147 more = (peek(state) == TOK_LIT_STRING);
12148 while(more) {
12149 struct triple *clobber;
12150 more = 0;
12151 if ((clobbers + out) > MAX_LHS) {
12152 error(state, 0, "Maximum clobber limit exceeded.");
12153 }
12154 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012155
12156 clob_param[clobbers].constraint = clobber;
12157 if (peek(state) == TOK_COMMA) {
12158 eat(state, TOK_COMMA);
12159 more = 1;
12160 }
12161 clobbers++;
12162 }
12163 }
12164 eat(state, TOK_RPAREN);
12165 eat(state, TOK_SEMI);
12166
12167
12168 info = xcmalloc(sizeof(*info), "asm_info");
12169 info->str = asm_str->u.blob;
12170 free_triple(state, asm_str);
12171
12172 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12173 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012174 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012175
12176 /* Find the register constraints */
12177 for(i = 0; i < out; i++) {
12178 struct triple *constraint;
12179 constraint = out_param[i].constraint;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012180 info->tmpl.lhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012181 out_param[i].expr->type, constraint->u.blob);
12182 free_triple(state, constraint);
12183 }
12184 for(; i - out < clobbers; i++) {
12185 struct triple *constraint;
12186 constraint = clob_param[i - out].constraint;
12187 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12188 free_triple(state, constraint);
12189 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012190 for(i = 0; i < in; i++) {
12191 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012192 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012193 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012194 str = constraint->u.blob;
12195 if (digitp(str[0]) && str[1] == '\0') {
12196 struct reg_info cinfo;
12197 int val;
12198 val = digval(str[0]);
12199 cinfo.reg = info->tmpl.lhs[val].reg;
12200 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12201 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12202 if (cinfo.reg == REG_UNSET) {
12203 cinfo.reg = REG_VIRT0 + val;
12204 }
12205 if (cinfo.regcm == 0) {
12206 error(state, 0, "No registers for %d", val);
12207 }
12208 info->tmpl.lhs[val] = cinfo;
12209 info->tmpl.rhs[i] = cinfo;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012210
Eric Biederman8d9c1232003-06-17 08:42:17 +000012211 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012212 info->tmpl.rhs[i] = arch_reg_constraint(state,
Eric Biederman8d9c1232003-06-17 08:42:17 +000012213 in_param[i].expr->type, str);
12214 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012215 free_triple(state, constraint);
12216 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012217
12218 /* Now build the helper expressions */
12219 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012220 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012221 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012222 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012223 for(i = 0; i < (out + clobbers); i++) {
12224 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012225 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012226 if (i < out) {
12227 type = out_param[i].expr->type;
12228 } else {
12229 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12230 if (size >= SIZEOF_LONG) {
12231 type = &ulong_type;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012232 }
Eric Biederman90089602004-05-28 14:11:54 +000012233 else if (size >= SIZEOF_INT) {
12234 type = &uint_type;
12235 }
12236 else if (size >= SIZEOF_SHORT) {
12237 type = &ushort_type;
12238 }
12239 else {
12240 type = &uchar_type;
12241 }
12242 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012243 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012244 piece->u.cval = i;
12245 LHS(def, i) = piece;
12246 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012247 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012248 /* And write the helpers to their destinations */
12249 for(i = 0; i < out; i++) {
12250 struct triple *piece;
12251 piece = LHS(def, i);
12252 flatten(state, first,
12253 write_expr(state, out_param[i].expr, piece));
12254 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012255}
12256
12257
12258static int isdecl(int tok)
12259{
12260 switch(tok) {
12261 case TOK_AUTO:
12262 case TOK_REGISTER:
12263 case TOK_STATIC:
12264 case TOK_EXTERN:
12265 case TOK_TYPEDEF:
12266 case TOK_CONST:
12267 case TOK_RESTRICT:
12268 case TOK_VOLATILE:
12269 case TOK_VOID:
12270 case TOK_CHAR:
12271 case TOK_SHORT:
12272 case TOK_INT:
12273 case TOK_LONG:
12274 case TOK_FLOAT:
12275 case TOK_DOUBLE:
12276 case TOK_SIGNED:
12277 case TOK_UNSIGNED:
12278 case TOK_STRUCT:
12279 case TOK_UNION:
12280 case TOK_ENUM:
12281 case TOK_TYPE_NAME: /* typedef name */
12282 return 1;
12283 default:
12284 return 0;
12285 }
12286}
12287
12288static void compound_statement(struct compile_state *state, struct triple *first)
12289{
12290 eat(state, TOK_LBRACE);
12291 start_scope(state);
12292
12293 /* statement-list opt */
12294 while (peek(state) != TOK_RBRACE) {
12295 statement(state, first);
12296 }
12297 end_scope(state);
12298 eat(state, TOK_RBRACE);
12299}
12300
12301static void statement(struct compile_state *state, struct triple *first)
12302{
12303 int tok;
12304 tok = peek(state);
12305 if (tok == TOK_LBRACE) {
12306 compound_statement(state, first);
12307 }
12308 else if (tok == TOK_IF) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012309 if_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012310 }
12311 else if (tok == TOK_FOR) {
12312 for_statement(state, first);
12313 }
12314 else if (tok == TOK_WHILE) {
12315 while_statement(state, first);
12316 }
12317 else if (tok == TOK_DO) {
12318 do_statement(state, first);
12319 }
12320 else if (tok == TOK_RETURN) {
12321 return_statement(state, first);
12322 }
12323 else if (tok == TOK_BREAK) {
12324 break_statement(state, first);
12325 }
12326 else if (tok == TOK_CONTINUE) {
12327 continue_statement(state, first);
12328 }
12329 else if (tok == TOK_GOTO) {
12330 goto_statement(state, first);
12331 }
12332 else if (tok == TOK_SWITCH) {
12333 switch_statement(state, first);
12334 }
12335 else if (tok == TOK_ASM) {
12336 asm_statement(state, first);
12337 }
12338 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012339 labeled_statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012340 }
12341 else if (tok == TOK_CASE) {
12342 case_statement(state, first);
12343 }
12344 else if (tok == TOK_DEFAULT) {
12345 default_statement(state, first);
12346 }
12347 else if (isdecl(tok)) {
12348 /* This handles C99 intermixing of statements and decls */
12349 decl(state, first);
12350 }
12351 else {
12352 expr_statement(state, first);
12353 }
12354}
12355
12356static struct type *param_decl(struct compile_state *state)
12357{
12358 struct type *type;
12359 struct hash_entry *ident;
12360 /* Cheat so the declarator will know we are not global */
Stefan Reinauer14e22772010-04-27 06:56:47 +000012361 start_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012362 ident = 0;
12363 type = decl_specifiers(state);
12364 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012365 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012366 end_scope(state);
12367 return type;
12368}
12369
12370static struct type *param_type_list(struct compile_state *state, struct type *type)
12371{
12372 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012373 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012374 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012375 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012376 while(peek(state) == TOK_COMMA) {
12377 eat(state, TOK_COMMA);
12378 if (peek(state) == TOK_DOTS) {
12379 eat(state, TOK_DOTS);
12380 error(state, 0, "variadic functions not supported");
12381 }
12382 else {
12383 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12384 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012385 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012386 }
12387 }
12388 return ftype;
12389}
12390
Eric Biedermanb138ac82003-04-22 18:44:01 +000012391static struct type *type_name(struct compile_state *state)
12392{
12393 struct type *type;
12394 type = specifier_qualifier_list(state);
12395 /* abstract-declarator (may consume no tokens) */
12396 type = declarator(state, type, 0, 0);
12397 return type;
12398}
12399
12400static struct type *direct_declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012401 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012402 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012403{
Eric Biederman41203d92004-11-08 09:31:09 +000012404 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012405 struct type *outer;
12406 int op;
12407 outer = 0;
12408 arrays_complete(state, type);
12409 switch(peek(state)) {
12410 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012411 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012412 if (!ident) {
12413 error(state, 0, "Unexpected identifier found");
12414 }
12415 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012416 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012417 break;
12418 case TOK_LPAREN:
12419 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012420 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012421 eat(state, TOK_RPAREN);
12422 break;
12423 default:
12424 if (need_ident) {
12425 error(state, 0, "Identifier expected");
12426 }
12427 break;
12428 }
12429 do {
12430 op = 1;
12431 arrays_complete(state, type);
12432 switch(peek(state)) {
12433 case TOK_LPAREN:
12434 eat(state, TOK_LPAREN);
12435 type = param_type_list(state, type);
12436 eat(state, TOK_RPAREN);
12437 break;
12438 case TOK_LBRACKET:
12439 {
12440 unsigned int qualifiers;
12441 struct triple *value;
12442 value = 0;
12443 eat(state, TOK_LBRACKET);
12444 if (peek(state) != TOK_RBRACKET) {
12445 value = constant_expr(state);
12446 integral(state, value);
12447 }
12448 eat(state, TOK_RBRACKET);
12449
12450 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12451 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12452 if (value) {
12453 type->elements = value->u.cval;
12454 free_triple(state, value);
12455 } else {
12456 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12457 op = 0;
12458 }
12459 }
12460 break;
12461 default:
12462 op = 0;
12463 break;
12464 }
12465 } while(op);
12466 if (outer) {
12467 struct type *inner;
12468 arrays_complete(state, type);
12469 FINISHME();
12470 for(inner = outer; inner->left; inner = inner->left)
12471 ;
12472 inner->left = type;
12473 type = outer;
12474 }
12475 return type;
12476}
12477
12478static struct type *declarator(
Stefan Reinauer14e22772010-04-27 06:56:47 +000012479 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012480 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012481{
12482 while(peek(state) == TOK_STAR) {
12483 eat(state, TOK_STAR);
12484 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12485 }
Eric Biederman41203d92004-11-08 09:31:09 +000012486 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012487 return type;
12488}
12489
Eric Biedermanb138ac82003-04-22 18:44:01 +000012490static struct type *typedef_name(
12491 struct compile_state *state, unsigned int specifiers)
12492{
12493 struct hash_entry *ident;
12494 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012495 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012496 type = ident->sym_ident->type;
12497 specifiers |= type->type & QUAL_MASK;
Stefan Reinauer14e22772010-04-27 06:56:47 +000012498 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
Eric Biedermanb138ac82003-04-22 18:44:01 +000012499 (type->type & (STOR_MASK | QUAL_MASK))) {
12500 type = clone_type(specifiers, type);
12501 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012502 return type;
12503}
12504
12505static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012506 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012507{
Eric Biederman83b991a2003-10-11 06:20:25 +000012508 struct hash_entry *ident;
12509 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012510 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012511 struct type *enum_type;
12512 enum_type = 0;
12513 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012514 eat(state, TOK_ENUM);
12515 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012516 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012517 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012518 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012519 base = 0;
12520 if (!ident || (peek(state) == TOK_LBRACE)) {
12521 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012522 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012523 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12524 enum_type->type_ident = ident;
12525 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012526 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012527 struct hash_entry *eident;
12528 struct triple *value;
12529 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012530 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012531 if (eident->sym_ident) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012532 error(state, 0, "%s already declared",
Eric Biederman83b991a2003-10-11 06:20:25 +000012533 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012534 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012535 eident->tok = TOK_ENUM_CONST;
12536 if (peek(state) == TOK_EQ) {
12537 struct triple *val;
12538 eat(state, TOK_EQ);
12539 val = constant_expr(state);
12540 integral(state, val);
12541 base = val->u.cval;
12542 }
12543 value = int_const(state, &int_type, base);
12544 symbol(state, eident, &eident->sym_ident, value, &int_type);
12545 entry = new_type(TYPE_LIST, 0, 0);
12546 entry->field_ident = eident;
12547 *next = entry;
12548 next = &entry->right;
12549 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012550 if (peek(state) == TOK_COMMA) {
12551 eat(state, TOK_COMMA);
12552 }
12553 } while(peek(state) != TOK_RBRACE);
12554 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012555 if (ident) {
12556 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12557 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012558 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012559 if (ident && ident->sym_tag &&
12560 ident->sym_tag->type &&
12561 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12562 enum_type = clone_type(spec, ident->sym_tag->type);
12563 }
12564 else if (ident && !enum_type) {
12565 error(state, 0, "enum %s undeclared", ident->name);
12566 }
12567 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012568}
12569
Eric Biedermanb138ac82003-04-22 18:44:01 +000012570static struct type *struct_declarator(
12571 struct compile_state *state, struct type *type, struct hash_entry **ident)
12572{
Eric Biederman90089602004-05-28 14:11:54 +000012573 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012574 type = declarator(state, type, ident, 1);
12575 }
Eric Biederman90089602004-05-28 14:11:54 +000012576 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012577 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012578 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012579 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012580 if (value->op != OP_INTCONST) {
12581 error(state, 0, "Invalid constant expression");
12582 }
12583 if (value->u.cval > size_of(state, type)) {
12584 error(state, 0, "bitfield larger than base type");
12585 }
12586 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12587 error(state, 0, "bitfield base not an integer type");
12588 }
12589 type = new_type(TYPE_BITFIELD, type, 0);
12590 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012591 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012592 return type;
12593}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012594
12595static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012596 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012597{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012598 struct type *struct_type;
12599 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012600 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012601 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012602 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012603 struct_type = 0;
12604 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012605 switch(peek(state)) {
12606 case TOK_STRUCT:
12607 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012608 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012609 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012610 break;
12611 case TOK_UNION:
12612 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012613 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012614 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012615 break;
12616 default:
12617 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012618 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012619 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012620 break;
12621 }
12622 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012623 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012624 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012625 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012626 if (!ident || (peek(state) == TOK_LBRACE)) {
12627 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012628 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012629 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012630 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012631 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012632 do {
12633 struct type *base_type;
12634 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012635 base_type = specifier_qualifier_list(state);
12636 do {
12637 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012638 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012639 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012640 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012641 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012642 if (peek(state) == TOK_COMMA) {
12643 done = 0;
12644 eat(state, TOK_COMMA);
12645 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012646 type = clone_type(0, type);
12647 type->field_ident = fident;
12648 if (*next) {
12649 *next = new_type(type_join, *next, type);
12650 next = &((*next)->right);
12651 } else {
12652 *next = type;
12653 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012654 } while(!done);
12655 eat(state, TOK_SEMI);
12656 } while(peek(state) != TOK_RBRACE);
12657 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012658 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012659 struct_type->type_ident = ident;
12660 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012661 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012662 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012663 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012664 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000012665 if (ident && ident->sym_tag &&
12666 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012667 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012668 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012669 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012670 else if (ident && !struct_type) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000012671 error(state, 0, "%s %s undeclared",
Eric Biederman90089602004-05-28 14:11:54 +000012672 (type_main == TYPE_STRUCT)?"struct" : "union",
12673 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012674 }
12675 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012676}
12677
12678static unsigned int storage_class_specifier_opt(struct compile_state *state)
12679{
12680 unsigned int specifiers;
12681 switch(peek(state)) {
12682 case TOK_AUTO:
12683 eat(state, TOK_AUTO);
12684 specifiers = STOR_AUTO;
12685 break;
12686 case TOK_REGISTER:
12687 eat(state, TOK_REGISTER);
12688 specifiers = STOR_REGISTER;
12689 break;
12690 case TOK_STATIC:
12691 eat(state, TOK_STATIC);
12692 specifiers = STOR_STATIC;
12693 break;
12694 case TOK_EXTERN:
12695 eat(state, TOK_EXTERN);
12696 specifiers = STOR_EXTERN;
12697 break;
12698 case TOK_TYPEDEF:
12699 eat(state, TOK_TYPEDEF);
12700 specifiers = STOR_TYPEDEF;
12701 break;
12702 default:
12703 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012704 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012705 }
12706 else {
12707 specifiers = STOR_AUTO;
12708 }
12709 }
12710 return specifiers;
12711}
12712
12713static unsigned int function_specifier_opt(struct compile_state *state)
12714{
12715 /* Ignore the inline keyword */
12716 unsigned int specifiers;
12717 specifiers = 0;
12718 switch(peek(state)) {
12719 case TOK_INLINE:
12720 eat(state, TOK_INLINE);
12721 specifiers = STOR_INLINE;
12722 }
12723 return specifiers;
12724}
12725
Eric Biederman90089602004-05-28 14:11:54 +000012726static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12727{
12728 int tok = peek(state);
12729 switch(tok) {
12730 case TOK_COMMA:
12731 case TOK_LPAREN:
12732 /* The empty attribute ignore it */
12733 break;
12734 case TOK_IDENT:
12735 case TOK_ENUM_CONST:
12736 case TOK_TYPE_NAME:
12737 {
12738 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012739 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012740
12741 if (ident == state->i_noinline) {
12742 if (attributes & ATTRIB_ALWAYS_INLINE) {
12743 error(state, 0, "both always_inline and noinline attribtes");
12744 }
12745 attributes |= ATTRIB_NOINLINE;
12746 }
12747 else if (ident == state->i_always_inline) {
12748 if (attributes & ATTRIB_NOINLINE) {
12749 error(state, 0, "both noinline and always_inline attribtes");
12750 }
12751 attributes |= ATTRIB_ALWAYS_INLINE;
12752 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012753 else if (ident == state->i_noreturn) {
12754 // attribute((noreturn)) does nothing (yet?)
12755 }
Stefan Reinauer57cd1dd2012-06-21 17:21:08 -070012756 else if (ident == state->i_unused) {
12757 // attribute((unused)) does nothing (yet?)
12758 }
Eric Biederman90089602004-05-28 14:11:54 +000012759 else {
12760 error(state, 0, "Unknown attribute:%s", ident->name);
12761 }
12762 break;
12763 }
12764 default:
12765 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12766 break;
12767 }
12768 return attributes;
12769}
12770
12771static unsigned int attribute_list(struct compile_state *state, unsigned type)
12772{
12773 type = attrib(state, type);
12774 while(peek(state) == TOK_COMMA) {
12775 eat(state, TOK_COMMA);
12776 type = attrib(state, type);
12777 }
12778 return type;
12779}
12780
12781static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12782{
12783 if (peek(state) == TOK_ATTRIBUTE) {
12784 eat(state, TOK_ATTRIBUTE);
12785 eat(state, TOK_LPAREN);
12786 eat(state, TOK_LPAREN);
12787 type = attribute_list(state, type);
12788 eat(state, TOK_RPAREN);
12789 eat(state, TOK_RPAREN);
12790 }
12791 return type;
12792}
12793
Eric Biedermanb138ac82003-04-22 18:44:01 +000012794static unsigned int type_qualifiers(struct compile_state *state)
12795{
12796 unsigned int specifiers;
12797 int done;
12798 done = 0;
12799 specifiers = QUAL_NONE;
12800 do {
12801 switch(peek(state)) {
12802 case TOK_CONST:
12803 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012804 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012805 break;
12806 case TOK_VOLATILE:
12807 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012808 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012809 break;
12810 case TOK_RESTRICT:
12811 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012812 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012813 break;
12814 default:
12815 done = 1;
12816 break;
12817 }
12818 } while(!done);
12819 return specifiers;
12820}
12821
12822static struct type *type_specifier(
12823 struct compile_state *state, unsigned int spec)
12824{
12825 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012826 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012827 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012828 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012829 case TOK_VOID:
12830 eat(state, TOK_VOID);
12831 type = new_type(TYPE_VOID | spec, 0, 0);
12832 break;
12833 case TOK_CHAR:
12834 eat(state, TOK_CHAR);
12835 type = new_type(TYPE_CHAR | spec, 0, 0);
12836 break;
12837 case TOK_SHORT:
12838 eat(state, TOK_SHORT);
12839 if (peek(state) == TOK_INT) {
12840 eat(state, TOK_INT);
12841 }
12842 type = new_type(TYPE_SHORT | spec, 0, 0);
12843 break;
12844 case TOK_INT:
12845 eat(state, TOK_INT);
12846 type = new_type(TYPE_INT | spec, 0, 0);
12847 break;
12848 case TOK_LONG:
12849 eat(state, TOK_LONG);
12850 switch(peek(state)) {
12851 case TOK_LONG:
12852 eat(state, TOK_LONG);
12853 error(state, 0, "long long not supported");
12854 break;
12855 case TOK_DOUBLE:
12856 eat(state, TOK_DOUBLE);
12857 error(state, 0, "long double not supported");
12858 break;
12859 case TOK_INT:
12860 eat(state, TOK_INT);
12861 type = new_type(TYPE_LONG | spec, 0, 0);
12862 break;
12863 default:
12864 type = new_type(TYPE_LONG | spec, 0, 0);
12865 break;
12866 }
12867 break;
12868 case TOK_FLOAT:
12869 eat(state, TOK_FLOAT);
12870 error(state, 0, "type float not supported");
12871 break;
12872 case TOK_DOUBLE:
12873 eat(state, TOK_DOUBLE);
12874 error(state, 0, "type double not supported");
12875 break;
12876 case TOK_SIGNED:
12877 eat(state, TOK_SIGNED);
12878 switch(peek(state)) {
12879 case TOK_LONG:
12880 eat(state, TOK_LONG);
12881 switch(peek(state)) {
12882 case TOK_LONG:
12883 eat(state, TOK_LONG);
12884 error(state, 0, "type long long not supported");
12885 break;
12886 case TOK_INT:
12887 eat(state, TOK_INT);
12888 type = new_type(TYPE_LONG | spec, 0, 0);
12889 break;
12890 default:
12891 type = new_type(TYPE_LONG | spec, 0, 0);
12892 break;
12893 }
12894 break;
12895 case TOK_INT:
12896 eat(state, TOK_INT);
12897 type = new_type(TYPE_INT | spec, 0, 0);
12898 break;
12899 case TOK_SHORT:
12900 eat(state, TOK_SHORT);
12901 type = new_type(TYPE_SHORT | spec, 0, 0);
12902 break;
12903 case TOK_CHAR:
12904 eat(state, TOK_CHAR);
12905 type = new_type(TYPE_CHAR | spec, 0, 0);
12906 break;
12907 default:
12908 type = new_type(TYPE_INT | spec, 0, 0);
12909 break;
12910 }
12911 break;
12912 case TOK_UNSIGNED:
12913 eat(state, TOK_UNSIGNED);
12914 switch(peek(state)) {
12915 case TOK_LONG:
12916 eat(state, TOK_LONG);
12917 switch(peek(state)) {
12918 case TOK_LONG:
12919 eat(state, TOK_LONG);
12920 error(state, 0, "unsigned long long not supported");
12921 break;
12922 case TOK_INT:
12923 eat(state, TOK_INT);
12924 type = new_type(TYPE_ULONG | spec, 0, 0);
12925 break;
12926 default:
12927 type = new_type(TYPE_ULONG | spec, 0, 0);
12928 break;
12929 }
12930 break;
12931 case TOK_INT:
12932 eat(state, TOK_INT);
12933 type = new_type(TYPE_UINT | spec, 0, 0);
12934 break;
12935 case TOK_SHORT:
12936 eat(state, TOK_SHORT);
12937 type = new_type(TYPE_USHORT | spec, 0, 0);
12938 break;
12939 case TOK_CHAR:
12940 eat(state, TOK_CHAR);
12941 type = new_type(TYPE_UCHAR | spec, 0, 0);
12942 break;
12943 default:
12944 type = new_type(TYPE_UINT | spec, 0, 0);
12945 break;
12946 }
12947 break;
12948 /* struct or union specifier */
12949 case TOK_STRUCT:
12950 case TOK_UNION:
12951 type = struct_or_union_specifier(state, spec);
12952 break;
12953 /* enum-spefifier */
12954 case TOK_ENUM:
12955 type = enum_specifier(state, spec);
12956 break;
12957 /* typedef name */
12958 case TOK_TYPE_NAME:
12959 type = typedef_name(state, spec);
12960 break;
12961 default:
Stefan Reinauer14e22772010-04-27 06:56:47 +000012962 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012963 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012964 break;
12965 }
12966 return type;
12967}
12968
12969static int istype(int tok)
12970{
12971 switch(tok) {
12972 case TOK_CONST:
12973 case TOK_RESTRICT:
12974 case TOK_VOLATILE:
12975 case TOK_VOID:
12976 case TOK_CHAR:
12977 case TOK_SHORT:
12978 case TOK_INT:
12979 case TOK_LONG:
12980 case TOK_FLOAT:
12981 case TOK_DOUBLE:
12982 case TOK_SIGNED:
12983 case TOK_UNSIGNED:
12984 case TOK_STRUCT:
12985 case TOK_UNION:
12986 case TOK_ENUM:
12987 case TOK_TYPE_NAME:
12988 return 1;
12989 default:
12990 return 0;
12991 }
12992}
12993
12994
12995static struct type *specifier_qualifier_list(struct compile_state *state)
12996{
12997 struct type *type;
12998 unsigned int specifiers = 0;
12999
13000 /* type qualifiers */
13001 specifiers |= type_qualifiers(state);
13002
13003 /* type specifier */
13004 type = type_specifier(state, specifiers);
13005
13006 return type;
13007}
13008
Stefan Reinauer50542a82007-10-24 11:14:14 +000013009#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013010static int isdecl_specifier(int tok)
13011{
13012 switch(tok) {
13013 /* storage class specifier */
13014 case TOK_AUTO:
13015 case TOK_REGISTER:
13016 case TOK_STATIC:
13017 case TOK_EXTERN:
13018 case TOK_TYPEDEF:
13019 /* type qualifier */
13020 case TOK_CONST:
13021 case TOK_RESTRICT:
13022 case TOK_VOLATILE:
13023 /* type specifiers */
13024 case TOK_VOID:
13025 case TOK_CHAR:
13026 case TOK_SHORT:
13027 case TOK_INT:
13028 case TOK_LONG:
13029 case TOK_FLOAT:
13030 case TOK_DOUBLE:
13031 case TOK_SIGNED:
13032 case TOK_UNSIGNED:
13033 /* struct or union specifier */
13034 case TOK_STRUCT:
13035 case TOK_UNION:
13036 /* enum-spefifier */
13037 case TOK_ENUM:
13038 /* typedef name */
13039 case TOK_TYPE_NAME:
13040 /* function specifiers */
13041 case TOK_INLINE:
13042 return 1;
13043 default:
13044 return 0;
13045 }
13046}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013047#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013048
13049static struct type *decl_specifiers(struct compile_state *state)
13050{
13051 struct type *type;
13052 unsigned int specifiers;
13053 /* I am overly restrictive in the arragement of specifiers supported.
13054 * C is overly flexible in this department it makes interpreting
13055 * the parse tree difficult.
13056 */
13057 specifiers = 0;
13058
13059 /* storage class specifier */
13060 specifiers |= storage_class_specifier_opt(state);
13061
13062 /* function-specifier */
13063 specifiers |= function_specifier_opt(state);
13064
Eric Biederman90089602004-05-28 14:11:54 +000013065 /* attributes */
13066 specifiers |= attributes_opt(state, 0);
13067
Eric Biedermanb138ac82003-04-22 18:44:01 +000013068 /* type qualifier */
13069 specifiers |= type_qualifiers(state);
13070
13071 /* type specifier */
13072 type = type_specifier(state, specifiers);
13073 return type;
13074}
13075
Eric Biederman00443072003-06-24 12:34:45 +000013076struct field_info {
13077 struct type *type;
13078 size_t offset;
13079};
13080
13081static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013082{
13083 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013084 struct field_info info;
13085 info.offset = ~0U;
13086 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013087 do {
13088 switch(peek(state)) {
13089 case TOK_LBRACKET:
13090 {
13091 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013092 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13093 error(state, 0, "Array designator not in array initializer");
13094 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013095 eat(state, TOK_LBRACKET);
13096 value = constant_expr(state);
13097 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013098
13099 info.type = type->left;
13100 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013101 break;
13102 }
13103 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013104 {
13105 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013106 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13107 ((type->type & TYPE_MASK) != TYPE_UNION))
13108 {
Eric Biederman00443072003-06-24 12:34:45 +000013109 error(state, 0, "Struct designator not in struct initializer");
13110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013111 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013112 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013113 info.offset = field_offset(state, type, field);
13114 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013115 break;
Eric Biederman00443072003-06-24 12:34:45 +000013116 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013117 default:
13118 error(state, 0, "Invalid designator");
13119 }
13120 tok = peek(state);
13121 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13122 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013123 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013124}
13125
13126static struct triple *initializer(
13127 struct compile_state *state, struct type *type)
13128{
13129 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013130#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013131#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013132#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013133 if (peek(state) != TOK_LBRACE) {
13134 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013135 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13136 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13137 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13138 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13139 (equiv_types(type->left, result->type->left))) {
13140 type->elements = result->type->elements;
13141 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013142 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013143 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13144 (type->type & TYPE_MASK) != TYPE_ARRAY)
13145 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013146 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013147 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013148 if (!is_init_compatible(state, type, result->type)) {
13149 error(state, 0, "Incompatible types in initializer");
13150 }
13151 if (!equiv_types(type, result->type)) {
13152 result = mk_cast_expr(state, type, result);
13153 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013154 }
13155 else {
13156 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013157 size_t max_offset;
13158 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013159 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013160 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13161 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13162 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013163 }
Eric Biederman00443072003-06-24 12:34:45 +000013164 info.offset = 0;
13165 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013166 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13167 info.type = next_field(state, type, 0);
13168 }
Eric Biederman00443072003-06-24 12:34:45 +000013169 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13170 max_offset = 0;
13171 } else {
13172 max_offset = size_of(state, type);
13173 }
Eric Biederman90089602004-05-28 14:11:54 +000013174 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013175 eat(state, TOK_LBRACE);
13176 do {
13177 struct triple *value;
13178 struct type *value_type;
13179 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013180 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013181 int tok;
13182 comma = 0;
13183 tok = peek(state);
13184 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013185 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013186 }
Eric Biederman00443072003-06-24 12:34:45 +000013187 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13188 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013189 error(state, 0, "element beyond bounds");
13190 }
Eric Biederman00443072003-06-24 12:34:45 +000013191 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013192 value = eval_const_expr(state, initializer(state, value_type));
13193 value_size = size_of(state, value_type);
13194 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013195 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13196 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013197 void *old_buf;
13198 size_t old_size;
13199 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013200 old_size = max_offset;
13201 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013202 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13203 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013204 xfree(old_buf);
13205 }
Eric Biederman90089602004-05-28 14:11:54 +000013206 dest = ((char *)buf) + bits_to_bytes(info.offset);
13207#if DEBUG_INITIALIZER
Stefan Reinauer14e22772010-04-27 06:56:47 +000013208 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000013209 dest - buf,
13210 bits_to_bytes(max_offset),
13211 bits_to_bytes(value_size),
13212 value->op);
13213#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013214 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013215 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
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_I8)) {
13218#if DEBUG_INITIALIZER
13219 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13220#endif
Eric Biederman00443072003-06-24 12:34:45 +000013221 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013222 }
Eric Biederman90089602004-05-28 14:11:54 +000013223 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013224 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013225 }
Eric Biederman90089602004-05-28 14:11:54 +000013226 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013227 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013228 }
13229 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013230 internal_error(state, 0, "unhandled constant initializer");
13231 }
Eric Biederman00443072003-06-24 12:34:45 +000013232 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013233 if (peek(state) == TOK_COMMA) {
13234 eat(state, TOK_COMMA);
13235 comma = 1;
13236 }
Eric Biederman00443072003-06-24 12:34:45 +000013237 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013238 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13239 info.type = next_field(state, type, info.type);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013240 info.offset = field_offset(state, type,
Eric Biederman03b59862003-06-24 14:27:37 +000013241 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013242 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013243 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013244 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13245 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13246 type->elements = max_offset / size_of(state, type->left);
13247 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013248 eat(state, TOK_RBRACE);
13249 result = triple(state, OP_BLOBCONST, type, 0, 0);
13250 result->u.blob = buf;
13251 }
13252 return result;
13253}
13254
Eric Biederman90089602004-05-28 14:11:54 +000013255static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013256{
13257 /* Make a second pass and finish anything outstanding
13258 * with respect to branches. The only outstanding item
13259 * is to see if there are goto to labels that have not
13260 * been defined and to error about them.
13261 */
13262 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013263 struct triple *ins;
13264 /* Also error on branches that do not use their targets */
13265 ins = first;
13266 do {
13267 if (!triple_is_ret(state, ins)) {
13268 struct triple **expr ;
13269 struct triple_set *set;
13270 expr = triple_targ(state, ins, 0);
13271 for(; expr; expr = triple_targ(state, ins, expr)) {
13272 struct triple *targ;
13273 targ = *expr;
13274 for(set = targ?targ->use:0; set; set = set->next) {
13275 if (set->member == ins) {
13276 break;
13277 }
13278 }
13279 if (!set) {
13280 internal_error(state, ins, "targ not used");
13281 }
13282 }
13283 }
13284 ins = ins->next;
13285 } while(ins != first);
13286 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013287 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13288 struct hash_entry *entry;
13289 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13290 struct triple *ins;
13291 if (!entry->sym_label) {
13292 continue;
13293 }
13294 ins = entry->sym_label->def;
13295 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13296 error(state, ins, "label `%s' used but not defined",
13297 entry->name);
13298 }
13299 }
13300 }
13301}
13302
Eric Biedermanb138ac82003-04-22 18:44:01 +000013303static struct triple *function_definition(
13304 struct compile_state *state, struct type *type)
13305{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013306 struct triple *def, *tmp, *first, *end, *retvar, *ret;
Eric Biederman90089602004-05-28 14:11:54 +000013307 struct triple *fname;
13308 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013309 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013310 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013311 int i;
13312 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13313 error(state, 0, "Invalid function header");
13314 }
13315
13316 /* Verify the function type */
13317 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13318 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013319 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013320 error(state, 0, "Invalid function parameters");
13321 }
13322 param = type->right;
13323 i = 0;
13324 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13325 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013326 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013327 error(state, 0, "No identifier for parameter %d\n", i);
13328 }
13329 param = param->right;
13330 }
13331 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013332 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013333 error(state, 0, "No identifier for paramter %d\n", i);
13334 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013335
Eric Biedermanb138ac82003-04-22 18:44:01 +000013336 /* Get a list of statements for this function. */
13337 def = triple(state, OP_LIST, type, 0, 0);
13338
13339 /* Start a new scope for the passed parameters */
13340 start_scope(state);
13341
13342 /* Put a label at the very start of a function */
13343 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013344 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013345
13346 /* Put a label at the very end of a function */
13347 end = label(state);
13348 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013349 /* Remember where return goes */
13350 ident = state->i_return;
13351 symbol(state, ident, &ident->sym_ident, end, end->type);
13352
Eric Biederman90089602004-05-28 14:11:54 +000013353 /* Get the initial closure type */
13354 ctype = new_type(TYPE_JOIN, &void_type, 0);
13355 ctype->elements = 1;
13356
13357 /* Add a variable for the return value */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013358 crtype = new_type(TYPE_TUPLE,
Eric Biederman90089602004-05-28 14:11:54 +000013359 /* Remove all type qualifiers from the return type */
13360 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13361 crtype->elements = 2;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010013362 flatten(state, end, variable(state, crtype));
Eric Biederman90089602004-05-28 14:11:54 +000013363
Eric Biederman5ade04a2003-10-22 04:03:46 +000013364 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013365 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013366
13367 /* Add in the return instruction */
13368 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13369 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013370
13371 /* Walk through the parameters and create symbol table entries
13372 * for them.
13373 */
13374 param = type->right;
13375 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013376 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013377 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013378 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013379 flatten(state, end, tmp);
13380 param = param->right;
13381 }
13382 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13383 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013384 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013385 tmp = variable(state, param);
13386 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13387 flatten(state, end, tmp);
13388 }
Eric Biederman90089602004-05-28 14:11:54 +000013389
13390 /* Add the declaration static const char __func__ [] = "func-name" */
Stefan Reinauer14e22772010-04-27 06:56:47 +000013391 fname_type = new_type(TYPE_ARRAY,
Eric Biederman90089602004-05-28 14:11:54 +000013392 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13393 fname_type->type |= QUAL_CONST | STOR_STATIC;
13394 fname_type->elements = strlen(state->function) + 1;
13395
13396 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13397 fname->u.blob = (void *)state->function;
13398 fname = flatten(state, end, fname);
13399
13400 ident = state->i___func__;
13401 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013402
13403 /* Remember which function I am compiling.
13404 * Also assume the last defined function is the main function.
13405 */
13406 state->main_function = def;
13407
13408 /* Now get the actual function definition */
13409 compound_statement(state, end);
13410
Eric Biederman153ea352003-06-20 14:43:20 +000013411 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013412 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013413
Eric Biedermanb138ac82003-04-22 18:44:01 +000013414 /* Remove the parameter scope */
13415 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013416
Eric Biederman5ade04a2003-10-22 04:03:46 +000013417
13418 /* Remember I have defined a function */
13419 if (!state->functions) {
13420 state->functions = def;
13421 } else {
13422 insert_triple(state, state->functions, def);
13423 }
13424 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013425 FILE *fp = state->dbgout;
13426 fprintf(fp, "\n");
13427 loc(fp, state, 0);
13428 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13429 display_func(state, fp, def);
13430 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013431 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013432
13433 return def;
13434}
13435
Stefan Reinauer14e22772010-04-27 06:56:47 +000013436static struct triple *do_decl(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013437 struct type *type, struct hash_entry *ident)
13438{
13439 struct triple *def;
13440 def = 0;
13441 /* Clean up the storage types used */
13442 switch (type->type & STOR_MASK) {
13443 case STOR_AUTO:
13444 case STOR_STATIC:
13445 /* These are the good types I am aiming for */
13446 break;
13447 case STOR_REGISTER:
13448 type->type &= ~STOR_MASK;
13449 type->type |= STOR_AUTO;
13450 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013451 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013452 case STOR_EXTERN:
13453 type->type &= ~STOR_MASK;
13454 type->type |= STOR_STATIC;
13455 break;
13456 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013457 if (!ident) {
13458 error(state, 0, "typedef without name");
13459 }
13460 symbol(state, ident, &ident->sym_ident, 0, type);
13461 ident->tok = TOK_TYPE_NAME;
13462 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013463 break;
13464 default:
13465 internal_error(state, 0, "Undefined storage class");
13466 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013467 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13468 error(state, 0, "Function prototypes not supported");
13469 }
Eric W. Biederman8483b702010-03-17 00:23:34 +000013470 if (ident &&
13471 ((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13472 ((type->type & STOR_MASK) != STOR_STATIC))
13473 error(state, 0, "non static arrays not supported");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013474 if (ident &&
Eric Biederman00443072003-06-24 12:34:45 +000013475 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013476 ((type->type & QUAL_CONST) == 0)) {
13477 error(state, 0, "non const static variables not supported");
13478 }
13479 if (ident) {
13480 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013481 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013482 }
13483 return def;
13484}
13485
13486static void decl(struct compile_state *state, struct triple *first)
13487{
13488 struct type *base_type, *type;
13489 struct hash_entry *ident;
13490 struct triple *def;
13491 int global;
13492 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13493 base_type = decl_specifiers(state);
13494 ident = 0;
13495 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013496 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013497 if (global && ident && (peek(state) == TOK_LBRACE)) {
13498 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013499 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013500 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013501 def = function_definition(state, type);
13502 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013503 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013504 }
13505 else {
13506 int done;
13507 flatten(state, first, do_decl(state, type, ident));
13508 /* type or variable definition */
13509 do {
13510 done = 1;
13511 if (peek(state) == TOK_EQ) {
13512 if (!ident) {
13513 error(state, 0, "cannot assign to a type");
13514 }
13515 eat(state, TOK_EQ);
13516 flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013517 init_expr(state,
13518 ident->sym_ident->def,
Eric Biedermanb138ac82003-04-22 18:44:01 +000013519 initializer(state, type)));
13520 }
13521 arrays_complete(state, type);
13522 if (peek(state) == TOK_COMMA) {
13523 eat(state, TOK_COMMA);
13524 ident = 0;
13525 type = declarator(state, base_type, &ident, 0);
13526 flatten(state, first, do_decl(state, type, ident));
13527 done = 0;
13528 }
13529 } while(!done);
13530 eat(state, TOK_SEMI);
13531 }
13532}
13533
13534static void decls(struct compile_state *state)
13535{
13536 struct triple *list;
13537 int tok;
13538 list = label(state);
13539 while(1) {
13540 tok = peek(state);
13541 if (tok == TOK_EOF) {
13542 return;
13543 }
13544 if (tok == TOK_SPACE) {
13545 eat(state, TOK_SPACE);
13546 }
13547 decl(state, list);
13548 if (list->next != list) {
13549 error(state, 0, "global variables not supported");
13550 }
13551 }
13552}
13553
Stefan Reinauer14e22772010-04-27 06:56:47 +000013554/*
Eric Biederman5ade04a2003-10-22 04:03:46 +000013555 * Function inlining
13556 */
Eric Biederman90089602004-05-28 14:11:54 +000013557struct triple_reg_set {
13558 struct triple_reg_set *next;
13559 struct triple *member;
13560 struct triple *new;
13561};
13562struct reg_block {
13563 struct block *block;
13564 struct triple_reg_set *in;
13565 struct triple_reg_set *out;
13566 int vertex;
13567};
13568static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13569static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13570static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13571static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13572static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13573 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13574 void *arg);
13575static void print_block(
13576 struct compile_state *state, struct block *block, void *arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013577static int do_triple_set(struct triple_reg_set **head,
Eric Biederman90089602004-05-28 14:11:54 +000013578 struct triple *member, struct triple *new_member);
13579static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13580static struct reg_block *compute_variable_lifetimes(
13581 struct compile_state *state, struct basic_blocks *bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013582static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013583 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013584#if DEBUG_EXPLICIT_CLOSURES
Stefan Reinauer14e22772010-04-27 06:56:47 +000013585static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013586 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013587#endif
Eric Biederman90089602004-05-28 14:11:54 +000013588
Eric Biederman5ade04a2003-10-22 04:03:46 +000013589
13590static struct triple *call(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000013591 struct triple *retvar, struct triple *ret_addr,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013592 struct triple *targ, struct triple *ret)
13593{
13594 struct triple *call;
13595
13596 if (!retvar || !is_lvalue(state, retvar)) {
13597 internal_error(state, 0, "writing to a non lvalue?");
13598 }
13599 write_compatible(state, retvar->type, &void_ptr_type);
13600
13601 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13602 TARG(call, 0) = targ;
13603 MISC(call, 0) = ret;
13604 if (!targ || (targ->op != OP_LABEL)) {
13605 internal_error(state, 0, "call not to a label");
13606 }
13607 if (!ret || (ret->op != OP_RET)) {
13608 internal_error(state, 0, "call not matched with return");
13609 }
13610 return call;
13611}
13612
Eric Biederman5ade04a2003-10-22 04:03:46 +000013613static void walk_functions(struct compile_state *state,
13614 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13615 void *arg)
13616{
13617 struct triple *func, *first;
13618 func = first = state->functions;
13619 do {
13620 cb(state, func, arg);
13621 func = func->next;
13622 } while(func != first);
13623}
13624
Eric Biederman90089602004-05-28 14:11:54 +000013625static void reverse_walk_functions(struct compile_state *state,
13626 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13627 void *arg)
13628{
13629 struct triple *func, *first;
13630 func = first = state->functions;
13631 do {
13632 func = func->prev;
13633 cb(state, func, arg);
13634 } while(func != first);
13635}
13636
13637
13638static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13639{
13640 struct triple *ptr, *first;
13641 if (func->u.cval == 0) {
13642 return;
13643 }
13644 ptr = first = RHS(func, 0);
13645 do {
13646 if (ptr->op == OP_FCALL) {
13647 struct triple *called_func;
13648 called_func = MISC(ptr, 0);
13649 /* Mark the called function as used */
13650 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13651 called_func->u.cval++;
13652 }
13653 /* Remove the called function from the list */
13654 called_func->prev->next = called_func->next;
13655 called_func->next->prev = called_func->prev;
13656
13657 /* Place the called function before me on the list */
13658 called_func->next = func;
13659 called_func->prev = func->prev;
13660 called_func->prev->next = called_func;
13661 called_func->next->prev = called_func;
13662 }
13663 ptr = ptr->next;
13664 } while(ptr != first);
13665 func->id |= TRIPLE_FLAG_FLATTENED;
13666}
13667
13668static void mark_live_functions(struct compile_state *state)
13669{
Stefan Reinauer14e22772010-04-27 06:56:47 +000013670 /* Ensure state->main_function is the last function in
Eric Biederman90089602004-05-28 14:11:54 +000013671 * the list of functions.
13672 */
13673 if ((state->main_function->next != state->functions) ||
13674 (state->functions->prev != state->main_function)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013675 internal_error(state, 0,
Eric Biederman90089602004-05-28 14:11:54 +000013676 "state->main_function is not at the end of the function list ");
13677 }
13678 state->main_function->u.cval = 1;
13679 reverse_walk_functions(state, mark_live, 0);
13680}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013681
Stefan Reinauer14e22772010-04-27 06:56:47 +000013682static int local_triple(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013683 struct triple *func, struct triple *ins)
13684{
13685 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13686#if 0
13687 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013688 FILE *fp = state->errout;
13689 fprintf(fp, "global: ");
13690 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013691 }
13692#endif
13693 return local;
13694}
13695
Stefan Reinauer14e22772010-04-27 06:56:47 +000013696struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000013697 struct occurance *base_occurance)
13698{
13699 struct triple *nfunc;
13700 struct triple *nfirst, *ofirst;
13701 struct triple *new, *old;
13702
13703 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013704 FILE *fp = state->dbgout;
13705 fprintf(fp, "\n");
13706 loc(fp, state, 0);
13707 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13708 display_func(state, fp, ofunc);
13709 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013710 }
13711
13712 /* Make a new copy of the old function */
13713 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13714 nfirst = 0;
13715 ofirst = old = RHS(ofunc, 0);
13716 do {
13717 struct triple *new;
13718 struct occurance *occurance;
13719 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013720 old_lhs = old->lhs;
13721 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013722 occurance = inline_occurance(state, base_occurance, old->occurance);
13723 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13724 MISC(old, 0)->u.cval += 1;
13725 }
13726 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13727 occurance);
13728 if (!triple_stores_block(state, new)) {
13729 memcpy(&new->u, &old->u, sizeof(new->u));
13730 }
13731 if (!nfirst) {
13732 RHS(nfunc, 0) = nfirst = new;
13733 }
13734 else {
13735 insert_triple(state, nfirst, new);
13736 }
13737 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013738 new->id |= old->id & TRIPLE_FLAG_COPY;
Stefan Reinauer14e22772010-04-27 06:56:47 +000013739
Eric Biederman5ade04a2003-10-22 04:03:46 +000013740 /* During the copy remember new as user of old */
13741 use_triple(old, new);
13742
Eric Biederman5ade04a2003-10-22 04:03:46 +000013743 /* Remember which instructions are local */
13744 old->id |= TRIPLE_FLAG_LOCAL;
13745 old = old->next;
13746 } while(old != ofirst);
13747
13748 /* Make a second pass to fix up any unresolved references */
13749 old = ofirst;
13750 new = nfirst;
13751 do {
13752 struct triple **oexpr, **nexpr;
13753 int count, i;
13754 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013755 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013756 for(i = 0; i < count; i++) {
13757 oexpr = &old->param[i];
13758 nexpr = &new->param[i];
13759 if (*oexpr && !*nexpr) {
13760 if (!local_triple(state, ofunc, *oexpr)) {
13761 *nexpr = *oexpr;
13762 }
13763 else if ((*oexpr)->use) {
13764 *nexpr = (*oexpr)->use->member;
13765 }
13766 if (*nexpr == old) {
13767 internal_error(state, 0, "new == old?");
13768 }
13769 use_triple(*nexpr, new);
13770 }
13771 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013772 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013773 }
13774 }
13775 old = old->next;
13776 new = new->next;
13777 } while((old != ofirst) && (new != nfirst));
Stefan Reinauer14e22772010-04-27 06:56:47 +000013778
Eric Biederman5ade04a2003-10-22 04:03:46 +000013779 /* Make a third pass to cleanup the extra useses */
13780 old = ofirst;
13781 new = nfirst;
13782 do {
13783 unuse_triple(old, new);
13784 /* Forget which instructions are local */
13785 old->id &= ~TRIPLE_FLAG_LOCAL;
13786 old = old->next;
13787 new = new->next;
13788 } while ((old != ofirst) && (new != nfirst));
13789 return nfunc;
13790}
13791
Eric Biederman90089602004-05-28 14:11:54 +000013792static void expand_inline_call(
13793 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013794{
13795 /* Inline the function call */
13796 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013797 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013798 struct triple *end, *nend;
13799 int pvals, i;
13800
13801 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013802 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013803 if (ofunc->op != OP_LIST) {
13804 internal_error(state, 0, "improper function");
13805 }
Eric Biederman90089602004-05-28 14:11:54 +000013806 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013807 /* Prepend the parameter reading into the new function list */
13808 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013809 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013810 for(i = 0; i < pvals; i++) {
13811 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013812 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013813 atype = ptype;
13814 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13815 atype = ptype->left;
13816 }
Eric Biederman90089602004-05-28 14:11:54 +000013817 param = farg(state, nfunc, i);
13818 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13819 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013820 }
Eric Biederman90089602004-05-28 14:11:54 +000013821 arg = RHS(fcall, i);
13822 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013823 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013824 }
13825 result = 0;
13826 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000013827 result = read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000013828 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013829 }
13830 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013831 FILE *fp = state->dbgout;
13832 fprintf(fp, "\n");
13833 loc(fp, state, 0);
13834 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13835 display_func(state, fp, nfunc);
13836 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013837 }
13838
Stefan Reinauer14e22772010-04-27 06:56:47 +000013839 /*
13840 * Get rid of the extra triples
Eric Biederman90089602004-05-28 14:11:54 +000013841 */
13842 /* Remove the read of the return address */
13843 ins = RHS(nfunc, 0)->prev->prev;
13844 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13845 internal_error(state, ins, "Not return addres read?");
13846 }
13847 release_triple(state, ins);
13848 /* Remove the return instruction */
13849 ins = RHS(nfunc, 0)->prev;
13850 if (ins->op != OP_RET) {
13851 internal_error(state, ins, "Not return?");
13852 }
13853 release_triple(state, ins);
13854 /* Remove the retaddres variable */
13855 retvar = fretaddr(state, nfunc);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013856 if ((retvar->lhs != 1) ||
Eric Biederman90089602004-05-28 14:11:54 +000013857 (retvar->op != OP_ADECL) ||
13858 (retvar->next->op != OP_PIECE) ||
13859 (MISC(retvar->next, 0) != retvar)) {
13860 internal_error(state, retvar, "Not the return address?");
13861 }
13862 release_triple(state, retvar->next);
13863 release_triple(state, retvar);
13864
13865 /* Remove the label at the start of the function */
13866 ins = RHS(nfunc, 0);
13867 if (ins->op != OP_LABEL) {
13868 internal_error(state, ins, "Not label?");
13869 }
13870 nfirst = ins->next;
13871 free_triple(state, ins);
13872 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013873 RHS(nfunc, 0) = 0;
13874 free_triple(state, nfunc);
13875
13876 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013877 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013878 nend = nfirst->prev;
13879 end->next = nfirst;
13880 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013881 nend->next = fcall;
13882 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013883
Eric Biederman90089602004-05-28 14:11:54 +000013884 /* Now the result reading code */
13885 if (result) {
13886 result = flatten(state, fcall, result);
13887 propogate_use(state, fcall, result);
13888 }
13889
13890 /* Release the original fcall instruction */
13891 release_triple(state, fcall);
13892
13893 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013894}
13895
Eric Biederman90089602004-05-28 14:11:54 +000013896/*
13897 *
13898 * Type of the result variable.
Stefan Reinauer14e22772010-04-27 06:56:47 +000013899 *
Eric Biederman90089602004-05-28 14:11:54 +000013900 * result
13901 * |
13902 * +----------+------------+
13903 * | |
13904 * union of closures result_type
13905 * |
13906 * +------------------+---------------+
13907 * | |
13908 * closure1 ... closuerN
Stefan Reinauer14e22772010-04-27 06:56:47 +000013909 * | |
Eric Biederman90089602004-05-28 14:11:54 +000013910 * +----+--+-+--------+-----+ +----+----+---+-----+
13911 * | | | | | | | | |
13912 * var1 var2 var3 ... varN result var1 var2 ... varN result
13913 * |
13914 * +--------+---------+
13915 * | |
13916 * union of closures result_type
13917 * |
13918 * +-----+-------------------+
13919 * | |
13920 * closure1 ... closureN
13921 * | |
13922 * +-----+---+----+----+ +----+---+----+-----+
13923 * | | | | | | | |
13924 * var1 var2 ... varN result var1 var2 ... varN result
13925 */
13926
Stefan Reinauer14e22772010-04-27 06:56:47 +000013927static int add_closure_type(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000013928 struct triple *func, struct type *closure_type)
13929{
13930 struct type *type, *ctype, **next;
13931 struct triple *var, *new_var;
13932 int i;
13933
13934#if 0
13935 FILE *fp = state->errout;
13936 fprintf(fp, "original_type: ");
13937 name_of(fp, fresult(state, func)->type);
13938 fprintf(fp, "\n");
13939#endif
13940 /* find the original type */
13941 var = fresult(state, func);
13942 type = var->type;
13943 if (type->elements != 2) {
13944 internal_error(state, var, "bad return type");
13945 }
13946
13947 /* Find the complete closure type and update it */
13948 ctype = type->left->left;
13949 next = &ctype->left;
13950 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13951 next = &(*next)->right;
13952 }
13953 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13954 ctype->elements += 1;
13955
13956#if 0
13957 fprintf(fp, "new_type: ");
13958 name_of(fp, type);
13959 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000013960 fprintf(fp, "ctype: %p %d bits: %d ",
Eric Biederman90089602004-05-28 14:11:54 +000013961 ctype, ctype->elements, reg_size_of(state, ctype));
13962 name_of(fp, ctype);
13963 fprintf(fp, "\n");
13964#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000013965
Eric Biederman90089602004-05-28 14:11:54 +000013966 /* Regenerate the variable with the new type definition */
13967 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13968 new_var->id |= TRIPLE_FLAG_FLATTENED;
13969 for(i = 0; i < new_var->lhs; i++) {
13970 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13971 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000013972
Eric Biederman90089602004-05-28 14:11:54 +000013973 /* Point everyone at the new variable */
13974 propogate_use(state, var, new_var);
13975
13976 /* Release the original variable */
13977 for(i = 0; i < var->lhs; i++) {
13978 release_triple(state, LHS(var, i));
13979 }
13980 release_triple(state, var);
Stefan Reinauer14e22772010-04-27 06:56:47 +000013981
Eric Biederman90089602004-05-28 14:11:54 +000013982 /* Return the index of the added closure type */
13983 return ctype->elements - 1;
13984}
13985
13986static struct triple *closure_expr(struct compile_state *state,
13987 struct triple *func, int closure_idx, int var_idx)
13988{
13989 return deref_index(state,
13990 deref_index(state,
13991 deref_index(state, fresult(state, func), 0),
13992 closure_idx),
13993 var_idx);
13994}
13995
13996
13997static void insert_triple_set(
13998 struct triple_reg_set **head, struct triple *member)
13999{
14000 struct triple_reg_set *new;
14001 new = xcmalloc(sizeof(*new), "triple_set");
14002 new->member = member;
14003 new->new = 0;
14004 new->next = *head;
14005 *head = new;
14006}
14007
14008static int ordered_triple_set(
14009 struct triple_reg_set **head, struct triple *member)
14010{
14011 struct triple_reg_set **ptr;
14012 if (!member)
14013 return 0;
14014 ptr = head;
14015 while(*ptr) {
14016 if (member == (*ptr)->member) {
14017 return 0;
14018 }
14019 /* keep the list ordered */
14020 if (member->id < (*ptr)->member->id) {
14021 break;
14022 }
14023 ptr = &(*ptr)->next;
14024 }
14025 insert_triple_set(ptr, member);
14026 return 1;
14027}
14028
14029
14030static void free_closure_variables(struct compile_state *state,
14031 struct triple_reg_set **enclose)
14032{
14033 struct triple_reg_set *entry, *next;
14034 for(entry = *enclose; entry; entry = next) {
14035 next = entry->next;
14036 do_triple_unset(enclose, entry->member);
14037 }
14038}
14039
14040static int lookup_closure_index(struct compile_state *state,
14041 struct triple *me, struct triple *val)
14042{
14043 struct triple *first, *ins, *next;
14044 first = RHS(me, 0);
14045 ins = next = first;
14046 do {
14047 struct triple *result;
14048 struct triple *index0, *index1, *index2, *read, *write;
14049 ins = next;
14050 next = ins->next;
14051 if (ins->op != OP_CALL) {
14052 continue;
14053 }
14054 /* I am at a previous call point examine it closely */
14055 if (ins->next->op != OP_LABEL) {
14056 internal_error(state, ins, "call not followed by label");
14057 }
14058 /* Does this call does not enclose any variables? */
14059 if ((ins->next->next->op != OP_INDEX) ||
14060 (ins->next->next->u.cval != 0) ||
14061 (result = MISC(ins->next->next, 0)) ||
14062 (result->id & TRIPLE_FLAG_LOCAL)) {
14063 continue;
14064 }
14065 index0 = ins->next->next;
14066 /* The pattern is:
14067 * 0 index result < 0 >
14068 * 1 index 0 < ? >
14069 * 2 index 1 < ? >
14070 * 3 read 2
14071 * 4 write 3 var
14072 */
14073 for(index0 = ins->next->next;
14074 (index0->op == OP_INDEX) &&
14075 (MISC(index0, 0) == result) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000014076 (index0->u.cval == 0) ;
Eric Biederman90089602004-05-28 14:11:54 +000014077 index0 = write->next)
14078 {
14079 index1 = index0->next;
14080 index2 = index1->next;
14081 read = index2->next;
14082 write = read->next;
14083 if ((index0->op != OP_INDEX) ||
14084 (index1->op != OP_INDEX) ||
14085 (index2->op != OP_INDEX) ||
14086 (read->op != OP_READ) ||
14087 (write->op != OP_WRITE) ||
14088 (MISC(index1, 0) != index0) ||
14089 (MISC(index2, 0) != index1) ||
14090 (RHS(read, 0) != index2) ||
14091 (RHS(write, 0) != read)) {
14092 internal_error(state, index0, "bad var read");
14093 }
14094 if (MISC(write, 0) == val) {
14095 return index2->u.cval;
14096 }
14097 }
14098 } while(next != first);
14099 return -1;
14100}
14101
14102static inline int enclose_triple(struct triple *ins)
14103{
14104 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14105}
14106
14107static void compute_closure_variables(struct compile_state *state,
14108 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14109{
14110 struct triple_reg_set *set, *vars, **last_var;
14111 struct basic_blocks bb;
14112 struct reg_block *rb;
14113 struct block *block;
14114 struct triple *old_result, *first, *ins;
14115 size_t count, idx;
14116 unsigned long used_indicies;
14117 int i, max_index;
14118#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14119#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
Stefan Reinauer14e22772010-04-27 06:56:47 +000014120 struct {
Eric Biederman90089602004-05-28 14:11:54 +000014121 unsigned id;
14122 int index;
14123 } *info;
14124
Stefan Reinauer14e22772010-04-27 06:56:47 +000014125
Eric Biederman90089602004-05-28 14:11:54 +000014126 /* Find the basic blocks of this function */
14127 bb.func = me;
14128 bb.first = RHS(me, 0);
14129 old_result = 0;
14130 if (!triple_is_ret(state, bb.first->prev)) {
14131 bb.func = 0;
14132 } else {
14133 old_result = fresult(state, me);
14134 }
14135 analyze_basic_blocks(state, &bb);
14136
14137 /* Find which variables are currently alive in a given block */
14138 rb = compute_variable_lifetimes(state, &bb);
14139
14140 /* Find the variables that are currently alive */
14141 block = block_of_triple(state, fcall);
14142 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14143 internal_error(state, fcall, "No reg block? block: %p", block);
14144 }
14145
14146#if DEBUG_EXPLICIT_CLOSURES
14147 print_live_variables(state, &bb, rb, state->dbgout);
14148 fflush(state->dbgout);
14149#endif
14150
14151 /* Count the number of triples in the function */
14152 first = RHS(me, 0);
14153 ins = first;
14154 count = 0;
14155 do {
14156 count++;
14157 ins = ins->next;
14158 } while(ins != first);
14159
14160 /* Allocate some memory to temorary hold the id info */
14161 info = xcmalloc(sizeof(*info) * (count +1), "info");
14162
14163 /* Mark the local function */
14164 first = RHS(me, 0);
14165 ins = first;
14166 idx = 1;
14167 do {
14168 info[idx].id = ins->id;
14169 ins->id = TRIPLE_FLAG_LOCAL | idx;
14170 idx++;
14171 ins = ins->next;
14172 } while(ins != first);
14173
Stefan Reinauer14e22772010-04-27 06:56:47 +000014174 /*
Eric Biederman90089602004-05-28 14:11:54 +000014175 * Build the list of variables to enclose.
14176 *
14177 * A target it to put the same variable in the
14178 * same slot for ever call of a given function.
14179 * After coloring this removes all of the variable
14180 * manipulation code.
14181 *
14182 * The list of variables to enclose is built ordered
14183 * program order because except in corner cases this
14184 * gives me the stability of assignment I need.
14185 *
14186 * To gurantee that stability I lookup the variables
14187 * to see where they have been used before and
14188 * I build my final list with the assigned indicies.
14189 */
14190 vars = 0;
14191 if (enclose_triple(old_result)) {
14192 ordered_triple_set(&vars, old_result);
14193 }
14194 for(set = rb[block->vertex].out; set; set = set->next) {
14195 if (!enclose_triple(set->member)) {
14196 continue;
14197 }
14198 if ((set->member == fcall) || (set->member == old_result)) {
14199 continue;
14200 }
14201 if (!local_triple(state, me, set->member)) {
14202 internal_error(state, set->member, "not local?");
14203 }
14204 ordered_triple_set(&vars, set->member);
14205 }
14206
14207 /* Lookup the current indicies of the live varialbe */
14208 used_indicies = 0;
14209 max_index = -1;
14210 for(set = vars; set ; set = set->next) {
14211 struct triple *ins;
14212 int index;
14213 ins = set->member;
14214 index = lookup_closure_index(state, me, ins);
14215 info[ID_BITS(ins->id)].index = index;
14216 if (index < 0) {
14217 continue;
14218 }
14219 if (index >= MAX_INDICIES) {
14220 internal_error(state, ins, "index unexpectedly large");
14221 }
14222 if (used_indicies & (1 << index)) {
14223 internal_error(state, ins, "index previously used?");
14224 }
14225 /* Remember which indicies have been used */
14226 used_indicies |= (1 << index);
14227 if (index > max_index) {
14228 max_index = index;
14229 }
14230 }
14231
14232 /* Walk through the live variables and make certain
14233 * everything is assigned an index.
14234 */
14235 for(set = vars; set; set = set->next) {
14236 struct triple *ins;
14237 int index;
14238 ins = set->member;
14239 index = info[ID_BITS(ins->id)].index;
14240 if (index >= 0) {
14241 continue;
14242 }
14243 /* Find the lowest unused index value */
14244 for(index = 0; index < MAX_INDICIES; index++) {
14245 if (!(used_indicies & (1 << index))) {
14246 break;
14247 }
14248 }
14249 if (index == MAX_INDICIES) {
14250 internal_error(state, ins, "no free indicies?");
14251 }
14252 info[ID_BITS(ins->id)].index = index;
14253 /* Remember which indicies have been used */
14254 used_indicies |= (1 << index);
14255 if (index > max_index) {
14256 max_index = index;
14257 }
14258 }
14259
14260 /* Build the return list of variables with positions matching
14261 * their indicies.
14262 */
14263 *enclose = 0;
14264 last_var = enclose;
14265 for(i = 0; i <= max_index; i++) {
14266 struct triple *var;
14267 var = 0;
14268 if (used_indicies & (1 << i)) {
14269 for(set = vars; set; set = set->next) {
14270 int index;
14271 index = info[ID_BITS(set->member->id)].index;
14272 if (index == i) {
14273 var = set->member;
14274 break;
14275 }
14276 }
14277 if (!var) {
14278 internal_error(state, me, "missing variable");
14279 }
14280 }
14281 insert_triple_set(last_var, var);
14282 last_var = &(*last_var)->next;
14283 }
14284
14285#if DEBUG_EXPLICIT_CLOSURES
14286 /* Print out the variables to be enclosed */
14287 loc(state->dbgout, state, fcall);
14288 fprintf(state->dbgout, "Alive: \n");
14289 for(set = *enclose; set; set = set->next) {
14290 display_triple(state->dbgout, set->member);
14291 }
14292 fflush(state->dbgout);
14293#endif
14294
14295 /* Clear the marks */
14296 ins = first;
14297 do {
14298 ins->id = info[ID_BITS(ins->id)].id;
14299 ins = ins->next;
14300 } while(ins != first);
14301
14302 /* Release the ordered list of live variables */
14303 free_closure_variables(state, &vars);
14304
14305 /* Release the storage of the old ids */
14306 xfree(info);
14307
14308 /* Release the variable lifetime information */
14309 free_variable_lifetimes(state, &bb, rb);
14310
14311 /* Release the basic blocks of this function */
14312 free_basic_blocks(state, &bb);
14313}
14314
14315static void expand_function_call(
14316 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014317{
14318 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014319 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014320 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014321 struct triple *first;
14322 struct type *ptype, *rtype;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014323 struct triple *ret_addr, *ret_loc;
Eric Biederman90089602004-05-28 14:11:54 +000014324 struct triple_reg_set *enclose, *set;
14325 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014326
Eric Biederman90089602004-05-28 14:11:54 +000014327#if DEBUG_EXPLICIT_CLOSURES
14328 FILE *fp = state->dbgout;
14329 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14330 display_func(state, fp, MISC(fcall, 0));
14331 display_func(state, fp, me);
14332 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14333#endif
14334
Eric Biederman5ade04a2003-10-22 04:03:46 +000014335 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014336 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014337 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014338 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014339 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014340 first = fcall->next;
14341
14342 /* Find what I need to enclose */
14343 compute_closure_variables(state, me, fcall, &enclose);
14344
14345 /* Compute the closure type */
14346 closure_type = new_type(TYPE_TUPLE, 0, 0);
14347 closure_type->elements = 0;
14348 closure_next = &closure_type->left;
14349 for(set = enclose; set ; set = set->next) {
14350 struct type *type;
14351 type = &void_type;
14352 if (set->member) {
14353 type = set->member->type;
14354 }
14355 if (!*closure_next) {
14356 *closure_next = type;
14357 } else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014358 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
Eric Biederman90089602004-05-28 14:11:54 +000014359 type);
14360 closure_next = &(*closure_next)->right;
14361 }
14362 closure_type->elements += 1;
14363 }
14364 if (closure_type->elements == 0) {
14365 closure_type->type = TYPE_VOID;
14366 }
14367
14368
14369#if DEBUG_EXPLICIT_CLOSURES
14370 fprintf(state->dbgout, "closure type: ");
14371 name_of(state->dbgout, closure_type);
14372 fprintf(state->dbgout, "\n");
14373#endif
14374
14375 /* Update the called functions closure variable */
14376 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014377
14378 /* Generate some needed triples */
14379 ret_loc = label(state);
14380 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14381
14382 /* Pass the parameters to the new function */
14383 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014384 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014385 for(i = 0; i < pvals; i++) {
14386 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014387 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014388 atype = ptype;
14389 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14390 atype = ptype->left;
14391 }
Eric Biederman90089602004-05-28 14:11:54 +000014392 param = farg(state, func, i);
14393 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14394 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014395 }
Eric Biederman90089602004-05-28 14:11:54 +000014396 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014397 flatten(state, first, write_expr(state, param, arg));
14398 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014399 }
Eric Biederman90089602004-05-28 14:11:54 +000014400 rtype = func->type->left;
14401
Eric Biederman5ade04a2003-10-22 04:03:46 +000014402 /* Thread the triples together */
14403 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014404
14405 /* Save the active variables in the result variable */
14406 for(i = 0, set = enclose; set ; set = set->next, i++) {
14407 if (!set->member) {
14408 continue;
14409 }
14410 flatten(state, ret_loc,
14411 write_expr(state,
14412 closure_expr(state, func, closure_idx, i),
14413 read_expr(state, set->member)));
14414 }
14415
14416 /* Initialize the return value */
14417 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014418 flatten(state, ret_loc,
14419 write_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014420 deref_index(state, fresult(state, func), 1),
14421 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14422 }
14423
Eric Biederman5ade04a2003-10-22 04:03:46 +000014424 ret_addr = flatten(state, ret_loc, ret_addr);
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014425 flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14426 flatten(state, ret_loc,
Eric Biederman5ade04a2003-10-22 04:03:46 +000014427 call(state, retvar, ret_addr, func_first, func_last));
14428
Eric Biederman7dea9552004-06-29 05:38:37 +000014429 /* Find the result */
14430 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14431 struct triple * result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014432 result = flatten(state, first,
14433 read_expr(state,
Eric Biederman7dea9552004-06-29 05:38:37 +000014434 deref_index(state, fresult(state, func), 1)));
14435
14436 propogate_use(state, fcall, result);
14437 }
14438
14439 /* Release the original fcall instruction */
14440 release_triple(state, fcall);
14441
Eric Biederman90089602004-05-28 14:11:54 +000014442 /* Restore the active variables from the result variable */
14443 for(i = 0, set = enclose; set ; set = set->next, i++) {
14444 struct triple_set *use, *next;
14445 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014446 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014447 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014448 continue;
14449 }
Eric Biederman90089602004-05-28 14:11:54 +000014450 /* Generate an expression for the value */
14451 new = flatten(state, first,
Stefan Reinauer14e22772010-04-27 06:56:47 +000014452 read_expr(state,
Eric Biederman90089602004-05-28 14:11:54 +000014453 closure_expr(state, func, closure_idx, i)));
14454
14455
14456 /* If the original is an lvalue restore the preserved value */
14457 if (is_lvalue(state, set->member)) {
14458 flatten(state, first,
14459 write_expr(state, set->member, new));
14460 continue;
14461 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014462 /*
14463 * If the original is a value update the dominated uses.
14464 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014465
Eric Biederman7dea9552004-06-29 05:38:37 +000014466 /* Analyze the basic blocks so I can see who dominates whom */
14467 bb.func = me;
14468 bb.first = RHS(me, 0);
14469 if (!triple_is_ret(state, bb.first->prev)) {
14470 bb.func = 0;
14471 }
14472 analyze_basic_blocks(state, &bb);
Stefan Reinauer14e22772010-04-27 06:56:47 +000014473
Eric Biederman90089602004-05-28 14:11:54 +000014474
14475#if DEBUG_EXPLICIT_CLOSURES
14476 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14477 set->member, new);
14478#endif
14479 /* If fcall dominates the use update the expression */
14480 for(use = set->member->use; use; use = next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014481 /* Replace use modifies the use chain and
Eric Biederman90089602004-05-28 14:11:54 +000014482 * removes use, so I must take a copy of the
14483 * next entry early.
14484 */
14485 next = use->next;
14486 if (!tdominates(state, fcall, use->member)) {
14487 continue;
14488 }
14489 replace_use(state, set->member, new, use->member);
14490 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014491
14492 /* Release the basic blocks, the instructions will be
14493 * different next time, and flatten/insert_triple does
14494 * not update the block values so I can't cache the analysis.
14495 */
14496 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014497 }
14498
Eric Biederman90089602004-05-28 14:11:54 +000014499 /* Release the closure variable list */
14500 free_closure_variables(state, &enclose);
14501
14502 if (state->compiler->debug & DEBUG_INLINE) {
14503 FILE *fp = state->dbgout;
14504 fprintf(fp, "\n");
14505 loc(fp, state, 0);
14506 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14507 display_func(state, fp, func);
14508 display_func(state, fp, me);
14509 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14510 }
14511
14512 return;
14513}
14514
14515static int do_inline(struct compile_state *state, struct triple *func)
14516{
14517 int do_inline;
14518 int policy;
14519
14520 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14521 switch(policy) {
14522 case COMPILER_INLINE_ALWAYS:
14523 do_inline = 1;
14524 if (func->type->type & ATTRIB_NOINLINE) {
14525 error(state, func, "noinline with always_inline compiler option");
14526 }
14527 break;
14528 case COMPILER_INLINE_NEVER:
14529 do_inline = 0;
14530 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14531 error(state, func, "always_inline with noinline compiler option");
14532 }
14533 break;
14534 case COMPILER_INLINE_DEFAULTON:
14535 switch(func->type->type & STOR_MASK) {
14536 case STOR_STATIC | STOR_INLINE:
14537 case STOR_LOCAL | STOR_INLINE:
14538 case STOR_EXTERN | STOR_INLINE:
14539 do_inline = 1;
14540 break;
14541 default:
14542 do_inline = 1;
14543 break;
14544 }
14545 break;
14546 case COMPILER_INLINE_DEFAULTOFF:
14547 switch(func->type->type & STOR_MASK) {
14548 case STOR_STATIC | STOR_INLINE:
14549 case STOR_LOCAL | STOR_INLINE:
14550 case STOR_EXTERN | STOR_INLINE:
14551 do_inline = 1;
14552 break;
14553 default:
14554 do_inline = 0;
14555 break;
14556 }
14557 break;
14558 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014559 switch(func->type->type & STOR_MASK) {
14560 case STOR_STATIC | STOR_INLINE:
14561 case STOR_LOCAL | STOR_INLINE:
14562 case STOR_EXTERN | STOR_INLINE:
14563 do_inline = 1;
14564 break;
14565 default:
14566 do_inline = (func->u.cval == 1);
14567 break;
14568 }
Eric Biederman90089602004-05-28 14:11:54 +000014569 break;
14570 default:
14571 do_inline = 0;
14572 internal_error(state, 0, "Unimplemented inline policy");
14573 break;
14574 }
14575 /* Force inlining */
14576 if (func->type->type & ATTRIB_NOINLINE) {
14577 do_inline = 0;
14578 }
14579 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14580 do_inline = 1;
14581 }
14582 return do_inline;
14583}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014584
Eric Biederman90089602004-05-28 14:11:54 +000014585static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14586{
14587 struct triple *first, *ptr, *next;
14588 /* If the function is not used don't bother */
14589 if (me->u.cval <= 0) {
14590 return;
14591 }
14592 if (state->compiler->debug & DEBUG_CALLS2) {
14593 FILE *fp = state->dbgout;
14594 fprintf(fp, "in: %s\n",
14595 me->type->type_ident->name);
14596 }
14597
14598 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014599 ptr = next = first;
14600 do {
Eric Biederman90089602004-05-28 14:11:54 +000014601 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014602 ptr = next;
14603 prev = ptr->prev;
14604 next = ptr->next;
14605 if (ptr->op != OP_FCALL) {
14606 continue;
14607 }
14608 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014609 /* See if the function should be inlined */
14610 if (!do_inline(state, func)) {
14611 /* Put a label after the fcall */
14612 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14613 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014614 }
Eric Biederman90089602004-05-28 14:11:54 +000014615 if (state->compiler->debug & DEBUG_CALLS) {
14616 FILE *fp = state->dbgout;
14617 if (state->compiler->debug & DEBUG_CALLS2) {
14618 loc(fp, state, ptr);
14619 }
14620 fprintf(fp, "inlining %s\n",
14621 func->type->type_ident->name);
14622 fflush(fp);
14623 }
14624
14625 /* Update the function use counts */
14626 func->u.cval -= 1;
14627
14628 /* Replace the fcall with the called function */
14629 expand_inline_call(state, me, ptr);
14630
14631 next = prev->next;
14632 } while (next != first);
14633
14634 ptr = next = first;
14635 do {
14636 struct triple *prev, *func;
14637 ptr = next;
14638 prev = ptr->prev;
14639 next = ptr->next;
14640 if (ptr->op != OP_FCALL) {
14641 continue;
14642 }
14643 func = MISC(ptr, 0);
14644 if (state->compiler->debug & DEBUG_CALLS) {
14645 FILE *fp = state->dbgout;
14646 if (state->compiler->debug & DEBUG_CALLS2) {
14647 loc(fp, state, ptr);
14648 }
14649 fprintf(fp, "calling %s\n",
14650 func->type->type_ident->name);
14651 fflush(fp);
14652 }
14653 /* Replace the fcall with the instruction sequence
14654 * needed to make the call.
14655 */
14656 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014657 next = prev->next;
14658 } while(next != first);
14659}
Eric Biederman90089602004-05-28 14:11:54 +000014660
14661static void inline_functions(struct compile_state *state, struct triple *func)
14662{
14663 inline_function(state, func, 0);
14664 reverse_walk_functions(state, inline_function, 0);
14665}
14666
Eric Biederman5ade04a2003-10-22 04:03:46 +000014667static void insert_function(struct compile_state *state,
14668 struct triple *func, void *arg)
14669{
14670 struct triple *first, *end, *ffirst, *fend;
14671
14672 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014673 FILE *fp = state->errout;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014674 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014675 func->type->type_ident->name, func->u.cval);
14676 }
14677 if (func->u.cval == 0) {
14678 return;
14679 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014680
14681 /* Find the end points of the lists */
14682 first = arg;
14683 end = first->prev;
14684 ffirst = RHS(func, 0);
14685 fend = ffirst->prev;
14686
14687 /* splice the lists together */
14688 end->next = ffirst;
14689 ffirst->prev = end;
14690 fend->next = first;
14691 first->prev = fend;
14692}
14693
Eric Biederman90089602004-05-28 14:11:54 +000014694struct triple *input_asm(struct compile_state *state)
14695{
14696 struct asm_info *info;
14697 struct triple *def;
14698 int i, out;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014699
Eric Biederman90089602004-05-28 14:11:54 +000014700 info = xcmalloc(sizeof(*info), "asm_info");
14701 info->str = "";
14702
14703 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14704 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14705
14706 def = new_triple(state, OP_ASM, &void_type, out, 0);
14707 def->u.ainfo = info;
14708 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014709
Eric Biederman90089602004-05-28 14:11:54 +000014710 for(i = 0; i < out; i++) {
14711 struct triple *piece;
14712 piece = triple(state, OP_PIECE, &int_type, def, 0);
14713 piece->u.cval = i;
14714 LHS(def, i) = piece;
14715 }
14716
14717 return def;
14718}
14719
14720struct triple *output_asm(struct compile_state *state)
14721{
14722 struct asm_info *info;
14723 struct triple *def;
14724 int in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014725
Eric Biederman90089602004-05-28 14:11:54 +000014726 info = xcmalloc(sizeof(*info), "asm_info");
14727 info->str = "";
14728
14729 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14730 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14731
14732 def = new_triple(state, OP_ASM, &void_type, 0, in);
14733 def->u.ainfo = info;
14734 def->id |= TRIPLE_FLAG_VOLATILE;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014735
Eric Biederman90089602004-05-28 14:11:54 +000014736 return def;
14737}
14738
Eric Biederman5ade04a2003-10-22 04:03:46 +000014739static void join_functions(struct compile_state *state)
14740{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014741 struct triple *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014742 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014743 struct type *pnext, *param;
14744 struct type *result_type, *args_type;
14745 int idx;
14746
14747 /* Be clear the functions have not been joined yet */
14748 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014749
14750 /* Dummy file state to get debug handing right */
14751 memset(&file, 0, sizeof(file));
14752 file.basename = "";
14753 file.line = 0;
14754 file.report_line = 0;
14755 file.report_name = file.basename;
14756 file.prev = state->file;
14757 state->file = &file;
14758 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014759
Eric Biederman41203d92004-11-08 09:31:09 +000014760 if (!state->main_function) {
14761 error(state, 0, "No functions to compile\n");
14762 }
14763
Eric Biederman90089602004-05-28 14:11:54 +000014764 /* The type of arguments */
14765 args_type = state->main_function->type->right;
14766 /* The return type without any specifiers */
14767 result_type = clone_type(0, state->main_function->type->left);
14768
14769
14770 /* Verify the external arguments */
14771 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014772 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014773 "Too many external input arguments");
14774 }
14775 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014776 error(state, state->main_function,
Eric Biederman90089602004-05-28 14:11:54 +000014777 "Too many external output arguments");
14778 }
14779
Eric Biederman5ade04a2003-10-22 04:03:46 +000014780 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014781 end = label(state);
14782 start = label(state);
14783 start = flatten(state, state->first, start);
14784 end = flatten(state, state->first, end);
14785 in = input_asm(state);
14786 out = output_asm(state);
14787 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014788 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014789 in = flatten(state, state->first, in);
14790 call = flatten(state, state->first, call);
14791 out = flatten(state, state->first, out);
14792
14793
14794 /* Read the external input arguments */
14795 pnext = args_type;
14796 idx = 0;
14797 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14798 struct triple *expr;
14799 param = pnext;
14800 pnext = 0;
14801 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14802 pnext = param->right;
14803 param = param->left;
14804 }
14805 if (registers_of(state, param) != 1) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000014806 error(state, state->main_function,
14807 "Arg: %d %s requires multiple registers",
Eric Biederman90089602004-05-28 14:11:54 +000014808 idx + 1, param->field_ident->name);
14809 }
14810 expr = read_expr(state, LHS(in, idx));
14811 RHS(call, idx) = expr;
14812 expr = flatten(state, call, expr);
14813 use_triple(expr, call);
14814
Stefan Reinauer14e22772010-04-27 06:56:47 +000014815 idx++;
Eric Biederman90089602004-05-28 14:11:54 +000014816 }
14817
14818
14819 /* Write the external output arguments */
14820 pnext = result_type;
14821 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14822 pnext = result_type->left;
14823 }
14824 for(idx = 0; idx < out->rhs; idx++) {
14825 struct triple *expr;
14826 param = pnext;
14827 pnext = 0;
14828 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14829 pnext = param->right;
14830 param = param->left;
14831 }
14832 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14833 param = 0;
14834 }
14835 if (param) {
14836 if (registers_of(state, param) != 1) {
14837 error(state, state->main_function,
14838 "Result: %d %s requires multiple registers",
14839 idx, param->field_ident->name);
14840 }
14841 expr = read_expr(state, call);
14842 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14843 expr = deref_field(state, expr, param->field_ident);
14844 }
14845 } else {
14846 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14847 }
14848 flatten(state, out, expr);
14849 RHS(out, idx) = expr;
14850 use_triple(expr, out);
14851 }
14852
14853 /* Allocate a dummy containing function */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014854 func = triple(state, OP_LIST,
Eric Biederman90089602004-05-28 14:11:54 +000014855 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14856 func->type->type_ident = lookup(state, "", 0);
14857 RHS(func, 0) = state->first;
14858 func->u.cval = 1;
14859
Eric Biederman5ade04a2003-10-22 04:03:46 +000014860 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014861 mark_live_functions(state);
14862 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014863 walk_functions(state, insert_function, end);
14864
14865 if (start->next != end) {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010014866 flatten(state, start, branch(state, end, 0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014867 }
14868
Eric Biederman90089602004-05-28 14:11:54 +000014869 /* OK now the functions have been joined. */
14870 state->functions_joined = 1;
14871
Eric Biederman5ade04a2003-10-22 04:03:46 +000014872 /* Done now cleanup */
14873 state->file = file.prev;
14874 state->function = 0;
14875}
14876
Eric Biedermanb138ac82003-04-22 18:44:01 +000014877/*
14878 * Data structurs for optimation.
14879 */
14880
Eric Biederman5ade04a2003-10-22 04:03:46 +000014881
Eric Biederman83b991a2003-10-11 06:20:25 +000014882static int do_use_block(
Stefan Reinauer14e22772010-04-27 06:56:47 +000014883 struct block *used, struct block_set **head, struct block *user,
Eric Biedermanb138ac82003-04-22 18:44:01 +000014884 int front)
14885{
14886 struct block_set **ptr, *new;
14887 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014888 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014889 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014890 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014891 ptr = head;
14892 while(*ptr) {
14893 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014894 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014895 }
14896 ptr = &(*ptr)->next;
14897 }
14898 new = xcmalloc(sizeof(*new), "block_set");
14899 new->member = user;
14900 if (front) {
14901 new->next = *head;
14902 *head = new;
14903 }
14904 else {
14905 new->next = 0;
14906 *ptr = new;
14907 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014908 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014909}
Eric Biederman83b991a2003-10-11 06:20:25 +000014910static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014911 struct block *used, struct block_set **head, struct block *unuser)
14912{
14913 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014914 int count;
14915 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014916 ptr = head;
14917 while(*ptr) {
14918 use = *ptr;
14919 if (use->member == unuser) {
14920 *ptr = use->next;
14921 memset(use, -1, sizeof(*use));
14922 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014923 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014924 }
14925 else {
14926 ptr = &use->next;
14927 }
14928 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014929 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014930}
14931
14932static void use_block(struct block *used, struct block *user)
14933{
Eric Biederman83b991a2003-10-11 06:20:25 +000014934 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014935 /* Append new to the head of the list, print_block
14936 * depends on this.
14937 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000014938 count = do_use_block(used, &used->use, user, 1);
Eric Biederman83b991a2003-10-11 06:20:25 +000014939 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014940}
14941static void unuse_block(struct block *used, struct block *unuser)
14942{
Eric Biederman83b991a2003-10-11 06:20:25 +000014943 int count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000014944 count = do_unuse_block(used, &used->use, unuser);
Eric Biederman83b991a2003-10-11 06:20:25 +000014945 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014946}
14947
Eric Biederman5ade04a2003-10-22 04:03:46 +000014948static void add_block_edge(struct block *block, struct block *edge, int front)
14949{
14950 int count;
14951 count = do_use_block(block, &block->edges, edge, front);
14952 block->edge_count += count;
14953}
14954
14955static void remove_block_edge(struct block *block, struct block *edge)
14956{
14957 int count;
14958 count = do_unuse_block(block, &block->edges, edge);
14959 block->edge_count -= count;
14960}
14961
Eric Biedermanb138ac82003-04-22 18:44:01 +000014962static void idom_block(struct block *idom, struct block *user)
14963{
14964 do_use_block(idom, &idom->idominates, user, 0);
14965}
14966
14967static void unidom_block(struct block *idom, struct block *unuser)
14968{
14969 do_unuse_block(idom, &idom->idominates, unuser);
14970}
14971
14972static void domf_block(struct block *block, struct block *domf)
14973{
14974 do_use_block(block, &block->domfrontier, domf, 0);
14975}
14976
14977static void undomf_block(struct block *block, struct block *undomf)
14978{
14979 do_unuse_block(block, &block->domfrontier, undomf);
14980}
14981
14982static void ipdom_block(struct block *ipdom, struct block *user)
14983{
14984 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14985}
14986
14987static void unipdom_block(struct block *ipdom, struct block *unuser)
14988{
14989 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14990}
14991
14992static void ipdomf_block(struct block *block, struct block *ipdomf)
14993{
14994 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14995}
14996
14997static void unipdomf_block(struct block *block, struct block *unipdomf)
14998{
14999 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15000}
15001
Eric Biederman83b991a2003-10-11 06:20:25 +000015002static int walk_triples(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015003 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015004 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15005 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015006{
Eric Biederman83b991a2003-10-11 06:20:25 +000015007 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015008 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015009 ptr = state->first;
15010 do {
Eric Biederman90089602004-05-28 14:11:54 +000015011 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015012 if (ptr->next->prev != ptr) {
15013 internal_error(state, ptr->next, "bad prev");
15014 }
15015 ptr = ptr->next;
15016 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015017 return result;
15018}
15019
Eric Biedermanb138ac82003-04-22 18:44:01 +000015020#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015021static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015022{
Eric Biederman90089602004-05-28 14:11:54 +000015023 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015024 int op;
15025 op = ins->op;
15026 if (op == OP_LIST) {
15027#if !PRINT_LIST
15028 return 0;
15029#endif
15030 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015031 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015032 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015033 }
Eric Biederman90089602004-05-28 14:11:54 +000015034 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015035
Stefan Reinauer14e22772010-04-27 06:56:47 +000015036 if (triple_is_branch(state, ins) && ins->use &&
Eric Biederman90089602004-05-28 14:11:54 +000015037 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015038 internal_error(state, ins, "branch used?");
15039 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015040 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015041 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015042 }
15043 return 0;
15044}
15045
Eric Biedermanb138ac82003-04-22 18:44:01 +000015046static void print_triples(struct compile_state *state)
15047{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015048 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015049 FILE *fp = state->dbgout;
15050 fprintf(fp, "--------------- triples ---------------\n");
15051 walk_triples(state, do_print_triple, fp);
15052 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015053 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015054}
15055
15056struct cf_block {
15057 struct block *block;
15058};
15059static void find_cf_blocks(struct cf_block *cf, struct block *block)
15060{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015061 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015062 if (!block || (cf[block->vertex].block == block)) {
15063 return;
15064 }
15065 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015066 for(edge = block->edges; edge; edge = edge->next) {
15067 find_cf_blocks(cf, edge->member);
15068 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015069}
15070
Eric Biederman90089602004-05-28 14:11:54 +000015071static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015072 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015073{
15074 struct cf_block *cf;
15075 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015076 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015077 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15078 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015079
Eric Biederman90089602004-05-28 14:11:54 +000015080 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015081 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015082 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015083 block = cf[i].block;
15084 if (!block)
15085 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015086 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015087 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015088 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015089 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015090 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015091 }
15092
15093 xfree(cf);
15094}
15095
Eric Biedermanb138ac82003-04-22 18:44:01 +000015096static void free_basic_block(struct compile_state *state, struct block *block)
15097{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015098 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015099 struct block *child;
15100 if (!block) {
15101 return;
15102 }
15103 if (block->vertex == -1) {
15104 return;
15105 }
15106 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015107 for(edge = block->edges; edge; edge = edge->next) {
15108 if (edge->member) {
15109 unuse_block(edge->member, block);
15110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015111 }
15112 if (block->idom) {
15113 unidom_block(block->idom, block);
15114 }
15115 block->idom = 0;
15116 if (block->ipdom) {
15117 unipdom_block(block->ipdom, block);
15118 }
15119 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015120 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015121 child = entry->member;
15122 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015123 if (child && (child->vertex != -1)) {
15124 for(edge = child->edges; edge; edge = edge->next) {
15125 edge->member = 0;
15126 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015127 }
15128 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015129 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015130 child = entry->member;
15131 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015132 if (child && (child->vertex != -1)) {
15133 child->idom = 0;
15134 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015135 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015136 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015137 child = entry->member;
15138 undomf_block(block, child);
15139 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015140 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015141 child = entry->member;
15142 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015143 if (child && (child->vertex != -1)) {
15144 child->ipdom = 0;
15145 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015146 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015147 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015148 child = entry->member;
15149 unipdomf_block(block, child);
15150 }
15151 if (block->users != 0) {
15152 internal_error(state, 0, "block still has users");
15153 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015154 while((edge = block->edges)) {
15155 child = edge->member;
15156 remove_block_edge(block, child);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015157
Eric Biederman5ade04a2003-10-22 04:03:46 +000015158 if (child && (child->vertex != -1)) {
15159 free_basic_block(state, child);
15160 }
15161 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015162 memset(block, -1, sizeof(*block));
Patrick Georgi26774f22009-11-21 19:54:02 +000015163#ifndef WIN32
Eric Biedermanb138ac82003-04-22 18:44:01 +000015164 xfree(block);
Patrick Georgi26774f22009-11-21 19:54:02 +000015165#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015166}
15167
Stefan Reinauer14e22772010-04-27 06:56:47 +000015168static void free_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015169 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015170{
15171 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015172 free_basic_block(state, bb->first_block);
15173 bb->last_vertex = 0;
15174 bb->first_block = bb->last_block = 0;
15175 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015176 ins = first;
15177 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015178 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015179 ins->u.block = 0;
15180 }
15181 ins = ins->next;
15182 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015183
Eric Biedermanb138ac82003-04-22 18:44:01 +000015184}
15185
Stefan Reinauer14e22772010-04-27 06:56:47 +000015186static struct block *basic_block(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015187 struct basic_blocks *bb, struct triple *first)
15188{
15189 struct block *block;
15190 struct triple *ptr;
15191 if (!triple_is_label(state, first)) {
15192 internal_error(state, first, "block does not start with a label");
15193 }
15194 /* See if this basic block has already been setup */
15195 if (first->u.block != 0) {
15196 return first->u.block;
15197 }
15198 /* Allocate another basic block structure */
15199 bb->last_vertex += 1;
15200 block = xcmalloc(sizeof(*block), "block");
15201 block->first = block->last = first;
15202 block->vertex = bb->last_vertex;
15203 ptr = first;
15204 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015205 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015206 break;
15207 }
15208 block->last = ptr;
15209 /* If ptr->u is not used remember where the baic block is */
15210 if (triple_stores_block(state, ptr)) {
15211 ptr->u.block = block;
15212 }
15213 if (triple_is_branch(state, ptr)) {
15214 break;
15215 }
15216 ptr = ptr->next;
15217 } while (ptr != bb->first);
15218 if ((ptr == bb->first) ||
15219 ((ptr->next == bb->first) && (
Stefan Reinauer14e22772010-04-27 06:56:47 +000015220 triple_is_end(state, ptr) ||
Eric Biederman90089602004-05-28 14:11:54 +000015221 triple_is_ret(state, ptr))))
15222 {
15223 /* The block has no outflowing edges */
15224 }
15225 else if (triple_is_label(state, ptr)) {
15226 struct block *next;
15227 next = basic_block(state, bb, ptr);
15228 add_block_edge(block, next, 0);
15229 use_block(next, block);
15230 }
15231 else if (triple_is_branch(state, ptr)) {
15232 struct triple **expr, *first;
15233 struct block *child;
15234 /* Find the branch targets.
15235 * I special case the first branch as that magically
15236 * avoids some difficult cases for the register allocator.
15237 */
15238 expr = triple_edge_targ(state, ptr, 0);
15239 if (!expr) {
15240 internal_error(state, ptr, "branch without targets");
15241 }
15242 first = *expr;
15243 expr = triple_edge_targ(state, ptr, expr);
15244 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15245 if (!*expr) continue;
15246 child = basic_block(state, bb, *expr);
15247 use_block(child, block);
15248 add_block_edge(block, child, 0);
15249 }
15250 if (first) {
15251 child = basic_block(state, bb, first);
15252 use_block(child, block);
15253 add_block_edge(block, child, 1);
15254
15255 /* Be certain the return block of a call is
15256 * in a basic block. When it is not find
15257 * start of the block, insert a label if
15258 * necessary and build the basic block.
15259 * Then add a fake edge from the start block
15260 * to the return block of the function.
15261 */
15262 if (state->functions_joined && triple_is_call(state, ptr)
15263 && !block_of_triple(state, MISC(ptr, 0))) {
15264 struct block *tail;
15265 struct triple *start;
15266 start = triple_to_block_start(state, MISC(ptr, 0));
15267 if (!triple_is_label(state, start)) {
15268 start = pre_triple(state,
15269 start, OP_LABEL, &void_type, 0, 0);
15270 }
15271 tail = basic_block(state, bb, start);
15272 add_block_edge(child, tail, 0);
15273 use_block(tail, child);
15274 }
15275 }
15276 }
15277 else {
15278 internal_error(state, 0, "Bad basic block split");
15279 }
15280#if 0
15281{
15282 struct block_set *edge;
15283 FILE *fp = state->errout;
15284 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015285 block, block->vertex,
Eric Biederman90089602004-05-28 14:11:54 +000015286 block->first, block->last);
15287 for(edge = block->edges; edge; edge = edge->next) {
15288 fprintf(fp, " %10p [%2d]",
15289 edge->member ? edge->member->first : 0,
15290 edge->member ? edge->member->vertex : -1);
15291 }
15292 fprintf(fp, "\n");
15293}
15294#endif
15295 return block;
15296}
15297
15298
15299static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15300 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15301 void *arg)
15302{
15303 struct triple *ptr, *first;
15304 struct block *last_block;
15305 last_block = 0;
15306 first = bb->first;
15307 ptr = first;
15308 do {
15309 if (triple_stores_block(state, ptr)) {
15310 struct block *block;
15311 block = ptr->u.block;
15312 if (block && (block != last_block)) {
15313 cb(state, block, arg);
15314 }
15315 last_block = block;
15316 }
15317 ptr = ptr->next;
15318 } while(ptr != first);
15319}
15320
15321static void print_block(
15322 struct compile_state *state, struct block *block, void *arg)
15323{
15324 struct block_set *user, *edge;
15325 struct triple *ptr;
15326 FILE *fp = arg;
15327
15328 fprintf(fp, "\nblock: %p (%d) ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000015329 block,
Eric Biederman90089602004-05-28 14:11:54 +000015330 block->vertex);
15331
15332 for(edge = block->edges; edge; edge = edge->next) {
15333 fprintf(fp, " %p<-%p",
15334 edge->member,
15335 (edge->member && edge->member->use)?
15336 edge->member->use->member : 0);
15337 }
15338 fprintf(fp, "\n");
15339 if (block->first->op == OP_LABEL) {
15340 fprintf(fp, "%p:\n", block->first);
15341 }
15342 for(ptr = block->first; ; ) {
15343 display_triple(fp, ptr);
15344 if (ptr == block->last)
15345 break;
15346 ptr = ptr->next;
15347 if (ptr == block->first) {
15348 internal_error(state, 0, "missing block last?");
15349 }
15350 }
15351 fprintf(fp, "users %d: ", block->users);
15352 for(user = block->use; user; user = user->next) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000015353 fprintf(fp, "%p (%d) ",
Eric Biederman90089602004-05-28 14:11:54 +000015354 user->member,
15355 user->member->vertex);
15356 }
15357 fprintf(fp,"\n\n");
15358}
15359
15360
15361static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15362{
15363 fprintf(fp, "--------------- blocks ---------------\n");
15364 walk_blocks(state, &state->bb, print_block, fp);
15365}
15366static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15367{
15368 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15369 fprintf(fp, "After %s\n", func);
15370 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015371 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15372 print_dominators(state, fp, &state->bb);
15373 print_dominance_frontiers(state, fp, &state->bb);
15374 }
15375 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015376 }
15377}
15378
Stefan Reinauer14e22772010-04-27 06:56:47 +000015379static void prune_nonblock_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015380 struct basic_blocks *bb)
15381{
15382 struct block *block;
15383 struct triple *first, *ins, *next;
15384 /* Delete the triples not in a basic block */
15385 block = 0;
15386 first = bb->first;
15387 ins = first;
15388 do {
15389 next = ins->next;
15390 if (ins->op == OP_LABEL) {
15391 block = ins->u.block;
15392 }
15393 if (!block) {
15394 struct triple_set *use;
15395 for(use = ins->use; use; use = use->next) {
15396 struct block *block;
15397 block = block_of_triple(state, use->member);
15398 if (block != 0) {
15399 internal_error(state, ins, "pruning used ins?");
15400 }
15401 }
15402 release_triple(state, ins);
15403 }
15404 if (block && block->last == ins) {
15405 block = 0;
15406 }
15407 ins = next;
15408 } while(ins != first);
15409}
15410
Stefan Reinauer14e22772010-04-27 06:56:47 +000015411static void setup_basic_blocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015412 struct basic_blocks *bb)
15413{
15414 if (!triple_stores_block(state, bb->first)) {
15415 internal_error(state, 0, "ins will not store block?");
15416 }
15417 /* Initialize the state */
15418 bb->first_block = bb->last_block = 0;
15419 bb->last_vertex = 0;
15420 free_basic_blocks(state, bb);
15421
15422 /* Find the basic blocks */
15423 bb->first_block = basic_block(state, bb, bb->first);
15424
15425 /* Be certain the last instruction of a function, or the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015426 * entire program is in a basic block. When it is not find
15427 * the start of the block, insert a label if necessary and build
Eric Biederman90089602004-05-28 14:11:54 +000015428 * basic block. Then add a fake edge from the start block
15429 * to the final block.
15430 */
15431 if (!block_of_triple(state, bb->first->prev)) {
15432 struct triple *start;
15433 struct block *tail;
15434 start = triple_to_block_start(state, bb->first->prev);
15435 if (!triple_is_label(state, start)) {
15436 start = pre_triple(state,
15437 start, OP_LABEL, &void_type, 0, 0);
15438 }
15439 tail = basic_block(state, bb, start);
15440 add_block_edge(bb->first_block, tail, 0);
15441 use_block(tail, bb->first_block);
15442 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015443
Eric Biederman90089602004-05-28 14:11:54 +000015444 /* Find the last basic block.
15445 */
15446 bb->last_block = block_of_triple(state, bb->first->prev);
15447
15448 /* Delete the triples not in a basic block */
15449 prune_nonblock_triples(state, bb);
15450
15451#if 0
15452 /* If we are debugging print what I have just done */
15453 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15454 print_blocks(state, state->dbgout);
15455 print_control_flow(state, bb);
15456 }
15457#endif
15458}
15459
15460
Eric Biedermanb138ac82003-04-22 18:44:01 +000015461struct sdom_block {
15462 struct block *block;
15463 struct sdom_block *sdominates;
15464 struct sdom_block *sdom_next;
15465 struct sdom_block *sdom;
15466 struct sdom_block *label;
15467 struct sdom_block *parent;
15468 struct sdom_block *ancestor;
15469 int vertex;
15470};
15471
15472
15473static void unsdom_block(struct sdom_block *block)
15474{
15475 struct sdom_block **ptr;
15476 if (!block->sdom_next) {
15477 return;
15478 }
15479 ptr = &block->sdom->sdominates;
15480 while(*ptr) {
15481 if ((*ptr) == block) {
15482 *ptr = block->sdom_next;
15483 return;
15484 }
15485 ptr = &(*ptr)->sdom_next;
15486 }
15487}
15488
15489static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15490{
15491 unsdom_block(block);
15492 block->sdom = sdom;
15493 block->sdom_next = sdom->sdominates;
15494 sdom->sdominates = block;
15495}
15496
15497
15498
15499static int initialize_sdblock(struct sdom_block *sd,
15500 struct block *parent, struct block *block, int vertex)
15501{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015502 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015503 if (!block || (sd[block->vertex].block == block)) {
15504 return vertex;
15505 }
15506 vertex += 1;
15507 /* Renumber the blocks in a convinient fashion */
15508 block->vertex = vertex;
15509 sd[vertex].block = block;
15510 sd[vertex].sdom = &sd[vertex];
15511 sd[vertex].label = &sd[vertex];
15512 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15513 sd[vertex].ancestor = 0;
15514 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015515 for(edge = block->edges; edge; edge = edge->next) {
15516 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15517 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015518 return vertex;
15519}
15520
Eric Biederman83b991a2003-10-11 06:20:25 +000015521static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015522 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015523 struct block *parent, struct block *block, int vertex)
15524{
15525 struct block_set *user;
15526 if (!block || (sd[block->vertex].block == block)) {
15527 return vertex;
15528 }
15529 vertex += 1;
15530 /* Renumber the blocks in a convinient fashion */
15531 block->vertex = vertex;
15532 sd[vertex].block = block;
15533 sd[vertex].sdom = &sd[vertex];
15534 sd[vertex].label = &sd[vertex];
15535 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15536 sd[vertex].ancestor = 0;
15537 sd[vertex].vertex = vertex;
15538 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015539 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015540 }
15541 return vertex;
15542}
15543
Stefan Reinauer14e22772010-04-27 06:56:47 +000015544static int setup_spdblocks(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015545 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015546{
15547 struct block *block;
15548 int vertex;
15549 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015550 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015551
Eric Biederman5ade04a2003-10-22 04:03:46 +000015552 /* Walk through the graph and find unconnected blocks. Add a
15553 * fake edge from the unconnected blocks to the end of the
Stefan Reinauer14e22772010-04-27 06:56:47 +000015554 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015555 */
Eric Biederman90089602004-05-28 14:11:54 +000015556 block = bb->first_block->last->next->u.block;
15557 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015558 if (sd[block->vertex].block == block) {
15559 continue;
15560 }
Eric Biederman530b5192003-07-01 10:05:30 +000015561#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015562 {
15563 FILE *fp = state->errout;
15564 fprintf(fp, "Adding %d\n", vertex +1);
15565 }
Eric Biederman530b5192003-07-01 10:05:30 +000015566#endif
Eric Biederman90089602004-05-28 14:11:54 +000015567 add_block_edge(block, bb->last_block, 0);
15568 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015569
Eric Biederman90089602004-05-28 14:11:54 +000015570 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015571 }
15572 return vertex;
15573}
15574
15575static void compress_ancestors(struct sdom_block *v)
15576{
15577 /* This procedure assumes ancestor(v) != 0 */
15578 /* if (ancestor(ancestor(v)) != 0) {
15579 * compress(ancestor(ancestor(v)));
15580 * if (semi(label(ancestor(v))) < semi(label(v))) {
15581 * label(v) = label(ancestor(v));
15582 * }
15583 * ancestor(v) = ancestor(ancestor(v));
15584 * }
15585 */
15586 if (!v->ancestor) {
15587 return;
15588 }
15589 if (v->ancestor->ancestor) {
15590 compress_ancestors(v->ancestor->ancestor);
15591 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15592 v->label = v->ancestor->label;
15593 }
15594 v->ancestor = v->ancestor->ancestor;
15595 }
15596}
15597
Stefan Reinauer14e22772010-04-27 06:56:47 +000015598static void compute_sdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015599 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015600{
15601 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015602 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015603 * for each v <= pred(w) {
15604 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015605 * if (semi[u] < semi[w] {
15606 * semi[w] = semi[u];
15607 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015608 * }
15609 * add w to bucket(vertex(semi[w]));
15610 * LINK(parent(w), w);
15611 *
15612 * // step 3
15613 * for each v <= bucket(parent(w)) {
15614 * delete v from bucket(parent(w));
15615 * u = EVAL(v);
15616 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15617 * }
15618 */
Eric Biederman90089602004-05-28 14:11:54 +000015619 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015620 struct sdom_block *v, *parent, *next;
15621 struct block_set *user;
15622 struct block *block;
15623 block = sd[i].block;
15624 parent = sd[i].parent;
15625 /* Step 2 */
15626 for(user = block->use; user; user = user->next) {
15627 struct sdom_block *v, *u;
15628 v = &sd[user->member->vertex];
15629 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15630 if (u->sdom->vertex < sd[i].sdom->vertex) {
15631 sd[i].sdom = u->sdom;
15632 }
15633 }
15634 sdom_block(sd[i].sdom, &sd[i]);
15635 sd[i].ancestor = parent;
15636 /* Step 3 */
15637 for(v = parent->sdominates; v; v = next) {
15638 struct sdom_block *u;
15639 next = v->sdom_next;
15640 unsdom_block(v);
15641 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015642 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015643 u->block : parent->block;
15644 }
15645 }
15646}
15647
Stefan Reinauer14e22772010-04-27 06:56:47 +000015648static void compute_spdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015649 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015650{
15651 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000015652 /* // step 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000015653 * for each v <= pred(w) {
15654 * u = EVAL(v);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015655 * if (semi[u] < semi[w] {
15656 * semi[w] = semi[u];
15657 * }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015658 * }
15659 * add w to bucket(vertex(semi[w]));
15660 * LINK(parent(w), w);
15661 *
15662 * // step 3
15663 * for each v <= bucket(parent(w)) {
15664 * delete v from bucket(parent(w));
15665 * u = EVAL(v);
15666 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15667 * }
15668 */
Eric Biederman90089602004-05-28 14:11:54 +000015669 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015670 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015671 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015672 struct block *block;
15673 block = sd[i].block;
15674 parent = sd[i].parent;
15675 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015676 for(edge = block->edges; edge; edge = edge->next) {
15677 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015678 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15679 if (u->sdom->vertex < sd[i].sdom->vertex) {
15680 sd[i].sdom = u->sdom;
15681 }
15682 }
15683 sdom_block(sd[i].sdom, &sd[i]);
15684 sd[i].ancestor = parent;
15685 /* Step 3 */
15686 for(v = parent->sdominates; v; v = next) {
15687 struct sdom_block *u;
15688 next = v->sdom_next;
15689 unsdom_block(v);
15690 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015691 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
Eric Biedermanb138ac82003-04-22 18:44:01 +000015692 u->block : parent->block;
15693 }
15694 }
15695}
15696
Stefan Reinauer14e22772010-04-27 06:56:47 +000015697static void compute_idom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015698 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015699{
15700 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015701 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015702 struct block *block;
15703 block = sd[i].block;
15704 if (block->idom->vertex != sd[i].sdom->vertex) {
15705 block->idom = block->idom->idom;
15706 }
15707 idom_block(block->idom, block);
15708 }
15709 sd[1].block->idom = 0;
15710}
15711
Stefan Reinauer14e22772010-04-27 06:56:47 +000015712static void compute_ipdom(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015713 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015714{
15715 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015716 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015717 struct block *block;
15718 block = sd[i].block;
15719 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15720 block->ipdom = block->ipdom->ipdom;
15721 }
15722 ipdom_block(block->ipdom, block);
15723 }
15724 sd[1].block->ipdom = 0;
15725}
15726
15727 /* Theorem 1:
15728 * Every vertex of a flowgraph G = (V, E, r) except r has
Stefan Reinauer14e22772010-04-27 06:56:47 +000015729 * a unique immediate dominator.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015730 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
Stefan Reinauer14e22772010-04-27 06:56:47 +000015731 * rooted at r, called the dominator tree of G, such that
Eric Biedermanb138ac82003-04-22 18:44:01 +000015732 * v dominates w if and only if v is a proper ancestor of w in
15733 * the dominator tree.
15734 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000015735 /* Lemma 1:
Eric Biedermanb138ac82003-04-22 18:44:01 +000015736 * If v and w are vertices of G such that v <= w,
15737 * than any path from v to w must contain a common ancestor
15738 * of v and w in T.
15739 */
15740 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15741 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15742 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15743 /* Theorem 2:
15744 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15745 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15746 */
15747 /* Theorem 3:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015748 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015749 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15750 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15751 */
15752 /* Lemma 5: Let vertices v,w satisfy v -> w.
15753 * Then v -> idom(w) or idom(w) -> idom(v)
15754 */
15755
Eric Biederman90089602004-05-28 14:11:54 +000015756static void find_immediate_dominators(struct compile_state *state,
15757 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015758{
15759 struct sdom_block *sd;
15760 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15761 * vi > w for (1 <= i <= k - 1}
15762 */
15763 /* Theorem 4:
15764 * For any vertex w != r.
15765 * sdom(w) = min(
Stefan Reinauer14e22772010-04-27 06:56:47 +000015766 * {v|(v,w) <= E and v < w } U
Eric Biedermanb138ac82003-04-22 18:44:01 +000015767 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15768 */
15769 /* Corollary 1:
Stefan Reinauer14e22772010-04-27 06:56:47 +000015770 * Let w != r and let u be a vertex for which sdom(u) is
Eric Biedermanb138ac82003-04-22 18:44:01 +000015771 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15772 * Then:
15773 * { sdom(w) if sdom(w) = sdom(u),
15774 * idom(w) = {
15775 * { idom(u) otherwise
15776 */
15777 /* The algorithm consists of the following 4 steps.
Stefan Reinauer14e22772010-04-27 06:56:47 +000015778 * Step 1. Carry out a depth-first search of the problem graph.
Eric Biedermanb138ac82003-04-22 18:44:01 +000015779 * Number the vertices from 1 to N as they are reached during
15780 * the search. Initialize the variables used in succeeding steps.
15781 * Step 2. Compute the semidominators of all vertices by applying
15782 * theorem 4. Carry out the computation vertex by vertex in
15783 * decreasing order by number.
15784 * Step 3. Implicitly define the immediate dominator of each vertex
15785 * by applying Corollary 1.
15786 * Step 4. Explicitly define the immediate dominator of each vertex,
15787 * carrying out the computation vertex by vertex in increasing order
15788 * by number.
15789 */
15790 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015791 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15792 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015793#if 0
15794 sd[1].size = 0;
15795 sd[1].label = 0;
15796 sd[1].sdom = 0;
15797#endif
15798 /* Step 2 compute the semidominators */
15799 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015800 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015801 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015802 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015803 xfree(sd);
15804}
15805
Eric Biederman90089602004-05-28 14:11:54 +000015806static void find_post_dominators(struct compile_state *state,
15807 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015808{
15809 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015810 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015811 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015812 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015813
Eric Biederman90089602004-05-28 14:11:54 +000015814 vertex = setup_spdblocks(state, bb, sd);
15815 if (vertex != bb->last_vertex) {
15816 internal_error(state, 0, "missing %d blocks",
15817 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015818 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015819
15820 /* Step 2 compute the semidominators */
15821 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015822 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015823 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015824 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015825 xfree(sd);
15826}
15827
15828
15829
15830static void find_block_domf(struct compile_state *state, struct block *block)
15831{
15832 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015833 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015834 if (block->domfrontier != 0) {
15835 internal_error(state, block->first, "domfrontier present?");
15836 }
15837 for(user = block->idominates; user; user = user->next) {
15838 child = user->member;
15839 if (child->idom != block) {
15840 internal_error(state, block->first, "bad idom");
15841 }
15842 find_block_domf(state, child);
15843 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015844 for(edge = block->edges; edge; edge = edge->next) {
15845 if (edge->member->idom != block) {
15846 domf_block(block, edge->member);
15847 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015848 }
15849 for(user = block->idominates; user; user = user->next) {
15850 struct block_set *frontier;
15851 child = user->member;
15852 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15853 if (frontier->member->idom != block) {
15854 domf_block(block, frontier->member);
15855 }
15856 }
15857 }
15858}
15859
15860static void find_block_ipdomf(struct compile_state *state, struct block *block)
15861{
15862 struct block *child;
15863 struct block_set *user;
15864 if (block->ipdomfrontier != 0) {
15865 internal_error(state, block->first, "ipdomfrontier present?");
15866 }
15867 for(user = block->ipdominates; user; user = user->next) {
15868 child = user->member;
15869 if (child->ipdom != block) {
15870 internal_error(state, block->first, "bad ipdom");
15871 }
15872 find_block_ipdomf(state, child);
15873 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015874 for(user = block->use; user; user = user->next) {
15875 if (user->member->ipdom != block) {
15876 ipdomf_block(block, user->member);
15877 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015878 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015879 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015880 struct block_set *frontier;
15881 child = user->member;
15882 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15883 if (frontier->member->ipdom != block) {
15884 ipdomf_block(block, frontier->member);
15885 }
15886 }
15887 }
15888}
15889
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015890static void print_dominated(
15891 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015892{
15893 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015894 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015895
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015896 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015897 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015898 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015899 if (user->member->idom != block) {
15900 internal_error(state, user->member->first, "bad idom");
15901 }
15902 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015903 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015904}
15905
Eric Biederman5ade04a2003-10-22 04:03:46 +000015906static void print_dominated2(
15907 struct compile_state *state, FILE *fp, int depth, struct block *block)
15908{
15909 struct block_set *user;
15910 struct triple *ins;
15911 struct occurance *ptr, *ptr2;
15912 const char *filename1, *filename2;
15913 int equal_filenames;
15914 int i;
15915 for(i = 0; i < depth; i++) {
15916 fprintf(fp, " ");
15917 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000015918 fprintf(fp, "%3d: %p (%p - %p) @",
Eric Biederman5ade04a2003-10-22 04:03:46 +000015919 block->vertex, block, block->first, block->last);
15920 ins = block->first;
15921 while(ins != block->last && (ins->occurance->line == 0)) {
15922 ins = ins->next;
15923 }
15924 ptr = ins->occurance;
15925 ptr2 = block->last->occurance;
15926 filename1 = ptr->filename? ptr->filename : "";
15927 filename2 = ptr2->filename? ptr2->filename : "";
15928 equal_filenames = (strcmp(filename1, filename2) == 0);
15929 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15930 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15931 } else if (equal_filenames) {
15932 fprintf(fp, " %s:(%d - %d)",
15933 ptr->filename, ptr->line, ptr2->line);
15934 } else {
15935 fprintf(fp, " (%s:%d - %s:%d)",
15936 ptr->filename, ptr->line,
15937 ptr2->filename, ptr2->line);
15938 }
15939 fprintf(fp, "\n");
15940 for(user = block->idominates; user; user = user->next) {
15941 print_dominated2(state, fp, depth + 1, user->member);
15942 }
15943}
15944
Eric Biederman90089602004-05-28 14:11:54 +000015945static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015946{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015947 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015948 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015949 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015950 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015951}
15952
15953
15954static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015955 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015956{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015957 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015958
15959 if (!block || (block->vertex != vertex + 1)) {
15960 return vertex;
15961 }
15962 vertex += 1;
15963
Eric Biederman7dea9552004-06-29 05:38:37 +000015964 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015965 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015966 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015967 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015968 fprintf(fp, "\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000015969
Eric Biederman5ade04a2003-10-22 04:03:46 +000015970 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015971 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015972 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015973 return vertex;
15974}
Eric Biederman90089602004-05-28 14:11:54 +000015975static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015976 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015977{
Eric Biederman7dea9552004-06-29 05:38:37 +000015978 fprintf(fp, "\ndominance frontiers\n");
15979 print_frontiers(state, fp, bb->first_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000015980
Eric Biedermanb138ac82003-04-22 18:44:01 +000015981}
15982
Eric Biederman90089602004-05-28 14:11:54 +000015983static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015984{
15985 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015986 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015987 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015988 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015989 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015990 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015991 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015992 print_dominance_frontiers(state, state->dbgout, bb);
15993 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015994 }
15995}
15996
15997
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015998static void print_ipdominated(
15999 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016000{
16001 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016002 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016003
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016004 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016005 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016006 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016007 if (user->member->ipdom != block) {
16008 internal_error(state, user->member->first, "bad ipdom");
16009 }
16010 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016011 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016012}
16013
Eric Biederman90089602004-05-28 14:11:54 +000016014static void print_ipdominators(struct compile_state *state, FILE *fp,
16015 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016016{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016017 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016018 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016019}
16020
16021static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016022 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016023{
16024 struct block_set *user;
16025
16026 if (!block || (block->vertex != vertex + 1)) {
16027 return vertex;
16028 }
16029 vertex += 1;
16030
Eric Biederman7dea9552004-06-29 05:38:37 +000016031 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016032 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016033 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016034 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016035 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016036 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016037 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016038 }
16039 return vertex;
16040}
Eric Biederman90089602004-05-28 14:11:54 +000016041static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016042 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016043{
Eric Biederman7dea9552004-06-29 05:38:37 +000016044 fprintf(fp, "\nipdominance frontiers\n");
16045 print_pfrontiers(state, fp, bb->last_block, 0);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016046
Eric Biedermanb138ac82003-04-22 18:44:01 +000016047}
16048
Eric Biederman90089602004-05-28 14:11:54 +000016049static void analyze_ipdominators(struct compile_state *state,
16050 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016051{
16052 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016053 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016054 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016055 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016056 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016057 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016058 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016059 print_ipdominance_frontiers(state, state->dbgout, bb);
16060 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016061 }
16062}
16063
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016064static int bdominates(struct compile_state *state,
16065 struct block *dom, struct block *sub)
16066{
16067 while(sub && (sub != dom)) {
16068 sub = sub->idom;
16069 }
16070 return sub == dom;
16071}
16072
16073static int tdominates(struct compile_state *state,
16074 struct triple *dom, struct triple *sub)
16075{
16076 struct block *bdom, *bsub;
16077 int result;
16078 bdom = block_of_triple(state, dom);
16079 bsub = block_of_triple(state, sub);
16080 if (bdom != bsub) {
16081 result = bdominates(state, bdom, bsub);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016082 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016083 else {
16084 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016085 if (!bdom || !bsub) {
16086 internal_error(state, dom, "huh?");
16087 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016088 ins = sub;
16089 while((ins != bsub->first) && (ins != dom)) {
16090 ins = ins->prev;
16091 }
16092 result = (ins == dom);
16093 }
16094 return result;
16095}
16096
Eric Biederman90089602004-05-28 14:11:54 +000016097static void analyze_basic_blocks(
16098 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016099{
Eric Biederman90089602004-05-28 14:11:54 +000016100 setup_basic_blocks(state, bb);
16101 analyze_idominators(state, bb);
16102 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016103}
16104
Eric Biedermanb138ac82003-04-22 18:44:01 +000016105static void insert_phi_operations(struct compile_state *state)
16106{
16107 size_t size;
16108 struct triple *first;
16109 int *has_already, *work;
16110 struct block *work_list, **work_list_tail;
16111 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016112 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016113
Eric Biederman90089602004-05-28 14:11:54 +000016114 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016115 has_already = xcmalloc(size, "has_already");
16116 work = xcmalloc(size, "work");
16117 iter = 0;
16118
Eric Biederman83b991a2003-10-11 06:20:25 +000016119 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016120 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016121 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016122 struct triple_set *user, *unext;
16123 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016124
16125 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016126 continue;
16127 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016128
Eric Biedermanb138ac82003-04-22 18:44:01 +000016129 iter += 1;
16130 work_list = 0;
16131 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016132 for(user = var->use; user; user = unext) {
16133 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016134 if (MISC(var, 0) == user->member) {
16135 continue;
16136 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016137 if (user->member->op == OP_READ) {
16138 continue;
16139 }
16140 if (user->member->op != OP_WRITE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000016141 internal_error(state, user->member,
Eric Biedermanb138ac82003-04-22 18:44:01 +000016142 "bad variable access");
16143 }
16144 block = user->member->u.block;
16145 if (!block) {
16146 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016147 release_triple(state, user->member);
16148 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016149 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016150 if (work[block->vertex] >= iter) {
16151 continue;
16152 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016153 work[block->vertex] = iter;
16154 *work_list_tail = block;
16155 block->work_next = 0;
16156 work_list_tail = &block->work_next;
16157 }
16158 for(block = work_list; block; block = block->work_next) {
16159 struct block_set *df;
16160 for(df = block->domfrontier; df; df = df->next) {
16161 struct triple *phi;
16162 struct block *front;
16163 int in_edges;
16164 front = df->member;
16165
16166 if (has_already[front->vertex] >= iter) {
16167 continue;
16168 }
16169 /* Count how many edges flow into this block */
16170 in_edges = front->users;
16171 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016172 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016173 phi = alloc_triple(
Stefan Reinauer14e22772010-04-27 06:56:47 +000016174 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016175 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016176 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016177 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016178 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016179#if 1
16180 if (phi->rhs != in_edges) {
16181 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16182 phi->rhs, in_edges);
16183 }
16184#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016185 /* Insert the phi functions immediately after the label */
16186 insert_triple(state, front->first->next, phi);
16187 if (front->first == front->last) {
16188 front->last = front->first->next;
16189 }
16190 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016191 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016192
Eric Biedermanb138ac82003-04-22 18:44:01 +000016193 /* If necessary plan to visit the basic block */
16194 if (work[front->vertex] >= iter) {
16195 continue;
16196 }
16197 work[front->vertex] = iter;
16198 *work_list_tail = front;
16199 front->work_next = 0;
16200 work_list_tail = &front->work_next;
16201 }
16202 }
16203 }
16204 xfree(has_already);
16205 xfree(work);
16206}
16207
Eric Biederman66fe2222003-07-04 15:14:04 +000016208
Eric Biederman83b991a2003-10-11 06:20:25 +000016209struct stack {
16210 struct triple_set *top;
16211 unsigned orig_id;
16212};
16213
Eric Biederman90089602004-05-28 14:11:54 +000016214static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016215{
16216 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016217 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016218 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016219 ins = first;
16220 do {
Eric Biederman90089602004-05-28 14:11:54 +000016221 if (triple_is_auto_var(state, ins)) {
16222 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016223 }
16224 ins = ins->next;
16225 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016226 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016227}
16228
Eric Biederman90089602004-05-28 14:11:54 +000016229static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016230{
16231 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016232 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016233 first = state->first;
16234 ins = first;
16235 do {
Eric Biederman90089602004-05-28 14:11:54 +000016236 if (triple_is_auto_var(state, ins)) {
16237 auto_vars += 1;
16238 stacks[auto_vars].orig_id = ins->id;
16239 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016240 }
16241 ins = ins->next;
16242 } while(ins != first);
16243}
16244
Eric Biederman90089602004-05-28 14:11:54 +000016245static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016246{
16247 struct triple *first, *ins;
16248 first = state->first;
16249 ins = first;
16250 do {
Eric Biederman90089602004-05-28 14:11:54 +000016251 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016252 ins->id = stacks[ins->id].orig_id;
16253 }
16254 ins = ins->next;
16255 } while(ins != first);
16256}
16257
16258static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016259{
16260 struct triple_set *head;
16261 struct triple *top_val;
16262 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016263 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016264 if (head) {
16265 top_val = head->member;
16266 }
16267 return top_val;
16268}
16269
Eric Biederman83b991a2003-10-11 06:20:25 +000016270static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016271{
16272 struct triple_set *new;
16273 /* Append new to the head of the list,
16274 * it's the only sensible behavoir for a stack.
16275 */
16276 new = xcmalloc(sizeof(*new), "triple_set");
16277 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016278 new->next = stacks[var->id].top;
16279 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016280}
16281
Eric Biederman83b991a2003-10-11 06:20:25 +000016282static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016283{
16284 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016285 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016286 while(*ptr) {
16287 set = *ptr;
16288 if (set->member == oldval) {
16289 *ptr = set->next;
16290 xfree(set);
16291 /* Only free one occurance from the stack */
16292 return;
16293 }
16294 else {
16295 ptr = &set->next;
16296 }
16297 }
16298}
16299
Eric Biedermanb138ac82003-04-22 18:44:01 +000016300/*
16301 * C(V)
16302 * S(V)
16303 */
16304static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016305 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016306{
16307 struct block_set *set;
16308 struct triple *ptr;
16309 int edge;
16310 if (!parent || !block)
16311 return;
16312 /* Find the edge I am coming in on */
16313 edge = 0;
16314 for(set = block->use; set; set = set->next, edge++) {
16315 if (set->member == parent) {
16316 break;
16317 }
16318 }
16319 if (!set) {
16320 internal_error(state, 0, "phi input is not on a control predecessor");
16321 }
16322 for(ptr = block->first; ; ptr = ptr->next) {
16323 if (ptr->op == OP_PHI) {
16324 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016325 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016326 if (!var) {
16327 internal_error(state, ptr, "no var???");
16328 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016329 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016330 val = peek_triple(stacks, var);
16331 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016332 internal_error(state, val, "bad value in phi");
16333 }
Eric Biederman90089602004-05-28 14:11:54 +000016334 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016335 internal_error(state, ptr, "edges > phi rhs");
16336 }
16337 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016338 if ((*slot != 0) && (*slot != val)) {
16339 internal_error(state, ptr, "phi already bound on this edge");
16340 }
16341 *slot = val;
16342 use_triple(val, ptr);
16343 }
16344 if (ptr == block->last) {
16345 break;
16346 }
16347 }
16348}
16349
16350
16351static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016352 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016353{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016354 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016355 struct triple *ptr, *next, *last;
16356 int done;
16357 if (!block)
16358 return;
16359 last = block->first;
16360 done = 0;
16361 for(ptr = block->first; !done; ptr = next) {
16362 next = ptr->next;
16363 if (ptr == block->last) {
16364 done = 1;
16365 }
16366 /* RHS(A) */
16367 if (ptr->op == OP_READ) {
16368 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016369 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016370 if (!triple_is_auto_var(state, var)) {
16371 internal_error(state, ptr, "read of non auto var!");
16372 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016373 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016374 /* Find the current value of the variable */
16375 val = peek_triple(stacks, var);
16376 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016377 /* Let the optimizer at variables that are not initially
16378 * set. But give it a bogus value so things seem to
16379 * work by accident. This is useful for bitfields because
16380 * setting them always involves a read-modify-write.
16381 */
16382 if (TYPE_ARITHMETIC(ptr->type->type)) {
16383 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16384 val->u.cval = 0xdeadbeaf;
16385 } else {
16386 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16387 }
16388 }
16389 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016390 error(state, ptr, "variable used without being set");
16391 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016392 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16393 internal_error(state, val, "bad value in read");
16394 }
16395 propogate_use(state, ptr, val);
16396 release_triple(state, ptr);
16397 continue;
16398 }
16399 /* LHS(A) */
16400 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016401 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016402 var = MISC(ptr, 0);
16403 if (!triple_is_auto_var(state, var)) {
16404 internal_error(state, ptr, "write to non auto var!");
16405 }
16406 tval = val = RHS(ptr, 0);
16407 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16408 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016409 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016410 }
Eric Biederman90089602004-05-28 14:11:54 +000016411 /* Insert a cast if the types differ */
16412 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016413 if (val->op == OP_INTCONST) {
16414 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16415 tval->u.cval = val->u.cval;
16416 }
16417 else {
Eric Biederman90089602004-05-28 14:11:54 +000016418 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016419 use_triple(val, tval);
16420 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016421 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016422 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016423 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016424 use_triple(tval, ptr);
16425 }
16426 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016427 unuse_triple(var, ptr);
16428 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016429 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016430 }
16431 if (ptr->op == OP_PHI) {
16432 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016433 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016434 if (!triple_is_auto_var(state, var)) {
16435 internal_error(state, ptr, "phi references non auto var!");
16436 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016437 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016438 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016439 }
16440 last = ptr;
16441 }
16442 block->last = last;
16443
16444 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016445 for(edge = block->edges; edge; edge = edge->next) {
16446 fixup_block_phi_variables(state, stacks, block, edge->member);
16447 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016448 /* rename variables in the dominated nodes */
16449 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016450 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016451 }
16452 /* pop the renamed variable stack */
16453 last = block->first;
16454 done = 0;
16455 for(ptr = block->first; !done ; ptr = next) {
16456 next = ptr->next;
16457 if (ptr == block->last) {
16458 done = 1;
16459 }
16460 if (ptr->op == OP_WRITE) {
16461 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +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 Biederman90089602004-05-28 14:11:54 +000016464 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016465 release_triple(state, ptr);
16466 continue;
16467 }
16468 if (ptr->op == OP_PHI) {
16469 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016470 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016471 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016472 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016473 }
16474 last = ptr;
16475 }
16476 block->last = last;
16477}
16478
Eric Biederman83b991a2003-10-11 06:20:25 +000016479static void rename_variables(struct compile_state *state)
16480{
16481 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016482 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016483
16484 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016485 auto_vars = count_auto_vars(state);
16486 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016487
Eric Biederman90089602004-05-28 14:11:54 +000016488 /* Give each auto_var a stack */
16489 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016490
16491 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016492 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016493
Eric Biederman90089602004-05-28 14:11:54 +000016494 /* Remove the stacks from the auto_vars */
16495 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016496 xfree(stacks);
16497}
16498
Eric Biedermanb138ac82003-04-22 18:44:01 +000016499static void prune_block_variables(struct compile_state *state,
16500 struct block *block)
16501{
16502 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016503 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016504 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016505
Eric Biedermanb138ac82003-04-22 18:44:01 +000016506 done = 0;
16507 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016508 /* Be extremely careful I am deleting the list
16509 * as I walk trhough it.
16510 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016511 next = ptr->next;
16512 if (ptr == block->last) {
16513 done = 1;
16514 }
Eric Biederman90089602004-05-28 14:11:54 +000016515 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016516 struct triple_set *user, *next;
16517 for(user = ptr->use; user; user = next) {
16518 struct triple *use;
16519 next = user->next;
16520 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016521 if (MISC(ptr, 0) == user->member) {
16522 continue;
16523 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016524 if (use->op != OP_PHI) {
16525 internal_error(state, use, "decl still used");
16526 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016527 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016528 internal_error(state, use, "bad phi use of decl");
16529 }
16530 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016531 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016532 }
Eric Biederman90089602004-05-28 14:11:54 +000016533 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16534 /* Delete the adecl */
16535 release_triple(state, MISC(ptr, 0));
16536 /* And the piece */
16537 release_triple(state, ptr);
16538 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016539 continue;
16540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016541 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016542 for(user = block->idominates; user; user = user->next) {
16543 prune_block_variables(state, user->member);
16544 }
16545}
16546
Eric Biederman66fe2222003-07-04 15:14:04 +000016547struct phi_triple {
16548 struct triple *phi;
16549 unsigned orig_id;
16550 int alive;
16551};
16552
16553static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16554{
16555 struct triple **slot;
16556 int zrhs, i;
16557 if (live[phi->id].alive) {
16558 return;
16559 }
16560 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016561 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016562 slot = &RHS(phi, 0);
16563 for(i = 0; i < zrhs; i++) {
16564 struct triple *used;
16565 used = slot[i];
16566 if (used && (used->op == OP_PHI)) {
16567 keep_phi(state, live, used);
16568 }
16569 }
16570}
16571
16572static void prune_unused_phis(struct compile_state *state)
16573{
16574 struct triple *first, *phi;
16575 struct phi_triple *live;
16576 int phis, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016577
Eric Biederman66fe2222003-07-04 15:14:04 +000016578 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016579 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016580
16581 /* Count how many phi functions I need to process */
16582 phis = 0;
16583 for(phi = first->next; phi != first; phi = phi->next) {
16584 if (phi->op == OP_PHI) {
16585 phis += 1;
16586 }
16587 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016588
Eric Biederman66fe2222003-07-04 15:14:04 +000016589 /* Mark them all dead */
16590 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16591 phis = 0;
16592 for(phi = first->next; phi != first; phi = phi->next) {
16593 if (phi->op != OP_PHI) {
16594 continue;
16595 }
16596 live[phis].alive = 0;
16597 live[phis].orig_id = phi->id;
16598 live[phis].phi = phi;
16599 phi->id = phis;
16600 phis += 1;
16601 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016602
Eric Biederman66fe2222003-07-04 15:14:04 +000016603 /* Mark phis alive that are used by non phis */
16604 for(i = 0; i < phis; i++) {
16605 struct triple_set *set;
16606 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16607 if (set->member->op != OP_PHI) {
16608 keep_phi(state, live, live[i].phi);
16609 break;
16610 }
16611 }
16612 }
16613
16614 /* Delete the extraneous phis */
16615 for(i = 0; i < phis; i++) {
16616 struct triple **slot;
16617 int zrhs, j;
16618 if (!live[i].alive) {
16619 release_triple(state, live[i].phi);
16620 continue;
16621 }
16622 phi = live[i].phi;
16623 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016624 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016625 for(j = 0; j < zrhs; j++) {
16626 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016627 struct triple *unknown;
16628 get_occurance(phi->occurance);
16629 unknown = flatten(state, state->global_pool,
16630 alloc_triple(state, OP_UNKNOWNVAL,
16631 phi->type, 0, 0, phi->occurance));
16632 slot[j] = unknown;
16633 use_triple(unknown, phi);
16634 transform_to_arch_instruction(state, unknown);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016635#if 0
Eric Biederman90089602004-05-28 14:11:54 +000016636 warning(state, phi, "variable not set at index %d on all paths to use", j);
16637#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016638 }
16639 }
16640 }
16641 xfree(live);
16642}
16643
Eric Biedermanb138ac82003-04-22 18:44:01 +000016644static void transform_to_ssa_form(struct compile_state *state)
16645{
16646 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016647 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016648
Eric Biederman90089602004-05-28 14:11:54 +000016649 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016650 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016651
Eric Biederman90089602004-05-28 14:11:54 +000016652 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016653}
16654
16655
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016656static void clear_vertex(
16657 struct compile_state *state, struct block *block, void *arg)
16658{
Eric Biederman83b991a2003-10-11 06:20:25 +000016659 /* Clear the current blocks vertex and the vertex of all
16660 * of the current blocks neighbors in case there are malformed
16661 * blocks with now instructions at this point.
16662 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016663 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016664 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016665 for(edge = block->edges; edge; edge = edge->next) {
16666 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016667 }
16668 for(user = block->use; user; user = user->next) {
16669 user->member->vertex = 0;
16670 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016671}
16672
16673static void mark_live_block(
16674 struct compile_state *state, struct block *block, int *next_vertex)
16675{
16676 /* See if this is a block that has not been marked */
16677 if (block->vertex != 0) {
16678 return;
16679 }
16680 block->vertex = *next_vertex;
16681 *next_vertex += 1;
16682 if (triple_is_branch(state, block->last)) {
16683 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016684 targ = triple_edge_targ(state, block->last, 0);
16685 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016686 if (!*targ) {
16687 continue;
16688 }
16689 if (!triple_stores_block(state, *targ)) {
16690 internal_error(state, 0, "bad targ");
16691 }
16692 mark_live_block(state, (*targ)->u.block, next_vertex);
16693 }
Eric Biederman90089602004-05-28 14:11:54 +000016694 /* Ensure the last block of a function remains alive */
16695 if (triple_is_call(state, block->last)) {
16696 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16697 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016698 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016699 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016700 struct triple *ins;
16701 ins = block->last->next;
16702 if (!triple_stores_block(state, ins)) {
16703 internal_error(state, 0, "bad block start");
16704 }
16705 mark_live_block(state, ins->u.block, next_vertex);
16706 }
16707}
16708
Eric Biedermanb138ac82003-04-22 18:44:01 +000016709static void transform_from_ssa_form(struct compile_state *state)
16710{
16711 /* To get out of ssa form we insert moves on the incoming
16712 * edges to blocks containting phi functions.
16713 */
16714 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016715 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016716 int next_vertex;
16717
16718 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016719 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016720 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016721 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016722
16723 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016724 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016725 for(phi = first->next; phi != first ; phi = next) {
16726 struct block_set *set;
16727 struct block *block;
16728 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016729 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016730 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016731 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016732 next = phi->next;
16733 if (phi->op != OP_PHI) {
16734 continue;
16735 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016736
Eric Biedermanb138ac82003-04-22 18:44:01 +000016737 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016738 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016739
Eric Biederman83b991a2003-10-11 06:20:25 +000016740 /* If this phi is in a dead block just forget it */
16741 if (block->vertex == 0) {
16742 release_triple(state, phi);
16743 continue;
16744 }
16745
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016746 /* Forget uses from code in dead blocks */
16747 for(use = phi->use; use; use = use_next) {
16748 struct block *ublock;
16749 struct triple **expr;
16750 use_next = use->next;
16751 ublock = block_of_triple(state, use->member);
16752 if ((use->member == phi) || (ublock->vertex != 0)) {
16753 continue;
16754 }
16755 expr = triple_rhs(state, use->member, 0);
16756 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16757 if (*expr == phi) {
16758 *expr = 0;
16759 }
16760 }
16761 unuse_triple(phi, use->member);
16762 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016763 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016764 if (registers_of(state, phi->type) != 1) {
16765 internal_error(state, phi, "phi->type does not fit in a single register!");
16766 }
16767 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16768 var = var->next; /* point at the var */
Stefan Reinauer14e22772010-04-27 06:56:47 +000016769
Eric Biederman83b991a2003-10-11 06:20:25 +000016770 /* Replaces use of phi with var */
16771 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016772
Eric Biederman90089602004-05-28 14:11:54 +000016773 /* Count the readers */
16774 readers = 0;
16775 for(use = var->use; use; use = use->next) {
16776 if (use->member != MISC(var, 0)) {
16777 readers++;
16778 }
16779 }
16780
Eric Biedermanb138ac82003-04-22 18:44:01 +000016781 /* Walk all of the incoming edges/blocks and insert moves.
16782 */
Eric Biederman90089602004-05-28 14:11:54 +000016783 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016784 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016785 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016786 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016787 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016788 eblock = set->member;
16789 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016790 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016791 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016792 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016793
Eric Biederman83b991a2003-10-11 06:20:25 +000016794 /* If we don't have a value that belongs in an OP_WRITE
16795 * continue on.
16796 */
Eric Biederman90089602004-05-28 14:11:54 +000016797 if (!val || (val == &unknown_triple) || (val == phi)
16798 || (vblock && (vblock->vertex == 0))) {
16799 continue;
16800 }
16801 /* If the value should never occur error */
16802 if (!vblock) {
16803 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016804 continue;
16805 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016806
16807 /* If the value occurs in a dead block see if a replacement
16808 * block can be found.
16809 */
16810 while(eblock && (eblock->vertex == 0)) {
16811 eblock = eblock->idom;
16812 }
16813 /* If not continue on with the next value. */
16814 if (!eblock || (eblock->vertex == 0)) {
16815 continue;
16816 }
16817
16818 /* If we have an empty incoming block ignore it. */
16819 if (!eblock->first) {
16820 internal_error(state, 0, "empty block?");
16821 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016822
Eric Biederman530b5192003-07-01 10:05:30 +000016823 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016824 /* Walk through the edge block backwards to find an
16825 * appropriate location for the OP_WRITE.
16826 */
16827 for(base = eblock->last; base != eblock->first; base = base->prev) {
16828 struct triple **expr;
16829 if (base->op == OP_PIECE) {
16830 base = MISC(base, 0);
16831 }
16832 if ((base == var) || (base == val)) {
16833 goto out;
16834 }
16835 expr = triple_lhs(state, base, 0);
16836 for(; expr; expr = triple_lhs(state, base, expr)) {
16837 if ((*expr) == val) {
16838 goto out;
16839 }
16840 }
16841 expr = triple_rhs(state, base, 0);
16842 for(; expr; expr = triple_rhs(state, base, expr)) {
16843 if ((*expr) == var) {
16844 goto out;
16845 }
16846 }
Eric Biederman530b5192003-07-01 10:05:30 +000016847 }
Eric Biederman90089602004-05-28 14:11:54 +000016848 out:
16849 if (triple_is_branch(state, base)) {
16850 internal_error(state, base,
16851 "Could not insert write to phi");
16852 }
16853 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016854 use_triple(val, move);
16855 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016856 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016857 }
Eric Biederman90089602004-05-28 14:11:54 +000016858 if (!writers && readers) {
16859 internal_error(state, var, "no value written to in use phi?");
16860 }
16861 /* If var is not used free it */
16862 if (!writers) {
16863 release_triple(state, MISC(var, 0));
16864 release_triple(state, var);
16865 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016866 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016867 release_triple(state, phi);
16868 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016869
Eric Biederman83b991a2003-10-11 06:20:25 +000016870 /* Walk all of the operations to find the adecls */
16871 for(var = first->next; var != first ; var = var->next) {
16872 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016873 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016874 continue;
16875 }
16876
16877 /* Walk through all of the rhs uses of var and
16878 * replace them with read of var.
16879 */
16880 for(use = var->use; use; use = use_next) {
16881 struct triple *read, *user;
16882 struct triple **slot;
16883 int zrhs, i, used;
16884 use_next = use->next;
16885 user = use->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000016886
Eric Biederman83b991a2003-10-11 06:20:25 +000016887 /* Generate a read of var */
16888 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16889 use_triple(var, read);
16890
16891 /* Find the rhs uses and see if they need to be replaced */
16892 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016893 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016894 slot = &RHS(user, 0);
16895 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016896 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016897 slot[i] = read;
16898 used = 1;
16899 }
16900 }
16901 /* If we did use it cleanup the uses */
16902 if (used) {
16903 unuse_triple(var, user);
16904 use_triple(read, user);
Stefan Reinauer14e22772010-04-27 06:56:47 +000016905 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016906 /* If we didn't use it release the extra triple */
16907 else {
16908 release_triple(state, read);
16909 }
16910 }
16911 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016912}
16913
Eric Biederman5ade04a2003-10-22 04:03:46 +000016914#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016915 FILE *fp = state->dbgout; \
16916 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000016917 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000016918
Eric Biederman83b991a2003-10-11 06:20:25 +000016919static void rebuild_ssa_form(struct compile_state *state)
16920{
16921HI();
16922 transform_from_ssa_form(state);
16923HI();
Eric Biederman90089602004-05-28 14:11:54 +000016924 state->bb.first = state->first;
16925 free_basic_blocks(state, &state->bb);
16926 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016927HI();
16928 insert_phi_operations(state);
16929HI();
16930 rename_variables(state);
16931HI();
Stefan Reinauer14e22772010-04-27 06:56:47 +000016932
Eric Biederman90089602004-05-28 14:11:54 +000016933 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016934HI();
16935 prune_unused_phis(state);
16936HI();
16937}
16938#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016939
Stefan Reinauer14e22772010-04-27 06:56:47 +000016940/*
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016941 * Register conflict resolution
16942 * =========================================================
16943 */
16944
16945static struct reg_info find_def_color(
16946 struct compile_state *state, struct triple *def)
16947{
16948 struct triple_set *set;
16949 struct reg_info info;
16950 info.reg = REG_UNSET;
16951 info.regcm = 0;
16952 if (!triple_is_def(state, def)) {
16953 return info;
16954 }
16955 info = arch_reg_lhs(state, def, 0);
16956 if (info.reg >= MAX_REGISTERS) {
16957 info.reg = REG_UNSET;
16958 }
16959 for(set = def->use; set; set = set->next) {
16960 struct reg_info tinfo;
16961 int i;
16962 i = find_rhs_use(state, set->member, def);
16963 if (i < 0) {
16964 continue;
16965 }
16966 tinfo = arch_reg_rhs(state, set->member, i);
16967 if (tinfo.reg >= MAX_REGISTERS) {
16968 tinfo.reg = REG_UNSET;
16969 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000016970 if ((tinfo.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016971 (info.reg != REG_UNSET) &&
16972 (tinfo.reg != info.reg)) {
16973 internal_error(state, def, "register conflict");
16974 }
16975 if ((info.regcm & tinfo.regcm) == 0) {
16976 internal_error(state, def, "regcm conflict %x & %x == 0",
16977 info.regcm, tinfo.regcm);
16978 }
16979 if (info.reg == REG_UNSET) {
16980 info.reg = tinfo.reg;
16981 }
16982 info.regcm &= tinfo.regcm;
16983 }
16984 if (info.reg >= MAX_REGISTERS) {
16985 internal_error(state, def, "register out of range");
16986 }
16987 return info;
16988}
16989
16990static struct reg_info find_lhs_pre_color(
16991 struct compile_state *state, struct triple *ins, int index)
16992{
16993 struct reg_info info;
16994 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016995 zrhs = ins->rhs;
16996 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016997 if (!zlhs && triple_is_def(state, ins)) {
16998 zlhs = 1;
16999 }
17000 if (index >= zlhs) {
17001 internal_error(state, ins, "Bad lhs %d", index);
17002 }
17003 info = arch_reg_lhs(state, ins, index);
17004 for(i = 0; i < zrhs; i++) {
17005 struct reg_info rinfo;
17006 rinfo = arch_reg_rhs(state, ins, i);
17007 if ((info.reg == rinfo.reg) &&
17008 (rinfo.reg >= MAX_REGISTERS)) {
17009 struct reg_info tinfo;
17010 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17011 info.reg = tinfo.reg;
17012 info.regcm &= tinfo.regcm;
17013 break;
17014 }
17015 }
17016 if (info.reg >= MAX_REGISTERS) {
17017 info.reg = REG_UNSET;
17018 }
17019 return info;
17020}
17021
17022static struct reg_info find_rhs_post_color(
17023 struct compile_state *state, struct triple *ins, int index);
17024
17025static struct reg_info find_lhs_post_color(
17026 struct compile_state *state, struct triple *ins, int index)
17027{
17028 struct triple_set *set;
17029 struct reg_info info;
17030 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017031#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017032 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017033 ins, index);
17034#endif
17035 if ((index == 0) && triple_is_def(state, ins)) {
17036 lhs = ins;
17037 }
Eric Biederman90089602004-05-28 14:11:54 +000017038 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017039 lhs = LHS(ins, index);
17040 }
17041 else {
17042 internal_error(state, ins, "Bad lhs %d", index);
17043 lhs = 0;
17044 }
17045 info = arch_reg_lhs(state, ins, index);
17046 if (info.reg >= MAX_REGISTERS) {
17047 info.reg = REG_UNSET;
17048 }
17049 for(set = lhs->use; set; set = set->next) {
17050 struct reg_info rinfo;
17051 struct triple *user;
17052 int zrhs, i;
17053 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017054 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017055 for(i = 0; i < zrhs; i++) {
17056 if (RHS(user, i) != lhs) {
17057 continue;
17058 }
17059 rinfo = find_rhs_post_color(state, user, i);
17060 if ((info.reg != REG_UNSET) &&
17061 (rinfo.reg != REG_UNSET) &&
17062 (info.reg != rinfo.reg)) {
17063 internal_error(state, ins, "register conflict");
17064 }
17065 if ((info.regcm & rinfo.regcm) == 0) {
17066 internal_error(state, ins, "regcm conflict %x & %x == 0",
17067 info.regcm, rinfo.regcm);
17068 }
17069 if (info.reg == REG_UNSET) {
17070 info.reg = rinfo.reg;
17071 }
17072 info.regcm &= rinfo.regcm;
17073 }
17074 }
Eric Biederman530b5192003-07-01 10:05:30 +000017075#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017076 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017077 ins, index, info.reg, info.regcm);
17078#endif
17079 return info;
17080}
17081
17082static struct reg_info find_rhs_post_color(
17083 struct compile_state *state, struct triple *ins, int index)
17084{
17085 struct reg_info info, rinfo;
17086 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017087#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017088 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017089 ins, index);
17090#endif
17091 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017092 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017093 if (!zlhs && triple_is_def(state, ins)) {
17094 zlhs = 1;
17095 }
17096 info = rinfo;
17097 if (info.reg >= MAX_REGISTERS) {
17098 info.reg = REG_UNSET;
17099 }
17100 for(i = 0; i < zlhs; i++) {
17101 struct reg_info linfo;
17102 linfo = arch_reg_lhs(state, ins, i);
17103 if ((linfo.reg == rinfo.reg) &&
17104 (linfo.reg >= MAX_REGISTERS)) {
17105 struct reg_info tinfo;
17106 tinfo = find_lhs_post_color(state, ins, i);
17107 if (tinfo.reg >= MAX_REGISTERS) {
17108 tinfo.reg = REG_UNSET;
17109 }
Eric Biederman530b5192003-07-01 10:05:30 +000017110 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017111 info.regcm &= tinfo.regcm;
17112 if (info.reg != REG_UNSET) {
17113 internal_error(state, ins, "register conflict");
17114 }
17115 if (info.regcm == 0) {
17116 internal_error(state, ins, "regcm conflict");
17117 }
17118 info.reg = tinfo.reg;
17119 }
17120 }
Eric Biederman530b5192003-07-01 10:05:30 +000017121#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017122 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017123 ins, index, info.reg, info.regcm);
17124#endif
17125 return info;
17126}
17127
17128static struct reg_info find_lhs_color(
17129 struct compile_state *state, struct triple *ins, int index)
17130{
17131 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017132#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017133 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017134 ins, index);
17135#endif
17136 pre = find_lhs_pre_color(state, ins, index);
17137 post = find_lhs_post_color(state, ins, index);
17138 if ((pre.reg != post.reg) &&
17139 (pre.reg != REG_UNSET) &&
17140 (post.reg != REG_UNSET)) {
17141 internal_error(state, ins, "register conflict");
17142 }
17143 info.regcm = pre.regcm & post.regcm;
17144 info.reg = pre.reg;
17145 if (info.reg == REG_UNSET) {
17146 info.reg = post.reg;
17147 }
Eric Biederman530b5192003-07-01 10:05:30 +000017148#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017149 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017150 ins, index, info.reg, info.regcm,
17151 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017152#endif
17153 return info;
17154}
17155
17156static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17157{
17158 struct triple_set *entry, *next;
17159 struct triple *out;
17160 struct reg_info info, rinfo;
17161
17162 info = arch_reg_lhs(state, ins, 0);
17163 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17164 use_triple(RHS(out, 0), out);
17165 /* Get the users of ins to use out instead */
17166 for(entry = ins->use; entry; entry = next) {
17167 int i;
17168 next = entry->next;
17169 if (entry->member == out) {
17170 continue;
17171 }
17172 i = find_rhs_use(state, entry->member, ins);
17173 if (i < 0) {
17174 continue;
17175 }
17176 rinfo = arch_reg_rhs(state, entry->member, i);
17177 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17178 continue;
17179 }
17180 replace_rhs_use(state, ins, out, entry->member);
17181 }
17182 transform_to_arch_instruction(state, out);
17183 return out;
17184}
17185
Eric Biedermand1ea5392003-06-28 06:49:45 +000017186static struct triple *typed_pre_copy(
17187 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017188{
17189 /* Carefully insert enough operations so that I can
17190 * enter any operation with a GPR32.
17191 */
17192 struct triple *in;
17193 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017194 unsigned classes;
17195 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017196 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017197 if (ins->op == OP_PHI) {
17198 internal_error(state, ins, "pre_copy on a phi?");
17199 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017200 classes = arch_type_to_regcm(state, type);
17201 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017202 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017203 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017204 FILE *fp = state->errout;
17205 fprintf(fp, "src_type: ");
17206 name_of(fp, ins->type);
17207 fprintf(fp, "\ndst_type: ");
17208 name_of(fp, type);
17209 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017210 internal_error(state, ins, "pre_copy with no register classes");
17211 }
Eric Biederman90089602004-05-28 14:11:54 +000017212 op = OP_COPY;
17213 if (!equiv_types(type, (*expr)->type)) {
17214 op = OP_CONVERT;
17215 }
17216 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017217 unuse_triple(*expr, ins);
17218 *expr = in;
17219 use_triple(RHS(in, 0), in);
17220 use_triple(in, ins);
17221 transform_to_arch_instruction(state, in);
17222 return in;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017223
Eric Biedermand1ea5392003-06-28 06:49:45 +000017224}
17225static struct triple *pre_copy(
17226 struct compile_state *state, struct triple *ins, int index)
17227{
17228 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017229}
17230
17231
Eric Biedermanb138ac82003-04-22 18:44:01 +000017232static void insert_copies_to_phi(struct compile_state *state)
17233{
17234 /* To get out of ssa form we insert moves on the incoming
17235 * edges to blocks containting phi functions.
17236 */
17237 struct triple *first;
17238 struct triple *phi;
17239
17240 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017241 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017242 for(phi = first->next; phi != first ; phi = phi->next) {
17243 struct block_set *set;
17244 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017245 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017246 int edge;
17247 if (phi->op != OP_PHI) {
17248 continue;
17249 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017250 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017251 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017252 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017253 /* Phi's that feed into mandatory live range joins
17254 * cause nasty complications. Insert a copy of
17255 * the phi value so I never have to deal with
17256 * that in the rest of the code.
17257 */
17258 copy = post_copy(state, phi);
17259 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017260 /* Walk all of the incoming edges/blocks and insert moves.
17261 */
17262 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17263 struct block *eblock;
17264 struct triple *move;
17265 struct triple *val;
17266 struct triple *ptr;
17267 eblock = set->member;
17268 val = slot[edge];
17269
17270 if (val == phi) {
17271 continue;
17272 }
17273
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017274 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017275 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017276 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017277 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017278 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017279 use_triple(val, move);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017280
Eric Biedermanb138ac82003-04-22 18:44:01 +000017281 slot[edge] = move;
17282 unuse_triple(val, phi);
17283 use_triple(move, phi);
17284
Eric Biederman66fe2222003-07-04 15:14:04 +000017285 /* Walk up the dominator tree until I have found the appropriate block */
17286 while(eblock && !tdominates(state, val, eblock->last)) {
17287 eblock = eblock->idom;
17288 }
17289 if (!eblock) {
17290 internal_error(state, phi, "Cannot find block dominated by %p",
17291 val);
17292 }
17293
Eric Biedermanb138ac82003-04-22 18:44:01 +000017294 /* Walk through the block backwards to find
17295 * an appropriate location for the OP_COPY.
17296 */
17297 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17298 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017299 if (ptr->op == OP_PIECE) {
17300 ptr = MISC(ptr, 0);
17301 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017302 if ((ptr == phi) || (ptr == val)) {
17303 goto out;
17304 }
Eric Biederman90089602004-05-28 14:11:54 +000017305 expr = triple_lhs(state, ptr, 0);
17306 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17307 if ((*expr) == val) {
17308 goto out;
17309 }
17310 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017311 expr = triple_rhs(state, ptr, 0);
17312 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17313 if ((*expr) == phi) {
17314 goto out;
17315 }
17316 }
17317 }
17318 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017319 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017320 internal_error(state, ptr,
17321 "Could not insert write to phi");
17322 }
Eric Biederman90089602004-05-28 14:11:54 +000017323 insert_triple(state, after_lhs(state, ptr), move);
17324 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017325 eblock->last = move;
17326 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017327 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017328 }
17329 }
Eric Biederman90089602004-05-28 14:11:54 +000017330 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017331}
17332
Eric Biederman90089602004-05-28 14:11:54 +000017333struct triple_reg_set;
17334struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017335
Eric Biedermanb138ac82003-04-22 18:44:01 +000017336
Stefan Reinauer14e22772010-04-27 06:56:47 +000017337static int do_triple_set(struct triple_reg_set **head,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017338 struct triple *member, struct triple *new_member)
17339{
17340 struct triple_reg_set **ptr, *new;
17341 if (!member)
17342 return 0;
17343 ptr = head;
17344 while(*ptr) {
17345 if ((*ptr)->member == member) {
17346 return 0;
17347 }
17348 ptr = &(*ptr)->next;
17349 }
17350 new = xcmalloc(sizeof(*new), "triple_set");
17351 new->member = member;
17352 new->new = new_member;
17353 new->next = *head;
17354 *head = new;
17355 return 1;
17356}
17357
17358static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17359{
17360 struct triple_reg_set *entry, **ptr;
17361 ptr = head;
17362 while(*ptr) {
17363 entry = *ptr;
17364 if (entry->member == member) {
17365 *ptr = entry->next;
17366 xfree(entry);
17367 return;
17368 }
17369 else {
17370 ptr = &entry->next;
17371 }
17372 }
17373}
17374
17375static int in_triple(struct reg_block *rb, struct triple *in)
17376{
17377 return do_triple_set(&rb->in, in, 0);
17378}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017379
17380#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017381static void unin_triple(struct reg_block *rb, struct triple *unin)
17382{
17383 do_triple_unset(&rb->in, unin);
17384}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017385#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017386
17387static int out_triple(struct reg_block *rb, struct triple *out)
17388{
17389 return do_triple_set(&rb->out, out, 0);
17390}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017391#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017392static void unout_triple(struct reg_block *rb, struct triple *unout)
17393{
17394 do_triple_unset(&rb->out, unout);
17395}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017396#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017397
17398static int initialize_regblock(struct reg_block *blocks,
17399 struct block *block, int vertex)
17400{
17401 struct block_set *user;
17402 if (!block || (blocks[block->vertex].block == block)) {
17403 return vertex;
17404 }
17405 vertex += 1;
17406 /* Renumber the blocks in a convinient fashion */
17407 block->vertex = vertex;
17408 blocks[vertex].block = block;
17409 blocks[vertex].vertex = vertex;
17410 for(user = block->use; user; user = user->next) {
17411 vertex = initialize_regblock(blocks, user->member, vertex);
17412 }
17413 return vertex;
17414}
17415
Eric Biederman90089602004-05-28 14:11:54 +000017416static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17417{
17418/* Part to piece is a best attempt and it cannot be correct all by
17419 * itself. If various values are read as different sizes in different
17420 * parts of the code this function cannot work. Or rather it cannot
17421 * work in conjunction with compute_variable_liftimes. As the
17422 * analysis will get confused.
17423 */
17424 struct triple *base;
17425 unsigned reg;
17426 if (!is_lvalue(state, ins)) {
17427 return ins;
17428 }
17429 base = 0;
17430 reg = 0;
17431 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17432 base = MISC(ins, 0);
17433 switch(ins->op) {
17434 case OP_INDEX:
17435 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17436 break;
17437 case OP_DOT:
17438 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17439 break;
17440 default:
17441 internal_error(state, ins, "unhandled part");
17442 break;
17443 }
17444 ins = base;
17445 }
17446 if (base) {
17447 if (reg > base->lhs) {
17448 internal_error(state, base, "part out of range?");
17449 }
17450 ins = LHS(base, reg);
17451 }
17452 return ins;
17453}
17454
Stefan Reinauer14e22772010-04-27 06:56:47 +000017455static int this_def(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017456 struct triple *ins, struct triple *other)
17457{
17458 if (ins == other) {
17459 return 1;
17460 }
17461 if (ins->op == OP_WRITE) {
17462 ins = part_to_piece(state, MISC(ins, 0));
17463 }
17464 return ins == other;
17465}
17466
Eric Biedermanb138ac82003-04-22 18:44:01 +000017467static int phi_in(struct compile_state *state, struct reg_block *blocks,
17468 struct reg_block *rb, struct block *suc)
17469{
17470 /* Read the conditional input set of a successor block
17471 * (i.e. the input to the phi nodes) and place it in the
17472 * current blocks output set.
17473 */
17474 struct block_set *set;
17475 struct triple *ptr;
17476 int edge;
17477 int done, change;
17478 change = 0;
17479 /* Find the edge I am coming in on */
17480 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17481 if (set->member == rb->block) {
17482 break;
17483 }
17484 }
17485 if (!set) {
17486 internal_error(state, 0, "Not coming on a control edge?");
17487 }
17488 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17489 struct triple **slot, *expr, *ptr2;
17490 int out_change, done2;
17491 done = (ptr == suc->last);
17492 if (ptr->op != OP_PHI) {
17493 continue;
17494 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017495 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017496 expr = slot[edge];
17497 out_change = out_triple(rb, expr);
17498 if (!out_change) {
17499 continue;
17500 }
17501 /* If we don't define the variable also plast it
17502 * in the current blocks input set.
17503 */
17504 ptr2 = rb->block->first;
17505 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017506 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017507 break;
17508 }
17509 done2 = (ptr2 == rb->block->last);
17510 }
17511 if (!done2) {
17512 continue;
17513 }
17514 change |= in_triple(rb, expr);
17515 }
17516 return change;
17517}
17518
17519static int reg_in(struct compile_state *state, struct reg_block *blocks,
17520 struct reg_block *rb, struct block *suc)
17521{
17522 struct triple_reg_set *in_set;
17523 int change;
17524 change = 0;
17525 /* Read the input set of a successor block
17526 * and place it in the current blocks output set.
17527 */
17528 in_set = blocks[suc->vertex].in;
17529 for(; in_set; in_set = in_set->next) {
17530 int out_change, done;
17531 struct triple *first, *last, *ptr;
17532 out_change = out_triple(rb, in_set->member);
17533 if (!out_change) {
17534 continue;
17535 }
17536 /* If we don't define the variable also place it
17537 * in the current blocks input set.
17538 */
17539 first = rb->block->first;
17540 last = rb->block->last;
17541 done = 0;
17542 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017543 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017544 break;
17545 }
17546 done = (ptr == last);
17547 }
17548 if (!done) {
17549 continue;
17550 }
17551 change |= in_triple(rb, in_set->member);
17552 }
17553 change |= phi_in(state, blocks, rb, suc);
17554 return change;
17555}
17556
Eric Biedermanb138ac82003-04-22 18:44:01 +000017557static int use_in(struct compile_state *state, struct reg_block *rb)
17558{
17559 /* Find the variables we use but don't define and add
17560 * it to the current blocks input set.
17561 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017562#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017563#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017564#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017565 struct block *block;
17566 struct triple *ptr;
17567 int done;
17568 int change;
17569 block = rb->block;
17570 change = 0;
17571 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17572 struct triple **expr;
17573 done = (ptr == block->first);
17574 /* The variable a phi function uses depends on the
17575 * control flow, and is handled in phi_in, not
17576 * here.
17577 */
17578 if (ptr->op == OP_PHI) {
17579 continue;
17580 }
17581 expr = triple_rhs(state, ptr, 0);
17582 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17583 struct triple *rhs, *test;
17584 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017585 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017586 if (!rhs) {
17587 continue;
17588 }
Eric Biederman90089602004-05-28 14:11:54 +000017589
17590 /* See if rhs is defined in this block.
17591 * A write counts as a definition.
17592 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017593 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17594 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017595 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017596 rhs = 0;
17597 break;
17598 }
17599 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017600 /* If I still have a valid rhs add it to in */
17601 change |= in_triple(rb, rhs);
17602 }
17603 }
17604 return change;
17605}
17606
17607static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017608 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017609{
17610 struct reg_block *blocks;
17611 int change;
17612 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017613 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17614 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017615 do {
17616 int i;
17617 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017618 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017619 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017620 struct reg_block *rb;
17621 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017622 /* Add the all successor's input set to in */
17623 for(edge = rb->block->edges; edge; edge = edge->next) {
17624 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017625 }
17626 /* Add use to in... */
17627 change |= use_in(state, rb);
17628 }
17629 } while(change);
17630 return blocks;
17631}
17632
Stefan Reinauer14e22772010-04-27 06:56:47 +000017633static void free_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017634 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017635{
17636 int i;
17637 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017638 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017639 struct triple_reg_set *entry, *next;
17640 struct reg_block *rb;
17641 rb = &blocks[i];
17642 for(entry = rb->in; entry ; entry = next) {
17643 next = entry->next;
17644 do_triple_unset(&rb->in, entry->member);
17645 }
17646 for(entry = rb->out; entry; entry = next) {
17647 next = entry->next;
17648 do_triple_unset(&rb->out, entry->member);
17649 }
17650 }
17651 xfree(blocks);
17652
17653}
17654
Eric Biedermanf96a8102003-06-16 16:57:34 +000017655typedef void (*wvl_cb_t)(
Stefan Reinauer14e22772010-04-27 06:56:47 +000017656 struct compile_state *state,
17657 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000017658 struct reg_block *rb, struct triple *ins, void *arg);
17659
17660static void walk_variable_lifetimes(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000017661 struct basic_blocks *bb, struct reg_block *blocks,
Eric Biederman90089602004-05-28 14:11:54 +000017662 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017663{
17664 int i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000017665
Eric Biederman90089602004-05-28 14:11:54 +000017666 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017667 struct triple_reg_set *live;
17668 struct triple_reg_set *entry, *next;
17669 struct triple *ptr, *prev;
17670 struct reg_block *rb;
17671 struct block *block;
17672 int done;
17673
17674 /* Get the blocks */
17675 rb = &blocks[i];
17676 block = rb->block;
17677
17678 /* Copy out into live */
17679 live = 0;
17680 for(entry = rb->out; entry; entry = next) {
17681 next = entry->next;
17682 do_triple_set(&live, entry->member, entry->new);
17683 }
17684 /* Walk through the basic block calculating live */
17685 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017686 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017687
17688 prev = ptr->prev;
17689 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017690
17691 /* Ensure the current definition is in live */
17692 if (triple_is_def(state, ptr)) {
17693 do_triple_set(&live, ptr, 0);
17694 }
17695
17696 /* Inform the callback function of what is
17697 * going on.
17698 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017699 cb(state, blocks, live, rb, ptr, arg);
Stefan Reinauer14e22772010-04-27 06:56:47 +000017700
Eric Biedermanb138ac82003-04-22 18:44:01 +000017701 /* Remove the current definition from live */
17702 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017703
Eric Biedermanb138ac82003-04-22 18:44:01 +000017704 /* Add the current uses to live.
17705 *
17706 * It is safe to skip phi functions because they do
17707 * not have any block local uses, and the block
17708 * output sets already properly account for what
17709 * control flow depedent uses phi functions do have.
17710 */
17711 if (ptr->op == OP_PHI) {
17712 continue;
17713 }
17714 expr = triple_rhs(state, ptr, 0);
17715 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17716 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017717 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017718 continue;
17719 }
17720 do_triple_set(&live, *expr, 0);
17721 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017722 }
17723 /* Free live */
17724 for(entry = live; entry; entry = next) {
17725 next = entry->next;
17726 do_triple_unset(&live, entry->member);
17727 }
17728 }
17729}
17730
Eric Biederman90089602004-05-28 14:11:54 +000017731struct print_live_variable_info {
17732 struct reg_block *rb;
17733 FILE *fp;
17734};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017735#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017736static void print_live_variables_block(
17737 struct compile_state *state, struct block *block, void *arg)
17738
17739{
17740 struct print_live_variable_info *info = arg;
17741 struct block_set *edge;
17742 FILE *fp = info->fp;
17743 struct reg_block *rb;
17744 struct triple *ptr;
17745 int phi_present;
17746 int done;
17747 rb = &info->rb[block->vertex];
17748
17749 fprintf(fp, "\nblock: %p (%d),",
17750 block, block->vertex);
17751 for(edge = block->edges; edge; edge = edge->next) {
17752 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000017753 edge->member,
Eric Biederman90089602004-05-28 14:11:54 +000017754 edge->member && edge->member->use?edge->member->use->member : 0);
17755 }
17756 fprintf(fp, "\n");
17757 if (rb->in) {
17758 struct triple_reg_set *in_set;
17759 fprintf(fp, " in:");
17760 for(in_set = rb->in; in_set; in_set = in_set->next) {
17761 fprintf(fp, " %-10p", in_set->member);
17762 }
17763 fprintf(fp, "\n");
17764 }
17765 phi_present = 0;
17766 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17767 done = (ptr == block->last);
17768 if (ptr->op == OP_PHI) {
17769 phi_present = 1;
17770 break;
17771 }
17772 }
17773 if (phi_present) {
17774 int edge;
17775 for(edge = 0; edge < block->users; edge++) {
17776 fprintf(fp, " in(%d):", edge);
17777 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17778 struct triple **slot;
17779 done = (ptr == block->last);
17780 if (ptr->op != OP_PHI) {
17781 continue;
17782 }
17783 slot = &RHS(ptr, 0);
17784 fprintf(fp, " %-10p", slot[edge]);
17785 }
17786 fprintf(fp, "\n");
17787 }
17788 }
17789 if (block->first->op == OP_LABEL) {
17790 fprintf(fp, "%p:\n", block->first);
17791 }
17792 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17793 done = (ptr == block->last);
17794 display_triple(fp, ptr);
17795 }
17796 if (rb->out) {
17797 struct triple_reg_set *out_set;
17798 fprintf(fp, " out:");
17799 for(out_set = rb->out; out_set; out_set = out_set->next) {
17800 fprintf(fp, " %-10p", out_set->member);
17801 }
17802 fprintf(fp, "\n");
17803 }
17804 fprintf(fp, "\n");
17805}
17806
Stefan Reinauer14e22772010-04-27 06:56:47 +000017807static void print_live_variables(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017808 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17809{
17810 struct print_live_variable_info info;
17811 info.rb = rb;
17812 info.fp = fp;
17813 fprintf(fp, "\nlive variables by block\n");
17814 walk_blocks(state, bb, print_live_variables_block, &info);
17815
17816}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017817#endif
Eric Biederman90089602004-05-28 14:11:54 +000017818
Eric Biedermanb138ac82003-04-22 18:44:01 +000017819static int count_triples(struct compile_state *state)
17820{
17821 struct triple *first, *ins;
17822 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017823 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017824 ins = first;
17825 do {
17826 triples++;
17827 ins = ins->next;
17828 } while (ins != first);
17829 return triples;
17830}
Eric Biederman66fe2222003-07-04 15:14:04 +000017831
17832
Eric Biedermanb138ac82003-04-22 18:44:01 +000017833struct dead_triple {
17834 struct triple *triple;
17835 struct dead_triple *work_next;
17836 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017837 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017838 int flags;
17839#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017840#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017841};
17842
Stefan Reinauer14e22772010-04-27 06:56:47 +000017843static void print_dead_triples(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017844 struct dead_triple *dtriple)
17845{
17846 struct triple *first, *ins;
17847 struct dead_triple *dt;
17848 FILE *fp;
17849 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17850 return;
17851 }
17852 fp = state->dbgout;
17853 fprintf(fp, "--------------- dtriples ---------------\n");
17854 first = state->first;
17855 ins = first;
17856 do {
17857 dt = &dtriple[ins->id];
17858 if ((ins->op == OP_LABEL) && (ins->use)) {
17859 fprintf(fp, "\n%p:\n", ins);
17860 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000017861 fprintf(fp, "%c",
Eric Biederman90089602004-05-28 14:11:54 +000017862 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17863 display_triple(fp, ins);
17864 if (triple_is_branch(state, ins)) {
17865 fprintf(fp, "\n");
17866 }
17867 ins = ins->next;
17868 } while(ins != first);
17869 fprintf(fp, "\n");
17870}
17871
Eric Biedermanb138ac82003-04-22 18:44:01 +000017872
17873static void awaken(
17874 struct compile_state *state,
17875 struct dead_triple *dtriple, struct triple **expr,
17876 struct dead_triple ***work_list_tail)
17877{
17878 struct triple *triple;
17879 struct dead_triple *dt;
17880 if (!expr) {
17881 return;
17882 }
17883 triple = *expr;
17884 if (!triple) {
17885 return;
17886 }
17887 if (triple->id <= 0) {
17888 internal_error(state, triple, "bad triple id: %d",
17889 triple->id);
17890 }
17891 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017892 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017893 return;
17894 }
17895 dt = &dtriple[triple->id];
17896 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17897 dt->flags |= TRIPLE_FLAG_ALIVE;
17898 if (!dt->work_next) {
17899 **work_list_tail = dt;
17900 *work_list_tail = &dt->work_next;
17901 }
17902 }
17903}
17904
17905static void eliminate_inefectual_code(struct compile_state *state)
17906{
Eric Biedermanb138ac82003-04-22 18:44:01 +000017907 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17908 int triples, i;
Bernhard Urbanf31abe32012-02-01 16:30:30 +010017909 struct triple *first, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017910
Eric Biederman5ade04a2003-10-22 04:03:46 +000017911 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17912 return;
17913 }
17914
Eric Biedermanb138ac82003-04-22 18:44:01 +000017915 /* Setup the work list */
17916 work_list = 0;
17917 work_list_tail = &work_list;
17918
Eric Biederman83b991a2003-10-11 06:20:25 +000017919 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017920
17921 /* Count how many triples I have */
17922 triples = count_triples(state);
17923
17924 /* Now put then in an array and mark all of the triples dead */
17925 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
Stefan Reinauer14e22772010-04-27 06:56:47 +000017926
Eric Biedermanb138ac82003-04-22 18:44:01 +000017927 ins = first;
17928 i = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017929 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017930 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017931 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017932 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017933 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017934 ins->id = i;
17935 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017936 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017937 awaken(state, dtriple, &ins, &work_list_tail);
17938 }
17939 i++;
17940 ins = ins->next;
17941 } while(ins != first);
17942 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017943 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017944 struct dead_triple *dt;
17945 struct block_set *user;
17946 struct triple **expr;
17947 dt = work_list;
17948 work_list = dt->work_next;
17949 if (!work_list) {
17950 work_list_tail = &work_list;
17951 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017952 /* Make certain the block the current instruction is in lives */
17953 block = block_of_triple(state, dt->triple);
17954 awaken(state, dtriple, &block->first, &work_list_tail);
17955 if (triple_is_branch(state, block->last)) {
17956 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017957 } else {
17958 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017959 }
17960
Eric Biedermanb138ac82003-04-22 18:44:01 +000017961 /* Wake up the data depencencies of this triple */
17962 expr = 0;
17963 do {
17964 expr = triple_rhs(state, dt->triple, expr);
17965 awaken(state, dtriple, expr, &work_list_tail);
17966 } while(expr);
17967 do {
17968 expr = triple_lhs(state, dt->triple, expr);
17969 awaken(state, dtriple, expr, &work_list_tail);
17970 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017971 do {
17972 expr = triple_misc(state, dt->triple, expr);
17973 awaken(state, dtriple, expr, &work_list_tail);
17974 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017975 /* Wake up the forward control dependencies */
17976 do {
17977 expr = triple_targ(state, dt->triple, expr);
17978 awaken(state, dtriple, expr, &work_list_tail);
17979 } while(expr);
17980 /* Wake up the reverse control dependencies of this triple */
17981 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017982 struct triple *last;
17983 last = user->member->last;
17984 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017985#if DEBUG_ROMCC_WARNINGS
17986#warning "Should we bring the awakening noops back?"
17987#endif
17988 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017989 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017990 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017991 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017992 }
17993 }
Eric Biederman90089602004-05-28 14:11:54 +000017994 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017995 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000017996 if ((dt->triple->op == OP_NOOP) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000017997 (dt->flags & TRIPLE_FLAG_ALIVE)) {
17998 internal_error(state, dt->triple, "noop effective?");
17999 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018000 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018001 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018002 release_triple(state, dt->triple);
18003 }
18004 }
18005 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018006
18007 rebuild_ssa_form(state);
18008
Eric Biederman90089602004-05-28 14:11:54 +000018009 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018010}
18011
18012
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018013static void insert_mandatory_copies(struct compile_state *state)
18014{
18015 struct triple *ins, *first;
18016
18017 /* The object is with a minimum of inserted copies,
18018 * to resolve in fundamental register conflicts between
18019 * register value producers and consumers.
18020 * Theoretically we may be greater than minimal when we
18021 * are inserting copies before instructions but that
18022 * case should be rare.
18023 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018024 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018025 ins = first;
18026 do {
18027 struct triple_set *entry, *next;
18028 struct triple *tmp;
18029 struct reg_info info;
18030 unsigned reg, regcm;
18031 int do_post_copy, do_pre_copy;
18032 tmp = 0;
18033 if (!triple_is_def(state, ins)) {
18034 goto next;
18035 }
18036 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018037 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018038 if (info.reg >= MAX_REGISTERS) {
18039 info.reg = REG_UNSET;
18040 }
Eric Biederman90089602004-05-28 14:11:54 +000018041
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018042 reg = REG_UNSET;
18043 regcm = arch_type_to_regcm(state, ins->type);
18044 do_post_copy = do_pre_copy = 0;
18045
18046 /* Walk through the uses of ins and check for conflicts */
18047 for(entry = ins->use; entry; entry = next) {
18048 struct reg_info rinfo;
18049 int i;
18050 next = entry->next;
18051 i = find_rhs_use(state, entry->member, ins);
18052 if (i < 0) {
18053 continue;
18054 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018055
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018056 /* Find the users color requirements */
18057 rinfo = arch_reg_rhs(state, entry->member, i);
18058 if (rinfo.reg >= MAX_REGISTERS) {
18059 rinfo.reg = REG_UNSET;
18060 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018061
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018062 /* See if I need a pre_copy */
18063 if (rinfo.reg != REG_UNSET) {
18064 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18065 do_pre_copy = 1;
18066 }
18067 reg = rinfo.reg;
18068 }
18069 regcm &= rinfo.regcm;
18070 regcm = arch_regcm_normalize(state, regcm);
18071 if (regcm == 0) {
18072 do_pre_copy = 1;
18073 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018074 /* Always use pre_copies for constants.
18075 * They do not take up any registers until a
18076 * copy places them in one.
18077 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000018078 if ((info.reg == REG_UNNEEDED) &&
Eric Biedermand1ea5392003-06-28 06:49:45 +000018079 (rinfo.reg != REG_UNNEEDED)) {
18080 do_pre_copy = 1;
18081 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018082 }
18083 do_post_copy =
18084 !do_pre_copy &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000018085 (((info.reg != REG_UNSET) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018086 (reg != REG_UNSET) &&
18087 (info.reg != reg)) ||
18088 ((info.regcm & regcm) == 0));
18089
18090 reg = info.reg;
18091 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018092 /* 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 +000018093 for(entry = ins->use; entry; entry = next) {
18094 struct reg_info rinfo;
18095 int i;
18096 next = entry->next;
18097 i = find_rhs_use(state, entry->member, ins);
18098 if (i < 0) {
18099 continue;
18100 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018101
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018102 /* Find the users color requirements */
18103 rinfo = arch_reg_rhs(state, entry->member, i);
18104 if (rinfo.reg >= MAX_REGISTERS) {
18105 rinfo.reg = REG_UNSET;
18106 }
18107
18108 /* Now see if it is time to do the pre_copy */
18109 if (rinfo.reg != REG_UNSET) {
18110 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18111 ((regcm & rinfo.regcm) == 0) ||
18112 /* Don't let a mandatory coalesce sneak
18113 * into a operation that is marked to prevent
18114 * coalescing.
18115 */
18116 ((reg != REG_UNNEEDED) &&
18117 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18118 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18119 ) {
18120 if (do_pre_copy) {
18121 struct triple *user;
18122 user = entry->member;
18123 if (RHS(user, i) != ins) {
18124 internal_error(state, user, "bad rhs");
18125 }
18126 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018127 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018128 continue;
18129 } else {
18130 do_post_copy = 1;
18131 }
18132 }
18133 reg = rinfo.reg;
18134 }
18135 if ((regcm & rinfo.regcm) == 0) {
18136 if (do_pre_copy) {
18137 struct triple *user;
18138 user = entry->member;
18139 if (RHS(user, i) != ins) {
18140 internal_error(state, user, "bad rhs");
18141 }
18142 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018143 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018144 continue;
18145 } else {
18146 do_post_copy = 1;
18147 }
18148 }
18149 regcm &= rinfo.regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018150
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018151 }
18152 if (do_post_copy) {
18153 struct reg_info pre, post;
18154 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018155 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018156 pre = arch_reg_lhs(state, ins, 0);
18157 post = arch_reg_lhs(state, tmp, 0);
18158 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18159 internal_error(state, tmp, "useless copy");
18160 }
18161 }
18162 next:
18163 ins = ins->next;
18164 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018165
Eric Biederman90089602004-05-28 14:11:54 +000018166 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018167}
18168
18169
Eric Biedermanb138ac82003-04-22 18:44:01 +000018170struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018171struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018172struct live_range {
18173 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018174 struct live_range_def *defs;
18175/* Note. The list pointed to by defs is kept in order.
18176 * That is baring splits in the flow control
18177 * defs dominates defs->next wich dominates defs->next->next
18178 * etc.
18179 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018180 unsigned color;
18181 unsigned classes;
18182 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018183 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018184 struct live_range *group_next, **group_prev;
18185};
18186
18187struct live_range_edge {
18188 struct live_range_edge *next;
18189 struct live_range *node;
18190};
18191
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018192struct live_range_def {
18193 struct live_range_def *next;
18194 struct live_range_def *prev;
18195 struct live_range *lr;
18196 struct triple *def;
18197 unsigned orig_id;
18198};
18199
Eric Biedermanb138ac82003-04-22 18:44:01 +000018200#define LRE_HASH_SIZE 2048
18201struct lre_hash {
18202 struct lre_hash *next;
18203 struct live_range *left;
18204 struct live_range *right;
18205};
18206
18207
18208struct reg_state {
18209 struct lre_hash *hash[LRE_HASH_SIZE];
18210 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018211 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018212 struct live_range *lr;
18213 struct live_range *low, **low_tail;
18214 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018215 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018216 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018217 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018218};
18219
18220
Eric Biedermand1ea5392003-06-28 06:49:45 +000018221struct print_interference_block_info {
18222 struct reg_state *rstate;
18223 FILE *fp;
18224 int need_edges;
18225};
18226static void print_interference_block(
18227 struct compile_state *state, struct block *block, void *arg)
18228
18229{
18230 struct print_interference_block_info *info = arg;
18231 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018232 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018233 FILE *fp = info->fp;
18234 struct reg_block *rb;
18235 struct triple *ptr;
18236 int phi_present;
18237 int done;
18238 rb = &rstate->blocks[block->vertex];
18239
Eric Biederman5ade04a2003-10-22 04:03:46 +000018240 fprintf(fp, "\nblock: %p (%d),",
18241 block, block->vertex);
18242 for(edge = block->edges; edge; edge = edge->next) {
18243 fprintf(fp, " %p<-%p",
Stefan Reinauer14e22772010-04-27 06:56:47 +000018244 edge->member,
Eric Biederman5ade04a2003-10-22 04:03:46 +000018245 edge->member && edge->member->use?edge->member->use->member : 0);
18246 }
18247 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018248 if (rb->in) {
18249 struct triple_reg_set *in_set;
18250 fprintf(fp, " in:");
18251 for(in_set = rb->in; in_set; in_set = in_set->next) {
18252 fprintf(fp, " %-10p", in_set->member);
18253 }
18254 fprintf(fp, "\n");
18255 }
18256 phi_present = 0;
18257 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18258 done = (ptr == block->last);
18259 if (ptr->op == OP_PHI) {
18260 phi_present = 1;
18261 break;
18262 }
18263 }
18264 if (phi_present) {
18265 int edge;
18266 for(edge = 0; edge < block->users; edge++) {
18267 fprintf(fp, " in(%d):", edge);
18268 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18269 struct triple **slot;
18270 done = (ptr == block->last);
18271 if (ptr->op != OP_PHI) {
18272 continue;
18273 }
18274 slot = &RHS(ptr, 0);
18275 fprintf(fp, " %-10p", slot[edge]);
18276 }
18277 fprintf(fp, "\n");
18278 }
18279 }
18280 if (block->first->op == OP_LABEL) {
18281 fprintf(fp, "%p:\n", block->first);
18282 }
18283 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018284 struct live_range *lr;
18285 unsigned id;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018286 done = (ptr == block->last);
18287 lr = rstate->lrd[ptr->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018288
Eric Biedermand1ea5392003-06-28 06:49:45 +000018289 id = ptr->id;
18290 ptr->id = rstate->lrd[id].orig_id;
18291 SET_REG(ptr->id, lr->color);
18292 display_triple(fp, ptr);
18293 ptr->id = id;
18294
18295 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18296 internal_error(state, ptr, "lr has no defs!");
18297 }
18298 if (info->need_edges) {
18299 if (lr->defs) {
18300 struct live_range_def *lrd;
18301 fprintf(fp, " range:");
18302 lrd = lr->defs;
18303 do {
18304 fprintf(fp, " %-10p", lrd->def);
18305 lrd = lrd->next;
18306 } while(lrd != lr->defs);
18307 fprintf(fp, "\n");
18308 }
18309 if (lr->edges > 0) {
18310 struct live_range_edge *edge;
18311 fprintf(fp, " edges:");
18312 for(edge = lr->edges; edge; edge = edge->next) {
18313 struct live_range_def *lrd;
18314 lrd = edge->node->defs;
18315 do {
18316 fprintf(fp, " %-10p", lrd->def);
18317 lrd = lrd->next;
18318 } while(lrd != edge->node->defs);
18319 fprintf(fp, "|");
18320 }
18321 fprintf(fp, "\n");
18322 }
18323 }
18324 /* Do a bunch of sanity checks */
18325 valid_ins(state, ptr);
18326 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18327 internal_error(state, ptr, "Invalid triple id: %d",
18328 ptr->id);
18329 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018330 }
18331 if (rb->out) {
18332 struct triple_reg_set *out_set;
18333 fprintf(fp, " out:");
18334 for(out_set = rb->out; out_set; out_set = out_set->next) {
18335 fprintf(fp, " %-10p", out_set->member);
18336 }
18337 fprintf(fp, "\n");
18338 }
18339 fprintf(fp, "\n");
18340}
18341
18342static void print_interference_blocks(
18343 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18344{
18345 struct print_interference_block_info info;
18346 info.rstate = rstate;
18347 info.fp = fp;
18348 info.need_edges = need_edges;
18349 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018350 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018351
18352}
18353
Eric Biedermanb138ac82003-04-22 18:44:01 +000018354static unsigned regc_max_size(struct compile_state *state, int classes)
18355{
18356 unsigned max_size;
18357 int i;
18358 max_size = 0;
18359 for(i = 0; i < MAX_REGC; i++) {
18360 if (classes & (1 << i)) {
18361 unsigned size;
18362 size = arch_regc_size(state, i);
18363 if (size > max_size) {
18364 max_size = size;
18365 }
18366 }
18367 }
18368 return max_size;
18369}
18370
18371static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18372{
18373 unsigned equivs[MAX_REG_EQUIVS];
18374 int i;
18375 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18376 internal_error(state, 0, "invalid register");
18377 }
18378 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18379 internal_error(state, 0, "invalid register");
18380 }
18381 arch_reg_equivs(state, equivs, reg1);
18382 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18383 if (equivs[i] == reg2) {
18384 return 1;
18385 }
18386 }
18387 return 0;
18388}
18389
18390static void reg_fill_used(struct compile_state *state, char *used, int reg)
18391{
18392 unsigned equivs[MAX_REG_EQUIVS];
18393 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018394 if (reg == REG_UNNEEDED) {
18395 return;
18396 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018397 arch_reg_equivs(state, equivs, reg);
18398 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18399 used[equivs[i]] = 1;
18400 }
18401 return;
18402}
18403
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018404static void reg_inc_used(struct compile_state *state, char *used, int reg)
18405{
18406 unsigned equivs[MAX_REG_EQUIVS];
18407 int i;
18408 if (reg == REG_UNNEEDED) {
18409 return;
18410 }
18411 arch_reg_equivs(state, equivs, reg);
18412 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18413 used[equivs[i]] += 1;
18414 }
18415 return;
18416}
18417
Eric Biedermanb138ac82003-04-22 18:44:01 +000018418static unsigned int hash_live_edge(
18419 struct live_range *left, struct live_range *right)
18420{
18421 unsigned int hash, val;
18422 unsigned long lval, rval;
18423 lval = ((unsigned long)left)/sizeof(struct live_range);
18424 rval = ((unsigned long)right)/sizeof(struct live_range);
18425 hash = 0;
18426 while(lval) {
18427 val = lval & 0xff;
18428 lval >>= 8;
18429 hash = (hash *263) + val;
18430 }
18431 while(rval) {
18432 val = rval & 0xff;
18433 rval >>= 8;
18434 hash = (hash *263) + val;
18435 }
18436 hash = hash & (LRE_HASH_SIZE - 1);
18437 return hash;
18438}
18439
18440static struct lre_hash **lre_probe(struct reg_state *rstate,
18441 struct live_range *left, struct live_range *right)
18442{
18443 struct lre_hash **ptr;
18444 unsigned int index;
18445 /* Ensure left <= right */
18446 if (left > right) {
18447 struct live_range *tmp;
18448 tmp = left;
18449 left = right;
18450 right = tmp;
18451 }
18452 index = hash_live_edge(left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000018453
Eric Biedermanb138ac82003-04-22 18:44:01 +000018454 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018455 while(*ptr) {
18456 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18457 break;
18458 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018459 ptr = &(*ptr)->next;
18460 }
18461 return ptr;
18462}
18463
18464static int interfere(struct reg_state *rstate,
18465 struct live_range *left, struct live_range *right)
18466{
18467 struct lre_hash **ptr;
18468 ptr = lre_probe(rstate, left, right);
18469 return ptr && *ptr;
18470}
18471
Stefan Reinauer14e22772010-04-27 06:56:47 +000018472static void add_live_edge(struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018473 struct live_range *left, struct live_range *right)
18474{
18475 /* FIXME the memory allocation overhead is noticeable here... */
18476 struct lre_hash **ptr, *new_hash;
18477 struct live_range_edge *edge;
18478
18479 if (left == right) {
18480 return;
18481 }
18482 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18483 return;
18484 }
18485 /* Ensure left <= right */
18486 if (left > right) {
18487 struct live_range *tmp;
18488 tmp = left;
18489 left = right;
18490 right = tmp;
18491 }
18492 ptr = lre_probe(rstate, left, right);
18493 if (*ptr) {
18494 return;
18495 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018496#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018497 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018498 left, right);
18499#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018500 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18501 new_hash->next = *ptr;
18502 new_hash->left = left;
18503 new_hash->right = right;
18504 *ptr = new_hash;
18505
18506 edge = xmalloc(sizeof(*edge), "live_range_edge");
18507 edge->next = left->edges;
18508 edge->node = right;
18509 left->edges = edge;
18510 left->degree += 1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018511
Eric Biedermanb138ac82003-04-22 18:44:01 +000018512 edge = xmalloc(sizeof(*edge), "live_range_edge");
18513 edge->next = right->edges;
18514 edge->node = left;
18515 right->edges = edge;
18516 right->degree += 1;
18517}
18518
18519static void remove_live_edge(struct reg_state *rstate,
18520 struct live_range *left, struct live_range *right)
18521{
18522 struct live_range_edge *edge, **ptr;
18523 struct lre_hash **hptr, *entry;
18524 hptr = lre_probe(rstate, left, right);
18525 if (!hptr || !*hptr) {
18526 return;
18527 }
18528 entry = *hptr;
18529 *hptr = entry->next;
18530 xfree(entry);
18531
18532 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18533 edge = *ptr;
18534 if (edge->node == right) {
18535 *ptr = edge->next;
18536 memset(edge, 0, sizeof(*edge));
18537 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018538 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018539 break;
18540 }
18541 }
18542 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18543 edge = *ptr;
18544 if (edge->node == left) {
18545 *ptr = edge->next;
18546 memset(edge, 0, sizeof(*edge));
18547 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018548 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018549 break;
18550 }
18551 }
18552}
18553
18554static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18555{
18556 struct live_range_edge *edge, *next;
18557 for(edge = range->edges; edge; edge = next) {
18558 next = edge->next;
18559 remove_live_edge(rstate, range, edge->node);
18560 }
18561}
18562
Stefan Reinauer14e22772010-04-27 06:56:47 +000018563static void transfer_live_edges(struct reg_state *rstate,
Eric Biederman153ea352003-06-20 14:43:20 +000018564 struct live_range *dest, struct live_range *src)
18565{
18566 struct live_range_edge *edge, *next;
18567 for(edge = src->edges; edge; edge = next) {
18568 struct live_range *other;
18569 next = edge->next;
18570 other = edge->node;
18571 remove_live_edge(rstate, src, other);
18572 add_live_edge(rstate, dest, other);
18573 }
18574}
18575
Eric Biedermanb138ac82003-04-22 18:44:01 +000018576
18577/* Interference graph...
Stefan Reinauer14e22772010-04-27 06:56:47 +000018578 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000018579 * new(n) --- Return a graph with n nodes but no edges.
18580 * add(g,x,y) --- Return a graph including g with an between x and y
18581 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18582 * x and y in the graph g
18583 * degree(g, x) --- Return the degree of the node x in the graph g
18584 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18585 *
18586 * Implement with a hash table && a set of adjcency vectors.
18587 * The hash table supports constant time implementations of add and interfere.
18588 * The adjacency vectors support an efficient implementation of neighbors.
18589 */
18590
Stefan Reinauer14e22772010-04-27 06:56:47 +000018591/*
Eric Biedermanb138ac82003-04-22 18:44:01 +000018592 * +---------------------------------------------------+
18593 * | +--------------+ |
18594 * v v | |
Stefan Reinauer14e22772010-04-27 06:56:47 +000018595 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
Eric Biedermanb138ac82003-04-22 18:44:01 +000018596 *
18597 * -- In simplify implment optimistic coloring... (No backtracking)
18598 * -- Implement Rematerialization it is the only form of spilling we can perform
18599 * Essentially this means dropping a constant from a register because
18600 * we can regenerate it later.
18601 *
18602 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18603 * coalesce at phi points...
18604 * --- Bias coloring if at all possible do the coalesing a compile time.
18605 *
18606 *
18607 */
18608
Stefan Reinauer50542a82007-10-24 11:14:14 +000018609#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018610static void different_colored(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018611 struct compile_state *state, struct reg_state *rstate,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018612 struct triple *parent, struct triple *ins)
18613{
18614 struct live_range *lr;
18615 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018616 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018617 expr = triple_rhs(state, ins, 0);
18618 for(;expr; expr = triple_rhs(state, ins, expr)) {
18619 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018620 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018621 continue;
18622 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018623 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018624 if (lr->color == lr2->color) {
18625 internal_error(state, ins, "live range too big");
18626 }
18627 }
18628}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018629#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018630
18631static struct live_range *coalesce_ranges(
18632 struct compile_state *state, struct reg_state *rstate,
18633 struct live_range *lr1, struct live_range *lr2)
18634{
18635 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18636 unsigned color;
18637 unsigned classes;
18638 if (lr1 == lr2) {
18639 return lr1;
18640 }
18641 if (!lr1->defs || !lr2->defs) {
18642 internal_error(state, 0,
18643 "cannot coalese dead live ranges");
18644 }
18645 if ((lr1->color == REG_UNNEEDED) ||
18646 (lr2->color == REG_UNNEEDED)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018647 internal_error(state, 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018648 "cannot coalesce live ranges without a possible color");
18649 }
18650 if ((lr1->color != lr2->color) &&
18651 (lr1->color != REG_UNSET) &&
18652 (lr2->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018653 internal_error(state, lr1->defs->def,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018654 "cannot coalesce live ranges of different colors");
18655 }
18656 color = lr1->color;
18657 if (color == REG_UNSET) {
18658 color = lr2->color;
18659 }
18660 classes = lr1->classes & lr2->classes;
18661 if (!classes) {
18662 internal_error(state, lr1->defs->def,
18663 "cannot coalesce live ranges with dissimilar register classes");
18664 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018665 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018666 FILE *fp = state->errout;
18667 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018668 lrd = lr1->defs;
18669 do {
Eric Biederman90089602004-05-28 14:11:54 +000018670 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018671 lrd = lrd->next;
18672 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018673 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018674 lrd = lr2->defs;
18675 do {
Eric Biederman90089602004-05-28 14:11:54 +000018676 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018677 lrd = lrd->next;
18678 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018679 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018680 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018681 /* If there is a clear dominate live range put it in lr1,
18682 * For purposes of this test phi functions are
18683 * considered dominated by the definitions that feed into
Stefan Reinauer14e22772010-04-27 06:56:47 +000018684 * them.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018685 */
18686 if ((lr1->defs->prev->def->op == OP_PHI) ||
18687 ((lr2->defs->prev->def->op != OP_PHI) &&
18688 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18689 struct live_range *tmp;
18690 tmp = lr1;
18691 lr1 = lr2;
18692 lr2 = tmp;
18693 }
18694#if 0
18695 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018696 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018697 }
18698 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018699 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018700 }
18701 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018702 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018703 }
18704 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018705 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018706 }
18707#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018708#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018709 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018710 lr1->defs->def,
18711 lr1->color,
18712 lr2->defs->def,
18713 lr2->color);
18714#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018715
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018716 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018717#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018718#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018719#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000018720 /* This FIXME item applies to the correctness of live_range_end
Eric Biederman153ea352003-06-20 14:43:20 +000018721 * and to the necessity of making multiple passes of coalesce_live_ranges.
18722 * A failure to find some coalesce opportunities in coaleace_live_ranges
18723 * does not impact the correct of the compiler just the efficiency with
18724 * which registers are allocated.
18725 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018726 head = lr1->defs;
18727 mid1 = lr1->defs->prev;
18728 mid2 = lr2->defs;
18729 end = lr2->defs->prev;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018730
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018731 head->prev = end;
18732 end->next = head;
18733
18734 mid1->next = mid2;
18735 mid2->prev = mid1;
18736
18737 /* Fixup the live range in the added live range defs */
18738 lrd = head;
18739 do {
18740 lrd->lr = lr1;
18741 lrd = lrd->next;
18742 } while(lrd != head);
18743
18744 /* Mark lr2 as free. */
18745 lr2->defs = 0;
18746 lr2->color = REG_UNNEEDED;
18747 lr2->classes = 0;
18748
18749 if (!lr1->defs) {
18750 internal_error(state, 0, "lr1->defs == 0 ?");
18751 }
18752
18753 lr1->color = color;
18754 lr1->classes = classes;
18755
Eric Biederman153ea352003-06-20 14:43:20 +000018756 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18757 transfer_live_edges(rstate, lr1, lr2);
18758
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018759 return lr1;
18760}
18761
18762static struct live_range_def *live_range_head(
18763 struct compile_state *state, struct live_range *lr,
18764 struct live_range_def *last)
18765{
18766 struct live_range_def *result;
18767 result = 0;
18768 if (last == 0) {
18769 result = lr->defs;
18770 }
18771 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18772 result = last->next;
18773 }
18774 return result;
18775}
18776
18777static struct live_range_def *live_range_end(
18778 struct compile_state *state, struct live_range *lr,
18779 struct live_range_def *last)
18780{
18781 struct live_range_def *result;
18782 result = 0;
18783 if (last == 0) {
18784 result = lr->defs->prev;
18785 }
18786 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18787 result = last->prev;
18788 }
18789 return result;
18790}
18791
18792
Eric Biedermanb138ac82003-04-22 18:44:01 +000018793static void initialize_live_ranges(
18794 struct compile_state *state, struct reg_state *rstate)
18795{
18796 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018797 size_t count, size;
18798 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018799
Eric Biederman83b991a2003-10-11 06:20:25 +000018800 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018801 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018802 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018803 count = count_triples(state);
18804 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018805 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018806 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018807 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018808 /* Potentially I need one live range for each instruction
18809 * plus an extra for the dummy live range.
18810 */
18811 rstate->ranges = count + 1;
18812 size = sizeof(rstate->lrd[0]) * rstate->defs;
18813 rstate->lrd = xcmalloc(size, "live_range_def");
18814 size = sizeof(rstate->lr[0]) * rstate->ranges;
18815 rstate->lr = xcmalloc(size, "live_range");
18816
Eric Biedermanb138ac82003-04-22 18:44:01 +000018817 /* Setup the dummy live range */
18818 rstate->lr[0].classes = 0;
18819 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018820 rstate->lr[0].defs = 0;
18821 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018822 ins = first;
18823 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018824 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018825 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018826 struct reg_info info;
18827 /* Find the architecture specific color information */
18828 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018829 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018830 rstate->lr[i].defs = &rstate->lrd[j];
18831 rstate->lr[i].color = info.reg;
18832 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018833 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018834 rstate->lrd[j].lr = &rstate->lr[i];
Stefan Reinauer14e22772010-04-27 06:56:47 +000018835 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018836 /* Otherwise give the triple the dummy live range. */
18837 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018838 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018839 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018840
18841 /* Initalize the live_range_def */
18842 rstate->lrd[j].next = &rstate->lrd[j];
18843 rstate->lrd[j].prev = &rstate->lrd[j];
18844 rstate->lrd[j].def = ins;
18845 rstate->lrd[j].orig_id = ins->id;
18846 ins->id = j;
18847
18848 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018849 ins = ins->next;
18850 } while(ins != first);
18851 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018852
Eric Biedermanb138ac82003-04-22 18:44:01 +000018853 /* Make a second pass to handle achitecture specific register
18854 * constraints.
18855 */
18856 ins = first;
18857 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018858 int zlhs, zrhs, i, j;
18859 if (ins->id > rstate->defs) {
18860 internal_error(state, ins, "bad id");
18861 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000018862
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018863 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018864 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018865 if ((zlhs == 0) && triple_is_def(state, ins)) {
18866 zlhs = 1;
18867 }
Eric Biederman90089602004-05-28 14:11:54 +000018868 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018869
Eric Biederman5ade04a2003-10-22 04:03:46 +000018870 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018871 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018872 ins, zlhs, zrhs);
18873 }
18874
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018875 for(i = 0; i < zlhs; i++) {
18876 struct reg_info linfo;
18877 struct live_range_def *lhs;
18878 linfo = arch_reg_lhs(state, ins, i);
18879 if (linfo.reg < MAX_REGISTERS) {
18880 continue;
18881 }
18882 if (triple_is_def(state, ins)) {
18883 lhs = &rstate->lrd[ins->id];
18884 } else {
18885 lhs = &rstate->lrd[LHS(ins, i)->id];
18886 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018887
18888 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018889 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018890 i, lhs, linfo.reg);
18891 }
18892
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018893 for(j = 0; j < zrhs; j++) {
18894 struct reg_info rinfo;
18895 struct live_range_def *rhs;
18896 rinfo = arch_reg_rhs(state, ins, j);
18897 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018898 continue;
18899 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018900 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018901
18902 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018903 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018904 j, rhs, rinfo.reg);
18905 }
18906
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018907 if (rinfo.reg == linfo.reg) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000018908 coalesce_ranges(state, rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018909 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018910 }
18911 }
18912 }
18913 ins = ins->next;
18914 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018915}
18916
Eric Biedermanf96a8102003-06-16 16:57:34 +000018917static void graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018918 struct compile_state *state,
18919 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000018920 struct reg_block *rb, struct triple *ins, void *arg)
18921{
18922 struct reg_state *rstate = arg;
18923 struct live_range *def;
18924 struct triple_reg_set *entry;
18925
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018926 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018927 * we do not have a definition to add to
18928 * the interference graph.
18929 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018930 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018931 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018932 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018933 def = rstate->lrd[ins->id].lr;
Stefan Reinauer14e22772010-04-27 06:56:47 +000018934
Eric Biedermanb138ac82003-04-22 18:44:01 +000018935 /* Create an edge between ins and everything that is
18936 * alive, unless the live_range cannot share
18937 * a physical register with ins.
18938 */
18939 for(entry = live; entry; entry = entry->next) {
18940 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018941 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18942 internal_error(state, 0, "bad entry?");
18943 }
18944 lr = rstate->lrd[entry->member->id].lr;
18945 if (def == lr) {
18946 continue;
18947 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018948 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18949 continue;
18950 }
18951 add_live_edge(rstate, def, lr);
18952 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018953 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018954}
18955
Stefan Reinauer50542a82007-10-24 11:14:14 +000018956#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018957static struct live_range *get_verify_live_range(
18958 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18959{
18960 struct live_range *lr;
18961 struct live_range_def *lrd;
18962 int ins_found;
18963 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18964 internal_error(state, ins, "bad ins?");
18965 }
18966 lr = rstate->lrd[ins->id].lr;
18967 ins_found = 0;
18968 lrd = lr->defs;
18969 do {
18970 if (lrd->def == ins) {
18971 ins_found = 1;
18972 }
18973 lrd = lrd->next;
18974 } while(lrd != lr->defs);
18975 if (!ins_found) {
18976 internal_error(state, ins, "ins not in live range");
18977 }
18978 return lr;
18979}
18980
18981static void verify_graph_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000018982 struct compile_state *state,
18983 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018984 struct reg_block *rb, struct triple *ins, void *arg)
18985{
18986 struct reg_state *rstate = arg;
18987 struct triple_reg_set *entry1, *entry2;
18988
18989
18990 /* Compare live against edges and make certain the code is working */
18991 for(entry1 = live; entry1; entry1 = entry1->next) {
18992 struct live_range *lr1;
18993 lr1 = get_verify_live_range(state, rstate, entry1->member);
18994 for(entry2 = live; entry2; entry2 = entry2->next) {
18995 struct live_range *lr2;
18996 struct live_range_edge *edge2;
18997 int lr1_found;
18998 int lr2_degree;
18999 if (entry2 == entry1) {
19000 continue;
19001 }
19002 lr2 = get_verify_live_range(state, rstate, entry2->member);
19003 if (lr1 == lr2) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019004 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019005 "live range with 2 values simultaneously alive");
19006 }
19007 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19008 continue;
19009 }
19010 if (!interfere(rstate, lr1, lr2)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019011 internal_error(state, entry2->member,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019012 "edges don't interfere?");
19013 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019014
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019015 lr1_found = 0;
19016 lr2_degree = 0;
19017 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19018 lr2_degree++;
19019 if (edge2->node == lr1) {
19020 lr1_found = 1;
19021 }
19022 }
19023 if (lr2_degree != lr2->degree) {
19024 internal_error(state, entry2->member,
19025 "computed degree: %d does not match reported degree: %d\n",
19026 lr2_degree, lr2->degree);
19027 }
19028 if (!lr1_found) {
19029 internal_error(state, entry2->member, "missing edge");
19030 }
19031 }
19032 }
19033 return;
19034}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019035#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019036
Eric Biedermanf96a8102003-06-16 16:57:34 +000019037static void print_interference_ins(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019038 struct compile_state *state,
19039 struct reg_block *blocks, struct triple_reg_set *live,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019040 struct reg_block *rb, struct triple *ins, void *arg)
19041{
19042 struct reg_state *rstate = arg;
19043 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019044 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019045 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019046
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019047 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019048 id = ins->id;
19049 ins->id = rstate->lrd[id].orig_id;
19050 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019051 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019052 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019053
19054 if (lr->defs) {
19055 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019056 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019057 lrd = lr->defs;
19058 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019059 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019060 lrd = lrd->next;
19061 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019062 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019063 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019064 if (live) {
19065 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019066 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019067 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019068 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019069 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019070 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019071 }
19072 if (lr->edges) {
19073 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019074 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019075 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019076 struct live_range_def *lrd;
19077 lrd = entry->node->defs;
19078 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019079 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019080 lrd = lrd->next;
19081 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019082 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019083 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019084 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019085 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019086 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019087 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019088 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019089 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019090}
19091
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019092static int coalesce_live_ranges(
19093 struct compile_state *state, struct reg_state *rstate)
19094{
19095 /* At the point where a value is moved from one
19096 * register to another that value requires two
19097 * registers, thus increasing register pressure.
19098 * Live range coaleescing reduces the register
19099 * pressure by keeping a value in one register
19100 * longer.
19101 *
19102 * In the case of a phi function all paths leading
19103 * into it must be allocated to the same register
19104 * otherwise the phi function may not be removed.
19105 *
19106 * Forcing a value to stay in a single register
19107 * for an extended period of time does have
19108 * limitations when applied to non homogenous
Stefan Reinauer14e22772010-04-27 06:56:47 +000019109 * register pool.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019110 *
19111 * The two cases I have identified are:
19112 * 1) Two forced register assignments may
19113 * collide.
19114 * 2) Registers may go unused because they
19115 * are only good for storing the value
19116 * and not manipulating it.
19117 *
19118 * Because of this I need to split live ranges,
19119 * even outside of the context of coalesced live
19120 * ranges. The need to split live ranges does
19121 * impose some constraints on live range coalescing.
19122 *
19123 * - Live ranges may not be coalesced across phi
19124 * functions. This creates a 2 headed live
19125 * range that cannot be sanely split.
19126 *
Stefan Reinauer14e22772010-04-27 06:56:47 +000019127 * - phi functions (coalesced in initialize_live_ranges)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019128 * are handled as pre split live ranges so we will
19129 * never attempt to split them.
19130 */
19131 int coalesced;
19132 int i;
19133
19134 coalesced = 0;
19135 for(i = 0; i <= rstate->ranges; i++) {
19136 struct live_range *lr1;
19137 struct live_range_def *lrd1;
19138 lr1 = &rstate->lr[i];
19139 if (!lr1->defs) {
19140 continue;
19141 }
19142 lrd1 = live_range_end(state, lr1, 0);
19143 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19144 struct triple_set *set;
19145 if (lrd1->def->op != OP_COPY) {
19146 continue;
19147 }
19148 /* Skip copies that are the result of a live range split. */
19149 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19150 continue;
19151 }
19152 for(set = lrd1->def->use; set; set = set->next) {
19153 struct live_range_def *lrd2;
19154 struct live_range *lr2, *res;
19155
19156 lrd2 = &rstate->lrd[set->member->id];
19157
19158 /* Don't coalesce with instructions
19159 * that are the result of a live range
19160 * split.
19161 */
19162 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19163 continue;
19164 }
19165 lr2 = rstate->lrd[set->member->id].lr;
19166 if (lr1 == lr2) {
19167 continue;
19168 }
19169 if ((lr1->color != lr2->color) &&
19170 (lr1->color != REG_UNSET) &&
19171 (lr2->color != REG_UNSET)) {
19172 continue;
19173 }
19174 if ((lr1->classes & lr2->classes) == 0) {
19175 continue;
19176 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019177
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019178 if (interfere(rstate, lr1, lr2)) {
19179 continue;
19180 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019181
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019182 res = coalesce_ranges(state, rstate, lr1, lr2);
19183 coalesced += 1;
19184 if (res != lr1) {
19185 goto next;
19186 }
19187 }
19188 }
19189 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019190 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019191 }
19192 return coalesced;
19193}
19194
19195
Eric Biedermanf96a8102003-06-16 16:57:34 +000019196static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019197 struct reg_block *blocks, struct triple_reg_set *live,
19198 struct reg_block *rb, struct triple *ins, void *arg)
19199{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019200 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019201 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019202
19203 /* See if we have a mandatory coalesce operation between
19204 * a lhs and a rhs value. If so and the rhs value is also
19205 * alive then this triple needs to be pre copied. Otherwise
19206 * we would have two definitions in the same live range simultaneously
19207 * alive.
19208 */
Eric Biederman90089602004-05-28 14:11:54 +000019209 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019210 if ((zlhs == 0) && triple_is_def(state, ins)) {
19211 zlhs = 1;
19212 }
Eric Biederman90089602004-05-28 14:11:54 +000019213 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019214 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019215 struct reg_info linfo;
19216 linfo = arch_reg_lhs(state, ins, i);
19217 if (linfo.reg < MAX_REGISTERS) {
19218 continue;
19219 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019220 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019221 struct reg_info rinfo;
19222 struct triple *rhs;
19223 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019224 int found;
19225 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019226 rinfo = arch_reg_rhs(state, ins, j);
19227 if (rinfo.reg != linfo.reg) {
19228 continue;
19229 }
19230 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019231 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019232 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019233 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019234 }
19235 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019236 if (found) {
19237 struct triple *copy;
19238 copy = pre_copy(state, ins, j);
19239 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019240 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019241 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019242 }
19243 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019244 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019245}
19246
Eric Biedermand1ea5392003-06-28 06:49:45 +000019247static int correct_coalesce_conflicts(
19248 struct compile_state *state, struct reg_block *blocks)
19249{
19250 int conflicts;
19251 conflicts = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019252 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019253 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019254 return conflicts;
19255}
19256
Eric Biedermanf96a8102003-06-16 16:57:34 +000019257static void replace_set_use(struct compile_state *state,
19258 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019259{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019260 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019261 for(set = head; set; set = set->next) {
19262 if (set->member == orig) {
19263 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019264 }
19265 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019266}
19267
Stefan Reinauer14e22772010-04-27 06:56:47 +000019268static void replace_block_use(struct compile_state *state,
Eric Biedermanf96a8102003-06-16 16:57:34 +000019269 struct reg_block *blocks, struct triple *orig, struct triple *new)
19270{
19271 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019272#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019273#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019274#endif
Eric Biederman90089602004-05-28 14:11:54 +000019275 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019276 struct reg_block *rb;
19277 rb = &blocks[i];
19278 replace_set_use(state, rb->in, orig, new);
19279 replace_set_use(state, rb->out, orig, new);
19280 }
19281}
19282
19283static void color_instructions(struct compile_state *state)
19284{
19285 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019286 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019287 ins = first;
19288 do {
19289 if (triple_is_def(state, ins)) {
19290 struct reg_info info;
19291 info = find_lhs_color(state, ins, 0);
19292 if (info.reg >= MAX_REGISTERS) {
19293 info.reg = REG_UNSET;
19294 }
19295 SET_INFO(ins->id, info);
19296 }
19297 ins = ins->next;
19298 } while(ins != first);
19299}
19300
19301static struct reg_info read_lhs_color(
19302 struct compile_state *state, struct triple *ins, int index)
19303{
19304 struct reg_info info;
19305 if ((index == 0) && triple_is_def(state, ins)) {
19306 info.reg = ID_REG(ins->id);
19307 info.regcm = ID_REGCM(ins->id);
19308 }
Eric Biederman90089602004-05-28 14:11:54 +000019309 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019310 info = read_lhs_color(state, LHS(ins, index), 0);
19311 }
19312 else {
19313 internal_error(state, ins, "Bad lhs %d", index);
19314 info.reg = REG_UNSET;
19315 info.regcm = 0;
19316 }
19317 return info;
19318}
19319
19320static struct triple *resolve_tangle(
19321 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019322{
19323 struct reg_info info, uinfo;
19324 struct triple_set *set, *next;
19325 struct triple *copy;
19326
Stefan Reinauer50542a82007-10-24 11:14:14 +000019327#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019328#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019329#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019330 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019331 for(set = tangle->use; set; set = next) {
19332 struct triple *user;
19333 int i, zrhs;
19334 next = set->next;
19335 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019336 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019337 for(i = 0; i < zrhs; i++) {
19338 if (RHS(user, i) != tangle) {
19339 continue;
19340 }
19341 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019342 if (uinfo.reg == info.reg) {
19343 copy = pre_copy(state, user, i);
19344 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019345 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019346 }
19347 }
19348 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019349 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019350 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019351 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019352 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019353 copy = post_copy(state, tangle);
19354 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019355 linfo = find_lhs_color(state, copy, 0);
19356 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019357 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019358 info = find_lhs_color(state, tangle, 0);
19359 SET_INFO(tangle->id, info);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019360
Eric Biedermanf96a8102003-06-16 16:57:34 +000019361 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019362}
19363
19364
Eric Biedermanf96a8102003-06-16 16:57:34 +000019365static void fix_tangles(struct compile_state *state,
19366 struct reg_block *blocks, struct triple_reg_set *live,
19367 struct reg_block *rb, struct triple *ins, void *arg)
19368{
Eric Biederman153ea352003-06-20 14:43:20 +000019369 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019370 struct triple *tangle;
19371 do {
19372 char used[MAX_REGISTERS];
19373 struct triple_reg_set *set;
19374 tangle = 0;
19375
19376 /* Find out which registers have multiple uses at this point */
19377 memset(used, 0, sizeof(used));
19378 for(set = live; set; set = set->next) {
19379 struct reg_info info;
19380 info = read_lhs_color(state, set->member, 0);
19381 if (info.reg == REG_UNSET) {
19382 continue;
19383 }
19384 reg_inc_used(state, used, info.reg);
19385 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019386
Eric Biedermanf96a8102003-06-16 16:57:34 +000019387 /* Now find the least dominated definition of a register in
19388 * conflict I have seen so far.
19389 */
19390 for(set = live; set; set = set->next) {
19391 struct reg_info info;
19392 info = read_lhs_color(state, set->member, 0);
19393 if (used[info.reg] < 2) {
19394 continue;
19395 }
Eric Biederman153ea352003-06-20 14:43:20 +000019396 /* Changing copies that feed into phi functions
19397 * is incorrect.
19398 */
Stefan Reinauer14e22772010-04-27 06:56:47 +000019399 if (set->member->use &&
Eric Biederman153ea352003-06-20 14:43:20 +000019400 (set->member->use->member->op == OP_PHI)) {
19401 continue;
19402 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019403 if (!tangle || tdominates(state, set->member, tangle)) {
19404 tangle = set->member;
19405 }
19406 }
19407 /* If I have found a tangle resolve it */
19408 if (tangle) {
19409 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019410 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019411 post_copy = resolve_tangle(state, tangle);
19412 if (post_copy) {
19413 replace_block_use(state, blocks, tangle, post_copy);
19414 }
19415 if (post_copy && (tangle != ins)) {
19416 replace_set_use(state, live, tangle, post_copy);
19417 }
19418 }
19419 } while(tangle);
19420 return;
19421}
19422
Eric Biederman153ea352003-06-20 14:43:20 +000019423static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019424 struct compile_state *state, struct reg_block *blocks)
19425{
Eric Biederman153ea352003-06-20 14:43:20 +000019426 int tangles;
19427 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019428 color_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019429 walk_variable_lifetimes(state, &state->bb, blocks,
Eric Biederman90089602004-05-28 14:11:54 +000019430 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019431 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019432}
19433
Eric Biedermand1ea5392003-06-28 06:49:45 +000019434
19435static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19436static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19437
19438struct triple *find_constrained_def(
19439 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019440{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019441 struct live_range_def *lrd, *lrd_next;
19442 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019443 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019444 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019445 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019446
19447 lrd = lrd_next;
19448 lrd_next = lrd->next;
19449
Eric Biedermand1ea5392003-06-28 06:49:45 +000019450 regcm = arch_type_to_regcm(state, lrd->def->type);
19451 info = find_lhs_color(state, lrd->def, 0);
19452 regcm = arch_regcm_reg_normalize(state, regcm);
19453 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019454 /* If the 2 register class masks are equal then
19455 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019456 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019457 if (regcm == info.regcm) {
19458 continue;
19459 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019460
Eric Biederman5ade04a2003-10-22 04:03:46 +000019461 /* If there is just one use.
19462 * That use cannot accept a larger register class.
19463 * There are no intervening definitions except
19464 * definitions that feed into that use.
19465 * Then a triple is not constrained.
19466 * FIXME handle this case!
19467 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019468#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019469#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019470#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019471
Eric Biederman5ade04a2003-10-22 04:03:46 +000019472
Eric Biedermand1ea5392003-06-28 06:49:45 +000019473 /* Of the constrained live ranges deal with the
19474 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019475 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019476 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019477 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019478 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019479 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019480 if (!constrained ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000019481 tdominates(state, lrd->def, constrained))
19482 {
19483 constrained = lrd->def;
19484 }
19485 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019486 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019487}
19488
Eric Biedermand1ea5392003-06-28 06:49:45 +000019489static int split_constrained_ranges(
Stefan Reinauer14e22772010-04-27 06:56:47 +000019490 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019491 struct live_range *range)
19492{
19493 /* Walk through the edges in conflict and our current live
19494 * range, and find definitions that are more severly constrained
19495 * than they type of data they contain require.
Stefan Reinauer14e22772010-04-27 06:56:47 +000019496 *
Eric Biedermand1ea5392003-06-28 06:49:45 +000019497 * Then pick one of those ranges and relax the constraints.
19498 */
19499 struct live_range_edge *edge;
19500 struct triple *constrained;
19501
19502 constrained = 0;
19503 for(edge = range->edges; edge; edge = edge->next) {
19504 constrained = find_constrained_def(state, edge->node, constrained);
19505 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019506#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019507#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019508#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019509 if (!constrained) {
19510 constrained = find_constrained_def(state, range, constrained);
19511 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019512
19513 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019514 fprintf(state->errout, "constrained: ");
19515 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019516 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019517 if (constrained) {
19518 ids_from_rstate(state, rstate);
19519 cleanup_rstate(state, rstate);
19520 resolve_tangle(state, constrained);
19521 }
19522 return !!constrained;
19523}
Stefan Reinauer14e22772010-04-27 06:56:47 +000019524
Eric Biedermand1ea5392003-06-28 06:49:45 +000019525static int split_ranges(
19526 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019527 char *used, struct live_range *range)
19528{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019529 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019530 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019531 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019532 rstate->passes, tops(range->defs->def->op), range->defs->def);
19533 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019534 if ((range->color == REG_UNNEEDED) ||
19535 (rstate->passes >= rstate->max_passes)) {
19536 return 0;
19537 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019538 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019539
Eric Biedermand1ea5392003-06-28 06:49:45 +000019540 /* Ideally I would split the live range that will not be used
Stefan Reinauer14e22772010-04-27 06:56:47 +000019541 * for the longest period of time in hopes that this will
Eric Biedermand1ea5392003-06-28 06:49:45 +000019542 * (a) allow me to spill a register or
19543 * (b) allow me to place a value in another register.
19544 *
19545 * So far I don't have a test case for this, the resolving
19546 * of mandatory constraints has solved all of my
19547 * know issues. So I have choosen not to write any
19548 * code until I cat get a better feel for cases where
19549 * it would be useful to have.
19550 *
19551 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019552#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019553#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019554#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000019555
Eric Biederman5ade04a2003-10-22 04:03:46 +000019556 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019557 FILE *fp = state->errout;
19558 print_interference_blocks(state, rstate, fp, 0);
19559 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019560 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019561 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019562}
19563
Eric Biederman5ade04a2003-10-22 04:03:46 +000019564static FILE *cgdebug_fp(struct compile_state *state)
19565{
19566 FILE *fp;
19567 fp = 0;
19568 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019569 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019570 }
19571 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019572 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019573 }
19574 return fp;
19575}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019576
Eric Biederman5ade04a2003-10-22 04:03:46 +000019577static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19578{
19579 FILE *fp;
19580 fp = cgdebug_fp(state);
19581 if (fp) {
19582 va_list args;
19583 va_start(args, fmt);
19584 vfprintf(fp, fmt, args);
19585 va_end(args);
19586 }
19587}
19588
19589static void cgdebug_flush(struct compile_state *state)
19590{
19591 FILE *fp;
19592 fp = cgdebug_fp(state);
19593 if (fp) {
19594 fflush(fp);
19595 }
19596}
19597
19598static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19599{
19600 FILE *fp;
19601 fp = cgdebug_fp(state);
19602 if (fp) {
19603 loc(fp, state, ins);
19604 }
19605}
19606
Stefan Reinauer14e22772010-04-27 06:56:47 +000019607static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019608 struct reg_state *rstate, struct live_range *range)
19609{
19610 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019611 struct live_range_def *lrd;
19612 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019613 struct live_range_edge *edge;
19614 char used[MAX_REGISTERS];
19615 struct triple **expr;
19616
Eric Biedermanb138ac82003-04-22 18:44:01 +000019617 /* Instead of doing just the trivial color select here I try
19618 * a few extra things because a good color selection will help reduce
19619 * copies.
19620 */
19621
19622 /* Find the registers currently in use */
19623 memset(used, 0, sizeof(used));
19624 for(edge = range->edges; edge; edge = edge->next) {
19625 if (edge->node->color == REG_UNSET) {
19626 continue;
19627 }
19628 reg_fill_used(state, used, edge->node->color);
19629 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019630
19631 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019632 int i;
19633 i = 0;
19634 for(edge = range->edges; edge; edge = edge->next) {
19635 i++;
19636 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019637 cgdebug_printf(state, "\n%s edges: %d",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019638 tops(range->defs->def->op), i);
19639 cgdebug_loc(state, range->defs->def);
19640 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019641 for(i = 0; i < MAX_REGISTERS; i++) {
19642 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019643 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019644 arch_reg_str(i));
19645 }
19646 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000019647 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019648
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019649 /* If a color is already assigned see if it will work */
19650 if (range->color != REG_UNSET) {
19651 struct live_range_def *lrd;
19652 if (!used[range->color]) {
19653 return 1;
19654 }
19655 for(edge = range->edges; edge; edge = edge->next) {
19656 if (edge->node->color != range->color) {
19657 continue;
19658 }
19659 warning(state, edge->node->defs->def, "edge: ");
19660 lrd = edge->node->defs;
19661 do {
19662 warning(state, lrd->def, " %p %s",
19663 lrd->def, tops(lrd->def->op));
19664 lrd = lrd->next;
19665 } while(lrd != edge->node->defs);
19666 }
19667 lrd = range->defs;
19668 warning(state, range->defs->def, "def: ");
19669 do {
19670 warning(state, lrd->def, " %p %s",
19671 lrd->def, tops(lrd->def->op));
19672 lrd = lrd->next;
19673 } while(lrd != range->defs);
19674 internal_error(state, range->defs->def,
19675 "live range with already used color %s",
19676 arch_reg_str(range->color));
19677 }
19678
Eric Biedermanb138ac82003-04-22 18:44:01 +000019679 /* If I feed into an expression reuse it's color.
19680 * This should help remove copies in the case of 2 register instructions
19681 * and phi functions.
19682 */
19683 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019684 lrd = live_range_end(state, range, 0);
19685 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19686 entry = lrd->def->use;
19687 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19688 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019689 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019690 insd = &rstate->lrd[entry->member->id];
19691 if (insd->lr->defs == 0) {
19692 continue;
19693 }
19694 if (!phi && (insd->def->op == OP_PHI) &&
19695 !interfere(rstate, range, insd->lr)) {
19696 phi = insd;
19697 }
Eric Biederman530b5192003-07-01 10:05:30 +000019698 if (insd->lr->color == REG_UNSET) {
19699 continue;
19700 }
19701 regcm = insd->lr->classes;
19702 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019703 (used[insd->lr->color])) {
19704 continue;
19705 }
19706 if (interfere(rstate, range, insd->lr)) {
19707 continue;
19708 }
19709 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019710 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019711 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019712 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019713 * of something else that feeds into the phi function.
19714 */
19715 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019716 if (phi->lr->color != REG_UNSET) {
19717 if (used[phi->lr->color]) {
19718 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019719 }
19720 }
19721 else {
19722 expr = triple_rhs(state, phi->def, 0);
19723 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19724 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019725 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019726 if (!*expr) {
19727 continue;
19728 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019729 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019730 if (lr->color == REG_UNSET) {
19731 continue;
19732 }
19733 regcm = lr->classes;
19734 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019735 (used[lr->color])) {
19736 continue;
19737 }
19738 if (interfere(rstate, range, lr)) {
19739 continue;
19740 }
19741 range->color = lr->color;
19742 }
19743 }
19744 }
19745 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019746 lrd = live_range_head(state, range, 0);
19747 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19748 expr = triple_rhs(state, lrd->def, 0);
19749 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019750 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019751 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019752 if (!*expr) {
19753 continue;
19754 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019755 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019756 if (lr->color == REG_UNSET) {
19757 continue;
19758 }
19759 regcm = lr->classes;
19760 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019761 (used[lr->color])) {
19762 continue;
19763 }
19764 if (interfere(rstate, range, lr)) {
19765 continue;
19766 }
19767 range->color = lr->color;
19768 break;
19769 }
19770 }
19771 /* If I have not opportunitically picked a useful color
19772 * pick the first color that is free.
19773 */
19774 if (range->color == REG_UNSET) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019775 range->color =
Eric Biedermanb138ac82003-04-22 18:44:01 +000019776 arch_select_free_register(state, used, range->classes);
19777 }
19778 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019779 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019780 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019781 if (split_ranges(state, rstate, used, range)) {
19782 return 0;
19783 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019784 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019785 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019786 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019787 lrd = edge->node->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 != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019793 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019794 warning(state, range->defs->def, "range: ");
19795 lrd = range->defs;
19796 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019797 warning(state, lrd->def, " %s %p",
19798 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019799 lrd = lrd->next;
19800 } while(lrd != range->defs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019801
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019802 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019803 range->classes);
19804 for(i = 0; i < MAX_REGISTERS; i++) {
19805 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019806 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019807 arch_reg_str(i));
19808 }
19809 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019810 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019811 }
Eric Biederman530b5192003-07-01 10:05:30 +000019812 range->classes &= arch_reg_regcm(state, range->color);
19813 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019814 internal_error(state, range->defs->def, "select_free_color did not?");
19815 }
19816 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019817}
19818
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019819static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019820{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019821 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019822 struct live_range_edge *edge;
19823 struct live_range *range;
19824 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019825 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019826 range = rstate->low;
19827 if (*range->group_prev != range) {
19828 internal_error(state, 0, "lo: *prev != range?");
19829 }
19830 *range->group_prev = range->group_next;
19831 if (range->group_next) {
19832 range->group_next->group_prev = range->group_prev;
19833 }
19834 if (&range->group_next == rstate->low_tail) {
19835 rstate->low_tail = range->group_prev;
19836 }
19837 if (rstate->low == range) {
19838 internal_error(state, 0, "low: next != prev?");
19839 }
19840 }
19841 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019842 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019843 range = rstate->high;
19844 if (*range->group_prev != range) {
19845 internal_error(state, 0, "hi: *prev != range?");
19846 }
19847 *range->group_prev = range->group_next;
19848 if (range->group_next) {
19849 range->group_next->group_prev = range->group_prev;
19850 }
19851 if (&range->group_next == rstate->high_tail) {
19852 rstate->high_tail = range->group_prev;
19853 }
19854 if (rstate->high == range) {
19855 internal_error(state, 0, "high: next != prev?");
19856 }
19857 }
19858 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019859 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019860 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019861 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019862 range->group_prev = 0;
19863 for(edge = range->edges; edge; edge = edge->next) {
19864 struct live_range *node;
19865 node = edge->node;
19866 /* Move nodes from the high to the low list */
19867 if (node->group_prev && (node->color == REG_UNSET) &&
19868 (node->degree == regc_max_size(state, node->classes))) {
19869 if (*node->group_prev != node) {
19870 internal_error(state, 0, "move: *prev != node?");
19871 }
19872 *node->group_prev = node->group_next;
19873 if (node->group_next) {
19874 node->group_next->group_prev = node->group_prev;
19875 }
19876 if (&node->group_next == rstate->high_tail) {
19877 rstate->high_tail = node->group_prev;
19878 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019879 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019880 node->group_prev = rstate->low_tail;
19881 node->group_next = 0;
19882 *rstate->low_tail = node;
19883 rstate->low_tail = &node->group_next;
19884 if (*node->group_prev != node) {
19885 internal_error(state, 0, "move2: *prev != node?");
19886 }
19887 }
19888 node->degree -= 1;
19889 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019890 colored = color_graph(state, rstate);
19891 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019892 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019893 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019894 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019895 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019896 if (colored) {
19897 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19898 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019899 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019900 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019901}
19902
Eric Biedermana96d6a92003-05-13 20:45:19 +000019903static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19904{
19905 struct live_range *lr;
19906 struct live_range_edge *edge;
19907 struct triple *ins, *first;
19908 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019909 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019910 ins = first;
19911 do {
19912 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019913 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019914 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019915 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019916 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019917 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019918 if (lr->color == REG_UNSET) {
19919 internal_error(state, ins,
19920 "triple without a color");
19921 }
19922 /* Find the registers used by the edges */
19923 memset(used, 0, sizeof(used));
19924 for(edge = lr->edges; edge; edge = edge->next) {
19925 if (edge->node->color == REG_UNSET) {
19926 internal_error(state, 0,
19927 "live range without a color");
19928 }
19929 reg_fill_used(state, used, edge->node->color);
19930 }
19931 if (used[lr->color]) {
19932 internal_error(state, ins,
19933 "triple with already used color");
19934 }
19935 }
19936 ins = ins->next;
19937 } while(ins != first);
19938}
19939
Eric Biedermanb138ac82003-04-22 18:44:01 +000019940static void color_triples(struct compile_state *state, struct reg_state *rstate)
19941{
Eric Biederman90089602004-05-28 14:11:54 +000019942 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019943 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019944 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019945 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019946 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019947 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019948 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000019949 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019950 "triple without a live range");
19951 }
Eric Biederman90089602004-05-28 14:11:54 +000019952 lrd = &rstate->lrd[ins->id];
19953 lr = lrd->lr;
19954 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019955 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019956 ins = ins->next;
19957 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019958}
19959
Eric Biedermanb138ac82003-04-22 18:44:01 +000019960static struct live_range *merge_sort_lr(
19961 struct live_range *first, struct live_range *last)
19962{
19963 struct live_range *mid, *join, **join_tail, *pick;
19964 size_t size;
19965 size = (last - first) + 1;
19966 if (size >= 2) {
19967 mid = first + size/2;
19968 first = merge_sort_lr(first, mid -1);
19969 mid = merge_sort_lr(mid, last);
Stefan Reinauer14e22772010-04-27 06:56:47 +000019970
Eric Biedermanb138ac82003-04-22 18:44:01 +000019971 join = 0;
19972 join_tail = &join;
19973 /* merge the two lists */
19974 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019975 if ((first->degree < mid->degree) ||
19976 ((first->degree == mid->degree) &&
19977 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019978 pick = first;
19979 first = first->group_next;
19980 if (first) {
19981 first->group_prev = 0;
19982 }
19983 }
19984 else {
19985 pick = mid;
19986 mid = mid->group_next;
19987 if (mid) {
19988 mid->group_prev = 0;
19989 }
19990 }
19991 pick->group_next = 0;
19992 pick->group_prev = join_tail;
19993 *join_tail = pick;
19994 join_tail = &pick->group_next;
19995 }
19996 /* Splice the remaining list */
19997 pick = (first)? first : mid;
19998 *join_tail = pick;
Stefan Reinauer14e22772010-04-27 06:56:47 +000019999 if (pick) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020000 pick->group_prev = join_tail;
20001 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020002 }
20003 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020004 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020005 first = 0;
20006 }
20007 join = first;
20008 }
20009 return join;
20010}
20011
Stefan Reinauer14e22772010-04-27 06:56:47 +000020012static void ids_from_rstate(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020013 struct reg_state *rstate)
20014{
20015 struct triple *ins, *first;
20016 if (!rstate->defs) {
20017 return;
20018 }
20019 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020020 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020021 FILE *fp = state->dbgout;
20022 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020023 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020024 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020025 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020026 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020027 ins = first;
20028 do {
20029 if (ins->id) {
20030 struct live_range_def *lrd;
20031 lrd = &rstate->lrd[ins->id];
20032 ins->id = lrd->orig_id;
20033 }
20034 ins = ins->next;
20035 } while(ins != first);
20036}
20037
20038static void cleanup_live_edges(struct reg_state *rstate)
20039{
20040 int i;
20041 /* Free the edges on each node */
20042 for(i = 1; i <= rstate->ranges; i++) {
20043 remove_live_edges(rstate, &rstate->lr[i]);
20044 }
20045}
20046
20047static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20048{
20049 cleanup_live_edges(rstate);
20050 xfree(rstate->lrd);
20051 xfree(rstate->lr);
20052
20053 /* Free the variable lifetime information */
20054 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020055 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020056 }
20057 rstate->defs = 0;
20058 rstate->ranges = 0;
20059 rstate->lrd = 0;
20060 rstate->lr = 0;
20061 rstate->blocks = 0;
20062}
20063
Eric Biederman153ea352003-06-20 14:43:20 +000020064static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020065static void allocate_registers(struct compile_state *state)
20066{
20067 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020068 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020069
20070 /* Clear out the reg_state */
20071 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020072 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020073
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020074 do {
20075 struct live_range **point, **next;
Eric Biederman153ea352003-06-20 14:43:20 +000020076 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020077 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020078
Eric Biederman5ade04a2003-10-22 04:03:46 +000020079 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020080 FILE *fp = state->errout;
20081 fprintf(fp, "pass: %d\n", rstate.passes);
20082 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020083 }
Eric Biederman153ea352003-06-20 14:43:20 +000020084
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020085 /* Restore ids */
20086 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020087
Eric Biedermanf96a8102003-06-16 16:57:34 +000020088 /* Cleanup the temporary data structures */
20089 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020090
Eric Biedermanf96a8102003-06-16 16:57:34 +000020091 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020092 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020093
Eric Biedermanf96a8102003-06-16 16:57:34 +000020094 /* Fix invalid mandatory live range coalesce conflicts */
Bernhard Urbanf31abe32012-02-01 16:30:30 +010020095 correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020096
Eric Biederman153ea352003-06-20 14:43:20 +000020097 /* Fix two simultaneous uses of the same register.
20098 * In a few pathlogical cases a partial untangle moves
20099 * the tangle to a part of the graph we won't revisit.
20100 * So we keep looping until we have no more tangle fixes
20101 * to apply.
20102 */
20103 do {
20104 tangles = correct_tangles(state, rstate.blocks);
20105 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020106
Stefan Reinauer14e22772010-04-27 06:56:47 +000020107
Eric Biederman90089602004-05-28 14:11:54 +000020108 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020109 verify_consistency(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020110
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020111 /* Allocate and initialize the live ranges */
20112 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020113
Stefan Reinauer14e22772010-04-27 06:56:47 +000020114 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020115 * buys me nothing. The code is left this way in case
20116 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020117 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020118 */
20119 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020120 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020121 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020122 }
20123
Eric Biederman153ea352003-06-20 14:43:20 +000020124 /* Remove any previous live edge calculations */
20125 cleanup_live_edges(&rstate);
20126
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020127 /* Compute the interference graph */
20128 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020129 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020130 graph_ins, &rstate);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020131
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020132 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020133 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020134 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020135 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020136 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020137 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020138 print_interference_ins, &rstate);
20139 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020140
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020141 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020142
Eric Biederman5ade04a2003-10-22 04:03:46 +000020143 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020144 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020145 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020146 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020147
Eric Biederman83b991a2003-10-11 06:20:25 +000020148#if DEBUG_CONSISTENCY > 1
20149# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020150 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020151# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020152 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020153 walk_variable_lifetimes(
Stefan Reinauer14e22772010-04-27 06:56:47 +000020154 state, &state->bb, rstate.blocks,
Eric Biederman90089602004-05-28 14:11:54 +000020155 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020156# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020157 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020158#endif
20159#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +000020160
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020161 /* Build the groups low and high. But with the nodes
20162 * first sorted by degree order.
20163 */
20164 rstate.low_tail = &rstate.low;
20165 rstate.high_tail = &rstate.high;
20166 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20167 if (rstate.high) {
20168 rstate.high->group_prev = &rstate.high;
20169 }
20170 for(point = &rstate.high; *point; point = &(*point)->group_next)
20171 ;
20172 rstate.high_tail = point;
20173 /* Walk through the high list and move everything that needs
20174 * to be onto low.
20175 */
20176 for(point = &rstate.high; *point; point = next) {
20177 struct live_range *range;
20178 next = &(*point)->group_next;
20179 range = *point;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020180
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020181 /* If it has a low degree or it already has a color
20182 * place the node in low.
20183 */
20184 if ((range->degree < regc_max_size(state, range->classes)) ||
20185 (range->color != REG_UNSET)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020186 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020187 range - rstate.lr, range->degree,
20188 (range->color != REG_UNSET) ? " (colored)": "");
20189 *range->group_prev = range->group_next;
20190 if (range->group_next) {
20191 range->group_next->group_prev = range->group_prev;
20192 }
20193 if (&range->group_next == rstate.high_tail) {
20194 rstate.high_tail = range->group_prev;
20195 }
20196 range->group_prev = rstate.low_tail;
20197 range->group_next = 0;
20198 *rstate.low_tail = range;
20199 rstate.low_tail = &range->group_next;
20200 next = point;
20201 }
20202 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020203 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020204 range - rstate.lr, range->degree,
20205 (range->color != REG_UNSET) ? " (colored)": "");
20206 }
20207 }
20208 /* Color the live_ranges */
20209 colored = color_graph(state, &rstate);
20210 rstate.passes++;
20211 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020212
Eric Biedermana96d6a92003-05-13 20:45:19 +000020213 /* Verify the graph was properly colored */
20214 verify_colors(state, &rstate);
20215
Eric Biedermanb138ac82003-04-22 18:44:01 +000020216 /* Move the colors from the graph to the triples */
20217 color_triples(state, &rstate);
20218
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020219 /* Cleanup the temporary data structures */
20220 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020221
20222 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020223 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020224}
20225
20226/* Sparce Conditional Constant Propogation
20227 * =========================================
20228 */
20229struct ssa_edge;
20230struct flow_block;
20231struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020232 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020233 struct triple *def;
20234 struct ssa_edge *out;
20235 struct flow_block *fblock;
20236 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020237 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020238 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020239 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020240 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020241};
20242struct ssa_edge {
20243 struct lattice_node *src;
20244 struct lattice_node *dst;
20245 struct ssa_edge *work_next;
20246 struct ssa_edge *work_prev;
20247 struct ssa_edge *out_next;
20248};
20249struct flow_edge {
20250 struct flow_block *src;
20251 struct flow_block *dst;
20252 struct flow_edge *work_next;
20253 struct flow_edge *work_prev;
20254 struct flow_edge *in_next;
20255 struct flow_edge *out_next;
20256 int executable;
20257};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020258#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020259struct flow_block {
20260 struct block *block;
20261 struct flow_edge *in;
20262 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020263 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020264};
20265
20266struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020267 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020268 struct lattice_node *lattice;
20269 struct ssa_edge *ssa_edges;
20270 struct flow_block *flow_blocks;
20271 struct flow_edge *flow_work_list;
20272 struct ssa_edge *ssa_work_list;
20273};
20274
20275
Eric Biederman90089602004-05-28 14:11:54 +000020276static int is_scc_const(struct compile_state *state, struct triple *ins)
20277{
20278 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20279}
20280
20281static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20282{
20283 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20284}
20285
20286static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20287{
20288 return is_scc_const(state, lnode->val);
20289}
20290
20291static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20292{
20293 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20294}
20295
Stefan Reinauer14e22772010-04-27 06:56:47 +000020296static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020297 struct flow_edge *fedge)
20298{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020299 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020300 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020301 fedge,
20302 fedge->src->block?fedge->src->block->last->id: 0,
20303 fedge->dst->block?fedge->dst->block->first->id: 0);
20304 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020305 if ((fedge == scc->flow_work_list) ||
20306 (fedge->work_next != fedge) ||
20307 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020308
20309 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020310 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020311 fedge);
20312 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020313 return;
20314 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020315 if (!scc->flow_work_list) {
20316 scc->flow_work_list = fedge;
20317 fedge->work_next = fedge->work_prev = fedge;
20318 }
20319 else {
20320 struct flow_edge *ftail;
20321 ftail = scc->flow_work_list->work_prev;
20322 fedge->work_next = ftail->work_next;
20323 fedge->work_prev = ftail;
20324 fedge->work_next->work_prev = fedge;
20325 fedge->work_prev->work_next = fedge;
20326 }
20327}
20328
20329static struct flow_edge *scc_next_fedge(
20330 struct compile_state *state, struct scc_state *scc)
20331{
20332 struct flow_edge *fedge;
20333 fedge = scc->flow_work_list;
20334 if (fedge) {
20335 fedge->work_next->work_prev = fedge->work_prev;
20336 fedge->work_prev->work_next = fedge->work_next;
20337 if (fedge->work_next != fedge) {
20338 scc->flow_work_list = fedge->work_next;
20339 } else {
20340 scc->flow_work_list = 0;
20341 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020342 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020343 }
20344 return fedge;
20345}
20346
20347static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20348 struct ssa_edge *sedge)
20349{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020350 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020351 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20352 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020353 sedge->src->def->id,
20354 sedge->dst->def->id);
20355 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020356 if ((sedge == scc->ssa_work_list) ||
20357 (sedge->work_next != sedge) ||
20358 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020359
20360 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020361 fprintf(state->errout, "dupped sedge: %5ld\n",
20362 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020363 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020364 return;
20365 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020366 if (!scc->ssa_work_list) {
20367 scc->ssa_work_list = sedge;
20368 sedge->work_next = sedge->work_prev = sedge;
20369 }
20370 else {
20371 struct ssa_edge *stail;
20372 stail = scc->ssa_work_list->work_prev;
20373 sedge->work_next = stail->work_next;
20374 sedge->work_prev = stail;
20375 sedge->work_next->work_prev = sedge;
20376 sedge->work_prev->work_next = sedge;
20377 }
20378}
20379
20380static struct ssa_edge *scc_next_sedge(
20381 struct compile_state *state, struct scc_state *scc)
20382{
20383 struct ssa_edge *sedge;
20384 sedge = scc->ssa_work_list;
20385 if (sedge) {
20386 sedge->work_next->work_prev = sedge->work_prev;
20387 sedge->work_prev->work_next = sedge->work_next;
20388 if (sedge->work_next != sedge) {
20389 scc->ssa_work_list = sedge->work_next;
20390 } else {
20391 scc->ssa_work_list = 0;
20392 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020393 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020394 }
20395 return sedge;
20396}
20397
20398static void initialize_scc_state(
20399 struct compile_state *state, struct scc_state *scc)
20400{
20401 int ins_count, ssa_edge_count;
20402 int ins_index, ssa_edge_index, fblock_index;
20403 struct triple *first, *ins;
20404 struct block *block;
20405 struct flow_block *fblock;
20406
20407 memset(scc, 0, sizeof(*scc));
20408
20409 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020410 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020411 ins = first;
20412 ins_count = ssa_edge_count = 0;
20413 do {
20414 struct triple_set *edge;
20415 ins_count += 1;
20416 for(edge = ins->use; edge; edge = edge->next) {
20417 ssa_edge_count++;
20418 }
20419 ins = ins->next;
20420 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020421 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020422 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20423 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020424 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020425 scc->ins_count = ins_count;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020426 scc->lattice =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020427 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020428 scc->ssa_edges =
Eric Biedermanb138ac82003-04-22 18:44:01 +000020429 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
Stefan Reinauer14e22772010-04-27 06:56:47 +000020430 scc->flow_blocks =
20431 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020432 "flow_blocks");
20433
20434 /* Initialize pass one collect up the nodes */
20435 fblock = 0;
20436 block = 0;
20437 ins_index = ssa_edge_index = fblock_index = 0;
20438 ins = first;
20439 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020440 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20441 block = ins->u.block;
20442 if (!block) {
20443 internal_error(state, ins, "label without block");
20444 }
20445 fblock_index += 1;
20446 block->vertex = fblock_index;
20447 fblock = &scc->flow_blocks[fblock_index];
20448 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020449 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20450 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020451 }
20452 {
20453 struct lattice_node *lnode;
20454 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020455 lnode = &scc->lattice[ins_index];
20456 lnode->def = ins;
20457 lnode->out = 0;
20458 lnode->fblock = fblock;
20459 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020460 if (lnode->val->op == OP_UNKNOWNVAL) {
20461 lnode->val = 0; /* LATTICE LOW by definition */
20462 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020463 lnode->old_id = ins->id;
20464 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020465 }
20466 ins = ins->next;
20467 } while(ins != first);
20468 /* Initialize pass two collect up the edges */
20469 block = 0;
20470 fblock = 0;
20471 ins = first;
20472 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020473 {
20474 struct triple_set *edge;
20475 struct ssa_edge **stail;
20476 struct lattice_node *lnode;
20477 lnode = &scc->lattice[ins->id];
20478 lnode->out = 0;
20479 stail = &lnode->out;
20480 for(edge = ins->use; edge; edge = edge->next) {
20481 struct ssa_edge *sedge;
20482 ssa_edge_index += 1;
20483 sedge = &scc->ssa_edges[ssa_edge_index];
20484 *stail = sedge;
20485 stail = &sedge->out_next;
20486 sedge->src = lnode;
20487 sedge->dst = &scc->lattice[edge->member->id];
20488 sedge->work_next = sedge->work_prev = sedge;
20489 sedge->out_next = 0;
20490 }
20491 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020492 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20493 struct flow_edge *fedge, **ftail;
20494 struct block_set *bedge;
20495 block = ins->u.block;
20496 fblock = &scc->flow_blocks[block->vertex];
20497 fblock->in = 0;
20498 fblock->out = 0;
20499 ftail = &fblock->out;
20500
20501 fedge = fblock->edges;
20502 bedge = block->edges;
20503 for(; bedge; bedge = bedge->next, fedge++) {
20504 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20505 if (fedge->dst->block != bedge->member) {
20506 internal_error(state, 0, "block mismatch");
20507 }
20508 *ftail = fedge;
20509 ftail = &fedge->out_next;
20510 fedge->out_next = 0;
20511 }
20512 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20513 fedge->src = fblock;
20514 fedge->work_next = fedge->work_prev = fedge;
20515 fedge->executable = 0;
20516 }
20517 }
20518 ins = ins->next;
20519 } while (ins != first);
20520 block = 0;
20521 fblock = 0;
20522 ins = first;
20523 do {
20524 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20525 struct flow_edge **ftail;
20526 struct block_set *bedge;
20527 block = ins->u.block;
20528 fblock = &scc->flow_blocks[block->vertex];
20529 ftail = &fblock->in;
20530 for(bedge = block->use; bedge; bedge = bedge->next) {
20531 struct block *src_block;
20532 struct flow_block *sfblock;
20533 struct flow_edge *sfedge;
20534 src_block = bedge->member;
20535 sfblock = &scc->flow_blocks[src_block->vertex];
20536 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20537 if (sfedge->dst == fblock) {
20538 break;
20539 }
20540 }
20541 if (!sfedge) {
20542 internal_error(state, 0, "edge mismatch");
20543 }
20544 *ftail = sfedge;
20545 ftail = &sfedge->in_next;
20546 sfedge->in_next = 0;
20547 }
20548 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020549 ins = ins->next;
20550 } while(ins != first);
20551 /* Setup a dummy block 0 as a node above the start node */
20552 {
20553 struct flow_block *fblock, *dst;
20554 struct flow_edge *fedge;
20555 fblock = &scc->flow_blocks[0];
20556 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020557 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020558 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020559 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020560 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020561 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020562 fedge->src = fblock;
20563 fedge->dst = dst;
20564 fedge->work_next = fedge;
20565 fedge->work_prev = fedge;
20566 fedge->in_next = fedge->dst->in;
20567 fedge->out_next = 0;
20568 fedge->executable = 0;
20569 fedge->dst->in = fedge;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020570
Eric Biedermanb138ac82003-04-22 18:44:01 +000020571 /* Initialize the work lists */
20572 scc->flow_work_list = 0;
20573 scc->ssa_work_list = 0;
20574 scc_add_fedge(state, scc, fedge);
20575 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020576 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020577 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020578 ins_index, ssa_edge_index, fblock_index);
20579 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020580}
20581
Stefan Reinauer14e22772010-04-27 06:56:47 +000020582
Eric Biedermanb138ac82003-04-22 18:44:01 +000020583static void free_scc_state(
20584 struct compile_state *state, struct scc_state *scc)
20585{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020586 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020587 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020588 struct flow_block *fblock;
20589 fblock = &scc->flow_blocks[i];
20590 if (fblock->edges) {
20591 xfree(fblock->edges);
20592 fblock->edges = 0;
20593 }
20594 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020595 xfree(scc->flow_blocks);
20596 xfree(scc->ssa_edges);
20597 xfree(scc->lattice);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020598
Eric Biedermanb138ac82003-04-22 18:44:01 +000020599}
20600
20601static struct lattice_node *triple_to_lattice(
20602 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20603{
20604 if (ins->id <= 0) {
20605 internal_error(state, ins, "bad id");
20606 }
20607 return &scc->lattice[ins->id];
20608}
20609
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020610static struct triple *preserve_lval(
20611 struct compile_state *state, struct lattice_node *lnode)
20612{
20613 struct triple *old;
20614 /* Preserve the original value */
20615 if (lnode->val) {
20616 old = dup_triple(state, lnode->val);
20617 if (lnode->val != lnode->def) {
20618 xfree(lnode->val);
20619 }
20620 lnode->val = 0;
20621 } else {
20622 old = 0;
20623 }
20624 return old;
20625}
20626
Stefan Reinauer14e22772010-04-27 06:56:47 +000020627static int lval_changed(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020628 struct triple *old, struct lattice_node *lnode)
20629{
20630 int changed;
20631 /* See if the lattice value has changed */
20632 changed = 1;
20633 if (!old && !lnode->val) {
20634 changed = 0;
20635 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020636 if (changed &&
20637 lnode->val && old &&
20638 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020639 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020640 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20641 changed = 0;
20642 }
20643 if (old) {
20644 xfree(old);
20645 }
20646 return changed;
20647
20648}
20649
Eric Biederman5ade04a2003-10-22 04:03:46 +000020650static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020651 struct compile_state *state, struct scc_state *scc,
20652 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020653{
Eric Biederman90089602004-05-28 14:11:54 +000020654 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20655 display_triple_changes(state->errout, lnode->val, lnode->def);
20656 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020657 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020658 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020659 struct triple *val, **expr;
20660 val = lnode->val? lnode->val : lnode->def;
20661 fprintf(fp, "%p %s %3d %10s (",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020662 lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020663 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20664 lnode->def->id,
20665 tops(lnode->def->op));
20666 expr = triple_rhs(state, lnode->def, 0);
20667 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20668 if (*expr) {
20669 fprintf(fp, " %d", (*expr)->id);
20670 }
20671 }
20672 if (val->op == OP_INTCONST) {
20673 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20674 }
20675 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020676 (is_lattice_hi(state, lnode)? "hi":
20677 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020678 changed? "changed" : ""
20679 );
20680 }
20681}
20682
Eric Biedermanb138ac82003-04-22 18:44:01 +000020683static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20684 struct lattice_node *lnode)
20685{
20686 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020687 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020688 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020689 int count, i;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020690
Eric Biedermanb138ac82003-04-22 18:44:01 +000020691 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020692 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020693
Eric Biedermanb138ac82003-04-22 18:44:01 +000020694 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020695 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020696 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020697 scratch->next = scratch;
20698 scratch->prev = scratch;
20699 scratch->use = 0;
20700
Eric Biederman90089602004-05-28 14:11:54 +000020701 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020702 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020703 dexpr = &lnode->def->param[i];
20704 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020705 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020706 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20707 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020708 *dexpr) {
20709 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020710 tmp = triple_to_lattice(state, scc, *dexpr);
20711 *vexpr = (tmp->val)? tmp->val : tmp->def;
20712 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020713 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020714 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020715 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020716 }
20717 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020718#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020719#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020720#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020721 /* So far it looks like only the strength reduction
20722 * optimization are things I need to worry about.
20723 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020724 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020725 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020726 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020727 internal_error(state, lnode->def, "scratch used?");
20728 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020729 if ((scratch->prev != scratch) ||
20730 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020731 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020732 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020733 internal_error(state, lnode->def, "scratch in list?");
20734 }
20735 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020736 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020737 for(i = 0; i < count; i++) {
20738 vexpr = &scratch->param[i];
20739 if (*vexpr) {
20740 unuse_triple(*vexpr, scratch);
20741 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020742 }
Eric Biederman90089602004-05-28 14:11:54 +000020743 if (lnode->val->op == OP_UNKNOWNVAL) {
20744 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020745 }
Eric Biederman90089602004-05-28 14:11:54 +000020746 /* Find the case when I am lattice high */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020747 if (lnode->val &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000020748 (lnode->val->op == lnode->def->op) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000020749 (memcmp(lnode->val->param, lnode->def->param,
Eric Biederman0babc1c2003-05-09 02:39:00 +000020750 count * sizeof(lnode->val->param[0])) == 0) &&
20751 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020752 lnode->val = lnode->def;
20753 }
Eric Biederman90089602004-05-28 14:11:54 +000020754 /* Only allow lattice high when all of my inputs
20755 * are also lattice high. Occassionally I can
20756 * have constants with a lattice low input, so
20757 * I do not need to check that case.
20758 */
20759 if (is_lattice_hi(state, lnode)) {
20760 struct lattice_node *tmp;
20761 int rhs;
20762 rhs = lnode->val->rhs;
20763 for(i = 0; i < rhs; i++) {
20764 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20765 if (!is_lattice_hi(state, tmp)) {
20766 lnode->val = 0;
20767 break;
20768 }
20769 }
20770 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020771 /* Find the cases that are always lattice lo */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020772 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020773 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020774 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020775 lnode->val = 0;
20776 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020777 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020778 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020779 /* See if this value should not change */
Stefan Reinauer14e22772010-04-27 06:56:47 +000020780 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020781 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020782 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020783 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020784#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020785#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020786#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020787 if (changed) {
20788 internal_warning(state, lnode->def, "non def changes value?");
20789 }
20790 lnode->val = 0;
20791 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020792
Eric Biederman83b991a2003-10-11 06:20:25 +000020793 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020794 if (lnode->val != scratch) {
20795 xfree(scratch);
20796 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000020797
Eric Biedermanb138ac82003-04-22 18:44:01 +000020798 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020799}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020800
Eric Biederman90089602004-05-28 14:11:54 +000020801
20802static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020803 struct lattice_node *lnode)
20804{
20805 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020806 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020807 int changed;
20808
20809 /* Update the branch value */
20810 changed = compute_lnode_val(state, scc, lnode);
20811 scc_debug_lnode(state, scc, lnode, changed);
20812
20813 /* This only applies to conditional branches */
20814 if (!triple_is_cbranch(state, lnode->def)) {
20815 internal_error(state, lnode->def, "not a conditional branch");
20816 }
20817
Eric Biederman5ade04a2003-10-22 04:03:46 +000020818 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020819 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020820 FILE *fp = state->errout;
20821 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020822 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020823 lnode->def->id);
Stefan Reinauer14e22772010-04-27 06:56:47 +000020824
Eric Biedermanb138ac82003-04-22 18:44:01 +000020825 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020826 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020827 }
Eric Biederman90089602004-05-28 14:11:54 +000020828 fprintf(fp, " )");
20829 if (lnode->def->rhs > 0) {
20830 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020831 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020832 }
Eric Biederman90089602004-05-28 14:11:54 +000020833 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020834 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020835 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020836 for(left = cond->fblock->out; left; left = left->out_next) {
20837 if (left->dst->block->first == lnode->def->next) {
20838 break;
20839 }
20840 }
20841 if (!left) {
20842 internal_error(state, lnode->def, "Cannot find left branch edge");
20843 }
20844 for(right = cond->fblock->out; right; right = right->out_next) {
20845 if (right->dst->block->first == TARG(lnode->def, 0)) {
20846 break;
20847 }
20848 }
20849 if (!right) {
20850 internal_error(state, lnode->def, "Cannot find right branch edge");
20851 }
Eric Biederman90089602004-05-28 14:11:54 +000020852 /* I should only come here if the controlling expressions value
20853 * has changed, which means it must be either a constant or lo.
20854 */
20855 if (is_lattice_hi(state, cond)) {
20856 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020857 return;
20858 }
Eric Biederman90089602004-05-28 14:11:54 +000020859 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020860 scc_add_fedge(state, scc, left);
20861 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020862 }
20863 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020864 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020865 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020866 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020867 }
20868
20869}
20870
Eric Biederman90089602004-05-28 14:11:54 +000020871
Stefan Reinauer14e22772010-04-27 06:56:47 +000020872static void scc_add_sedge_dst(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000020873 struct scc_state *scc, struct ssa_edge *sedge)
20874{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020875 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020876 scc_visit_cbranch(state, scc, sedge->dst);
20877 }
20878 else if (triple_is_def(state, sedge->dst->def)) {
20879 scc_add_sedge(state, scc, sedge);
20880 }
20881}
20882
Stefan Reinauer14e22772010-04-27 06:56:47 +000020883static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
Eric Biederman90089602004-05-28 14:11:54 +000020884 struct lattice_node *lnode)
20885{
20886 struct lattice_node *tmp;
20887 struct triple **slot, *old;
20888 struct flow_edge *fedge;
20889 int changed;
20890 int index;
20891 if (lnode->def->op != OP_PHI) {
20892 internal_error(state, lnode->def, "not phi");
20893 }
20894 /* Store the original value */
20895 old = preserve_lval(state, lnode);
20896
20897 /* default to lattice high */
20898 lnode->val = lnode->def;
20899 slot = &RHS(lnode->def, 0);
20900 index = 0;
20901 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20902 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000020903 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
Eric Biederman90089602004-05-28 14:11:54 +000020904 index,
20905 fedge->dst->block->vertex,
20906 fedge->executable
20907 );
20908 }
20909 if (!fedge->executable) {
20910 continue;
20911 }
20912 if (!slot[index]) {
20913 internal_error(state, lnode->def, "no phi value");
20914 }
20915 tmp = triple_to_lattice(state, scc, slot[index]);
20916 /* meet(X, lattice low) = lattice low */
20917 if (is_lattice_lo(state, tmp)) {
20918 lnode->val = 0;
20919 }
20920 /* meet(X, lattice high) = X */
20921 else if (is_lattice_hi(state, tmp)) {
20922 lnode->val = lnode->val;
20923 }
20924 /* meet(lattice high, X) = X */
20925 else if (is_lattice_hi(state, lnode)) {
20926 lnode->val = dup_triple(state, tmp->val);
20927 /* Only change the type if necessary */
20928 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20929 lnode->val->type = lnode->def->type;
20930 }
20931 }
20932 /* meet(const, const) = const or lattice low */
20933 else if (!constants_equal(state, lnode->val, tmp->val)) {
20934 lnode->val = 0;
20935 }
20936
20937 /* meet(lattice low, X) = lattice low */
20938 if (is_lattice_lo(state, lnode)) {
20939 lnode->val = 0;
20940 break;
20941 }
20942 }
20943 changed = lval_changed(state, old, lnode);
20944 scc_debug_lnode(state, scc, lnode, changed);
20945
20946 /* If the lattice value has changed update the work lists. */
20947 if (changed) {
20948 struct ssa_edge *sedge;
20949 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20950 scc_add_sedge_dst(state, scc, sedge);
20951 }
20952 }
20953}
20954
20955
Eric Biedermanb138ac82003-04-22 18:44:01 +000020956static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20957 struct lattice_node *lnode)
20958{
20959 int changed;
20960
Eric Biederman90089602004-05-28 14:11:54 +000020961 if (!triple_is_def(state, lnode->def)) {
20962 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020963 }
Eric Biederman90089602004-05-28 14:11:54 +000020964 changed = compute_lnode_val(state, scc, lnode);
20965 scc_debug_lnode(state, scc, lnode, changed);
20966
20967 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020968 struct ssa_edge *sedge;
20969 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020970 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020971 }
20972 }
20973}
20974
20975static void scc_writeback_values(
20976 struct compile_state *state, struct scc_state *scc)
20977{
20978 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020979 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020980 ins = first;
20981 do {
20982 struct lattice_node *lnode;
20983 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020984 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020985 if (is_lattice_hi(state, lnode) &&
20986 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020987 {
20988 struct flow_edge *fedge;
20989 int executable;
20990 executable = 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +000020991 for(fedge = lnode->fblock->in;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020992 !executable && fedge; fedge = fedge->in_next) {
20993 executable |= fedge->executable;
20994 }
20995 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000020996 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020997 "lattice node %d %s->%s still high?",
Stefan Reinauer14e22772010-04-27 06:56:47 +000020998 ins->id,
Eric Biederman5ade04a2003-10-22 04:03:46 +000020999 tops(lnode->def->op),
21000 tops(lnode->val->op));
21001 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021002 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021003 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021004
Eric Biederman83b991a2003-10-11 06:20:25 +000021005 /* Restore id */
21006 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021007 if (lnode->val && (lnode->val != ins)) {
21008 /* See if it something I know how to write back */
21009 switch(lnode->val->op) {
21010 case OP_INTCONST:
21011 mkconst(state, ins, lnode->val->u.cval);
21012 break;
21013 case OP_ADDRCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000021014 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021015 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021016 break;
21017 default:
21018 /* By default don't copy the changes,
21019 * recompute them in place instead.
21020 */
21021 simplify(state, ins);
21022 break;
21023 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021024 if (is_const(lnode->val) &&
21025 !constants_equal(state, lnode->val, ins)) {
21026 internal_error(state, 0, "constants not equal");
21027 }
21028 /* Free the lattice nodes */
21029 xfree(lnode->val);
21030 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021031 }
21032 ins = ins->next;
21033 } while(ins != first);
21034}
21035
21036static void scc_transform(struct compile_state *state)
21037{
21038 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021039 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21040 return;
21041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021042
21043 initialize_scc_state(state, &scc);
21044
21045 while(scc.flow_work_list || scc.ssa_work_list) {
21046 struct flow_edge *fedge;
21047 struct ssa_edge *sedge;
21048 struct flow_edge *fptr;
21049 while((fedge = scc_next_fedge(state, &scc))) {
21050 struct block *block;
21051 struct triple *ptr;
21052 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021053 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021054 int done;
21055 if (fedge->executable) {
21056 continue;
21057 }
21058 if (!fedge->dst) {
21059 internal_error(state, 0, "fedge without dst");
21060 }
21061 if (!fedge->src) {
21062 internal_error(state, 0, "fedge without src");
21063 }
21064 fedge->executable = 1;
21065 fblock = fedge->dst;
21066 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021067 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021068 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21069 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021070 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021071 }
21072 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021073
Eric Biederman5ade04a2003-10-22 04:03:46 +000021074 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021075 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021076 block->vertex, reps);
21077 }
21078
Eric Biedermanb138ac82003-04-22 18:44:01 +000021079 done = 0;
21080 for(ptr = block->first; !done; ptr = ptr->next) {
21081 struct lattice_node *lnode;
21082 done = (ptr == block->last);
21083 lnode = &scc.lattice[ptr->id];
21084 if (ptr->op == OP_PHI) {
21085 scc_visit_phi(state, &scc, lnode);
21086 }
Eric Biederman90089602004-05-28 14:11:54 +000021087 else if ((reps == 1) && triple_is_def(state, ptr))
21088 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021089 scc_visit_expr(state, &scc, lnode);
21090 }
21091 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021092 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021093 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021094 struct flow_edge *out;
21095 for(out = fblock->out; out; out = out->out_next) {
21096 scc_add_fedge(state, &scc, out);
21097 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021098 }
21099 }
21100 while((sedge = scc_next_sedge(state, &scc))) {
21101 struct lattice_node *lnode;
21102 struct flow_block *fblock;
21103 lnode = sedge->dst;
21104 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021105
21106 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021107 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021108 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021109 sedge->src->def->id,
21110 sedge->dst->def->id);
21111 }
21112
Eric Biedermanb138ac82003-04-22 18:44:01 +000021113 if (lnode->def->op == OP_PHI) {
21114 scc_visit_phi(state, &scc, lnode);
21115 }
21116 else {
21117 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21118 if (fptr->executable) {
21119 break;
21120 }
21121 }
21122 if (fptr) {
21123 scc_visit_expr(state, &scc, lnode);
21124 }
21125 }
21126 }
21127 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021128
Eric Biedermanb138ac82003-04-22 18:44:01 +000021129 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021130 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021131 rebuild_ssa_form(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021132
Eric Biederman90089602004-05-28 14:11:54 +000021133 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021134}
21135
21136
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021137static void transform_to_arch_instructions(struct compile_state *state)
21138{
21139 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021140 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021141 ins = first;
21142 do {
21143 ins = transform_to_arch_instruction(state, ins);
21144 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021145
Eric Biederman90089602004-05-28 14:11:54 +000021146 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021147}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021148
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021149#if DEBUG_CONSISTENCY
21150static void verify_uses(struct compile_state *state)
21151{
21152 struct triple *first, *ins;
21153 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021154 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021155 ins = first;
21156 do {
21157 struct triple **expr;
21158 expr = triple_rhs(state, ins, 0);
21159 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021160 struct triple *rhs;
21161 rhs = *expr;
21162 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021163 if (set->member == ins) {
21164 break;
21165 }
21166 }
21167 if (!set) {
21168 internal_error(state, ins, "rhs not used");
21169 }
21170 }
21171 expr = triple_lhs(state, ins, 0);
21172 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021173 struct triple *lhs;
21174 lhs = *expr;
21175 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021176 if (set->member == ins) {
21177 break;
21178 }
21179 }
21180 if (!set) {
21181 internal_error(state, ins, "lhs not used");
21182 }
21183 }
Eric Biederman90089602004-05-28 14:11:54 +000021184 expr = triple_misc(state, ins, 0);
21185 if (ins->op != OP_PHI) {
21186 for(; expr; expr = triple_targ(state, ins, expr)) {
21187 struct triple *misc;
21188 misc = *expr;
21189 for(set = misc?misc->use:0; set; set = set->next) {
21190 if (set->member == ins) {
21191 break;
21192 }
21193 }
21194 if (!set) {
21195 internal_error(state, ins, "misc not used");
21196 }
21197 }
21198 }
21199 if (!triple_is_ret(state, ins)) {
21200 expr = triple_targ(state, ins, 0);
21201 for(; expr; expr = triple_targ(state, ins, expr)) {
21202 struct triple *targ;
21203 targ = *expr;
21204 for(set = targ?targ->use:0; set; set = set->next) {
21205 if (set->member == ins) {
21206 break;
21207 }
21208 }
21209 if (!set) {
21210 internal_error(state, ins, "targ not used");
21211 }
21212 }
21213 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021214 ins = ins->next;
21215 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021216
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021217}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021218static void verify_blocks_present(struct compile_state *state)
21219{
21220 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021221 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021222 return;
21223 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021224 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021225 ins = first;
21226 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021227 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021228 if (triple_stores_block(state, ins)) {
21229 if (!ins->u.block) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021230 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021231 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021232 }
21233 }
21234 ins = ins->next;
21235 } while(ins != first);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021236
21237
Eric Biedermand1ea5392003-06-28 06:49:45 +000021238}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021239
21240static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21241{
21242 struct block_set *bedge;
21243 struct block *targ;
21244 targ = block_of_triple(state, edge);
21245 for(bedge = block->edges; bedge; bedge = bedge->next) {
21246 if (bedge->member == targ) {
21247 return 1;
21248 }
21249 }
21250 return 0;
21251}
21252
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021253static void verify_blocks(struct compile_state *state)
21254{
21255 struct triple *ins;
21256 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021257 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021258 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021259 if (!block) {
21260 return;
21261 }
Eric Biederman530b5192003-07-01 10:05:30 +000021262 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021263 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021264 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021265 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021266 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021267 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021268 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021269 internal_error(state, ins, "inconsitent block specified");
21270 }
Eric Biederman530b5192003-07-01 10:05:30 +000021271 valid_ins(state, ins);
21272 }
21273 users = 0;
21274 for(user = block->use; user; user = user->next) {
21275 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021276 if (!user->member->first) {
21277 internal_error(state, block->first, "user is empty");
21278 }
Eric Biederman90089602004-05-28 14:11:54 +000021279 if ((block == state->bb.last_block) &&
21280 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021281 continue;
21282 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021283 for(edge = user->member->edges; edge; edge = edge->next) {
21284 if (edge->member == block) {
21285 break;
21286 }
21287 }
21288 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021289 internal_error(state, user->member->first,
21290 "user does not use block");
21291 }
21292 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021293 if (triple_is_branch(state, block->last)) {
21294 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021295 expr = triple_edge_targ(state, block->last, 0);
21296 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021297 if (*expr && !edge_present(state, block, *expr)) {
21298 internal_error(state, block->last, "no edge to targ");
21299 }
21300 }
Eric Biederman530b5192003-07-01 10:05:30 +000021301 }
Eric Biederman90089602004-05-28 14:11:54 +000021302 if (!triple_is_ubranch(state, block->last) &&
21303 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021304 !edge_present(state, block, block->last->next)) {
21305 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021306 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021307 for(edge = block->edges; edge; edge = edge->next) {
21308 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021309 if (user->member == block) {
21310 break;
21311 }
21312 }
21313 if (!user || user->member != block) {
21314 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021315 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021316 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021317 if (!edge->member->first) {
21318 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021319 }
Eric Biederman530b5192003-07-01 10:05:30 +000021320 }
21321 if (block->users != users) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021322 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021323 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021324 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021325 }
21326 if (!triple_stores_block(state, block->last->next)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021327 internal_error(state, block->last->next,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021328 "cannot find next block");
21329 }
21330 block = block->last->next->u.block;
21331 if (!block) {
21332 internal_error(state, block->last->next,
21333 "bad next block");
21334 }
Eric Biederman90089602004-05-28 14:11:54 +000021335 } while(block != state->bb.first_block);
21336 if (blocks != state->bb.last_vertex) {
21337 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21338 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021339 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021340}
21341
21342static void verify_domination(struct compile_state *state)
21343{
21344 struct triple *first, *ins;
21345 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021346 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021347 return;
21348 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000021349
Eric Biederman83b991a2003-10-11 06:20:25 +000021350 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021351 ins = first;
21352 do {
21353 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021354 struct triple **slot;
21355 struct triple *use_point;
21356 int i, zrhs;
21357 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021358 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021359 slot = &RHS(set->member, 0);
21360 /* See if the use is on the right hand side */
21361 for(i = 0; i < zrhs; i++) {
21362 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021363 break;
21364 }
21365 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021366 if (i < zrhs) {
21367 use_point = set->member;
21368 if (set->member->op == OP_PHI) {
21369 struct block_set *bset;
21370 int edge;
21371 bset = set->member->u.block->use;
21372 for(edge = 0; bset && (edge < i); edge++) {
21373 bset = bset->next;
21374 }
21375 if (!bset) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021376 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021377 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021378 }
21379 use_point = bset->member->last;
21380 }
21381 }
21382 if (use_point &&
21383 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021384 if (is_const(ins)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021385 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021386 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021387 }
21388 else {
Stefan Reinauer14e22772010-04-27 06:56:47 +000021389 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021390 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021391 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021392 }
21393 }
21394 ins = ins->next;
21395 } while(ins != first);
21396}
21397
Eric Biederman83b991a2003-10-11 06:20:25 +000021398static void verify_rhs(struct compile_state *state)
21399{
21400 struct triple *first, *ins;
21401 first = state->first;
21402 ins = first;
21403 do {
21404 struct triple **slot;
21405 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021406 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021407 slot = &RHS(ins, 0);
21408 for(i = 0; i < zrhs; i++) {
21409 if (slot[i] == 0) {
21410 internal_error(state, ins,
21411 "missing rhs %d on %s",
21412 i, tops(ins->op));
21413 }
21414 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21415 internal_error(state, ins,
21416 "ins == rhs[%d] on %s",
21417 i, tops(ins->op));
21418 }
21419 }
21420 ins = ins->next;
21421 } while(ins != first);
21422}
21423
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021424static void verify_piece(struct compile_state *state)
21425{
21426 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021427 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021428 ins = first;
21429 do {
21430 struct triple *ptr;
21431 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021432 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021433 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21434 if (ptr != LHS(ins, i)) {
21435 internal_error(state, ins, "malformed lhs on %s",
21436 tops(ins->op));
21437 }
21438 if (ptr->op != OP_PIECE) {
21439 internal_error(state, ins, "bad lhs op %s at %d on %s",
21440 tops(ptr->op), i, tops(ins->op));
21441 }
21442 if (ptr->u.cval != i) {
21443 internal_error(state, ins, "bad u.cval of %d %d expected",
21444 ptr->u.cval, i);
21445 }
21446 }
21447 ins = ins->next;
21448 } while(ins != first);
21449}
Eric Biederman83b991a2003-10-11 06:20:25 +000021450
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021451static void verify_ins_colors(struct compile_state *state)
21452{
21453 struct triple *first, *ins;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021454
Eric Biederman83b991a2003-10-11 06:20:25 +000021455 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021456 ins = first;
21457 do {
21458 ins = ins->next;
21459 } while(ins != first);
21460}
Eric Biederman90089602004-05-28 14:11:54 +000021461
21462static void verify_unknown(struct compile_state *state)
21463{
21464 struct triple *first, *ins;
21465 if ( (unknown_triple.next != &unknown_triple) ||
21466 (unknown_triple.prev != &unknown_triple) ||
21467#if 0
21468 (unknown_triple.use != 0) ||
21469#endif
21470 (unknown_triple.op != OP_UNKNOWNVAL) ||
21471 (unknown_triple.lhs != 0) ||
21472 (unknown_triple.rhs != 0) ||
21473 (unknown_triple.misc != 0) ||
21474 (unknown_triple.targ != 0) ||
21475 (unknown_triple.template_id != 0) ||
21476 (unknown_triple.id != -1) ||
21477 (unknown_triple.type != &unknown_type) ||
21478 (unknown_triple.occurance != &dummy_occurance) ||
21479 (unknown_triple.param[0] != 0) ||
21480 (unknown_triple.param[1] != 0)) {
21481 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21482 }
21483 if ( (dummy_occurance.count != 2) ||
21484 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21485 (strcmp(dummy_occurance.function, "") != 0) ||
21486 (dummy_occurance.col != 0) ||
21487 (dummy_occurance.parent != 0)) {
21488 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21489 }
21490 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21491 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21492 }
21493 first = state->first;
21494 ins = first;
21495 do {
21496 int params, i;
21497 if (ins == &unknown_triple) {
21498 internal_error(state, ins, "unknown triple in list");
21499 }
21500 params = TRIPLE_SIZE(ins);
21501 for(i = 0; i < params; i++) {
21502 if (ins->param[i] == &unknown_triple) {
21503 internal_error(state, ins, "unknown triple used!");
21504 }
21505 }
21506 ins = ins->next;
21507 } while(ins != first);
21508}
21509
21510static void verify_types(struct compile_state *state)
21511{
21512 struct triple *first, *ins;
21513 first = state->first;
21514 ins = first;
21515 do {
21516 struct type *invalid;
21517 invalid = invalid_type(state, ins->type);
21518 if (invalid) {
21519 FILE *fp = state->errout;
21520 fprintf(fp, "type: ");
21521 name_of(fp, ins->type);
21522 fprintf(fp, "\n");
21523 fprintf(fp, "invalid type: ");
21524 name_of(fp, invalid);
21525 fprintf(fp, "\n");
21526 internal_error(state, ins, "invalid ins type");
21527 }
21528 } while(ins != first);
21529}
21530
21531static void verify_copy(struct compile_state *state)
21532{
21533 struct triple *first, *ins, *next;
21534 first = state->first;
21535 next = ins = first;
21536 do {
21537 ins = next;
21538 next = ins->next;
21539 if (ins->op != OP_COPY) {
21540 continue;
21541 }
21542 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21543 FILE *fp = state->errout;
21544 fprintf(fp, "src type: ");
21545 name_of(fp, RHS(ins, 0)->type);
21546 fprintf(fp, "\n");
21547 fprintf(fp, "dst type: ");
21548 name_of(fp, ins->type);
21549 fprintf(fp, "\n");
21550 internal_error(state, ins, "type mismatch in copy");
21551 }
21552 } while(next != first);
21553}
21554
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021555static void verify_consistency(struct compile_state *state)
21556{
Eric Biederman90089602004-05-28 14:11:54 +000021557 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021558 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021559 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021560 verify_blocks(state);
21561 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021562 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021563 verify_piece(state);
21564 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021565 verify_types(state);
21566 verify_copy(state);
21567 if (state->compiler->debug & DEBUG_VERIFICATION) {
21568 fprintf(state->dbgout, "consistency verified\n");
21569 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021570}
Stefan Reinauer14e22772010-04-27 06:56:47 +000021571#else
Eric Biederman153ea352003-06-20 14:43:20 +000021572static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021573#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021574
21575static void optimize(struct compile_state *state)
21576{
Eric Biederman90089602004-05-28 14:11:54 +000021577 /* Join all of the functions into one giant function */
21578 join_functions(state);
21579
Eric Biederman5ade04a2003-10-22 04:03:46 +000021580 /* Dump what the instruction graph intially looks like */
21581 print_triples(state);
21582
Eric Biederman0babc1c2003-05-09 02:39:00 +000021583 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021584 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021585 print_triples(state);
21586
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021587 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021588 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021589 state->bb.first = state->first;
21590 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021591
Eric Biederman530b5192003-07-01 10:05:30 +000021592 /* Transform the code to ssa form. */
21593 /*
21594 * The transformation to ssa form puts a phi function
21595 * on each of edge of a dominance frontier where that
21596 * phi function might be needed. At -O2 if we don't
21597 * eleminate the excess phi functions we can get an
21598 * exponential code size growth. So I kill the extra
21599 * phi functions early and I kill them often.
21600 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021601 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021602 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021603
Eric Biederman83b991a2003-10-11 06:20:25 +000021604 /* Remove dead code */
21605 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021606 verify_consistency(state);
21607
Eric Biedermanb138ac82003-04-22 18:44:01 +000021608 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021609 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021610 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021611 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021612 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021613 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021614#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021615#warning "WISHLIST implement single use constants (least possible register pressure)"
21616#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021617#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021618 /* Select architecture instructions and an initial partial
21619 * coloring based on architecture constraints.
21620 */
21621 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021622 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021623
Eric Biederman83b991a2003-10-11 06:20:25 +000021624 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021625 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021626 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021627
Eric Biedermanb138ac82003-04-22 18:44:01 +000021628 /* Color all of the variables to see if they will fit in registers */
21629 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021630 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021631
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021632 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021633 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021634
Eric Biedermanb138ac82003-04-22 18:44:01 +000021635 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021636 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021637
Eric Biedermanb138ac82003-04-22 18:44:01 +000021638 /* Remove the optimization information.
21639 * This is more to check for memory consistency than to free memory.
21640 */
Eric Biederman90089602004-05-28 14:11:54 +000021641 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021642}
21643
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021644static void print_op_asm(struct compile_state *state,
21645 struct triple *ins, FILE *fp)
21646{
21647 struct asm_info *info;
21648 const char *ptr;
21649 unsigned lhs, rhs, i;
21650 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021651 lhs = ins->lhs;
21652 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021653 /* Don't count the clobbers in lhs */
21654 for(i = 0; i < lhs; i++) {
21655 if (LHS(ins, i)->type == &void_type) {
21656 break;
21657 }
21658 }
21659 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021660 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021661 fputc('\t', fp);
21662 for(ptr = info->str; *ptr; ptr++) {
21663 char *next;
21664 unsigned long param;
21665 struct triple *piece;
21666 if (*ptr != '%') {
21667 fputc(*ptr, fp);
21668 continue;
21669 }
21670 ptr++;
21671 if (*ptr == '%') {
21672 fputc('%', fp);
21673 continue;
21674 }
21675 param = strtoul(ptr, &next, 10);
21676 if (ptr == next) {
21677 error(state, ins, "Invalid asm template");
21678 }
21679 if (param >= (lhs + rhs)) {
21680 error(state, ins, "Invalid param %%%u in asm template",
21681 param);
21682 }
21683 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
Stefan Reinauer14e22772010-04-27 06:56:47 +000021684 fprintf(fp, "%s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021685 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021686 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021687 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021688 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021689}
21690
21691
21692/* Only use the low x86 byte registers. This allows me
21693 * allocate the entire register when a byte register is used.
21694 */
21695#define X86_4_8BIT_GPRS 1
21696
Eric Biederman83b991a2003-10-11 06:20:25 +000021697/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021698#define X86_MMX_REGS (1<<0)
21699#define X86_XMM_REGS (1<<1)
21700#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021701
Eric Biedermanb138ac82003-04-22 18:44:01 +000021702/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021703#define REGC_FLAGS 0
21704#define REGC_GPR8 1
21705#define REGC_GPR16 2
21706#define REGC_GPR32 3
21707#define REGC_DIVIDEND64 4
21708#define REGC_DIVIDEND32 5
21709#define REGC_MMX 6
21710#define REGC_XMM 7
21711#define REGC_GPR32_8 8
21712#define REGC_GPR16_8 9
21713#define REGC_GPR8_LO 10
21714#define REGC_IMM32 11
21715#define REGC_IMM16 12
21716#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021717#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021718#if LAST_REGC >= MAX_REGC
21719#error "MAX_REGC is to low"
21720#endif
21721
21722/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021723#define REGCM_FLAGS (1 << REGC_FLAGS)
21724#define REGCM_GPR8 (1 << REGC_GPR8)
21725#define REGCM_GPR16 (1 << REGC_GPR16)
21726#define REGCM_GPR32 (1 << REGC_GPR32)
21727#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21728#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21729#define REGCM_MMX (1 << REGC_MMX)
21730#define REGCM_XMM (1 << REGC_XMM)
21731#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21732#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21733#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21734#define REGCM_IMM32 (1 << REGC_IMM32)
21735#define REGCM_IMM16 (1 << REGC_IMM16)
21736#define REGCM_IMM8 (1 << REGC_IMM8)
21737#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021738#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021739
21740/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021741#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021742#define REGC_FLAGS_FIRST REG_EFLAGS
21743#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021744#define REG_AL 3
21745#define REG_BL 4
21746#define REG_CL 5
21747#define REG_DL 6
21748#define REG_AH 7
21749#define REG_BH 8
21750#define REG_CH 9
21751#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021752#define REGC_GPR8_LO_FIRST REG_AL
21753#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021754#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021755#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021756#define REG_AX 11
21757#define REG_BX 12
21758#define REG_CX 13
21759#define REG_DX 14
21760#define REG_SI 15
21761#define REG_DI 16
21762#define REG_BP 17
21763#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021764#define REGC_GPR16_FIRST REG_AX
21765#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021766#define REG_EAX 19
21767#define REG_EBX 20
21768#define REG_ECX 21
21769#define REG_EDX 22
21770#define REG_ESI 23
21771#define REG_EDI 24
21772#define REG_EBP 25
21773#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021774#define REGC_GPR32_FIRST REG_EAX
21775#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021776#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021777#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21778#define REGC_DIVIDEND64_LAST REG_EDXEAX
21779#define REG_DXAX 28
21780#define REGC_DIVIDEND32_FIRST REG_DXAX
21781#define REGC_DIVIDEND32_LAST REG_DXAX
21782#define REG_MMX0 29
21783#define REG_MMX1 30
21784#define REG_MMX2 31
21785#define REG_MMX3 32
21786#define REG_MMX4 33
21787#define REG_MMX5 34
21788#define REG_MMX6 35
21789#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021790#define REGC_MMX_FIRST REG_MMX0
21791#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021792#define REG_XMM0 37
21793#define REG_XMM1 38
21794#define REG_XMM2 39
21795#define REG_XMM3 40
21796#define REG_XMM4 41
21797#define REG_XMM5 42
21798#define REG_XMM6 43
21799#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021800#define REGC_XMM_FIRST REG_XMM0
21801#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021802
21803#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021804#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021805#endif
21806
Eric Biedermanb138ac82003-04-22 18:44:01 +000021807#define LAST_REG REG_XMM7
21808
21809#define REGC_GPR32_8_FIRST REG_EAX
21810#define REGC_GPR32_8_LAST REG_EDX
21811#define REGC_GPR16_8_FIRST REG_AX
21812#define REGC_GPR16_8_LAST REG_DX
21813
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021814#define REGC_IMM8_FIRST -1
21815#define REGC_IMM8_LAST -1
21816#define REGC_IMM16_FIRST -2
21817#define REGC_IMM16_LAST -1
21818#define REGC_IMM32_FIRST -4
21819#define REGC_IMM32_LAST -1
21820
Eric Biedermanb138ac82003-04-22 18:44:01 +000021821#if LAST_REG >= MAX_REGISTERS
21822#error "MAX_REGISTERS to low"
21823#endif
21824
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021825
21826static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021827 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21828 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21829 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21830 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21831 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21832 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21833 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21834 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21835 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21836 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21837 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21838 [REGC_IMM32] = 0,
21839 [REGC_IMM16] = 0,
21840 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021841};
21842
21843static const struct {
21844 int first, last;
21845} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021846 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21847 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21848 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21849 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21850 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21851 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21852 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21853 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21854 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21855 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21856 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21857 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21858 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21859 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021860};
21861
Eric Biederman90089602004-05-28 14:11:54 +000021862#if ARCH_INPUT_REGS != 4
21863#error ARCH_INPUT_REGS size mismatch
21864#endif
21865static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21866 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21867 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21868 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21869 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21870};
21871
21872#if ARCH_OUTPUT_REGS != 4
21873#error ARCH_INPUT_REGS size mismatch
21874#endif
21875static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21876 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21877 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21878 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21879 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21880};
21881
Eric Biederman5ade04a2003-10-22 04:03:46 +000021882static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021883{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021884 memset(arch, 0, sizeof(*arch));
21885 arch->features = 0;
21886}
21887
Eric Biederman90089602004-05-28 14:11:54 +000021888static const struct compiler_flag arch_flags[] = {
21889 { "mmx", X86_MMX_REGS },
21890 { "sse", X86_XMM_REGS },
21891 { "noop-copy", X86_NOOP_COPY },
21892 { 0, 0 },
21893};
21894static const struct compiler_flag arch_cpus[] = {
21895 { "i386", 0 },
21896 { "p2", X86_MMX_REGS },
21897 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21898 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21899 { "k7", X86_MMX_REGS },
21900 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21901 { "c3", X86_MMX_REGS },
21902 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21903 { 0, 0 }
21904};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021905static int arch_encode_flag(struct arch_state *arch, const char *flag)
21906{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021907 int result;
21908 int act;
21909
21910 act = 1;
21911 result = -1;
21912 if (strncmp(flag, "no-", 3) == 0) {
21913 flag += 3;
21914 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021915 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021916 if (act && strncmp(flag, "cpu=", 4) == 0) {
21917 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021918 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021919 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021920 else {
Eric Biederman90089602004-05-28 14:11:54 +000021921 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021922 }
21923 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021924}
21925
Eric Biederman90089602004-05-28 14:11:54 +000021926static void arch_usage(FILE *fp)
21927{
21928 flag_usage(fp, arch_flags, "-m", "-mno-");
21929 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21930}
21931
Eric Biedermanb138ac82003-04-22 18:44:01 +000021932static unsigned arch_regc_size(struct compile_state *state, int class)
21933{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021934 if ((class < 0) || (class > LAST_REGC)) {
21935 return 0;
21936 }
21937 return regc_size[class];
21938}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021939
Eric Biedermanb138ac82003-04-22 18:44:01 +000021940static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21941{
21942 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021943 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
Stefan Reinauer14e22772010-04-27 06:56:47 +000021944 REGCM_GPR32_8 | REGCM_GPR32 |
Eric Biederman530b5192003-07-01 10:05:30 +000021945 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021946
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021947 /* Special case for the immediates */
21948 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21949 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21950 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
Stefan Reinauer14e22772010-04-27 06:56:47 +000021951 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021952 return 0;
21953 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021954 return (regcm1 & regcm2) ||
21955 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21956}
21957
21958static void arch_reg_equivs(
21959 struct compile_state *state, unsigned *equiv, int reg)
21960{
21961 if ((reg < 0) || (reg > LAST_REG)) {
21962 internal_error(state, 0, "invalid register");
21963 }
21964 *equiv++ = reg;
21965 switch(reg) {
21966 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021967#if X86_4_8BIT_GPRS
21968 *equiv++ = REG_AH;
21969#endif
21970 *equiv++ = REG_AX;
21971 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021972 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021973 *equiv++ = REG_EDXEAX;
21974 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021975 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021976#if X86_4_8BIT_GPRS
21977 *equiv++ = REG_AL;
21978#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021979 *equiv++ = REG_AX;
21980 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021981 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021982 *equiv++ = REG_EDXEAX;
21983 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000021984 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021985#if X86_4_8BIT_GPRS
21986 *equiv++ = REG_BH;
21987#endif
21988 *equiv++ = REG_BX;
21989 *equiv++ = REG_EBX;
21990 break;
21991
Eric Biedermanb138ac82003-04-22 18:44:01 +000021992 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021993#if X86_4_8BIT_GPRS
21994 *equiv++ = REG_BL;
21995#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021996 *equiv++ = REG_BX;
21997 *equiv++ = REG_EBX;
21998 break;
21999 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022000#if X86_4_8BIT_GPRS
22001 *equiv++ = REG_CH;
22002#endif
22003 *equiv++ = REG_CX;
22004 *equiv++ = REG_ECX;
22005 break;
22006
Eric Biedermanb138ac82003-04-22 18:44:01 +000022007 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022008#if X86_4_8BIT_GPRS
22009 *equiv++ = REG_CL;
22010#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022011 *equiv++ = REG_CX;
22012 *equiv++ = REG_ECX;
22013 break;
22014 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022015#if X86_4_8BIT_GPRS
22016 *equiv++ = REG_DH;
22017#endif
22018 *equiv++ = REG_DX;
22019 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022020 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022021 *equiv++ = REG_EDXEAX;
22022 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022023 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022024#if X86_4_8BIT_GPRS
22025 *equiv++ = REG_DL;
22026#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022027 *equiv++ = REG_DX;
22028 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022029 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022030 *equiv++ = REG_EDXEAX;
22031 break;
22032 case REG_AX:
22033 *equiv++ = REG_AL;
22034 *equiv++ = REG_AH;
22035 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022036 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022037 *equiv++ = REG_EDXEAX;
22038 break;
22039 case REG_BX:
22040 *equiv++ = REG_BL;
22041 *equiv++ = REG_BH;
22042 *equiv++ = REG_EBX;
22043 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022044 case REG_CX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022045 *equiv++ = REG_CL;
22046 *equiv++ = REG_CH;
22047 *equiv++ = REG_ECX;
22048 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022049 case REG_DX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022050 *equiv++ = REG_DL;
22051 *equiv++ = REG_DH;
22052 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022053 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022054 *equiv++ = REG_EDXEAX;
22055 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022056 case REG_SI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022057 *equiv++ = REG_ESI;
22058 break;
22059 case REG_DI:
22060 *equiv++ = REG_EDI;
22061 break;
22062 case REG_BP:
22063 *equiv++ = REG_EBP;
22064 break;
22065 case REG_SP:
22066 *equiv++ = REG_ESP;
22067 break;
22068 case REG_EAX:
22069 *equiv++ = REG_AL;
22070 *equiv++ = REG_AH;
22071 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022072 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022073 *equiv++ = REG_EDXEAX;
22074 break;
22075 case REG_EBX:
22076 *equiv++ = REG_BL;
22077 *equiv++ = REG_BH;
22078 *equiv++ = REG_BX;
22079 break;
22080 case REG_ECX:
22081 *equiv++ = REG_CL;
22082 *equiv++ = REG_CH;
22083 *equiv++ = REG_CX;
22084 break;
22085 case REG_EDX:
22086 *equiv++ = REG_DL;
22087 *equiv++ = REG_DH;
22088 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022089 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022090 *equiv++ = REG_EDXEAX;
22091 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022092 case REG_ESI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022093 *equiv++ = REG_SI;
22094 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022095 case REG_EDI:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022096 *equiv++ = REG_DI;
22097 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022098 case REG_EBP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022099 *equiv++ = REG_BP;
22100 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022101 case REG_ESP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022102 *equiv++ = REG_SP;
22103 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022104 case REG_DXAX:
Eric Biederman530b5192003-07-01 10:05:30 +000022105 *equiv++ = REG_AL;
22106 *equiv++ = REG_AH;
22107 *equiv++ = REG_DL;
22108 *equiv++ = REG_DH;
22109 *equiv++ = REG_AX;
22110 *equiv++ = REG_DX;
22111 *equiv++ = REG_EAX;
22112 *equiv++ = REG_EDX;
22113 *equiv++ = REG_EDXEAX;
22114 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022115 case REG_EDXEAX:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022116 *equiv++ = REG_AL;
22117 *equiv++ = REG_AH;
22118 *equiv++ = REG_DL;
22119 *equiv++ = REG_DH;
22120 *equiv++ = REG_AX;
22121 *equiv++ = REG_DX;
22122 *equiv++ = REG_EAX;
22123 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022124 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022125 break;
22126 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000022127 *equiv++ = REG_UNSET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022128}
22129
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022130static unsigned arch_avail_mask(struct compile_state *state)
22131{
22132 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022133 /* REGCM_GPR8 is not available */
Stefan Reinauer14e22772010-04-27 06:56:47 +000022134 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22135 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022136 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022137 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022138 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022139 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022140 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022141 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022142 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022143 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022144 return avail_mask;
22145}
22146
22147static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22148{
22149 unsigned mask, result;
22150 int class, class2;
22151 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022152
22153 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22154 if ((result & mask) == 0) {
22155 continue;
22156 }
22157 if (class > LAST_REGC) {
22158 result &= ~mask;
22159 }
22160 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22161 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22162 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22163 result |= (1 << class2);
22164 }
22165 }
22166 }
Eric Biederman530b5192003-07-01 10:05:30 +000022167 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022168 return result;
22169}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022170
Eric Biedermand1ea5392003-06-28 06:49:45 +000022171static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22172{
22173 /* Like arch_regcm_normalize except immediate register classes are excluded */
22174 regcm = arch_regcm_normalize(state, regcm);
22175 /* Remove the immediate register classes */
22176 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22177 return regcm;
Stefan Reinauer14e22772010-04-27 06:56:47 +000022178
Eric Biedermand1ea5392003-06-28 06:49:45 +000022179}
22180
Eric Biedermanb138ac82003-04-22 18:44:01 +000022181static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22182{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022183 unsigned mask;
22184 int class;
22185 mask = 0;
22186 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022187 if ((reg >= regcm_bound[class].first) &&
22188 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022189 mask |= (1 << class);
22190 }
22191 }
22192 if (!mask) {
22193 internal_error(state, 0, "reg %d not in any class", reg);
22194 }
22195 return mask;
22196}
22197
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022198static struct reg_info arch_reg_constraint(
22199 struct compile_state *state, struct type *type, const char *constraint)
22200{
22201 static const struct {
22202 char class;
22203 unsigned int mask;
22204 unsigned int reg;
22205 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022206 { 'r', REGCM_GPR32, REG_UNSET },
22207 { 'g', REGCM_GPR32, REG_UNSET },
22208 { 'p', REGCM_GPR32, REG_UNSET },
22209 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022210 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022211 { 'x', REGCM_XMM, REG_UNSET },
22212 { 'y', REGCM_MMX, REG_UNSET },
22213 { 'a', REGCM_GPR32, REG_EAX },
22214 { 'b', REGCM_GPR32, REG_EBX },
22215 { 'c', REGCM_GPR32, REG_ECX },
22216 { 'd', REGCM_GPR32, REG_EDX },
22217 { 'D', REGCM_GPR32, REG_EDI },
22218 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022219 { '\0', 0, REG_UNSET },
22220 };
22221 unsigned int regcm;
22222 unsigned int mask, reg;
22223 struct reg_info result;
22224 const char *ptr;
22225 regcm = arch_type_to_regcm(state, type);
22226 reg = REG_UNSET;
22227 mask = 0;
22228 for(ptr = constraint; *ptr; ptr++) {
22229 int i;
22230 if (*ptr == ' ') {
22231 continue;
22232 }
22233 for(i = 0; constraints[i].class != '\0'; i++) {
22234 if (constraints[i].class == *ptr) {
22235 break;
22236 }
22237 }
22238 if (constraints[i].class == '\0') {
22239 error(state, 0, "invalid register constraint ``%c''", *ptr);
22240 break;
22241 }
22242 if ((constraints[i].mask & regcm) == 0) {
22243 error(state, 0, "invalid register class %c specified",
22244 *ptr);
22245 }
22246 mask |= constraints[i].mask;
22247 if (constraints[i].reg != REG_UNSET) {
22248 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22249 error(state, 0, "Only one register may be specified");
22250 }
22251 reg = constraints[i].reg;
22252 }
22253 }
22254 result.reg = reg;
22255 result.regcm = mask;
22256 return result;
22257}
22258
22259static struct reg_info arch_reg_clobber(
22260 struct compile_state *state, const char *clobber)
22261{
22262 struct reg_info result;
22263 if (strcmp(clobber, "memory") == 0) {
22264 result.reg = REG_UNSET;
22265 result.regcm = 0;
22266 }
Eric Biederman90089602004-05-28 14:11:54 +000022267 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022268 result.reg = REG_EAX;
22269 result.regcm = REGCM_GPR32;
22270 }
Eric Biederman90089602004-05-28 14:11:54 +000022271 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022272 result.reg = REG_EBX;
22273 result.regcm = REGCM_GPR32;
22274 }
Eric Biederman90089602004-05-28 14:11:54 +000022275 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022276 result.reg = REG_ECX;
22277 result.regcm = REGCM_GPR32;
22278 }
Eric Biederman90089602004-05-28 14:11:54 +000022279 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022280 result.reg = REG_EDX;
22281 result.regcm = REGCM_GPR32;
22282 }
Eric Biederman90089602004-05-28 14:11:54 +000022283 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022284 result.reg = REG_ESI;
22285 result.regcm = REGCM_GPR32;
22286 }
Eric Biederman90089602004-05-28 14:11:54 +000022287 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022288 result.reg = REG_EDI;
22289 result.regcm = REGCM_GPR32;
22290 }
Eric Biederman90089602004-05-28 14:11:54 +000022291 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022292 result.reg = REG_EBP;
22293 result.regcm = REGCM_GPR32;
22294 }
Eric Biederman90089602004-05-28 14:11:54 +000022295 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022296 result.reg = REG_ESP;
22297 result.regcm = REGCM_GPR32;
22298 }
22299 else if (strcmp(clobber, "cc") == 0) {
22300 result.reg = REG_EFLAGS;
22301 result.regcm = REGCM_FLAGS;
22302 }
22303 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22304 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22305 result.reg = REG_XMM0 + octdigval(clobber[3]);
22306 result.regcm = REGCM_XMM;
22307 }
Eric Biederman90089602004-05-28 14:11:54 +000022308 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022309 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22310 result.reg = REG_MMX0 + octdigval(clobber[3]);
22311 result.regcm = REGCM_MMX;
22312 }
22313 else {
Eric Biederman90089602004-05-28 14:11:54 +000022314 error(state, 0, "unknown register name `%s' in asm",
22315 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022316 result.reg = REG_UNSET;
22317 result.regcm = 0;
22318 }
22319 return result;
22320}
22321
Stefan Reinauer14e22772010-04-27 06:56:47 +000022322static int do_select_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000022323 char *used, int reg, unsigned classes)
22324{
22325 unsigned mask;
22326 if (used[reg]) {
22327 return REG_UNSET;
22328 }
22329 mask = arch_reg_regcm(state, reg);
22330 return (classes & mask) ? reg : REG_UNSET;
22331}
22332
22333static int arch_select_free_register(
22334 struct compile_state *state, char *used, int classes)
22335{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022336 /* Live ranges with the most neighbors are colored first.
22337 *
22338 * Generally it does not matter which colors are given
22339 * as the register allocator attempts to color live ranges
22340 * in an order where you are guaranteed not to run out of colors.
22341 *
22342 * Occasionally the register allocator cannot find an order
22343 * of register selection that will find a free color. To
22344 * increase the odds the register allocator will work when
22345 * it guesses first give out registers from register classes
22346 * least likely to run out of registers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000022347 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022348 */
22349 int i, reg;
22350 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022351 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022352 reg = do_select_reg(state, used, i, classes);
22353 }
22354 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22355 reg = do_select_reg(state, used, i, classes);
22356 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022357 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022358 reg = do_select_reg(state, used, i, classes);
22359 }
22360 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22361 reg = do_select_reg(state, used, i, classes);
22362 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022363 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22364 reg = do_select_reg(state, used, i, classes);
22365 }
Eric Biederman530b5192003-07-01 10:05:30 +000022366 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22367 reg = do_select_reg(state, used, i, classes);
22368 }
22369 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22370 reg = do_select_reg(state, used, i, classes);
22371 }
22372 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022373 reg = do_select_reg(state, used, i, classes);
22374 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022375 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22376 reg = do_select_reg(state, used, i, classes);
22377 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022378 return reg;
22379}
22380
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022381
Stefan Reinauer14e22772010-04-27 06:56:47 +000022382static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022383{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022384
22385#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022386#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022387#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022388 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022389 mask = 0;
22390 switch(type->type & TYPE_MASK) {
22391 case TYPE_ARRAY:
Stefan Reinauer14e22772010-04-27 06:56:47 +000022392 case TYPE_VOID:
22393 mask = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022394 break;
22395 case TYPE_CHAR:
22396 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022397 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Stefan Reinauer14e22772010-04-27 06:56:47 +000022398 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 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022403 break;
22404 case TYPE_SHORT:
22405 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022406 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022407 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022408 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022409 REGCM_MMX | REGCM_XMM |
22410 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022411 break;
Eric Biederman90089602004-05-28 14:11:54 +000022412 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022413 case TYPE_INT:
22414 case TYPE_UINT:
22415 case TYPE_LONG:
22416 case TYPE_ULONG:
22417 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022418 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022419 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22420 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022421 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022422 break;
Eric Biederman90089602004-05-28 14:11:54 +000022423 case TYPE_JOIN:
22424 case TYPE_UNION:
22425 mask = arch_type_to_regcm(state, type->left);
22426 break;
22427 case TYPE_OVERLAP:
22428 mask = arch_type_to_regcm(state, type->left) &
22429 arch_type_to_regcm(state, type->right);
22430 break;
22431 case TYPE_BITFIELD:
22432 mask = arch_type_to_regcm(state, type->left);
22433 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022434 default:
Eric Biederman90089602004-05-28 14:11:54 +000022435 fprintf(state->errout, "type: ");
22436 name_of(state->errout, type);
22437 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022438 internal_error(state, 0, "no register class for type");
22439 break;
22440 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022441 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022442 return mask;
22443}
22444
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022445static int is_imm32(struct triple *imm)
22446{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022447 // second condition commented out to prevent compiler warning:
22448 // imm->u.cval is always 32bit unsigned, so the comparison is
22449 // always true.
22450 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022451 (imm->op == OP_ADDRCONST);
Stefan Reinauer14e22772010-04-27 06:56:47 +000022452
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022453}
22454static int is_imm16(struct triple *imm)
22455{
22456 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22457}
22458static int is_imm8(struct triple *imm)
22459{
22460 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22461}
22462
22463static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022464{
22465 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022466 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022467 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022468 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022469 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022470 if (!is_imm32(imm)) {
22471 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022472 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022473 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022474 use_triple(imm, ins);
22475 *expr = imm;
22476 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022477}
22478
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022479static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022480{
22481 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022482 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022483 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022484 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022485 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022486 if (!is_imm8(imm)) {
22487 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022488 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022489 unuse_triple(*expr, ins);
22490 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022491 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022492 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022493}
22494
Eric Biederman530b5192003-07-01 10:05:30 +000022495#define TEMPLATE_NOP 0
22496#define TEMPLATE_INTCONST8 1
22497#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022498#define TEMPLATE_UNKNOWNVAL 3
22499#define TEMPLATE_COPY8_REG 5
22500#define TEMPLATE_COPY16_REG 6
22501#define TEMPLATE_COPY32_REG 7
22502#define TEMPLATE_COPY_IMM8 8
22503#define TEMPLATE_COPY_IMM16 9
22504#define TEMPLATE_COPY_IMM32 10
22505#define TEMPLATE_PHI8 11
22506#define TEMPLATE_PHI16 12
22507#define TEMPLATE_PHI32 13
22508#define TEMPLATE_STORE8 14
22509#define TEMPLATE_STORE16 15
22510#define TEMPLATE_STORE32 16
22511#define TEMPLATE_LOAD8 17
22512#define TEMPLATE_LOAD16 18
22513#define TEMPLATE_LOAD32 19
22514#define TEMPLATE_BINARY8_REG 20
22515#define TEMPLATE_BINARY16_REG 21
22516#define TEMPLATE_BINARY32_REG 22
22517#define TEMPLATE_BINARY8_IMM 23
22518#define TEMPLATE_BINARY16_IMM 24
22519#define TEMPLATE_BINARY32_IMM 25
22520#define TEMPLATE_SL8_CL 26
22521#define TEMPLATE_SL16_CL 27
22522#define TEMPLATE_SL32_CL 28
22523#define TEMPLATE_SL8_IMM 29
22524#define TEMPLATE_SL16_IMM 30
22525#define TEMPLATE_SL32_IMM 31
22526#define TEMPLATE_UNARY8 32
22527#define TEMPLATE_UNARY16 33
22528#define TEMPLATE_UNARY32 34
22529#define TEMPLATE_CMP8_REG 35
22530#define TEMPLATE_CMP16_REG 36
22531#define TEMPLATE_CMP32_REG 37
22532#define TEMPLATE_CMP8_IMM 38
22533#define TEMPLATE_CMP16_IMM 39
22534#define TEMPLATE_CMP32_IMM 40
22535#define TEMPLATE_TEST8 41
22536#define TEMPLATE_TEST16 42
22537#define TEMPLATE_TEST32 43
22538#define TEMPLATE_SET 44
22539#define TEMPLATE_JMP 45
22540#define TEMPLATE_RET 46
22541#define TEMPLATE_INB_DX 47
22542#define TEMPLATE_INB_IMM 48
22543#define TEMPLATE_INW_DX 49
22544#define TEMPLATE_INW_IMM 50
22545#define TEMPLATE_INL_DX 51
22546#define TEMPLATE_INL_IMM 52
22547#define TEMPLATE_OUTB_DX 53
22548#define TEMPLATE_OUTB_IMM 54
22549#define TEMPLATE_OUTW_DX 55
22550#define TEMPLATE_OUTW_IMM 56
22551#define TEMPLATE_OUTL_DX 57
22552#define TEMPLATE_OUTL_IMM 58
22553#define TEMPLATE_BSF 59
22554#define TEMPLATE_RDMSR 60
22555#define TEMPLATE_WRMSR 61
22556#define TEMPLATE_UMUL8 62
22557#define TEMPLATE_UMUL16 63
22558#define TEMPLATE_UMUL32 64
22559#define TEMPLATE_DIV8 65
22560#define TEMPLATE_DIV16 66
22561#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022562#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022563#if LAST_TEMPLATE >= MAX_TEMPLATES
22564#error "MAX_TEMPLATES to low"
22565#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022566
Eric Biederman530b5192003-07-01 10:05:30 +000022567#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 +000022568#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
Eric Biederman530b5192003-07-01 10:05:30 +000022569#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022570
Eric Biedermanb138ac82003-04-22 18:44:01 +000022571
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022572static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022573 [TEMPLATE_NOP] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022574 .lhs = {
Eric Biederman90089602004-05-28 14:11:54 +000022575 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22576 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22577 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22578 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22638 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22639 },
22640 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022641 [TEMPLATE_INTCONST8] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022642 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22643 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022644 [TEMPLATE_INTCONST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022645 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22646 },
Eric Biederman90089602004-05-28 14:11:54 +000022647 [TEMPLATE_UNKNOWNVAL] = {
22648 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22649 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022650 [TEMPLATE_COPY8_REG] = {
22651 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22652 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022653 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022654 [TEMPLATE_COPY16_REG] = {
22655 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22656 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22657 },
22658 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022659 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022660 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022661 },
22662 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022663 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022664 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22665 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022666 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022667 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022668 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22669 },
22670 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022671 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022672 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22673 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022674 [TEMPLATE_PHI8] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022675 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022676 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22677 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022678 [TEMPLATE_PHI16] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022679 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022680 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022681 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022682 [TEMPLATE_PHI32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022683 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022684 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022685 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022686 [TEMPLATE_STORE8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022687 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022688 [0] = { REG_UNSET, REGCM_GPR32 },
22689 [1] = { REG_UNSET, REGCM_GPR8_LO },
22690 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022691 },
22692 [TEMPLATE_STORE16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022693 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022694 [0] = { REG_UNSET, REGCM_GPR32 },
22695 [1] = { REG_UNSET, REGCM_GPR16 },
22696 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022697 },
22698 [TEMPLATE_STORE32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022699 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022700 [0] = { REG_UNSET, REGCM_GPR32 },
22701 [1] = { REG_UNSET, REGCM_GPR32 },
22702 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022703 },
22704 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022705 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022706 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22707 },
22708 [TEMPLATE_LOAD16] = {
22709 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22710 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22711 },
22712 [TEMPLATE_LOAD32] = {
22713 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22714 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22715 },
Eric Biederman530b5192003-07-01 10:05:30 +000022716 [TEMPLATE_BINARY8_REG] = {
22717 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022718 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022719 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22720 [1] = { REG_UNSET, REGCM_GPR8_LO },
22721 },
22722 },
22723 [TEMPLATE_BINARY16_REG] = {
22724 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022725 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022726 [0] = { REG_VIRT0, REGCM_GPR16 },
22727 [1] = { REG_UNSET, REGCM_GPR16 },
22728 },
22729 },
22730 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022731 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022732 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022733 [0] = { REG_VIRT0, REGCM_GPR32 },
22734 [1] = { REG_UNSET, REGCM_GPR32 },
22735 },
22736 },
Eric Biederman530b5192003-07-01 10:05:30 +000022737 [TEMPLATE_BINARY8_IMM] = {
22738 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022739 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022740 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22741 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22742 },
22743 },
22744 [TEMPLATE_BINARY16_IMM] = {
22745 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022746 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022747 [0] = { REG_VIRT0, REGCM_GPR16 },
22748 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22749 },
22750 },
22751 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022752 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022753 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022754 [0] = { REG_VIRT0, REGCM_GPR32 },
22755 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22756 },
22757 },
Eric Biederman530b5192003-07-01 10:05:30 +000022758 [TEMPLATE_SL8_CL] = {
22759 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022760 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022761 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22762 [1] = { REG_CL, REGCM_GPR8_LO },
22763 },
22764 },
22765 [TEMPLATE_SL16_CL] = {
22766 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022767 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022768 [0] = { REG_VIRT0, REGCM_GPR16 },
22769 [1] = { REG_CL, REGCM_GPR8_LO },
22770 },
22771 },
22772 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022773 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022774 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022775 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022776 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022777 },
22778 },
Eric Biederman530b5192003-07-01 10:05:30 +000022779 [TEMPLATE_SL8_IMM] = {
22780 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022781 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022782 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22783 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22784 },
22785 },
22786 [TEMPLATE_SL16_IMM] = {
22787 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022788 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022789 [0] = { REG_VIRT0, REGCM_GPR16 },
22790 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22791 },
22792 },
22793 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022794 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022795 .rhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022796 [0] = { REG_VIRT0, REGCM_GPR32 },
22797 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22798 },
22799 },
Eric Biederman530b5192003-07-01 10:05:30 +000022800 [TEMPLATE_UNARY8] = {
22801 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22802 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22803 },
22804 [TEMPLATE_UNARY16] = {
22805 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22806 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22807 },
22808 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022809 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22810 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22811 },
Eric Biederman530b5192003-07-01 10:05:30 +000022812 [TEMPLATE_CMP8_REG] = {
22813 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22814 .rhs = {
22815 [0] = { REG_UNSET, REGCM_GPR8_LO },
22816 [1] = { REG_UNSET, REGCM_GPR8_LO },
22817 },
22818 },
22819 [TEMPLATE_CMP16_REG] = {
22820 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22821 .rhs = {
22822 [0] = { REG_UNSET, REGCM_GPR16 },
22823 [1] = { REG_UNSET, REGCM_GPR16 },
22824 },
22825 },
22826 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022827 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22828 .rhs = {
22829 [0] = { REG_UNSET, REGCM_GPR32 },
22830 [1] = { REG_UNSET, REGCM_GPR32 },
22831 },
22832 },
Eric Biederman530b5192003-07-01 10:05:30 +000022833 [TEMPLATE_CMP8_IMM] = {
22834 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22835 .rhs = {
22836 [0] = { REG_UNSET, REGCM_GPR8_LO },
22837 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22838 },
22839 },
22840 [TEMPLATE_CMP16_IMM] = {
22841 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22842 .rhs = {
22843 [0] = { REG_UNSET, REGCM_GPR16 },
22844 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22845 },
22846 },
22847 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022848 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22849 .rhs = {
22850 [0] = { REG_UNSET, REGCM_GPR32 },
22851 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22852 },
22853 },
Eric Biederman530b5192003-07-01 10:05:30 +000022854 [TEMPLATE_TEST8] = {
22855 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22856 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22857 },
22858 [TEMPLATE_TEST16] = {
22859 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22860 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22861 },
22862 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022863 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22864 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22865 },
22866 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022867 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022868 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22869 },
22870 [TEMPLATE_JMP] = {
22871 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22872 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022873 [TEMPLATE_RET] = {
22874 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22875 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022876 [TEMPLATE_INB_DX] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022877 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022878 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22879 },
22880 [TEMPLATE_INB_IMM] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022881 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022882 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22883 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022884 [TEMPLATE_INW_DX] = {
22885 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022886 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22887 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022888 [TEMPLATE_INW_IMM] = {
22889 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022890 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22891 },
22892 [TEMPLATE_INL_DX] = {
22893 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22894 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22895 },
22896 [TEMPLATE_INL_IMM] = {
22897 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22898 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22899 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022900 [TEMPLATE_OUTB_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022901 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022902 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022903 [1] = { REG_DX, REGCM_GPR16 },
22904 },
22905 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022906 [TEMPLATE_OUTB_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022907 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022908 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022909 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22910 },
22911 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022912 [TEMPLATE_OUTW_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022913 .rhs = {
22914 [0] = { REG_AX, REGCM_GPR16 },
22915 [1] = { REG_DX, REGCM_GPR16 },
22916 },
22917 },
22918 [TEMPLATE_OUTW_IMM] = {
22919 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022920 [0] = { REG_AX, REGCM_GPR16 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022921 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22922 },
22923 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022924 [TEMPLATE_OUTL_DX] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022925 .rhs = {
22926 [0] = { REG_EAX, REGCM_GPR32 },
22927 [1] = { REG_DX, REGCM_GPR16 },
22928 },
22929 },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022930 [TEMPLATE_OUTL_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022931 .rhs = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022932 [0] = { REG_EAX, REGCM_GPR32 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022933 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22934 },
22935 },
22936 [TEMPLATE_BSF] = {
22937 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22938 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22939 },
22940 [TEMPLATE_RDMSR] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022941 .lhs = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022942 [0] = { REG_EAX, REGCM_GPR32 },
22943 [1] = { REG_EDX, REGCM_GPR32 },
22944 },
22945 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22946 },
22947 [TEMPLATE_WRMSR] = {
22948 .rhs = {
22949 [0] = { REG_ECX, REGCM_GPR32 },
22950 [1] = { REG_EAX, REGCM_GPR32 },
22951 [2] = { REG_EDX, REGCM_GPR32 },
22952 },
22953 },
Eric Biederman530b5192003-07-01 10:05:30 +000022954 [TEMPLATE_UMUL8] = {
22955 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022956 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022957 [0] = { REG_AL, REGCM_GPR8_LO },
22958 [1] = { REG_UNSET, REGCM_GPR8_LO },
22959 },
22960 },
22961 [TEMPLATE_UMUL16] = {
22962 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022963 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022964 [0] = { REG_AX, REGCM_GPR16 },
22965 [1] = { REG_UNSET, REGCM_GPR16 },
22966 },
22967 },
22968 [TEMPLATE_UMUL32] = {
22969 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Stefan Reinauer14e22772010-04-27 06:56:47 +000022970 .rhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022971 [0] = { REG_EAX, REGCM_GPR32 },
22972 [1] = { REG_UNSET, REGCM_GPR32 },
22973 },
22974 },
Eric Biederman530b5192003-07-01 10:05:30 +000022975 [TEMPLATE_DIV8] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022976 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022977 [0] = { REG_AL, REGCM_GPR8_LO },
22978 [1] = { REG_AH, REGCM_GPR8 },
22979 },
22980 .rhs = {
22981 [0] = { REG_AX, REGCM_GPR16 },
22982 [1] = { REG_UNSET, REGCM_GPR8_LO },
22983 },
22984 },
22985 [TEMPLATE_DIV16] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022986 .lhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022987 [0] = { REG_AX, REGCM_GPR16 },
22988 [1] = { REG_DX, REGCM_GPR16 },
22989 },
22990 .rhs = {
22991 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22992 [1] = { REG_UNSET, REGCM_GPR16 },
22993 },
22994 },
22995 [TEMPLATE_DIV32] = {
Stefan Reinauer14e22772010-04-27 06:56:47 +000022996 .lhs = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022997 [0] = { REG_EAX, REGCM_GPR32 },
22998 [1] = { REG_EDX, REGCM_GPR32 },
22999 },
23000 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023001 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023002 [1] = { REG_UNSET, REGCM_GPR32 },
23003 },
23004 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023005};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023006
Eric Biederman83b991a2003-10-11 06:20:25 +000023007static void fixup_branch(struct compile_state *state,
23008 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23009 struct triple *left, struct triple *right)
23010{
23011 struct triple *test;
23012 if (!left) {
23013 internal_error(state, branch, "no branch test?");
23014 }
23015 test = pre_triple(state, branch,
23016 cmp_op, cmp_type, left, right);
Stefan Reinauer14e22772010-04-27 06:56:47 +000023017 test->template_id = TEMPLATE_TEST32;
Eric Biederman83b991a2003-10-11 06:20:25 +000023018 if (cmp_op == OP_CMP) {
23019 test->template_id = TEMPLATE_CMP32_REG;
23020 if (get_imm32(test, &RHS(test, 1))) {
23021 test->template_id = TEMPLATE_CMP32_IMM;
23022 }
23023 }
23024 use_triple(RHS(test, 0), test);
23025 use_triple(RHS(test, 1), test);
23026 unuse_triple(RHS(branch, 0), branch);
23027 RHS(branch, 0) = test;
23028 branch->op = jmp_op;
23029 branch->template_id = TEMPLATE_JMP;
23030 use_triple(RHS(branch, 0), branch);
23031}
23032
Eric Biedermanb138ac82003-04-22 18:44:01 +000023033static void fixup_branches(struct compile_state *state,
23034 struct triple *cmp, struct triple *use, int jmp_op)
23035{
23036 struct triple_set *entry, *next;
23037 for(entry = use->use; entry; entry = next) {
23038 next = entry->next;
23039 if (entry->member->op == OP_COPY) {
23040 fixup_branches(state, cmp, entry->member, jmp_op);
23041 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023042 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023043 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023044 struct triple *left, *right;
23045 left = right = 0;
23046 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023047 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023048 right = RHS(cmp, 1);
23049 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023050 branch = entry->member;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023051 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023052 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023053 }
23054 }
23055}
23056
Stefan Reinauer14e22772010-04-27 06:56:47 +000023057static void bool_cmp(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023058 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23059{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023060 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023061 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023062
23063 /* Put a barrier up before the cmp which preceeds the
23064 * copy instruction. If a set actually occurs this gives
23065 * us a chance to move variables in registers out of the way.
23066 */
23067
23068 /* Modify the comparison operator */
23069 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023070 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023071 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023072 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023073 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023074 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023075 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023076 }
23077 /* Generate the instruction sequence that will transform the
23078 * result of the comparison into a logical value.
23079 */
Eric Biederman90089602004-05-28 14:11:54 +000023080 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023081 use_triple(ins, set);
23082 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023083
Eric Biederman90089602004-05-28 14:11:54 +000023084 convert = set;
23085 if (!equiv_types(ins->type, set->type)) {
23086 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23087 use_triple(set, convert);
23088 convert->template_id = TEMPLATE_COPY32_REG;
23089 }
23090
Eric Biedermanb138ac82003-04-22 18:44:01 +000023091 for(entry = ins->use; entry; entry = next) {
23092 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023093 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023094 continue;
23095 }
Eric Biederman90089602004-05-28 14:11:54 +000023096 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023097 }
Eric Biederman90089602004-05-28 14:11:54 +000023098 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023099}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023100
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023101struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23102{
23103 struct ins_template *template;
23104 struct reg_info result;
23105 int zlhs;
23106 if (ins->op == OP_PIECE) {
23107 index = ins->u.cval;
23108 ins = MISC(ins, 0);
23109 }
Eric Biederman90089602004-05-28 14:11:54 +000023110 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023111 if (triple_is_def(state, ins)) {
23112 zlhs = 1;
23113 }
23114 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023115 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023116 index, tops(ins->op));
23117 }
23118 switch(ins->op) {
23119 case OP_ASM:
23120 template = &ins->u.ainfo->tmpl;
23121 break;
23122 default:
23123 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023124 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023125 ins->template_id);
23126 }
23127 template = &templates[ins->template_id];
23128 break;
23129 }
23130 result = template->lhs[index];
23131 result.regcm = arch_regcm_normalize(state, result.regcm);
23132 if (result.reg != REG_UNNEEDED) {
23133 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23134 }
23135 if (result.regcm == 0) {
23136 internal_error(state, ins, "lhs %d regcm == 0", index);
23137 }
23138 return result;
23139}
23140
23141struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23142{
23143 struct reg_info result;
23144 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023145 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023146 (ins->op == OP_PIECE)) {
23147 internal_error(state, ins, "index %d out of range for %s\n",
23148 index, tops(ins->op));
23149 }
23150 switch(ins->op) {
23151 case OP_ASM:
23152 template = &ins->u.ainfo->tmpl;
23153 break;
Eric Biederman90089602004-05-28 14:11:54 +000023154 case OP_PHI:
23155 index = 0;
23156 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023157 default:
23158 if (ins->template_id > LAST_TEMPLATE) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023159 internal_error(state, ins, "bad template number %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023160 ins->template_id);
23161 }
23162 template = &templates[ins->template_id];
23163 break;
23164 }
23165 result = template->rhs[index];
23166 result.regcm = arch_regcm_normalize(state, result.regcm);
23167 if (result.regcm == 0) {
23168 internal_error(state, ins, "rhs %d regcm == 0", index);
23169 }
23170 return result;
23171}
23172
Eric Biederman530b5192003-07-01 10:05:30 +000023173static struct triple *mod_div(struct compile_state *state,
23174 struct triple *ins, int div_op, int index)
23175{
Bernhard Urbanf31abe32012-02-01 16:30:30 +010023176 struct triple *div, *piece1;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023177
Eric Biederman530b5192003-07-01 10:05:30 +000023178 /* Generate the appropriate division instruction */
23179 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23180 RHS(div, 0) = RHS(ins, 0);
23181 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023182 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023183 div->template_id = TEMPLATE_DIV32;
23184 use_triple(RHS(div, 0), div);
23185 use_triple(RHS(div, 1), div);
23186 use_triple(LHS(div, 0), div);
23187 use_triple(LHS(div, 1), div);
23188
Eric Biederman530b5192003-07-01 10:05:30 +000023189 /* Replate uses of ins with the appropriate piece of the div */
23190 propogate_use(state, ins, LHS(div, index));
23191 release_triple(state, ins);
23192
23193 /* Return the address of the next instruction */
23194 return piece1->next;
23195}
23196
Eric Biederman90089602004-05-28 14:11:54 +000023197static int noop_adecl(struct triple *adecl)
23198{
23199 struct triple_set *use;
23200 /* It's a noop if it doesn't specify stoorage */
23201 if (adecl->lhs == 0) {
23202 return 1;
23203 }
23204 /* Is the adecl used? If not it's a noop */
23205 for(use = adecl->use; use ; use = use->next) {
23206 if ((use->member->op != OP_PIECE) ||
23207 (MISC(use->member, 0) != adecl)) {
23208 return 0;
23209 }
23210 }
23211 return 1;
23212}
23213
23214static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23215{
23216 struct triple *mask, *nmask, *shift;
23217 struct triple *val, *val_mask, *val_shift;
23218 struct triple *targ, *targ_mask;
23219 struct triple *new;
23220 ulong_t the_mask, the_nmask;
23221
23222 targ = RHS(ins, 0);
23223 val = RHS(ins, 1);
23224
23225 /* Get constant for the mask value */
23226 the_mask = 1;
23227 the_mask <<= ins->u.bitfield.size;
23228 the_mask -= 1;
23229 the_mask <<= ins->u.bitfield.offset;
23230 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23231 mask->u.cval = the_mask;
23232
23233 /* Get the inverted mask value */
23234 the_nmask = ~the_mask;
23235 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23236 nmask->u.cval = the_nmask;
23237
23238 /* Get constant for the shift value */
23239 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23240 shift->u.cval = ins->u.bitfield.offset;
23241
23242 /* Shift and mask the source value */
23243 val_shift = val;
23244 if (shift->u.cval != 0) {
23245 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23246 use_triple(val, val_shift);
23247 use_triple(shift, val_shift);
23248 }
23249 val_mask = val_shift;
23250 if (is_signed(val->type)) {
23251 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23252 use_triple(val_shift, val_mask);
23253 use_triple(mask, val_mask);
23254 }
23255
23256 /* Mask the target value */
23257 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23258 use_triple(targ, targ_mask);
23259 use_triple(nmask, targ_mask);
23260
23261 /* Now combined them together */
23262 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23263 use_triple(targ_mask, new);
23264 use_triple(val_mask, new);
23265
23266 /* Move all of the users over to the new expression */
23267 propogate_use(state, ins, new);
23268
23269 /* Delete the original triple */
23270 release_triple(state, ins);
23271
23272 /* Restart the transformation at mask */
23273 return mask;
23274}
23275
23276static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23277{
23278 struct triple *mask, *shift;
23279 struct triple *val, *val_mask, *val_shift;
23280 ulong_t the_mask;
23281
23282 val = RHS(ins, 0);
23283
23284 /* Get constant for the mask value */
23285 the_mask = 1;
23286 the_mask <<= ins->u.bitfield.size;
23287 the_mask -= 1;
23288 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23289 mask->u.cval = the_mask;
23290
23291 /* Get constant for the right shift value */
23292 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23293 shift->u.cval = ins->u.bitfield.offset;
23294
23295 /* Shift arithmetic right, to correct the sign */
23296 val_shift = val;
23297 if (shift->u.cval != 0) {
23298 int op;
23299 if (ins->op == OP_SEXTRACT) {
23300 op = OP_SSR;
23301 } else {
23302 op = OP_USR;
23303 }
23304 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23305 use_triple(val, val_shift);
23306 use_triple(shift, val_shift);
23307 }
23308
23309 /* Finally mask the value */
23310 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23311 use_triple(val_shift, val_mask);
23312 use_triple(mask, val_mask);
23313
23314 /* Move all of the users over to the new expression */
23315 propogate_use(state, ins, val_mask);
23316
23317 /* Release the original instruction */
23318 release_triple(state, ins);
23319
23320 return mask;
23321
23322}
23323
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023324static struct triple *transform_to_arch_instruction(
23325 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023326{
23327 /* Transform from generic 3 address instructions
23328 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023329 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023330 * Copies are inserted to preserve the register flexibility
23331 * of 3 address instructions.
23332 */
Eric Biederman90089602004-05-28 14:11:54 +000023333 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023334 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023335 next = ins->next;
23336 switch(ins->op) {
23337 case OP_INTCONST:
23338 ins->template_id = TEMPLATE_INTCONST32;
23339 if (ins->u.cval < 256) {
23340 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023341 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023342 break;
23343 case OP_ADDRCONST:
23344 ins->template_id = TEMPLATE_INTCONST32;
23345 break;
Eric Biederman90089602004-05-28 14:11:54 +000023346 case OP_UNKNOWNVAL:
23347 ins->template_id = TEMPLATE_UNKNOWNVAL;
23348 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023349 case OP_NOOP:
23350 case OP_SDECL:
23351 case OP_BLOBCONST:
23352 case OP_LABEL:
23353 ins->template_id = TEMPLATE_NOP;
23354 break;
23355 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023356 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023357 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023358 value = RHS(ins, 0);
23359 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023360 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023361 }
Eric Biederman90089602004-05-28 14:11:54 +000023362 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023363 ins->template_id = TEMPLATE_COPY_IMM16;
23364 }
Eric Biederman90089602004-05-28 14:11:54 +000023365 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023366 ins->template_id = TEMPLATE_COPY_IMM32;
23367 }
Eric Biederman90089602004-05-28 14:11:54 +000023368 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023369 internal_error(state, ins, "bad constant passed to copy");
23370 }
Eric Biederman90089602004-05-28 14:11:54 +000023371 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023372 ins->template_id = TEMPLATE_COPY8_REG;
23373 }
Eric Biederman90089602004-05-28 14:11:54 +000023374 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023375 ins->template_id = TEMPLATE_COPY16_REG;
23376 }
Eric Biederman90089602004-05-28 14:11:54 +000023377 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023378 ins->template_id = TEMPLATE_COPY32_REG;
23379 }
23380 else {
23381 internal_error(state, ins, "bad type passed to copy");
23382 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023383 break;
23384 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023385 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023386 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023387 ins->template_id = TEMPLATE_PHI8;
23388 }
Eric Biederman90089602004-05-28 14:11:54 +000023389 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023390 ins->template_id = TEMPLATE_PHI16;
23391 }
Eric Biederman90089602004-05-28 14:11:54 +000023392 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023393 ins->template_id = TEMPLATE_PHI32;
23394 }
23395 else {
23396 internal_error(state, ins, "bad type passed to phi");
23397 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023398 break;
Eric Biederman90089602004-05-28 14:11:54 +000023399 case OP_ADECL:
23400 /* Adecls should always be treated as dead code and
23401 * removed. If we are not optimizing they may linger.
23402 */
23403 if (!noop_adecl(ins)) {
23404 internal_error(state, ins, "adecl remains?");
23405 }
23406 ins->template_id = TEMPLATE_NOP;
23407 next = after_lhs(state, ins);
23408 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023409 case OP_STORE:
23410 switch(ins->type->type & TYPE_MASK) {
23411 case TYPE_CHAR: case TYPE_UCHAR:
23412 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023413 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023414 case TYPE_SHORT: case TYPE_USHORT:
23415 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023416 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023417 case TYPE_INT: case TYPE_UINT:
23418 case TYPE_LONG: case TYPE_ULONG:
23419 case TYPE_POINTER:
23420 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023421 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023422 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023423 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023424 break;
23425 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023426 break;
23427 case OP_LOAD:
23428 switch(ins->type->type & TYPE_MASK) {
23429 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023430 case TYPE_SHORT: case TYPE_USHORT:
23431 case TYPE_INT: case TYPE_UINT:
23432 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023433 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023434 break;
23435 default:
23436 internal_error(state, ins, "unknown type in load");
23437 break;
23438 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023439 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023440 break;
23441 case OP_ADD:
23442 case OP_SUB:
23443 case OP_AND:
23444 case OP_XOR:
23445 case OP_OR:
23446 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023447 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023448 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023449 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023450 }
23451 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023452 case OP_SDIVT:
23453 case OP_UDIVT:
23454 ins->template_id = TEMPLATE_DIV32;
23455 next = after_lhs(state, ins);
23456 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023457 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023458 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023459 break;
23460 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023461 next = mod_div(state, ins, OP_UDIVT, 0);
23462 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023463 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023464 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023465 break;
23466 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023467 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023468 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023469 case OP_SMOD:
23470 next = mod_div(state, ins, OP_SDIVT, 1);
23471 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023472 case OP_SL:
23473 case OP_SSR:
23474 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023475 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023476 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023477 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023478 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23479 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023480 }
23481 break;
23482 case OP_INVERT:
23483 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023484 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023485 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000023486 case OP_EQ:
23487 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023488 break;
23489 case OP_NOTEQ:
23490 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23491 break;
23492 case OP_SLESS:
23493 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23494 break;
23495 case OP_ULESS:
23496 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23497 break;
23498 case OP_SMORE:
23499 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23500 break;
23501 case OP_UMORE:
23502 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23503 break;
23504 case OP_SLESSEQ:
23505 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23506 break;
23507 case OP_ULESSEQ:
23508 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23509 break;
23510 case OP_SMOREEQ:
23511 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23512 break;
23513 case OP_UMOREEQ:
23514 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23515 break;
23516 case OP_LTRUE:
23517 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23518 break;
23519 case OP_LFALSE:
23520 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23521 break;
23522 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023523 ins->op = OP_JMP;
23524 ins->template_id = TEMPLATE_NOP;
23525 break;
23526 case OP_CBRANCH:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023527 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
Eric Biederman5ade04a2003-10-22 04:03:46 +000023528 RHS(ins, 0)->type, RHS(ins, 0), 0);
23529 break;
23530 case OP_CALL:
23531 ins->template_id = TEMPLATE_NOP;
23532 break;
23533 case OP_RET:
23534 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023535 break;
23536 case OP_INB:
23537 case OP_INW:
23538 case OP_INL:
23539 switch(ins->op) {
23540 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23541 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23542 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23543 }
23544 if (get_imm8(ins, &RHS(ins, 0))) {
23545 ins->template_id += 1;
23546 }
23547 break;
23548 case OP_OUTB:
23549 case OP_OUTW:
23550 case OP_OUTL:
23551 switch(ins->op) {
23552 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23553 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23554 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23555 }
23556 if (get_imm8(ins, &RHS(ins, 1))) {
23557 ins->template_id += 1;
23558 }
23559 break;
23560 case OP_BSF:
23561 case OP_BSR:
23562 ins->template_id = TEMPLATE_BSF;
23563 break;
23564 case OP_RDMSR:
23565 ins->template_id = TEMPLATE_RDMSR;
23566 next = after_lhs(state, ins);
23567 break;
23568 case OP_WRMSR:
23569 ins->template_id = TEMPLATE_WRMSR;
23570 break;
23571 case OP_HLT:
23572 ins->template_id = TEMPLATE_NOP;
23573 break;
23574 case OP_ASM:
23575 ins->template_id = TEMPLATE_NOP;
23576 next = after_lhs(state, ins);
23577 break;
23578 /* Already transformed instructions */
23579 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023580 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023581 break;
23582 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023583 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023584 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023585 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023586 }
23587 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023588 case OP_JMP:
23589 ins->template_id = TEMPLATE_NOP;
23590 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023591 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23592 case OP_JMP_SLESS: case OP_JMP_ULESS:
23593 case OP_JMP_SMORE: case OP_JMP_UMORE:
23594 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23595 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23596 ins->template_id = TEMPLATE_JMP;
23597 break;
23598 case OP_SET_EQ: case OP_SET_NOTEQ:
23599 case OP_SET_SLESS: case OP_SET_ULESS:
23600 case OP_SET_SMORE: case OP_SET_UMORE:
23601 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23602 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23603 ins->template_id = TEMPLATE_SET;
23604 break;
Eric Biederman90089602004-05-28 14:11:54 +000023605 case OP_DEPOSIT:
23606 next = x86_deposit(state, ins);
23607 break;
23608 case OP_SEXTRACT:
23609 case OP_UEXTRACT:
23610 next = x86_extract(state, ins);
23611 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023612 /* Unhandled instructions */
23613 case OP_PIECE:
23614 default:
Eric Biederman90089602004-05-28 14:11:54 +000023615 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023616 ins->op, tops(ins->op));
23617 break;
23618 }
23619 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023620}
23621
Eric Biederman530b5192003-07-01 10:05:30 +000023622static long next_label(struct compile_state *state)
23623{
Eric Biederman90089602004-05-28 14:11:54 +000023624 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023625 return ++label_counter;
23626}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023627static void generate_local_labels(struct compile_state *state)
23628{
23629 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023630 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023631 label = first;
23632 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000023633 if ((label->op == OP_LABEL) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000023634 (label->op == OP_SDECL)) {
23635 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023636 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023637 } else {
23638 label->u.cval = 0;
23639 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023640
Eric Biedermanb138ac82003-04-22 18:44:01 +000023641 }
23642 label = label->next;
23643 } while(label != first);
23644}
23645
Stefan Reinauer14e22772010-04-27 06:56:47 +000023646static int check_reg(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023647 struct triple *triple, int classes)
23648{
23649 unsigned mask;
23650 int reg;
23651 reg = ID_REG(triple->id);
23652 if (reg == REG_UNSET) {
23653 internal_error(state, triple, "register not set");
23654 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023655 mask = arch_reg_regcm(state, reg);
23656 if (!(classes & mask)) {
23657 internal_error(state, triple, "reg %d in wrong class",
23658 reg);
23659 }
23660 return reg;
23661}
23662
Eric Biederman90089602004-05-28 14:11:54 +000023663
Eric Biederman530b5192003-07-01 10:05:30 +000023664#if REG_XMM7 != 44
23665#error "Registers have renumberd fix arch_reg_str"
23666#endif
Eric Biederman90089602004-05-28 14:11:54 +000023667static const char *arch_regs[] = {
23668 "%unset",
23669 "%unneeded",
23670 "%eflags",
23671 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23672 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23673 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23674 "%edx:%eax",
23675 "%dx:%ax",
23676 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023677 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
Eric Biederman90089602004-05-28 14:11:54 +000023678 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23679};
23680static const char *arch_reg_str(int reg)
23681{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023682 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23683 reg = 0;
23684 }
Eric Biederman90089602004-05-28 14:11:54 +000023685 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023686}
23687
23688static const char *reg(struct compile_state *state, struct triple *triple,
23689 int classes)
23690{
23691 int reg;
23692 reg = check_reg(state, triple, classes);
23693 return arch_reg_str(reg);
23694}
23695
Eric Biederman90089602004-05-28 14:11:54 +000023696static int arch_reg_size(int reg)
23697{
23698 int size;
23699 size = 0;
23700 if (reg == REG_EFLAGS) {
23701 size = 32;
23702 }
23703 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23704 size = 8;
23705 }
23706 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23707 size = 16;
23708 }
23709 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23710 size = 32;
23711 }
23712 else if (reg == REG_EDXEAX) {
23713 size = 64;
23714 }
23715 else if (reg == REG_DXAX) {
23716 size = 32;
23717 }
23718 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23719 size = 64;
23720 }
23721 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23722 size = 128;
23723 }
23724 return size;
23725}
23726
23727static int reg_size(struct compile_state *state, struct triple *ins)
23728{
23729 int reg;
23730 reg = ID_REG(ins->id);
23731 if (reg == REG_UNSET) {
23732 internal_error(state, ins, "register not set");
23733 }
23734 return arch_reg_size(reg);
23735}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023736
Eric Biederman90089602004-05-28 14:11:54 +000023737
23738
Eric Biedermanb138ac82003-04-22 18:44:01 +000023739const char *type_suffix(struct compile_state *state, struct type *type)
23740{
23741 const char *suffix;
23742 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023743 case SIZEOF_I8: suffix = "b"; break;
23744 case SIZEOF_I16: suffix = "w"; break;
23745 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023746 default:
23747 internal_error(state, 0, "unknown suffix");
23748 suffix = 0;
23749 break;
23750 }
23751 return suffix;
23752}
23753
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023754static void print_const_val(
23755 struct compile_state *state, struct triple *ins, FILE *fp)
23756{
23757 switch(ins->op) {
23758 case OP_INTCONST:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023759 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023760 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023761 break;
23762 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023763 if ((MISC(ins, 0)->op != OP_SDECL) &&
23764 (MISC(ins, 0)->op != OP_LABEL))
23765 {
Eric Biederman830c9882003-07-04 00:27:33 +000023766 internal_error(state, ins, "bad base for addrconst");
23767 }
23768 if (MISC(ins, 0)->u.cval <= 0) {
23769 internal_error(state, ins, "unlabeled constant");
23770 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023771 fprintf(fp, " $L%s%lu+%lu ",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023772 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023773 (unsigned long)(MISC(ins, 0)->u.cval),
23774 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023775 break;
23776 default:
23777 internal_error(state, ins, "unknown constant type");
23778 break;
23779 }
23780}
23781
Eric Biederman530b5192003-07-01 10:05:30 +000023782static void print_const(struct compile_state *state,
23783 struct triple *ins, FILE *fp)
23784{
23785 switch(ins->op) {
23786 case OP_INTCONST:
23787 switch(ins->type->type & TYPE_MASK) {
23788 case TYPE_CHAR:
23789 case TYPE_UCHAR:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023790 fprintf(fp, ".byte 0x%02lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023791 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023792 break;
23793 case TYPE_SHORT:
23794 case TYPE_USHORT:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023795 fprintf(fp, ".short 0x%04lx\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023796 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023797 break;
23798 case TYPE_INT:
23799 case TYPE_UINT:
23800 case TYPE_LONG:
23801 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023802 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000023803 fprintf(fp, ".int %lu\n",
Eric Biederman83b991a2003-10-11 06:20:25 +000023804 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023805 break;
23806 default:
Eric Biederman90089602004-05-28 14:11:54 +000023807 fprintf(state->errout, "type: ");
23808 name_of(state->errout, ins->type);
23809 fprintf(state->errout, "\n");
23810 internal_error(state, ins, "Unknown constant type. Val: %lu",
23811 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023812 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000023813
Eric Biederman530b5192003-07-01 10:05:30 +000023814 break;
23815 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023816 if ((MISC(ins, 0)->op != OP_SDECL) &&
23817 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023818 internal_error(state, ins, "bad base for addrconst");
23819 }
23820 if (MISC(ins, 0)->u.cval <= 0) {
23821 internal_error(state, ins, "unlabeled constant");
23822 }
23823 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023824 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023825 (unsigned long)(MISC(ins, 0)->u.cval),
23826 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023827 break;
23828 case OP_BLOBCONST:
23829 {
23830 unsigned char *blob;
23831 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023832 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023833 blob = ins->u.blob;
23834 for(i = 0; i < size; i++) {
23835 fprintf(fp, ".byte 0x%02x\n",
23836 blob[i]);
23837 }
23838 break;
23839 }
23840 default:
23841 internal_error(state, ins, "Unknown constant type");
23842 break;
23843 }
23844}
23845
23846#define TEXT_SECTION ".rom.text"
23847#define DATA_SECTION ".rom.data"
23848
23849static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023850 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023851{
Eric Biederman90089602004-05-28 14:11:54 +000023852 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023853 long ref;
23854 ref = next_label(state);
23855 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023856 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023857 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023858 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023859 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23860 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023861 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023862 }
Eric Biederman530b5192003-07-01 10:05:30 +000023863 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23864 return ref;
23865}
23866
Eric Biederman90089602004-05-28 14:11:54 +000023867static long get_mask_pool_ref(
23868 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23869{
23870 long ref;
23871 if (mask == 0xff) {
23872 ref = 1;
23873 }
23874 else if (mask == 0xffff) {
23875 ref = 2;
23876 }
23877 else {
23878 ref = 0;
23879 internal_error(state, ins, "unhandled mask value");
23880 }
23881 return ref;
23882}
23883
Eric Biedermanb138ac82003-04-22 18:44:01 +000023884static void print_binary_op(struct compile_state *state,
Stefan Reinauer14e22772010-04-27 06:56:47 +000023885 const char *op, struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023886{
23887 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023888 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023889 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023890 internal_error(state, ins, "invalid register assignment");
23891 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023892 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023893 fprintf(fp, "\t%s ", op);
23894 print_const_val(state, RHS(ins, 1), fp);
23895 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023896 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023897 }
23898 else {
23899 unsigned lmask, rmask;
23900 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023901 lreg = check_reg(state, RHS(ins, 0), mask);
23902 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023903 lmask = arch_reg_regcm(state, lreg);
23904 rmask = arch_reg_regcm(state, rreg);
23905 mask = lmask & rmask;
23906 fprintf(fp, "\t%s %s, %s\n",
23907 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023908 reg(state, RHS(ins, 1), mask),
23909 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023910 }
23911}
Stefan Reinauer14e22772010-04-27 06:56:47 +000023912static void print_unary_op(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000023913 const char *op, struct triple *ins, FILE *fp)
23914{
23915 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023916 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023917 fprintf(fp, "\t%s %s\n",
23918 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023919 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023920}
23921
23922static void print_op_shift(struct compile_state *state,
23923 const char *op, struct triple *ins, FILE *fp)
23924{
23925 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023926 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023927 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023928 internal_error(state, ins, "invalid register assignment");
23929 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023930 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023931 fprintf(fp, "\t%s ", op);
23932 print_const_val(state, RHS(ins, 1), fp);
23933 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023934 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023935 }
23936 else {
23937 fprintf(fp, "\t%s %s, %s\n",
23938 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023939 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023940 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023941 }
23942}
23943
23944static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23945{
23946 const char *op;
23947 int mask;
23948 int dreg;
23949 mask = 0;
23950 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023951 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023952 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23953 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23954 default:
23955 internal_error(state, ins, "not an in operation");
23956 op = 0;
23957 break;
23958 }
23959 dreg = check_reg(state, ins, mask);
23960 if (!reg_is_reg(state, dreg, REG_EAX)) {
23961 internal_error(state, ins, "dst != %%eax");
23962 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023963 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023964 fprintf(fp, "\t%s ", op);
23965 print_const_val(state, RHS(ins, 0), fp);
23966 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023967 reg(state, ins, mask));
23968 }
23969 else {
23970 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023971 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023972 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23973 internal_error(state, ins, "src != %%dx");
23974 }
23975 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000023976 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023977 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023978 reg(state, ins, mask));
23979 }
23980}
23981
23982static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23983{
23984 const char *op;
23985 int mask;
23986 int lreg;
23987 mask = 0;
23988 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023989 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023990 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23991 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23992 default:
23993 internal_error(state, ins, "not an out operation");
23994 op = 0;
23995 break;
23996 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023997 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023998 if (!reg_is_reg(state, lreg, REG_EAX)) {
23999 internal_error(state, ins, "src != %%eax");
24000 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024001 if (is_const(RHS(ins, 1))) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024002 fprintf(fp, "\t%s %s,",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024003 op, reg(state, RHS(ins, 0), mask));
24004 print_const_val(state, RHS(ins, 1), fp);
24005 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024006 }
24007 else {
24008 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024009 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024010 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24011 internal_error(state, ins, "dst != %%dx");
24012 }
24013 fprintf(fp, "\t%s %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024014 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024015 reg(state, RHS(ins, 0), mask),
24016 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024017 }
24018}
24019
24020static void print_op_move(struct compile_state *state,
24021 struct triple *ins, FILE *fp)
24022{
24023 /* op_move is complex because there are many types
24024 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024025 * Because OP_COPY will be introduced in arbitrary locations
24026 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024027 * OP_CONVERT can change the flags and it is the only operation
24028 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024029 */
24030 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24031 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024032 if (state->arch->features & X86_NOOP_COPY) {
24033 omit_copy = 0;
24034 }
24035 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024036 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024037 dst = ins;
24038 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024039 else {
24040 internal_error(state, ins, "unknown move operation");
24041 src = dst = 0;
24042 }
Eric Biederman90089602004-05-28 14:11:54 +000024043 if (reg_size(state, dst) < size_of(state, dst->type)) {
24044 internal_error(state, ins, "Invalid destination register");
24045 }
24046 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24047 fprintf(state->errout, "src type: ");
24048 name_of(state->errout, src->type);
24049 fprintf(state->errout, "\n");
24050 fprintf(state->errout, "dst type: ");
24051 name_of(state->errout, dst->type);
24052 fprintf(state->errout, "\n");
24053 internal_error(state, ins, "Type mismatch for OP_COPY");
24054 }
24055
Eric Biederman0babc1c2003-05-09 02:39:00 +000024056 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024057 int src_reg, dst_reg;
24058 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024059 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024060 dst_reg = ID_REG(dst->id);
24061 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024062 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024063 /* If the class is the same just move the register */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024064 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024065 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024066 if ((src_reg != dst_reg) || !omit_copy) {
24067 fprintf(fp, "\tmov %s, %s\n",
24068 reg(state, src, src_regcm),
24069 reg(state, dst, dst_regcm));
24070 }
24071 }
24072 /* Move 32bit to 16bit */
24073 else if ((src_regcm & REGCM_GPR32) &&
24074 (dst_regcm & REGCM_GPR16)) {
24075 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24076 if ((src_reg != dst_reg) || !omit_copy) {
24077 fprintf(fp, "\tmovw %s, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024078 arch_reg_str(src_reg),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024079 arch_reg_str(dst_reg));
24080 }
24081 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024082 /* Move from 32bit gprs to 16bit gprs */
24083 else if ((src_regcm & REGCM_GPR32) &&
24084 (dst_regcm & REGCM_GPR16)) {
24085 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24086 if ((src_reg != dst_reg) || !omit_copy) {
24087 fprintf(fp, "\tmov %s, %s\n",
24088 arch_reg_str(src_reg),
24089 arch_reg_str(dst_reg));
24090 }
24091 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024092 /* Move 32bit to 8bit */
24093 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024094 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024095 {
24096 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24097 if ((src_reg != dst_reg) || !omit_copy) {
24098 fprintf(fp, "\tmovb %s, %s\n",
24099 arch_reg_str(src_reg),
24100 arch_reg_str(dst_reg));
24101 }
24102 }
24103 /* Move 16bit to 8bit */
24104 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024105 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024106 {
24107 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24108 if ((src_reg != dst_reg) || !omit_copy) {
24109 fprintf(fp, "\tmovb %s, %s\n",
24110 arch_reg_str(src_reg),
24111 arch_reg_str(dst_reg));
24112 }
24113 }
24114 /* Move 8/16bit to 16/32bit */
Stefan Reinauer14e22772010-04-27 06:56:47 +000024115 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024116 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024117 const char *op;
24118 op = is_signed(src->type)? "movsx": "movzx";
24119 fprintf(fp, "\t%s %s, %s\n",
24120 op,
24121 reg(state, src, src_regcm),
24122 reg(state, dst, dst_regcm));
24123 }
24124 /* Move between sse registers */
24125 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24126 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024127 fprintf(fp, "\tmovdqa %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 between mmx registers */
24133 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024134 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024135 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024136 reg(state, src, src_regcm),
24137 reg(state, dst, dst_regcm));
24138 }
24139 }
Eric Biederman530b5192003-07-01 10:05:30 +000024140 /* Move from sse to mmx registers */
24141 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24142 fprintf(fp, "\tmovdq2q %s, %s\n",
24143 reg(state, src, src_regcm),
24144 reg(state, dst, dst_regcm));
24145 }
24146 /* Move from mmx to sse registers */
24147 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24148 fprintf(fp, "\tmovq2dq %s, %s\n",
24149 reg(state, src, src_regcm),
24150 reg(state, dst, dst_regcm));
24151 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024152 /* Move between 32bit gprs & mmx/sse registers */
24153 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24154 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24155 fprintf(fp, "\tmovd %s, %s\n",
24156 reg(state, src, src_regcm),
24157 reg(state, dst, dst_regcm));
24158 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024159 /* Move from 16bit gprs & mmx/sse registers */
24160 else if ((src_regcm & REGCM_GPR16) &&
24161 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24162 const char *op;
24163 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024164 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024165 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24166 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24167 op,
24168 arch_reg_str(src_reg),
24169 arch_reg_str(mid_reg),
24170 arch_reg_str(mid_reg),
24171 arch_reg_str(dst_reg));
24172 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024173 /* Move from mmx/sse registers to 16bit gprs */
24174 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24175 (dst_regcm & REGCM_GPR16)) {
24176 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24177 fprintf(fp, "\tmovd %s, %s\n",
24178 arch_reg_str(src_reg),
24179 arch_reg_str(dst_reg));
24180 }
Eric Biederman530b5192003-07-01 10:05:30 +000024181 /* Move from gpr to 64bit dividend */
24182 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24183 (dst_regcm & REGCM_DIVIDEND64)) {
24184 const char *extend;
24185 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24186 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024187 arch_reg_str(src_reg),
Eric Biederman530b5192003-07-01 10:05:30 +000024188 extend);
24189 }
24190 /* Move from 64bit gpr to gpr */
24191 else if ((src_regcm & REGCM_DIVIDEND64) &&
24192 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24193 if (dst_regcm & REGCM_GPR32) {
24194 src_reg = REG_EAX;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024195 }
Eric Biederman530b5192003-07-01 10:05:30 +000024196 else if (dst_regcm & REGCM_GPR16) {
24197 src_reg = REG_AX;
24198 }
24199 else if (dst_regcm & REGCM_GPR8_LO) {
24200 src_reg = REG_AL;
24201 }
24202 fprintf(fp, "\tmov %s, %s\n",
24203 arch_reg_str(src_reg),
24204 arch_reg_str(dst_reg));
24205 }
24206 /* Move from mmx/sse registers to 64bit gpr */
24207 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24208 (dst_regcm & REGCM_DIVIDEND64)) {
24209 const char *extend;
24210 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24211 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24212 arch_reg_str(src_reg),
24213 extend);
24214 }
24215 /* Move from 64bit gpr to mmx/sse register */
24216 else if ((src_regcm & REGCM_DIVIDEND64) &&
24217 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24218 fprintf(fp, "\tmovd %%eax, %s\n",
24219 arch_reg_str(dst_reg));
24220 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024221#if X86_4_8BIT_GPRS
24222 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024223 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024224 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24225 const char *op;
24226 int mid_reg;
24227 op = is_signed(src->type)? "movsx":"movzx";
24228 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24229 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24230 op,
24231 reg(state, src, src_regcm),
24232 arch_reg_str(mid_reg),
24233 arch_reg_str(mid_reg),
24234 reg(state, dst, dst_regcm));
24235 }
24236 /* Move from mmx/sse registers and 8bit gprs */
24237 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024238 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024239 int mid_reg;
24240 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24241 fprintf(fp, "\tmovd %s, %s\n",
24242 reg(state, src, src_regcm),
24243 arch_reg_str(mid_reg));
24244 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024245 /* Move from 32bit gprs to 8bit gprs */
24246 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024247 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024248 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24249 if ((src_reg != dst_reg) || !omit_copy) {
24250 fprintf(fp, "\tmov %s, %s\n",
24251 arch_reg_str(src_reg),
24252 arch_reg_str(dst_reg));
24253 }
24254 }
24255 /* Move from 16bit gprs to 8bit gprs */
24256 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024257 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024258 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24259 if ((src_reg != dst_reg) || !omit_copy) {
24260 fprintf(fp, "\tmov %s, %s\n",
24261 arch_reg_str(src_reg),
24262 arch_reg_str(dst_reg));
24263 }
24264 }
24265#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024266 /* Move from %eax:%edx to %eax:%edx */
24267 else if ((src_regcm & REGCM_DIVIDEND64) &&
24268 (dst_regcm & REGCM_DIVIDEND64) &&
24269 (src_reg == dst_reg)) {
24270 if (!omit_copy) {
24271 fprintf(fp, "\t/*mov %s, %s*/\n",
24272 arch_reg_str(src_reg),
24273 arch_reg_str(dst_reg));
24274 }
24275 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024276 else {
Eric Biederman90089602004-05-28 14:11:54 +000024277 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24278 internal_error(state, ins, "attempt to copy from %%eflags!");
24279 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024280 internal_error(state, ins, "unknown copy type");
24281 }
24282 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024283 else {
Eric Biederman90089602004-05-28 14:11:54 +000024284 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024285 int dst_reg;
24286 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024287 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024288 dst_reg = ID_REG(dst->id);
24289 dst_regcm = arch_reg_regcm(state, dst_reg);
24290 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24291 fprintf(fp, "\tmov ");
24292 print_const_val(state, src, fp);
24293 fprintf(fp, ", %s\n",
24294 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24295 }
24296 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024297 if (dst_size > SIZEOF_I32) {
24298 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024299 }
24300 fprintf(fp, "\tmov $0, %%edx\n");
24301 fprintf(fp, "\tmov ");
24302 print_const_val(state, src, fp);
24303 fprintf(fp, ", %%eax\n");
24304 }
24305 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024306 if (dst_size > SIZEOF_I16) {
24307 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024308 }
24309 fprintf(fp, "\tmov $0, %%dx\n");
24310 fprintf(fp, "\tmov ");
24311 print_const_val(state, src, fp);
24312 fprintf(fp, ", %%ax");
24313 }
24314 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24315 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024316 if (dst_size > SIZEOF_I32) {
24317 internal_error(state, ins, "%d bit constant...", dst_size);
24318 }
24319 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024320 fprintf(fp, "\tmovd L%s%lu, %s\n",
24321 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024322 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24323 }
24324 else {
24325 internal_error(state, ins, "unknown copy immediate type");
24326 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024327 }
Eric Biederman90089602004-05-28 14:11:54 +000024328 /* Leave now if this is not a type conversion */
24329 if (ins->op != OP_CONVERT) {
24330 return;
24331 }
24332 /* Now make certain I have not logically overflowed the destination */
24333 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24334 (size_of(state, dst->type) < reg_size(state, dst)))
24335 {
24336 unsigned long mask;
24337 int dst_reg;
24338 int dst_regcm;
24339 if (size_of(state, dst->type) >= 32) {
24340 fprintf(state->errout, "dst type: ");
24341 name_of(state->errout, dst->type);
24342 fprintf(state->errout, "\n");
24343 internal_error(state, dst, "unhandled dst type size");
24344 }
24345 mask = 1;
24346 mask <<= size_of(state, dst->type);
24347 mask -= 1;
24348
24349 dst_reg = ID_REG(dst->id);
24350 dst_regcm = arch_reg_regcm(state, dst_reg);
24351
24352 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24353 fprintf(fp, "\tand $0x%lx, %s\n",
24354 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24355 }
24356 else if (dst_regcm & REGCM_MMX) {
24357 long ref;
24358 ref = get_mask_pool_ref(state, dst, mask, fp);
24359 fprintf(fp, "\tpand L%s%lu, %s\n",
24360 state->compiler->label_prefix, ref,
24361 reg(state, dst, REGCM_MMX));
24362 }
24363 else if (dst_regcm & REGCM_XMM) {
24364 long ref;
24365 ref = get_mask_pool_ref(state, dst, mask, fp);
24366 fprintf(fp, "\tpand L%s%lu, %s\n",
24367 state->compiler->label_prefix, ref,
24368 reg(state, dst, REGCM_XMM));
24369 }
24370 else {
24371 fprintf(state->errout, "dst type: ");
24372 name_of(state->errout, dst->type);
24373 fprintf(state->errout, "\n");
24374 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24375 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24376 }
24377 }
24378 /* Make certain I am properly sign extended */
24379 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24380 (is_signed(src->type)))
24381 {
Bernhard Urbanf31abe32012-02-01 16:30:30 +010024382 int reg_bits, shift_bits;
Eric Biederman90089602004-05-28 14:11:54 +000024383 int dst_reg;
24384 int dst_regcm;
24385
Eric Biederman90089602004-05-28 14:11:54 +000024386 reg_bits = reg_size(state, dst);
24387 if (reg_bits > 32) {
24388 reg_bits = 32;
24389 }
24390 shift_bits = reg_bits - size_of(state, src->type);
24391 dst_reg = ID_REG(dst->id);
24392 dst_regcm = arch_reg_regcm(state, dst_reg);
24393
24394 if (shift_bits < 0) {
24395 internal_error(state, dst, "negative shift?");
24396 }
24397
24398 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024399 fprintf(fp, "\tshl $%d, %s\n",
24400 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024401 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024402 fprintf(fp, "\tsar $%d, %s\n",
24403 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024404 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24405 }
24406 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24407 fprintf(fp, "\tpslld $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024408 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024409 reg(state, dst, REGCM_MMX | REGCM_XMM));
24410 fprintf(fp, "\tpsrad $%d, %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024411 shift_bits,
Eric Biederman90089602004-05-28 14:11:54 +000024412 reg(state, dst, REGCM_MMX | REGCM_XMM));
24413 }
24414 else {
24415 fprintf(state->errout, "dst type: ");
24416 name_of(state->errout, dst->type);
24417 fprintf(state->errout, "\n");
24418 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24419 internal_error(state, dst, "failed to signed extend value");
24420 }
24421 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024422}
24423
24424static void print_op_load(struct compile_state *state,
24425 struct triple *ins, FILE *fp)
24426{
24427 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024428 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024429 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024430 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024431 if (is_const(src) || is_const(dst)) {
24432 internal_error(state, ins, "unknown load operation");
24433 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024434 switch(ins->type->type & TYPE_MASK) {
24435 case TYPE_CHAR: op = "movsbl"; break;
24436 case TYPE_UCHAR: op = "movzbl"; break;
24437 case TYPE_SHORT: op = "movswl"; break;
24438 case TYPE_USHORT: op = "movzwl"; break;
24439 case TYPE_INT: case TYPE_UINT:
24440 case TYPE_LONG: case TYPE_ULONG:
24441 case TYPE_POINTER:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024442 op = "movl";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024443 break;
24444 default:
24445 internal_error(state, ins, "unknown type in load");
24446 op = "<invalid opcode>";
24447 break;
24448 }
24449 fprintf(fp, "\t%s (%s), %s\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024450 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024451 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024452 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024453}
24454
24455
24456static void print_op_store(struct compile_state *state,
24457 struct triple *ins, FILE *fp)
24458{
24459 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024460 dst = RHS(ins, 0);
24461 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024462 if (is_const(src) && (src->op == OP_INTCONST)) {
24463 long_t value;
24464 value = (long_t)(src->u.cval);
24465 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24466 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024467 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024468 reg(state, dst, REGCM_GPR32));
24469 }
24470 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24471 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24472 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024473 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024474 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024475 }
24476 else {
24477 if (is_const(src) || is_const(dst)) {
24478 internal_error(state, ins, "unknown store operation");
24479 }
24480 fprintf(fp, "\tmov%s %s, (%s)\n",
24481 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024482 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024483 reg(state, dst, REGCM_GPR32));
24484 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024485
24486
Eric Biedermanb138ac82003-04-22 18:44:01 +000024487}
24488
24489static void print_op_smul(struct compile_state *state,
24490 struct triple *ins, FILE *fp)
24491{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024492 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024493 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024494 reg(state, RHS(ins, 1), REGCM_GPR32),
24495 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024496 }
24497 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024498 fprintf(fp, "\timul ");
24499 print_const_val(state, RHS(ins, 1), fp);
24500 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024501 }
24502}
24503
24504static void print_op_cmp(struct compile_state *state,
24505 struct triple *ins, FILE *fp)
24506{
24507 unsigned mask;
24508 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024509 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024510 dreg = check_reg(state, ins, REGCM_FLAGS);
24511 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24512 internal_error(state, ins, "bad dest register for cmp");
24513 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024514 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024515 fprintf(fp, "\tcmp ");
24516 print_const_val(state, RHS(ins, 1), fp);
24517 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024518 }
24519 else {
24520 unsigned lmask, rmask;
24521 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024522 lreg = check_reg(state, RHS(ins, 0), mask);
24523 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024524 lmask = arch_reg_regcm(state, lreg);
24525 rmask = arch_reg_regcm(state, rreg);
24526 mask = lmask & rmask;
24527 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024528 reg(state, RHS(ins, 1), mask),
24529 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024530 }
24531}
24532
24533static void print_op_test(struct compile_state *state,
24534 struct triple *ins, FILE *fp)
24535{
24536 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024537 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024538 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024539 reg(state, RHS(ins, 0), mask),
24540 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024541}
24542
24543static void print_op_branch(struct compile_state *state,
24544 struct triple *branch, FILE *fp)
24545{
24546 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024547 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024548 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024549 internal_error(state, branch, "jmp with condition?");
24550 }
24551 bop = "jmp";
24552 }
24553 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024554 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024555 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024556 internal_error(state, branch, "jmpcc without condition?");
24557 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024558 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24559 if ((RHS(branch, 0)->op != OP_CMP) &&
24560 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024561 internal_error(state, branch, "bad branch test");
24562 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024563#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024564#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024565#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024566 ptr = RHS(branch, 0);
24567 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24568 if (ptr->op != OP_COPY) {
24569 internal_error(state, branch, "branch does not follow test");
24570 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024571 }
24572 switch(branch->op) {
24573 case OP_JMP_EQ: bop = "jz"; break;
24574 case OP_JMP_NOTEQ: bop = "jnz"; break;
24575 case OP_JMP_SLESS: bop = "jl"; break;
24576 case OP_JMP_ULESS: bop = "jb"; break;
24577 case OP_JMP_SMORE: bop = "jg"; break;
24578 case OP_JMP_UMORE: bop = "ja"; break;
24579 case OP_JMP_SLESSEQ: bop = "jle"; break;
24580 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24581 case OP_JMP_SMOREEQ: bop = "jge"; break;
24582 case OP_JMP_UMOREEQ: bop = "jae"; break;
24583 default:
24584 internal_error(state, branch, "Invalid branch op");
24585 break;
24586 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024587
Eric Biedermanb138ac82003-04-22 18:44:01 +000024588 }
Eric Biederman90089602004-05-28 14:11:54 +000024589#if 1
24590 if (branch->op == OP_CALL) {
24591 fprintf(fp, "\t/* call */\n");
24592 }
24593#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024594 fprintf(fp, "\t%s L%s%lu\n",
Stefan Reinauer14e22772010-04-27 06:56:47 +000024595 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024596 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024597 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024598}
24599
Eric Biederman5ade04a2003-10-22 04:03:46 +000024600static void print_op_ret(struct compile_state *state,
24601 struct triple *branch, FILE *fp)
24602{
24603 fprintf(fp, "\tjmp *%s\n",
24604 reg(state, RHS(branch, 0), REGCM_GPR32));
24605}
24606
Eric Biedermanb138ac82003-04-22 18:44:01 +000024607static void print_op_set(struct compile_state *state,
24608 struct triple *set, FILE *fp)
24609{
24610 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024611 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024612 internal_error(state, set, "setcc without condition?");
24613 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024614 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24615 if ((RHS(set, 0)->op != OP_CMP) &&
24616 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024617 internal_error(state, set, "bad set test");
24618 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024619 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024620 internal_error(state, set, "set does not follow test");
24621 }
24622 switch(set->op) {
24623 case OP_SET_EQ: sop = "setz"; break;
24624 case OP_SET_NOTEQ: sop = "setnz"; break;
24625 case OP_SET_SLESS: sop = "setl"; break;
24626 case OP_SET_ULESS: sop = "setb"; break;
24627 case OP_SET_SMORE: sop = "setg"; break;
24628 case OP_SET_UMORE: sop = "seta"; break;
24629 case OP_SET_SLESSEQ: sop = "setle"; break;
24630 case OP_SET_ULESSEQ: sop = "setbe"; break;
24631 case OP_SET_SMOREEQ: sop = "setge"; break;
24632 case OP_SET_UMOREEQ: sop = "setae"; break;
24633 default:
24634 internal_error(state, set, "Invalid set op");
24635 break;
24636 }
24637 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024638 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024639}
24640
Stefan Reinauer14e22772010-04-27 06:56:47 +000024641static void print_op_bit_scan(struct compile_state *state,
24642 struct triple *ins, FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024643{
24644 const char *op;
24645 switch(ins->op) {
24646 case OP_BSF: op = "bsf"; break;
24647 case OP_BSR: op = "bsr"; break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024648 default:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024649 internal_error(state, ins, "unknown bit scan");
24650 op = 0;
24651 break;
24652 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024653 fprintf(fp,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024654 "\t%s %s, %s\n"
24655 "\tjnz 1f\n"
24656 "\tmovl $-1, %s\n"
24657 "1:\n",
24658 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024659 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024660 reg(state, ins, REGCM_GPR32),
24661 reg(state, ins, REGCM_GPR32));
24662}
24663
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024664
Eric Biedermanb138ac82003-04-22 18:44:01 +000024665static void print_sdecl(struct compile_state *state,
24666 struct triple *ins, FILE *fp)
24667{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024668 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024669 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Stefan Reinauer14e22772010-04-27 06:56:47 +000024670 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024671 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024672 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024673 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Stefan Reinauer14e22772010-04-27 06:56:47 +000024674
Eric Biedermanb138ac82003-04-22 18:44:01 +000024675}
24676
24677static void print_instruction(struct compile_state *state,
24678 struct triple *ins, FILE *fp)
24679{
24680 /* Assumption: after I have exted the register allocator
Stefan Reinauer14e22772010-04-27 06:56:47 +000024681 * everything is in a valid register.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024682 */
24683 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024684 case OP_ASM:
24685 print_op_asm(state, ins, fp);
24686 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024687 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24688 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24689 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24690 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24691 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24692 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24693 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24694 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24695 case OP_POS: break;
24696 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24697 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024698 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024699 case OP_INTCONST:
24700 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024701 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024702 /* Don't generate anything here for constants */
24703 case OP_PHI:
24704 /* Don't generate anything for variable declarations. */
24705 break;
Eric Biederman90089602004-05-28 14:11:54 +000024706 case OP_UNKNOWNVAL:
24707 fprintf(fp, " /* unknown %s */\n",
24708 reg(state, ins, REGCM_ALL));
24709 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024710 case OP_SDECL:
24711 print_sdecl(state, ins, fp);
24712 break;
Stefan Reinauer14e22772010-04-27 06:56:47 +000024713 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024714 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024715 print_op_move(state, ins, fp);
24716 break;
24717 case OP_LOAD:
24718 print_op_load(state, ins, fp);
24719 break;
24720 case OP_STORE:
24721 print_op_store(state, ins, fp);
24722 break;
24723 case OP_SMUL:
24724 print_op_smul(state, ins, fp);
24725 break;
24726 case OP_CMP: print_op_cmp(state, ins, fp); break;
24727 case OP_TEST: print_op_test(state, ins, fp); break;
24728 case OP_JMP:
24729 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24730 case OP_JMP_SLESS: case OP_JMP_ULESS:
24731 case OP_JMP_SMORE: case OP_JMP_UMORE:
24732 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24733 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024734 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024735 print_op_branch(state, ins, fp);
24736 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024737 case OP_RET:
24738 print_op_ret(state, ins, fp);
24739 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024740 case OP_SET_EQ: case OP_SET_NOTEQ:
24741 case OP_SET_SLESS: case OP_SET_ULESS:
24742 case OP_SET_SMORE: case OP_SET_UMORE:
24743 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24744 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24745 print_op_set(state, ins, fp);
24746 break;
24747 case OP_INB: case OP_INW: case OP_INL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024748 print_op_in(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024749 break;
24750 case OP_OUTB: case OP_OUTW: case OP_OUTL:
Stefan Reinauer14e22772010-04-27 06:56:47 +000024751 print_op_out(state, ins, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024752 break;
24753 case OP_BSF:
24754 case OP_BSR:
24755 print_op_bit_scan(state, ins, fp);
24756 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024757 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024758 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024759 fprintf(fp, "\trdmsr\n");
24760 break;
24761 case OP_WRMSR:
24762 fprintf(fp, "\twrmsr\n");
24763 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024764 case OP_HLT:
24765 fprintf(fp, "\thlt\n");
24766 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024767 case OP_SDIVT:
24768 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24769 break;
24770 case OP_UDIVT:
24771 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24772 break;
24773 case OP_UMUL:
24774 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24775 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024776 case OP_LABEL:
24777 if (!ins->use) {
24778 return;
24779 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024780 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024781 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024782 break;
Eric Biederman90089602004-05-28 14:11:54 +000024783 case OP_ADECL:
24784 /* Ignore adecls with no registers error otherwise */
24785 if (!noop_adecl(ins)) {
24786 internal_error(state, ins, "adecl remains?");
24787 }
24788 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024789 /* Ignore OP_PIECE */
24790 case OP_PIECE:
24791 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024792 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024793 case OP_SDIV: case OP_UDIV:
24794 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024795 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24796 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24797 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24798 default:
24799 internal_error(state, ins, "unknown op: %d %s",
24800 ins->op, tops(ins->op));
24801 break;
24802 }
24803}
24804
24805static void print_instructions(struct compile_state *state)
24806{
24807 struct triple *first, *ins;
24808 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024809 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024810 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024811 int max_inline_depth;
24812 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024813 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024814 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024815 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024816 /* Masks for common sizes */
24817 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24818 fprintf(fp, ".balign 16\n");
24819 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24820 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24821 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24822 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024823 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024824 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024825 ins = first;
24826 do {
Stefan Reinauer14e22772010-04-27 06:56:47 +000024827 if (print_location &&
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024828 last_occurance != ins->occurance) {
24829 if (!ins->occurance->parent) {
24830 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024831 ins->occurance->function?ins->occurance->function:"(null)",
24832 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024833 ins->occurance->line,
24834 ins->occurance->col);
24835 }
24836 else {
24837 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024838 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024839 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024840 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024841 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024842 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024843 fprintf(fp, "\t * %s,%s:%d.%d\n",
24844 ptr->function,
24845 ptr->filename,
24846 ptr->line,
24847 ptr->col);
24848 }
24849 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024850 if (inline_depth > max_inline_depth) {
24851 max_inline_depth = inline_depth;
24852 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024853 }
24854 if (last_occurance) {
24855 put_occurance(last_occurance);
24856 }
24857 get_occurance(ins->occurance);
24858 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024859 }
24860
24861 print_instruction(state, ins, fp);
24862 ins = ins->next;
24863 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024864 if (print_location) {
24865 fprintf(fp, "/* max inline depth %d */\n",
24866 max_inline_depth);
24867 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024868}
Eric Biederman530b5192003-07-01 10:05:30 +000024869
Eric Biedermanb138ac82003-04-22 18:44:01 +000024870static void generate_code(struct compile_state *state)
24871{
24872 generate_local_labels(state);
24873 print_instructions(state);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024874
Eric Biedermanb138ac82003-04-22 18:44:01 +000024875}
24876
Eric Biederman90089602004-05-28 14:11:54 +000024877static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024878{
Eric Biederman90089602004-05-28 14:11:54 +000024879 int tok;
24880 FILE *fp;
24881 int line;
24882 const char *filename;
24883 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024884 filename = 0;
24885 line = 0;
24886 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024887 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024888 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024889 const char *token_str;
24890 tok = peek(state);
24891 if (tok == TOK_EOF) {
24892 break;
24893 }
Eric Biederman41203d92004-11-08 09:31:09 +000024894 tk = eat(state, tok);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024895 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024896 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024897 tk->str_len ? tk->val.str :
24898 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024899
24900 file = state->file;
24901 while(file->macro && file->prev) {
24902 file = file->prev;
24903 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024904 if (!file->macro &&
24905 ((file->line != line) || (file->basename != filename)))
Eric Biederman132368b2004-11-09 08:59:23 +000024906 {
Eric Biederman90089602004-05-28 14:11:54 +000024907 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024908 if ((file->basename == filename) &&
24909 (line < file->line)) {
24910 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024911 fprintf(fp, "\n");
24912 line++;
24913 }
24914 }
24915 else {
24916 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024917 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024918 }
Eric Biederman132368b2004-11-09 08:59:23 +000024919 line = file->line;
24920 filename = file->basename;
24921 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024922 for(i = 0; i < col; i++) {
24923 fprintf(fp, " ");
24924 }
24925 }
Stefan Reinauer14e22772010-04-27 06:56:47 +000024926
Eric Biederman90089602004-05-28 14:11:54 +000024927 fprintf(fp, "%s ", token_str);
Stefan Reinauer14e22772010-04-27 06:56:47 +000024928
Eric Biederman90089602004-05-28 14:11:54 +000024929 if (state->compiler->debug & DEBUG_TOKENS) {
24930 loc(state->dbgout, state, 0);
24931 fprintf(state->dbgout, "%s <- `%s'\n",
24932 tokens[tok], token_str);
24933 }
24934 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024935}
24936
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024937static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024938 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024939{
24940 int i;
24941 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024942 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024943 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024944 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024945 state.compiler = compiler;
24946 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024947 state.file = 0;
24948 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24949 memset(&state.token[i], 0, sizeof(state.token[i]));
24950 state.token[i].tok = -1;
24951 }
Eric Biederman90089602004-05-28 14:11:54 +000024952 /* Remember the output descriptors */
24953 state.errout = stderr;
24954 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024955 /* Remember the output filename */
Patrick Georgi516a2a72010-03-25 21:45:25 +000024956 if ((state.compiler->flags & COMPILER_PP_ONLY) && (strcmp("auto.inc",state.compiler->ofilename) == 0)) {
24957 state.output = stdout;
24958 } else {
24959 state.output = fopen(state.compiler->ofilename, "w");
24960 if (!state.output) {
24961 error(&state, 0, "Cannot open output file %s\n",
24962 state.compiler->ofilename);
24963 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024964 }
Eric Biederman90089602004-05-28 14:11:54 +000024965 /* Make certain a good cleanup happens */
24966 exit_state = &state;
24967 atexit(exit_cleanup);
24968
Eric Biedermanb138ac82003-04-22 18:44:01 +000024969 /* Prep the preprocessor */
24970 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024971 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024972 /* register the C keywords */
24973 register_keywords(&state);
24974 /* register the keywords the macro preprocessor knows */
24975 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024976 /* generate some builtin macros */
24977 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024978 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024979 state.i_switch = lookup(&state, "switch", 6);
24980 state.i_case = lookup(&state, "case", 4);
24981 state.i_continue = lookup(&state, "continue", 8);
24982 state.i_break = lookup(&state, "break", 5);
24983 state.i_default = lookup(&state, "default", 7);
24984 state.i_return = lookup(&state, "return", 6);
24985 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024986 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24987 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24988 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24989 /* Memorize where predefined identifiers are. */
24990 state.i___func__ = lookup(&state, "__func__", 8);
24991 /* Memorize where some attribute keywords are. */
24992 state.i_noinline = lookup(&state, "noinline", 8);
24993 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000024994 state.i_noreturn = lookup(&state, "noreturn", 8);
Sven Schnelleefb479c2012-06-22 08:53:36 +020024995 state.i_unused = lookup(&state, "unused", 6);
Eric Biederman90089602004-05-28 14:11:54 +000024996
24997 /* Process the command line macros */
24998 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000024999
25000 /* Allocate beginning bounding labels for the function list */
25001 state.first = label(&state);
25002 state.first->id |= TRIPLE_FLAG_VOLATILE;
25003 use_triple(state.first, state.first);
25004 ptr = label(&state);
25005 ptr->id |= TRIPLE_FLAG_VOLATILE;
25006 use_triple(ptr, ptr);
25007 flatten(&state, state.first, ptr);
25008
Eric Biederman5ade04a2003-10-22 04:03:46 +000025009 /* Allocate a label for the pool of global variables */
25010 state.global_pool = label(&state);
25011 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25012 flatten(&state, state.first, state.global_pool);
25013
Eric Biedermanb138ac82003-04-22 18:44:01 +000025014 /* Enter the globl definition scope */
25015 start_scope(&state);
25016 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025017
Eric Biedermanb138ac82003-04-22 18:44:01 +000025018 compile_file(&state, filename, 1);
Stefan Reinauer14e22772010-04-27 06:56:47 +000025019
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025020 while (includes) {
25021 compile_file(&state, includes->filename, 1);
25022 includes=includes->next;
25023 }
Eric Biederman90089602004-05-28 14:11:54 +000025024
25025 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025026 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025027 print_preprocessed_tokens(&state);
25028 return;
25029 }
25030
Eric Biedermanb138ac82003-04-22 18:44:01 +000025031 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025032
Eric Biedermanb138ac82003-04-22 18:44:01 +000025033 /* Exit the global definition scope */
25034 end_scope(&state);
25035
Stefan Reinauer14e22772010-04-27 06:56:47 +000025036 /* Now that basic compilation has happened
25037 * optimize the intermediate code
Eric Biedermanb138ac82003-04-22 18:44:01 +000025038 */
25039 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025040
Eric Biedermanb138ac82003-04-22 18:44:01 +000025041 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025042 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025043 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025044 }
Eric Biederman90089602004-05-28 14:11:54 +000025045 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025046}
25047
Eric Biederman90089602004-05-28 14:11:54 +000025048static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025049{
Eric Biederman90089602004-05-28 14:11:54 +000025050 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025051}
25052
25053static void usage(void)
25054{
Eric Biederman90089602004-05-28 14:11:54 +000025055 FILE *fp = stdout;
25056 version(fp);
25057 fprintf(fp,
25058 "\nUsage: romcc [options] <source>.c\n"
25059 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25060 "Options: \n"
25061 "-o <output file name>\n"
25062 "-f<option> Specify a generic compiler option\n"
25063 "-m<option> Specify a arch dependent option\n"
25064 "-- Specify this is the last option\n"
25065 "\nGeneric compiler options:\n"
25066 );
25067 compiler_usage(fp);
25068 fprintf(fp,
25069 "\nArchitecture compiler options:\n"
25070 );
25071 arch_usage(fp);
25072 fprintf(fp,
25073 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025074 );
25075}
25076
25077static void arg_error(char *fmt, ...)
25078{
25079 va_list args;
25080 va_start(args, fmt);
25081 vfprintf(stderr, fmt, args);
25082 va_end(args);
25083 usage();
25084 exit(1);
25085}
25086
25087int main(int argc, char **argv)
25088{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025089 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025090 struct compiler_state compiler;
25091 struct arch_state arch;
25092 int all_opts;
Stefan Reinauer14e22772010-04-27 06:56:47 +000025093
25094
Eric Biederman90089602004-05-28 14:11:54 +000025095 /* I don't want any surprises */
25096 setlocale(LC_ALL, "C");
25097
Eric Biederman5ade04a2003-10-22 04:03:46 +000025098 init_compiler_state(&compiler);
25099 init_arch_state(&arch);
25100 filename = 0;
25101 all_opts = 0;
25102 while(argc > 1) {
25103 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25104 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025105 argv += 2;
25106 argc -= 2;
25107 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025108 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025109 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025110 result = -1;
25111 if (strcmp(argv[1], "--") == 0) {
25112 result = 0;
25113 all_opts = 1;
25114 }
Eric Biederman90089602004-05-28 14:11:54 +000025115 else if (strncmp(argv[1], "-E", 2) == 0) {
25116 result = compiler_encode_flag(&compiler, argv[1]);
25117 }
25118 else if (strncmp(argv[1], "-O", 2) == 0) {
25119 result = compiler_encode_flag(&compiler, argv[1]);
25120 }
25121 else if (strncmp(argv[1], "-I", 2) == 0) {
25122 result = compiler_encode_flag(&compiler, argv[1]);
25123 }
25124 else if (strncmp(argv[1], "-D", 2) == 0) {
25125 result = compiler_encode_flag(&compiler, argv[1]);
25126 }
25127 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025128 result = compiler_encode_flag(&compiler, argv[1]);
25129 }
25130 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25131 result = compiler_encode_flag(&compiler, argv[1]+2);
25132 }
25133 else if (strncmp(argv[1], "-f", 2) == 0) {
25134 result = compiler_encode_flag(&compiler, argv[1]+2);
25135 }
25136 else if (strncmp(argv[1], "-m", 2) == 0) {
25137 result = arch_encode_flag(&arch, argv[1]+2);
25138 }
Patrick Georgi516a2a72010-03-25 21:45:25 +000025139 else if (strncmp(argv[1], "-c", 2) == 0) {
25140 result = 0;
25141 }
25142 else if (strncmp(argv[1], "-S", 2) == 0) {
25143 result = 0;
25144 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025145 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025146 struct filelist *old_head = include_filelist;
25147 include_filelist = malloc(sizeof(struct filelist));
25148 if (!include_filelist) {
25149 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025150 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025151 argv++;
25152 argc--;
Patrick Georgic2d0b622010-03-25 15:49:40 +000025153 include_filelist->filename = strdup(argv[1]);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025154 include_filelist->next = old_head;
25155 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025156 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025157 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025158 arg_error("Invalid option specified: %s\n",
25159 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025160 }
25161 argv++;
25162 argc--;
25163 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025164 else {
25165 if (filename) {
25166 arg_error("Only one filename may be specified\n");
25167 }
25168 filename = argv[1];
25169 argv++;
25170 argc--;
25171 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025172 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025173 if (!filename) {
25174 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025175 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025176 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025177
25178 return 0;
25179}