blob: b6f68409be2b6fb4ca2fce029ae0fff8bce51db4 [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 Reinauer50542a82007-10-24 11:14:14 +000028/* NOTE: Before you even start thinking to touch anything
29 * in this code, set DEBUG_ROMCC_WARNINGS to 1 to get an
30 * insight on the original author's thoughts. We introduced
31 * 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.
64 *
65 * AAA
66 * +---/
67 * /
68 * / +--->CCC
69 * | | / \
70 * | | DDD EEE break;
71 * | | \ \
72 * | | FFF \
73 * \| / \ \
74 * |\ GGG HHH | continue;
75 * | \ \ | |
76 * | \ III | /
77 * | \ | / /
78 * | vvv /
79 * +----BBB /
80 * | /
81 * vv
82 * JJJ
83 *
84 *
85 * AAA
86 * +-----+ | +----+
87 * | \ | / |
88 * | BBB +-+ |
89 * | / \ / | |
90 * | CCC JJJ / /
91 * | / \ / /
92 * | DDD EEE / /
93 * | | +-/ /
94 * | FFF /
95 * | / \ /
96 * | GGG HHH /
97 * | | +-/
98 * | III
99 * +--+
100 *
101 *
102 * 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 )
120 * / \
121 * 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.
128 *
129 */
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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000138static void die(char *fmt, ...)
139{
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;
Eric Biedermanb138ac82003-04-22 18:44:01 +0000234
235 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
409#define OP_SSR 12
410#define OP_AND 13
411#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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000416
417#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
427
428#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
486#define OP_WRITE 60
487/* 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.
555 *
556 * Although OP_TUPLE always has register sized pieces they are not
557 * used until structures are flattened/decomposed into their register
558 * components.
559 * ???? 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
Eric Biederman5ade04a2003-10-22 04:03:46 +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
575 *
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
Eric Biederman5ade04a2003-10-22 04:03:46 +0000625#define OP_ADECL 87
626/* 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.
Eric Biederman90089602004-05-28 14:11:54 +0000631 *
632 * Although OP_ADECL always has register sized pieces they are not
633 * used until structures are flattened/decomposed into their register
634 * 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
Eric Biedermanb138ac82003-04-22 18:44:01 +0000646/* OP_PHI is a triple used in SSA form code.
647 * 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.
Eric Biedermanb138ac82003-04-22 18:44:01 +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 */
664/* OP_CPS_BRANCH calls a continuation
665 * 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 */
669/* OP_CPS_CBRANCH conditionally calls one of two continuations
670 * 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
683/* OP_CPS_RET conditionally calls one of two continuations
684 * 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"),
Eric Biederman6aa31cc2003-06-10 21:22:07 +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
895static const char *tops(int index)
896{
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;
1006
1007};
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;
Eric Biederman90089602004-05-28 14:11:54 +00001087 /* Additional hash entries for predefined macros */
1088 struct hash_entry *i_defined;
1089 struct hash_entry *i___VA_ARGS__;
1090 struct hash_entry *i___FILE__;
1091 struct hash_entry *i___LINE__;
1092 /* Additional hash entries for predefined identifiers */
1093 struct hash_entry *i___func__;
1094 /* Additional hash entries for attributes */
1095 struct hash_entry *i_noinline;
1096 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001097 int scope_depth;
Eric Biedermancb364952004-11-15 10:46:44 +00001098 unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
Eric Biederman90089602004-05-28 14:11:54 +00001099 int if_depth;
1100 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001101 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001102 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001103 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001104 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001105 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001106 struct basic_blocks bb;
1107 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001108};
1109
Eric Biederman0babc1c2003-05-09 02:39:00 +00001110/* visibility global/local */
1111/* static/auto duration */
1112/* typedef, register, inline */
1113#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001114#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001115/* Visibility */
1116#define STOR_GLOBAL 0x0001
1117/* Duration */
1118#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001119/* Definition locality */
1120#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001121/* Storage specifiers */
1122#define STOR_AUTO 0x0000
1123#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001124#define STOR_LOCAL 0x0003
1125#define STOR_EXTERN 0x0007
1126#define STOR_INLINE 0x0008
1127#define STOR_REGISTER 0x0010
1128#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001129
Eric Biederman5ade04a2003-10-22 04:03:46 +00001130#define QUAL_SHIFT 5
1131#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001132#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001133#define QUAL_CONST 0x0020
1134#define QUAL_VOLATILE 0x0040
1135#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001136
1137#define TYPE_SHIFT 8
1138#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001139#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1140#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001141#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1142#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001143#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1144#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001145#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1146#define TYPE_DEFAULT 0x0000
1147#define TYPE_VOID 0x0100
1148#define TYPE_CHAR 0x0200
1149#define TYPE_UCHAR 0x0300
1150#define TYPE_SHORT 0x0400
1151#define TYPE_USHORT 0x0500
1152#define TYPE_INT 0x0600
1153#define TYPE_UINT 0x0700
1154#define TYPE_LONG 0x0800
1155#define TYPE_ULONG 0x0900
1156#define TYPE_LLONG 0x0a00 /* long long */
1157#define TYPE_ULLONG 0x0b00
1158#define TYPE_FLOAT 0x0c00
1159#define TYPE_DOUBLE 0x0d00
1160#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001161
1162/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1163#define TYPE_ENUM 0x1600
1164#define TYPE_LIST 0x1700
1165/* TYPE_LIST is a basic building block when defining enumerations
1166 * type->field_ident holds the name of this enumeration entry.
1167 * type->right holds the entry in the list.
1168 */
1169
Eric Biederman0babc1c2003-05-09 02:39:00 +00001170#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001171/* For TYPE_STRUCT
1172 * type->left holds the link list of TYPE_PRODUCT entries that
1173 * make up the structure.
1174 * type->elements hold the length of the linked list
1175 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001176#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001177/* For TYPE_UNION
1178 * type->left holds the link list of TYPE_OVERLAP entries that
1179 * make up the union.
1180 * type->elements hold the length of the linked list
1181 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001182#define TYPE_POINTER 0x1200
1183/* For TYPE_POINTER:
1184 * type->left holds the type pointed to.
1185 */
1186#define TYPE_FUNCTION 0x1300
1187/* For TYPE_FUNCTION:
1188 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001189 * type->right holds the type of the arguments
1190 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001191 */
1192#define TYPE_PRODUCT 0x1400
1193/* TYPE_PRODUCT is a basic building block when defining structures
1194 * type->left holds the type that appears first in memory.
1195 * type->right holds the type that appears next in memory.
1196 */
1197#define TYPE_OVERLAP 0x1500
1198/* TYPE_OVERLAP is a basic building block when defining unions
1199 * type->left and type->right holds to types that overlap
1200 * each other in memory.
1201 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001202#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001203/* TYPE_ARRAY is a basic building block when definitng arrays.
1204 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001205 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001206 */
Eric Biederman90089602004-05-28 14:11:54 +00001207#define TYPE_TUPLE 0x1900
1208/* TYPE_TUPLE is a basic building block when defining
1209 * positionally reference type conglomerations. (i.e. closures)
1210 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1211 * except it has no field names.
1212 * type->left holds the liked list of TYPE_PRODUCT entries that
1213 * make up the closure type.
1214 * type->elements hold the number of elements in the closure.
1215 */
1216#define TYPE_JOIN 0x1a00
1217/* TYPE_JOIN is a basic building block when defining
1218 * positionally reference type conglomerations. (i.e. closures)
1219 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1220 * except it has no field names.
1221 * type->left holds the liked list of TYPE_OVERLAP entries that
1222 * make up the closure type.
1223 * type->elements hold the number of elements in the closure.
1224 */
1225#define TYPE_BITFIELD 0x1b00
1226/* TYPE_BITFIED is the type of a bitfield.
1227 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1228 * type->elements holds the number of bits in the bitfield.
1229 */
1230#define TYPE_UNKNOWN 0x1c00
1231/* TYPE_UNKNOWN is the type of an unknown value.
1232 * Used on unknown consts and other places where I don't know the type.
1233 */
1234
1235#define ATTRIB_SHIFT 16
1236#define ATTRIB_MASK 0xffff0000
1237#define ATTRIB_NOINLINE 0x00010000
1238#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001239
Eric Biederman83b991a2003-10-11 06:20:25 +00001240#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001241
1242struct type {
1243 unsigned int type;
1244 struct type *left, *right;
1245 ulong_t elements;
1246 struct hash_entry *field_ident;
1247 struct hash_entry *type_ident;
1248};
1249
Eric Biederman530b5192003-07-01 10:05:30 +00001250#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001251#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001252#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001253#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001254#define MAX_REGISTERS 75
1255#define REGISTER_BITS 7
1256#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001257#define REG_ERROR 0
1258#define REG_UNSET 1
1259#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001260#define REG_VIRT0 (MAX_REGISTERS + 0)
1261#define REG_VIRT1 (MAX_REGISTERS + 1)
1262#define REG_VIRT2 (MAX_REGISTERS + 2)
1263#define REG_VIRT3 (MAX_REGISTERS + 3)
1264#define REG_VIRT4 (MAX_REGISTERS + 4)
1265#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001266#define REG_VIRT6 (MAX_REGISTERS + 6)
1267#define REG_VIRT7 (MAX_REGISTERS + 7)
1268#define REG_VIRT8 (MAX_REGISTERS + 8)
1269#define REG_VIRT9 (MAX_REGISTERS + 9)
1270
1271#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1272#error "MAX_VIRT_REGISTERS to small"
1273#endif
Eric Biederman90089602004-05-28 14:11:54 +00001274#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001275#error "Too many id bits used"
1276#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001277
1278/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001279#define REG_SHIFT 0
1280#define REGC_SHIFT REGISTER_BITS
1281#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001282#define REG_MASK (MAX_VIRT_REGISTERS -1)
1283#define ID_REG(ID) ((ID) & REG_MASK)
1284#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001285#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1286#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1287#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1288 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001289
Eric Biederman90089602004-05-28 14:11:54 +00001290#define ARCH_INPUT_REGS 4
1291#define ARCH_OUTPUT_REGS 4
1292
1293static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1294static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001295static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001296static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001297static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001298static void arch_reg_equivs(
1299 struct compile_state *state, unsigned *equiv, int reg);
1300static int arch_select_free_register(
1301 struct compile_state *state, char *used, int classes);
1302static unsigned arch_regc_size(struct compile_state *state, int class);
1303static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1304static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1305static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001306static struct reg_info arch_reg_constraint(
1307 struct compile_state *state, struct type *type, const char *constraint);
1308static struct reg_info arch_reg_clobber(
1309 struct compile_state *state, const char *clobber);
1310static struct reg_info arch_reg_lhs(struct compile_state *state,
1311 struct triple *ins, int index);
1312static struct reg_info arch_reg_rhs(struct compile_state *state,
1313 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001314static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001315static struct triple *transform_to_arch_instruction(
1316 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001317static struct triple *flatten(
1318 struct compile_state *state, struct triple *first, struct triple *ptr);
Jason Schildt27b85112005-08-10 14:31:52 +00001319static void print_dominators(struct compile_state *state,
1320 FILE *fp, struct basic_blocks *bb);
1321static void print_dominance_frontiers(struct compile_state *state,
1322 FILE *fp, struct basic_blocks *bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001323
1324
Eric Biedermanb138ac82003-04-22 18:44:01 +00001325
Eric Biederman5ade04a2003-10-22 04:03:46 +00001326#define DEBUG_ABORT_ON_ERROR 0x00000001
1327#define DEBUG_BASIC_BLOCKS 0x00000002
1328#define DEBUG_FDOMINATORS 0x00000004
1329#define DEBUG_RDOMINATORS 0x00000008
1330#define DEBUG_TRIPLES 0x00000010
1331#define DEBUG_INTERFERENCE 0x00000020
1332#define DEBUG_SCC_TRANSFORM 0x00000040
1333#define DEBUG_SCC_TRANSFORM2 0x00000080
1334#define DEBUG_REBUILD_SSA_FORM 0x00000100
1335#define DEBUG_INLINE 0x00000200
1336#define DEBUG_RANGE_CONFLICTS 0x00000400
1337#define DEBUG_RANGE_CONFLICTS2 0x00000800
1338#define DEBUG_COLOR_GRAPH 0x00001000
1339#define DEBUG_COLOR_GRAPH2 0x00002000
1340#define DEBUG_COALESCING 0x00004000
1341#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001342#define DEBUG_VERIFICATION 0x00010000
1343#define DEBUG_CALLS 0x00020000
1344#define DEBUG_CALLS2 0x00040000
1345#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001346
1347#define DEBUG_DEFAULT ( \
1348 DEBUG_ABORT_ON_ERROR | \
1349 DEBUG_BASIC_BLOCKS | \
1350 DEBUG_FDOMINATORS | \
1351 DEBUG_RDOMINATORS | \
1352 DEBUG_TRIPLES | \
1353 0 )
1354
Eric Biederman90089602004-05-28 14:11:54 +00001355#define DEBUG_ALL ( \
1356 DEBUG_ABORT_ON_ERROR | \
1357 DEBUG_BASIC_BLOCKS | \
1358 DEBUG_FDOMINATORS | \
1359 DEBUG_RDOMINATORS | \
1360 DEBUG_TRIPLES | \
1361 DEBUG_INTERFERENCE | \
1362 DEBUG_SCC_TRANSFORM | \
1363 DEBUG_SCC_TRANSFORM2 | \
1364 DEBUG_REBUILD_SSA_FORM | \
1365 DEBUG_INLINE | \
1366 DEBUG_RANGE_CONFLICTS | \
1367 DEBUG_RANGE_CONFLICTS2 | \
1368 DEBUG_COLOR_GRAPH | \
1369 DEBUG_COLOR_GRAPH2 | \
1370 DEBUG_COALESCING | \
1371 DEBUG_COALESCING2 | \
1372 DEBUG_VERIFICATION | \
1373 DEBUG_CALLS | \
1374 DEBUG_CALLS2 | \
1375 DEBUG_TOKENS | \
1376 0 )
1377
1378#define COMPILER_INLINE_MASK 0x00000007
1379#define COMPILER_INLINE_ALWAYS 0x00000000
1380#define COMPILER_INLINE_NEVER 0x00000001
1381#define COMPILER_INLINE_DEFAULTON 0x00000002
1382#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1383#define COMPILER_INLINE_NOPENALTY 0x00000004
1384#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1385#define COMPILER_SIMPLIFY 0x00000010
1386#define COMPILER_SCC_TRANSFORM 0x00000020
1387#define COMPILER_SIMPLIFY_OP 0x00000040
1388#define COMPILER_SIMPLIFY_PHI 0x00000080
1389#define COMPILER_SIMPLIFY_LABEL 0x00000100
1390#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1391#define COMPILER_SIMPLIFY_COPY 0x00000400
1392#define COMPILER_SIMPLIFY_ARITH 0x00000800
1393#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1394#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1395#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1396#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1397
Eric Biedermancb364952004-11-15 10:46:44 +00001398#define COMPILER_TRIGRAPHS 0x40000000
1399#define COMPILER_PP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001400
1401#define COMPILER_DEFAULT_FLAGS ( \
Eric Biedermancb364952004-11-15 10:46:44 +00001402 COMPILER_TRIGRAPHS | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001403 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001404 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001405 COMPILER_SIMPLIFY_OP | \
1406 COMPILER_SIMPLIFY_PHI | \
1407 COMPILER_SIMPLIFY_LABEL | \
1408 COMPILER_SIMPLIFY_BRANCH | \
1409 COMPILER_SIMPLIFY_COPY | \
1410 COMPILER_SIMPLIFY_ARITH | \
1411 COMPILER_SIMPLIFY_SHIFT | \
1412 COMPILER_SIMPLIFY_BITWISE | \
1413 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001414 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001415 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001416
Eric Biederman153ea352003-06-20 14:43:20 +00001417#define GLOBAL_SCOPE_DEPTH 1
1418#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001419
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001420static void compile_file(struct compile_state *old_state, const char *filename, int local);
1421
Eric Biederman5ade04a2003-10-22 04:03:46 +00001422
1423
1424static void init_compiler_state(struct compiler_state *compiler)
1425{
1426 memset(compiler, 0, sizeof(*compiler));
1427 compiler->label_prefix = "";
1428 compiler->ofilename = "auto.inc";
1429 compiler->flags = COMPILER_DEFAULT_FLAGS;
1430 compiler->debug = 0;
1431 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001432 compiler->include_path_count = 1;
1433 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1434 compiler->define_count = 1;
1435 compiler->defines = xcmalloc(sizeof(char *), "defines");
1436 compiler->undef_count = 1;
1437 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001438}
1439
1440struct compiler_flag {
1441 const char *name;
1442 unsigned long flag;
1443};
Eric Biederman90089602004-05-28 14:11:54 +00001444
1445struct compiler_arg {
1446 const char *name;
1447 unsigned long mask;
1448 struct compiler_flag flags[16];
1449};
1450
Eric Biederman5ade04a2003-10-22 04:03:46 +00001451static int set_flag(
1452 const struct compiler_flag *ptr, unsigned long *flags,
1453 int act, const char *flag)
1454{
1455 int result = -1;
1456 for(; ptr->name; ptr++) {
1457 if (strcmp(ptr->name, flag) == 0) {
1458 break;
1459 }
1460 }
1461 if (ptr->name) {
1462 result = 0;
1463 *flags &= ~(ptr->flag);
1464 if (act) {
1465 *flags |= ptr->flag;
1466 }
1467 }
1468 return result;
1469}
1470
Eric Biederman90089602004-05-28 14:11:54 +00001471static int set_arg(
1472 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1473{
1474 const char *val;
1475 int result = -1;
1476 int len;
1477 val = strchr(arg, '=');
1478 if (val) {
1479 len = val - arg;
1480 val++;
1481 for(; ptr->name; ptr++) {
1482 if (strncmp(ptr->name, arg, len) == 0) {
1483 break;
1484 }
1485 }
1486 if (ptr->name) {
1487 *flags &= ~ptr->mask;
1488 result = set_flag(&ptr->flags[0], flags, 1, val);
1489 }
1490 }
1491 return result;
1492}
1493
1494
1495static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
1496 const char *prefix, const char *invert_prefix)
1497{
1498 for(;ptr->name; ptr++) {
1499 fprintf(fp, "%s%s\n", prefix, ptr->name);
1500 if (invert_prefix) {
1501 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1502 }
1503 }
1504}
1505
1506static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1507 const char *prefix)
1508{
1509 for(;ptr->name; ptr++) {
1510 const struct compiler_flag *flag;
1511 for(flag = &ptr->flags[0]; flag->name; flag++) {
1512 fprintf(fp, "%s%s=%s\n",
1513 prefix, ptr->name, flag->name);
1514 }
1515 }
1516}
1517
1518static int append_string(size_t *max, const char ***vec, const char *str,
1519 const char *name)
1520{
1521 size_t count;
1522 count = ++(*max);
1523 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1524 (*vec)[count -1] = 0;
1525 (*vec)[count -2] = str;
1526 return 0;
1527}
1528
1529static void arg_error(char *fmt, ...);
1530static const char *identifier(const char *str, const char *end);
1531
1532static int append_include_path(struct compiler_state *compiler, const char *str)
1533{
1534 int result;
1535 if (!exists(str, ".")) {
1536 arg_error("Nonexistent include path: `%s'\n",
1537 str);
1538 }
1539 result = append_string(&compiler->include_path_count,
1540 &compiler->include_paths, str, "include_paths");
1541 return result;
1542}
1543
1544static int append_define(struct compiler_state *compiler, const char *str)
1545{
1546 const char *end, *rest;
1547 int result;
1548
1549 end = strchr(str, '=');
1550 if (!end) {
1551 end = str + strlen(str);
1552 }
1553 rest = identifier(str, end);
1554 if (rest != end) {
1555 int len = end - str - 1;
1556 arg_error("Invalid name cannot define macro: `%*.*s'\n",
1557 len, len, str);
1558 }
1559 result = append_string(&compiler->define_count,
1560 &compiler->defines, str, "defines");
1561 return result;
1562}
1563
1564static int append_undef(struct compiler_state *compiler, const char *str)
1565{
1566 const char *end, *rest;
1567 int result;
1568
1569 end = str + strlen(str);
1570 rest = identifier(str, end);
1571 if (rest != end) {
1572 int len = end - str - 1;
1573 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
1574 len, len, str);
1575 }
1576 result = append_string(&compiler->undef_count,
1577 &compiler->undefs, str, "undefs");
1578 return result;
1579}
1580
1581static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001582 { "trigraphs", COMPILER_TRIGRAPHS },
1583 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001584 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1585 { "simplify", COMPILER_SIMPLIFY },
1586 { "scc-transform", COMPILER_SCC_TRANSFORM },
1587 { "simplify-op", COMPILER_SIMPLIFY_OP },
1588 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1589 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1590 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1591 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1592 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1593 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1594 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1595 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1596 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1597 { 0, 0 },
1598};
1599static const struct compiler_arg romcc_args[] = {
1600 { "inline-policy", COMPILER_INLINE_MASK,
1601 {
1602 { "always", COMPILER_INLINE_ALWAYS, },
1603 { "never", COMPILER_INLINE_NEVER, },
1604 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1605 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1606 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1607 { 0, 0 },
1608 },
1609 },
1610 { 0, 0 },
1611};
1612static const struct compiler_flag romcc_opt_flags[] = {
1613 { "-O", COMPILER_SIMPLIFY },
1614 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001615 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001616 { 0, 0, },
1617};
1618static const struct compiler_flag romcc_debug_flags[] = {
1619 { "all", DEBUG_ALL },
1620 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1621 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1622 { "fdominators", DEBUG_FDOMINATORS },
1623 { "rdominators", DEBUG_RDOMINATORS },
1624 { "triples", DEBUG_TRIPLES },
1625 { "interference", DEBUG_INTERFERENCE },
1626 { "scc-transform", DEBUG_SCC_TRANSFORM },
1627 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1628 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1629 { "inline", DEBUG_INLINE },
1630 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1631 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1632 { "color-graph", DEBUG_COLOR_GRAPH },
1633 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1634 { "coalescing", DEBUG_COALESCING },
1635 { "coalescing2", DEBUG_COALESCING2 },
1636 { "verification", DEBUG_VERIFICATION },
1637 { "calls", DEBUG_CALLS },
1638 { "calls2", DEBUG_CALLS2 },
1639 { "tokens", DEBUG_TOKENS },
1640 { 0, 0 },
1641};
1642
Eric Biederman5ade04a2003-10-22 04:03:46 +00001643static int compiler_encode_flag(
1644 struct compiler_state *compiler, const char *flag)
1645{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001646 int act;
1647 int result;
1648
1649 act = 1;
1650 result = -1;
1651 if (strncmp(flag, "no-", 3) == 0) {
1652 flag += 3;
1653 act = 0;
1654 }
1655 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001656 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1657 }
1658 else if (strncmp(flag, "-E", 2) == 0) {
1659 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1660 }
1661 else if (strncmp(flag, "-I", 2) == 0) {
1662 result = append_include_path(compiler, flag + 2);
1663 }
1664 else if (strncmp(flag, "-D", 2) == 0) {
1665 result = append_define(compiler, flag + 2);
1666 }
1667 else if (strncmp(flag, "-U", 2) == 0) {
1668 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001669 }
1670 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1671 result = 0;
1672 compiler->label_prefix = flag + 13;
1673 }
1674 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1675 unsigned long max_passes;
1676 char *end;
1677 max_passes = strtoul(flag + 22, &end, 10);
1678 if (end[0] == '\0') {
1679 result = 0;
1680 compiler->max_allocation_passes = max_passes;
1681 }
1682 }
1683 else if (act && strcmp(flag, "debug") == 0) {
1684 result = 0;
1685 compiler->debug |= DEBUG_DEFAULT;
1686 }
1687 else if (strncmp(flag, "debug-", 6) == 0) {
1688 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001689 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001690 }
1691 else {
Eric Biederman90089602004-05-28 14:11:54 +00001692 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1693 if (result < 0) {
1694 result = set_arg(romcc_args, &compiler->flags, flag);
1695 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001696 }
1697 return result;
1698}
1699
Eric Biederman90089602004-05-28 14:11:54 +00001700static void compiler_usage(FILE *fp)
1701{
1702 flag_usage(fp, romcc_opt_flags, "", 0);
1703 flag_usage(fp, romcc_flags, "-f", "-fno-");
1704 arg_usage(fp, romcc_args, "-f");
1705 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1706 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1707 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1708 fprintf(fp, "-I<include path>\n");
1709 fprintf(fp, "-D<macro>[=defn]\n");
1710 fprintf(fp, "-U<macro>\n");
1711}
1712
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001713static void do_cleanup(struct compile_state *state)
1714{
1715 if (state->output) {
1716 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001717 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001718 state->output = 0;
1719 }
1720 if (state->dbgout) {
1721 fflush(state->dbgout);
1722 }
1723 if (state->errout) {
1724 fflush(state->errout);
1725 }
1726}
1727
1728static struct compile_state *exit_state;
1729static void exit_cleanup(void)
1730{
1731 if (exit_state) {
1732 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001733 }
1734}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001735
1736static int get_col(struct file_state *file)
1737{
1738 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001739 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001740 ptr = file->line_start;
1741 end = file->pos;
1742 for(col = 0; ptr < end; ptr++) {
1743 if (*ptr != '\t') {
1744 col++;
1745 }
1746 else {
1747 col = (col & ~7) + 8;
1748 }
1749 }
1750 return col;
1751}
1752
1753static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1754{
1755 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001756 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001757 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001758 for(spot = triple->occurance; spot; spot = spot->parent) {
1759 fprintf(fp, "%s:%d.%d: ",
1760 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001761 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001762 return;
1763 }
1764 if (!state->file) {
1765 return;
1766 }
1767 col = get_col(state->file);
1768 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001769 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001770}
1771
Patrick Georgib203c2f2009-08-20 14:48:03 +00001772static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001773 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001774{
Eric Biederman90089602004-05-28 14:11:54 +00001775 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001776 va_list args;
1777 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001778 loc(fp, state, ptr);
1779 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001780 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001781 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001782 }
Eric Biederman90089602004-05-28 14:11:54 +00001783 fprintf(fp, "Internal compiler error: ");
1784 vfprintf(fp, fmt, args);
1785 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001786 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001787 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001788 abort();
1789}
1790
1791
Eric Biederman5ade04a2003-10-22 04:03:46 +00001792static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001793 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001794{
Eric Biederman90089602004-05-28 14:11:54 +00001795 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001796 va_list args;
1797 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001798 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001799 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001800 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001801 }
Eric Biederman90089602004-05-28 14:11:54 +00001802 fprintf(fp, "Internal compiler warning: ");
1803 vfprintf(fp, fmt, args);
1804 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001805 va_end(args);
1806}
1807
1808
1809
Patrick Georgib203c2f2009-08-20 14:48:03 +00001810static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001811 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001812{
Eric Biederman90089602004-05-28 14:11:54 +00001813 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001814 va_list args;
1815 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001816 loc(fp, state, ptr);
1817 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001818 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001819 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001820 }
Eric Biederman90089602004-05-28 14:11:54 +00001821 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001822 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001823 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001824 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001825 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001826 abort();
1827 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001828 exit(1);
1829}
1830
Eric Biederman5ade04a2003-10-22 04:03:46 +00001831static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001832 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001833{
Eric Biederman90089602004-05-28 14:11:54 +00001834 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001835 va_list args;
1836 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001837 loc(fp, state, ptr);
1838 fprintf(fp, "warning: ");
1839 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1840 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1841 }
1842 vfprintf(fp, fmt, args);
1843 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001844 va_end(args);
1845}
1846
Eric Biedermanb138ac82003-04-22 18:44:01 +00001847#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1848
Eric Biederman0babc1c2003-05-09 02:39:00 +00001849static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001850{
1851 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001852 if (op >= OP_MAX) {
1853 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001854 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001855 if (op < 0) {
1856 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001857 }
1858}
1859
Eric Biederman0babc1c2003-05-09 02:39:00 +00001860static void valid_ins(struct compile_state *state, struct triple *ptr)
1861{
1862 valid_op(state, ptr->op);
1863}
1864
Stefan Reinauer50542a82007-10-24 11:14:14 +00001865#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001866static void valid_param_count(struct compile_state *state, struct triple *ins)
1867{
1868 int lhs, rhs, misc, targ;
1869 valid_ins(state, ins);
1870 lhs = table_ops[ins->op].lhs;
1871 rhs = table_ops[ins->op].rhs;
1872 misc = table_ops[ins->op].misc;
1873 targ = table_ops[ins->op].targ;
1874
1875 if ((lhs >= 0) && (ins->lhs != lhs)) {
1876 internal_error(state, ins, "Bad lhs count");
1877 }
1878 if ((rhs >= 0) && (ins->rhs != rhs)) {
1879 internal_error(state, ins, "Bad rhs count");
1880 }
1881 if ((misc >= 0) && (ins->misc != misc)) {
1882 internal_error(state, ins, "Bad misc count");
1883 }
1884 if ((targ >= 0) && (ins->targ != targ)) {
1885 internal_error(state, ins, "Bad targ count");
1886 }
1887}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001888#endif
Eric Biederman90089602004-05-28 14:11:54 +00001889
Eric Biedermanb138ac82003-04-22 18:44:01 +00001890static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001891static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001892static void use_triple(struct triple *used, struct triple *user)
1893{
1894 struct triple_set **ptr, *new;
1895 if (!used)
1896 return;
1897 if (!user)
1898 return;
Patrick Georgif761aef2010-03-14 22:20:57 +00001899 if (used->use == (void*)-1)
1900 used->use = 0;
1901 if (used->use) {
1902 ptr = &used->use;
1903 while(*ptr) {
1904 if ((*ptr)->member == user) {
1905 return;
1906 }
1907 ptr = &(*ptr)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001908 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001909 }
1910 /* Append new to the head of the list,
1911 * copy_func and rename_block_variables
1912 * depends on this.
1913 */
1914 new = xcmalloc(sizeof(*new), "triple_set");
1915 new->member = user;
1916 new->next = used->use;
1917 used->use = new;
1918}
1919
1920static void unuse_triple(struct triple *used, struct triple *unuser)
1921{
1922 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001923 if (!used) {
1924 return;
1925 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001926 ptr = &used->use;
1927 while(*ptr) {
1928 use = *ptr;
1929 if (use->member == unuser) {
1930 *ptr = use->next;
1931 xfree(use);
1932 }
1933 else {
1934 ptr = &use->next;
1935 }
1936 }
1937}
1938
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001939static void put_occurance(struct occurance *occurance)
1940{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001941 if (occurance) {
1942 occurance->count -= 1;
1943 if (occurance->count <= 0) {
1944 if (occurance->parent) {
1945 put_occurance(occurance->parent);
1946 }
1947 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001948 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001949 }
1950}
1951
1952static void get_occurance(struct occurance *occurance)
1953{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001954 if (occurance) {
1955 occurance->count += 1;
1956 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001957}
1958
1959
1960static struct occurance *new_occurance(struct compile_state *state)
1961{
1962 struct occurance *result, *last;
1963 const char *filename;
1964 const char *function;
1965 int line, col;
1966
1967 function = "";
1968 filename = 0;
1969 line = 0;
1970 col = 0;
1971 if (state->file) {
1972 filename = state->file->report_name;
1973 line = state->file->report_line;
1974 col = get_col(state->file);
1975 }
1976 if (state->function) {
1977 function = state->function;
1978 }
1979 last = state->last_occurance;
1980 if (last &&
1981 (last->col == col) &&
1982 (last->line == line) &&
1983 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001984 ((last->filename == filename) ||
1985 (strcmp(last->filename, filename) == 0)))
1986 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001987 get_occurance(last);
1988 return last;
1989 }
1990 if (last) {
1991 state->last_occurance = 0;
1992 put_occurance(last);
1993 }
1994 result = xmalloc(sizeof(*result), "occurance");
1995 result->count = 2;
1996 result->filename = filename;
1997 result->function = function;
1998 result->line = line;
1999 result->col = col;
2000 result->parent = 0;
2001 state->last_occurance = result;
2002 return result;
2003}
2004
2005static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002006 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002007{
2008 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002009 if (top->parent) {
2010 internal_error(state, 0, "inlining an already inlined function?");
2011 }
2012 /* If I have a null base treat it that way */
2013 if ((base->parent == 0) &&
2014 (base->col == 0) &&
2015 (base->line == 0) &&
2016 (base->function[0] == '\0') &&
2017 (base->filename[0] == '\0')) {
2018 base = 0;
2019 }
2020 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002021 last = state->last_occurance;
2022 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002023 (last->parent == base) &&
2024 (last->col == top->col) &&
2025 (last->line == top->line) &&
2026 (last->function == top->function) &&
2027 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002028 get_occurance(last);
2029 return last;
2030 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002031 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002032 if (last) {
2033 state->last_occurance = 0;
2034 put_occurance(last);
2035 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002036 /* Generate a new occurance structure */
2037 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002038 result = xmalloc(sizeof(*result), "occurance");
2039 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002040 result->filename = top->filename;
2041 result->function = top->function;
2042 result->line = top->line;
2043 result->col = top->col;
2044 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002045 state->last_occurance = result;
2046 return result;
2047}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002048
2049static struct occurance dummy_occurance = {
2050 .count = 2,
2051 .filename = __FILE__,
2052 .function = "",
2053 .line = __LINE__,
2054 .col = 0,
2055 .parent = 0,
2056};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002057
Eric Biederman90089602004-05-28 14:11:54 +00002058/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002059 * from a triple. Having allows certain sanity checks to pass even
2060 * when the original triple that was pointed to is gone.
2061 */
Eric Biederman90089602004-05-28 14:11:54 +00002062static struct triple unknown_triple = {
2063 .next = &unknown_triple,
2064 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002065 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002066 .op = OP_UNKNOWNVAL,
2067 .lhs = 0,
2068 .rhs = 0,
2069 .misc = 0,
2070 .targ = 0,
2071 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002072 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002073 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002074 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002075 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002076};
2077
Eric Biederman0babc1c2003-05-09 02:39:00 +00002078
Eric Biederman90089602004-05-28 14:11:54 +00002079static size_t registers_of(struct compile_state *state, struct type *type);
2080
2081static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002082 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2083 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002084{
Eric Biederman90089602004-05-28 14:11:54 +00002085 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002086 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002087 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002088 dummy.op = op;
2089 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002090 valid_op(state, op);
2091 lhs = table_ops[op].lhs;
2092 rhs = table_ops[op].rhs;
2093 misc = table_ops[op].misc;
2094 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002095
2096 switch(op) {
2097 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002098 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002099 break;
2100 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002101 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002102 break;
2103 case OP_ADECL:
2104 lhs = registers_of(state, type);
2105 break;
2106 case OP_TUPLE:
2107 lhs = registers_of(state, type);
2108 break;
2109 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002110 rhs = rhs_wanted;
2111 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002112 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002113 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002114 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002115 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002116 }
2117 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002118 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002119 }
2120 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002121 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002122 }
2123 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002124 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002125 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002126
2127 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002128 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002129 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2130
2131 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2132 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002133 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002134 ret->lhs = lhs;
2135 ret->rhs = rhs;
2136 ret->misc = misc;
2137 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002138 ret->type = type;
2139 ret->next = ret;
2140 ret->prev = ret;
2141 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002142 /* A simple sanity check */
2143 if ((ret->op != op) ||
2144 (ret->lhs != lhs) ||
2145 (ret->rhs != rhs) ||
2146 (ret->misc != misc) ||
2147 (ret->targ != targ) ||
2148 (ret->type != type) ||
2149 (ret->next != ret) ||
2150 (ret->prev != ret) ||
2151 (ret->occurance != occurance)) {
2152 internal_error(state, ret, "huh?");
2153 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002154 return ret;
2155}
2156
Eric Biederman0babc1c2003-05-09 02:39:00 +00002157struct triple *dup_triple(struct compile_state *state, struct triple *src)
2158{
2159 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002160 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002161 src_lhs = src->lhs;
2162 src_rhs = src->rhs;
2163 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002164 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002165 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002166 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002167 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002168 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002169 return dup;
2170}
2171
Eric Biederman41203d92004-11-08 09:31:09 +00002172static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2173{
2174 struct triple *copy;
2175 copy = dup_triple(state, src);
2176 copy->use = 0;
2177 copy->next = copy->prev = copy;
2178 return copy;
2179}
2180
Eric Biederman0babc1c2003-05-09 02:39:00 +00002181static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002182 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002183{
2184 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002185 struct occurance *occurance;
2186 occurance = new_occurance(state);
2187 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002188 return ret;
2189}
2190
2191static struct triple *build_triple(struct compile_state *state,
2192 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002193 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002194{
2195 struct triple *ret;
2196 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002197 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002198 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002199 if (count > 0) {
2200 ret->param[0] = left;
2201 }
2202 if (count > 1) {
2203 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002204 }
2205 return ret;
2206}
2207
Eric Biederman0babc1c2003-05-09 02:39:00 +00002208static struct triple *triple(struct compile_state *state,
2209 int op, struct type *type, struct triple *left, struct triple *right)
2210{
2211 struct triple *ret;
2212 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002213 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002214 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002215 if (count >= 1) {
2216 ret->param[0] = left;
2217 }
2218 if (count >= 2) {
2219 ret->param[1] = right;
2220 }
2221 return ret;
2222}
2223
2224static struct triple *branch(struct compile_state *state,
2225 struct triple *targ, struct triple *test)
2226{
2227 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002228 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002229 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002230 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002231 } else {
2232 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002233 }
2234 TARG(ret, 0) = targ;
2235 /* record the branch target was used */
2236 if (!targ || (targ->op != OP_LABEL)) {
2237 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002238 }
2239 return ret;
2240}
2241
Eric Biederman90089602004-05-28 14:11:54 +00002242static int triple_is_label(struct compile_state *state, struct triple *ins);
2243static int triple_is_call(struct compile_state *state, struct triple *ins);
2244static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002245static void insert_triple(struct compile_state *state,
2246 struct triple *first, struct triple *ptr)
2247{
2248 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002249 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002250 internal_error(state, ptr, "expression already used");
2251 }
2252 ptr->next = first;
2253 ptr->prev = first->prev;
2254 ptr->prev->next = ptr;
2255 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002256
2257 if (triple_is_cbranch(state, ptr->prev) ||
2258 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002259 unuse_triple(first, ptr->prev);
2260 use_triple(ptr, ptr->prev);
2261 }
2262 }
2263}
2264
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002265static int triple_stores_block(struct compile_state *state, struct triple *ins)
2266{
2267 /* This function is used to determine if u.block
2268 * is utilized to store the current block number.
2269 */
2270 int stores_block;
2271 valid_ins(state, ins);
2272 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2273 return stores_block;
2274}
2275
Eric Biederman90089602004-05-28 14:11:54 +00002276static int triple_is_branch(struct compile_state *state, struct triple *ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002277static struct block *block_of_triple(struct compile_state *state,
2278 struct triple *ins)
2279{
2280 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002281 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002282 return 0;
2283 }
2284 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002285 while(ins != first && !triple_is_branch(state, ins->prev) &&
2286 !triple_stores_block(state, ins))
2287 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002288 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002289 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002290 }
2291 ins = ins->prev;
2292 }
Eric Biederman90089602004-05-28 14:11:54 +00002293 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002294}
2295
Eric Biederman90089602004-05-28 14:11:54 +00002296static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002297static struct triple *pre_triple(struct compile_state *state,
2298 struct triple *base,
2299 int op, struct type *type, struct triple *left, struct triple *right)
2300{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002301 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002302 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002303 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002304 /* If I am an OP_PIECE jump to the real instruction */
2305 if (base->op == OP_PIECE) {
2306 base = MISC(base, 0);
2307 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002308 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002309 get_occurance(base->occurance);
2310 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002311 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002312 if (triple_stores_block(state, ret)) {
2313 ret->u.block = block;
2314 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002315 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002316 for(i = 0; i < ret->lhs; i++) {
2317 struct triple *piece;
2318 piece = LHS(ret, i);
2319 insert_triple(state, base, piece);
2320 use_triple(ret, piece);
2321 use_triple(piece, ret);
2322 }
2323 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002324 block->first = ret;
2325 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002326 return ret;
2327}
2328
2329static struct triple *post_triple(struct compile_state *state,
2330 struct triple *base,
2331 int op, struct type *type, struct triple *left, struct triple *right)
2332{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002333 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002334 struct triple *ret, *next;
2335 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002336 /* If I am an OP_PIECE jump to the real instruction */
2337 if (base->op == OP_PIECE) {
2338 base = MISC(base, 0);
2339 }
2340 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002341 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002342 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002343 base = LHS(base, zlhs - 1);
2344 }
2345
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002346 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002347 get_occurance(base->occurance);
2348 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002349 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002350 if (triple_stores_block(state, ret)) {
2351 ret->u.block = block;
2352 }
Eric Biederman90089602004-05-28 14:11:54 +00002353 next = base->next;
2354 insert_triple(state, next, ret);
2355 zlhs = ret->lhs;
2356 for(i = 0; i < zlhs; i++) {
2357 struct triple *piece;
2358 piece = LHS(ret, i);
2359 insert_triple(state, next, piece);
2360 use_triple(ret, piece);
2361 use_triple(piece, ret);
2362 }
2363 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002364 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002365 if (zlhs) {
2366 block->last = LHS(ret, zlhs - 1);
2367 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002368 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002369 return ret;
2370}
2371
Eric Biederman90089602004-05-28 14:11:54 +00002372static struct type *reg_type(
2373 struct compile_state *state, struct type *type, int reg);
2374
2375static void generate_lhs_piece(
2376 struct compile_state *state, struct triple *ins, int index)
2377{
2378 struct type *piece_type;
2379 struct triple *piece;
2380 get_occurance(ins->occurance);
2381 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2382
2383 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2384 piece_type = piece_type->left;
2385 }
2386#if 0
2387{
2388 static void name_of(FILE *fp, struct type *type);
2389 FILE * fp = state->errout;
2390 fprintf(fp, "piece_type(%d): ", index);
2391 name_of(fp, piece_type);
2392 fprintf(fp, "\n");
2393}
2394#endif
2395 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2396 piece->u.cval = index;
2397 LHS(ins, piece->u.cval) = piece;
2398 MISC(piece, 0) = ins;
2399}
2400
2401static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2402{
2403 int i, zlhs;
2404 zlhs = ins->lhs;
2405 for(i = 0; i < zlhs; i++) {
2406 generate_lhs_piece(state, ins, i);
2407 }
2408}
2409
Eric Biedermanb138ac82003-04-22 18:44:01 +00002410static struct triple *label(struct compile_state *state)
2411{
2412 /* Labels don't get a type */
2413 struct triple *result;
2414 result = triple(state, OP_LABEL, &void_type, 0, 0);
2415 return result;
2416}
2417
Eric Biederman90089602004-05-28 14:11:54 +00002418static struct triple *mkprog(struct compile_state *state, ...)
2419{
2420 struct triple *prog, *head, *arg;
2421 va_list args;
2422 int i;
2423
2424 head = label(state);
2425 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2426 RHS(prog, 0) = head;
2427 va_start(args, state);
2428 i = 0;
2429 while((arg = va_arg(args, struct triple *)) != 0) {
2430 if (++i >= 100) {
2431 internal_error(state, 0, "too many arguments to mkprog");
2432 }
2433 flatten(state, head, arg);
2434 }
2435 va_end(args);
2436 prog->type = head->prev->type;
2437 return prog;
2438}
2439static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002440static void display_triple(FILE *fp, struct triple *ins)
2441{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002442 struct occurance *ptr;
2443 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002444 char pre, post, vol;
2445 pre = post = vol = ' ';
2446 if (ins) {
2447 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2448 pre = '^';
2449 }
2450 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2451 post = ',';
2452 }
2453 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2454 vol = 'v';
2455 }
2456 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002457 }
Eric Biederman90089602004-05-28 14:11:54 +00002458 if (ins == 0) {
2459 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002460 }
Eric Biederman90089602004-05-28 14:11:54 +00002461 else if (ins->op == OP_INTCONST) {
2462 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
2463 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002464 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002465 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002466 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002467 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2468 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2469 MISC(ins, 0), (unsigned long)(ins->u.cval));
2470 }
2471 else if (ins->op == OP_INDEX) {
2472 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2473 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2474 RHS(ins, 0), (unsigned long)(ins->u.cval));
2475 }
2476 else if (ins->op == OP_PIECE) {
2477 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2478 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002479 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002480 }
2481 else {
2482 int i, count;
Eric Biederman90089602004-05-28 14:11:54 +00002483 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
2484 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2485 if (table_ops[ins->op].flags & BITFIELD) {
2486 fprintf(fp, " <%2d-%2d:%2d>",
2487 ins->u.bitfield.offset,
2488 ins->u.bitfield.offset + ins->u.bitfield.size,
2489 ins->u.bitfield.size);
2490 }
2491 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002492 for(i = 0; i < count; i++) {
2493 fprintf(fp, " %-10p", ins->param[i]);
2494 }
2495 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002496 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002497 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002498 }
Eric Biederman90089602004-05-28 14:11:54 +00002499 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002500 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002501#if DEBUG_DISPLAY_TYPES
2502 fprintf(fp, " <");
2503 name_of(fp, ins->type);
2504 fprintf(fp, "> ");
2505#endif
2506#if DEBUG_DISPLAY_USES
2507 fprintf(fp, " [");
2508 for(user = ins->use; user; user = user->next) {
2509 fprintf(fp, " %-10p", user->member);
2510 }
2511 fprintf(fp, " ]");
2512#endif
2513 fprintf(fp, " @");
2514 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2515 fprintf(fp, " %s,%s:%d.%d",
2516 ptr->function,
2517 ptr->filename,
2518 ptr->line,
2519 ptr->col);
2520 }
2521 if (ins->op == OP_ASM) {
2522 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002523 }
2524 }
Eric Biederman90089602004-05-28 14:11:54 +00002525 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002526 fflush(fp);
2527}
2528
Eric Biederman90089602004-05-28 14:11:54 +00002529static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002530static void display_triple_changes(
2531 FILE *fp, const struct triple *new, const struct triple *orig)
2532{
2533
2534 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002535 new_count = TRIPLE_SIZE(new);
2536 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002537 if ((new->op != orig->op) ||
2538 (new_count != orig_count) ||
2539 (memcmp(orig->param, new->param,
2540 orig_count * sizeof(orig->param[0])) != 0) ||
2541 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
2542 {
2543 struct occurance *ptr;
2544 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002545 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002546 if (orig->op == new->op) {
2547 fprintf(fp, " %-11s", tops(orig->op));
2548 } else {
2549 fprintf(fp, " [%-10s %-10s]",
2550 tops(new->op), tops(orig->op));
2551 }
2552 min_count = new_count;
2553 if (min_count > orig_count) {
2554 min_count = orig_count;
2555 }
2556 for(indent = i = 0; i < min_count; i++) {
2557 if (orig->param[i] == new->param[i]) {
2558 fprintf(fp, " %-11p",
2559 orig->param[i]);
2560 indent += 12;
2561 } else {
2562 fprintf(fp, " [%-10p %-10p]",
2563 new->param[i],
2564 orig->param[i]);
2565 indent += 24;
2566 }
2567 }
2568 for(; i < orig_count; i++) {
2569 fprintf(fp, " [%-9p]", orig->param[i]);
2570 indent += 12;
2571 }
2572 for(; i < new_count; i++) {
2573 fprintf(fp, " [%-9p]", new->param[i]);
2574 indent += 12;
2575 }
2576 if ((new->op == OP_INTCONST)||
2577 (new->op == OP_ADDRCONST)) {
2578 fprintf(fp, " <0x%08lx>",
2579 (unsigned long)(new->u.cval));
2580 indent += 13;
2581 }
2582 for(;indent < 36; indent++) {
2583 putc(' ', fp);
2584 }
Eric Biederman90089602004-05-28 14:11:54 +00002585
2586#if DEBUG_DISPLAY_TYPES
2587 fprintf(fp, " <");
2588 name_of(fp, new->type);
2589 if (!equiv_types(new->type, orig->type)) {
2590 fprintf(fp, " -- ");
2591 name_of(fp, orig->type);
2592 }
2593 fprintf(fp, "> ");
2594#endif
2595
Eric Biederman5ade04a2003-10-22 04:03:46 +00002596 fprintf(fp, " @");
2597 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2598 fprintf(fp, " %s,%s:%d.%d",
2599 ptr->function,
2600 ptr->filename,
2601 ptr->line,
2602 ptr->col);
2603
2604 }
2605 fprintf(fp, "\n");
2606 fflush(fp);
2607 }
2608}
2609
Eric Biederman83b991a2003-10-11 06:20:25 +00002610static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002611{
2612 /* Does the triple have no side effects.
2613 * I.e. Rexecuting the triple with the same arguments
2614 * gives the same value.
2615 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002616 unsigned pure;
2617 valid_ins(state, ins);
2618 pure = PURE_BITS(table_ops[ins->op].flags);
2619 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002620 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002621 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002622 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002623 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002624}
2625
Eric Biederman90089602004-05-28 14:11:54 +00002626static int triple_is_branch_type(struct compile_state *state,
2627 struct triple *ins, unsigned type)
2628{
2629 /* Is this one of the passed branch types? */
2630 valid_ins(state, ins);
2631 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2632}
2633
Eric Biederman0babc1c2003-05-09 02:39:00 +00002634static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002635{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002636 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002637 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002638 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002639}
2640
Eric Biederman90089602004-05-28 14:11:54 +00002641static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002642{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002643 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002644 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002645}
2646
Eric Biederman90089602004-05-28 14:11:54 +00002647static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002648{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002649 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002650 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002651 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002652 type = BRANCH_BITS(table_ops[ins->op].flags);
2653 return (type != 0) && (type != CBRANCH);
2654}
2655
2656static int triple_is_call(struct compile_state *state, struct triple *ins)
2657{
2658 /* Is this triple a call instruction? */
2659 return triple_is_branch_type(state, ins, CALLBRANCH);
2660}
2661
2662static int triple_is_ret(struct compile_state *state, struct triple *ins)
2663{
2664 /* Is this triple a return instruction? */
2665 return triple_is_branch_type(state, ins, RETBRANCH);
2666}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002667
2668#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002669static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2670{
2671 /* Is this triple an unconditional branch and not a call or a
2672 * return? */
2673 return triple_is_branch_type(state, ins, UBRANCH);
2674}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002675#endif
Eric Biederman90089602004-05-28 14:11:54 +00002676
2677static int triple_is_end(struct compile_state *state, struct triple *ins)
2678{
2679 return triple_is_branch_type(state, ins, ENDBRANCH);
2680}
2681
2682static int triple_is_label(struct compile_state *state, struct triple *ins)
2683{
2684 valid_ins(state, ins);
2685 return (ins->op == OP_LABEL);
2686}
2687
2688static struct triple *triple_to_block_start(
2689 struct compile_state *state, struct triple *start)
2690{
2691 while(!triple_is_branch(state, start->prev) &&
2692 (!triple_is_label(state, start) || !start->use)) {
2693 start = start->prev;
2694 }
2695 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002696}
2697
Eric Biederman0babc1c2003-05-09 02:39:00 +00002698static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002699{
2700 /* This function is used to determine which triples need
2701 * a register.
2702 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002703 int is_def;
2704 valid_ins(state, ins);
2705 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002706 if (ins->lhs >= 1) {
2707 is_def = 0;
2708 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002709 return is_def;
2710}
2711
Eric Biederman83b991a2003-10-11 06:20:25 +00002712static int triple_is_structural(struct compile_state *state, struct triple *ins)
2713{
2714 int is_structural;
2715 valid_ins(state, ins);
2716 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2717 return is_structural;
2718}
2719
Eric Biederman90089602004-05-28 14:11:54 +00002720static int triple_is_part(struct compile_state *state, struct triple *ins)
2721{
2722 int is_part;
2723 valid_ins(state, ins);
2724 is_part = (table_ops[ins->op].flags & PART) == PART;
2725 return is_part;
2726}
2727
2728static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2729{
2730 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2731}
2732
Eric Biederman0babc1c2003-05-09 02:39:00 +00002733static struct triple **triple_iter(struct compile_state *state,
2734 size_t count, struct triple **vector,
2735 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002736{
2737 struct triple **ret;
2738 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002739 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002740 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002741 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002742 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002743 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002744 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002745 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002746 }
2747 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002748
Eric Biedermanb138ac82003-04-22 18:44:01 +00002749}
2750
2751static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002752 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002753{
Eric Biederman90089602004-05-28 14:11:54 +00002754 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002755 ins, last);
2756}
2757
2758static struct triple **triple_rhs(struct compile_state *state,
2759 struct triple *ins, struct triple **last)
2760{
Eric Biederman90089602004-05-28 14:11:54 +00002761 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002762 ins, last);
2763}
2764
2765static struct triple **triple_misc(struct compile_state *state,
2766 struct triple *ins, struct triple **last)
2767{
Eric Biederman90089602004-05-28 14:11:54 +00002768 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002769 ins, last);
2770}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002771
Eric Biederman90089602004-05-28 14:11:54 +00002772static struct triple **do_triple_targ(struct compile_state *state,
2773 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002774{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002775 size_t count;
2776 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002777 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002778 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002779 count = ins->targ;
2780 next_is_targ = 0;
2781 if (triple_is_cbranch(state, ins)) {
2782 next_is_targ = 1;
2783 }
2784 if (!call_edges && triple_is_call(state, ins)) {
2785 count = 0;
2786 }
2787 if (next_edges && triple_is_call(state, ins)) {
2788 next_is_targ = 1;
2789 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002790 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002791 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002792 if (!last) {
2793 ret = &ins->next;
2794 } else if (last == &ins->next) {
2795 last = 0;
2796 }
2797 }
2798 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002799 if (!last) {
2800 ret = vector;
2801 }
2802 else if ((last >= vector) && (last < (vector + count - 1))) {
2803 ret = last + 1;
2804 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002805 else if (last == vector + count - 1) {
2806 last = 0;
2807 }
2808 }
Eric Biederman90089602004-05-28 14:11:54 +00002809 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002810 struct triple_set *use;
2811 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002812 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002813 continue;
2814 }
2815 if (!last) {
2816 ret = &use->member->next;
2817 break;
2818 }
2819 else if (last == &use->member->next) {
2820 last = 0;
2821 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002822 }
2823 }
2824 return ret;
2825}
2826
Eric Biederman90089602004-05-28 14:11:54 +00002827static struct triple **triple_targ(struct compile_state *state,
2828 struct triple *ins, struct triple **last)
2829{
2830 return do_triple_targ(state, ins, last, 1, 1);
2831}
2832
2833static struct triple **triple_edge_targ(struct compile_state *state,
2834 struct triple *ins, struct triple **last)
2835{
2836 return do_triple_targ(state, ins, last,
2837 state->functions_joined, !state->functions_joined);
2838}
2839
2840static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2841{
2842 struct triple *next;
2843 int lhs, i;
2844 lhs = ins->lhs;
2845 next = ins->next;
2846 for(i = 0; i < lhs; i++) {
2847 struct triple *piece;
2848 piece = LHS(ins, i);
2849 if (next != piece) {
2850 internal_error(state, ins, "malformed lhs on %s",
2851 tops(ins->op));
2852 }
2853 if (next->op != OP_PIECE) {
2854 internal_error(state, ins, "bad lhs op %s at %d on %s",
2855 tops(next->op), i, tops(ins->op));
2856 }
2857 if (next->u.cval != i) {
2858 internal_error(state, ins, "bad u.cval of %d %d expected",
2859 next->u.cval, i);
2860 }
2861 next = next->next;
2862 }
2863 return next;
2864}
2865
2866/* Function piece accessor functions */
2867static struct triple *do_farg(struct compile_state *state,
2868 struct triple *func, unsigned index)
2869{
2870 struct type *ftype;
2871 struct triple *first, *arg;
2872 unsigned i;
2873
2874 ftype = func->type;
2875 if((index < 0) || (index >= (ftype->elements + 2))) {
2876 internal_error(state, func, "bad argument index: %d", index);
2877 }
2878 first = RHS(func, 0);
2879 arg = first->next;
2880 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2881 /* do nothing */
2882 }
2883 if (arg->op != OP_ADECL) {
2884 internal_error(state, 0, "arg not adecl?");
2885 }
2886 return arg;
2887}
2888static struct triple *fresult(struct compile_state *state, struct triple *func)
2889{
2890 return do_farg(state, func, 0);
2891}
2892static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2893{
2894 return do_farg(state, func, 1);
2895}
2896static struct triple *farg(struct compile_state *state,
2897 struct triple *func, unsigned index)
2898{
2899 return do_farg(state, func, index + 2);
2900}
2901
2902
2903static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2904{
2905 struct triple *first, *ins;
2906 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2907 first = ins = RHS(func, 0);
2908 do {
2909 if (triple_is_label(state, ins) && ins->use) {
2910 fprintf(fp, "%p:\n", ins);
2911 }
2912 display_triple(fp, ins);
2913
2914 if (triple_is_branch(state, ins)) {
2915 fprintf(fp, "\n");
2916 }
2917 if (ins->next->prev != ins) {
2918 internal_error(state, ins->next, "bad prev");
2919 }
2920 ins = ins->next;
2921 } while(ins != first);
2922}
2923
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002924static void verify_use(struct compile_state *state,
2925 struct triple *user, struct triple *used)
2926{
2927 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002928 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002929 for(i = 0; i < size; i++) {
2930 if (user->param[i] == used) {
2931 break;
2932 }
2933 }
2934 if (triple_is_branch(state, user)) {
2935 if (user->next == used) {
2936 i = -1;
2937 }
2938 }
2939 if (i == size) {
2940 internal_error(state, user, "%s(%p) does not use %s(%p)",
2941 tops(user->op), user, tops(used->op), used);
2942 }
2943}
2944
2945static int find_rhs_use(struct compile_state *state,
2946 struct triple *user, struct triple *used)
2947{
2948 struct triple **param;
2949 int size, i;
2950 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002951
2952#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002953#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002954#endif
Eric Biederman90089602004-05-28 14:11:54 +00002955 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002956 param = &RHS(user, 0);
2957 for(i = 0; i < size; i++) {
2958 if (param[i] == used) {
2959 return i;
2960 }
2961 }
2962 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002963}
2964
2965static void free_triple(struct compile_state *state, struct triple *ptr)
2966{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002967 size_t size;
2968 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002969 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002970 ptr->prev->next = ptr->next;
2971 ptr->next->prev = ptr->prev;
2972 if (ptr->use) {
2973 internal_error(state, ptr, "ptr->use != 0");
2974 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002975 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002976 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002977 xfree(ptr);
2978}
2979
2980static void release_triple(struct compile_state *state, struct triple *ptr)
2981{
2982 struct triple_set *set, *next;
2983 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002984 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002985 if (ptr == &unknown_triple) {
2986 return;
2987 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002988 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002989 /* Make certain the we are not the first or last element of a block */
2990 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002991 if (block) {
2992 if ((block->last == ptr) && (block->first == ptr)) {
2993 block->last = block->first = 0;
2994 }
2995 else if (block->last == ptr) {
2996 block->last = ptr->prev;
2997 }
2998 else if (block->first == ptr) {
2999 block->first = ptr->next;
3000 }
Eric Biederman66fe2222003-07-04 15:14:04 +00003001 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003002 /* Remove ptr from use chains where it is the user */
3003 expr = triple_rhs(state, ptr, 0);
3004 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3005 if (*expr) {
3006 unuse_triple(*expr, ptr);
3007 }
3008 }
3009 expr = triple_lhs(state, ptr, 0);
3010 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3011 if (*expr) {
3012 unuse_triple(*expr, ptr);
3013 }
3014 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003015 expr = triple_misc(state, ptr, 0);
3016 for(; expr; expr = triple_misc(state, ptr, expr)) {
3017 if (*expr) {
3018 unuse_triple(*expr, ptr);
3019 }
3020 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003021 expr = triple_targ(state, ptr, 0);
3022 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003023 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003024 unuse_triple(*expr, ptr);
3025 }
3026 }
3027 /* Reomve ptr from use chains where it is used */
3028 for(set = ptr->use; set; set = next) {
3029 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003030 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003031 expr = triple_rhs(state, set->member, 0);
3032 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3033 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003034 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003035 }
3036 }
3037 expr = triple_lhs(state, set->member, 0);
3038 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3039 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003040 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003041 }
3042 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003043 expr = triple_misc(state, set->member, 0);
3044 for(; expr; expr = triple_misc(state, set->member, expr)) {
3045 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003046 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003047 }
3048 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003049 expr = triple_targ(state, set->member, 0);
3050 for(; expr; expr = triple_targ(state, set->member, expr)) {
3051 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003052 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003053 }
3054 }
3055 unuse_triple(ptr, set->member);
3056 }
3057 free_triple(state, ptr);
3058}
3059
Eric Biederman5ade04a2003-10-22 04:03:46 +00003060static void print_triples(struct compile_state *state);
3061static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003062
Jason Schildt27b85112005-08-10 14:31:52 +00003063#define TOK_UNKNOWN 0
3064#define TOK_SPACE 1
3065#define TOK_SEMI 2
3066#define TOK_LBRACE 3
3067#define TOK_RBRACE 4
3068#define TOK_COMMA 5
3069#define TOK_EQ 6
3070#define TOK_COLON 7
3071#define TOK_LBRACKET 8
3072#define TOK_RBRACKET 9
3073#define TOK_LPAREN 10
3074#define TOK_RPAREN 11
3075#define TOK_STAR 12
3076#define TOK_DOTS 13
3077#define TOK_MORE 14
3078#define TOK_LESS 15
3079#define TOK_TIMESEQ 16
3080#define TOK_DIVEQ 17
3081#define TOK_MODEQ 18
3082#define TOK_PLUSEQ 19
3083#define TOK_MINUSEQ 20
3084#define TOK_SLEQ 21
3085#define TOK_SREQ 22
3086#define TOK_ANDEQ 23
3087#define TOK_XOREQ 24
3088#define TOK_OREQ 25
3089#define TOK_EQEQ 26
3090#define TOK_NOTEQ 27
3091#define TOK_QUEST 28
3092#define TOK_LOGOR 29
3093#define TOK_LOGAND 30
3094#define TOK_OR 31
3095#define TOK_AND 32
3096#define TOK_XOR 33
3097#define TOK_LESSEQ 34
3098#define TOK_MOREEQ 35
3099#define TOK_SL 36
3100#define TOK_SR 37
3101#define TOK_PLUS 38
3102#define TOK_MINUS 39
3103#define TOK_DIV 40
3104#define TOK_MOD 41
3105#define TOK_PLUSPLUS 42
3106#define TOK_MINUSMINUS 43
3107#define TOK_BANG 44
3108#define TOK_ARROW 45
3109#define TOK_DOT 46
3110#define TOK_TILDE 47
3111#define TOK_LIT_STRING 48
3112#define TOK_LIT_CHAR 49
3113#define TOK_LIT_INT 50
3114#define TOK_LIT_FLOAT 51
3115#define TOK_MACRO 52
3116#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003117
Jason Schildt27b85112005-08-10 14:31:52 +00003118#define TOK_IDENT 54
3119#define TOK_STRUCT_NAME 55
3120#define TOK_ENUM_CONST 56
3121#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003122
Jason Schildt27b85112005-08-10 14:31:52 +00003123#define TOK_AUTO 58
3124#define TOK_BREAK 59
3125#define TOK_CASE 60
3126#define TOK_CHAR 61
3127#define TOK_CONST 62
3128#define TOK_CONTINUE 63
3129#define TOK_DEFAULT 64
3130#define TOK_DO 65
3131#define TOK_DOUBLE 66
3132#define TOK_ELSE 67
3133#define TOK_ENUM 68
3134#define TOK_EXTERN 69
3135#define TOK_FLOAT 70
3136#define TOK_FOR 71
3137#define TOK_GOTO 72
3138#define TOK_IF 73
3139#define TOK_INLINE 74
3140#define TOK_INT 75
3141#define TOK_LONG 76
3142#define TOK_REGISTER 77
3143#define TOK_RESTRICT 78
3144#define TOK_RETURN 79
3145#define TOK_SHORT 80
3146#define TOK_SIGNED 81
3147#define TOK_SIZEOF 82
3148#define TOK_STATIC 83
3149#define TOK_STRUCT 84
3150#define TOK_SWITCH 85
3151#define TOK_TYPEDEF 86
3152#define TOK_UNION 87
3153#define TOK_UNSIGNED 88
3154#define TOK_VOID 89
3155#define TOK_VOLATILE 90
3156#define TOK_WHILE 91
3157#define TOK_ASM 92
3158#define TOK_ATTRIBUTE 93
3159#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003160#define TOK_FIRST_KEYWORD TOK_AUTO
3161#define TOK_LAST_KEYWORD TOK_ALIGNOF
3162
Eric Biedermancb364952004-11-15 10:46:44 +00003163#define TOK_MDEFINE 100
3164#define TOK_MDEFINED 101
3165#define TOK_MUNDEF 102
3166#define TOK_MINCLUDE 103
3167#define TOK_MLINE 104
3168#define TOK_MERROR 105
3169#define TOK_MWARNING 106
3170#define TOK_MPRAGMA 107
3171#define TOK_MIFDEF 108
3172#define TOK_MIFNDEF 109
3173#define TOK_MELIF 110
3174#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003175
Eric Biedermancb364952004-11-15 10:46:44 +00003176#define TOK_FIRST_MACRO TOK_MDEFINE
3177#define TOK_LAST_MACRO TOK_MENDIF
Eric Biedermanb138ac82003-04-22 18:44:01 +00003178
Eric Biedermancb364952004-11-15 10:46:44 +00003179#define TOK_MIF 112
3180#define TOK_MELSE 113
3181#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003182
Eric Biedermancb364952004-11-15 10:46:44 +00003183#define TOK_EOL 115
3184#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003185
3186static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003187[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003188[TOK_SPACE ] = ":space:",
3189[TOK_SEMI ] = ";",
3190[TOK_LBRACE ] = "{",
3191[TOK_RBRACE ] = "}",
3192[TOK_COMMA ] = ",",
3193[TOK_EQ ] = "=",
3194[TOK_COLON ] = ":",
3195[TOK_LBRACKET ] = "[",
3196[TOK_RBRACKET ] = "]",
3197[TOK_LPAREN ] = "(",
3198[TOK_RPAREN ] = ")",
3199[TOK_STAR ] = "*",
3200[TOK_DOTS ] = "...",
3201[TOK_MORE ] = ">",
3202[TOK_LESS ] = "<",
3203[TOK_TIMESEQ ] = "*=",
3204[TOK_DIVEQ ] = "/=",
3205[TOK_MODEQ ] = "%=",
3206[TOK_PLUSEQ ] = "+=",
3207[TOK_MINUSEQ ] = "-=",
3208[TOK_SLEQ ] = "<<=",
3209[TOK_SREQ ] = ">>=",
3210[TOK_ANDEQ ] = "&=",
3211[TOK_XOREQ ] = "^=",
3212[TOK_OREQ ] = "|=",
3213[TOK_EQEQ ] = "==",
3214[TOK_NOTEQ ] = "!=",
3215[TOK_QUEST ] = "?",
3216[TOK_LOGOR ] = "||",
3217[TOK_LOGAND ] = "&&",
3218[TOK_OR ] = "|",
3219[TOK_AND ] = "&",
3220[TOK_XOR ] = "^",
3221[TOK_LESSEQ ] = "<=",
3222[TOK_MOREEQ ] = ">=",
3223[TOK_SL ] = "<<",
3224[TOK_SR ] = ">>",
3225[TOK_PLUS ] = "+",
3226[TOK_MINUS ] = "-",
3227[TOK_DIV ] = "/",
3228[TOK_MOD ] = "%",
3229[TOK_PLUSPLUS ] = "++",
3230[TOK_MINUSMINUS ] = "--",
3231[TOK_BANG ] = "!",
3232[TOK_ARROW ] = "->",
3233[TOK_DOT ] = ".",
3234[TOK_TILDE ] = "~",
3235[TOK_LIT_STRING ] = ":string:",
3236[TOK_IDENT ] = ":ident:",
3237[TOK_TYPE_NAME ] = ":typename:",
3238[TOK_LIT_CHAR ] = ":char:",
3239[TOK_LIT_INT ] = ":integer:",
3240[TOK_LIT_FLOAT ] = ":float:",
3241[TOK_MACRO ] = "#",
3242[TOK_CONCATENATE ] = "##",
3243
3244[TOK_AUTO ] = "auto",
3245[TOK_BREAK ] = "break",
3246[TOK_CASE ] = "case",
3247[TOK_CHAR ] = "char",
3248[TOK_CONST ] = "const",
3249[TOK_CONTINUE ] = "continue",
3250[TOK_DEFAULT ] = "default",
3251[TOK_DO ] = "do",
3252[TOK_DOUBLE ] = "double",
3253[TOK_ELSE ] = "else",
3254[TOK_ENUM ] = "enum",
3255[TOK_EXTERN ] = "extern",
3256[TOK_FLOAT ] = "float",
3257[TOK_FOR ] = "for",
3258[TOK_GOTO ] = "goto",
3259[TOK_IF ] = "if",
3260[TOK_INLINE ] = "inline",
3261[TOK_INT ] = "int",
3262[TOK_LONG ] = "long",
3263[TOK_REGISTER ] = "register",
3264[TOK_RESTRICT ] = "restrict",
3265[TOK_RETURN ] = "return",
3266[TOK_SHORT ] = "short",
3267[TOK_SIGNED ] = "signed",
3268[TOK_SIZEOF ] = "sizeof",
3269[TOK_STATIC ] = "static",
3270[TOK_STRUCT ] = "struct",
3271[TOK_SWITCH ] = "switch",
3272[TOK_TYPEDEF ] = "typedef",
3273[TOK_UNION ] = "union",
3274[TOK_UNSIGNED ] = "unsigned",
3275[TOK_VOID ] = "void",
3276[TOK_VOLATILE ] = "volatile",
3277[TOK_WHILE ] = "while",
3278[TOK_ASM ] = "asm",
3279[TOK_ATTRIBUTE ] = "__attribute__",
3280[TOK_ALIGNOF ] = "__alignof__",
3281
Eric Biederman41203d92004-11-08 09:31:09 +00003282[TOK_MDEFINE ] = "#define",
3283[TOK_MDEFINED ] = "#defined",
3284[TOK_MUNDEF ] = "#undef",
3285[TOK_MINCLUDE ] = "#include",
3286[TOK_MLINE ] = "#line",
3287[TOK_MERROR ] = "#error",
3288[TOK_MWARNING ] = "#warning",
3289[TOK_MPRAGMA ] = "#pragma",
3290[TOK_MIFDEF ] = "#ifdef",
3291[TOK_MIFNDEF ] = "#ifndef",
3292[TOK_MELIF ] = "#elif",
3293[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003294
Eric Biederman41203d92004-11-08 09:31:09 +00003295[TOK_MIF ] = "#if",
3296[TOK_MELSE ] = "#else",
3297[TOK_MIDENT ] = "#:ident:",
3298[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003299[TOK_EOF ] = "EOF",
3300};
3301
3302static unsigned int hash(const char *str, int str_len)
3303{
3304 unsigned int hash;
3305 const char *end;
3306 end = str + str_len;
3307 hash = 0;
3308 for(; str < end; str++) {
3309 hash = (hash *263) + *str;
3310 }
3311 hash = hash & (HASH_TABLE_SIZE -1);
3312 return hash;
3313}
3314
3315static struct hash_entry *lookup(
3316 struct compile_state *state, const char *name, int name_len)
3317{
3318 struct hash_entry *entry;
3319 unsigned int index;
3320 index = hash(name, name_len);
3321 entry = state->hash_table[index];
3322 while(entry &&
3323 ((entry->name_len != name_len) ||
3324 (memcmp(entry->name, name, name_len) != 0))) {
3325 entry = entry->next;
3326 }
3327 if (!entry) {
3328 char *new_name;
3329 /* Get a private copy of the name */
3330 new_name = xmalloc(name_len + 1, "hash_name");
3331 memcpy(new_name, name, name_len);
3332 new_name[name_len] = '\0';
3333
3334 /* Create a new hash entry */
3335 entry = xcmalloc(sizeof(*entry), "hash_entry");
3336 entry->next = state->hash_table[index];
3337 entry->name = new_name;
3338 entry->name_len = name_len;
3339
3340 /* Place the new entry in the hash table */
3341 state->hash_table[index] = entry;
3342 }
3343 return entry;
3344}
3345
3346static void ident_to_keyword(struct compile_state *state, struct token *tk)
3347{
3348 struct hash_entry *entry;
3349 entry = tk->ident;
3350 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3351 (entry->tok == TOK_ENUM_CONST) ||
3352 ((entry->tok >= TOK_FIRST_KEYWORD) &&
3353 (entry->tok <= TOK_LAST_KEYWORD)))) {
3354 tk->tok = entry->tok;
3355 }
3356}
3357
3358static void ident_to_macro(struct compile_state *state, struct token *tk)
3359{
3360 struct hash_entry *entry;
3361 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003362 if (!entry)
3363 return;
3364 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003365 tk->tok = entry->tok;
3366 }
Eric Biederman41203d92004-11-08 09:31:09 +00003367 else if (entry->tok == TOK_IF) {
3368 tk->tok = TOK_MIF;
3369 }
3370 else if (entry->tok == TOK_ELSE) {
3371 tk->tok = TOK_MELSE;
3372 }
3373 else {
3374 tk->tok = TOK_MIDENT;
3375 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003376}
3377
3378static void hash_keyword(
3379 struct compile_state *state, const char *keyword, int tok)
3380{
3381 struct hash_entry *entry;
3382 entry = lookup(state, keyword, strlen(keyword));
3383 if (entry && entry->tok != TOK_UNKNOWN) {
3384 die("keyword %s already hashed", keyword);
3385 }
3386 entry->tok = tok;
3387}
3388
Eric Biederman90089602004-05-28 14:11:54 +00003389static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003390 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003391 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003392{
3393 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003394 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003395 error(state, 0, "%s already defined", ident->name);
3396 }
3397 sym = xcmalloc(sizeof(*sym), "symbol");
3398 sym->ident = ident;
3399 sym->def = def;
3400 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003401 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003402 sym->next = *chain;
3403 *chain = sym;
3404}
3405
Eric Biederman90089602004-05-28 14:11:54 +00003406static void symbol(
3407 struct compile_state *state, struct hash_entry *ident,
3408 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003409{
Eric Biederman90089602004-05-28 14:11:54 +00003410 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3411}
3412
3413static void var_symbol(struct compile_state *state,
3414 struct hash_entry *ident, struct triple *def)
3415{
3416 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3417 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003418 }
Eric Biederman90089602004-05-28 14:11:54 +00003419 symbol(state, ident, &ident->sym_ident, def, def->type);
3420}
3421
3422static void label_symbol(struct compile_state *state,
3423 struct hash_entry *ident, struct triple *label, int depth)
3424{
3425 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003426}
3427
Eric Biedermanb138ac82003-04-22 18:44:01 +00003428static void start_scope(struct compile_state *state)
3429{
3430 state->scope_depth++;
3431}
3432
Eric Biederman90089602004-05-28 14:11:54 +00003433static void end_scope_syms(struct compile_state *state,
3434 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003435{
3436 struct symbol *sym, *next;
3437 sym = *chain;
3438 while(sym && (sym->scope_depth == depth)) {
3439 next = sym->next;
3440 xfree(sym);
3441 sym = next;
3442 }
3443 *chain = sym;
3444}
3445
3446static void end_scope(struct compile_state *state)
3447{
3448 int i;
3449 int depth;
3450 /* Walk through the hash table and remove all symbols
3451 * in the current scope.
3452 */
3453 depth = state->scope_depth;
3454 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3455 struct hash_entry *entry;
3456 entry = state->hash_table[i];
3457 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003458 end_scope_syms(state, &entry->sym_label, depth);
3459 end_scope_syms(state, &entry->sym_tag, depth);
3460 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003461 entry = entry->next;
3462 }
3463 }
3464 state->scope_depth = depth - 1;
3465}
3466
3467static void register_keywords(struct compile_state *state)
3468{
3469 hash_keyword(state, "auto", TOK_AUTO);
3470 hash_keyword(state, "break", TOK_BREAK);
3471 hash_keyword(state, "case", TOK_CASE);
3472 hash_keyword(state, "char", TOK_CHAR);
3473 hash_keyword(state, "const", TOK_CONST);
3474 hash_keyword(state, "continue", TOK_CONTINUE);
3475 hash_keyword(state, "default", TOK_DEFAULT);
3476 hash_keyword(state, "do", TOK_DO);
3477 hash_keyword(state, "double", TOK_DOUBLE);
3478 hash_keyword(state, "else", TOK_ELSE);
3479 hash_keyword(state, "enum", TOK_ENUM);
3480 hash_keyword(state, "extern", TOK_EXTERN);
3481 hash_keyword(state, "float", TOK_FLOAT);
3482 hash_keyword(state, "for", TOK_FOR);
3483 hash_keyword(state, "goto", TOK_GOTO);
3484 hash_keyword(state, "if", TOK_IF);
3485 hash_keyword(state, "inline", TOK_INLINE);
3486 hash_keyword(state, "int", TOK_INT);
3487 hash_keyword(state, "long", TOK_LONG);
3488 hash_keyword(state, "register", TOK_REGISTER);
3489 hash_keyword(state, "restrict", TOK_RESTRICT);
3490 hash_keyword(state, "return", TOK_RETURN);
3491 hash_keyword(state, "short", TOK_SHORT);
3492 hash_keyword(state, "signed", TOK_SIGNED);
3493 hash_keyword(state, "sizeof", TOK_SIZEOF);
3494 hash_keyword(state, "static", TOK_STATIC);
3495 hash_keyword(state, "struct", TOK_STRUCT);
3496 hash_keyword(state, "switch", TOK_SWITCH);
3497 hash_keyword(state, "typedef", TOK_TYPEDEF);
3498 hash_keyword(state, "union", TOK_UNION);
3499 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3500 hash_keyword(state, "void", TOK_VOID);
3501 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003502 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003503 hash_keyword(state, "while", TOK_WHILE);
3504 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003505 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003506 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3507 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3508}
3509
3510static void register_macro_keywords(struct compile_state *state)
3511{
Eric Biederman41203d92004-11-08 09:31:09 +00003512 hash_keyword(state, "define", TOK_MDEFINE);
3513 hash_keyword(state, "defined", TOK_MDEFINED);
3514 hash_keyword(state, "undef", TOK_MUNDEF);
3515 hash_keyword(state, "include", TOK_MINCLUDE);
3516 hash_keyword(state, "line", TOK_MLINE);
3517 hash_keyword(state, "error", TOK_MERROR);
3518 hash_keyword(state, "warning", TOK_MWARNING);
3519 hash_keyword(state, "pragma", TOK_MPRAGMA);
3520 hash_keyword(state, "ifdef", TOK_MIFDEF);
3521 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3522 hash_keyword(state, "elif", TOK_MELIF);
3523 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003524}
3525
Eric Biederman90089602004-05-28 14:11:54 +00003526
3527static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3528{
3529 if (ident->sym_define != 0) {
3530 struct macro *macro;
3531 struct macro_arg *arg, *anext;
3532 macro = ident->sym_define;
3533 ident->sym_define = 0;
3534
3535 /* Free the macro arguments... */
3536 anext = macro->args;
3537 while(anext) {
3538 arg = anext;
3539 anext = arg->next;
3540 xfree(arg);
3541 }
3542
3543 /* Free the macro buffer */
3544 xfree(macro->buf);
3545
3546 /* Now free the macro itself */
3547 xfree(macro);
3548 }
3549}
3550
Eric Biedermancb364952004-11-15 10:46:44 +00003551static void do_define_macro(struct compile_state *state,
3552 struct hash_entry *ident, const char *body,
3553 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003554{
3555 struct macro *macro;
3556 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003557 size_t body_len;
3558
3559 /* Find the length of the body */
3560 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003561 macro = ident->sym_define;
3562 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003563 int identical_bodies, identical_args;
3564 struct macro_arg *oarg;
3565 /* Explicitly allow identical redfinitions of the same macro */
3566 identical_bodies =
3567 (macro->buf_len == body_len) &&
3568 (memcmp(macro->buf, body, body_len) == 0);
3569 identical_args = macro->argc == argc;
3570 oarg = macro->args;
3571 arg = args;
3572 while(identical_args && arg) {
3573 identical_args = oarg->ident == arg->ident;
3574 arg = arg->next;
3575 oarg = oarg->next;
3576 }
3577 if (identical_bodies && identical_args) {
3578 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003579 return;
3580 }
3581 error(state, 0, "macro %s already defined\n", ident->name);
3582 }
3583#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003584 fprintf(state->errout, "#define %s: `%*.*s'\n",
3585 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003586#endif
3587 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003588 macro->ident = ident;
3589 macro->buf = body;
3590 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003591 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003592 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003593
3594 ident->sym_define = macro;
3595}
Eric Biedermancb364952004-11-15 10:46:44 +00003596
3597static void define_macro(
3598 struct compile_state *state,
3599 struct hash_entry *ident,
3600 const char *body, int body_len,
3601 int argc, struct macro_arg *args)
3602{
3603 char *buf;
3604 buf = xmalloc(body_len + 1, "macro buf");
3605 memcpy(buf, body, body_len);
3606 buf[body_len] = '\0';
3607 do_define_macro(state, ident, buf, argc, args);
3608}
Eric Biederman90089602004-05-28 14:11:54 +00003609
3610static void register_builtin_macro(struct compile_state *state,
3611 const char *name, const char *value)
3612{
3613 struct hash_entry *ident;
3614
3615 if (value[0] == '(') {
3616 internal_error(state, 0, "Builtin macros with arguments not supported");
3617 }
3618 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003619 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003620}
3621
3622static void register_builtin_macros(struct compile_state *state)
3623{
3624 char buf[30];
3625 char scratch[30];
3626 time_t now;
3627 struct tm *tm;
3628 now = time(NULL);
3629 tm = localtime(&now);
3630
3631 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3632 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3633 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3634 register_builtin_macro(state, "__LINE__", "54321");
3635
3636 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3637 sprintf(buf, "\"%s\"", scratch);
3638 register_builtin_macro(state, "__DATE__", buf);
3639
3640 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3641 sprintf(buf, "\"%s\"", scratch);
3642 register_builtin_macro(state, "__TIME__", buf);
3643
3644 /* I can't be a conforming implementation of C :( */
3645 register_builtin_macro(state, "__STDC__", "0");
3646 /* In particular I don't conform to C99 */
3647 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
3648
3649}
3650
3651static void process_cmdline_macros(struct compile_state *state)
3652{
3653 const char **macro, *name;
3654 struct hash_entry *ident;
3655 for(macro = state->compiler->defines; (name = *macro); macro++) {
3656 const char *body;
3657 size_t name_len;
3658
3659 name_len = strlen(name);
3660 body = strchr(name, '=');
3661 if (!body) {
3662 body = "\0";
3663 } else {
3664 name_len = body - name;
3665 body++;
3666 }
3667 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003668 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003669 }
3670 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3671 ident = lookup(state, name, strlen(name));
3672 undef_macro(state, ident);
3673 }
3674}
3675
Eric Biedermanb138ac82003-04-22 18:44:01 +00003676static int spacep(int c)
3677{
3678 int ret = 0;
3679 switch(c) {
3680 case ' ':
3681 case '\t':
3682 case '\f':
3683 case '\v':
3684 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003685 ret = 1;
3686 break;
3687 }
3688 return ret;
3689}
3690
Eric Biedermanb138ac82003-04-22 18:44:01 +00003691static int digitp(int c)
3692{
3693 int ret = 0;
3694 switch(c) {
3695 case '0': case '1': case '2': case '3': case '4':
3696 case '5': case '6': case '7': case '8': case '9':
3697 ret = 1;
3698 break;
3699 }
3700 return ret;
3701}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003702static int digval(int c)
3703{
3704 int val = -1;
3705 if ((c >= '0') && (c <= '9')) {
3706 val = c - '0';
3707 }
3708 return val;
3709}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003710
3711static int hexdigitp(int c)
3712{
3713 int ret = 0;
3714 switch(c) {
3715 case '0': case '1': case '2': case '3': case '4':
3716 case '5': case '6': case '7': case '8': case '9':
3717 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3718 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3719 ret = 1;
3720 break;
3721 }
3722 return ret;
3723}
3724static int hexdigval(int c)
3725{
3726 int val = -1;
3727 if ((c >= '0') && (c <= '9')) {
3728 val = c - '0';
3729 }
3730 else if ((c >= 'A') && (c <= 'F')) {
3731 val = 10 + (c - 'A');
3732 }
3733 else if ((c >= 'a') && (c <= 'f')) {
3734 val = 10 + (c - 'a');
3735 }
3736 return val;
3737}
3738
3739static int octdigitp(int c)
3740{
3741 int ret = 0;
3742 switch(c) {
3743 case '0': case '1': case '2': case '3':
3744 case '4': case '5': case '6': case '7':
3745 ret = 1;
3746 break;
3747 }
3748 return ret;
3749}
3750static int octdigval(int c)
3751{
3752 int val = -1;
3753 if ((c >= '0') && (c <= '7')) {
3754 val = c - '0';
3755 }
3756 return val;
3757}
3758
3759static int letterp(int c)
3760{
3761 int ret = 0;
3762 switch(c) {
3763 case 'a': case 'b': case 'c': case 'd': case 'e':
3764 case 'f': case 'g': case 'h': case 'i': case 'j':
3765 case 'k': case 'l': case 'm': case 'n': case 'o':
3766 case 'p': case 'q': case 'r': case 's': case 't':
3767 case 'u': case 'v': case 'w': case 'x': case 'y':
3768 case 'z':
3769 case 'A': case 'B': case 'C': case 'D': case 'E':
3770 case 'F': case 'G': case 'H': case 'I': case 'J':
3771 case 'K': case 'L': case 'M': case 'N': case 'O':
3772 case 'P': case 'Q': case 'R': case 'S': case 'T':
3773 case 'U': case 'V': case 'W': case 'X': case 'Y':
3774 case 'Z':
3775 case '_':
3776 ret = 1;
3777 break;
3778 }
3779 return ret;
3780}
3781
Eric Biederman90089602004-05-28 14:11:54 +00003782static const char *identifier(const char *str, const char *end)
3783{
3784 if (letterp(*str)) {
3785 for(; str < end; str++) {
3786 int c;
3787 c = *str;
3788 if (!letterp(c) && !digitp(c)) {
3789 break;
3790 }
3791 }
3792 }
3793 return str;
3794}
3795
Eric Biedermanb138ac82003-04-22 18:44:01 +00003796static int char_value(struct compile_state *state,
3797 const signed char **strp, const signed char *end)
3798{
3799 const signed char *str;
3800 int c;
3801 str = *strp;
3802 c = *str++;
3803 if ((c == '\\') && (str < end)) {
3804 switch(*str) {
3805 case 'n': c = '\n'; str++; break;
3806 case 't': c = '\t'; str++; break;
3807 case 'v': c = '\v'; str++; break;
3808 case 'b': c = '\b'; str++; break;
3809 case 'r': c = '\r'; str++; break;
3810 case 'f': c = '\f'; str++; break;
3811 case 'a': c = '\a'; str++; break;
3812 case '\\': c = '\\'; str++; break;
3813 case '?': c = '?'; str++; break;
3814 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003815 case '"': c = '"'; str++; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003816 case 'x':
3817 c = 0;
3818 str++;
3819 while((str < end) && hexdigitp(*str)) {
3820 c <<= 4;
3821 c += hexdigval(*str);
3822 str++;
3823 }
3824 break;
3825 case '0': case '1': case '2': case '3':
3826 case '4': case '5': case '6': case '7':
3827 c = 0;
3828 while((str < end) && octdigitp(*str)) {
3829 c <<= 3;
3830 c += octdigval(*str);
3831 str++;
3832 }
3833 break;
3834 default:
3835 error(state, 0, "Invalid character constant");
3836 break;
3837 }
3838 }
3839 *strp = str;
3840 return c;
3841}
3842
Eric Biedermancb364952004-11-15 10:46:44 +00003843static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003844{
Eric Biedermancb364952004-11-15 10:46:44 +00003845 const char *end = file->buf + file->size;
3846 while(pos < end) {
3847 /* Lookup the character */
3848 int size = 1;
3849 int c = *pos;
3850 /* Is this a trigraph? */
3851 if (file->trigraphs &&
3852 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3853 {
3854 switch(pos[2]) {
3855 case '=': c = '#'; break;
3856 case '/': c = '\\'; break;
3857 case '\'': c = '^'; break;
3858 case '(': c = '['; break;
3859 case ')': c = ']'; break;
3860 case '!': c = '!'; break;
3861 case '<': c = '{'; break;
3862 case '>': c = '}'; break;
3863 case '-': c = '~'; break;
3864 }
3865 if (c != '?') {
3866 size = 3;
3867 }
3868 }
3869 /* Is this an escaped newline? */
3870 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003871 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003872 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003873 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003874 /* At the start of a line just eat it */
3875 if (pos == file->pos) {
3876 file->line++;
3877 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003878 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003879 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003880 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003881 }
3882 /* Do I need to ga any farther? */
3883 else if (index == 0) {
3884 break;
3885 }
3886 /* Process a normal character */
3887 else {
3888 pos += size;
3889 index -= 1;
3890 }
3891 }
3892 return pos;
3893}
3894
3895static int get_char(struct file_state *file, const char *pos)
3896{
3897 const char *end = file->buf + file->size;
3898 int c;
3899 c = -1;
3900 pos = next_char(file, pos, 0);
3901 if (pos < end) {
3902 /* Lookup the character */
3903 c = *pos;
3904 /* If it is a trigraph get the trigraph value */
3905 if (file->trigraphs &&
3906 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3907 {
3908 switch(pos[2]) {
3909 case '=': c = '#'; break;
3910 case '/': c = '\\'; break;
3911 case '\'': c = '^'; break;
3912 case '(': c = '['; break;
3913 case ')': c = ']'; break;
3914 case '!': c = '!'; break;
3915 case '<': c = '{'; break;
3916 case '>': c = '}'; break;
3917 case '-': c = '~'; break;
3918 }
3919 }
3920 }
3921 return c;
3922}
3923
3924static void eat_chars(struct file_state *file, const char *targ)
3925{
3926 const char *pos = file->pos;
3927 while(pos < targ) {
3928 /* Do we have a newline? */
3929 if (pos[0] == '\n') {
3930 file->line++;
3931 file->report_line++;
3932 file->line_start = pos + 1;
3933 }
3934 pos++;
3935 }
3936 file->pos = pos;
3937}
3938
3939
3940static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3941{
3942 size_t len;
3943 len = 0;
3944 while(src < end) {
3945 src = next_char(file, src, 1);
3946 len++;
3947 }
3948 return len;
3949}
3950
3951static void char_strcpy(char *dest,
3952 struct file_state *file, const char *src, const char *end)
3953{
3954 while(src < end) {
3955 int c;
3956 c = get_char(file, src);
3957 src = next_char(file, src, 1);
3958 *dest++ = c;
3959 }
3960}
3961
3962static char *char_strdup(struct file_state *file,
3963 const char *start, const char *end, const char *id)
3964{
3965 char *str;
3966 size_t str_len;
3967 str_len = char_strlen(file, start, end);
3968 str = xcmalloc(str_len + 1, id);
3969 char_strcpy(str, file, start, end);
3970 str[str_len] = '\0';
3971 return str;
3972}
3973
3974static const char *after_digits(struct file_state *file, const char *ptr)
3975{
3976 while(digitp(get_char(file, ptr))) {
3977 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003978 }
3979 return ptr;
3980}
3981
Eric Biedermancb364952004-11-15 10:46:44 +00003982static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003983{
Eric Biedermancb364952004-11-15 10:46:44 +00003984 while(octdigitp(get_char(file, ptr))) {
3985 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003986 }
3987 return ptr;
3988}
3989
Eric Biedermancb364952004-11-15 10:46:44 +00003990static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003991{
Eric Biedermancb364952004-11-15 10:46:44 +00003992 while(hexdigitp(get_char(file, ptr))) {
3993 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003994 }
3995 return ptr;
3996}
3997
Eric Biedermancb364952004-11-15 10:46:44 +00003998static const char *after_alnums(struct file_state *file, const char *ptr)
3999{
4000 int c;
4001 c = get_char(file, ptr);
4002 while(letterp(c) || digitp(c)) {
4003 ptr = next_char(file, ptr, 1);
4004 c = get_char(file, ptr);
4005 }
4006 return ptr;
4007}
4008
4009static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004010 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004011{
4012 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004013
Eric Biedermanb138ac82003-04-22 18:44:01 +00004014 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004015 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004016
4017 /* Store the copy in the token */
4018 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004019 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004020}
4021
4022static void raw_next_token(struct compile_state *state,
4023 struct file_state *file, struct token *tk)
4024{
4025 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004026 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004027 const char *tokp;
4028 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004029 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004030
Eric Biedermanb138ac82003-04-22 18:44:01 +00004031 tk->str_len = 0;
4032 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004033 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004034 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004035 c = get_char(file, tokp);
4036 tokp = next_char(file, tokp, 1);
4037 eat = 0;
4038 c1 = get_char(file, tokp);
4039 c2 = get_char(file, next_char(file, tokp, 1));
4040 c3 = get_char(file, next_char(file, tokp, 2));
4041
4042 /* The end of the file */
4043 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004044 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004045 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004046 /* Whitespace */
4047 else if (spacep(c)) {
4048 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004049 while (spacep(get_char(file, tokp))) {
4050 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004051 }
4052 }
4053 /* EOL Comments */
4054 else if ((c == '/') && (c1 == '/')) {
4055 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004056 tokp = next_char(file, tokp, 1);
4057 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004058 /* Advance to the next character only after we verify
4059 * the current character is not a newline.
4060 * EOL is special to the preprocessor so we don't
4061 * want to loose any.
4062 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004063 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004064 break;
4065 }
Eric Biederman57183382006-12-02 16:48:48 +00004066 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004067 }
4068 }
4069 /* Comments */
4070 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004071 tokp = next_char(file, tokp, 2);
4072 c = c2;
4073 while((c1 = get_char(file, tokp)) != -1) {
4074 tokp = next_char(file, tokp, 1);
4075 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004076 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004077 break;
4078 }
Eric Biedermancb364952004-11-15 10:46:44 +00004079 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004080 }
4081 if (tok == TOK_UNKNOWN) {
4082 error(state, 0, "unterminated comment");
4083 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004084 }
4085 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004086 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
4087 int wchar, multiline;
4088
Eric Biedermanb138ac82003-04-22 18:44:01 +00004089 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004090 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004091 if (c == 'L') {
4092 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004093 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004094 }
Eric Biedermancb364952004-11-15 10:46:44 +00004095 while((c = get_char(file, tokp)) != -1) {
4096 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004097 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004098 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004099 }
Eric Biedermancb364952004-11-15 10:46:44 +00004100 else if (c == '\\') {
4101 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004102 }
4103 else if (c == '"') {
4104 tok = TOK_LIT_STRING;
4105 break;
4106 }
4107 }
4108 if (tok == TOK_UNKNOWN) {
4109 error(state, 0, "unterminated string constant");
4110 }
Eric Biedermancb364952004-11-15 10:46:44 +00004111 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004112 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004113 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004114
4115 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004116 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004117 }
4118 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004119 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
4120 int wchar, multiline;
4121
Eric Biedermanb138ac82003-04-22 18:44:01 +00004122 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004123 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004124 if (c == 'L') {
4125 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004126 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004127 }
Eric Biedermancb364952004-11-15 10:46:44 +00004128 while((c = get_char(file, tokp)) != -1) {
4129 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004130 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004131 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004132 }
Eric Biedermancb364952004-11-15 10:46:44 +00004133 else if (c == '\\') {
4134 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004135 }
4136 else if (c == '\'') {
4137 tok = TOK_LIT_CHAR;
4138 break;
4139 }
4140 }
4141 if (tok == TOK_UNKNOWN) {
4142 error(state, 0, "unterminated character constant");
4143 }
Eric Biedermancb364952004-11-15 10:46:44 +00004144 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004145 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004146 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004147
4148 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004149 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004150 }
4151 /* integer and floating constants
4152 * Integer Constants
4153 * {digits}
4154 * 0[Xx]{hexdigits}
4155 * 0{octdigit}+
4156 *
4157 * Floating constants
4158 * {digits}.{digits}[Ee][+-]?{digits}
4159 * {digits}.{digits}
4160 * {digits}[Ee][+-]?{digits}
4161 * .{digits}[Ee][+-]?{digits}
4162 * .{digits}
4163 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004164 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004165 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004166 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004167 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004168 is_float = 0;
4169 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004170 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004171 }
4172 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004173 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004174 }
Eric Biedermancb364952004-11-15 10:46:44 +00004175 cn = get_char(file, next);
4176 if (cn == '.') {
4177 next = next_char(file, next, 1);
4178 next = after_digits(file, next);
4179 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004180 }
Eric Biedermancb364952004-11-15 10:46:44 +00004181 cn = get_char(file, next);
4182 if ((cn == 'e') || (cn == 'E')) {
4183 const char *new;
4184 next = next_char(file, next, 1);
4185 cn = get_char(file, next);
4186 if ((cn == '+') || (cn == '-')) {
4187 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004188 }
Eric Biedermancb364952004-11-15 10:46:44 +00004189 new = after_digits(file, next);
4190 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004191 next = new;
4192 }
4193 if (is_float) {
4194 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004195 cn = get_char(file, next);
4196 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4197 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004198 }
4199 }
4200 if (!is_float && digitp(c)) {
4201 tok = TOK_LIT_INT;
4202 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004203 next = next_char(file, tokp, 1);
4204 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004205 }
4206 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004207 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004208 }
4209 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004210 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004211 }
4212 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004213 cn = get_char(file, next);
4214 if ((cn == 'u') || (cn == 'U')) {
4215 next = next_char(file, next, 1);
4216 cn = get_char(file, next);
4217 if ((cn == 'l') || (cn == 'L')) {
4218 next = next_char(file, next, 1);
4219 cn = get_char(file, next);
4220 }
4221 if ((cn == 'l') || (cn == 'L')) {
4222 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004223 }
4224 }
Eric Biedermancb364952004-11-15 10:46:44 +00004225 else if ((cn == 'l') || (cn == 'L')) {
4226 next = next_char(file, next, 1);
4227 cn = get_char(file, next);
4228 if ((cn == 'l') || (cn == 'L')) {
4229 next = next_char(file, next, 1);
4230 cn = get_char(file, next);
4231 }
4232 if ((cn == 'u') || (cn == 'U')) {
4233 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004234 }
4235 }
4236 }
Eric Biedermancb364952004-11-15 10:46:44 +00004237 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004238
4239 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004240 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004241 }
4242 /* identifiers */
4243 else if (letterp(c)) {
4244 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004245
4246 /* Find and save the identifier string */
4247 tokp = after_alnums(file, tokp);
4248 save_string(file, tk, token, tokp, "identifier");
4249
4250 /* Look up to see which identifier it is */
4251 tk->ident = lookup(state, tk->val.str, tk->str_len);
4252
4253 /* Free the identifier string */
4254 tk->str_len = 0;
4255 xfree(tk->val.str);
4256
Eric Biederman90089602004-05-28 14:11:54 +00004257 /* See if this identifier can be macro expanded */
4258 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004259 c = get_char(file, tokp);
4260 if (c == '$') {
4261 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004262 tk->val.notmacro = 1;
4263 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004264 }
4265 /* C99 alternate macro characters */
4266 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004267 eat += 3;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004268 tok = TOK_CONCATENATE;
4269 }
Eric Biedermancb364952004-11-15 10:46:44 +00004270 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4271 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4272 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4273 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4274 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4275 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4276 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4277 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4278 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4279 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4280 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4281 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4282 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4283 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4284 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4285 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4286 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4287 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4288 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4289 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4290 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4291 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4292 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4293 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4294 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4295 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4296 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4297 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004298 else if (c == ';') { tok = TOK_SEMI; }
4299 else if (c == '{') { tok = TOK_LBRACE; }
4300 else if (c == '}') { tok = TOK_RBRACE; }
4301 else if (c == ',') { tok = TOK_COMMA; }
4302 else if (c == '=') { tok = TOK_EQ; }
4303 else if (c == ':') { tok = TOK_COLON; }
4304 else if (c == '[') { tok = TOK_LBRACKET; }
4305 else if (c == ']') { tok = TOK_RBRACKET; }
4306 else if (c == '(') { tok = TOK_LPAREN; }
4307 else if (c == ')') { tok = TOK_RPAREN; }
4308 else if (c == '*') { tok = TOK_STAR; }
4309 else if (c == '>') { tok = TOK_MORE; }
4310 else if (c == '<') { tok = TOK_LESS; }
4311 else if (c == '?') { tok = TOK_QUEST; }
4312 else if (c == '|') { tok = TOK_OR; }
4313 else if (c == '&') { tok = TOK_AND; }
4314 else if (c == '^') { tok = TOK_XOR; }
4315 else if (c == '+') { tok = TOK_PLUS; }
4316 else if (c == '-') { tok = TOK_MINUS; }
4317 else if (c == '/') { tok = TOK_DIV; }
4318 else if (c == '%') { tok = TOK_MOD; }
4319 else if (c == '!') { tok = TOK_BANG; }
4320 else if (c == '.') { tok = TOK_DOT; }
4321 else if (c == '~') { tok = TOK_TILDE; }
4322 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004323 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004324
Eric Biedermancb364952004-11-15 10:46:44 +00004325 tokp = next_char(file, tokp, eat);
4326 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004327 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004328 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004329}
4330
4331static void check_tok(struct compile_state *state, struct token *tk, int tok)
4332{
4333 if (tk->tok != tok) {
4334 const char *name1, *name2;
4335 name1 = tokens[tk->tok];
4336 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004337 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004338 name2 = tk->ident->name;
4339 }
4340 error(state, 0, "\tfound %s %s expected %s",
4341 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004342 }
4343}
4344
Eric Biederman90089602004-05-28 14:11:54 +00004345struct macro_arg_value {
4346 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004347 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004348 size_t len;
4349};
4350static struct macro_arg_value *read_macro_args(
4351 struct compile_state *state, struct macro *macro,
4352 struct file_state *file, struct token *tk)
4353{
4354 struct macro_arg_value *argv;
4355 struct macro_arg *arg;
4356 int paren_depth;
4357 int i;
4358
4359 if (macro->argc == 0) {
4360 do {
4361 raw_next_token(state, file, tk);
4362 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004363 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004364 }
4365 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4366 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4367 argv[i].value = 0;
4368 argv[i].len = 0;
4369 argv[i].ident = arg->ident;
4370 }
4371 paren_depth = 0;
4372 i = 0;
4373
4374 for(;;) {
4375 const char *start;
4376 size_t len;
4377 start = file->pos;
4378 raw_next_token(state, file, tk);
4379
4380 if (!paren_depth && (tk->tok == TOK_COMMA) &&
4381 (argv[i].ident != state->i___VA_ARGS__))
4382 {
4383 i++;
4384 if (i >= macro->argc) {
4385 error(state, 0, "too many args to %s\n",
4386 macro->ident->name);
4387 }
4388 continue;
4389 }
4390
4391 if (tk->tok == TOK_LPAREN) {
4392 paren_depth++;
4393 }
4394
4395 if (tk->tok == TOK_RPAREN) {
4396 if (paren_depth == 0) {
4397 break;
4398 }
4399 paren_depth--;
4400 }
4401 if (tk->tok == TOK_EOF) {
4402 error(state, 0, "End of file encountered while parsing macro arguments");
4403 }
Eric Biedermancb364952004-11-15 10:46:44 +00004404
4405 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004406 argv[i].value = xrealloc(
4407 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004408 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004409 argv[i].len += len;
4410 }
4411 if (i != macro->argc -1) {
4412 error(state, 0, "missing %s arg %d\n",
4413 macro->ident->name, i +2);
4414 }
4415 return argv;
4416}
4417
4418
4419static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4420{
4421 int i;
4422 for(i = 0; i < macro->argc; i++) {
4423 xfree(argv[i].value);
4424 }
4425 xfree(argv);
4426}
4427
4428struct macro_buf {
4429 char *str;
4430 size_t len, pos;
4431};
4432
Eric Biedermancb364952004-11-15 10:46:44 +00004433static void grow_macro_buf(struct compile_state *state,
4434 const char *id, struct macro_buf *buf,
4435 size_t grow)
4436{
4437 if ((buf->pos + grow) >= buf->len) {
4438 buf->str = xrealloc(buf->str, buf->len + grow, id);
4439 buf->len += grow;
4440 }
4441}
4442
Eric Biederman90089602004-05-28 14:11:54 +00004443static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004444 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004445 const char *fstart, size_t flen)
4446{
Eric Biedermancb364952004-11-15 10:46:44 +00004447 grow_macro_buf(state, id, buf, flen);
4448 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004449#if 0
4450 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4451 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004452 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004453#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004454 buf->pos += flen;
4455}
4456
4457
4458static void append_macro_chars(struct compile_state *state,
4459 const char *id, struct macro_buf *buf,
4460 struct file_state *file, const char *start, const char *end)
4461{
4462 size_t flen;
4463 flen = char_strlen(file, start, end);
4464 grow_macro_buf(state, id, buf, flen);
4465 char_strcpy(buf->str + buf->pos, file, start, end);
4466#if 0
4467 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4468 buf->pos, buf->pos, buf->str,
4469 flen, flen, buf->str + buf->pos);
4470#endif
Eric Biederman90089602004-05-28 14:11:54 +00004471 buf->pos += flen;
4472}
4473
4474static int compile_macro(struct compile_state *state,
4475 struct file_state **filep, struct token *tk);
4476
4477static void macro_expand_args(struct compile_state *state,
4478 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4479{
Eric Biedermancb364952004-11-15 10:46:44 +00004480 int i;
Eric Biederman90089602004-05-28 14:11:54 +00004481
4482 for(i = 0; i < macro->argc; i++) {
4483 struct file_state fmacro, *file;
4484 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004485
Eric Biedermancb364952004-11-15 10:46:44 +00004486 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004487 fmacro.basename = argv[i].ident->name;
4488 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004489 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004490 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004491 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004492 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004493 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004494 fmacro.report_line = 1;
4495 fmacro.report_name = fmacro.basename;
4496 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004497 fmacro.macro = 1;
4498 fmacro.trigraphs = 0;
4499 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004500
4501 buf.len = argv[i].len;
4502 buf.str = xmalloc(buf.len, argv[i].ident->name);
4503 buf.pos = 0;
4504
4505 file = &fmacro;
4506 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004507 raw_next_token(state, file, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004508
Eric Biedermancb364952004-11-15 10:46:44 +00004509 /* If we have recursed into another macro body
4510 * get out of it.
4511 */
Eric Biederman90089602004-05-28 14:11:54 +00004512 if (tk->tok == TOK_EOF) {
4513 struct file_state *old;
4514 old = file;
4515 file = file->prev;
4516 if (!file) {
4517 break;
4518 }
4519 /* old->basename is used keep it */
4520 xfree(old->dirname);
4521 xfree(old->buf);
4522 xfree(old);
4523 continue;
4524 }
4525 else if (tk->ident && tk->ident->sym_define) {
4526 if (compile_macro(state, &file, tk)) {
4527 continue;
4528 }
4529 }
4530
Eric Biedermancb364952004-11-15 10:46:44 +00004531 append_macro_chars(state, macro->ident->name, &buf,
4532 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004533 }
4534
4535 xfree(argv[i].value);
4536 argv[i].value = buf.str;
4537 argv[i].len = buf.pos;
4538 }
4539 return;
4540}
4541
4542static void expand_macro(struct compile_state *state,
4543 struct macro *macro, struct macro_buf *buf,
4544 struct macro_arg_value *argv, struct token *tk)
4545{
4546 struct file_state fmacro;
4547 const char space[] = " ";
4548 const char *fstart;
4549 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004550 int i, j;
4551
4552 /* Place the macro body in a dummy file */
4553 fmacro.prev = 0;
4554 fmacro.basename = macro->ident->name;
4555 fmacro.dirname = "";
4556 fmacro.buf = macro->buf;
4557 fmacro.size = macro->buf_len;
4558 fmacro.pos = fmacro.buf;
4559 fmacro.line = 1;
4560 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004561 fmacro.report_line = 1;
4562 fmacro.report_name = fmacro.basename;
4563 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004564 fmacro.macro = 1;
4565 fmacro.trigraphs = 0;
4566 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004567
Eric Biedermancb364952004-11-15 10:46:44 +00004568 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004569 buf->len = macro->buf_len + 3;
4570 buf->str = xmalloc(buf->len, macro->ident->name);
4571 buf->pos = 0;
4572
4573 fstart = fmacro.pos;
4574 raw_next_token(state, &fmacro, tk);
4575 while(tk->tok != TOK_EOF) {
4576 flen = fmacro.pos - fstart;
4577 switch(tk->tok) {
4578 case TOK_IDENT:
4579 for(i = 0; i < macro->argc; i++) {
4580 if (argv[i].ident == tk->ident) {
4581 break;
4582 }
4583 }
4584 if (i >= macro->argc) {
4585 break;
4586 }
4587 /* Substitute macro parameter */
4588 fstart = argv[i].value;
4589 flen = argv[i].len;
4590 break;
4591 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004592 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004593 break;
4594 }
4595 do {
4596 raw_next_token(state, &fmacro, tk);
4597 } while(tk->tok == TOK_SPACE);
4598 check_tok(state, tk, TOK_IDENT);
4599 for(i = 0; i < macro->argc; i++) {
4600 if (argv[i].ident == tk->ident) {
4601 break;
4602 }
4603 }
4604 if (i >= macro->argc) {
4605 error(state, 0, "parameter `%s' not found",
4606 tk->ident->name);
4607 }
4608 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004609 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004610 for(j = 0; j < argv[i].len; j++) {
4611 char *str = argv[i].value + j;
4612 size_t len = 1;
4613 if (*str == '\\') {
4614 str = "\\";
4615 len = 2;
4616 }
4617 else if (*str == '"') {
4618 str = "\\\"";
4619 len = 2;
4620 }
Eric Biedermancb364952004-11-15 10:46:44 +00004621 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004622 }
Eric Biedermancb364952004-11-15 10:46:44 +00004623 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004624 fstart = 0;
4625 flen = 0;
4626 break;
4627 case TOK_CONCATENATE:
4628 /* Concatenate tokens */
4629 /* Delete the previous whitespace token */
4630 if (buf->str[buf->pos - 1] == ' ') {
4631 buf->pos -= 1;
4632 }
4633 /* Skip the next sequence of whitspace tokens */
4634 do {
4635 fstart = fmacro.pos;
4636 raw_next_token(state, &fmacro, tk);
4637 } while(tk->tok == TOK_SPACE);
4638 /* Restart at the top of the loop.
4639 * I need to process the non white space token.
4640 */
4641 continue;
4642 break;
4643 case TOK_SPACE:
4644 /* Collapse multiple spaces into one */
4645 if (buf->str[buf->pos - 1] != ' ') {
4646 fstart = space;
4647 flen = 1;
4648 } else {
4649 fstart = 0;
4650 flen = 0;
4651 }
4652 break;
4653 default:
4654 break;
4655 }
4656
Eric Biedermancb364952004-11-15 10:46:44 +00004657 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004658
4659 fstart = fmacro.pos;
4660 raw_next_token(state, &fmacro, tk);
4661 }
4662}
4663
4664static void tag_macro_name(struct compile_state *state,
4665 struct macro *macro, struct macro_buf *buf,
4666 struct token *tk)
4667{
4668 /* Guard all instances of the macro name in the replacement
4669 * text from further macro expansion.
4670 */
4671 struct file_state fmacro;
4672 const char *fstart;
4673 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004674
4675 /* Put the old macro expansion buffer in a file */
4676 fmacro.prev = 0;
4677 fmacro.basename = macro->ident->name;
4678 fmacro.dirname = "";
4679 fmacro.buf = buf->str;
4680 fmacro.size = buf->pos;
4681 fmacro.pos = fmacro.buf;
4682 fmacro.line = 1;
4683 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004684 fmacro.report_line = 1;
4685 fmacro.report_name = fmacro.basename;
4686 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004687 fmacro.macro = 1;
4688 fmacro.trigraphs = 0;
4689 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004690
Eric Biedermancb364952004-11-15 10:46:44 +00004691 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004692 buf->len = macro->buf_len + 3;
4693 buf->str = xmalloc(buf->len, macro->ident->name);
4694 buf->pos = 0;
4695
4696 fstart = fmacro.pos;
4697 raw_next_token(state, &fmacro, tk);
4698 while(tk->tok != TOK_EOF) {
4699 flen = fmacro.pos - fstart;
4700 if ((tk->tok == TOK_IDENT) &&
4701 (tk->ident == macro->ident) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004702 (tk->val.notmacro == 0))
4703 {
4704 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004705 fstart = "$";
4706 flen = 1;
4707 }
4708
Eric Biedermancb364952004-11-15 10:46:44 +00004709 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004710
4711 fstart = fmacro.pos;
4712 raw_next_token(state, &fmacro, tk);
4713 }
4714 xfree(fmacro.buf);
4715}
Eric Biedermancb364952004-11-15 10:46:44 +00004716
Eric Biederman90089602004-05-28 14:11:54 +00004717static int compile_macro(struct compile_state *state,
4718 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004719{
4720 struct file_state *file;
4721 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004722 struct macro *macro;
4723 struct macro_arg_value *argv;
4724 struct macro_buf buf;
4725
4726#if 0
4727 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4728#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004729 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004730 macro = ident->sym_define;
4731
4732 /* If this token comes from a macro expansion ignore it */
4733 if (tk->val.notmacro) {
4734 return 0;
4735 }
4736 /* If I am a function like macro and the identifier is not followed
4737 * by a left parenthesis, do nothing.
4738 */
Eric Biedermancb364952004-11-15 10:46:44 +00004739 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004740 return 0;
4741 }
4742
4743 /* Read in the macro arguments */
4744 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004745 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004746 raw_next_token(state, *filep, tk);
4747 check_tok(state, tk, TOK_LPAREN);
4748
4749 argv = read_macro_args(state, macro, *filep, tk);
4750
4751 check_tok(state, tk, TOK_RPAREN);
4752 }
4753 /* Macro expand the macro arguments */
4754 macro_expand_args(state, macro, argv, tk);
4755
4756 buf.str = 0;
4757 buf.len = 0;
4758 buf.pos = 0;
4759 if (ident == state->i___FILE__) {
4760 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4761 buf.str = xmalloc(buf.len, ident->name);
4762 sprintf(buf.str, "\"%s\"", state->file->basename);
4763 buf.pos = strlen(buf.str);
4764 }
4765 else if (ident == state->i___LINE__) {
4766 buf.len = 30;
4767 buf.str = xmalloc(buf.len, ident->name);
4768 sprintf(buf.str, "%d", state->file->line);
4769 buf.pos = strlen(buf.str);
4770 }
4771 else {
4772 expand_macro(state, macro, &buf, argv, tk);
4773 }
4774 /* Tag the macro name with a $ so it will no longer
4775 * be regonized as a canidate for macro expansion.
4776 */
4777 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004778
4779#if 0
4780 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4781 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4782#endif
4783
4784 free_macro_args(macro, argv);
4785
Eric Biedermanb138ac82003-04-22 18:44:01 +00004786 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004787 file->prev = *filep;
4788 file->basename = xstrdup(ident->name);
4789 file->dirname = xstrdup("");
4790 file->buf = buf.str;
4791 file->size = buf.pos;
4792 file->pos = file->buf;
4793 file->line = 1;
4794 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004795 file->report_line = 1;
4796 file->report_name = file->basename;
4797 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004798 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004799 file->trigraphs = 0;
4800 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004801 *filep = file;
4802 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004803}
4804
Eric Biederman90089602004-05-28 14:11:54 +00004805static void eat_tokens(struct compile_state *state, int targ_tok)
4806{
4807 if (state->eat_depth > 0) {
4808 internal_error(state, 0, "Already eating...");
4809 }
4810 state->eat_depth = state->if_depth;
4811 state->eat_targ = targ_tok;
4812}
4813static int if_eat(struct compile_state *state)
4814{
4815 return state->eat_depth > 0;
4816}
4817static int if_value(struct compile_state *state)
4818{
4819 int index, offset;
4820 index = state->if_depth / CHAR_BIT;
4821 offset = state->if_depth % CHAR_BIT;
4822 return !!(state->if_bytes[index] & (1 << (offset)));
4823}
4824static void set_if_value(struct compile_state *state, int value)
4825{
4826 int index, offset;
4827 index = state->if_depth / CHAR_BIT;
4828 offset = state->if_depth % CHAR_BIT;
4829
4830 state->if_bytes[index] &= ~(1 << offset);
4831 if (value) {
4832 state->if_bytes[index] |= (1 << offset);
4833 }
4834}
4835static void in_if(struct compile_state *state, const char *name)
4836{
4837 if (state->if_depth <= 0) {
4838 error(state, 0, "%s without #if", name);
4839 }
4840}
4841static void enter_if(struct compile_state *state)
4842{
4843 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004844 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004845 error(state, 0, "#if depth too great");
4846 }
4847}
4848static void reenter_if(struct compile_state *state, const char *name)
4849{
4850 in_if(state, name);
4851 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004852 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004853 state->eat_depth = 0;
4854 state->eat_targ = 0;
4855 }
4856}
4857static void enter_else(struct compile_state *state, const char *name)
4858{
4859 in_if(state, name);
4860 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004861 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004862 state->eat_depth = 0;
4863 state->eat_targ = 0;
4864 }
4865}
4866static void exit_if(struct compile_state *state, const char *name)
4867{
4868 in_if(state, name);
4869 if (state->eat_depth == state->if_depth) {
4870 state->eat_depth = 0;
4871 state->eat_targ = 0;
4872 }
4873 state->if_depth -= 1;
4874}
4875
Eric Biedermancb364952004-11-15 10:46:44 +00004876static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004877{
4878 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004879 int rescan;
4880
Eric Biedermancb364952004-11-15 10:46:44 +00004881 file = state->file;
4882 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004883 do {
4884 rescan = 0;
4885 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004886 /* Exit out of an include directive or macro call */
4887 if ((tk->tok == TOK_EOF) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004888 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004889 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004890 state->file = file->prev;
4891 /* file->basename is used keep it */
4892 xfree(file->dirname);
4893 xfree(file->buf);
4894 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004895 file = 0;
4896 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004897 rescan = 1;
4898 }
Eric Biederman41203d92004-11-08 09:31:09 +00004899 } while(rescan);
4900}
4901
Eric Biedermancb364952004-11-15 10:46:44 +00004902static void pp_token(struct compile_state *state, struct token *tk)
4903{
4904 struct file_state *file;
4905 int rescan;
4906
4907 raw_token(state, tk);
4908 do {
4909 rescan = 0;
4910 file = state->file;
4911 if (tk->tok == TOK_SPACE) {
4912 raw_token(state, tk);
4913 rescan = 1;
4914 }
4915 else if (tk->tok == TOK_IDENT) {
4916 if (state->token_base == 0) {
4917 ident_to_keyword(state, tk);
4918 } else {
4919 ident_to_macro(state, tk);
4920 }
4921 }
4922 } while(rescan);
4923}
4924
Eric Biederman41203d92004-11-08 09:31:09 +00004925static void preprocess(struct compile_state *state, struct token *tk);
4926
4927static void token(struct compile_state *state, struct token *tk)
4928{
4929 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004930 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004931 do {
4932 rescan = 0;
4933 /* Process a macro directive */
4934 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004935 /* Only match preprocessor directives at the start of a line */
4936 const char *ptr;
4937 ptr = state->file->line_start;
4938 while((ptr < tk->pos)
4939 && spacep(get_char(state->file, ptr)))
4940 {
4941 ptr = next_char(state->file, ptr, 1);
4942 }
4943 if (ptr == tk->pos) {
4944 preprocess(state, tk);
4945 rescan = 1;
4946 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004947 }
Eric Biederman41203d92004-11-08 09:31:09 +00004948 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004949 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004950 rescan = compile_macro(state, &state->file, tk);
4951 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004952 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004953 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004954 }
Eric Biedermancb364952004-11-15 10:46:44 +00004955 /* Eat tokens disabled by the preprocessor
4956 * (Unless we are parsing a preprocessor directive
4957 */
Eric Biedermana649a412004-11-09 00:35:39 +00004958 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004959 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004960 rescan = 1;
4961 }
Eric Biedermana649a412004-11-09 00:35:39 +00004962 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004963 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004964 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004965 rescan = 1;
4966 }
Eric Biedermancb364952004-11-15 10:46:44 +00004967 /* Error on unknown tokens */
4968 else if (tk->tok == TOK_UNKNOWN) {
4969 error(state, 0, "unknown token");
4970 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004971 } while(rescan);
4972}
4973
Eric Biederman41203d92004-11-08 09:31:09 +00004974
4975static inline struct token *get_token(struct compile_state *state, int offset)
4976{
4977 int index;
4978 index = state->token_base + offset;
4979 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4980 internal_error(state, 0, "token array to small");
4981 }
4982 return &state->token[index];
4983}
4984
4985static struct token *do_eat_token(struct compile_state *state, int tok)
4986{
4987 struct token *tk;
4988 int i;
4989 check_tok(state, get_token(state, 1), tok);
4990
4991 /* Free the old token value */
4992 tk = get_token(state, 0);
4993 if (tk->str_len) {
4994 memset((void *)tk->val.str, -1, tk->str_len);
4995 xfree(tk->val.str);
4996 }
4997 /* Overwrite the old token with newer tokens */
4998 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4999 state->token[i] = state->token[i + 1];
5000 }
5001 /* Clear the last token */
5002 memset(&state->token[i], 0, sizeof(state->token[i]));
5003 state->token[i].tok = -1;
5004
5005 /* Return the token */
5006 return tk;
5007}
5008
Eric Biedermancb364952004-11-15 10:46:44 +00005009static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005010{
5011 struct token *tk1;
5012 tk1 = get_token(state, 1);
5013 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005014 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005015 }
5016 return tk1->tok;
5017}
5018
Eric Biedermancb364952004-11-15 10:46:44 +00005019static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005020{
Eric Biedermancb364952004-11-15 10:46:44 +00005021 raw_peek(state);
5022 return do_eat_token(state, tok);
5023}
5024
5025static int pp_peek(struct compile_state *state)
5026{
5027 struct token *tk1;
5028 tk1 = get_token(state, 1);
5029 if (tk1->tok == -1) {
5030 pp_token(state, tk1);
5031 }
5032 return tk1->tok;
5033}
5034
5035static struct token *pp_eat(struct compile_state *state, int tok)
5036{
5037 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005038 return do_eat_token(state, tok);
5039}
5040
Eric Biedermanb138ac82003-04-22 18:44:01 +00005041static int peek(struct compile_state *state)
5042{
Eric Biederman41203d92004-11-08 09:31:09 +00005043 struct token *tk1;
5044 tk1 = get_token(state, 1);
5045 if (tk1->tok == -1) {
5046 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005047 }
Eric Biederman41203d92004-11-08 09:31:09 +00005048 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005049}
5050
5051static int peek2(struct compile_state *state)
5052{
Eric Biederman41203d92004-11-08 09:31:09 +00005053 struct token *tk1, *tk2;
5054 tk1 = get_token(state, 1);
5055 tk2 = get_token(state, 2);
5056 if (tk1->tok == -1) {
5057 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005058 }
Eric Biederman41203d92004-11-08 09:31:09 +00005059 if (tk2->tok == -1) {
5060 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005061 }
Eric Biederman41203d92004-11-08 09:31:09 +00005062 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005063}
5064
Eric Biederman41203d92004-11-08 09:31:09 +00005065static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005066{
Eric Biederman90089602004-05-28 14:11:54 +00005067 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005068 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005069}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005070
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005071static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005072{
Eric Biederman90089602004-05-28 14:11:54 +00005073 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005074 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005075 int subdir_len;
5076 struct file_state *file;
5077 char *basename;
5078 file = xmalloc(sizeof(*file), "file_state");
5079
5080 base = strrchr(filename, '/');
5081 subdir = filename;
5082 if (base != 0) {
5083 subdir_len = base - filename;
5084 base++;
5085 }
5086 else {
5087 base = filename;
5088 subdir_len = 0;
5089 }
5090 basename = xmalloc(strlen(base) +1, "basename");
5091 strcpy(basename, base);
5092 file->basename = basename;
5093
5094 if (getcwd(cwd, sizeof(cwd)) == 0) {
5095 die("cwd buffer to small");
5096 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005097 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005098 file->dirname = xmalloc(subdir_len + 1, "dirname");
5099 memcpy(file->dirname, subdir, subdir_len);
5100 file->dirname[subdir_len] = '\0';
5101 }
5102 else {
Eric Biederman90089602004-05-28 14:11:54 +00005103 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005104 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005105 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005106 /* Find the appropriate directory... */
5107 dir = 0;
5108 if (!state->file && exists(cwd, filename)) {
5109 dir = cwd;
5110 }
5111 if (local && state->file && exists(state->file->dirname, filename)) {
5112 dir = state->file->dirname;
5113 }
Eric Biederman90089602004-05-28 14:11:54 +00005114 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005115 if (exists(*path, filename)) {
5116 dir = *path;
5117 }
5118 }
5119 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005120 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005121 }
5122 dirlen = strlen(dir);
5123 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5124 memcpy(file->dirname, dir, dirlen);
5125 file->dirname[dirlen] = '/';
5126 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5127 file->dirname[dirlen + 1 + subdir_len] = '\0';
5128 }
5129 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005130
5131 file->pos = file->buf;
5132 file->line_start = file->pos;
5133 file->line = 1;
5134
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005135 file->report_line = 1;
5136 file->report_name = file->basename;
5137 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005138 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005139 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5140 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005141
Eric Biedermanb138ac82003-04-22 18:44:01 +00005142 file->prev = state->file;
5143 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005144}
5145
Eric Biederman41203d92004-11-08 09:31:09 +00005146static struct triple *constant_expr(struct compile_state *state);
5147static void integral(struct compile_state *state, struct triple *def);
5148
5149static int mcexpr(struct compile_state *state)
5150{
5151 struct triple *cvalue;
5152 cvalue = constant_expr(state);
5153 integral(state, cvalue);
5154 if (cvalue->op != OP_INTCONST) {
5155 error(state, 0, "integer constant expected");
5156 }
5157 return cvalue->u.cval != 0;
5158}
5159
5160static void preprocess(struct compile_state *state, struct token *current_token)
5161{
5162 /* Doing much more with the preprocessor would require
5163 * a parser and a major restructuring.
5164 * Postpone that for later.
5165 */
Eric Biederman41203d92004-11-08 09:31:09 +00005166 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005167 int tok;
5168
Eric Biedermancb364952004-11-15 10:46:44 +00005169 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005170
5171 old_token_base = state->token_base;
5172 state->token_base = current_token - state->token;
5173
Eric Biedermancb364952004-11-15 10:46:44 +00005174 tok = pp_peek(state);
5175 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005176 case TOK_LIT_INT:
5177 {
5178 struct token *tk;
5179 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005180 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005181 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005182 /* I have a preprocessor line marker parse it */
5183 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005184 const char *token, *base;
5185 char *name, *dir;
5186 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005187 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005188 name = xmalloc(tk->str_len, "report_name");
5189 token = tk->val.str + 1;
5190 base = strrchr(token, '/');
5191 name_len = tk->str_len -2;
5192 if (base != 0) {
5193 dir_len = base - token;
5194 base++;
5195 name_len -= base - token;
5196 } else {
5197 dir_len = 0;
5198 base = token;
5199 }
5200 memcpy(name, base, name_len);
5201 name[name_len] = '\0';
5202 dir = xmalloc(dir_len + 1, "report_dir");
5203 memcpy(dir, token, dir_len);
5204 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005205 state->file->report_line = override_line - 1;
5206 state->file->report_name = name;
5207 state->file->report_dir = dir;
5208 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005209 }
5210 break;
5211 }
5212 case TOK_MLINE:
5213 {
5214 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005215 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005216 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005217 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5218 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005219 const char *token, *base;
5220 char *name, *dir;
5221 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005222 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005223 name = xmalloc(tk->str_len, "report_name");
5224 token = tk->val.str + 1;
5225 base = strrchr(token, '/');
5226 name_len = tk->str_len - 2;
5227 if (base != 0) {
5228 dir_len = base - token;
5229 base++;
5230 name_len -= base - token;
5231 } else {
5232 dir_len = 0;
5233 base = token;
5234 }
5235 memcpy(name, base, name_len);
5236 name[name_len] = '\0';
5237 dir = xmalloc(dir_len + 1, "report_dir");
5238 memcpy(dir, token, dir_len);
5239 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005240 state->file->report_name = name;
5241 state->file->report_dir = dir;
5242 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005243 }
5244 break;
5245 }
5246 case TOK_MUNDEF:
5247 {
5248 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005249 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005250 if (if_eat(state)) /* quit early when #if'd out */
5251 break;
5252
Eric Biedermancb364952004-11-15 10:46:44 +00005253 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005254
5255 undef_macro(state, ident);
5256 break;
5257 }
5258 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005259 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005260 if (if_eat(state)) /* quit early when #if'd out */
5261 break;
5262 warning(state, 0, "Ignoring pragma");
5263 break;
5264 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005265 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005266 reenter_if(state, "#elif");
5267 if (if_eat(state)) /* quit early when #if'd out */
5268 break;
5269 /* If the #if was taken the #elif just disables the following code */
5270 if (if_value(state)) {
5271 eat_tokens(state, TOK_MENDIF);
5272 }
5273 /* If the previous #if was not taken see if the #elif enables the
5274 * trailing code.
5275 */
5276 else {
5277 set_if_value(state, mcexpr(state));
5278 if (!if_value(state)) {
5279 eat_tokens(state, TOK_MELSE);
5280 }
5281 }
5282 break;
5283 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005284 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005285 enter_if(state);
5286 if (if_eat(state)) /* quit early when #if'd out */
5287 break;
5288 set_if_value(state, mcexpr(state));
5289 if (!if_value(state)) {
5290 eat_tokens(state, TOK_MELSE);
5291 }
5292 break;
5293 case TOK_MIFNDEF:
5294 {
5295 struct hash_entry *ident;
5296
Eric Biedermancb364952004-11-15 10:46:44 +00005297 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005298 enter_if(state);
5299 if (if_eat(state)) /* quit early when #if'd out */
5300 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005301 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005302 set_if_value(state, ident->sym_define == 0);
5303 if (!if_value(state)) {
5304 eat_tokens(state, TOK_MELSE);
5305 }
5306 break;
5307 }
5308 case TOK_MIFDEF:
5309 {
5310 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005311 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005312 enter_if(state);
5313 if (if_eat(state)) /* quit early when #if'd out */
5314 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005315 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005316 set_if_value(state, ident->sym_define != 0);
5317 if (!if_value(state)) {
5318 eat_tokens(state, TOK_MELSE);
5319 }
5320 break;
5321 }
5322 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005323 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005324 enter_else(state, "#else");
5325 if (!if_eat(state) && if_value(state)) {
5326 eat_tokens(state, TOK_MENDIF);
5327 }
5328 break;
5329 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005330 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005331 exit_if(state, "#endif");
5332 break;
5333 case TOK_MDEFINE:
5334 {
5335 struct hash_entry *ident;
5336 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005337 const char *mstart, *mend;
5338 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005339
Eric Biedermancb364952004-11-15 10:46:44 +00005340 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005341 if (if_eat(state)) /* quit early when #if'd out */
5342 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005343 ident = pp_eat(state, TOK_MIDENT)->ident;
5344 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005345 args = 0;
5346 larg = &args;
5347
Eric Biederman41203d92004-11-08 09:31:09 +00005348 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005349 if (raw_peek(state) == TOK_LPAREN) {
5350 raw_eat(state, TOK_LPAREN);
5351 argc += 1;
5352
Eric Biederman41203d92004-11-08 09:31:09 +00005353 for(;;) {
5354 struct macro_arg *narg, *arg;
5355 struct hash_entry *aident;
5356 int tok;
5357
Eric Biedermancb364952004-11-15 10:46:44 +00005358 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005359 if (!args && (tok == TOK_RPAREN)) {
5360 break;
5361 }
5362 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005363 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005364 aident = state->i___VA_ARGS__;
5365 }
5366 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005367 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005368 }
5369
5370 narg = xcmalloc(sizeof(*arg), "macro arg");
5371 narg->ident = aident;
5372
5373 /* Verify I don't have a duplicate identifier */
5374 for(arg = args; arg; arg = arg->next) {
5375 if (arg->ident == narg->ident) {
5376 error(state, 0, "Duplicate macro arg `%s'",
5377 narg->ident->name);
5378 }
5379 }
5380 /* Add the new argument to the end of the list */
5381 *larg = narg;
5382 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005383 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005384
5385 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005386 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005387 break;
5388 }
Eric Biedermancb364952004-11-15 10:46:44 +00005389 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005390 }
Eric Biedermancb364952004-11-15 10:46:44 +00005391 pp_eat(state, TOK_RPAREN);
5392 }
5393 /* Remove leading whitespace */
5394 while(raw_peek(state) == TOK_SPACE) {
5395 raw_eat(state, TOK_SPACE);
5396 }
Eric Biederman41203d92004-11-08 09:31:09 +00005397
Eric Biedermancb364952004-11-15 10:46:44 +00005398 /* Remember the start of the macro body */
5399 tok = raw_peek(state);
5400 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005401
Eric Biedermancb364952004-11-15 10:46:44 +00005402 /* Find the end of the macro */
5403 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5404 raw_eat(state, tok);
5405 /* Remember the end of the last non space token */
5406 raw_peek(state);
5407 if (tok != TOK_SPACE) {
5408 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005409 }
5410 }
Eric Biedermancb364952004-11-15 10:46:44 +00005411
5412 /* Now that I have found the body defined the token */
5413 do_define_macro(state, ident,
5414 char_strdup(state->file, mstart, mend, "macro buf"),
5415 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005416 break;
5417 }
5418 case TOK_MERROR:
5419 {
Eric Biedermancb364952004-11-15 10:46:44 +00005420 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005421 int len;
5422
Eric Biedermancb364952004-11-15 10:46:44 +00005423 pp_eat(state, TOK_MERROR);
5424 /* Find the start of the line */
5425 raw_peek(state);
5426 start = get_token(state, 1)->pos;
5427
Eric Biederman41203d92004-11-08 09:31:09 +00005428 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005429 while((tok = raw_peek(state)) != TOK_EOL) {
5430 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005431 }
Eric Biedermancb364952004-11-15 10:46:44 +00005432 end = get_token(state, 1)->pos;
5433 len = end - start;
5434 if (!if_eat(state)) {
5435 error(state, 0, "%*.*s", len, len, start);
5436 }
Eric Biederman41203d92004-11-08 09:31:09 +00005437 break;
5438 }
5439 case TOK_MWARNING:
5440 {
Eric Biedermancb364952004-11-15 10:46:44 +00005441 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005442 int len;
5443
Eric Biedermancb364952004-11-15 10:46:44 +00005444 pp_eat(state, TOK_MWARNING);
5445
5446 /* Find the start of the line */
5447 raw_peek(state);
5448 start = get_token(state, 1)->pos;
5449
Eric Biederman41203d92004-11-08 09:31:09 +00005450 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005451 while((tok = raw_peek(state)) != TOK_EOL) {
5452 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005453 }
Eric Biedermancb364952004-11-15 10:46:44 +00005454 end = get_token(state, 1)->pos;
5455 len = end - start;
5456 if (!if_eat(state)) {
5457 warning(state, 0, "%*.*s", len, len, start);
5458 }
Eric Biederman41203d92004-11-08 09:31:09 +00005459 break;
5460 }
5461 case TOK_MINCLUDE:
5462 {
5463 char *name;
5464 int local;
5465 local = 0;
5466 name = 0;
5467
Eric Biedermancb364952004-11-15 10:46:44 +00005468 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005469 if (if_eat(state)) {
5470 /* Find the end of the line */
5471 while((tok = raw_peek(state)) != TOK_EOL) {
5472 raw_eat(state, tok);
5473 }
5474 break;
5475 }
Eric Biederman41203d92004-11-08 09:31:09 +00005476 tok = peek(state);
5477 if (tok == TOK_LIT_STRING) {
5478 struct token *tk;
5479 const char *token;
5480 int name_len;
5481 tk = eat(state, TOK_LIT_STRING);
5482 name = xmalloc(tk->str_len, "include");
5483 token = tk->val.str +1;
5484 name_len = tk->str_len -2;
5485 if (*token == '"') {
5486 token++;
5487 name_len--;
5488 }
5489 memcpy(name, token, name_len);
5490 name[name_len] = '\0';
5491 local = 1;
5492 }
5493 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005494 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005495 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005496
5497 buf.len = 40;
5498 buf.str = xmalloc(buf.len, "include");
5499 buf.pos = 0;
5500
5501 tok = peek(state);
5502 while((tok != TOK_MORE) &&
5503 (tok != TOK_EOL) && (tok != TOK_EOF))
5504 {
5505 struct token *tk;
5506 tk = eat(state, tok);
5507 append_macro_chars(state, "include", &buf,
5508 state->file, tk->pos, state->file->pos);
5509 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005510 }
Eric Biedermancb364952004-11-15 10:46:44 +00005511 append_macro_text(state, "include", &buf, "\0", 1);
5512 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005513 error(state, 0, "Unterminated include directive");
5514 }
Eric Biederman41203d92004-11-08 09:31:09 +00005515 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005516 local = 0;
5517 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005518 }
5519 else {
5520 error(state, 0, "Invalid include directive");
5521 }
5522 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005523 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005524 error(state, 0, "garbage after include directive");
5525 }
5526 if (!if_eat(state)) {
5527 compile_file(state, name, local);
5528 }
5529 xfree(name);
5530 break;
5531 }
5532 case TOK_EOL:
5533 /* Ignore # without a follwing ident */
5534 break;
5535 default:
5536 {
5537 const char *name1, *name2;
5538 name1 = tokens[tok];
5539 name2 = "";
5540 if (tok == TOK_MIDENT) {
5541 name2 = get_token(state, 1)->ident->name;
5542 }
5543 error(state, 0, "Invalid preprocessor directive: %s %s",
5544 name1, name2);
5545 break;
5546 }
5547 }
5548 /* Consume the rest of the macro line */
5549 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005550 tok = pp_peek(state);
5551 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005552 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5553 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005554 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005555 return;
5556}
5557
Eric Biederman0babc1c2003-05-09 02:39:00 +00005558/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005559
5560static struct type *new_type(
5561 unsigned int type, struct type *left, struct type *right)
5562{
5563 struct type *result;
5564 result = xmalloc(sizeof(*result), "type");
5565 result->type = type;
5566 result->left = left;
5567 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005568 result->field_ident = 0;
5569 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005570 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005571 return result;
5572}
5573
5574static struct type *clone_type(unsigned int specifiers, struct type *old)
5575{
5576 struct type *result;
5577 result = xmalloc(sizeof(*result), "type");
5578 memcpy(result, old, sizeof(*result));
5579 result->type &= TYPE_MASK;
5580 result->type |= specifiers;
5581 return result;
5582}
5583
Eric Biederman90089602004-05-28 14:11:54 +00005584static struct type *dup_type(struct compile_state *state, struct type *orig)
5585{
5586 struct type *new;
5587 new = xcmalloc(sizeof(*new), "type");
5588 new->type = orig->type;
5589 new->field_ident = orig->field_ident;
5590 new->type_ident = orig->type_ident;
5591 new->elements = orig->elements;
5592 if (orig->left) {
5593 new->left = dup_type(state, orig->left);
5594 }
5595 if (orig->right) {
5596 new->right = dup_type(state, orig->right);
5597 }
5598 return new;
5599}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005600
Eric Biederman90089602004-05-28 14:11:54 +00005601
5602static struct type *invalid_type(struct compile_state *state, struct type *type)
5603{
5604 struct type *invalid, *member;
5605 invalid = 0;
5606 if (!type) {
5607 internal_error(state, 0, "type missing?");
5608 }
5609 switch(type->type & TYPE_MASK) {
5610 case TYPE_VOID:
5611 case TYPE_CHAR: case TYPE_UCHAR:
5612 case TYPE_SHORT: case TYPE_USHORT:
5613 case TYPE_INT: case TYPE_UINT:
5614 case TYPE_LONG: case TYPE_ULONG:
5615 case TYPE_LLONG: case TYPE_ULLONG:
5616 case TYPE_POINTER:
5617 case TYPE_ENUM:
5618 break;
5619 case TYPE_BITFIELD:
5620 invalid = invalid_type(state, type->left);
5621 break;
5622 case TYPE_ARRAY:
5623 invalid = invalid_type(state, type->left);
5624 break;
5625 case TYPE_STRUCT:
5626 case TYPE_TUPLE:
5627 member = type->left;
5628 while(member && (invalid == 0) &&
5629 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5630 invalid = invalid_type(state, member->left);
5631 member = member->right;
5632 }
5633 if (!invalid) {
5634 invalid = invalid_type(state, member);
5635 }
5636 break;
5637 case TYPE_UNION:
5638 case TYPE_JOIN:
5639 member = type->left;
5640 while(member && (invalid == 0) &&
5641 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5642 invalid = invalid_type(state, member->left);
5643 member = member->right;
5644 }
5645 if (!invalid) {
5646 invalid = invalid_type(state, member);
5647 }
5648 break;
5649 default:
5650 invalid = type;
5651 break;
5652 }
5653 return invalid;
5654
5655}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005656
5657#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005658#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005659static inline ulong_t mask_uint(ulong_t x)
5660{
5661 if (SIZEOF_INT < SIZEOF_LONG) {
Stefan Reinauer50542a82007-10-24 11:14:14 +00005662 ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005663 x &= mask;
5664 }
5665 return x;
5666}
5667#define MASK_UINT(X) (mask_uint(X))
5668#define MASK_ULONG(X) (X)
5669
Eric Biederman90089602004-05-28 14:11:54 +00005670static struct type void_type = { .type = TYPE_VOID };
5671static struct type char_type = { .type = TYPE_CHAR };
5672static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005673#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005674static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005675#endif
Eric Biederman90089602004-05-28 14:11:54 +00005676static struct type ushort_type = { .type = TYPE_USHORT };
5677static struct type int_type = { .type = TYPE_INT };
5678static struct type uint_type = { .type = TYPE_UINT };
5679static struct type long_type = { .type = TYPE_LONG };
5680static struct type ulong_type = { .type = TYPE_ULONG };
5681static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005682
Eric Biederman5ade04a2003-10-22 04:03:46 +00005683static struct type void_ptr_type = {
5684 .type = TYPE_POINTER,
5685 .left = &void_type,
5686};
5687
Stefan Reinauer50542a82007-10-24 11:14:14 +00005688#if DEBUG_ROMCC_WARNING
Eric Biederman5ade04a2003-10-22 04:03:46 +00005689static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005690 .type = TYPE_FUNCTION,
5691 .left = &void_type,
5692 .right = &void_type,
5693};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005694#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005695
Eric Biederman90089602004-05-28 14:11:54 +00005696static size_t bits_to_bytes(size_t size)
5697{
5698 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5699}
5700
Eric Biedermanb138ac82003-04-22 18:44:01 +00005701static struct triple *variable(struct compile_state *state, struct type *type)
5702{
5703 struct triple *result;
5704 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005705 result = triple(state, OP_ADECL, type, 0, 0);
5706 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005707 }
5708 else {
5709 result = triple(state, OP_SDECL, type, 0, 0);
5710 }
5711 return result;
5712}
5713
5714static void stor_of(FILE *fp, struct type *type)
5715{
5716 switch(type->type & STOR_MASK) {
5717 case STOR_AUTO:
5718 fprintf(fp, "auto ");
5719 break;
5720 case STOR_STATIC:
5721 fprintf(fp, "static ");
5722 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005723 case STOR_LOCAL:
5724 fprintf(fp, "local ");
5725 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005726 case STOR_EXTERN:
5727 fprintf(fp, "extern ");
5728 break;
5729 case STOR_REGISTER:
5730 fprintf(fp, "register ");
5731 break;
5732 case STOR_TYPEDEF:
5733 fprintf(fp, "typedef ");
5734 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005735 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005736 fprintf(fp, "inline ");
5737 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005738 case STOR_INLINE | STOR_STATIC:
5739 fprintf(fp, "static inline");
5740 break;
5741 case STOR_INLINE | STOR_EXTERN:
5742 fprintf(fp, "extern inline");
5743 break;
5744 default:
5745 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5746 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005747 }
5748}
5749static void qual_of(FILE *fp, struct type *type)
5750{
5751 if (type->type & QUAL_CONST) {
5752 fprintf(fp, " const");
5753 }
5754 if (type->type & QUAL_VOLATILE) {
5755 fprintf(fp, " volatile");
5756 }
5757 if (type->type & QUAL_RESTRICT) {
5758 fprintf(fp, " restrict");
5759 }
5760}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005761
Eric Biedermanb138ac82003-04-22 18:44:01 +00005762static void name_of(FILE *fp, struct type *type)
5763{
Eric Biederman90089602004-05-28 14:11:54 +00005764 unsigned int base_type;
5765 base_type = type->type & TYPE_MASK;
5766 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5767 stor_of(fp, type);
5768 }
5769 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005770 case TYPE_VOID:
5771 fprintf(fp, "void");
5772 qual_of(fp, type);
5773 break;
5774 case TYPE_CHAR:
5775 fprintf(fp, "signed char");
5776 qual_of(fp, type);
5777 break;
5778 case TYPE_UCHAR:
5779 fprintf(fp, "unsigned char");
5780 qual_of(fp, type);
5781 break;
5782 case TYPE_SHORT:
5783 fprintf(fp, "signed short");
5784 qual_of(fp, type);
5785 break;
5786 case TYPE_USHORT:
5787 fprintf(fp, "unsigned short");
5788 qual_of(fp, type);
5789 break;
5790 case TYPE_INT:
5791 fprintf(fp, "signed int");
5792 qual_of(fp, type);
5793 break;
5794 case TYPE_UINT:
5795 fprintf(fp, "unsigned int");
5796 qual_of(fp, type);
5797 break;
5798 case TYPE_LONG:
5799 fprintf(fp, "signed long");
5800 qual_of(fp, type);
5801 break;
5802 case TYPE_ULONG:
5803 fprintf(fp, "unsigned long");
5804 qual_of(fp, type);
5805 break;
5806 case TYPE_POINTER:
5807 name_of(fp, type->left);
5808 fprintf(fp, " * ");
5809 qual_of(fp, type);
5810 break;
5811 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005812 name_of(fp, type->left);
5813 fprintf(fp, ", ");
5814 name_of(fp, type->right);
5815 break;
Eric Biederman90089602004-05-28 14:11:54 +00005816 case TYPE_OVERLAP:
5817 name_of(fp, type->left);
5818 fprintf(fp, ",| ");
5819 name_of(fp, type->right);
5820 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005821 case TYPE_ENUM:
Eric Biederman90089602004-05-28 14:11:54 +00005822 fprintf(fp, "enum %s",
5823 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005824 qual_of(fp, type);
5825 break;
5826 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005827 fprintf(fp, "struct %s { ",
5828 (type->type_ident)? type->type_ident->name : "");
5829 name_of(fp, type->left);
5830 fprintf(fp, " } ");
5831 qual_of(fp, type);
5832 break;
5833 case TYPE_UNION:
5834 fprintf(fp, "union %s { ",
5835 (type->type_ident)? type->type_ident->name : "");
5836 name_of(fp, type->left);
5837 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005838 qual_of(fp, type);
5839 break;
5840 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005841 name_of(fp, type->left);
5842 fprintf(fp, " (*)(");
5843 name_of(fp, type->right);
5844 fprintf(fp, ")");
5845 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005846 case TYPE_ARRAY:
5847 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005848 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005849 break;
Eric Biederman90089602004-05-28 14:11:54 +00005850 case TYPE_TUPLE:
5851 fprintf(fp, "tuple { ");
5852 name_of(fp, type->left);
5853 fprintf(fp, " } ");
5854 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005855 break;
Eric Biederman90089602004-05-28 14:11:54 +00005856 case TYPE_JOIN:
5857 fprintf(fp, "join { ");
5858 name_of(fp, type->left);
5859 fprintf(fp, " } ");
5860 qual_of(fp, type);
5861 break;
5862 case TYPE_BITFIELD:
5863 name_of(fp, type->left);
5864 fprintf(fp, " : %d ", type->elements);
5865 qual_of(fp, type);
5866 break;
5867 case TYPE_UNKNOWN:
5868 fprintf(fp, "unknown_t");
5869 break;
5870 default:
5871 fprintf(fp, "????: %x", base_type);
5872 break;
5873 }
5874 if (type->field_ident && type->field_ident->name) {
5875 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005876 }
5877}
5878
5879static size_t align_of(struct compile_state *state, struct type *type)
5880{
5881 size_t align;
5882 align = 0;
5883 switch(type->type & TYPE_MASK) {
5884 case TYPE_VOID:
5885 align = 1;
5886 break;
Eric Biederman90089602004-05-28 14:11:54 +00005887 case TYPE_BITFIELD:
5888 align = 1;
5889 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005890 case TYPE_CHAR:
5891 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005892 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005893 break;
5894 case TYPE_SHORT:
5895 case TYPE_USHORT:
5896 align = ALIGNOF_SHORT;
5897 break;
5898 case TYPE_INT:
5899 case TYPE_UINT:
5900 case TYPE_ENUM:
5901 align = ALIGNOF_INT;
5902 break;
5903 case TYPE_LONG:
5904 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005905 align = ALIGNOF_LONG;
5906 break;
Eric Biederman90089602004-05-28 14:11:54 +00005907 case TYPE_POINTER:
5908 align = ALIGNOF_POINTER;
5909 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005910 case TYPE_PRODUCT:
5911 case TYPE_OVERLAP:
5912 {
5913 size_t left_align, right_align;
5914 left_align = align_of(state, type->left);
5915 right_align = align_of(state, type->right);
5916 align = (left_align >= right_align) ? left_align : right_align;
5917 break;
5918 }
5919 case TYPE_ARRAY:
5920 align = align_of(state, type->left);
5921 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005922 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005923 case TYPE_TUPLE:
5924 case TYPE_UNION:
5925 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005926 align = align_of(state, type->left);
5927 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005928 default:
5929 error(state, 0, "alignof not yet defined for type\n");
5930 break;
5931 }
5932 return align;
5933}
5934
Eric Biederman90089602004-05-28 14:11:54 +00005935static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005936{
Eric Biederman90089602004-05-28 14:11:54 +00005937 size_t align;
5938 align = 0;
5939 switch(type->type & TYPE_MASK) {
5940 case TYPE_VOID:
5941 align = 1;
5942 break;
5943 case TYPE_BITFIELD:
5944 align = 1;
5945 break;
5946 case TYPE_CHAR:
5947 case TYPE_UCHAR:
5948 align = REG_ALIGNOF_CHAR;
5949 break;
5950 case TYPE_SHORT:
5951 case TYPE_USHORT:
5952 align = REG_ALIGNOF_SHORT;
5953 break;
5954 case TYPE_INT:
5955 case TYPE_UINT:
5956 case TYPE_ENUM:
5957 align = REG_ALIGNOF_INT;
5958 break;
5959 case TYPE_LONG:
5960 case TYPE_ULONG:
5961 align = REG_ALIGNOF_LONG;
5962 break;
5963 case TYPE_POINTER:
5964 align = REG_ALIGNOF_POINTER;
5965 break;
5966 case TYPE_PRODUCT:
5967 case TYPE_OVERLAP:
5968 {
5969 size_t left_align, right_align;
5970 left_align = reg_align_of(state, type->left);
5971 right_align = reg_align_of(state, type->right);
5972 align = (left_align >= right_align) ? left_align : right_align;
5973 break;
5974 }
5975 case TYPE_ARRAY:
5976 align = reg_align_of(state, type->left);
5977 break;
5978 case TYPE_STRUCT:
5979 case TYPE_UNION:
5980 case TYPE_TUPLE:
5981 case TYPE_JOIN:
5982 align = reg_align_of(state, type->left);
5983 break;
5984 default:
5985 error(state, 0, "alignof not yet defined for type\n");
5986 break;
5987 }
5988 return align;
5989}
5990
5991static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5992{
5993 return bits_to_bytes(align_of(state, type));
5994}
5995static size_t size_of(struct compile_state *state, struct type *type);
5996static size_t reg_size_of(struct compile_state *state, struct type *type);
5997
5998static size_t needed_padding(struct compile_state *state,
5999 struct type *type, size_t offset)
6000{
6001 size_t padding, align;
6002 align = align_of(state, type);
6003 /* Align to the next machine word if the bitfield does completely
6004 * fit into the current word.
6005 */
6006 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
6007 size_t size;
6008 size = size_of(state, type);
6009 if ((offset + type->elements)/size != offset/size) {
6010 align = size;
6011 }
6012 }
Eric Biederman03b59862003-06-24 14:27:37 +00006013 padding = 0;
6014 if (offset % align) {
6015 padding = align - (offset % align);
6016 }
6017 return padding;
6018}
Eric Biederman90089602004-05-28 14:11:54 +00006019
6020static size_t reg_needed_padding(struct compile_state *state,
6021 struct type *type, size_t offset)
6022{
6023 size_t padding, align;
6024 align = reg_align_of(state, type);
6025 /* Align to the next register word if the bitfield does completely
6026 * fit into the current register.
6027 */
6028 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
6029 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
6030 {
6031 align = REG_SIZEOF_REG;
6032 }
6033 padding = 0;
6034 if (offset % align) {
6035 padding = align - (offset % align);
6036 }
6037 return padding;
6038}
6039
Eric Biedermanb138ac82003-04-22 18:44:01 +00006040static size_t size_of(struct compile_state *state, struct type *type)
6041{
6042 size_t size;
6043 size = 0;
6044 switch(type->type & TYPE_MASK) {
6045 case TYPE_VOID:
6046 size = 0;
6047 break;
Eric Biederman90089602004-05-28 14:11:54 +00006048 case TYPE_BITFIELD:
6049 size = type->elements;
6050 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006051 case TYPE_CHAR:
6052 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006053 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006054 break;
6055 case TYPE_SHORT:
6056 case TYPE_USHORT:
6057 size = SIZEOF_SHORT;
6058 break;
6059 case TYPE_INT:
6060 case TYPE_UINT:
6061 case TYPE_ENUM:
6062 size = SIZEOF_INT;
6063 break;
6064 case TYPE_LONG:
6065 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006066 size = SIZEOF_LONG;
6067 break;
Eric Biederman90089602004-05-28 14:11:54 +00006068 case TYPE_POINTER:
6069 size = SIZEOF_POINTER;
6070 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006071 case TYPE_PRODUCT:
6072 {
Eric Biederman90089602004-05-28 14:11:54 +00006073 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006074 size = 0;
6075 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006076 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006077 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006078 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006079 }
Eric Biederman90089602004-05-28 14:11:54 +00006080 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006081 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006082 break;
6083 }
6084 case TYPE_OVERLAP:
6085 {
6086 size_t size_left, size_right;
6087 size_left = size_of(state, type->left);
6088 size_right = size_of(state, type->right);
6089 size = (size_left >= size_right)? size_left : size_right;
6090 break;
6091 }
6092 case TYPE_ARRAY:
6093 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6094 internal_error(state, 0, "Invalid array type");
6095 } else {
6096 size = size_of(state, type->left) * type->elements;
6097 }
6098 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006099 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006100 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006101 {
Eric Biederman90089602004-05-28 14:11:54 +00006102 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006103 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006104 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006105 pad = needed_padding(state, type, size);
6106 size = size + pad;
6107 break;
6108 }
6109 case TYPE_UNION:
6110 case TYPE_JOIN:
6111 {
6112 size_t pad;
6113 size = size_of(state, type->left);
6114 /* Pad unions so their size is a multiple of their alignment */
6115 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006116 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006117 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006118 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006119 default:
Eric Biederman90089602004-05-28 14:11:54 +00006120 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006121 break;
6122 }
6123 return size;
6124}
6125
Eric Biederman90089602004-05-28 14:11:54 +00006126static size_t reg_size_of(struct compile_state *state, struct type *type)
6127{
6128 size_t size;
6129 size = 0;
6130 switch(type->type & TYPE_MASK) {
6131 case TYPE_VOID:
6132 size = 0;
6133 break;
6134 case TYPE_BITFIELD:
6135 size = type->elements;
6136 break;
6137 case TYPE_CHAR:
6138 case TYPE_UCHAR:
6139 size = REG_SIZEOF_CHAR;
6140 break;
6141 case TYPE_SHORT:
6142 case TYPE_USHORT:
6143 size = REG_SIZEOF_SHORT;
6144 break;
6145 case TYPE_INT:
6146 case TYPE_UINT:
6147 case TYPE_ENUM:
6148 size = REG_SIZEOF_INT;
6149 break;
6150 case TYPE_LONG:
6151 case TYPE_ULONG:
6152 size = REG_SIZEOF_LONG;
6153 break;
6154 case TYPE_POINTER:
6155 size = REG_SIZEOF_POINTER;
6156 break;
6157 case TYPE_PRODUCT:
6158 {
6159 size_t pad;
6160 size = 0;
6161 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6162 pad = reg_needed_padding(state, type->left, size);
6163 size = size + pad + reg_size_of(state, type->left);
6164 type = type->right;
6165 }
6166 pad = reg_needed_padding(state, type, size);
6167 size = size + pad + reg_size_of(state, type);
6168 break;
6169 }
6170 case TYPE_OVERLAP:
6171 {
6172 size_t size_left, size_right;
6173 size_left = reg_size_of(state, type->left);
6174 size_right = reg_size_of(state, type->right);
6175 size = (size_left >= size_right)? size_left : size_right;
6176 break;
6177 }
6178 case TYPE_ARRAY:
6179 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6180 internal_error(state, 0, "Invalid array type");
6181 } else {
6182 size = reg_size_of(state, type->left) * type->elements;
6183 }
6184 break;
6185 case TYPE_STRUCT:
6186 case TYPE_TUPLE:
6187 {
6188 size_t pad;
6189 size = reg_size_of(state, type->left);
6190 /* Pad structures so their size is a multiples of their alignment */
6191 pad = reg_needed_padding(state, type, size);
6192 size = size + pad;
6193 break;
6194 }
6195 case TYPE_UNION:
6196 case TYPE_JOIN:
6197 {
6198 size_t pad;
6199 size = reg_size_of(state, type->left);
6200 /* Pad unions so their size is a multiple of their alignment */
6201 pad = reg_needed_padding(state, type, size);
6202 size = size + pad;
6203 break;
6204 }
6205 default:
6206 internal_error(state, 0, "sizeof not yet defined for type");
6207 break;
6208 }
6209 return size;
6210}
6211
6212static size_t registers_of(struct compile_state *state, struct type *type)
6213{
6214 size_t registers;
6215 registers = reg_size_of(state, type);
6216 registers += REG_SIZEOF_REG - 1;
6217 registers /= REG_SIZEOF_REG;
6218 return registers;
6219}
6220
6221static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6222{
6223 return bits_to_bytes(size_of(state, type));
6224}
6225
Eric Biederman0babc1c2003-05-09 02:39:00 +00006226static size_t field_offset(struct compile_state *state,
6227 struct type *type, struct hash_entry *field)
6228{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006229 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006230 size_t size;
6231
Eric Biederman0babc1c2003-05-09 02:39:00 +00006232 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006233 member = 0;
6234 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6235 member = type->left;
6236 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6237 size += needed_padding(state, member->left, size);
6238 if (member->left->field_ident == field) {
6239 member = member->left;
6240 break;
6241 }
6242 size += size_of(state, member->left);
6243 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006244 }
Eric Biederman90089602004-05-28 14:11:54 +00006245 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006246 }
Eric Biederman90089602004-05-28 14:11:54 +00006247 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6248 member = type->left;
6249 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6250 if (member->left->field_ident == field) {
6251 member = member->left;
6252 break;
6253 }
6254 member = member->right;
6255 }
6256 }
6257 else {
6258 internal_error(state, 0, "field_offset only works on structures and unions");
6259 }
6260
6261 if (!member || (member->field_ident != field)) {
6262 error(state, 0, "member %s not present", field->name);
6263 }
6264 return size;
6265}
6266
6267static size_t field_reg_offset(struct compile_state *state,
6268 struct type *type, struct hash_entry *field)
6269{
6270 struct type *member;
6271 size_t size;
6272
6273 size = 0;
6274 member = 0;
6275 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6276 member = type->left;
6277 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6278 size += reg_needed_padding(state, member->left, size);
6279 if (member->left->field_ident == field) {
6280 member = member->left;
6281 break;
6282 }
6283 size += reg_size_of(state, member->left);
6284 member = member->right;
6285 }
6286 }
6287 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6288 member = type->left;
6289 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6290 if (member->left->field_ident == field) {
6291 member = member->left;
6292 break;
6293 }
6294 member = member->right;
6295 }
6296 }
6297 else {
6298 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6299 }
6300
6301 size += reg_needed_padding(state, member, size);
6302 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006303 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006304 }
6305 return size;
6306}
6307
6308static struct type *field_type(struct compile_state *state,
6309 struct type *type, struct hash_entry *field)
6310{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006311 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006312
6313 member = 0;
6314 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6315 member = type->left;
6316 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6317 if (member->left->field_ident == field) {
6318 member = member->left;
6319 break;
6320 }
6321 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006322 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006323 }
Eric Biederman90089602004-05-28 14:11:54 +00006324 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6325 member = type->left;
6326 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6327 if (member->left->field_ident == field) {
6328 member = member->left;
6329 break;
6330 }
6331 member = member->right;
6332 }
6333 }
6334 else {
6335 internal_error(state, 0, "field_type only works on structures and unions");
6336 }
6337
6338 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006339 error(state, 0, "member %s not present", field->name);
6340 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006341 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006342}
6343
Eric Biederman90089602004-05-28 14:11:54 +00006344static size_t index_offset(struct compile_state *state,
6345 struct type *type, ulong_t index)
6346{
6347 struct type *member;
6348 size_t size;
6349 size = 0;
6350 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6351 size = size_of(state, type->left) * index;
6352 }
6353 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6354 ulong_t i;
6355 member = type->left;
6356 i = 0;
6357 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6358 size += needed_padding(state, member->left, size);
6359 if (i == index) {
6360 member = member->left;
6361 break;
6362 }
6363 size += size_of(state, member->left);
6364 i++;
6365 member = member->right;
6366 }
6367 size += needed_padding(state, member, size);
6368 if (i != index) {
6369 internal_error(state, 0, "Missing member index: %u", index);
6370 }
6371 }
6372 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6373 ulong_t i;
6374 size = 0;
6375 member = type->left;
6376 i = 0;
6377 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6378 if (i == index) {
6379 member = member->left;
6380 break;
6381 }
6382 i++;
6383 member = member->right;
6384 }
6385 if (i != index) {
6386 internal_error(state, 0, "Missing member index: %u", index);
6387 }
6388 }
6389 else {
6390 internal_error(state, 0,
6391 "request for index %u in something not an array, tuple or join",
6392 index);
6393 }
6394 return size;
6395}
6396
6397static size_t index_reg_offset(struct compile_state *state,
6398 struct type *type, ulong_t index)
6399{
6400 struct type *member;
6401 size_t size;
6402 size = 0;
6403 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6404 size = reg_size_of(state, type->left) * index;
6405 }
6406 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6407 ulong_t i;
6408 member = type->left;
6409 i = 0;
6410 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6411 size += reg_needed_padding(state, member->left, size);
6412 if (i == index) {
6413 member = member->left;
6414 break;
6415 }
6416 size += reg_size_of(state, member->left);
6417 i++;
6418 member = member->right;
6419 }
6420 size += reg_needed_padding(state, member, size);
6421 if (i != index) {
6422 internal_error(state, 0, "Missing member index: %u", index);
6423 }
6424
6425 }
6426 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6427 ulong_t i;
6428 size = 0;
6429 member = type->left;
6430 i = 0;
6431 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6432 if (i == index) {
6433 member = member->left;
6434 break;
6435 }
6436 i++;
6437 member = member->right;
6438 }
6439 if (i != index) {
6440 internal_error(state, 0, "Missing member index: %u", index);
6441 }
6442 }
6443 else {
6444 internal_error(state, 0,
6445 "request for index %u in something not an array, tuple or join",
6446 index);
6447 }
6448 return size;
6449}
6450
6451static struct type *index_type(struct compile_state *state,
6452 struct type *type, ulong_t index)
6453{
6454 struct type *member;
6455 if (index >= type->elements) {
6456 internal_error(state, 0, "Invalid element %u requested", index);
6457 }
6458 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6459 member = type->left;
6460 }
6461 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6462 ulong_t i;
6463 member = type->left;
6464 i = 0;
6465 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6466 if (i == index) {
6467 member = member->left;
6468 break;
6469 }
6470 i++;
6471 member = member->right;
6472 }
6473 if (i != index) {
6474 internal_error(state, 0, "Missing member index: %u", index);
6475 }
6476 }
6477 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6478 ulong_t i;
6479 member = type->left;
6480 i = 0;
6481 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6482 if (i == index) {
6483 member = member->left;
6484 break;
6485 }
6486 i++;
6487 member = member->right;
6488 }
6489 if (i != index) {
6490 internal_error(state, 0, "Missing member index: %u", index);
6491 }
6492 }
6493 else {
6494 member = 0;
6495 internal_error(state, 0,
6496 "request for index %u in something not an array, tuple or join",
6497 index);
6498 }
6499 return member;
6500}
6501
6502static struct type *unpack_type(struct compile_state *state, struct type *type)
6503{
6504 /* If I have a single register compound type not a bit-field
6505 * find the real type.
6506 */
6507 struct type *start_type;
6508 size_t size;
6509 /* Get out early if I need multiple registers for this type */
6510 size = reg_size_of(state, type);
6511 if (size > REG_SIZEOF_REG) {
6512 return type;
6513 }
6514 /* Get out early if I don't need any registers for this type */
6515 if (size == 0) {
6516 return &void_type;
6517 }
6518 /* Loop until I have no more layers I can remove */
6519 do {
6520 start_type = type;
6521 switch(type->type & TYPE_MASK) {
6522 case TYPE_ARRAY:
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 break;
6530 case TYPE_STRUCT:
6531 case TYPE_TUPLE:
6532 /* If I have a single element the unpacked type
6533 * is that element.
6534 */
6535 if (type->elements == 1) {
6536 type = type->left;
6537 }
6538 /* If I have multiple elements the unpacked
6539 * type is the non-void element.
6540 */
6541 else {
6542 struct type *next, *member;
6543 struct type *sub_type;
6544 sub_type = 0;
6545 next = type->left;
6546 while(next) {
6547 member = next;
6548 next = 0;
6549 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6550 next = member->right;
6551 member = member->left;
6552 }
6553 if (reg_size_of(state, member) > 0) {
6554 if (sub_type) {
6555 internal_error(state, 0, "true compound type in a register");
6556 }
6557 sub_type = member;
6558 }
6559 }
6560 if (sub_type) {
6561 type = sub_type;
6562 }
6563 }
6564 break;
6565
6566 case TYPE_UNION:
6567 case TYPE_JOIN:
6568 /* If I have a single element the unpacked type
6569 * is that element.
6570 */
6571 if (type->elements == 1) {
6572 type = type->left;
6573 }
6574 /* I can't in general unpack union types */
6575 break;
6576 default:
6577 /* If I'm not a compound type I can't unpack it */
6578 break;
6579 }
6580 } while(start_type != type);
6581 switch(type->type & TYPE_MASK) {
6582 case TYPE_STRUCT:
6583 case TYPE_ARRAY:
6584 case TYPE_TUPLE:
6585 internal_error(state, 0, "irredicible type?");
6586 break;
6587 }
6588 return type;
6589}
6590
6591static int equiv_types(struct type *left, struct type *right);
6592static int is_compound_type(struct type *type);
6593
6594static struct type *reg_type(
6595 struct compile_state *state, struct type *type, int reg_offset)
6596{
6597 struct type *member;
6598 size_t size;
6599#if 1
6600 struct type *invalid;
6601 invalid = invalid_type(state, type);
6602 if (invalid) {
6603 fprintf(state->errout, "type: ");
6604 name_of(state->errout, type);
6605 fprintf(state->errout, "\n");
6606 fprintf(state->errout, "invalid: ");
6607 name_of(state->errout, invalid);
6608 fprintf(state->errout, "\n");
6609 internal_error(state, 0, "bad input type?");
6610 }
6611#endif
6612
6613 size = reg_size_of(state, type);
6614 if (reg_offset > size) {
6615 member = 0;
6616 fprintf(state->errout, "type: ");
6617 name_of(state->errout, type);
6618 fprintf(state->errout, "\n");
6619 internal_error(state, 0, "offset outside of type");
6620 }
6621 else {
6622 switch(type->type & TYPE_MASK) {
6623 /* Don't do anything with the basic types */
6624 case TYPE_VOID:
6625 case TYPE_CHAR: case TYPE_UCHAR:
6626 case TYPE_SHORT: case TYPE_USHORT:
6627 case TYPE_INT: case TYPE_UINT:
6628 case TYPE_LONG: case TYPE_ULONG:
6629 case TYPE_LLONG: case TYPE_ULLONG:
6630 case TYPE_FLOAT: case TYPE_DOUBLE:
6631 case TYPE_LDOUBLE:
6632 case TYPE_POINTER:
6633 case TYPE_ENUM:
6634 case TYPE_BITFIELD:
6635 member = type;
6636 break;
6637 case TYPE_ARRAY:
6638 member = type->left;
6639 size = reg_size_of(state, member);
6640 if (size > REG_SIZEOF_REG) {
6641 member = reg_type(state, member, reg_offset % size);
6642 }
6643 break;
6644 case TYPE_STRUCT:
6645 case TYPE_TUPLE:
6646 {
6647 size_t offset;
6648 offset = 0;
6649 member = type->left;
6650 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6651 size = reg_size_of(state, member->left);
6652 offset += reg_needed_padding(state, member->left, offset);
6653 if ((offset + size) > reg_offset) {
6654 member = member->left;
6655 break;
6656 }
6657 offset += size;
6658 member = member->right;
6659 }
6660 offset += reg_needed_padding(state, member, offset);
6661 member = reg_type(state, member, reg_offset - offset);
6662 break;
6663 }
6664 case TYPE_UNION:
6665 case TYPE_JOIN:
6666 {
6667 struct type *join, **jnext, *mnext;
6668 join = new_type(TYPE_JOIN, 0, 0);
6669 jnext = &join->left;
6670 mnext = type->left;
6671 while(mnext) {
6672 size_t size;
6673 member = mnext;
6674 mnext = 0;
6675 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6676 mnext = member->right;
6677 member = member->left;
6678 }
6679 size = reg_size_of(state, member);
6680 if (size > reg_offset) {
6681 struct type *part, *hunt;
6682 part = reg_type(state, member, reg_offset);
6683 /* See if this type is already in the union */
6684 hunt = join->left;
6685 while(hunt) {
6686 struct type *test = hunt;
6687 hunt = 0;
6688 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6689 hunt = test->right;
6690 test = test->left;
6691 }
6692 if (equiv_types(part, test)) {
6693 goto next;
6694 }
6695 }
6696 /* Nope add it */
6697 if (!*jnext) {
6698 *jnext = part;
6699 } else {
6700 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6701 jnext = &(*jnext)->right;
6702 }
6703 join->elements++;
6704 }
6705 next:
6706 ;
6707 }
6708 if (join->elements == 0) {
6709 internal_error(state, 0, "No elements?");
6710 }
6711 member = join;
6712 break;
6713 }
6714 default:
6715 member = 0;
6716 fprintf(state->errout, "type: ");
6717 name_of(state->errout, type);
6718 fprintf(state->errout, "\n");
6719 internal_error(state, 0, "reg_type not yet defined for type");
6720
6721 }
6722 }
6723 /* If I have a single register compound type not a bit-field
6724 * find the real type.
6725 */
6726 member = unpack_type(state, member);
6727 ;
6728 size = reg_size_of(state, member);
6729 if (size > REG_SIZEOF_REG) {
6730 internal_error(state, 0, "Cannot find type of single register");
6731 }
6732#if 1
6733 invalid = invalid_type(state, member);
6734 if (invalid) {
6735 fprintf(state->errout, "type: ");
6736 name_of(state->errout, member);
6737 fprintf(state->errout, "\n");
6738 fprintf(state->errout, "invalid: ");
6739 name_of(state->errout, invalid);
6740 fprintf(state->errout, "\n");
6741 internal_error(state, 0, "returning bad type?");
6742 }
6743#endif
6744 return member;
6745}
6746
Eric Biederman03b59862003-06-24 14:27:37 +00006747static struct type *next_field(struct compile_state *state,
6748 struct type *type, struct type *prev_member)
6749{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006750 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006751 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6752 internal_error(state, 0, "next_field only works on structures");
6753 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006754 member = type->left;
6755 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006756 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006757 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006758 break;
6759 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006760 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006761 prev_member = 0;
6762 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006763 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006764 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006765 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006766 prev_member = 0;
6767 }
6768 if (prev_member) {
6769 internal_error(state, 0, "prev_member %s not present",
6770 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006771 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006772 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006773}
6774
Eric Biederman90089602004-05-28 14:11:54 +00006775typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
6776 size_t ret_offset, size_t mem_offset, void *arg);
6777
6778static void walk_type_fields(struct compile_state *state,
6779 struct type *type, size_t reg_offset, size_t mem_offset,
6780 walk_type_fields_cb_t cb, void *arg);
6781
6782static void walk_struct_fields(struct compile_state *state,
6783 struct type *type, size_t reg_offset, size_t mem_offset,
6784 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006785{
Eric Biederman90089602004-05-28 14:11:54 +00006786 struct type *tptr;
6787 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006788 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006789 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006790 }
Eric Biederman90089602004-05-28 14:11:54 +00006791 tptr = type->left;
6792 for(i = 0; i < type->elements; i++) {
6793 struct type *mtype;
6794 mtype = tptr;
6795 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6796 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006797 }
Eric Biederman90089602004-05-28 14:11:54 +00006798 walk_type_fields(state, mtype,
6799 reg_offset +
6800 field_reg_offset(state, type, mtype->field_ident),
6801 mem_offset +
6802 field_offset(state, type, mtype->field_ident),
6803 cb, arg);
6804 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006805 }
Eric Biederman90089602004-05-28 14:11:54 +00006806
6807}
6808
6809static void walk_type_fields(struct compile_state *state,
6810 struct type *type, size_t reg_offset, size_t mem_offset,
6811 walk_type_fields_cb_t cb, void *arg)
6812{
6813 switch(type->type & TYPE_MASK) {
6814 case TYPE_STRUCT:
6815 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6816 break;
6817 case TYPE_CHAR:
6818 case TYPE_UCHAR:
6819 case TYPE_SHORT:
6820 case TYPE_USHORT:
6821 case TYPE_INT:
6822 case TYPE_UINT:
6823 case TYPE_LONG:
6824 case TYPE_ULONG:
6825 cb(state, type, reg_offset, mem_offset, arg);
6826 break;
6827 case TYPE_VOID:
6828 break;
6829 default:
6830 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006831 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006832}
6833
Eric Biedermanb138ac82003-04-22 18:44:01 +00006834static void arrays_complete(struct compile_state *state, struct type *type)
6835{
6836 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6837 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6838 error(state, 0, "array size not specified");
6839 }
6840 arrays_complete(state, type->left);
6841 }
6842}
6843
Eric Biederman90089602004-05-28 14:11:54 +00006844static unsigned int get_basic_type(struct type *type)
6845{
6846 unsigned int basic;
6847 basic = type->type & TYPE_MASK;
6848 /* Convert enums to ints */
6849 if (basic == TYPE_ENUM) {
6850 basic = TYPE_INT;
6851 }
6852 /* Convert bitfields to standard types */
6853 else if (basic == TYPE_BITFIELD) {
6854 if (type->elements <= SIZEOF_CHAR) {
6855 basic = TYPE_CHAR;
6856 }
6857 else if (type->elements <= SIZEOF_SHORT) {
6858 basic = TYPE_SHORT;
6859 }
6860 else if (type->elements <= SIZEOF_INT) {
6861 basic = TYPE_INT;
6862 }
6863 else if (type->elements <= SIZEOF_LONG) {
6864 basic = TYPE_LONG;
6865 }
6866 if (!TYPE_SIGNED(type->left->type)) {
6867 basic += 1;
6868 }
6869 }
6870 return basic;
6871}
6872
Eric Biedermanb138ac82003-04-22 18:44:01 +00006873static unsigned int do_integral_promotion(unsigned int type)
6874{
Eric Biederman83b991a2003-10-11 06:20:25 +00006875 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006876 type = TYPE_INT;
6877 }
6878 return type;
6879}
6880
6881static unsigned int do_arithmetic_conversion(
6882 unsigned int left, unsigned int right)
6883{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006884 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6885 return TYPE_LDOUBLE;
6886 }
6887 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6888 return TYPE_DOUBLE;
6889 }
6890 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6891 return TYPE_FLOAT;
6892 }
6893 left = do_integral_promotion(left);
6894 right = do_integral_promotion(right);
6895 /* If both operands have the same size done */
6896 if (left == right) {
6897 return left;
6898 }
6899 /* If both operands have the same signedness pick the larger */
6900 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6901 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6902 }
6903 /* If the signed type can hold everything use it */
6904 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6905 return left;
6906 }
6907 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6908 return right;
6909 }
6910 /* Convert to the unsigned type with the same rank as the signed type */
6911 else if (TYPE_SIGNED(left)) {
6912 return TYPE_MKUNSIGNED(left);
6913 }
6914 else {
6915 return TYPE_MKUNSIGNED(right);
6916 }
6917}
6918
6919/* see if two types are the same except for qualifiers */
6920static int equiv_types(struct type *left, struct type *right)
6921{
6922 unsigned int type;
6923 /* Error if the basic types do not match */
6924 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6925 return 0;
6926 }
6927 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006928 /* If the basic types match and it is a void type we are done */
6929 if (type == TYPE_VOID) {
6930 return 1;
6931 }
Eric Biederman90089602004-05-28 14:11:54 +00006932 /* For bitfields we need to compare the sizes */
6933 else if (type == TYPE_BITFIELD) {
6934 return (left->elements == right->elements) &&
6935 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6936 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006937 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006938 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006939 return 1;
6940 }
6941 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006942 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006943 return equiv_types(left->left, right->left);
6944 }
6945 else if (type == TYPE_ARRAY) {
6946 return (left->elements == right->elements) &&
6947 equiv_types(left->left, right->left);
6948 }
Eric Biederman90089602004-05-28 14:11:54 +00006949 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006950 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006951 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006952 }
Eric Biederman90089602004-05-28 14:11:54 +00006953 /* test for union equality */
6954 else if (type == TYPE_UNION) {
6955 return left->type_ident == right->type_ident;
6956 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006957 /* Test for equivalent functions */
6958 else if (type == TYPE_FUNCTION) {
6959 return equiv_types(left->left, right->left) &&
6960 equiv_types(left->right, right->right);
6961 }
6962 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006963 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006964 else if (type == TYPE_PRODUCT) {
6965 return equiv_types(left->left, right->left) &&
6966 equiv_types(left->right, right->right);
6967 }
Eric Biederman90089602004-05-28 14:11:54 +00006968 /* We should see TYPE_OVERLAP when comparing joins */
6969 else if (type == TYPE_OVERLAP) {
6970 return equiv_types(left->left, right->left) &&
6971 equiv_types(left->right, right->right);
6972 }
6973 /* Test for equivalence of tuples */
6974 else if (type == TYPE_TUPLE) {
6975 return (left->elements == right->elements) &&
6976 equiv_types(left->left, right->left);
6977 }
6978 /* Test for equivalence of joins */
6979 else if (type == TYPE_JOIN) {
6980 return (left->elements == right->elements) &&
6981 equiv_types(left->left, right->left);
6982 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006983 else {
6984 return 0;
6985 }
6986}
6987
6988static int equiv_ptrs(struct type *left, struct type *right)
6989{
6990 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6991 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6992 return 0;
6993 }
6994 return equiv_types(left->left, right->left);
6995}
6996
6997static struct type *compatible_types(struct type *left, struct type *right)
6998{
6999 struct type *result;
7000 unsigned int type, qual_type;
7001 /* Error if the basic types do not match */
7002 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
7003 return 0;
7004 }
7005 type = left->type & TYPE_MASK;
7006 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7007 result = 0;
7008 /* if the basic types match and it is an arithmetic type we are done */
7009 if (TYPE_ARITHMETIC(type)) {
7010 result = new_type(qual_type, 0, 0);
7011 }
7012 /* If it is a pointer type recurse and keep testing */
7013 else if (type == TYPE_POINTER) {
7014 result = compatible_types(left->left, right->left);
7015 if (result) {
7016 result = new_type(qual_type, result, 0);
7017 }
7018 }
Eric Biederman90089602004-05-28 14:11:54 +00007019 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007020 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007021 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007022 result = left;
7023 }
7024 }
Eric Biederman90089602004-05-28 14:11:54 +00007025 /* test for union equality */
7026 else if (type == TYPE_UNION) {
7027 if (left->type_ident == right->type_ident) {
7028 result = left;
7029 }
7030 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007031 /* Test for equivalent functions */
7032 else if (type == TYPE_FUNCTION) {
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 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7041 else if (type == TYPE_PRODUCT) {
7042 struct type *lf, *rf;
7043 lf = compatible_types(left->left, right->left);
7044 rf = compatible_types(left->right, right->right);
7045 if (lf && rf) {
7046 result = new_type(qual_type, lf, rf);
7047 }
7048 }
7049 else {
7050 /* Nothing else is compatible */
7051 }
7052 return result;
7053}
7054
Eric Biederman90089602004-05-28 14:11:54 +00007055/* See if left is a equivalent to right or right is a union member of left */
7056static int is_subset_type(struct type *left, struct type *right)
7057{
7058 if (equiv_types(left, right)) {
7059 return 1;
7060 }
7061 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7062 struct type *member, *mnext;
7063 mnext = left->left;
7064 while(mnext) {
7065 member = mnext;
7066 mnext = 0;
7067 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7068 mnext = member->right;
7069 member = member->left;
7070 }
7071 if (is_subset_type( member, right)) {
7072 return 1;
7073 }
7074 }
7075 }
7076 return 0;
7077}
7078
Eric Biedermanb138ac82003-04-22 18:44:01 +00007079static struct type *compatible_ptrs(struct type *left, struct type *right)
7080{
7081 struct type *result;
7082 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7083 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7084 return 0;
7085 }
7086 result = compatible_types(left->left, right->left);
7087 if (result) {
7088 unsigned int qual_type;
7089 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7090 result = new_type(qual_type, result, 0);
7091 }
7092 return result;
7093
7094}
7095static struct triple *integral_promotion(
7096 struct compile_state *state, struct triple *def)
7097{
7098 struct type *type;
7099 type = def->type;
7100 /* As all operations are carried out in registers
7101 * the values are converted on load I just convert
7102 * logical type of the operand.
7103 */
7104 if (TYPE_INTEGER(type->type)) {
7105 unsigned int int_type;
7106 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007107 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007108 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007109 if (def->op != OP_LOAD) {
7110 def->type = new_type(int_type, 0, 0);
7111 }
7112 else {
Eric Biederman90089602004-05-28 14:11:54 +00007113 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007114 new_type(int_type, 0, 0), def, 0);
7115 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007116 }
7117 }
7118 return def;
7119}
7120
7121
7122static void arithmetic(struct compile_state *state, struct triple *def)
7123{
7124 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007125 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007126 }
7127}
7128
7129static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7130{
7131 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7132 error(state, def, "pointer or arithmetic type expected");
7133 }
7134}
7135
7136static int is_integral(struct triple *ins)
7137{
7138 return TYPE_INTEGER(ins->type->type);
7139}
7140
7141static void integral(struct compile_state *state, struct triple *def)
7142{
7143 if (!is_integral(def)) {
7144 error(state, 0, "integral type expected");
7145 }
7146}
7147
7148
7149static void bool(struct compile_state *state, struct triple *def)
7150{
7151 if (!TYPE_ARITHMETIC(def->type->type) &&
7152 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7153 error(state, 0, "arithmetic or pointer type expected");
7154 }
7155}
7156
7157static int is_signed(struct type *type)
7158{
Eric Biederman90089602004-05-28 14:11:54 +00007159 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7160 type = type->left;
7161 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007162 return !!TYPE_SIGNED(type->type);
7163}
Eric Biederman90089602004-05-28 14:11:54 +00007164static int is_compound_type(struct type *type)
7165{
7166 int is_compound;
7167 switch((type->type & TYPE_MASK)) {
7168 case TYPE_ARRAY:
7169 case TYPE_STRUCT:
7170 case TYPE_TUPLE:
7171 case TYPE_UNION:
7172 case TYPE_JOIN:
7173 is_compound = 1;
7174 break;
7175 default:
7176 is_compound = 0;
7177 break;
7178 }
7179 return is_compound;
7180}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007181
Eric Biederman0babc1c2003-05-09 02:39:00 +00007182/* Is this value located in a register otherwise it must be in memory */
7183static int is_in_reg(struct compile_state *state, struct triple *def)
7184{
7185 int in_reg;
7186 if (def->op == OP_ADECL) {
7187 in_reg = 1;
7188 }
7189 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7190 in_reg = 0;
7191 }
Eric Biederman90089602004-05-28 14:11:54 +00007192 else if (triple_is_part(state, def)) {
7193 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007194 }
7195 else {
Eric Biederman90089602004-05-28 14:11:54 +00007196 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007197 in_reg = -1;
7198 }
7199 return in_reg;
7200}
7201
Eric Biederman90089602004-05-28 14:11:54 +00007202/* Is this an auto or static variable location? Something that can
7203 * be assigned to. Otherwise it must must be a pure value, a temporary.
7204 */
7205static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007206{
7207 int ret;
7208 ret = 0;
7209 if (!def) {
7210 return 0;
7211 }
7212 if ((def->op == OP_ADECL) ||
7213 (def->op == OP_SDECL) ||
7214 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007215 (def->op == OP_BLOBCONST) ||
7216 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007217 ret = 1;
7218 }
Eric Biederman90089602004-05-28 14:11:54 +00007219 else if (triple_is_part(state, def)) {
7220 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007221 }
7222 return ret;
7223}
7224
Eric Biederman00443072003-06-24 12:34:45 +00007225static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007226{
7227 if (!def) {
7228 internal_error(state, def, "nothing where lvalue expected?");
7229 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007230 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007231 error(state, def, "lvalue expected");
7232 }
7233}
Eric Biederman00443072003-06-24 12:34:45 +00007234static void lvalue(struct compile_state *state, struct triple *def)
7235{
7236 clvalue(state, def);
7237 if (def->type->type & QUAL_CONST) {
7238 error(state, def, "modifable lvalue expected");
7239 }
7240}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007241
7242static int is_pointer(struct triple *def)
7243{
7244 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7245}
7246
7247static void pointer(struct compile_state *state, struct triple *def)
7248{
7249 if (!is_pointer(def)) {
7250 error(state, def, "pointer expected");
7251 }
7252}
7253
7254static struct triple *int_const(
7255 struct compile_state *state, struct type *type, ulong_t value)
7256{
7257 struct triple *result;
7258 switch(type->type & TYPE_MASK) {
7259 case TYPE_CHAR:
7260 case TYPE_INT: case TYPE_UINT:
7261 case TYPE_LONG: case TYPE_ULONG:
7262 break;
7263 default:
Eric Biederman90089602004-05-28 14:11:54 +00007264 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007265 }
7266 result = triple(state, OP_INTCONST, type, 0, 0);
7267 result->u.cval = value;
7268 return result;
7269}
7270
7271
Eric Biederman83b991a2003-10-11 06:20:25 +00007272static struct triple *read_expr(struct compile_state *state, struct triple *def);
7273
Eric Biederman0babc1c2003-05-09 02:39:00 +00007274static struct triple *do_mk_addr_expr(struct compile_state *state,
7275 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007276{
7277 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007278 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007279 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007280
Eric Biederman90089602004-05-28 14:11:54 +00007281 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007282
Eric Biederman90089602004-05-28 14:11:54 +00007283
Eric Biedermanb138ac82003-04-22 18:44:01 +00007284 result = 0;
7285 if (expr->op == OP_ADECL) {
7286 error(state, expr, "address of auto variables not supported");
7287 }
7288 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007289 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007290 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007291 result->u.cval = offset;
7292 }
7293 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007294 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007295 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007296 int_const(state, &ulong_type, offset));
7297 }
Eric Biederman90089602004-05-28 14:11:54 +00007298 else if (expr->op == OP_BLOBCONST) {
7299 FINISHME();
7300 internal_error(state, expr, "not yet implemented");
7301 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007302 else if (expr->op == OP_LIST) {
7303 error(state, 0, "Function addresses not supported");
7304 }
Eric Biederman90089602004-05-28 14:11:54 +00007305 else if (triple_is_part(state, expr)) {
7306 struct triple *part;
7307 part = expr;
7308 expr = MISC(expr, 0);
7309 if (part->op == OP_DOT) {
7310 offset += bits_to_bytes(
7311 field_offset(state, expr->type, part->u.field));
7312 }
7313 else if (part->op == OP_INDEX) {
7314 offset += bits_to_bytes(
7315 index_offset(state, expr->type, part->u.cval));
7316 }
7317 else {
7318 internal_error(state, part, "unhandled part type");
7319 }
7320 result = do_mk_addr_expr(state, expr, type, offset);
7321 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007322 if (!result) {
7323 internal_error(state, expr, "cannot take address of expression");
7324 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007325 return result;
7326}
7327
Eric Biederman0babc1c2003-05-09 02:39:00 +00007328static struct triple *mk_addr_expr(
7329 struct compile_state *state, struct triple *expr, ulong_t offset)
7330{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007331 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007332}
7333
Eric Biedermanb138ac82003-04-22 18:44:01 +00007334static struct triple *mk_deref_expr(
7335 struct compile_state *state, struct triple *expr)
7336{
7337 struct type *base_type;
7338 pointer(state, expr);
7339 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007340 return triple(state, OP_DEREF, base_type, expr, 0);
7341}
7342
Eric Biederman90089602004-05-28 14:11:54 +00007343/* lvalue conversions always apply except when certain operators
7344 * are applied. So I apply apply it when I know no more
7345 * operators will be applied.
7346 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007347static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007348{
Eric Biederman5cd81732004-03-11 15:01:31 +00007349 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007350 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7351 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007352 type = new_type(
7353 TYPE_POINTER | (def->type->type & QUAL_MASK),
7354 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007355 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007356 struct triple *addrconst;
7357 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7358 internal_error(state, def, "bad array constant");
7359 }
7360 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7361 MISC(addrconst, 0) = def;
7362 def = addrconst;
7363 }
7364 else {
Eric Biederman90089602004-05-28 14:11:54 +00007365 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007366 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007367 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007368 /* Transform a function to a pointer to it */
7369 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7370 def = mk_addr_expr(state, def, 0);
7371 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007372 return def;
7373}
7374
Eric Biederman0babc1c2003-05-09 02:39:00 +00007375static struct triple *deref_field(
7376 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7377{
7378 struct triple *result;
7379 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007380 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007381 if (!field) {
7382 internal_error(state, 0, "No field passed to deref_field");
7383 }
7384 result = 0;
7385 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007386 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7387 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007388 error(state, 0, "request for member %s in something not a struct or union",
7389 field->name);
7390 }
Eric Biederman03b59862003-06-24 14:27:37 +00007391 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007392 if ((type->type & STOR_MASK) == STOR_PERM) {
7393 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007394 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007395 result = do_mk_addr_expr(state, expr, member, offset);
7396 result = mk_deref_expr(state, result);
7397 }
7398 else {
7399 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007400 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007401 result->u.field = field;
7402 }
7403 return result;
7404}
7405
Eric Biederman90089602004-05-28 14:11:54 +00007406static struct triple *deref_index(
7407 struct compile_state *state, struct triple *expr, size_t index)
7408{
7409 struct triple *result;
7410 struct type *type, *member;
7411 ulong_t offset;
7412
7413 result = 0;
7414 type = expr->type;
7415 member = index_type(state, type, index);
7416
7417 if ((type->type & STOR_MASK) == STOR_PERM) {
7418 offset = bits_to_bytes(index_offset(state, type, index));
7419 result = do_mk_addr_expr(state, expr, member, offset);
7420 result = mk_deref_expr(state, result);
7421 }
7422 else {
7423 result = triple(state, OP_INDEX, member, expr, 0);
7424 result->u.cval = index;
7425 }
7426 return result;
7427}
7428
Eric Biedermanb138ac82003-04-22 18:44:01 +00007429static struct triple *read_expr(struct compile_state *state, struct triple *def)
7430{
7431 int op;
7432 if (!def) {
7433 return 0;
7434 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007435#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007436#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007437#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007438 /* Transform lvalues into something we can read */
7439 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007440 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007441 return def;
7442 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007443 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007444 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007445 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007446 if (def->op == OP_SDECL) {
7447 def = mk_addr_expr(state, def, 0);
7448 def = mk_deref_expr(state, def);
7449 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007450 op = OP_LOAD;
7451 }
Eric Biederman90089602004-05-28 14:11:54 +00007452 def = triple(state, op, def->type, def, 0);
7453 if (def->type->type & QUAL_VOLATILE) {
7454 def->id |= TRIPLE_FLAG_VOLATILE;
7455 }
7456 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007457}
7458
Eric Biedermane058a1e2003-07-12 01:21:31 +00007459int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007460 struct type *dest, struct type *rval)
7461{
7462 int compatible = 0;
7463 /* Both operands have arithmetic type */
7464 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7465 compatible = 1;
7466 }
7467 /* One operand is a pointer and the other is a pointer to void */
7468 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7469 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7470 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7471 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7472 compatible = 1;
7473 }
7474 /* If both types are the same without qualifiers we are good */
7475 else if (equiv_ptrs(dest, rval)) {
7476 compatible = 1;
7477 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007478 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007479 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007480 compatible = 1;
7481 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007482 return compatible;
7483}
7484
Eric Biedermane058a1e2003-07-12 01:21:31 +00007485static void write_compatible(struct compile_state *state,
7486 struct type *dest, struct type *rval)
7487{
7488 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007489 FILE *fp = state->errout;
7490 fprintf(fp, "dest: ");
7491 name_of(fp, dest);
7492 fprintf(fp,"\nrval: ");
7493 name_of(fp, rval);
7494 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007495 error(state, 0, "Incompatible types in assignment");
7496 }
7497}
7498
Eric Biedermane058a1e2003-07-12 01:21:31 +00007499static int is_init_compatible(struct compile_state *state,
7500 struct type *dest, struct type *rval)
7501{
7502 int compatible = 0;
7503 if (is_write_compatible(state, dest, rval)) {
7504 compatible = 1;
7505 }
7506 else if (equiv_types(dest, rval)) {
7507 compatible = 1;
7508 }
7509 return compatible;
7510}
7511
Eric Biedermanb138ac82003-04-22 18:44:01 +00007512static struct triple *write_expr(
7513 struct compile_state *state, struct triple *dest, struct triple *rval)
7514{
7515 struct triple *def;
7516 int op;
7517
7518 def = 0;
7519 if (!rval) {
7520 internal_error(state, 0, "missing rval");
7521 }
7522
7523 if (rval->op == OP_LIST) {
7524 internal_error(state, 0, "expression of type OP_LIST?");
7525 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007526 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007527 internal_error(state, 0, "writing to a non lvalue?");
7528 }
Eric Biederman00443072003-06-24 12:34:45 +00007529 if (dest->type->type & QUAL_CONST) {
7530 internal_error(state, 0, "modifable lvalue expexted");
7531 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007532
7533 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007534 if (!equiv_types(dest->type, rval->type)) {
7535 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7536 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007537
7538 /* Now figure out which assignment operator to use */
7539 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007540 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007541 def = triple(state, OP_WRITE, dest->type, rval, dest);
7542 if (MISC(def, 0) != dest) {
7543 internal_error(state, def, "huh?");
7544 }
7545 if (RHS(def, 0) != rval) {
7546 internal_error(state, def, "huh?");
7547 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007548 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007549 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007550 }
Eric Biederman90089602004-05-28 14:11:54 +00007551 if (def->type->type & QUAL_VOLATILE) {
7552 def->id |= TRIPLE_FLAG_VOLATILE;
7553 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007554 return def;
7555}
7556
7557static struct triple *init_expr(
7558 struct compile_state *state, struct triple *dest, struct triple *rval)
7559{
7560 struct triple *def;
7561
7562 def = 0;
7563 if (!rval) {
7564 internal_error(state, 0, "missing rval");
7565 }
7566 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7567 rval = read_expr(state, rval);
7568 def = write_expr(state, dest, rval);
7569 }
7570 else {
7571 /* Fill in the array size if necessary */
7572 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7573 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7574 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7575 dest->type->elements = rval->type->elements;
7576 }
7577 }
7578 if (!equiv_types(dest->type, rval->type)) {
7579 error(state, 0, "Incompatible types in inializer");
7580 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007581 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007582 insert_triple(state, dest, rval);
7583 rval->id |= TRIPLE_FLAG_FLATTENED;
7584 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007585 }
7586 return def;
7587}
7588
7589struct type *arithmetic_result(
7590 struct compile_state *state, struct triple *left, struct triple *right)
7591{
7592 struct type *type;
7593 /* Sanity checks to ensure I am working with arithmetic types */
7594 arithmetic(state, left);
7595 arithmetic(state, right);
7596 type = new_type(
7597 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007598 get_basic_type(left->type),
7599 get_basic_type(right->type)),
7600 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007601 return type;
7602}
7603
7604struct type *ptr_arithmetic_result(
7605 struct compile_state *state, struct triple *left, struct triple *right)
7606{
7607 struct type *type;
7608 /* Sanity checks to ensure I am working with the proper types */
7609 ptr_arithmetic(state, left);
7610 arithmetic(state, right);
7611 if (TYPE_ARITHMETIC(left->type->type) &&
7612 TYPE_ARITHMETIC(right->type->type)) {
7613 type = arithmetic_result(state, left, right);
7614 }
7615 else if (TYPE_PTR(left->type->type)) {
7616 type = left->type;
7617 }
7618 else {
7619 internal_error(state, 0, "huh?");
7620 type = 0;
7621 }
7622 return type;
7623}
7624
Eric Biedermanb138ac82003-04-22 18:44:01 +00007625/* boolean helper function */
7626
7627static struct triple *ltrue_expr(struct compile_state *state,
7628 struct triple *expr)
7629{
7630 switch(expr->op) {
7631 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7632 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7633 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7634 /* If the expression is already boolean do nothing */
7635 break;
7636 default:
7637 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7638 break;
7639 }
7640 return expr;
7641}
7642
7643static struct triple *lfalse_expr(struct compile_state *state,
7644 struct triple *expr)
7645{
7646 return triple(state, OP_LFALSE, &int_type, expr, 0);
7647}
7648
Eric Biederman90089602004-05-28 14:11:54 +00007649static struct triple *mkland_expr(
7650 struct compile_state *state,
7651 struct triple *left, struct triple *right)
7652{
7653 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007654 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007655
7656 /* Generate some intermediate triples */
7657 end = label(state);
7658 var = variable(state, &int_type);
7659
7660 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007661 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007662
7663 /* Jump if the value is false */
7664 jmp = branch(state, end,
7665 lfalse_expr(state, read_expr(state, var)));
7666 mid = label(state);
7667
7668 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007669 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007670
7671 /* An expression for the computed value */
7672 val = read_expr(state, var);
7673
7674 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007675 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007676
7677 return def;
7678}
7679
7680static struct triple *mklor_expr(
7681 struct compile_state *state,
7682 struct triple *left, struct triple *right)
7683{
7684 struct triple *def, *val, *var, *jmp, *mid, *end;
7685
7686 /* Generate some intermediate triples */
7687 end = label(state);
7688 var = variable(state, &int_type);
7689
7690 /* Store the left hand side value */
7691 left = write_expr(state, var, left);
7692
7693 /* Jump if the value is true */
7694 jmp = branch(state, end, read_expr(state, var));
7695 mid = label(state);
7696
7697 /* Store the right hand side value */
7698 right = write_expr(state, var, right);
7699
7700 /* An expression for the computed value*/
7701 val = read_expr(state, var);
7702
7703 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007704 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007705
7706 return def;
7707}
7708
7709static struct triple *mkcond_expr(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007710 struct compile_state *state,
7711 struct triple *test, struct triple *left, struct triple *right)
7712{
Eric Biederman90089602004-05-28 14:11:54 +00007713 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007714 struct type *result_type;
7715 unsigned int left_type, right_type;
7716 bool(state, test);
7717 left_type = left->type->type;
7718 right_type = right->type->type;
7719 result_type = 0;
7720 /* Both operands have arithmetic type */
7721 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7722 result_type = arithmetic_result(state, left, right);
7723 }
7724 /* Both operands have void type */
7725 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7726 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7727 result_type = &void_type;
7728 }
7729 /* pointers to the same type... */
7730 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7731 ;
7732 }
7733 /* Both operands are pointers and left is a pointer to void */
7734 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7735 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7736 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7737 result_type = right->type;
7738 }
7739 /* Both operands are pointers and right is a pointer to void */
7740 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7741 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7742 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7743 result_type = left->type;
7744 }
7745 if (!result_type) {
7746 error(state, 0, "Incompatible types in conditional expression");
7747 }
Eric Biederman90089602004-05-28 14:11:54 +00007748 /* Generate some intermediate triples */
7749 mid = label(state);
7750 end = label(state);
7751 var = variable(state, result_type);
7752
7753 /* Branch if the test is false */
7754 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7755 top = label(state);
7756
7757 /* Store the left hand side value */
7758 left = write_expr(state, var, left);
7759
7760 /* Branch to the end */
7761 jmp2 = branch(state, end, 0);
7762
7763 /* Store the right hand side value */
7764 right = write_expr(state, var, right);
7765
7766 /* An expression for the computed value */
7767 val = read_expr(state, var);
7768
7769 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007770 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007771
Eric Biedermanb138ac82003-04-22 18:44:01 +00007772 return def;
7773}
7774
7775
Eric Biederman0babc1c2003-05-09 02:39:00 +00007776static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007777{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007778#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007779#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007780#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007781 int count;
7782 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007783 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7784 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007785 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007786 else if (ins->op == OP_DEREF) {
7787 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007788 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007789 else if (ins->op == OP_VAL) {
7790 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007791 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007792 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007793 /* Don't figure the depth of a call just guess it is huge */
7794 count = 1000;
7795 }
7796 else {
7797 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007798 expr = triple_rhs(state, ins, 0);
7799 for(;expr; expr = triple_rhs(state, ins, expr)) {
7800 if (*expr) {
7801 int depth;
7802 depth = expr_depth(state, *expr);
7803 if (depth > count) {
7804 count = depth;
7805 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007806 }
7807 }
7808 }
7809 return count + 1;
7810}
7811
Eric Biederman0babc1c2003-05-09 02:39:00 +00007812static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007813 struct compile_state *state, struct triple *first, struct triple *ptr,
7814 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007815{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007816 struct rhs_vector {
7817 int depth;
7818 struct triple **ins;
7819 } vector[MAX_RHS];
7820 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007821 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007822 rhs = ptr->rhs;
7823 lhs = ptr->lhs;
7824 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007825 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007826 ptr->op, tops(ptr->op));
7827 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007828 /* Find the depth of the rhs elements */
7829 for(i = 0; i < rhs; i++) {
7830 vector[i].ins = &RHS(ptr, i);
7831 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007832 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007833 /* Selection sort the rhs */
7834 for(i = 0; i < rhs; i++) {
7835 int j, max = i;
7836 for(j = i + 1; j < rhs; j++ ) {
7837 if (vector[j].depth > vector[max].depth) {
7838 max = j;
7839 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007840 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007841 if (max != i) {
7842 struct rhs_vector tmp;
7843 tmp = vector[i];
7844 vector[i] = vector[max];
7845 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007846 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007847 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007848 /* Now flatten the rhs elements */
7849 for(i = 0; i < rhs; i++) {
7850 *vector[i].ins = flatten(state, first, *vector[i].ins);
7851 use_triple(*vector[i].ins, ptr);
7852 }
Eric Biederman90089602004-05-28 14:11:54 +00007853 if (lhs) {
7854 insert_triple(state, first, ptr);
7855 ptr->id |= TRIPLE_FLAG_FLATTENED;
7856 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7857
7858 /* Now flatten the lhs elements */
7859 for(i = 0; i < lhs; i++) {
7860 struct triple **ins = &LHS(ptr, i);
7861 *ins = flatten(state, first, *ins);
7862 use_triple(*ins, ptr);
7863 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007864 }
7865 return ptr;
7866}
7867
Eric Biederman90089602004-05-28 14:11:54 +00007868static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007869 struct compile_state *state, struct triple *first, struct triple *ptr)
7870{
Eric Biederman90089602004-05-28 14:11:54 +00007871 struct triple *head, *body, *val;
7872 head = RHS(ptr, 0);
7873 RHS(ptr, 0) = 0;
7874 val = head->prev;
7875 body = head->next;
7876 release_triple(state, head);
7877 release_triple(state, ptr);
7878 val->next = first;
7879 body->prev = first->prev;
7880 body->prev->next = body;
7881 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007882
Eric Biederman90089602004-05-28 14:11:54 +00007883 if (triple_is_cbranch(state, body->prev) ||
7884 triple_is_call(state, body->prev)) {
7885 unuse_triple(first, body->prev);
7886 use_triple(body, body->prev);
7887 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007888
Eric Biederman90089602004-05-28 14:11:54 +00007889 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7890 internal_error(state, val, "val not flattened?");
7891 }
7892
7893 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007894}
7895
Eric Biederman90089602004-05-28 14:11:54 +00007896
7897static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007898 struct compile_state *state, struct triple *first, struct triple *ptr)
7899{
Eric Biederman90089602004-05-28 14:11:54 +00007900 if (!triple_is_part(state, ptr)) {
7901 internal_error(state, ptr, "not a part");
7902 }
7903 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7904 internal_error(state, ptr, "unexpected args for: %d %s",
7905 ptr->op, tops(ptr->op));
7906 }
7907 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7908 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007909 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007910}
7911
7912static struct triple *flatten(
7913 struct compile_state *state, struct triple *first, struct triple *ptr)
7914{
7915 struct triple *orig_ptr;
7916 if (!ptr)
7917 return 0;
7918 do {
7919 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007920 /* Only flatten triples once */
7921 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7922 return ptr;
7923 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007924 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007925 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007926 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7927 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007928 break;
Eric Biederman90089602004-05-28 14:11:54 +00007929 case OP_PROG:
7930 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007931 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007932 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007933 ptr = flatten_generic(state, first, ptr, 1);
7934 insert_triple(state, first, ptr);
7935 ptr->id |= TRIPLE_FLAG_FLATTENED;
7936 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7937 if (ptr->next != ptr) {
7938 use_triple(ptr->next, ptr);
7939 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007940 break;
7941 case OP_READ:
7942 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007943 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7944 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007945 break;
Eric Biederman90089602004-05-28 14:11:54 +00007946 case OP_WRITE:
7947 ptr = flatten_generic(state, first, ptr, 1);
7948 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7949 use_triple(MISC(ptr, 0), ptr);
7950 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007951 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007952 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007953 break;
7954 case OP_CBRANCH:
7955 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7956 use_triple(RHS(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);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007963 }
7964 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007965 case OP_CALL:
7966 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7967 use_triple(MISC(ptr, 0), ptr);
7968 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007969 insert_triple(state, first, ptr);
7970 ptr->id |= TRIPLE_FLAG_FLATTENED;
7971 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007972 if (ptr->next != ptr) {
7973 use_triple(ptr->next, ptr);
7974 }
7975 break;
7976 case OP_RET:
7977 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7978 use_triple(RHS(ptr, 0), ptr);
7979 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007980 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007981 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007982 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007983 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007984 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007985 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007986 break;
7987 case OP_DEREF:
7988 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007989 ptr = RHS(ptr, 0);
7990 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007991 free_triple(state, orig_ptr);
7992 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007993 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007994 if (RHS(ptr, 0)->op == OP_DEREF) {
7995 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007996 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007997 base = MISC(ptr, 0);
7998 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007999 left = RHS(base, 0);
8000 ptr = triple(state, OP_ADD, left->type,
8001 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00008002 int_const(state, &ulong_type, offset));
8003 free_triple(state, base);
8004 }
Eric Biederman90089602004-05-28 14:11:54 +00008005 else {
8006 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008007 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008008 break;
Eric Biederman90089602004-05-28 14:11:54 +00008009 case OP_INDEX:
8010 if (RHS(ptr, 0)->op == OP_DEREF) {
8011 struct triple *base, *left;
8012 ulong_t offset;
8013 base = MISC(ptr, 0);
8014 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8015 left = RHS(base, 0);
8016 ptr = triple(state, OP_ADD, left->type,
8017 read_expr(state, left),
8018 int_const(state, &long_type, offset));
8019 free_triple(state, base);
8020 }
8021 else {
8022 ptr = flatten_part(state, first, ptr);
8023 }
8024 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008025 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008026 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008027 use_triple(ptr, MISC(ptr, 0));
8028 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008029 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008030 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8031 use_triple(MISC(ptr, 0), ptr);
8032 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008033 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008034 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008035 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8036 use_triple(MISC(ptr, 0), ptr);
8037 insert_triple(state, first, ptr);
8038 ptr->id |= TRIPLE_FLAG_FLATTENED;
8039 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8040 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008041 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008042 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008043 break;
8044 default:
8045 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008046 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008047 break;
8048 }
8049 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008050 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008051 insert_triple(state, first, ptr);
8052 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008053 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008054 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008055 return ptr;
8056}
8057
8058static void release_expr(struct compile_state *state, struct triple *expr)
8059{
8060 struct triple *head;
8061 head = label(state);
8062 flatten(state, head, expr);
8063 while(head->next != head) {
8064 release_triple(state, head->next);
8065 }
8066 free_triple(state, head);
8067}
8068
8069static int replace_rhs_use(struct compile_state *state,
8070 struct triple *orig, struct triple *new, struct triple *use)
8071{
8072 struct triple **expr;
8073 int found;
8074 found = 0;
8075 expr = triple_rhs(state, use, 0);
8076 for(;expr; expr = triple_rhs(state, use, expr)) {
8077 if (*expr == orig) {
8078 *expr = new;
8079 found = 1;
8080 }
8081 }
8082 if (found) {
8083 unuse_triple(orig, use);
8084 use_triple(new, use);
8085 }
8086 return found;
8087}
8088
8089static int replace_lhs_use(struct compile_state *state,
8090 struct triple *orig, struct triple *new, struct triple *use)
8091{
8092 struct triple **expr;
8093 int found;
8094 found = 0;
8095 expr = triple_lhs(state, use, 0);
8096 for(;expr; expr = triple_lhs(state, use, expr)) {
8097 if (*expr == orig) {
8098 *expr = new;
8099 found = 1;
8100 }
8101 }
8102 if (found) {
8103 unuse_triple(orig, use);
8104 use_triple(new, use);
8105 }
8106 return found;
8107}
8108
Eric Biederman90089602004-05-28 14:11:54 +00008109static int replace_misc_use(struct compile_state *state,
8110 struct triple *orig, struct triple *new, struct triple *use)
8111{
8112 struct triple **expr;
8113 int found;
8114 found = 0;
8115 expr = triple_misc(state, use, 0);
8116 for(;expr; expr = triple_misc(state, use, expr)) {
8117 if (*expr == orig) {
8118 *expr = new;
8119 found = 1;
8120 }
8121 }
8122 if (found) {
8123 unuse_triple(orig, use);
8124 use_triple(new, use);
8125 }
8126 return found;
8127}
8128
8129static int replace_targ_use(struct compile_state *state,
8130 struct triple *orig, struct triple *new, struct triple *use)
8131{
8132 struct triple **expr;
8133 int found;
8134 found = 0;
8135 expr = triple_targ(state, use, 0);
8136 for(;expr; expr = triple_targ(state, use, expr)) {
8137 if (*expr == orig) {
8138 *expr = new;
8139 found = 1;
8140 }
8141 }
8142 if (found) {
8143 unuse_triple(orig, use);
8144 use_triple(new, use);
8145 }
8146 return found;
8147}
8148
8149static void replace_use(struct compile_state *state,
8150 struct triple *orig, struct triple *new, struct triple *use)
8151{
8152 int found;
8153 found = 0;
8154 found |= replace_rhs_use(state, orig, new, use);
8155 found |= replace_lhs_use(state, orig, new, use);
8156 found |= replace_misc_use(state, orig, new, use);
8157 found |= replace_targ_use(state, orig, new, use);
8158 if (!found) {
8159 internal_error(state, use, "use without use");
8160 }
8161}
8162
Eric Biedermanb138ac82003-04-22 18:44:01 +00008163static void propogate_use(struct compile_state *state,
8164 struct triple *orig, struct triple *new)
8165{
8166 struct triple_set *user, *next;
8167 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008168 /* Careful replace_use modifies the use chain and
8169 * removes use. So we must get a copy of the next
8170 * entry early.
8171 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008172 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008173 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008174 }
8175 if (orig->use) {
8176 internal_error(state, orig, "used after propogate_use");
8177 }
8178}
8179
8180/*
8181 * Code generators
8182 * ===========================
8183 */
8184
Eric Biederman90089602004-05-28 14:11:54 +00008185static struct triple *mk_cast_expr(
8186 struct compile_state *state, struct type *type, struct triple *expr)
8187{
8188 struct triple *def;
8189 def = read_expr(state, expr);
8190 def = triple(state, OP_CONVERT, type, def, 0);
8191 return def;
8192}
8193
Eric Biedermanb138ac82003-04-22 18:44:01 +00008194static struct triple *mk_add_expr(
8195 struct compile_state *state, struct triple *left, struct triple *right)
8196{
8197 struct type *result_type;
8198 /* Put pointer operands on the left */
8199 if (is_pointer(right)) {
8200 struct triple *tmp;
8201 tmp = left;
8202 left = right;
8203 right = tmp;
8204 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008205 left = read_expr(state, left);
8206 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008207 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008208 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008209 struct type *ptr_math;
8210 int op;
8211 if (is_signed(right->type)) {
8212 ptr_math = &long_type;
8213 op = OP_SMUL;
8214 } else {
8215 ptr_math = &ulong_type;
8216 op = OP_UMUL;
8217 }
8218 if (!equiv_types(right->type, ptr_math)) {
8219 right = mk_cast_expr(state, ptr_math, right);
8220 }
8221 right = triple(state, op, ptr_math, right,
8222 int_const(state, ptr_math,
8223 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008224 }
8225 return triple(state, OP_ADD, result_type, left, right);
8226}
8227
8228static struct triple *mk_sub_expr(
8229 struct compile_state *state, struct triple *left, struct triple *right)
8230{
8231 struct type *result_type;
8232 result_type = ptr_arithmetic_result(state, left, right);
8233 left = read_expr(state, left);
8234 right = read_expr(state, right);
8235 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008236 struct type *ptr_math;
8237 int op;
8238 if (is_signed(right->type)) {
8239 ptr_math = &long_type;
8240 op = OP_SMUL;
8241 } else {
8242 ptr_math = &ulong_type;
8243 op = OP_UMUL;
8244 }
8245 if (!equiv_types(right->type, ptr_math)) {
8246 right = mk_cast_expr(state, ptr_math, right);
8247 }
8248 right = triple(state, op, ptr_math, right,
8249 int_const(state, ptr_math,
8250 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008251 }
8252 return triple(state, OP_SUB, result_type, left, right);
8253}
8254
8255static struct triple *mk_pre_inc_expr(
8256 struct compile_state *state, struct triple *def)
8257{
8258 struct triple *val;
8259 lvalue(state, def);
8260 val = mk_add_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_pre_dec_expr(
8267 struct compile_state *state, struct triple *def)
8268{
8269 struct triple *val;
8270 lvalue(state, def);
8271 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8272 return triple(state, OP_VAL, def->type,
8273 write_expr(state, def, val),
8274 val);
8275}
8276
8277static struct triple *mk_post_inc_expr(
8278 struct compile_state *state, struct triple *def)
8279{
8280 struct triple *val;
8281 lvalue(state, def);
8282 val = read_expr(state, def);
8283 return triple(state, OP_VAL, def->type,
8284 write_expr(state, def,
8285 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8286 , val);
8287}
8288
8289static struct triple *mk_post_dec_expr(
8290 struct compile_state *state, struct triple *def)
8291{
8292 struct triple *val;
8293 lvalue(state, def);
8294 val = read_expr(state, def);
8295 return triple(state, OP_VAL, def->type,
8296 write_expr(state, def,
8297 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8298 , val);
8299}
8300
8301static struct triple *mk_subscript_expr(
8302 struct compile_state *state, struct triple *left, struct triple *right)
8303{
8304 left = read_expr(state, left);
8305 right = read_expr(state, right);
8306 if (!is_pointer(left) && !is_pointer(right)) {
8307 error(state, left, "subscripted value is not a pointer");
8308 }
8309 return mk_deref_expr(state, mk_add_expr(state, left, right));
8310}
8311
Eric Biedermane058a1e2003-07-12 01:21:31 +00008312
Eric Biedermanb138ac82003-04-22 18:44:01 +00008313/*
8314 * Compile time evaluation
8315 * ===========================
8316 */
8317static int is_const(struct triple *ins)
8318{
8319 return IS_CONST_OP(ins->op);
8320}
8321
Eric Biederman83b991a2003-10-11 06:20:25 +00008322static int is_simple_const(struct triple *ins)
8323{
Eric Biederman90089602004-05-28 14:11:54 +00008324 /* Is this a constant that u.cval has the value.
8325 * Or equivalently is this a constant that read_const
8326 * works on.
8327 * So far only OP_INTCONST qualifies.
8328 */
8329 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008330}
8331
Eric Biedermanb138ac82003-04-22 18:44:01 +00008332static int constants_equal(struct compile_state *state,
8333 struct triple *left, struct triple *right)
8334{
8335 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008336 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8337 equal = 0;
8338 }
8339 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008340 equal = 0;
8341 }
8342 else if (left->op != right->op) {
8343 equal = 0;
8344 }
8345 else if (!equiv_types(left->type, right->type)) {
8346 equal = 0;
8347 }
8348 else {
8349 equal = 0;
8350 switch(left->op) {
8351 case OP_INTCONST:
8352 if (left->u.cval == right->u.cval) {
8353 equal = 1;
8354 }
8355 break;
8356 case OP_BLOBCONST:
8357 {
Eric Biederman90089602004-05-28 14:11:54 +00008358 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008359 lsize = size_of(state, left->type);
8360 rsize = size_of(state, right->type);
8361 if (lsize != rsize) {
8362 break;
8363 }
Eric Biederman90089602004-05-28 14:11:54 +00008364 bytes = bits_to_bytes(lsize);
8365 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008366 equal = 1;
8367 }
8368 break;
8369 }
8370 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008371 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008372 (left->u.cval == right->u.cval)) {
8373 equal = 1;
8374 }
8375 break;
8376 default:
8377 internal_error(state, left, "uknown constant type");
8378 break;
8379 }
8380 }
8381 return equal;
8382}
8383
8384static int is_zero(struct triple *ins)
8385{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008386 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008387}
8388
8389static int is_one(struct triple *ins)
8390{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008391 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008392}
8393
Stefan Reinauer50542a82007-10-24 11:14:14 +00008394#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008395static long_t bit_count(ulong_t value)
8396{
8397 int count;
8398 int i;
8399 count = 0;
8400 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8401 ulong_t mask;
8402 mask = 1;
8403 mask <<= i;
8404 if (value & mask) {
8405 count++;
8406 }
8407 }
8408 return count;
8409
8410}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008411#endif
8412
Eric Biedermanb138ac82003-04-22 18:44:01 +00008413static long_t bsr(ulong_t value)
8414{
8415 int i;
8416 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8417 ulong_t mask;
8418 mask = 1;
8419 mask <<= i;
8420 if (value & mask) {
8421 return i;
8422 }
8423 }
8424 return -1;
8425}
8426
8427static long_t bsf(ulong_t value)
8428{
8429 int i;
8430 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8431 ulong_t mask;
8432 mask = 1;
8433 mask <<= 1;
8434 if (value & mask) {
8435 return i;
8436 }
8437 }
8438 return -1;
8439}
8440
Eric Biedermancb364952004-11-15 10:46:44 +00008441static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008442{
8443 return bsr(value);
8444}
8445
8446static long_t tlog2(struct triple *ins)
8447{
Eric Biedermancb364952004-11-15 10:46:44 +00008448 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008449}
8450
8451static int is_pow2(struct triple *ins)
8452{
8453 ulong_t value, mask;
8454 long_t log;
8455 if (!is_const(ins)) {
8456 return 0;
8457 }
8458 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008459 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008460 if (log == -1) {
8461 return 0;
8462 }
8463 mask = 1;
8464 mask <<= log;
8465 return ((value & mask) == value);
8466}
8467
8468static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008469 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008470{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008471 switch(rhs->type->type &TYPE_MASK) {
8472 case TYPE_CHAR:
8473 case TYPE_SHORT:
8474 case TYPE_INT:
8475 case TYPE_LONG:
8476 case TYPE_UCHAR:
8477 case TYPE_USHORT:
8478 case TYPE_UINT:
8479 case TYPE_ULONG:
8480 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008481 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008482 break;
8483 default:
Eric Biederman90089602004-05-28 14:11:54 +00008484 fprintf(state->errout, "type: ");
8485 name_of(state->errout, rhs->type);
8486 fprintf(state->errout, "\n");
8487 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008488 break;
8489 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008490 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008491 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008492 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008493 return rhs->u.cval;
8494}
8495
Eric Biederman5ade04a2003-10-22 04:03:46 +00008496static long_t read_sconst(struct compile_state *state,
8497 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008498{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008499 return (long_t)(rhs->u.cval);
8500}
8501
Eric Biederman5ade04a2003-10-22 04:03:46 +00008502int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8503{
8504 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008505 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008506 }
8507 return !is_zero(rhs);
8508}
8509
8510int const_eq(struct compile_state *state, struct triple *ins,
8511 struct triple *left, struct triple *right)
8512{
8513 int result;
8514 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008515 internal_warning(state, ins, "non const passed to const_eq");
8516 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008517 }
8518 else if (left == right) {
8519 result = 1;
8520 }
8521 else if (is_simple_const(left) && is_simple_const(right)) {
8522 ulong_t lval, rval;
8523 lval = read_const(state, ins, left);
8524 rval = read_const(state, ins, right);
8525 result = (lval == rval);
8526 }
8527 else if ((left->op == OP_ADDRCONST) &&
8528 (right->op == OP_ADDRCONST)) {
8529 result = (MISC(left, 0) == MISC(right, 0)) &&
8530 (left->u.cval == right->u.cval);
8531 }
8532 else {
Eric Biederman90089602004-05-28 14:11:54 +00008533 internal_warning(state, ins, "incomparable constants passed to const_eq");
8534 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008535 }
8536 return result;
8537
8538}
8539
8540int const_ucmp(struct compile_state *state, struct triple *ins,
8541 struct triple *left, struct triple *right)
8542{
8543 int result;
8544 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008545 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008546 result = -2;
8547 }
8548 else if (left == right) {
8549 result = 0;
8550 }
8551 else if (is_simple_const(left) && is_simple_const(right)) {
8552 ulong_t lval, rval;
8553 lval = read_const(state, ins, left);
8554 rval = read_const(state, ins, right);
8555 result = 0;
8556 if (lval > rval) {
8557 result = 1;
8558 } else if (rval > lval) {
8559 result = -1;
8560 }
8561 }
8562 else if ((left->op == OP_ADDRCONST) &&
8563 (right->op == OP_ADDRCONST) &&
8564 (MISC(left, 0) == MISC(right, 0))) {
8565 result = 0;
8566 if (left->u.cval > right->u.cval) {
8567 result = 1;
8568 } else if (left->u.cval < right->u.cval) {
8569 result = -1;
8570 }
8571 }
8572 else {
Eric Biederman90089602004-05-28 14:11:54 +00008573 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008574 result = -2;
8575 }
8576 return result;
8577}
8578
8579int const_scmp(struct compile_state *state, struct triple *ins,
8580 struct triple *left, struct triple *right)
8581{
8582 int result;
8583 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008584 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008585 result = -2;
8586 }
8587 else if (left == right) {
8588 result = 0;
8589 }
8590 else if (is_simple_const(left) && is_simple_const(right)) {
8591 long_t lval, rval;
8592 lval = read_sconst(state, ins, left);
8593 rval = read_sconst(state, ins, right);
8594 result = 0;
8595 if (lval > rval) {
8596 result = 1;
8597 } else if (rval > lval) {
8598 result = -1;
8599 }
8600 }
8601 else {
Eric Biederman90089602004-05-28 14:11:54 +00008602 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008603 result = -2;
8604 }
8605 return result;
8606}
8607
Eric Biedermanb138ac82003-04-22 18:44:01 +00008608static void unuse_rhs(struct compile_state *state, struct triple *ins)
8609{
8610 struct triple **expr;
8611 expr = triple_rhs(state, ins, 0);
8612 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008613 if (*expr) {
8614 unuse_triple(*expr, ins);
8615 *expr = 0;
8616 }
8617 }
8618}
8619
8620static void unuse_lhs(struct compile_state *state, struct triple *ins)
8621{
8622 struct triple **expr;
8623 expr = triple_lhs(state, ins, 0);
8624 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008625 unuse_triple(*expr, ins);
8626 *expr = 0;
8627 }
8628}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008629
Stefan Reinauer50542a82007-10-24 11:14:14 +00008630#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008631static void unuse_misc(struct compile_state *state, struct triple *ins)
8632{
8633 struct triple **expr;
8634 expr = triple_misc(state, ins, 0);
8635 for(;expr;expr = triple_misc(state, ins, expr)) {
8636 unuse_triple(*expr, ins);
8637 *expr = 0;
8638 }
8639}
8640
8641static void unuse_targ(struct compile_state *state, struct triple *ins)
8642{
8643 int i;
8644 struct triple **slot;
8645 slot = &TARG(ins, 0);
8646 for(i = 0; i < ins->targ; i++) {
8647 unuse_triple(slot[i], ins);
8648 slot[i] = 0;
8649 }
8650}
8651
Eric Biedermanb138ac82003-04-22 18:44:01 +00008652static void check_lhs(struct compile_state *state, struct triple *ins)
8653{
8654 struct triple **expr;
8655 expr = triple_lhs(state, ins, 0);
8656 for(;expr;expr = triple_lhs(state, ins, expr)) {
8657 internal_error(state, ins, "unexpected lhs");
8658 }
8659
8660}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008661#endif
Eric Biederman90089602004-05-28 14:11:54 +00008662
8663static void check_misc(struct compile_state *state, struct triple *ins)
8664{
8665 struct triple **expr;
8666 expr = triple_misc(state, ins, 0);
8667 for(;expr;expr = triple_misc(state, ins, expr)) {
8668 if (*expr) {
8669 internal_error(state, ins, "unexpected misc");
8670 }
8671 }
8672}
8673
Eric Biedermanb138ac82003-04-22 18:44:01 +00008674static void check_targ(struct compile_state *state, struct triple *ins)
8675{
8676 struct triple **expr;
8677 expr = triple_targ(state, ins, 0);
8678 for(;expr;expr = triple_targ(state, ins, expr)) {
8679 internal_error(state, ins, "unexpected targ");
8680 }
8681}
8682
8683static void wipe_ins(struct compile_state *state, struct triple *ins)
8684{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008685 /* Becareful which instructions you replace the wiped
8686 * instruction with, as there are not enough slots
8687 * in all instructions to hold all others.
8688 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008689 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008690 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008691 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008692 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008693 ins->lhs = 0;
8694 ins->rhs = 0;
8695 ins->misc = 0;
8696 ins->targ = 0;
8697}
8698
Stefan Reinauer50542a82007-10-24 11:14:14 +00008699#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008700static void wipe_branch(struct compile_state *state, struct triple *ins)
8701{
8702 /* Becareful which instructions you replace the wiped
8703 * instruction with, as there are not enough slots
8704 * in all instructions to hold all others.
8705 */
8706 unuse_rhs(state, ins);
8707 unuse_lhs(state, ins);
8708 unuse_misc(state, ins);
8709 unuse_targ(state, ins);
8710 ins->lhs = 0;
8711 ins->rhs = 0;
8712 ins->misc = 0;
8713 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008714}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008715#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008716
8717static void mkcopy(struct compile_state *state,
8718 struct triple *ins, struct triple *rhs)
8719{
Eric Biederman83b991a2003-10-11 06:20:25 +00008720 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008721 if (!equiv_types(ins->type, rhs->type)) {
8722 FILE *fp = state->errout;
8723 fprintf(fp, "src type: ");
8724 name_of(fp, rhs->type);
8725 fprintf(fp, "\ndst type: ");
8726 name_of(fp, ins->type);
8727 fprintf(fp, "\n");
8728 internal_error(state, ins, "mkcopy type mismatch");
8729 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008730 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008731 wipe_ins(state, ins);
8732 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008733 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008734 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008735 RHS(ins, 0) = rhs;
8736 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008737}
8738
8739static void mkconst(struct compile_state *state,
8740 struct triple *ins, ulong_t value)
8741{
8742 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008743 fprintf(state->errout, "type: ");
8744 name_of(state->errout, ins->type);
8745 fprintf(state->errout, "\n");
8746 internal_error(state, ins, "unknown type to make constant value: %ld",
8747 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008748 }
8749 wipe_ins(state, ins);
8750 ins->op = OP_INTCONST;
8751 ins->u.cval = value;
8752}
8753
8754static void mkaddr_const(struct compile_state *state,
8755 struct triple *ins, struct triple *sdecl, ulong_t value)
8756{
Eric Biederman90089602004-05-28 14:11:54 +00008757 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008758 internal_error(state, ins, "bad base for addrconst");
8759 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008760 wipe_ins(state, ins);
8761 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008762 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008763 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008764 ins->u.cval = value;
8765 use_triple(sdecl, ins);
8766}
8767
Eric Biederman90089602004-05-28 14:11:54 +00008768#if DEBUG_DECOMPOSE_PRINT_TUPLES
8769static void print_tuple(struct compile_state *state,
8770 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008771{
Eric Biederman90089602004-05-28 14:11:54 +00008772 FILE *fp = state->dbgout;
8773 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8774 name_of(fp, tuple->type);
8775 if (tuple->lhs > 0) {
8776 fprintf(fp, " lhs: ");
8777 name_of(fp, LHS(tuple, 0)->type);
8778 }
8779 fprintf(fp, "\n");
8780
8781}
8782#endif
8783
8784static struct triple *decompose_with_tuple(struct compile_state *state,
8785 struct triple *ins, struct triple *tuple)
8786{
8787 struct triple *next;
8788 next = ins->next;
8789 flatten(state, next, tuple);
8790#if DEBUG_DECOMPOSE_PRINT_TUPLES
8791 print_tuple(state, ins, tuple);
8792#endif
8793
8794 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8795 struct triple *tmp;
8796 if (tuple->lhs != 1) {
8797 internal_error(state, tuple, "plain type in multiple registers?");
8798 }
8799 tmp = LHS(tuple, 0);
8800 release_triple(state, tuple);
8801 tuple = tmp;
8802 }
8803
8804 propogate_use(state, ins, tuple);
8805 release_triple(state, ins);
8806
8807 return next;
8808}
8809
8810static struct triple *decompose_unknownval(struct compile_state *state,
8811 struct triple *ins)
8812{
8813 struct triple *tuple;
8814 ulong_t i;
8815
8816#if DEBUG_DECOMPOSE_HIRES
8817 FILE *fp = state->dbgout;
8818 fprintf(fp, "unknown type: ");
8819 name_of(fp, ins->type);
8820 fprintf(fp, "\n");
8821#endif
8822
8823 get_occurance(ins->occurance);
8824 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8825 ins->occurance);
8826
8827 for(i = 0; i < tuple->lhs; i++) {
8828 struct type *piece_type;
8829 struct triple *unknown;
8830
8831 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8832 get_occurance(tuple->occurance);
8833 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8834 tuple->occurance);
8835 LHS(tuple, i) = unknown;
8836 }
8837 return decompose_with_tuple(state, ins, tuple);
8838}
8839
8840
8841static struct triple *decompose_read(struct compile_state *state,
8842 struct triple *ins)
8843{
8844 struct triple *tuple, *lval;
8845 ulong_t i;
8846
8847 lval = RHS(ins, 0);
8848
8849 if (lval->op == OP_PIECE) {
8850 return ins->next;
8851 }
8852 get_occurance(ins->occurance);
8853 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8854 ins->occurance);
8855
8856 if ((tuple->lhs != lval->lhs) &&
8857 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
8858 {
8859 internal_error(state, ins, "lhs size inconsistency?");
8860 }
8861 for(i = 0; i < tuple->lhs; i++) {
8862 struct triple *piece, *read, *bitref;
8863 if ((i != 0) || !triple_is_def(state, lval)) {
8864 piece = LHS(lval, i);
8865 } else {
8866 piece = lval;
8867 }
8868
8869 /* See if the piece is really a bitref */
8870 bitref = 0;
8871 if (piece->op == OP_BITREF) {
8872 bitref = piece;
8873 piece = RHS(bitref, 0);
8874 }
8875
8876 get_occurance(tuple->occurance);
8877 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8878 tuple->occurance);
8879 RHS(read, 0) = piece;
8880
8881 if (bitref) {
8882 struct triple *extract;
8883 int op;
8884 if (is_signed(bitref->type->left)) {
8885 op = OP_SEXTRACT;
8886 } else {
8887 op = OP_UEXTRACT;
8888 }
8889 get_occurance(tuple->occurance);
8890 extract = alloc_triple(state, op, bitref->type, -1, -1,
8891 tuple->occurance);
8892 RHS(extract, 0) = read;
8893 extract->u.bitfield.size = bitref->u.bitfield.size;
8894 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8895
8896 read = extract;
8897 }
8898
8899 LHS(tuple, i) = read;
8900 }
8901 return decompose_with_tuple(state, ins, tuple);
8902}
8903
8904static struct triple *decompose_write(struct compile_state *state,
8905 struct triple *ins)
8906{
8907 struct triple *tuple, *lval, *val;
8908 ulong_t i;
8909
8910 lval = MISC(ins, 0);
8911 val = RHS(ins, 0);
8912 get_occurance(ins->occurance);
8913 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8914 ins->occurance);
8915
8916 if ((tuple->lhs != lval->lhs) &&
8917 (!triple_is_def(state, lval) || tuple->lhs != 1))
8918 {
8919 internal_error(state, ins, "lhs size inconsistency?");
8920 }
8921 for(i = 0; i < tuple->lhs; i++) {
8922 struct triple *piece, *write, *pval, *bitref;
8923 if ((i != 0) || !triple_is_def(state, lval)) {
8924 piece = LHS(lval, i);
8925 } else {
8926 piece = lval;
8927 }
8928 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8929 pval = val;
8930 }
8931 else {
8932 if (i > val->lhs) {
8933 internal_error(state, ins, "lhs size inconsistency?");
8934 }
8935 pval = LHS(val, i);
8936 }
8937
8938 /* See if the piece is really a bitref */
8939 bitref = 0;
8940 if (piece->op == OP_BITREF) {
8941 struct triple *read, *deposit;
8942 bitref = piece;
8943 piece = RHS(bitref, 0);
8944
8945 /* Read the destination register */
8946 get_occurance(tuple->occurance);
8947 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8948 tuple->occurance);
8949 RHS(read, 0) = piece;
8950
8951 /* Deposit the new bitfield value */
8952 get_occurance(tuple->occurance);
8953 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8954 tuple->occurance);
8955 RHS(deposit, 0) = read;
8956 RHS(deposit, 1) = pval;
8957 deposit->u.bitfield.size = bitref->u.bitfield.size;
8958 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8959
8960 /* Now write the newly generated value */
8961 pval = deposit;
8962 }
8963
8964 get_occurance(tuple->occurance);
8965 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
8966 tuple->occurance);
8967 MISC(write, 0) = piece;
8968 RHS(write, 0) = pval;
8969 LHS(tuple, i) = write;
8970 }
8971 return decompose_with_tuple(state, ins, tuple);
8972}
8973
8974struct decompose_load_info {
8975 struct occurance *occurance;
8976 struct triple *lval;
8977 struct triple *tuple;
8978};
8979static void decompose_load_cb(struct compile_state *state,
8980 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8981{
8982 struct decompose_load_info *info = arg;
8983 struct triple *load;
8984
8985 if (reg_offset > info->tuple->lhs) {
8986 internal_error(state, info->tuple, "lhs to small?");
8987 }
8988 get_occurance(info->occurance);
8989 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8990 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8991 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8992}
8993
8994static struct triple *decompose_load(struct compile_state *state,
8995 struct triple *ins)
8996{
8997 struct triple *tuple;
8998 struct decompose_load_info info;
8999
9000 if (!is_compound_type(ins->type)) {
9001 return ins->next;
9002 }
9003 get_occurance(ins->occurance);
9004 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9005 ins->occurance);
9006
9007 info.occurance = ins->occurance;
9008 info.lval = RHS(ins, 0);
9009 info.tuple = tuple;
9010 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
9011
9012 return decompose_with_tuple(state, ins, tuple);
9013}
9014
9015
9016struct decompose_store_info {
9017 struct occurance *occurance;
9018 struct triple *lval;
9019 struct triple *val;
9020 struct triple *tuple;
9021};
9022static void decompose_store_cb(struct compile_state *state,
9023 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9024{
9025 struct decompose_store_info *info = arg;
9026 struct triple *store;
9027
9028 if (reg_offset > info->tuple->lhs) {
9029 internal_error(state, info->tuple, "lhs to small?");
9030 }
9031 get_occurance(info->occurance);
9032 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9033 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9034 RHS(store, 1) = LHS(info->val, reg_offset);
9035 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9036}
9037
9038static struct triple *decompose_store(struct compile_state *state,
9039 struct triple *ins)
9040{
9041 struct triple *tuple;
9042 struct decompose_store_info info;
9043
9044 if (!is_compound_type(ins->type)) {
9045 return ins->next;
9046 }
9047 get_occurance(ins->occurance);
9048 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9049 ins->occurance);
9050
9051 info.occurance = ins->occurance;
9052 info.lval = RHS(ins, 0);
9053 info.val = RHS(ins, 1);
9054 info.tuple = tuple;
9055 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9056
9057 return decompose_with_tuple(state, ins, tuple);
9058}
9059
9060static struct triple *decompose_dot(struct compile_state *state,
9061 struct triple *ins)
9062{
9063 struct triple *tuple, *lval;
9064 struct type *type;
9065 size_t reg_offset;
9066 int i, idx;
9067
9068 lval = MISC(ins, 0);
9069 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9070 idx = reg_offset/REG_SIZEOF_REG;
9071 type = field_type(state, lval->type, ins->u.field);
9072#if DEBUG_DECOMPOSE_HIRES
9073 {
9074 FILE *fp = state->dbgout;
9075 fprintf(fp, "field type: ");
9076 name_of(fp, type);
9077 fprintf(fp, "\n");
9078 }
9079#endif
9080
9081 get_occurance(ins->occurance);
9082 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9083 ins->occurance);
9084
9085 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9086 (tuple->lhs != 1))
9087 {
9088 internal_error(state, ins, "multi register bitfield?");
9089 }
9090
9091 for(i = 0; i < tuple->lhs; i++, idx++) {
9092 struct triple *piece;
9093 if (!triple_is_def(state, lval)) {
9094 if (idx > lval->lhs) {
9095 internal_error(state, ins, "inconsistent lhs count");
9096 }
9097 piece = LHS(lval, idx);
9098 } else {
9099 if (idx != 0) {
9100 internal_error(state, ins, "bad reg_offset into def");
9101 }
9102 if (i != 0) {
9103 internal_error(state, ins, "bad reg count from def");
9104 }
9105 piece = lval;
9106 }
9107
9108 /* Remember the offset of the bitfield */
9109 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9110 get_occurance(ins->occurance);
9111 piece = build_triple(state, OP_BITREF, type, piece, 0,
9112 ins->occurance);
9113 piece->u.bitfield.size = size_of(state, type);
9114 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9115 }
9116 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
9117 internal_error(state, ins,
9118 "request for a nonbitfield sub register?");
9119 }
9120
9121 LHS(tuple, i) = piece;
9122 }
9123
9124 return decompose_with_tuple(state, ins, tuple);
9125}
9126
9127static struct triple *decompose_index(struct compile_state *state,
9128 struct triple *ins)
9129{
9130 struct triple *tuple, *lval;
9131 struct type *type;
9132 int i, idx;
9133
9134 lval = MISC(ins, 0);
9135 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9136 type = index_type(state, lval->type, ins->u.cval);
9137#if DEBUG_DECOMPOSE_HIRES
9138{
9139 FILE *fp = state->dbgout;
9140 fprintf(fp, "index type: ");
9141 name_of(fp, type);
9142 fprintf(fp, "\n");
9143}
9144#endif
9145
9146 get_occurance(ins->occurance);
9147 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9148 ins->occurance);
9149
9150 for(i = 0; i < tuple->lhs; i++, idx++) {
9151 struct triple *piece;
9152 if (!triple_is_def(state, lval)) {
9153 if (idx > lval->lhs) {
9154 internal_error(state, ins, "inconsistent lhs count");
9155 }
9156 piece = LHS(lval, idx);
9157 } else {
9158 if (idx != 0) {
9159 internal_error(state, ins, "bad reg_offset into def");
9160 }
9161 if (i != 0) {
9162 internal_error(state, ins, "bad reg count from def");
9163 }
9164 piece = lval;
9165 }
9166 LHS(tuple, i) = piece;
9167 }
9168
9169 return decompose_with_tuple(state, ins, tuple);
9170}
9171
9172static void decompose_compound_types(struct compile_state *state)
9173{
9174 struct triple *ins, *next, *first;
9175 FILE *fp;
9176 fp = state->dbgout;
Eric Biederman83b991a2003-10-11 06:20:25 +00009177 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009178 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009179
9180 /* Pass one expand compound values into pseudo registers.
9181 */
9182 next = first;
9183 do {
9184 ins = next;
9185 next = ins->next;
9186 switch(ins->op) {
9187 case OP_UNKNOWNVAL:
9188 next = decompose_unknownval(state, ins);
9189 break;
9190
9191 case OP_READ:
9192 next = decompose_read(state, ins);
9193 break;
9194
9195 case OP_WRITE:
9196 next = decompose_write(state, ins);
9197 break;
9198
9199
9200 /* Be very careful with the load/store logic. These
9201 * operations must convert from the in register layout
9202 * to the in memory layout, which is nontrivial.
9203 */
9204 case OP_LOAD:
9205 next = decompose_load(state, ins);
9206 break;
9207 case OP_STORE:
9208 next = decompose_store(state, ins);
9209 break;
9210
9211 case OP_DOT:
9212 next = decompose_dot(state, ins);
9213 break;
9214 case OP_INDEX:
9215 next = decompose_index(state, ins);
9216 break;
9217
9218 }
9219#if DEBUG_DECOMPOSE_HIRES
9220 fprintf(fp, "decompose next: %p \n", next);
9221 fflush(fp);
9222 fprintf(fp, "next->op: %d %s\n",
9223 next->op, tops(next->op));
9224 /* High resolution debugging mode */
9225 print_triples(state);
9226#endif
9227 } while (next != first);
9228
9229 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009230 */
9231 ins = first;
9232 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009233 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009234 if (ins->op == OP_TUPLE) {
9235 if (ins->use) {
9236 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +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 }
Eric Biederman90089602004-05-28 14:11:54 +00009241 }
9242 ins = next;
9243 } while(ins != first);
9244 ins = first;
9245 do {
9246 next = ins->next;
9247 if (ins->op == OP_BITREF) {
9248 if (ins->use) {
9249 internal_error(state, ins, "bitref used");
9250 }
9251 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009252 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009253 }
9254 }
9255 ins = next;
9256 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009257
Eric Biederman0babc1c2003-05-09 02:39:00 +00009258 /* Pass three verify the state and set ->id to 0.
9259 */
Eric Biederman90089602004-05-28 14:11:54 +00009260 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009261 do {
Eric Biederman90089602004-05-28 14:11:54 +00009262 ins = next;
9263 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009264 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009265 if (triple_stores_block(state, ins)) {
9266 ins->u.block = 0;
9267 }
9268 if (triple_is_def(state, ins)) {
9269 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9270 internal_error(state, ins, "multi register value remains?");
9271 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009272 }
9273 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009274 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009275 }
Eric Biederman90089602004-05-28 14:11:54 +00009276 if (ins->op == OP_INDEX) {
9277 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009278 }
Eric Biederman90089602004-05-28 14:11:54 +00009279 if (ins->op == OP_BITREF) {
9280 internal_error(state, ins, "OP_BITREF remains?");
9281 }
9282 if (ins->op == OP_TUPLE) {
9283 internal_error(state, ins, "OP_TUPLE remains?");
9284 }
9285 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009286}
9287
Eric Biedermanb138ac82003-04-22 18:44:01 +00009288/* For those operations that cannot be simplified */
9289static void simplify_noop(struct compile_state *state, struct triple *ins)
9290{
9291 return;
9292}
9293
9294static void simplify_smul(struct compile_state *state, struct triple *ins)
9295{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009296 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009297 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009298 tmp = RHS(ins, 0);
9299 RHS(ins, 0) = RHS(ins, 1);
9300 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009301 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009302 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009303 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009304 left = read_sconst(state, ins, RHS(ins, 0));
9305 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009306 mkconst(state, ins, left * right);
9307 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009308 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009309 mkconst(state, ins, 0);
9310 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009311 else if (is_one(RHS(ins, 1))) {
9312 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009313 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009314 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009315 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009316 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009317 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009318 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009319 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009320 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009321 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009322 }
9323}
9324
9325static void simplify_umul(struct compile_state *state, struct triple *ins)
9326{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009327 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009328 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009329 tmp = RHS(ins, 0);
9330 RHS(ins, 0) = RHS(ins, 1);
9331 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009332 }
Eric Biederman90089602004-05-28 14:11:54 +00009333 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009334 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009335 left = read_const(state, ins, RHS(ins, 0));
9336 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009337 mkconst(state, ins, left * right);
9338 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009339 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009340 mkconst(state, ins, 0);
9341 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009342 else if (is_one(RHS(ins, 1))) {
9343 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009344 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009345 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009346 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009347 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009348 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009349 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009350 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009351 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009352 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009353 }
9354}
9355
9356static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9357{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009358 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009359 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009360 left = read_sconst(state, ins, RHS(ins, 0));
9361 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009362 mkconst(state, ins, left / right);
9363 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009364 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009365 mkconst(state, ins, 0);
9366 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009367 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009368 error(state, ins, "division by zero");
9369 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009370 else if (is_one(RHS(ins, 1))) {
9371 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009372 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009373 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009374 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009375 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009376 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009377 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009378 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009379 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009380 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009381 }
9382}
9383
9384static void simplify_udiv(struct compile_state *state, struct triple *ins)
9385{
Eric Biederman90089602004-05-28 14:11:54 +00009386 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009387 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009388 left = read_const(state, ins, RHS(ins, 0));
9389 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009390 mkconst(state, ins, left / right);
9391 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009392 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009393 mkconst(state, ins, 0);
9394 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009395 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396 error(state, ins, "division by zero");
9397 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009398 else if (is_one(RHS(ins, 1))) {
9399 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009400 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009401 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009403 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009404 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009405 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009406 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009407 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009408 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009409 }
9410}
9411
9412static void simplify_smod(struct compile_state *state, struct triple *ins)
9413{
Eric Biederman90089602004-05-28 14:11:54 +00009414 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009415 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009416 left = read_const(state, ins, RHS(ins, 0));
9417 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009418 mkconst(state, ins, left % right);
9419 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009420 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009421 mkconst(state, ins, 0);
9422 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009423 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009424 error(state, ins, "division by zero");
9425 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009426 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009427 mkconst(state, ins, 0);
9428 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009429 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009431 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009432 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009433 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009434 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009435 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009436 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009437 }
9438}
Eric Biederman83b991a2003-10-11 06:20:25 +00009439
Eric Biedermanb138ac82003-04-22 18:44:01 +00009440static void simplify_umod(struct compile_state *state, struct triple *ins)
9441{
Eric Biederman90089602004-05-28 14:11:54 +00009442 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009443 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009444 left = read_const(state, ins, RHS(ins, 0));
9445 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009446 mkconst(state, ins, left % right);
9447 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009448 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009449 mkconst(state, ins, 0);
9450 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009451 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009452 error(state, ins, "division by zero");
9453 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009454 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009455 mkconst(state, ins, 0);
9456 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009457 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009458 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009459 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009460 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009461 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009462 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009463 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009464 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009465 }
9466}
9467
9468static void simplify_add(struct compile_state *state, struct triple *ins)
9469{
9470 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009471 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009473 tmp = RHS(ins, 0);
9474 RHS(ins, 0) = RHS(ins, 1);
9475 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009476 }
Eric Biederman90089602004-05-28 14:11:54 +00009477 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009478 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009479 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009480 left = read_const(state, ins, RHS(ins, 0));
9481 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009482 mkconst(state, ins, left + right);
9483 }
Eric Biederman530b5192003-07-01 10:05:30 +00009484 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009485 struct triple *sdecl;
9486 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009487 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009488 left = RHS(ins, 0)->u.cval;
9489 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009490 mkaddr_const(state, ins, sdecl, left + right);
9491 }
Eric Biederman530b5192003-07-01 10:05:30 +00009492 else {
9493 internal_warning(state, ins, "Optimize me!");
9494 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009495 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009496 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009497 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009498 tmp = RHS(ins, 1);
9499 RHS(ins, 1) = RHS(ins, 0);
9500 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009501 }
9502}
9503
9504static void simplify_sub(struct compile_state *state, struct triple *ins)
9505{
Eric Biederman90089602004-05-28 14:11:54 +00009506 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009507 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009508 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009509 left = read_const(state, ins, RHS(ins, 0));
9510 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009511 mkconst(state, ins, left - right);
9512 }
Eric Biederman530b5192003-07-01 10:05:30 +00009513 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009514 struct triple *sdecl;
9515 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009516 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009517 left = RHS(ins, 0)->u.cval;
9518 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009519 mkaddr_const(state, ins, sdecl, left - right);
9520 }
Eric Biederman530b5192003-07-01 10:05:30 +00009521 else {
9522 internal_warning(state, ins, "Optimize me!");
9523 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009524 }
9525}
9526
9527static void simplify_sl(struct compile_state *state, struct triple *ins)
9528{
Eric Biederman90089602004-05-28 14:11:54 +00009529 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009530 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009531 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009532 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009533 warning(state, ins, "left shift count >= width of type");
9534 }
9535 }
Eric Biederman90089602004-05-28 14:11:54 +00009536 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009537 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009538 left = read_const(state, ins, RHS(ins, 0));
9539 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009540 mkconst(state, ins, left << right);
9541 }
9542}
9543
9544static void simplify_usr(struct compile_state *state, struct triple *ins)
9545{
Eric Biederman90089602004-05-28 14:11:54 +00009546 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009547 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009548 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009549 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009550 warning(state, ins, "right shift count >= width of type");
9551 }
9552 }
Eric Biederman90089602004-05-28 14:11:54 +00009553 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009554 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009555 left = read_const(state, ins, RHS(ins, 0));
9556 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009557 mkconst(state, ins, left >> right);
9558 }
9559}
9560
9561static void simplify_ssr(struct compile_state *state, struct triple *ins)
9562{
Eric Biederman90089602004-05-28 14:11:54 +00009563 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009564 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009565 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009566 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009567 warning(state, ins, "right shift count >= width of type");
9568 }
9569 }
Eric Biederman90089602004-05-28 14:11:54 +00009570 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009571 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009572 left = read_sconst(state, ins, RHS(ins, 0));
9573 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009574 mkconst(state, ins, left >> right);
9575 }
9576}
9577
9578static void simplify_and(struct compile_state *state, struct triple *ins)
9579{
Eric Biederman90089602004-05-28 14:11:54 +00009580 struct triple *left, *right;
9581 left = RHS(ins, 0);
9582 right = RHS(ins, 1);
9583
9584 if (is_simple_const(left) && is_simple_const(right)) {
9585 ulong_t lval, rval;
9586 lval = read_const(state, ins, left);
9587 rval = read_const(state, ins, right);
9588 mkconst(state, ins, lval & rval);
9589 }
9590 else if (is_zero(right) || is_zero(left)) {
9591 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009592 }
9593}
9594
9595static void simplify_or(struct compile_state *state, struct triple *ins)
9596{
Eric Biederman90089602004-05-28 14:11:54 +00009597 struct triple *left, *right;
9598 left = RHS(ins, 0);
9599 right = RHS(ins, 1);
9600
9601 if (is_simple_const(left) && is_simple_const(right)) {
9602 ulong_t lval, rval;
9603 lval = read_const(state, ins, left);
9604 rval = read_const(state, ins, right);
9605 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009606 }
Eric Biederman90089602004-05-28 14:11:54 +00009607#if 0 /* I need to handle type mismatches here... */
9608 else if (is_zero(right)) {
9609 mkcopy(state, ins, left);
9610 }
9611 else if (is_zero(left)) {
9612 mkcopy(state, ins, right);
9613 }
9614#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009615}
9616
9617static void simplify_xor(struct compile_state *state, struct triple *ins)
9618{
Eric Biederman90089602004-05-28 14:11:54 +00009619 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009620 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009621 left = read_const(state, ins, RHS(ins, 0));
9622 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009623 mkconst(state, ins, left ^ right);
9624 }
9625}
9626
9627static void simplify_pos(struct compile_state *state, struct triple *ins)
9628{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009629 if (is_const(RHS(ins, 0))) {
9630 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009631 }
9632 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009633 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009634 }
9635}
9636
9637static void simplify_neg(struct compile_state *state, struct triple *ins)
9638{
Eric Biederman90089602004-05-28 14:11:54 +00009639 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009640 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009641 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009642 mkconst(state, ins, -left);
9643 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009644 else if (RHS(ins, 0)->op == OP_NEG) {
9645 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009646 }
9647}
9648
9649static void simplify_invert(struct compile_state *state, struct triple *ins)
9650{
Eric Biederman90089602004-05-28 14:11:54 +00009651 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009652 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009653 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009654 mkconst(state, ins, ~left);
9655 }
9656}
9657
9658static void simplify_eq(struct compile_state *state, struct triple *ins)
9659{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009660 struct triple *left, *right;
9661 left = RHS(ins, 0);
9662 right = RHS(ins, 1);
9663
9664 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009665 int val;
9666 val = const_eq(state, ins, left, right);
9667 if (val >= 0) {
9668 mkconst(state, ins, val == 1);
9669 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009670 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009671 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009672 mkconst(state, ins, 1);
9673 }
9674}
9675
9676static void simplify_noteq(struct compile_state *state, struct triple *ins)
9677{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009678 struct triple *left, *right;
9679 left = RHS(ins, 0);
9680 right = RHS(ins, 1);
9681
9682 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009683 int val;
9684 val = const_eq(state, ins, left, right);
9685 if (val >= 0) {
9686 mkconst(state, ins, val != 1);
9687 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009688 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009689 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009690 mkconst(state, ins, 0);
9691 }
9692}
9693
9694static void simplify_sless(struct compile_state *state, struct triple *ins)
9695{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009696 struct triple *left, *right;
9697 left = RHS(ins, 0);
9698 right = RHS(ins, 1);
9699
9700 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009701 int val;
9702 val = const_scmp(state, ins, left, right);
9703 if ((val >= -1) && (val <= 1)) {
9704 mkconst(state, ins, val < 0);
9705 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009706 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009707 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009708 mkconst(state, ins, 0);
9709 }
9710}
9711
9712static void simplify_uless(struct compile_state *state, struct triple *ins)
9713{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009714 struct triple *left, *right;
9715 left = RHS(ins, 0);
9716 right = RHS(ins, 1);
9717
9718 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009719 int val;
9720 val = const_ucmp(state, ins, left, right);
9721 if ((val >= -1) && (val <= 1)) {
9722 mkconst(state, ins, val < 0);
9723 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009724 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009725 else if (is_zero(right)) {
9726 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009727 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009728 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009729 mkconst(state, ins, 0);
9730 }
9731}
9732
9733static void simplify_smore(struct compile_state *state, struct triple *ins)
9734{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009735 struct triple *left, *right;
9736 left = RHS(ins, 0);
9737 right = RHS(ins, 1);
9738
9739 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009740 int val;
9741 val = const_scmp(state, ins, left, right);
9742 if ((val >= -1) && (val <= 1)) {
9743 mkconst(state, ins, val > 0);
9744 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009745 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009746 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009747 mkconst(state, ins, 0);
9748 }
9749}
9750
9751static void simplify_umore(struct compile_state *state, struct triple *ins)
9752{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009753 struct triple *left, *right;
9754 left = RHS(ins, 0);
9755 right = RHS(ins, 1);
9756
9757 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009758 int val;
9759 val = const_ucmp(state, ins, left, right);
9760 if ((val >= -1) && (val <= 1)) {
9761 mkconst(state, ins, val > 0);
9762 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009763 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009764 else if (is_zero(left)) {
9765 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009766 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009767 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009768 mkconst(state, ins, 0);
9769 }
9770}
9771
9772
9773static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9774{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009775 struct triple *left, *right;
9776 left = RHS(ins, 0);
9777 right = RHS(ins, 1);
9778
9779 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009780 int val;
9781 val = const_scmp(state, ins, left, right);
9782 if ((val >= -1) && (val <= 1)) {
9783 mkconst(state, ins, val <= 0);
9784 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009785 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009786 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009787 mkconst(state, ins, 1);
9788 }
9789}
9790
9791static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9792{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009793 struct triple *left, *right;
9794 left = RHS(ins, 0);
9795 right = RHS(ins, 1);
9796
9797 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009798 int val;
9799 val = const_ucmp(state, ins, left, right);
9800 if ((val >= -1) && (val <= 1)) {
9801 mkconst(state, ins, val <= 0);
9802 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009803 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009804 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009805 mkconst(state, ins, 1);
9806 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009807 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009808 mkconst(state, ins, 1);
9809 }
9810}
9811
9812static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9813{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009814 struct triple *left, *right;
9815 left = RHS(ins, 0);
9816 right = RHS(ins, 1);
9817
9818 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009819 int val;
9820 val = const_scmp(state, ins, left, right);
9821 if ((val >= -1) && (val <= 1)) {
9822 mkconst(state, ins, val >= 0);
9823 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009824 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009825 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009826 mkconst(state, ins, 1);
9827 }
9828}
9829
9830static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9831{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009832 struct triple *left, *right;
9833 left = RHS(ins, 0);
9834 right = RHS(ins, 1);
9835
9836 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009837 int val;
9838 val = const_ucmp(state, ins, left, right);
9839 if ((val >= -1) && (val <= 1)) {
9840 mkconst(state, ins, val >= 0);
9841 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009842 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009843 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009844 mkconst(state, ins, 1);
9845 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009846 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009847 mkconst(state, ins, 1);
9848 }
9849}
9850
9851static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9852{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009853 struct triple *rhs;
9854 rhs = RHS(ins, 0);
9855
9856 if (is_const(rhs)) {
9857 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009858 }
9859 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009860 else if ((rhs->use) &&
9861 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009862 int need_copy = 1;
9863 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009864 switch(rhs->op) {
9865 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9866 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9867 case OP_EQ: rhs->op = OP_NOTEQ; break;
9868 case OP_NOTEQ: rhs->op = OP_EQ; break;
9869 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9870 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9871 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9872 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9873 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9874 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9875 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9876 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009877 default:
9878 need_copy = 0;
9879 break;
9880 }
9881 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009882 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009883 }
9884 }
9885}
9886
9887static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9888{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009889 struct triple *rhs;
9890 rhs = RHS(ins, 0);
9891
9892 if (is_const(rhs)) {
9893 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009894 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009895 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009896 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9897 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9898 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009899 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009900 }
9901
9902}
9903
Eric Biederman90089602004-05-28 14:11:54 +00009904static void simplify_load(struct compile_state *state, struct triple *ins)
9905{
9906 struct triple *addr, *sdecl, *blob;
9907
9908 /* If I am doing a load with a constant pointer from a constant
9909 * table get the value.
9910 */
9911 addr = RHS(ins, 0);
9912 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9913 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9914 (blob->op == OP_BLOBCONST)) {
9915 unsigned char buffer[SIZEOF_WORD];
9916 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009917 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009918 ulong_t val;
9919 reg_size = reg_size_of(state, ins->type);
9920 if (reg_size > REG_SIZEOF_REG) {
9921 internal_error(state, ins, "load size greater than register");
9922 }
9923 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009924 end = blob->u.blob;
9925 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009926 src = blob->u.blob;
9927 src += addr->u.cval;
9928
Eric Biederman7dea9552004-06-29 05:38:37 +00009929 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009930 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009931 }
9932
Eric Biederman90089602004-05-28 14:11:54 +00009933 memset(buffer, 0, sizeof(buffer));
9934 memcpy(buffer, src, bits_to_bytes(mem_size));
9935
9936 switch(mem_size) {
9937 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9938 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9939 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9940 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9941 default:
9942 internal_error(state, ins, "mem_size: %d not handled",
9943 mem_size);
9944 val = 0;
9945 break;
9946 }
9947 mkconst(state, ins, val);
9948 }
9949}
9950
9951static void simplify_uextract(struct compile_state *state, struct triple *ins)
9952{
9953 if (is_simple_const(RHS(ins, 0))) {
9954 ulong_t val;
9955 ulong_t mask;
9956 val = read_const(state, ins, RHS(ins, 0));
9957 mask = 1;
9958 mask <<= ins->u.bitfield.size;
9959 mask -= 1;
9960 val >>= ins->u.bitfield.offset;
9961 val &= mask;
9962 mkconst(state, ins, val);
9963 }
9964}
9965
9966static void simplify_sextract(struct compile_state *state, struct triple *ins)
9967{
9968 if (is_simple_const(RHS(ins, 0))) {
9969 ulong_t val;
9970 ulong_t mask;
9971 long_t sval;
9972 val = read_const(state, ins, RHS(ins, 0));
9973 mask = 1;
9974 mask <<= ins->u.bitfield.size;
9975 mask -= 1;
9976 val >>= ins->u.bitfield.offset;
9977 val &= mask;
9978 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9979 sval = val;
9980 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
9981 mkconst(state, ins, sval);
9982 }
9983}
9984
9985static void simplify_deposit(struct compile_state *state, struct triple *ins)
9986{
9987 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9988 ulong_t targ, val;
9989 ulong_t mask;
9990 targ = read_const(state, ins, RHS(ins, 0));
9991 val = read_const(state, ins, RHS(ins, 1));
9992 mask = 1;
9993 mask <<= ins->u.bitfield.size;
9994 mask -= 1;
9995 mask <<= ins->u.bitfield.offset;
9996 targ &= ~mask;
9997 val <<= ins->u.bitfield.offset;
9998 val &= mask;
9999 targ |= val;
10000 mkconst(state, ins, targ);
10001 }
10002}
10003
Eric Biedermanb138ac82003-04-22 18:44:01 +000010004static void simplify_copy(struct compile_state *state, struct triple *ins)
10005{
Eric Biederman90089602004-05-28 14:11:54 +000010006 struct triple *right;
10007 right = RHS(ins, 0);
10008 if (is_subset_type(ins->type, right->type)) {
10009 ins->type = right->type;
10010 }
10011 if (equiv_types(ins->type, right->type)) {
10012 ins->op = OP_COPY;/* I don't need to convert if the types match */
10013 } else {
10014 if (ins->op == OP_COPY) {
10015 internal_error(state, ins, "type mismatch on copy");
10016 }
10017 }
10018 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10019 struct triple *sdecl;
10020 ulong_t offset;
10021 sdecl = MISC(right, 0);
10022 offset = right->u.cval;
10023 mkaddr_const(state, ins, sdecl, offset);
10024 }
10025 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10026 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010027 case OP_INTCONST:
10028 {
10029 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010030 left = read_const(state, ins, right);
10031 /* Ensure I have not overflowed the destination. */
10032 if (size_of(state, right->type) > size_of(state, ins->type)) {
10033 ulong_t mask;
10034 mask = 1;
10035 mask <<= size_of(state, ins->type);
10036 mask -= 1;
10037 left &= mask;
10038 }
10039 /* Ensure I am properly sign extended */
10040 if (size_of(state, right->type) < size_of(state, ins->type) &&
10041 is_signed(right->type)) {
10042 long_t val;
10043 int shift;
10044 shift = SIZEOF_LONG - size_of(state, right->type);
10045 val = left;
10046 val <<= shift;
10047 val >>= shift;
10048 left = val;
10049 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010050 mkconst(state, ins, left);
10051 break;
10052 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010053 default:
10054 internal_error(state, ins, "uknown constant");
10055 break;
10056 }
10057 }
10058}
10059
Eric Biederman83b991a2003-10-11 06:20:25 +000010060static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010061{
10062 struct triple *ptr;
10063 if (!block) {
10064 return 0;
10065 }
10066 ptr = block->first;
10067 do {
10068 if (ptr->op == OP_PHI) {
10069 return 1;
10070 }
10071 ptr = ptr->next;
10072 } while(ptr != block->last);
10073 return 0;
10074}
10075
Eric Biederman83b991a2003-10-11 06:20:25 +000010076static int phi_dependency(struct block *block)
10077{
10078 /* A block has a phi dependency if a phi function
10079 * depends on that block to exist, and makes a block
10080 * that is otherwise useless unsafe to remove.
10081 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010082 if (block) {
10083 struct block_set *edge;
10084 for(edge = block->edges; edge; edge = edge->next) {
10085 if (phi_present(edge->member)) {
10086 return 1;
10087 }
10088 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010089 }
10090 return 0;
10091}
10092
10093static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10094{
10095 struct triple *targ;
10096 targ = TARG(ins, 0);
10097 /* During scc_transform temporary triples are allocated that
10098 * loop back onto themselves. If I see one don't advance the
10099 * target.
10100 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010101 while(triple_is_structural(state, targ) &&
10102 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010103 targ = targ->next;
10104 }
10105 return targ;
10106}
10107
10108
10109static void simplify_branch(struct compile_state *state, struct triple *ins)
10110{
Eric Biederman90089602004-05-28 14:11:54 +000010111 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010112 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010113 internal_error(state, ins, "not branch");
10114 }
10115 if (ins->use != 0) {
10116 internal_error(state, ins, "branch use");
10117 }
10118 /* The challenge here with simplify branch is that I need to
10119 * make modifications to the control flow graph as well
10120 * as to the branch instruction itself. That is handled
10121 * by rebuilding the basic blocks after simplify all is called.
10122 */
10123
10124 /* If we have a branch to an unconditional branch update
10125 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010126 * functions.
10127 * Also only do this a limited number of times so
10128 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010129 */
Eric Biederman90089602004-05-28 14:11:54 +000010130 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010131 do {
10132 struct triple *targ;
10133 simplified = 0;
10134 targ = branch_target(state, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010135 if ((targ != ins) && (targ->op == OP_BRANCH) &&
10136 !phi_dependency(targ->u.block))
10137 {
10138 unuse_triple(TARG(ins, 0), ins);
10139 TARG(ins, 0) = TARG(targ, 0);
10140 use_triple(TARG(ins, 0), ins);
10141 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010142 }
Eric Biederman90089602004-05-28 14:11:54 +000010143 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010144
10145 /* If we have a conditional branch with a constant condition
10146 * make it an unconditional branch.
10147 */
Eric Biederman90089602004-05-28 14:11:54 +000010148 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010149 struct triple *targ;
10150 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010151 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010152 unuse_triple(RHS(ins, 0), ins);
10153 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010154 ins->rhs = 0;
10155 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010156 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010157 if (value) {
10158 unuse_triple(ins->next, ins);
10159 TARG(ins, 0) = targ;
10160 }
10161 else {
10162 unuse_triple(targ, ins);
10163 TARG(ins, 0) = ins->next;
10164 }
10165 }
Eric Biederman90089602004-05-28 14:11:54 +000010166
10167 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010168 * make it a noop.
10169 */
10170 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010171 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010172 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010173 unuse_triple(RHS(ins, 0), ins);
10174 unuse_triple(ins->next, ins);
10175 }
Eric Biederman90089602004-05-28 14:11:54 +000010176 ins->lhs = 0;
10177 ins->rhs = 0;
10178 ins->misc = 0;
10179 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010180 ins->op = OP_NOOP;
10181 if (ins->use) {
10182 internal_error(state, ins, "noop use != 0");
10183 }
10184 }
10185}
10186
Eric Biederman530b5192003-07-01 10:05:30 +000010187static void simplify_label(struct compile_state *state, struct triple *ins)
10188{
Eric Biederman83b991a2003-10-11 06:20:25 +000010189 /* Ignore volatile labels */
10190 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010191 return;
10192 }
10193 if (ins->use == 0) {
10194 ins->op = OP_NOOP;
10195 }
10196 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010197 /* In general it is not safe to merge one label that
10198 * imediately follows another. The problem is that the empty
10199 * looking block may have phi functions that depend on it.
10200 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010201 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010202 struct triple_set *user, *next;
10203 ins->op = OP_NOOP;
10204 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010205 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010206 next = user->next;
10207 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010208 expr = triple_targ(state, use, 0);
10209 for(;expr; expr = triple_targ(state, use, expr)) {
10210 if (*expr == ins) {
10211 *expr = ins->prev;
10212 unuse_triple(ins, use);
10213 use_triple(ins->prev, use);
10214 }
10215
Eric Biederman530b5192003-07-01 10:05:30 +000010216 }
10217 }
10218 if (ins->use) {
10219 internal_error(state, ins, "noop use != 0");
10220 }
10221 }
10222 }
10223}
10224
Eric Biedermanb138ac82003-04-22 18:44:01 +000010225static void simplify_phi(struct compile_state *state, struct triple *ins)
10226{
Eric Biederman83b991a2003-10-11 06:20:25 +000010227 struct triple **slot;
10228 struct triple *value;
10229 int zrhs, i;
10230 ulong_t cvalue;
10231 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010232 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010233 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010234 return;
10235 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010236 /* See if all of the rhs members of a phi have the same value */
10237 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010238 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010239 for(i = 1; i < zrhs; i++) {
10240 if ( !slot[i] ||
10241 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010242 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010243 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010244 break;
10245 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010246 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010247 if (i == zrhs) {
10248 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010249 return;
10250 }
10251 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010252
10253 /* See if all of rhs members of a phi are the same */
10254 value = slot[0];
10255 for(i = 1; i < zrhs; i++) {
10256 if (slot[i] != value) {
10257 break;
10258 }
10259 }
10260 if (i == zrhs) {
10261 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010262 if (!is_subset_type(ins->type, value->type)) {
10263 internal_error(state, ins, "bad input type to phi");
10264 }
10265 /* Make the types match */
10266 if (!equiv_types(ins->type, value->type)) {
10267 ins->type = value->type;
10268 }
10269 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010270 mkcopy(state, ins, value);
10271 return;
10272 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010273}
10274
10275
10276static void simplify_bsf(struct compile_state *state, struct triple *ins)
10277{
Eric Biederman90089602004-05-28 14:11:54 +000010278 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010279 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010280 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010281 mkconst(state, ins, bsf(left));
10282 }
10283}
10284
10285static void simplify_bsr(struct compile_state *state, struct triple *ins)
10286{
Eric Biederman90089602004-05-28 14:11:54 +000010287 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010288 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010289 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010290 mkconst(state, ins, bsr(left));
10291 }
10292}
10293
10294
10295typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010296static const struct simplify_table {
10297 simplify_t func;
10298 unsigned long flag;
10299} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010300#define simplify_sdivt simplify_noop
10301#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010302#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010303
Eric Biederman5ade04a2003-10-22 04:03:46 +000010304[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10305[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10306[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10307[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10308[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10309[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10310[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10311[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10312[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10313[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10314[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10315[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10316[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10317[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10318[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10319[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10320[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10321[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10322[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010323
Eric Biederman5ade04a2003-10-22 04:03:46 +000010324[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10325[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10326[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10327[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10328[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10329[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10330[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10331[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10332[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10333[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10334[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10335[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010336
Eric Biederman90089602004-05-28 14:11:54 +000010337[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010338[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010339
Eric Biederman90089602004-05-28 14:11:54 +000010340[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10341[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10342[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10343
Eric Biederman5ade04a2003-10-22 04:03:46 +000010344[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010345
Eric Biederman5ade04a2003-10-22 04:03:46 +000010346[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10347[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10348[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010349[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010350
Eric Biederman5ade04a2003-10-22 04:03:46 +000010351[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10352[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10353[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010354[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010355[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10356[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010357
Eric Biederman5ade04a2003-10-22 04:03:46 +000010358[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010359[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010360
Eric Biederman5ade04a2003-10-22 04:03:46 +000010361[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10362[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10363[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10364[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10365[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10366[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10367[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10368[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10369[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010370
Eric Biederman5ade04a2003-10-22 04:03:46 +000010371[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10372[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10373[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10374[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10375[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10376[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10377[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10378[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10379[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10380[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10381[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010382};
10383
Eric Biederman90089602004-05-28 14:11:54 +000010384static inline void debug_simplify(struct compile_state *state,
10385 simplify_t do_simplify, struct triple *ins)
10386{
10387#if DEBUG_SIMPLIFY_HIRES
10388 if (state->functions_joined && (do_simplify != simplify_noop)) {
10389 /* High resolution debugging mode */
10390 fprintf(state->dbgout, "simplifing: ");
10391 display_triple(state->dbgout, ins);
10392 }
10393#endif
10394 do_simplify(state, ins);
10395#if DEBUG_SIMPLIFY_HIRES
10396 if (state->functions_joined && (do_simplify != simplify_noop)) {
10397 /* High resolution debugging mode */
10398 fprintf(state->dbgout, "simplified: ");
10399 display_triple(state->dbgout, ins);
10400 }
10401#endif
10402}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010403static void simplify(struct compile_state *state, struct triple *ins)
10404{
10405 int op;
10406 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010407 if (ins == &unknown_triple) {
10408 internal_error(state, ins, "simplifying the unknown triple?");
10409 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010410 do {
10411 op = ins->op;
10412 do_simplify = 0;
10413 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10414 do_simplify = 0;
10415 }
Eric Biederman90089602004-05-28 14:11:54 +000010416 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010417 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010418 }
Eric Biederman90089602004-05-28 14:11:54 +000010419 if (do_simplify &&
10420 !(state->compiler->flags & table_simplify[op].flag)) {
10421 do_simplify = simplify_noop;
10422 }
10423 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010424 do_simplify = simplify_noop;
10425 }
10426
Eric Biedermanb138ac82003-04-22 18:44:01 +000010427 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010428 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010429 op, tops(op));
10430 return;
10431 }
Eric Biederman90089602004-05-28 14:11:54 +000010432 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010433 } while(ins->op != op);
10434}
10435
Eric Biederman5ade04a2003-10-22 04:03:46 +000010436static void rebuild_ssa_form(struct compile_state *state);
10437
Eric Biedermanb138ac82003-04-22 18:44:01 +000010438static void simplify_all(struct compile_state *state)
10439{
10440 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010441 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10442 return;
10443 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010444 first = state->first;
10445 ins = first->prev;
10446 do {
10447 simplify(state, ins);
10448 ins = ins->prev;
10449 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010450 ins = first;
10451 do {
10452 simplify(state, ins);
10453 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010454 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010455 rebuild_ssa_form(state);
10456
Eric Biederman90089602004-05-28 14:11:54 +000010457 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010458}
10459
10460/*
10461 * Builtins....
10462 * ============================
10463 */
10464
Eric Biederman0babc1c2003-05-09 02:39:00 +000010465static void register_builtin_function(struct compile_state *state,
10466 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010467{
Eric Biederman90089602004-05-28 14:11:54 +000010468 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010469 struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010470 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010471 struct file_state file;
10472 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010473 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010474 va_list args;
10475 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010476
10477 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010478 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010479 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010480 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010481 file.report_line = 1;
10482 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010483 file.prev = state->file;
10484 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010485 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010486
10487 /* Find the Parameter count */
10488 valid_op(state, op);
10489 parameters = table_ops[op].rhs;
10490 if (parameters < 0 ) {
10491 internal_error(state, 0, "Invalid builtin parameter count");
10492 }
10493
10494 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010495 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010496 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010497 next = &ftype->right;
10498 va_start(args, rtype);
10499 for(i = 0; i < parameters; i++) {
10500 atype = va_arg(args, struct type *);
10501 if (!*next) {
10502 *next = atype;
10503 } else {
10504 *next = new_type(TYPE_PRODUCT, *next, atype);
10505 next = &((*next)->right);
10506 }
10507 }
10508 if (!*next) {
10509 *next = &void_type;
10510 }
10511 va_end(args);
10512
Eric Biederman90089602004-05-28 14:11:54 +000010513 /* Get the initial closure type */
10514 ctype = new_type(TYPE_JOIN, &void_type, 0);
10515 ctype->elements = 1;
10516
10517 /* Get the return type */
10518 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10519 crtype->elements = 2;
10520
Eric Biedermanb138ac82003-04-22 18:44:01 +000010521 /* Generate the needed triples */
10522 def = triple(state, OP_LIST, ftype, 0, 0);
10523 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010524 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010525 result = flatten(state, first, variable(state, crtype));
10526 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010527 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010528
10529 /* Now string them together */
10530 param = ftype->right;
10531 for(i = 0; i < parameters; i++) {
10532 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10533 atype = param->left;
10534 } else {
10535 atype = param;
10536 }
10537 arg = flatten(state, first, variable(state, atype));
10538 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010539 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010540 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010541 generate_lhs_pieces(state, work);
10542 for(i = 0; i < parameters; i++) {
10543 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010544 }
Eric Biederman90089602004-05-28 14:11:54 +000010545 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10546 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010547 }
10548 work = flatten(state, first, work);
10549 last = flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010550 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010551 name_len = strlen(name);
10552 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010553 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010554 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010555
Eric Biedermanb138ac82003-04-22 18:44:01 +000010556 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010557 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010558 state->main_function = 0;
10559
Eric Biederman5ade04a2003-10-22 04:03:46 +000010560 if (!state->functions) {
10561 state->functions = def;
10562 } else {
10563 insert_triple(state, state->functions, def);
10564 }
10565 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010566 FILE *fp = state->dbgout;
10567 fprintf(fp, "\n");
10568 loc(fp, state, 0);
10569 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10570 display_func(state, fp, def);
10571 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010572 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010573}
10574
Eric Biederman0babc1c2003-05-09 02:39:00 +000010575static struct type *partial_struct(struct compile_state *state,
10576 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010577{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010578 struct hash_entry *field_ident;
10579 struct type *result;
10580 int field_name_len;
10581
10582 field_name_len = strlen(field_name);
10583 field_ident = lookup(state, field_name, field_name_len);
10584
10585 result = clone_type(0, type);
10586 result->field_ident = field_ident;
10587
10588 if (rest) {
10589 result = new_type(TYPE_PRODUCT, result, rest);
10590 }
10591 return result;
10592}
10593
10594static struct type *register_builtin_type(struct compile_state *state,
10595 const char *name, struct type *type)
10596{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010597 struct hash_entry *ident;
10598 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010599
Eric Biedermanb138ac82003-04-22 18:44:01 +000010600 name_len = strlen(name);
10601 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010602
10603 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10604 ulong_t elements = 0;
10605 struct type *field;
10606 type = new_type(TYPE_STRUCT, type, 0);
10607 field = type->left;
10608 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10609 elements++;
10610 field = field->right;
10611 }
10612 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010613 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010614 type->type_ident = ident;
10615 type->elements = elements;
10616 }
10617 symbol(state, ident, &ident->sym_ident, 0, type);
10618 ident->tok = TOK_TYPE_NAME;
10619 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010620}
10621
Eric Biederman0babc1c2003-05-09 02:39:00 +000010622
Eric Biedermanb138ac82003-04-22 18:44:01 +000010623static void register_builtins(struct compile_state *state)
10624{
Eric Biederman530b5192003-07-01 10:05:30 +000010625 struct type *div_type, *ldiv_type;
10626 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010627 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010628
Eric Biederman530b5192003-07-01 10:05:30 +000010629 div_type = register_builtin_type(state, "__builtin_div_t",
10630 partial_struct(state, "quot", &int_type,
10631 partial_struct(state, "rem", &int_type, 0)));
10632 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10633 partial_struct(state, "quot", &long_type,
10634 partial_struct(state, "rem", &long_type, 0)));
10635 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10636 partial_struct(state, "quot", &uint_type,
10637 partial_struct(state, "rem", &uint_type, 0)));
10638 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10639 partial_struct(state, "quot", &ulong_type,
10640 partial_struct(state, "rem", &ulong_type, 0)));
10641
10642 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10643 &int_type, &int_type);
10644 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10645 &long_type, &long_type);
10646 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10647 &uint_type, &uint_type);
10648 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10649 &ulong_type, &ulong_type);
10650
Eric Biederman0babc1c2003-05-09 02:39:00 +000010651 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
10652 &ushort_type);
10653 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10654 &ushort_type);
10655 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
10656 &ushort_type);
10657
10658 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
10659 &uchar_type, &ushort_type);
10660 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
10661 &ushort_type, &ushort_type);
10662 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
10663 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010664
Eric Biederman0babc1c2003-05-09 02:39:00 +000010665 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
10666 &int_type);
10667 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
10668 &int_type);
10669
10670 msr_type = register_builtin_type(state, "__builtin_msr_t",
10671 partial_struct(state, "lo", &ulong_type,
10672 partial_struct(state, "hi", &ulong_type, 0)));
10673
10674 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10675 &ulong_type);
10676 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10677 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010678
Eric Biederman0babc1c2003-05-09 02:39:00 +000010679 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
10680 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010681}
10682
10683static struct type *declarator(
10684 struct compile_state *state, struct type *type,
10685 struct hash_entry **ident, int need_ident);
10686static void decl(struct compile_state *state, struct triple *first);
10687static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010688#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010689static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010690#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010691static struct type *decl_specifiers(struct compile_state *state);
10692static int istype(int tok);
10693static struct triple *expr(struct compile_state *state);
10694static struct triple *assignment_expr(struct compile_state *state);
10695static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010696static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010697
10698static struct triple *call_expr(
10699 struct compile_state *state, struct triple *func)
10700{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010701 struct triple *def;
10702 struct type *param, *type;
10703 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010704
10705 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10706 error(state, 0, "Called object is not a function");
10707 }
10708 if (func->op != OP_LIST) {
10709 internal_error(state, 0, "improper function");
10710 }
10711 eat(state, TOK_LPAREN);
10712 /* Find the return type without any specifiers */
10713 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010714 /* Count the number of rhs entries for OP_FCALL */
10715 param = func->type->right;
10716 pvals = 0;
10717 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10718 pvals++;
10719 param = param->right;
10720 }
10721 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10722 pvals++;
10723 }
10724 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010725 MISC(def, 0) = func;
10726
10727 param = func->type->right;
10728 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010729 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010730 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010731 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010732 arg_type = param;
10733 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10734 arg_type = param->left;
10735 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010736 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010737 RHS(def, index) = val;
10738 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010739 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010740 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010741 }
10742 }
10743 eat(state, TOK_RPAREN);
10744 return def;
10745}
10746
10747
10748static struct triple *character_constant(struct compile_state *state)
10749{
10750 struct triple *def;
10751 struct token *tk;
10752 const signed char *str, *end;
10753 int c;
10754 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010755 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010756 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010757 str_len = tk->str_len - 2;
10758 if (str_len <= 0) {
10759 error(state, 0, "empty character constant");
10760 }
10761 end = str + str_len;
10762 c = char_value(state, &str, end);
10763 if (str != end) {
10764 error(state, 0, "multibyte character constant not supported");
10765 }
10766 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10767 return def;
10768}
10769
10770static struct triple *string_constant(struct compile_state *state)
10771{
10772 struct triple *def;
10773 struct token *tk;
10774 struct type *type;
10775 const signed char *str, *end;
10776 signed char *buf, *ptr;
10777 int str_len;
10778
10779 buf = 0;
10780 type = new_type(TYPE_ARRAY, &char_type, 0);
10781 type->elements = 0;
10782 /* The while loop handles string concatenation */
10783 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010784 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010785 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010786 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010787 if (str_len < 0) {
10788 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010789 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010790 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010791 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010792 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010793 end = str + str_len;
10794 ptr = buf;
10795 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10796 memcpy(buf, ptr, type->elements);
10797 ptr = buf + type->elements;
10798 do {
10799 *ptr++ = char_value(state, &str, end);
10800 } while(str < end);
10801 type->elements = ptr - buf;
10802 } while(peek(state) == TOK_LIT_STRING);
10803 *ptr = '\0';
10804 type->elements += 1;
10805 def = triple(state, OP_BLOBCONST, type, 0, 0);
10806 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010807
Eric Biedermanb138ac82003-04-22 18:44:01 +000010808 return def;
10809}
10810
10811
10812static struct triple *integer_constant(struct compile_state *state)
10813{
10814 struct triple *def;
10815 unsigned long val;
10816 struct token *tk;
10817 char *end;
10818 int u, l, decimal;
10819 struct type *type;
10820
Eric Biederman41203d92004-11-08 09:31:09 +000010821 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010822 errno = 0;
10823 decimal = (tk->val.str[0] != '0');
10824 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010825 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010826 error(state, 0, "Integer constant to large");
10827 }
10828 u = l = 0;
10829 if ((*end == 'u') || (*end == 'U')) {
10830 u = 1;
10831 end++;
10832 }
10833 if ((*end == 'l') || (*end == 'L')) {
10834 l = 1;
10835 end++;
10836 }
10837 if ((*end == 'u') || (*end == 'U')) {
10838 u = 1;
10839 end++;
10840 }
10841 if (*end) {
10842 error(state, 0, "Junk at end of integer constant");
10843 }
10844 if (u && l) {
10845 type = &ulong_type;
10846 }
10847 else if (l) {
10848 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010849 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010850 type = &ulong_type;
10851 }
10852 }
10853 else if (u) {
10854 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010855 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010856 type = &ulong_type;
10857 }
10858 }
10859 else {
10860 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010861 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010862 type = &uint_type;
10863 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010864 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010865 type = &ulong_type;
10866 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010867 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010868 type = &long_type;
10869 }
10870 }
10871 def = int_const(state, type, val);
10872 return def;
10873}
10874
10875static struct triple *primary_expr(struct compile_state *state)
10876{
10877 struct triple *def;
10878 int tok;
10879 tok = peek(state);
10880 switch(tok) {
10881 case TOK_IDENT:
10882 {
10883 struct hash_entry *ident;
10884 /* Here ident is either:
10885 * a varable name
10886 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010887 */
Eric Biederman41203d92004-11-08 09:31:09 +000010888 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010889 if (!ident->sym_ident) {
10890 error(state, 0, "%s undeclared", ident->name);
10891 }
10892 def = ident->sym_ident->def;
10893 break;
10894 }
10895 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010896 {
10897 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010898 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010899 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010900 if (!ident->sym_ident) {
10901 error(state, 0, "%s undeclared", ident->name);
10902 }
10903 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010904 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010905 }
Eric Biederman41203d92004-11-08 09:31:09 +000010906 case TOK_MIDENT:
10907 {
10908 struct hash_entry *ident;
10909 ident = eat(state, TOK_MIDENT)->ident;
10910 warning(state, 0, "Replacing undefined macro: %s with 0",
10911 ident->name);
10912 def = int_const(state, &int_type, 0);
10913 break;
10914 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010915 case TOK_LPAREN:
10916 eat(state, TOK_LPAREN);
10917 def = expr(state);
10918 eat(state, TOK_RPAREN);
10919 break;
10920 case TOK_LIT_INT:
10921 def = integer_constant(state);
10922 break;
10923 case TOK_LIT_FLOAT:
10924 eat(state, TOK_LIT_FLOAT);
10925 error(state, 0, "Floating point constants not supported");
10926 def = 0;
10927 FINISHME();
10928 break;
10929 case TOK_LIT_CHAR:
10930 def = character_constant(state);
10931 break;
10932 case TOK_LIT_STRING:
10933 def = string_constant(state);
10934 break;
10935 default:
10936 def = 0;
10937 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10938 }
10939 return def;
10940}
10941
10942static struct triple *postfix_expr(struct compile_state *state)
10943{
10944 struct triple *def;
10945 int postfix;
10946 def = primary_expr(state);
10947 do {
10948 struct triple *left;
10949 int tok;
10950 postfix = 1;
10951 left = def;
10952 switch((tok = peek(state))) {
10953 case TOK_LBRACKET:
10954 eat(state, TOK_LBRACKET);
10955 def = mk_subscript_expr(state, left, expr(state));
10956 eat(state, TOK_RBRACKET);
10957 break;
10958 case TOK_LPAREN:
10959 def = call_expr(state, def);
10960 break;
10961 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010962 {
10963 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010964 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010965 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010966 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010967 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010968 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010969 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010970 {
10971 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010972 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010973 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010974 def = mk_deref_expr(state, read_expr(state, def));
10975 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010976 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010977 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010978 case TOK_PLUSPLUS:
10979 eat(state, TOK_PLUSPLUS);
10980 def = mk_post_inc_expr(state, left);
10981 break;
10982 case TOK_MINUSMINUS:
10983 eat(state, TOK_MINUSMINUS);
10984 def = mk_post_dec_expr(state, left);
10985 break;
10986 default:
10987 postfix = 0;
10988 break;
10989 }
10990 } while(postfix);
10991 return def;
10992}
10993
10994static struct triple *cast_expr(struct compile_state *state);
10995
10996static struct triple *unary_expr(struct compile_state *state)
10997{
10998 struct triple *def, *right;
10999 int tok;
11000 switch((tok = peek(state))) {
11001 case TOK_PLUSPLUS:
11002 eat(state, TOK_PLUSPLUS);
11003 def = mk_pre_inc_expr(state, unary_expr(state));
11004 break;
11005 case TOK_MINUSMINUS:
11006 eat(state, TOK_MINUSMINUS);
11007 def = mk_pre_dec_expr(state, unary_expr(state));
11008 break;
11009 case TOK_AND:
11010 eat(state, TOK_AND);
11011 def = mk_addr_expr(state, cast_expr(state), 0);
11012 break;
11013 case TOK_STAR:
11014 eat(state, TOK_STAR);
11015 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11016 break;
11017 case TOK_PLUS:
11018 eat(state, TOK_PLUS);
11019 right = read_expr(state, cast_expr(state));
11020 arithmetic(state, right);
11021 def = integral_promotion(state, right);
11022 break;
11023 case TOK_MINUS:
11024 eat(state, TOK_MINUS);
11025 right = read_expr(state, cast_expr(state));
11026 arithmetic(state, right);
11027 def = integral_promotion(state, right);
11028 def = triple(state, OP_NEG, def->type, def, 0);
11029 break;
11030 case TOK_TILDE:
11031 eat(state, TOK_TILDE);
11032 right = read_expr(state, cast_expr(state));
11033 integral(state, right);
11034 def = integral_promotion(state, right);
11035 def = triple(state, OP_INVERT, def->type, def, 0);
11036 break;
11037 case TOK_BANG:
11038 eat(state, TOK_BANG);
11039 right = read_expr(state, cast_expr(state));
11040 bool(state, right);
11041 def = lfalse_expr(state, right);
11042 break;
11043 case TOK_SIZEOF:
11044 {
11045 struct type *type;
11046 int tok1, tok2;
11047 eat(state, TOK_SIZEOF);
11048 tok1 = peek(state);
11049 tok2 = peek2(state);
11050 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11051 eat(state, TOK_LPAREN);
11052 type = type_name(state);
11053 eat(state, TOK_RPAREN);
11054 }
11055 else {
11056 struct triple *expr;
11057 expr = unary_expr(state);
11058 type = expr->type;
11059 release_expr(state, expr);
11060 }
Eric Biederman90089602004-05-28 14:11:54 +000011061 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011062 break;
11063 }
11064 case TOK_ALIGNOF:
11065 {
11066 struct type *type;
11067 int tok1, tok2;
11068 eat(state, TOK_ALIGNOF);
11069 tok1 = peek(state);
11070 tok2 = peek2(state);
11071 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11072 eat(state, TOK_LPAREN);
11073 type = type_name(state);
11074 eat(state, TOK_RPAREN);
11075 }
11076 else {
11077 struct triple *expr;
11078 expr = unary_expr(state);
11079 type = expr->type;
11080 release_expr(state, expr);
11081 }
Eric Biederman90089602004-05-28 14:11:54 +000011082 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011083 break;
11084 }
Eric Biederman41203d92004-11-08 09:31:09 +000011085 case TOK_MDEFINED:
11086 {
11087 /* We only come here if we are called from the preprocessor */
11088 struct hash_entry *ident;
11089 int parens;
11090 eat(state, TOK_MDEFINED);
11091 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011092 if (pp_peek(state) == TOK_LPAREN) {
11093 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011094 parens = 1;
11095 }
Eric Biedermancb364952004-11-15 10:46:44 +000011096 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011097 if (parens) {
11098 eat(state, TOK_RPAREN);
11099 }
11100 def = int_const(state, &int_type, ident->sym_define != 0);
11101 break;
11102 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011103 default:
11104 def = postfix_expr(state);
11105 break;
11106 }
11107 return def;
11108}
11109
11110static struct triple *cast_expr(struct compile_state *state)
11111{
11112 struct triple *def;
11113 int tok1, tok2;
11114 tok1 = peek(state);
11115 tok2 = peek2(state);
11116 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11117 struct type *type;
11118 eat(state, TOK_LPAREN);
11119 type = type_name(state);
11120 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011121 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011122 }
11123 else {
11124 def = unary_expr(state);
11125 }
11126 return def;
11127}
11128
11129static struct triple *mult_expr(struct compile_state *state)
11130{
11131 struct triple *def;
11132 int done;
11133 def = cast_expr(state);
11134 do {
11135 struct triple *left, *right;
11136 struct type *result_type;
11137 int tok, op, sign;
11138 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011139 tok = peek(state);
11140 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011141 case TOK_STAR:
11142 case TOK_DIV:
11143 case TOK_MOD:
11144 left = read_expr(state, def);
11145 arithmetic(state, left);
11146
11147 eat(state, tok);
11148
11149 right = read_expr(state, cast_expr(state));
11150 arithmetic(state, right);
11151
11152 result_type = arithmetic_result(state, left, right);
11153 sign = is_signed(result_type);
11154 op = -1;
11155 switch(tok) {
11156 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11157 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11158 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11159 }
11160 def = triple(state, op, result_type, left, right);
11161 break;
11162 default:
11163 done = 1;
11164 break;
11165 }
11166 } while(!done);
11167 return def;
11168}
11169
11170static struct triple *add_expr(struct compile_state *state)
11171{
11172 struct triple *def;
11173 int done;
11174 def = mult_expr(state);
11175 do {
11176 done = 0;
11177 switch( peek(state)) {
11178 case TOK_PLUS:
11179 eat(state, TOK_PLUS);
11180 def = mk_add_expr(state, def, mult_expr(state));
11181 break;
11182 case TOK_MINUS:
11183 eat(state, TOK_MINUS);
11184 def = mk_sub_expr(state, def, mult_expr(state));
11185 break;
11186 default:
11187 done = 1;
11188 break;
11189 }
11190 } while(!done);
11191 return def;
11192}
11193
11194static struct triple *shift_expr(struct compile_state *state)
11195{
11196 struct triple *def;
11197 int done;
11198 def = add_expr(state);
11199 do {
11200 struct triple *left, *right;
11201 int tok, op;
11202 done = 0;
11203 switch((tok = peek(state))) {
11204 case TOK_SL:
11205 case TOK_SR:
11206 left = read_expr(state, def);
11207 integral(state, left);
11208 left = integral_promotion(state, left);
11209
11210 eat(state, tok);
11211
11212 right = read_expr(state, add_expr(state));
11213 integral(state, right);
11214 right = integral_promotion(state, right);
11215
11216 op = (tok == TOK_SL)? OP_SL :
11217 is_signed(left->type)? OP_SSR: OP_USR;
11218
11219 def = triple(state, op, left->type, left, right);
11220 break;
11221 default:
11222 done = 1;
11223 break;
11224 }
11225 } while(!done);
11226 return def;
11227}
11228
11229static struct triple *relational_expr(struct compile_state *state)
11230{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011231#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011232#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011233#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011234 struct triple *def;
11235 int done;
11236 def = shift_expr(state);
11237 do {
11238 struct triple *left, *right;
11239 struct type *arg_type;
11240 int tok, op, sign;
11241 done = 0;
11242 switch((tok = peek(state))) {
11243 case TOK_LESS:
11244 case TOK_MORE:
11245 case TOK_LESSEQ:
11246 case TOK_MOREEQ:
11247 left = read_expr(state, def);
11248 arithmetic(state, left);
11249
11250 eat(state, tok);
11251
11252 right = read_expr(state, shift_expr(state));
11253 arithmetic(state, right);
11254
11255 arg_type = arithmetic_result(state, left, right);
11256 sign = is_signed(arg_type);
11257 op = -1;
11258 switch(tok) {
11259 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11260 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11261 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11262 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11263 }
11264 def = triple(state, op, &int_type, left, right);
11265 break;
11266 default:
11267 done = 1;
11268 break;
11269 }
11270 } while(!done);
11271 return def;
11272}
11273
11274static struct triple *equality_expr(struct compile_state *state)
11275{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011276#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011277#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011278#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011279 struct triple *def;
11280 int done;
11281 def = relational_expr(state);
11282 do {
11283 struct triple *left, *right;
11284 int tok, op;
11285 done = 0;
11286 switch((tok = peek(state))) {
11287 case TOK_EQEQ:
11288 case TOK_NOTEQ:
11289 left = read_expr(state, def);
11290 arithmetic(state, left);
11291 eat(state, tok);
11292 right = read_expr(state, relational_expr(state));
11293 arithmetic(state, right);
11294 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11295 def = triple(state, op, &int_type, left, right);
11296 break;
11297 default:
11298 done = 1;
11299 break;
11300 }
11301 } while(!done);
11302 return def;
11303}
11304
11305static struct triple *and_expr(struct compile_state *state)
11306{
11307 struct triple *def;
11308 def = equality_expr(state);
11309 while(peek(state) == TOK_AND) {
11310 struct triple *left, *right;
11311 struct type *result_type;
11312 left = read_expr(state, def);
11313 integral(state, left);
11314 eat(state, TOK_AND);
11315 right = read_expr(state, equality_expr(state));
11316 integral(state, right);
11317 result_type = arithmetic_result(state, left, right);
11318 def = triple(state, OP_AND, result_type, left, right);
11319 }
11320 return def;
11321}
11322
11323static struct triple *xor_expr(struct compile_state *state)
11324{
11325 struct triple *def;
11326 def = and_expr(state);
11327 while(peek(state) == TOK_XOR) {
11328 struct triple *left, *right;
11329 struct type *result_type;
11330 left = read_expr(state, def);
11331 integral(state, left);
11332 eat(state, TOK_XOR);
11333 right = read_expr(state, and_expr(state));
11334 integral(state, right);
11335 result_type = arithmetic_result(state, left, right);
11336 def = triple(state, OP_XOR, result_type, left, right);
11337 }
11338 return def;
11339}
11340
11341static struct triple *or_expr(struct compile_state *state)
11342{
11343 struct triple *def;
11344 def = xor_expr(state);
11345 while(peek(state) == TOK_OR) {
11346 struct triple *left, *right;
11347 struct type *result_type;
11348 left = read_expr(state, def);
11349 integral(state, left);
11350 eat(state, TOK_OR);
11351 right = read_expr(state, xor_expr(state));
11352 integral(state, right);
11353 result_type = arithmetic_result(state, left, right);
11354 def = triple(state, OP_OR, result_type, left, right);
11355 }
11356 return def;
11357}
11358
11359static struct triple *land_expr(struct compile_state *state)
11360{
11361 struct triple *def;
11362 def = or_expr(state);
11363 while(peek(state) == TOK_LOGAND) {
11364 struct triple *left, *right;
11365 left = read_expr(state, def);
11366 bool(state, left);
11367 eat(state, TOK_LOGAND);
11368 right = read_expr(state, or_expr(state));
11369 bool(state, right);
11370
Eric Biederman90089602004-05-28 14:11:54 +000011371 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011372 ltrue_expr(state, left),
11373 ltrue_expr(state, right));
11374 }
11375 return def;
11376}
11377
11378static struct triple *lor_expr(struct compile_state *state)
11379{
11380 struct triple *def;
11381 def = land_expr(state);
11382 while(peek(state) == TOK_LOGOR) {
11383 struct triple *left, *right;
11384 left = read_expr(state, def);
11385 bool(state, left);
11386 eat(state, TOK_LOGOR);
11387 right = read_expr(state, land_expr(state));
11388 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011389
11390 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011391 ltrue_expr(state, left),
11392 ltrue_expr(state, right));
11393 }
11394 return def;
11395}
11396
11397static struct triple *conditional_expr(struct compile_state *state)
11398{
11399 struct triple *def;
11400 def = lor_expr(state);
11401 if (peek(state) == TOK_QUEST) {
11402 struct triple *test, *left, *right;
11403 bool(state, def);
11404 test = ltrue_expr(state, read_expr(state, def));
11405 eat(state, TOK_QUEST);
11406 left = read_expr(state, expr(state));
11407 eat(state, TOK_COLON);
11408 right = read_expr(state, conditional_expr(state));
11409
Eric Biederman90089602004-05-28 14:11:54 +000011410 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011411 }
11412 return def;
11413}
11414
Eric Biederman41203d92004-11-08 09:31:09 +000011415struct cv_triple {
11416 struct triple *val;
11417 int id;
11418};
11419
11420static void set_cv(struct compile_state *state, struct cv_triple *cv,
11421 struct triple *dest, struct triple *val)
11422{
11423 if (cv[dest->id].val) {
11424 free_triple(state, cv[dest->id].val);
11425 }
11426 cv[dest->id].val = val;
11427}
11428static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11429 struct triple *src)
11430{
11431 return cv[src->id].val;
11432}
11433
Eric Biedermanb138ac82003-04-22 18:44:01 +000011434static struct triple *eval_const_expr(
11435 struct compile_state *state, struct triple *expr)
11436{
11437 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011438 if (is_const(expr)) {
11439 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011440 }
Eric Biederman00443072003-06-24 12:34:45 +000011441 else {
11442 /* If we don't start out as a constant simplify into one */
11443 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011444 struct cv_triple *cv;
11445 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011446 head = label(state); /* dummy initial triple */
11447 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011448 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011449 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011450 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011451 }
Eric Biederman41203d92004-11-08 09:31:09 +000011452 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11453 i = 1;
11454 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11455 cv[i].val = 0;
11456 cv[i].id = ptr->id;
11457 ptr->id = i;
11458 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011459 }
Eric Biederman41203d92004-11-08 09:31:09 +000011460 ptr = head->next;
11461 do {
11462 valid_ins(state, ptr);
11463 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
11464 internal_error(state, ptr,
11465 "unexpected %s in constant expression",
11466 tops(ptr->op));
11467 }
11468 else if (ptr->op == OP_LIST) {
11469 }
11470 else if (triple_is_structural(state, ptr)) {
11471 ptr = ptr->next;
11472 }
11473 else if (triple_is_ubranch(state, ptr)) {
11474 ptr = TARG(ptr, 0);
11475 }
11476 else if (triple_is_cbranch(state, ptr)) {
11477 struct triple *cond_val;
11478 cond_val = get_cv(state, cv, RHS(ptr, 0));
11479 if (!cond_val || !is_const(cond_val) ||
11480 (cond_val->op != OP_INTCONST))
11481 {
11482 internal_error(state, ptr, "bad branch condition");
11483 }
11484 if (cond_val->u.cval == 0) {
11485 ptr = ptr->next;
11486 } else {
11487 ptr = TARG(ptr, 0);
11488 }
11489 }
11490 else if (triple_is_branch(state, ptr)) {
11491 error(state, ptr, "bad branch type in constant expression");
11492 }
11493 else if (ptr->op == OP_WRITE) {
11494 struct triple *val;
11495 val = get_cv(state, cv, RHS(ptr, 0));
11496
11497 set_cv(state, cv, MISC(ptr, 0),
11498 copy_triple(state, val));
11499 set_cv(state, cv, ptr,
11500 copy_triple(state, val));
11501 ptr = ptr->next;
11502 }
11503 else if (ptr->op == OP_READ) {
11504 set_cv(state, cv, ptr,
11505 copy_triple(state,
11506 get_cv(state, cv, RHS(ptr, 0))));
11507 ptr = ptr->next;
11508 }
11509 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11510 struct triple *val, **rhs;
11511 val = copy_triple(state, ptr);
11512 rhs = triple_rhs(state, val, 0);
11513 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11514 if (!*rhs) {
11515 internal_error(state, ptr, "Missing rhs");
11516 }
11517 *rhs = get_cv(state, cv, *rhs);
11518 }
11519 simplify(state, val);
11520 set_cv(state, cv, ptr, val);
11521 ptr = ptr->next;
11522 }
11523 else {
11524 error(state, ptr, "impure operation in constant expression");
11525 }
11526
11527 } while(ptr != head);
11528
11529 /* Get the result value */
11530 def = get_cv(state, cv, head->prev);
11531 cv[head->prev->id].val = 0;
11532
11533 /* Free the temporary values */
11534 for(i = 0; i < count; i++) {
11535 if (cv[i].val) {
11536 free_triple(state, cv[i].val);
11537 cv[i].val = 0;
11538 }
11539 }
11540 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011541 /* Free the intermediate expressions */
11542 while(head->next != head) {
11543 release_triple(state, head->next);
11544 }
11545 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011546 }
Eric Biederman41203d92004-11-08 09:31:09 +000011547 if (!is_const(def)) {
11548 error(state, expr, "Not a constant expression");
11549 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011550 return def;
11551}
11552
11553static struct triple *constant_expr(struct compile_state *state)
11554{
11555 return eval_const_expr(state, conditional_expr(state));
11556}
11557
11558static struct triple *assignment_expr(struct compile_state *state)
11559{
Patrick Georgi8f4546f2010-03-15 10:32:59 +000011560 struct triple *def, *left, *left2, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011561 int tok, op, sign;
11562 /* The C grammer in K&R shows assignment expressions
11563 * only taking unary expressions as input on their
11564 * left hand side. But specifies the precedence of
11565 * assignemnt as the lowest operator except for comma.
11566 *
11567 * Allowing conditional expressions on the left hand side
11568 * of an assignement results in a grammar that accepts
11569 * a larger set of statements than standard C. As long
11570 * as the subset of the grammar that is standard C behaves
11571 * correctly this should cause no problems.
11572 *
11573 * For the extra token strings accepted by the grammar
11574 * none of them should produce a valid lvalue, so they
11575 * should not produce functioning programs.
11576 *
11577 * GCC has this bug as well, so surprises should be minimal.
11578 */
11579 def = conditional_expr(state);
11580 left = def;
Patrick Georgi8f4546f2010-03-15 10:32:59 +000011581 left2 = left;
11582 if (!(left2->id & TRIPLE_FLAG_FLATTENED))
11583 left2 = copy_triple(state, left2);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011584 switch((tok = peek(state))) {
11585 case TOK_EQ:
11586 lvalue(state, left);
11587 eat(state, TOK_EQ);
11588 def = write_expr(state, left,
11589 read_expr(state, assignment_expr(state)));
11590 break;
11591 case TOK_TIMESEQ:
11592 case TOK_DIVEQ:
11593 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011594 lvalue(state, left);
11595 arithmetic(state, left);
11596 eat(state, tok);
11597 right = read_expr(state, assignment_expr(state));
11598 arithmetic(state, right);
11599
11600 sign = is_signed(left->type);
11601 op = -1;
11602 switch(tok) {
11603 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11604 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11605 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011606 }
11607 def = write_expr(state, left,
11608 triple(state, op, left->type,
Patrick Georgi8f4546f2010-03-15 10:32:59 +000011609 read_expr(state, left2), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011610 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011611 case TOK_PLUSEQ:
11612 lvalue(state, left);
11613 eat(state, TOK_PLUSEQ);
11614 def = write_expr(state, left,
Patrick Georgi8f4546f2010-03-15 10:32:59 +000011615 mk_add_expr(state, left2, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011616 break;
11617 case TOK_MINUSEQ:
11618 lvalue(state, left);
11619 eat(state, TOK_MINUSEQ);
11620 def = write_expr(state, left,
Patrick Georgi8f4546f2010-03-15 10:32:59 +000011621 mk_sub_expr(state, left2, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011622 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011623 case TOK_SLEQ:
11624 case TOK_SREQ:
11625 case TOK_ANDEQ:
11626 case TOK_XOREQ:
11627 case TOK_OREQ:
11628 lvalue(state, left);
11629 integral(state, left);
11630 eat(state, tok);
11631 right = read_expr(state, assignment_expr(state));
11632 integral(state, right);
11633 right = integral_promotion(state, right);
11634 sign = is_signed(left->type);
11635 op = -1;
11636 switch(tok) {
11637 case TOK_SLEQ: op = OP_SL; break;
11638 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11639 case TOK_ANDEQ: op = OP_AND; break;
11640 case TOK_XOREQ: op = OP_XOR; break;
11641 case TOK_OREQ: op = OP_OR; break;
11642 }
11643 def = write_expr(state, left,
11644 triple(state, op, left->type,
Patrick Georgi8f4546f2010-03-15 10:32:59 +000011645 read_expr(state, left2), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011646 break;
11647 }
11648 return def;
11649}
11650
11651static struct triple *expr(struct compile_state *state)
11652{
11653 struct triple *def;
11654 def = assignment_expr(state);
11655 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011656 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011657 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011658 }
11659 return def;
11660}
11661
11662static void expr_statement(struct compile_state *state, struct triple *first)
11663{
11664 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011665 /* lvalue conversions always apply except when certian operators
11666 * are applied. I apply the lvalue conversions here
11667 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011668 */
11669 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011670 }
11671 eat(state, TOK_SEMI);
11672}
11673
11674static void if_statement(struct compile_state *state, struct triple *first)
11675{
11676 struct triple *test, *jmp1, *jmp2, *middle, *end;
11677
11678 jmp1 = jmp2 = middle = 0;
11679 eat(state, TOK_IF);
11680 eat(state, TOK_LPAREN);
11681 test = expr(state);
11682 bool(state, test);
11683 /* Cleanup and invert the test */
11684 test = lfalse_expr(state, read_expr(state, test));
11685 eat(state, TOK_RPAREN);
11686 /* Generate the needed pieces */
11687 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011688 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011689 /* Thread the pieces together */
11690 flatten(state, first, test);
11691 flatten(state, first, jmp1);
11692 flatten(state, first, label(state));
11693 statement(state, first);
11694 if (peek(state) == TOK_ELSE) {
11695 eat(state, TOK_ELSE);
11696 /* Generate the rest of the pieces */
11697 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011698 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011699 /* Thread them together */
11700 flatten(state, first, jmp2);
11701 flatten(state, first, middle);
11702 statement(state, first);
11703 flatten(state, first, end);
11704 }
11705 else {
11706 flatten(state, first, middle);
11707 }
11708}
11709
11710static void for_statement(struct compile_state *state, struct triple *first)
11711{
11712 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11713 struct triple *label1, *label2, *label3;
11714 struct hash_entry *ident;
11715
11716 eat(state, TOK_FOR);
11717 eat(state, TOK_LPAREN);
11718 head = test = tail = jmp1 = jmp2 = 0;
11719 if (peek(state) != TOK_SEMI) {
11720 head = expr(state);
11721 }
11722 eat(state, TOK_SEMI);
11723 if (peek(state) != TOK_SEMI) {
11724 test = expr(state);
11725 bool(state, test);
11726 test = ltrue_expr(state, read_expr(state, test));
11727 }
11728 eat(state, TOK_SEMI);
11729 if (peek(state) != TOK_RPAREN) {
11730 tail = expr(state);
11731 }
11732 eat(state, TOK_RPAREN);
11733 /* Generate the needed pieces */
11734 label1 = label(state);
11735 label2 = label(state);
11736 label3 = label(state);
11737 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011738 jmp1 = branch(state, label3, 0);
11739 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011740 }
11741 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011742 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011743 }
11744 end = label(state);
11745 /* Remember where break and continue go */
11746 start_scope(state);
11747 ident = state->i_break;
11748 symbol(state, ident, &ident->sym_ident, end, end->type);
11749 ident = state->i_continue;
11750 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11751 /* Now include the body */
11752 flatten(state, first, head);
11753 flatten(state, first, jmp1);
11754 flatten(state, first, label1);
11755 statement(state, first);
11756 flatten(state, first, label2);
11757 flatten(state, first, tail);
11758 flatten(state, first, label3);
11759 flatten(state, first, test);
11760 flatten(state, first, jmp2);
11761 flatten(state, first, end);
11762 /* Cleanup the break/continue scope */
11763 end_scope(state);
11764}
11765
11766static void while_statement(struct compile_state *state, struct triple *first)
11767{
11768 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11769 struct hash_entry *ident;
11770 eat(state, TOK_WHILE);
11771 eat(state, TOK_LPAREN);
11772 test = expr(state);
11773 bool(state, test);
11774 test = ltrue_expr(state, read_expr(state, test));
11775 eat(state, TOK_RPAREN);
11776 /* Generate the needed pieces */
11777 label1 = label(state);
11778 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011779 jmp1 = branch(state, label2, 0);
11780 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011781 end = label(state);
11782 /* Remember where break and continue go */
11783 start_scope(state);
11784 ident = state->i_break;
11785 symbol(state, ident, &ident->sym_ident, end, end->type);
11786 ident = state->i_continue;
11787 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11788 /* Thread them together */
11789 flatten(state, first, jmp1);
11790 flatten(state, first, label1);
11791 statement(state, first);
11792 flatten(state, first, label2);
11793 flatten(state, first, test);
11794 flatten(state, first, jmp2);
11795 flatten(state, first, end);
11796 /* Cleanup the break/continue scope */
11797 end_scope(state);
11798}
11799
11800static void do_statement(struct compile_state *state, struct triple *first)
11801{
11802 struct triple *label1, *label2, *test, *end;
11803 struct hash_entry *ident;
11804 eat(state, TOK_DO);
11805 /* Generate the needed pieces */
11806 label1 = label(state);
11807 label2 = label(state);
11808 end = label(state);
11809 /* Remember where break and continue go */
11810 start_scope(state);
11811 ident = state->i_break;
11812 symbol(state, ident, &ident->sym_ident, end, end->type);
11813 ident = state->i_continue;
11814 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11815 /* Now include the body */
11816 flatten(state, first, label1);
11817 statement(state, first);
11818 /* Cleanup the break/continue scope */
11819 end_scope(state);
11820 /* Eat the rest of the loop */
11821 eat(state, TOK_WHILE);
11822 eat(state, TOK_LPAREN);
11823 test = read_expr(state, expr(state));
11824 bool(state, test);
11825 eat(state, TOK_RPAREN);
11826 eat(state, TOK_SEMI);
11827 /* Thread the pieces together */
11828 test = ltrue_expr(state, test);
11829 flatten(state, first, label2);
11830 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011831 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011832 flatten(state, first, end);
11833}
11834
11835
11836static void return_statement(struct compile_state *state, struct triple *first)
11837{
11838 struct triple *jmp, *mv, *dest, *var, *val;
11839 int last;
11840 eat(state, TOK_RETURN);
11841
Stefan Reinauer50542a82007-10-24 11:14:14 +000011842#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011843#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011844#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011845 val = 0;
11846 /* If we have a return value do some more work */
11847 if (peek(state) != TOK_SEMI) {
11848 val = read_expr(state, expr(state));
11849 }
11850 eat(state, TOK_SEMI);
11851
11852 /* See if this last statement in a function */
11853 last = ((peek(state) == TOK_RBRACE) &&
11854 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11855
11856 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011857 var = fresult(state, state->main_function);
11858
Eric Biedermanb138ac82003-04-22 18:44:01 +000011859 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011860 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011861 mv = jmp = 0;
11862 /* If needed generate a jump instruction */
11863 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011864 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011865 }
11866 /* If needed generate an assignment instruction */
11867 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011868 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011869 }
11870 /* Now put the code together */
11871 if (mv) {
11872 flatten(state, first, mv);
11873 flatten(state, first, jmp);
11874 }
11875 else if (jmp) {
11876 flatten(state, first, jmp);
11877 }
11878}
11879
11880static void break_statement(struct compile_state *state, struct triple *first)
11881{
11882 struct triple *dest;
11883 eat(state, TOK_BREAK);
11884 eat(state, TOK_SEMI);
11885 if (!state->i_break->sym_ident) {
11886 error(state, 0, "break statement not within loop or switch");
11887 }
11888 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011889 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011890}
11891
11892static void continue_statement(struct compile_state *state, struct triple *first)
11893{
11894 struct triple *dest;
11895 eat(state, TOK_CONTINUE);
11896 eat(state, TOK_SEMI);
11897 if (!state->i_continue->sym_ident) {
11898 error(state, 0, "continue statement outside of a loop");
11899 }
11900 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011901 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011902}
11903
11904static void goto_statement(struct compile_state *state, struct triple *first)
11905{
Eric Biederman153ea352003-06-20 14:43:20 +000011906 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011907 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011908 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011909 if (!ident->sym_label) {
11910 /* If this is a forward branch allocate the label now,
11911 * it will be flattend in the appropriate location later.
11912 */
11913 struct triple *ins;
11914 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011915 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011916 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011917 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011918
11919 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011920}
11921
11922static void labeled_statement(struct compile_state *state, struct triple *first)
11923{
Eric Biederman153ea352003-06-20 14:43:20 +000011924 struct triple *ins;
11925 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011926
Eric Biederman41203d92004-11-08 09:31:09 +000011927 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011928 if (ident->sym_label && ident->sym_label->def) {
11929 ins = ident->sym_label->def;
11930 put_occurance(ins->occurance);
11931 ins->occurance = new_occurance(state);
11932 }
11933 else {
11934 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011935 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011936 }
11937 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11938 error(state, 0, "label %s already defined", ident->name);
11939 }
11940 flatten(state, first, ins);
11941
Eric Biedermanb138ac82003-04-22 18:44:01 +000011942 eat(state, TOK_COLON);
11943 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011944}
11945
11946static void switch_statement(struct compile_state *state, struct triple *first)
11947{
Eric Biederman83b991a2003-10-11 06:20:25 +000011948 struct triple *value, *top, *end, *dbranch;
11949 struct hash_entry *ident;
11950
11951 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011952 eat(state, TOK_SWITCH);
11953 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011954 value = expr(state);
11955 integral(state, value);
11956 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011957 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011958 /* Generate the needed pieces */
11959 top = label(state);
11960 end = label(state);
11961 dbranch = branch(state, end, 0);
11962 /* Remember where case branches and break goes */
11963 start_scope(state);
11964 ident = state->i_switch;
11965 symbol(state, ident, &ident->sym_ident, value, value->type);
11966 ident = state->i_case;
11967 symbol(state, ident, &ident->sym_ident, top, top->type);
11968 ident = state->i_break;
11969 symbol(state, ident, &ident->sym_ident, end, end->type);
11970 ident = state->i_default;
11971 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11972 /* Thread them together */
11973 flatten(state, first, value);
11974 flatten(state, first, top);
11975 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011976 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011977 flatten(state, first, end);
11978 /* Cleanup the switch scope */
11979 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011980}
11981
11982static void case_statement(struct compile_state *state, struct triple *first)
11983{
Eric Biederman83b991a2003-10-11 06:20:25 +000011984 struct triple *cvalue, *dest, *test, *jmp;
11985 struct triple *ptr, *value, *top, *dbranch;
11986
11987 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011988 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011989 cvalue = constant_expr(state);
11990 integral(state, cvalue);
11991 if (cvalue->op != OP_INTCONST) {
11992 error(state, 0, "integer constant expected");
11993 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011994 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011995 if (!state->i_case->sym_ident) {
11996 error(state, 0, "case statement not within a switch");
11997 }
11998
11999 /* Lookup the interesting pieces */
12000 top = state->i_case->sym_ident->def;
12001 value = state->i_switch->sym_ident->def;
12002 dbranch = state->i_default->sym_ident->def;
12003
12004 /* See if this case label has already been used */
12005 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
12006 if (ptr->op != OP_EQ) {
12007 continue;
12008 }
12009 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
12010 error(state, 0, "duplicate case %d statement",
12011 cvalue->u.cval);
12012 }
12013 }
12014 /* Generate the needed pieces */
12015 dest = label(state);
12016 test = triple(state, OP_EQ, &int_type, value, cvalue);
12017 jmp = branch(state, dest, test);
12018 /* Thread the pieces together */
12019 flatten(state, dbranch, test);
12020 flatten(state, dbranch, jmp);
12021 flatten(state, dbranch, label(state));
12022 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012023 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012024}
12025
12026static void default_statement(struct compile_state *state, struct triple *first)
12027{
Eric Biederman83b991a2003-10-11 06:20:25 +000012028 struct triple *dest;
12029 struct triple *dbranch, *end;
12030
12031 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012032 eat(state, TOK_DEFAULT);
12033 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012034
12035 if (!state->i_case->sym_ident) {
12036 error(state, 0, "default statement not within a switch");
12037 }
12038
12039 /* Lookup the interesting pieces */
12040 dbranch = state->i_default->sym_ident->def;
12041 end = state->i_break->sym_ident->def;
12042
12043 /* See if a default statement has already happened */
12044 if (TARG(dbranch, 0) != end) {
12045 error(state, 0, "duplicate default statement");
12046 }
12047
12048 /* Generate the needed pieces */
12049 dest = label(state);
12050
Eric Biederman90089602004-05-28 14:11:54 +000012051 /* Blame the branch on the default statement */
12052 put_occurance(dbranch->occurance);
12053 dbranch->occurance = new_occurance(state);
12054
Eric Biederman83b991a2003-10-11 06:20:25 +000012055 /* Thread the pieces together */
12056 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012057 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012058 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012059 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012060}
12061
12062static void asm_statement(struct compile_state *state, struct triple *first)
12063{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012064 struct asm_info *info;
12065 struct {
12066 struct triple *constraint;
12067 struct triple *expr;
12068 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12069 struct triple *def, *asm_str;
12070 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012071 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012072
Eric Biederman90089602004-05-28 14:11:54 +000012073 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012074 eat(state, TOK_ASM);
12075 /* For now ignore the qualifiers */
12076 switch(peek(state)) {
12077 case TOK_CONST:
12078 eat(state, TOK_CONST);
12079 break;
12080 case TOK_VOLATILE:
12081 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012082 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012083 break;
12084 }
12085 eat(state, TOK_LPAREN);
12086 asm_str = string_constant(state);
12087
12088 colons = 0;
12089 out = in = clobbers = 0;
12090 /* Outputs */
12091 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12092 eat(state, TOK_COLON);
12093 colons++;
12094 more = (peek(state) == TOK_LIT_STRING);
12095 while(more) {
12096 struct triple *var;
12097 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012098 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012099 more = 0;
12100 if (out > MAX_LHS) {
12101 error(state, 0, "Maximum output count exceeded.");
12102 }
12103 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012104 str = constraint->u.blob;
12105 if (str[0] != '=') {
12106 error(state, 0, "Output constraint does not start with =");
12107 }
12108 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012109 eat(state, TOK_LPAREN);
12110 var = conditional_expr(state);
12111 eat(state, TOK_RPAREN);
12112
12113 lvalue(state, var);
12114 out_param[out].constraint = constraint;
12115 out_param[out].expr = var;
12116 if (peek(state) == TOK_COMMA) {
12117 eat(state, TOK_COMMA);
12118 more = 1;
12119 }
12120 out++;
12121 }
12122 }
12123 /* Inputs */
12124 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12125 eat(state, TOK_COLON);
12126 colons++;
12127 more = (peek(state) == TOK_LIT_STRING);
12128 while(more) {
12129 struct triple *val;
12130 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012131 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012132 more = 0;
12133 if (in > MAX_RHS) {
12134 error(state, 0, "Maximum input count exceeded.");
12135 }
12136 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012137 str = constraint->u.blob;
12138 if (digitp(str[0] && str[1] == '\0')) {
12139 int val;
12140 val = digval(str[0]);
12141 if ((val < 0) || (val >= out)) {
12142 error(state, 0, "Invalid input constraint %d", val);
12143 }
12144 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012145 eat(state, TOK_LPAREN);
12146 val = conditional_expr(state);
12147 eat(state, TOK_RPAREN);
12148
12149 in_param[in].constraint = constraint;
12150 in_param[in].expr = val;
12151 if (peek(state) == TOK_COMMA) {
12152 eat(state, TOK_COMMA);
12153 more = 1;
12154 }
12155 in++;
12156 }
12157 }
12158
12159 /* Clobber */
12160 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12161 eat(state, TOK_COLON);
12162 colons++;
12163 more = (peek(state) == TOK_LIT_STRING);
12164 while(more) {
12165 struct triple *clobber;
12166 more = 0;
12167 if ((clobbers + out) > MAX_LHS) {
12168 error(state, 0, "Maximum clobber limit exceeded.");
12169 }
12170 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012171
12172 clob_param[clobbers].constraint = clobber;
12173 if (peek(state) == TOK_COMMA) {
12174 eat(state, TOK_COMMA);
12175 more = 1;
12176 }
12177 clobbers++;
12178 }
12179 }
12180 eat(state, TOK_RPAREN);
12181 eat(state, TOK_SEMI);
12182
12183
12184 info = xcmalloc(sizeof(*info), "asm_info");
12185 info->str = asm_str->u.blob;
12186 free_triple(state, asm_str);
12187
12188 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12189 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012190 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012191
12192 /* Find the register constraints */
12193 for(i = 0; i < out; i++) {
12194 struct triple *constraint;
12195 constraint = out_param[i].constraint;
12196 info->tmpl.lhs[i] = arch_reg_constraint(state,
12197 out_param[i].expr->type, constraint->u.blob);
12198 free_triple(state, constraint);
12199 }
12200 for(; i - out < clobbers; i++) {
12201 struct triple *constraint;
12202 constraint = clob_param[i - out].constraint;
12203 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12204 free_triple(state, constraint);
12205 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012206 for(i = 0; i < in; i++) {
12207 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012208 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012209 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012210 str = constraint->u.blob;
12211 if (digitp(str[0]) && str[1] == '\0') {
12212 struct reg_info cinfo;
12213 int val;
12214 val = digval(str[0]);
12215 cinfo.reg = info->tmpl.lhs[val].reg;
12216 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12217 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12218 if (cinfo.reg == REG_UNSET) {
12219 cinfo.reg = REG_VIRT0 + val;
12220 }
12221 if (cinfo.regcm == 0) {
12222 error(state, 0, "No registers for %d", val);
12223 }
12224 info->tmpl.lhs[val] = cinfo;
12225 info->tmpl.rhs[i] = cinfo;
12226
12227 } else {
12228 info->tmpl.rhs[i] = arch_reg_constraint(state,
12229 in_param[i].expr->type, str);
12230 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012231 free_triple(state, constraint);
12232 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012233
12234 /* Now build the helper expressions */
12235 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012236 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012237 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012238 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012239 for(i = 0; i < (out + clobbers); i++) {
12240 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012241 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012242 if (i < out) {
12243 type = out_param[i].expr->type;
12244 } else {
12245 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12246 if (size >= SIZEOF_LONG) {
12247 type = &ulong_type;
12248 }
12249 else if (size >= SIZEOF_INT) {
12250 type = &uint_type;
12251 }
12252 else if (size >= SIZEOF_SHORT) {
12253 type = &ushort_type;
12254 }
12255 else {
12256 type = &uchar_type;
12257 }
12258 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012259 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012260 piece->u.cval = i;
12261 LHS(def, i) = piece;
12262 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012263 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012264 /* And write the helpers to their destinations */
12265 for(i = 0; i < out; i++) {
12266 struct triple *piece;
12267 piece = LHS(def, i);
12268 flatten(state, first,
12269 write_expr(state, out_param[i].expr, piece));
12270 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012271}
12272
12273
12274static int isdecl(int tok)
12275{
12276 switch(tok) {
12277 case TOK_AUTO:
12278 case TOK_REGISTER:
12279 case TOK_STATIC:
12280 case TOK_EXTERN:
12281 case TOK_TYPEDEF:
12282 case TOK_CONST:
12283 case TOK_RESTRICT:
12284 case TOK_VOLATILE:
12285 case TOK_VOID:
12286 case TOK_CHAR:
12287 case TOK_SHORT:
12288 case TOK_INT:
12289 case TOK_LONG:
12290 case TOK_FLOAT:
12291 case TOK_DOUBLE:
12292 case TOK_SIGNED:
12293 case TOK_UNSIGNED:
12294 case TOK_STRUCT:
12295 case TOK_UNION:
12296 case TOK_ENUM:
12297 case TOK_TYPE_NAME: /* typedef name */
12298 return 1;
12299 default:
12300 return 0;
12301 }
12302}
12303
12304static void compound_statement(struct compile_state *state, struct triple *first)
12305{
12306 eat(state, TOK_LBRACE);
12307 start_scope(state);
12308
12309 /* statement-list opt */
12310 while (peek(state) != TOK_RBRACE) {
12311 statement(state, first);
12312 }
12313 end_scope(state);
12314 eat(state, TOK_RBRACE);
12315}
12316
12317static void statement(struct compile_state *state, struct triple *first)
12318{
12319 int tok;
12320 tok = peek(state);
12321 if (tok == TOK_LBRACE) {
12322 compound_statement(state, first);
12323 }
12324 else if (tok == TOK_IF) {
12325 if_statement(state, first);
12326 }
12327 else if (tok == TOK_FOR) {
12328 for_statement(state, first);
12329 }
12330 else if (tok == TOK_WHILE) {
12331 while_statement(state, first);
12332 }
12333 else if (tok == TOK_DO) {
12334 do_statement(state, first);
12335 }
12336 else if (tok == TOK_RETURN) {
12337 return_statement(state, first);
12338 }
12339 else if (tok == TOK_BREAK) {
12340 break_statement(state, first);
12341 }
12342 else if (tok == TOK_CONTINUE) {
12343 continue_statement(state, first);
12344 }
12345 else if (tok == TOK_GOTO) {
12346 goto_statement(state, first);
12347 }
12348 else if (tok == TOK_SWITCH) {
12349 switch_statement(state, first);
12350 }
12351 else if (tok == TOK_ASM) {
12352 asm_statement(state, first);
12353 }
12354 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
12355 labeled_statement(state, first);
12356 }
12357 else if (tok == TOK_CASE) {
12358 case_statement(state, first);
12359 }
12360 else if (tok == TOK_DEFAULT) {
12361 default_statement(state, first);
12362 }
12363 else if (isdecl(tok)) {
12364 /* This handles C99 intermixing of statements and decls */
12365 decl(state, first);
12366 }
12367 else {
12368 expr_statement(state, first);
12369 }
12370}
12371
12372static struct type *param_decl(struct compile_state *state)
12373{
12374 struct type *type;
12375 struct hash_entry *ident;
12376 /* Cheat so the declarator will know we are not global */
12377 start_scope(state);
12378 ident = 0;
12379 type = decl_specifiers(state);
12380 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012381 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012382 end_scope(state);
12383 return type;
12384}
12385
12386static struct type *param_type_list(struct compile_state *state, struct type *type)
12387{
12388 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012389 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012390 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012391 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012392 while(peek(state) == TOK_COMMA) {
12393 eat(state, TOK_COMMA);
12394 if (peek(state) == TOK_DOTS) {
12395 eat(state, TOK_DOTS);
12396 error(state, 0, "variadic functions not supported");
12397 }
12398 else {
12399 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12400 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012401 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012402 }
12403 }
12404 return ftype;
12405}
12406
Eric Biedermanb138ac82003-04-22 18:44:01 +000012407static struct type *type_name(struct compile_state *state)
12408{
12409 struct type *type;
12410 type = specifier_qualifier_list(state);
12411 /* abstract-declarator (may consume no tokens) */
12412 type = declarator(state, type, 0, 0);
12413 return type;
12414}
12415
12416static struct type *direct_declarator(
12417 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012418 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012419{
Eric Biederman41203d92004-11-08 09:31:09 +000012420 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012421 struct type *outer;
12422 int op;
12423 outer = 0;
12424 arrays_complete(state, type);
12425 switch(peek(state)) {
12426 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012427 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012428 if (!ident) {
12429 error(state, 0, "Unexpected identifier found");
12430 }
12431 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012432 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012433 break;
12434 case TOK_LPAREN:
12435 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012436 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012437 eat(state, TOK_RPAREN);
12438 break;
12439 default:
12440 if (need_ident) {
12441 error(state, 0, "Identifier expected");
12442 }
12443 break;
12444 }
12445 do {
12446 op = 1;
12447 arrays_complete(state, type);
12448 switch(peek(state)) {
12449 case TOK_LPAREN:
12450 eat(state, TOK_LPAREN);
12451 type = param_type_list(state, type);
12452 eat(state, TOK_RPAREN);
12453 break;
12454 case TOK_LBRACKET:
12455 {
12456 unsigned int qualifiers;
12457 struct triple *value;
12458 value = 0;
12459 eat(state, TOK_LBRACKET);
12460 if (peek(state) != TOK_RBRACKET) {
12461 value = constant_expr(state);
12462 integral(state, value);
12463 }
12464 eat(state, TOK_RBRACKET);
12465
12466 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12467 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12468 if (value) {
12469 type->elements = value->u.cval;
12470 free_triple(state, value);
12471 } else {
12472 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12473 op = 0;
12474 }
12475 }
12476 break;
12477 default:
12478 op = 0;
12479 break;
12480 }
12481 } while(op);
12482 if (outer) {
12483 struct type *inner;
12484 arrays_complete(state, type);
12485 FINISHME();
12486 for(inner = outer; inner->left; inner = inner->left)
12487 ;
12488 inner->left = type;
12489 type = outer;
12490 }
12491 return type;
12492}
12493
12494static struct type *declarator(
12495 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012496 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012497{
12498 while(peek(state) == TOK_STAR) {
12499 eat(state, TOK_STAR);
12500 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12501 }
Eric Biederman41203d92004-11-08 09:31:09 +000012502 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012503 return type;
12504}
12505
Eric Biedermanb138ac82003-04-22 18:44:01 +000012506static struct type *typedef_name(
12507 struct compile_state *state, unsigned int specifiers)
12508{
12509 struct hash_entry *ident;
12510 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012511 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012512 type = ident->sym_ident->type;
12513 specifiers |= type->type & QUAL_MASK;
12514 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
12515 (type->type & (STOR_MASK | QUAL_MASK))) {
12516 type = clone_type(specifiers, type);
12517 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012518 return type;
12519}
12520
12521static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012522 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012523{
Eric Biederman83b991a2003-10-11 06:20:25 +000012524 struct hash_entry *ident;
12525 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012526 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012527 struct type *enum_type;
12528 enum_type = 0;
12529 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012530 eat(state, TOK_ENUM);
12531 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012532 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012533 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012534 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012535 base = 0;
12536 if (!ident || (peek(state) == TOK_LBRACE)) {
12537 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012538 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012539 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12540 enum_type->type_ident = ident;
12541 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012542 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012543 struct hash_entry *eident;
12544 struct triple *value;
12545 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012546 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012547 if (eident->sym_ident) {
12548 error(state, 0, "%s already declared",
12549 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012550 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012551 eident->tok = TOK_ENUM_CONST;
12552 if (peek(state) == TOK_EQ) {
12553 struct triple *val;
12554 eat(state, TOK_EQ);
12555 val = constant_expr(state);
12556 integral(state, val);
12557 base = val->u.cval;
12558 }
12559 value = int_const(state, &int_type, base);
12560 symbol(state, eident, &eident->sym_ident, value, &int_type);
12561 entry = new_type(TYPE_LIST, 0, 0);
12562 entry->field_ident = eident;
12563 *next = entry;
12564 next = &entry->right;
12565 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012566 if (peek(state) == TOK_COMMA) {
12567 eat(state, TOK_COMMA);
12568 }
12569 } while(peek(state) != TOK_RBRACE);
12570 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012571 if (ident) {
12572 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12573 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012574 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012575 if (ident && ident->sym_tag &&
12576 ident->sym_tag->type &&
12577 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12578 enum_type = clone_type(spec, ident->sym_tag->type);
12579 }
12580 else if (ident && !enum_type) {
12581 error(state, 0, "enum %s undeclared", ident->name);
12582 }
12583 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012584}
12585
Eric Biedermanb138ac82003-04-22 18:44:01 +000012586static struct type *struct_declarator(
12587 struct compile_state *state, struct type *type, struct hash_entry **ident)
12588{
Eric Biederman90089602004-05-28 14:11:54 +000012589 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012590 type = declarator(state, type, ident, 1);
12591 }
Eric Biederman90089602004-05-28 14:11:54 +000012592 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012593 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012594 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012595 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012596 if (value->op != OP_INTCONST) {
12597 error(state, 0, "Invalid constant expression");
12598 }
12599 if (value->u.cval > size_of(state, type)) {
12600 error(state, 0, "bitfield larger than base type");
12601 }
12602 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12603 error(state, 0, "bitfield base not an integer type");
12604 }
12605 type = new_type(TYPE_BITFIELD, type, 0);
12606 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012607 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012608 return type;
12609}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012610
12611static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012612 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012613{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012614 struct type *struct_type;
12615 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012616 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012617 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012618 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012619 struct_type = 0;
12620 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012621 switch(peek(state)) {
12622 case TOK_STRUCT:
12623 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012624 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012625 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012626 break;
12627 case TOK_UNION:
12628 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012629 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012630 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012631 break;
12632 default:
12633 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012634 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012635 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012636 break;
12637 }
12638 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012639 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012640 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012641 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012642 if (!ident || (peek(state) == TOK_LBRACE)) {
12643 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012644 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012645 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012646 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012647 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012648 do {
12649 struct type *base_type;
12650 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012651 base_type = specifier_qualifier_list(state);
12652 do {
12653 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012654 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012655 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012656 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012657 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012658 if (peek(state) == TOK_COMMA) {
12659 done = 0;
12660 eat(state, TOK_COMMA);
12661 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012662 type = clone_type(0, type);
12663 type->field_ident = fident;
12664 if (*next) {
12665 *next = new_type(type_join, *next, type);
12666 next = &((*next)->right);
12667 } else {
12668 *next = type;
12669 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012670 } while(!done);
12671 eat(state, TOK_SEMI);
12672 } while(peek(state) != TOK_RBRACE);
12673 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012674 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012675 struct_type->type_ident = ident;
12676 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012677 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012678 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012679 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012680 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012681 if (ident && ident->sym_tag &&
12682 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012683 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012684 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012685 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012686 else if (ident && !struct_type) {
Eric Biederman90089602004-05-28 14:11:54 +000012687 error(state, 0, "%s %s undeclared",
12688 (type_main == TYPE_STRUCT)?"struct" : "union",
12689 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012690 }
12691 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012692}
12693
12694static unsigned int storage_class_specifier_opt(struct compile_state *state)
12695{
12696 unsigned int specifiers;
12697 switch(peek(state)) {
12698 case TOK_AUTO:
12699 eat(state, TOK_AUTO);
12700 specifiers = STOR_AUTO;
12701 break;
12702 case TOK_REGISTER:
12703 eat(state, TOK_REGISTER);
12704 specifiers = STOR_REGISTER;
12705 break;
12706 case TOK_STATIC:
12707 eat(state, TOK_STATIC);
12708 specifiers = STOR_STATIC;
12709 break;
12710 case TOK_EXTERN:
12711 eat(state, TOK_EXTERN);
12712 specifiers = STOR_EXTERN;
12713 break;
12714 case TOK_TYPEDEF:
12715 eat(state, TOK_TYPEDEF);
12716 specifiers = STOR_TYPEDEF;
12717 break;
12718 default:
12719 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012720 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012721 }
12722 else {
12723 specifiers = STOR_AUTO;
12724 }
12725 }
12726 return specifiers;
12727}
12728
12729static unsigned int function_specifier_opt(struct compile_state *state)
12730{
12731 /* Ignore the inline keyword */
12732 unsigned int specifiers;
12733 specifiers = 0;
12734 switch(peek(state)) {
12735 case TOK_INLINE:
12736 eat(state, TOK_INLINE);
12737 specifiers = STOR_INLINE;
12738 }
12739 return specifiers;
12740}
12741
Eric Biederman90089602004-05-28 14:11:54 +000012742static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12743{
12744 int tok = peek(state);
12745 switch(tok) {
12746 case TOK_COMMA:
12747 case TOK_LPAREN:
12748 /* The empty attribute ignore it */
12749 break;
12750 case TOK_IDENT:
12751 case TOK_ENUM_CONST:
12752 case TOK_TYPE_NAME:
12753 {
12754 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012755 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012756
12757 if (ident == state->i_noinline) {
12758 if (attributes & ATTRIB_ALWAYS_INLINE) {
12759 error(state, 0, "both always_inline and noinline attribtes");
12760 }
12761 attributes |= ATTRIB_NOINLINE;
12762 }
12763 else if (ident == state->i_always_inline) {
12764 if (attributes & ATTRIB_NOINLINE) {
12765 error(state, 0, "both noinline and always_inline attribtes");
12766 }
12767 attributes |= ATTRIB_ALWAYS_INLINE;
12768 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012769 else if (ident == state->i_noreturn) {
12770 // attribute((noreturn)) does nothing (yet?)
12771 }
Eric Biederman90089602004-05-28 14:11:54 +000012772 else {
12773 error(state, 0, "Unknown attribute:%s", ident->name);
12774 }
12775 break;
12776 }
12777 default:
12778 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12779 break;
12780 }
12781 return attributes;
12782}
12783
12784static unsigned int attribute_list(struct compile_state *state, unsigned type)
12785{
12786 type = attrib(state, type);
12787 while(peek(state) == TOK_COMMA) {
12788 eat(state, TOK_COMMA);
12789 type = attrib(state, type);
12790 }
12791 return type;
12792}
12793
12794static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12795{
12796 if (peek(state) == TOK_ATTRIBUTE) {
12797 eat(state, TOK_ATTRIBUTE);
12798 eat(state, TOK_LPAREN);
12799 eat(state, TOK_LPAREN);
12800 type = attribute_list(state, type);
12801 eat(state, TOK_RPAREN);
12802 eat(state, TOK_RPAREN);
12803 }
12804 return type;
12805}
12806
Eric Biedermanb138ac82003-04-22 18:44:01 +000012807static unsigned int type_qualifiers(struct compile_state *state)
12808{
12809 unsigned int specifiers;
12810 int done;
12811 done = 0;
12812 specifiers = QUAL_NONE;
12813 do {
12814 switch(peek(state)) {
12815 case TOK_CONST:
12816 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012817 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012818 break;
12819 case TOK_VOLATILE:
12820 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012821 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012822 break;
12823 case TOK_RESTRICT:
12824 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012825 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012826 break;
12827 default:
12828 done = 1;
12829 break;
12830 }
12831 } while(!done);
12832 return specifiers;
12833}
12834
12835static struct type *type_specifier(
12836 struct compile_state *state, unsigned int spec)
12837{
12838 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012839 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012840 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012841 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012842 case TOK_VOID:
12843 eat(state, TOK_VOID);
12844 type = new_type(TYPE_VOID | spec, 0, 0);
12845 break;
12846 case TOK_CHAR:
12847 eat(state, TOK_CHAR);
12848 type = new_type(TYPE_CHAR | spec, 0, 0);
12849 break;
12850 case TOK_SHORT:
12851 eat(state, TOK_SHORT);
12852 if (peek(state) == TOK_INT) {
12853 eat(state, TOK_INT);
12854 }
12855 type = new_type(TYPE_SHORT | spec, 0, 0);
12856 break;
12857 case TOK_INT:
12858 eat(state, TOK_INT);
12859 type = new_type(TYPE_INT | spec, 0, 0);
12860 break;
12861 case TOK_LONG:
12862 eat(state, TOK_LONG);
12863 switch(peek(state)) {
12864 case TOK_LONG:
12865 eat(state, TOK_LONG);
12866 error(state, 0, "long long not supported");
12867 break;
12868 case TOK_DOUBLE:
12869 eat(state, TOK_DOUBLE);
12870 error(state, 0, "long double not supported");
12871 break;
12872 case TOK_INT:
12873 eat(state, TOK_INT);
12874 type = new_type(TYPE_LONG | spec, 0, 0);
12875 break;
12876 default:
12877 type = new_type(TYPE_LONG | spec, 0, 0);
12878 break;
12879 }
12880 break;
12881 case TOK_FLOAT:
12882 eat(state, TOK_FLOAT);
12883 error(state, 0, "type float not supported");
12884 break;
12885 case TOK_DOUBLE:
12886 eat(state, TOK_DOUBLE);
12887 error(state, 0, "type double not supported");
12888 break;
12889 case TOK_SIGNED:
12890 eat(state, TOK_SIGNED);
12891 switch(peek(state)) {
12892 case TOK_LONG:
12893 eat(state, TOK_LONG);
12894 switch(peek(state)) {
12895 case TOK_LONG:
12896 eat(state, TOK_LONG);
12897 error(state, 0, "type long long not supported");
12898 break;
12899 case TOK_INT:
12900 eat(state, TOK_INT);
12901 type = new_type(TYPE_LONG | spec, 0, 0);
12902 break;
12903 default:
12904 type = new_type(TYPE_LONG | spec, 0, 0);
12905 break;
12906 }
12907 break;
12908 case TOK_INT:
12909 eat(state, TOK_INT);
12910 type = new_type(TYPE_INT | spec, 0, 0);
12911 break;
12912 case TOK_SHORT:
12913 eat(state, TOK_SHORT);
12914 type = new_type(TYPE_SHORT | spec, 0, 0);
12915 break;
12916 case TOK_CHAR:
12917 eat(state, TOK_CHAR);
12918 type = new_type(TYPE_CHAR | spec, 0, 0);
12919 break;
12920 default:
12921 type = new_type(TYPE_INT | spec, 0, 0);
12922 break;
12923 }
12924 break;
12925 case TOK_UNSIGNED:
12926 eat(state, TOK_UNSIGNED);
12927 switch(peek(state)) {
12928 case TOK_LONG:
12929 eat(state, TOK_LONG);
12930 switch(peek(state)) {
12931 case TOK_LONG:
12932 eat(state, TOK_LONG);
12933 error(state, 0, "unsigned long long not supported");
12934 break;
12935 case TOK_INT:
12936 eat(state, TOK_INT);
12937 type = new_type(TYPE_ULONG | spec, 0, 0);
12938 break;
12939 default:
12940 type = new_type(TYPE_ULONG | spec, 0, 0);
12941 break;
12942 }
12943 break;
12944 case TOK_INT:
12945 eat(state, TOK_INT);
12946 type = new_type(TYPE_UINT | spec, 0, 0);
12947 break;
12948 case TOK_SHORT:
12949 eat(state, TOK_SHORT);
12950 type = new_type(TYPE_USHORT | spec, 0, 0);
12951 break;
12952 case TOK_CHAR:
12953 eat(state, TOK_CHAR);
12954 type = new_type(TYPE_UCHAR | spec, 0, 0);
12955 break;
12956 default:
12957 type = new_type(TYPE_UINT | spec, 0, 0);
12958 break;
12959 }
12960 break;
12961 /* struct or union specifier */
12962 case TOK_STRUCT:
12963 case TOK_UNION:
12964 type = struct_or_union_specifier(state, spec);
12965 break;
12966 /* enum-spefifier */
12967 case TOK_ENUM:
12968 type = enum_specifier(state, spec);
12969 break;
12970 /* typedef name */
12971 case TOK_TYPE_NAME:
12972 type = typedef_name(state, spec);
12973 break;
12974 default:
12975 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012976 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012977 break;
12978 }
12979 return type;
12980}
12981
12982static int istype(int tok)
12983{
12984 switch(tok) {
12985 case TOK_CONST:
12986 case TOK_RESTRICT:
12987 case TOK_VOLATILE:
12988 case TOK_VOID:
12989 case TOK_CHAR:
12990 case TOK_SHORT:
12991 case TOK_INT:
12992 case TOK_LONG:
12993 case TOK_FLOAT:
12994 case TOK_DOUBLE:
12995 case TOK_SIGNED:
12996 case TOK_UNSIGNED:
12997 case TOK_STRUCT:
12998 case TOK_UNION:
12999 case TOK_ENUM:
13000 case TOK_TYPE_NAME:
13001 return 1;
13002 default:
13003 return 0;
13004 }
13005}
13006
13007
13008static struct type *specifier_qualifier_list(struct compile_state *state)
13009{
13010 struct type *type;
13011 unsigned int specifiers = 0;
13012
13013 /* type qualifiers */
13014 specifiers |= type_qualifiers(state);
13015
13016 /* type specifier */
13017 type = type_specifier(state, specifiers);
13018
13019 return type;
13020}
13021
Stefan Reinauer50542a82007-10-24 11:14:14 +000013022#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013023static int isdecl_specifier(int tok)
13024{
13025 switch(tok) {
13026 /* storage class specifier */
13027 case TOK_AUTO:
13028 case TOK_REGISTER:
13029 case TOK_STATIC:
13030 case TOK_EXTERN:
13031 case TOK_TYPEDEF:
13032 /* type qualifier */
13033 case TOK_CONST:
13034 case TOK_RESTRICT:
13035 case TOK_VOLATILE:
13036 /* type specifiers */
13037 case TOK_VOID:
13038 case TOK_CHAR:
13039 case TOK_SHORT:
13040 case TOK_INT:
13041 case TOK_LONG:
13042 case TOK_FLOAT:
13043 case TOK_DOUBLE:
13044 case TOK_SIGNED:
13045 case TOK_UNSIGNED:
13046 /* struct or union specifier */
13047 case TOK_STRUCT:
13048 case TOK_UNION:
13049 /* enum-spefifier */
13050 case TOK_ENUM:
13051 /* typedef name */
13052 case TOK_TYPE_NAME:
13053 /* function specifiers */
13054 case TOK_INLINE:
13055 return 1;
13056 default:
13057 return 0;
13058 }
13059}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013060#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013061
13062static struct type *decl_specifiers(struct compile_state *state)
13063{
13064 struct type *type;
13065 unsigned int specifiers;
13066 /* I am overly restrictive in the arragement of specifiers supported.
13067 * C is overly flexible in this department it makes interpreting
13068 * the parse tree difficult.
13069 */
13070 specifiers = 0;
13071
13072 /* storage class specifier */
13073 specifiers |= storage_class_specifier_opt(state);
13074
13075 /* function-specifier */
13076 specifiers |= function_specifier_opt(state);
13077
Eric Biederman90089602004-05-28 14:11:54 +000013078 /* attributes */
13079 specifiers |= attributes_opt(state, 0);
13080
Eric Biedermanb138ac82003-04-22 18:44:01 +000013081 /* type qualifier */
13082 specifiers |= type_qualifiers(state);
13083
13084 /* type specifier */
13085 type = type_specifier(state, specifiers);
13086 return type;
13087}
13088
Eric Biederman00443072003-06-24 12:34:45 +000013089struct field_info {
13090 struct type *type;
13091 size_t offset;
13092};
13093
13094static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013095{
13096 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013097 struct field_info info;
13098 info.offset = ~0U;
13099 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013100 do {
13101 switch(peek(state)) {
13102 case TOK_LBRACKET:
13103 {
13104 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013105 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13106 error(state, 0, "Array designator not in array initializer");
13107 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013108 eat(state, TOK_LBRACKET);
13109 value = constant_expr(state);
13110 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013111
13112 info.type = type->left;
13113 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013114 break;
13115 }
13116 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013117 {
13118 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013119 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13120 ((type->type & TYPE_MASK) != TYPE_UNION))
13121 {
Eric Biederman00443072003-06-24 12:34:45 +000013122 error(state, 0, "Struct designator not in struct initializer");
13123 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013124 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013125 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013126 info.offset = field_offset(state, type, field);
13127 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013128 break;
Eric Biederman00443072003-06-24 12:34:45 +000013129 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013130 default:
13131 error(state, 0, "Invalid designator");
13132 }
13133 tok = peek(state);
13134 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13135 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013136 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013137}
13138
13139static struct triple *initializer(
13140 struct compile_state *state, struct type *type)
13141{
13142 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013143#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013144#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013145#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013146 if (peek(state) != TOK_LBRACE) {
13147 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013148 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13149 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13150 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13151 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13152 (equiv_types(type->left, result->type->left))) {
13153 type->elements = result->type->elements;
13154 }
Eric Biederman90089602004-05-28 14:11:54 +000013155 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013156 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13157 (type->type & TYPE_MASK) != TYPE_ARRAY)
13158 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013159 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013160 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013161 if (!is_init_compatible(state, type, result->type)) {
13162 error(state, 0, "Incompatible types in initializer");
13163 }
13164 if (!equiv_types(type, result->type)) {
13165 result = mk_cast_expr(state, type, result);
13166 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013167 }
13168 else {
13169 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013170 size_t max_offset;
13171 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013172 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013173 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13174 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13175 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013176 }
Eric Biederman00443072003-06-24 12:34:45 +000013177 info.offset = 0;
13178 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013179 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13180 info.type = next_field(state, type, 0);
13181 }
Eric Biederman00443072003-06-24 12:34:45 +000013182 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13183 max_offset = 0;
13184 } else {
13185 max_offset = size_of(state, type);
13186 }
Eric Biederman90089602004-05-28 14:11:54 +000013187 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013188 eat(state, TOK_LBRACE);
13189 do {
13190 struct triple *value;
13191 struct type *value_type;
13192 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013193 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013194 int tok;
13195 comma = 0;
13196 tok = peek(state);
13197 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013198 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013199 }
Eric Biederman00443072003-06-24 12:34:45 +000013200 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13201 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013202 error(state, 0, "element beyond bounds");
13203 }
Eric Biederman00443072003-06-24 12:34:45 +000013204 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013205 value = eval_const_expr(state, initializer(state, value_type));
13206 value_size = size_of(state, value_type);
13207 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013208 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13209 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013210 void *old_buf;
13211 size_t old_size;
13212 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013213 old_size = max_offset;
13214 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013215 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13216 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013217 xfree(old_buf);
13218 }
Eric Biederman90089602004-05-28 14:11:54 +000013219 dest = ((char *)buf) + bits_to_bytes(info.offset);
13220#if DEBUG_INITIALIZER
13221 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
13222 dest - buf,
13223 bits_to_bytes(max_offset),
13224 bits_to_bytes(value_size),
13225 value->op);
13226#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013227 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013228 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013229 }
Eric Biederman90089602004-05-28 14:11:54 +000013230 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13231#if DEBUG_INITIALIZER
13232 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13233#endif
Eric Biederman00443072003-06-24 12:34:45 +000013234 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013235 }
Eric Biederman90089602004-05-28 14:11:54 +000013236 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013237 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013238 }
Eric Biederman90089602004-05-28 14:11:54 +000013239 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013240 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013241 }
13242 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013243 internal_error(state, 0, "unhandled constant initializer");
13244 }
Eric Biederman00443072003-06-24 12:34:45 +000013245 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013246 if (peek(state) == TOK_COMMA) {
13247 eat(state, TOK_COMMA);
13248 comma = 1;
13249 }
Eric Biederman00443072003-06-24 12:34:45 +000013250 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013251 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13252 info.type = next_field(state, type, info.type);
13253 info.offset = field_offset(state, type,
13254 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013255 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013256 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013257 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13258 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13259 type->elements = max_offset / size_of(state, type->left);
13260 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013261 eat(state, TOK_RBRACE);
13262 result = triple(state, OP_BLOBCONST, type, 0, 0);
13263 result->u.blob = buf;
13264 }
13265 return result;
13266}
13267
Eric Biederman90089602004-05-28 14:11:54 +000013268static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013269{
13270 /* Make a second pass and finish anything outstanding
13271 * with respect to branches. The only outstanding item
13272 * is to see if there are goto to labels that have not
13273 * been defined and to error about them.
13274 */
13275 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013276 struct triple *ins;
13277 /* Also error on branches that do not use their targets */
13278 ins = first;
13279 do {
13280 if (!triple_is_ret(state, ins)) {
13281 struct triple **expr ;
13282 struct triple_set *set;
13283 expr = triple_targ(state, ins, 0);
13284 for(; expr; expr = triple_targ(state, ins, expr)) {
13285 struct triple *targ;
13286 targ = *expr;
13287 for(set = targ?targ->use:0; set; set = set->next) {
13288 if (set->member == ins) {
13289 break;
13290 }
13291 }
13292 if (!set) {
13293 internal_error(state, ins, "targ not used");
13294 }
13295 }
13296 }
13297 ins = ins->next;
13298 } while(ins != first);
13299 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013300 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13301 struct hash_entry *entry;
13302 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13303 struct triple *ins;
13304 if (!entry->sym_label) {
13305 continue;
13306 }
13307 ins = entry->sym_label->def;
13308 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13309 error(state, ins, "label `%s' used but not defined",
13310 entry->name);
13311 }
13312 }
13313 }
13314}
13315
Eric Biedermanb138ac82003-04-22 18:44:01 +000013316static struct triple *function_definition(
13317 struct compile_state *state, struct type *type)
13318{
Eric Biederman90089602004-05-28 14:11:54 +000013319 struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13320 struct triple *fname;
13321 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013322 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013323 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013324 int i;
13325 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13326 error(state, 0, "Invalid function header");
13327 }
13328
13329 /* Verify the function type */
13330 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13331 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013332 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013333 error(state, 0, "Invalid function parameters");
13334 }
13335 param = type->right;
13336 i = 0;
13337 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13338 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013339 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013340 error(state, 0, "No identifier for parameter %d\n", i);
13341 }
13342 param = param->right;
13343 }
13344 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013345 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013346 error(state, 0, "No identifier for paramter %d\n", i);
13347 }
13348
13349 /* Get a list of statements for this function. */
13350 def = triple(state, OP_LIST, type, 0, 0);
13351
13352 /* Start a new scope for the passed parameters */
13353 start_scope(state);
13354
13355 /* Put a label at the very start of a function */
13356 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013357 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013358
13359 /* Put a label at the very end of a function */
13360 end = label(state);
13361 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013362 /* Remember where return goes */
13363 ident = state->i_return;
13364 symbol(state, ident, &ident->sym_ident, end, end->type);
13365
Eric Biederman90089602004-05-28 14:11:54 +000013366 /* Get the initial closure type */
13367 ctype = new_type(TYPE_JOIN, &void_type, 0);
13368 ctype->elements = 1;
13369
13370 /* Add a variable for the return value */
13371 crtype = new_type(TYPE_TUPLE,
13372 /* Remove all type qualifiers from the return type */
13373 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13374 crtype->elements = 2;
13375 result = flatten(state, end, variable(state, crtype));
13376
Eric Biederman5ade04a2003-10-22 04:03:46 +000013377 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013378 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013379
13380 /* Add in the return instruction */
13381 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13382 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013383
13384 /* Walk through the parameters and create symbol table entries
13385 * for them.
13386 */
13387 param = type->right;
13388 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013389 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013390 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013391 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013392 flatten(state, end, tmp);
13393 param = param->right;
13394 }
13395 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13396 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013397 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013398 tmp = variable(state, param);
13399 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13400 flatten(state, end, tmp);
13401 }
Eric Biederman90089602004-05-28 14:11:54 +000013402
13403 /* Add the declaration static const char __func__ [] = "func-name" */
13404 fname_type = new_type(TYPE_ARRAY,
13405 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13406 fname_type->type |= QUAL_CONST | STOR_STATIC;
13407 fname_type->elements = strlen(state->function) + 1;
13408
13409 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13410 fname->u.blob = (void *)state->function;
13411 fname = flatten(state, end, fname);
13412
13413 ident = state->i___func__;
13414 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013415
13416 /* Remember which function I am compiling.
13417 * Also assume the last defined function is the main function.
13418 */
13419 state->main_function = def;
13420
13421 /* Now get the actual function definition */
13422 compound_statement(state, end);
13423
Eric Biederman153ea352003-06-20 14:43:20 +000013424 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013425 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013426
Eric Biedermanb138ac82003-04-22 18:44:01 +000013427 /* Remove the parameter scope */
13428 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013429
Eric Biederman5ade04a2003-10-22 04:03:46 +000013430
13431 /* Remember I have defined a function */
13432 if (!state->functions) {
13433 state->functions = def;
13434 } else {
13435 insert_triple(state, state->functions, def);
13436 }
13437 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013438 FILE *fp = state->dbgout;
13439 fprintf(fp, "\n");
13440 loc(fp, state, 0);
13441 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13442 display_func(state, fp, def);
13443 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013444 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013445
13446 return def;
13447}
13448
13449static struct triple *do_decl(struct compile_state *state,
13450 struct type *type, struct hash_entry *ident)
13451{
13452 struct triple *def;
13453 def = 0;
13454 /* Clean up the storage types used */
13455 switch (type->type & STOR_MASK) {
13456 case STOR_AUTO:
13457 case STOR_STATIC:
13458 /* These are the good types I am aiming for */
13459 break;
13460 case STOR_REGISTER:
13461 type->type &= ~STOR_MASK;
13462 type->type |= STOR_AUTO;
13463 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013464 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013465 case STOR_EXTERN:
13466 type->type &= ~STOR_MASK;
13467 type->type |= STOR_STATIC;
13468 break;
13469 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013470 if (!ident) {
13471 error(state, 0, "typedef without name");
13472 }
13473 symbol(state, ident, &ident->sym_ident, 0, type);
13474 ident->tok = TOK_TYPE_NAME;
13475 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013476 break;
13477 default:
13478 internal_error(state, 0, "Undefined storage class");
13479 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013480 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13481 error(state, 0, "Function prototypes not supported");
13482 }
Eric Biederman00443072003-06-24 12:34:45 +000013483 if (ident &&
13484 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013485 ((type->type & QUAL_CONST) == 0)) {
13486 error(state, 0, "non const static variables not supported");
13487 }
13488 if (ident) {
13489 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013490 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013491 }
13492 return def;
13493}
13494
13495static void decl(struct compile_state *state, struct triple *first)
13496{
13497 struct type *base_type, *type;
13498 struct hash_entry *ident;
13499 struct triple *def;
13500 int global;
13501 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13502 base_type = decl_specifiers(state);
13503 ident = 0;
13504 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013505 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013506 if (global && ident && (peek(state) == TOK_LBRACE)) {
13507 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013508 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013509 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013510 def = function_definition(state, type);
13511 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013512 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013513 }
13514 else {
13515 int done;
13516 flatten(state, first, do_decl(state, type, ident));
13517 /* type or variable definition */
13518 do {
13519 done = 1;
13520 if (peek(state) == TOK_EQ) {
13521 if (!ident) {
13522 error(state, 0, "cannot assign to a type");
13523 }
13524 eat(state, TOK_EQ);
13525 flatten(state, first,
13526 init_expr(state,
13527 ident->sym_ident->def,
13528 initializer(state, type)));
13529 }
13530 arrays_complete(state, type);
13531 if (peek(state) == TOK_COMMA) {
13532 eat(state, TOK_COMMA);
13533 ident = 0;
13534 type = declarator(state, base_type, &ident, 0);
13535 flatten(state, first, do_decl(state, type, ident));
13536 done = 0;
13537 }
13538 } while(!done);
13539 eat(state, TOK_SEMI);
13540 }
13541}
13542
13543static void decls(struct compile_state *state)
13544{
13545 struct triple *list;
13546 int tok;
13547 list = label(state);
13548 while(1) {
13549 tok = peek(state);
13550 if (tok == TOK_EOF) {
13551 return;
13552 }
13553 if (tok == TOK_SPACE) {
13554 eat(state, TOK_SPACE);
13555 }
13556 decl(state, list);
13557 if (list->next != list) {
13558 error(state, 0, "global variables not supported");
13559 }
13560 }
13561}
13562
Eric Biederman5ade04a2003-10-22 04:03:46 +000013563/*
13564 * Function inlining
13565 */
Eric Biederman90089602004-05-28 14:11:54 +000013566struct triple_reg_set {
13567 struct triple_reg_set *next;
13568 struct triple *member;
13569 struct triple *new;
13570};
13571struct reg_block {
13572 struct block *block;
13573 struct triple_reg_set *in;
13574 struct triple_reg_set *out;
13575 int vertex;
13576};
13577static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13578static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13579static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13580static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13581static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13582 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13583 void *arg);
13584static void print_block(
13585 struct compile_state *state, struct block *block, void *arg);
13586static int do_triple_set(struct triple_reg_set **head,
13587 struct triple *member, struct triple *new_member);
13588static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13589static struct reg_block *compute_variable_lifetimes(
13590 struct compile_state *state, struct basic_blocks *bb);
13591static void free_variable_lifetimes(struct compile_state *state,
13592 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013593#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000013594static void print_live_variables(struct compile_state *state,
13595 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013596#endif
Eric Biederman90089602004-05-28 14:11:54 +000013597
Eric Biederman5ade04a2003-10-22 04:03:46 +000013598
13599static struct triple *call(struct compile_state *state,
13600 struct triple *retvar, struct triple *ret_addr,
13601 struct triple *targ, struct triple *ret)
13602{
13603 struct triple *call;
13604
13605 if (!retvar || !is_lvalue(state, retvar)) {
13606 internal_error(state, 0, "writing to a non lvalue?");
13607 }
13608 write_compatible(state, retvar->type, &void_ptr_type);
13609
13610 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13611 TARG(call, 0) = targ;
13612 MISC(call, 0) = ret;
13613 if (!targ || (targ->op != OP_LABEL)) {
13614 internal_error(state, 0, "call not to a label");
13615 }
13616 if (!ret || (ret->op != OP_RET)) {
13617 internal_error(state, 0, "call not matched with return");
13618 }
13619 return call;
13620}
13621
Eric Biederman5ade04a2003-10-22 04:03:46 +000013622static void walk_functions(struct compile_state *state,
13623 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13624 void *arg)
13625{
13626 struct triple *func, *first;
13627 func = first = state->functions;
13628 do {
13629 cb(state, func, arg);
13630 func = func->next;
13631 } while(func != first);
13632}
13633
Eric Biederman90089602004-05-28 14:11:54 +000013634static void reverse_walk_functions(struct compile_state *state,
13635 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13636 void *arg)
13637{
13638 struct triple *func, *first;
13639 func = first = state->functions;
13640 do {
13641 func = func->prev;
13642 cb(state, func, arg);
13643 } while(func != first);
13644}
13645
13646
13647static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13648{
13649 struct triple *ptr, *first;
13650 if (func->u.cval == 0) {
13651 return;
13652 }
13653 ptr = first = RHS(func, 0);
13654 do {
13655 if (ptr->op == OP_FCALL) {
13656 struct triple *called_func;
13657 called_func = MISC(ptr, 0);
13658 /* Mark the called function as used */
13659 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13660 called_func->u.cval++;
13661 }
13662 /* Remove the called function from the list */
13663 called_func->prev->next = called_func->next;
13664 called_func->next->prev = called_func->prev;
13665
13666 /* Place the called function before me on the list */
13667 called_func->next = func;
13668 called_func->prev = func->prev;
13669 called_func->prev->next = called_func;
13670 called_func->next->prev = called_func;
13671 }
13672 ptr = ptr->next;
13673 } while(ptr != first);
13674 func->id |= TRIPLE_FLAG_FLATTENED;
13675}
13676
13677static void mark_live_functions(struct compile_state *state)
13678{
13679 /* Ensure state->main_function is the last function in
13680 * the list of functions.
13681 */
13682 if ((state->main_function->next != state->functions) ||
13683 (state->functions->prev != state->main_function)) {
13684 internal_error(state, 0,
13685 "state->main_function is not at the end of the function list ");
13686 }
13687 state->main_function->u.cval = 1;
13688 reverse_walk_functions(state, mark_live, 0);
13689}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013690
13691static int local_triple(struct compile_state *state,
13692 struct triple *func, struct triple *ins)
13693{
13694 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13695#if 0
13696 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013697 FILE *fp = state->errout;
13698 fprintf(fp, "global: ");
13699 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013700 }
13701#endif
13702 return local;
13703}
13704
13705struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
13706 struct occurance *base_occurance)
13707{
13708 struct triple *nfunc;
13709 struct triple *nfirst, *ofirst;
13710 struct triple *new, *old;
13711
13712 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013713 FILE *fp = state->dbgout;
13714 fprintf(fp, "\n");
13715 loc(fp, state, 0);
13716 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13717 display_func(state, fp, ofunc);
13718 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013719 }
13720
13721 /* Make a new copy of the old function */
13722 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13723 nfirst = 0;
13724 ofirst = old = RHS(ofunc, 0);
13725 do {
13726 struct triple *new;
13727 struct occurance *occurance;
13728 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013729 old_lhs = old->lhs;
13730 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013731 occurance = inline_occurance(state, base_occurance, old->occurance);
13732 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13733 MISC(old, 0)->u.cval += 1;
13734 }
13735 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13736 occurance);
13737 if (!triple_stores_block(state, new)) {
13738 memcpy(&new->u, &old->u, sizeof(new->u));
13739 }
13740 if (!nfirst) {
13741 RHS(nfunc, 0) = nfirst = new;
13742 }
13743 else {
13744 insert_triple(state, nfirst, new);
13745 }
13746 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013747 new->id |= old->id & TRIPLE_FLAG_COPY;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013748
13749 /* During the copy remember new as user of old */
13750 use_triple(old, new);
13751
Eric Biederman5ade04a2003-10-22 04:03:46 +000013752 /* Remember which instructions are local */
13753 old->id |= TRIPLE_FLAG_LOCAL;
13754 old = old->next;
13755 } while(old != ofirst);
13756
13757 /* Make a second pass to fix up any unresolved references */
13758 old = ofirst;
13759 new = nfirst;
13760 do {
13761 struct triple **oexpr, **nexpr;
13762 int count, i;
13763 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013764 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013765 for(i = 0; i < count; i++) {
13766 oexpr = &old->param[i];
13767 nexpr = &new->param[i];
13768 if (*oexpr && !*nexpr) {
13769 if (!local_triple(state, ofunc, *oexpr)) {
13770 *nexpr = *oexpr;
13771 }
13772 else if ((*oexpr)->use) {
13773 *nexpr = (*oexpr)->use->member;
13774 }
13775 if (*nexpr == old) {
13776 internal_error(state, 0, "new == old?");
13777 }
13778 use_triple(*nexpr, new);
13779 }
13780 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013781 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013782 }
13783 }
13784 old = old->next;
13785 new = new->next;
13786 } while((old != ofirst) && (new != nfirst));
13787
13788 /* Make a third pass to cleanup the extra useses */
13789 old = ofirst;
13790 new = nfirst;
13791 do {
13792 unuse_triple(old, new);
13793 /* Forget which instructions are local */
13794 old->id &= ~TRIPLE_FLAG_LOCAL;
13795 old = old->next;
13796 new = new->next;
13797 } while ((old != ofirst) && (new != nfirst));
13798 return nfunc;
13799}
13800
Eric Biederman90089602004-05-28 14:11:54 +000013801static void expand_inline_call(
13802 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013803{
13804 /* Inline the function call */
13805 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013806 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013807 struct triple *end, *nend;
13808 int pvals, i;
13809
13810 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013811 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013812 if (ofunc->op != OP_LIST) {
13813 internal_error(state, 0, "improper function");
13814 }
Eric Biederman90089602004-05-28 14:11:54 +000013815 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013816 /* Prepend the parameter reading into the new function list */
13817 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013818 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013819 for(i = 0; i < pvals; i++) {
13820 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013821 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013822 atype = ptype;
13823 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13824 atype = ptype->left;
13825 }
Eric Biederman90089602004-05-28 14:11:54 +000013826 param = farg(state, nfunc, i);
13827 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13828 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013829 }
Eric Biederman90089602004-05-28 14:11:54 +000013830 arg = RHS(fcall, i);
13831 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013832 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013833 }
13834 result = 0;
13835 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman90089602004-05-28 14:11:54 +000013836 result = read_expr(state,
13837 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013838 }
13839 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013840 FILE *fp = state->dbgout;
13841 fprintf(fp, "\n");
13842 loc(fp, state, 0);
13843 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13844 display_func(state, fp, nfunc);
13845 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013846 }
13847
Eric Biederman90089602004-05-28 14:11:54 +000013848 /*
13849 * Get rid of the extra triples
13850 */
13851 /* Remove the read of the return address */
13852 ins = RHS(nfunc, 0)->prev->prev;
13853 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13854 internal_error(state, ins, "Not return addres read?");
13855 }
13856 release_triple(state, ins);
13857 /* Remove the return instruction */
13858 ins = RHS(nfunc, 0)->prev;
13859 if (ins->op != OP_RET) {
13860 internal_error(state, ins, "Not return?");
13861 }
13862 release_triple(state, ins);
13863 /* Remove the retaddres variable */
13864 retvar = fretaddr(state, nfunc);
13865 if ((retvar->lhs != 1) ||
13866 (retvar->op != OP_ADECL) ||
13867 (retvar->next->op != OP_PIECE) ||
13868 (MISC(retvar->next, 0) != retvar)) {
13869 internal_error(state, retvar, "Not the return address?");
13870 }
13871 release_triple(state, retvar->next);
13872 release_triple(state, retvar);
13873
13874 /* Remove the label at the start of the function */
13875 ins = RHS(nfunc, 0);
13876 if (ins->op != OP_LABEL) {
13877 internal_error(state, ins, "Not label?");
13878 }
13879 nfirst = ins->next;
13880 free_triple(state, ins);
13881 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013882 RHS(nfunc, 0) = 0;
13883 free_triple(state, nfunc);
13884
13885 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013886 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013887 nend = nfirst->prev;
13888 end->next = nfirst;
13889 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013890 nend->next = fcall;
13891 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013892
Eric Biederman90089602004-05-28 14:11:54 +000013893 /* Now the result reading code */
13894 if (result) {
13895 result = flatten(state, fcall, result);
13896 propogate_use(state, fcall, result);
13897 }
13898
13899 /* Release the original fcall instruction */
13900 release_triple(state, fcall);
13901
13902 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013903}
13904
Eric Biederman90089602004-05-28 14:11:54 +000013905/*
13906 *
13907 * Type of the result variable.
13908 *
13909 * result
13910 * |
13911 * +----------+------------+
13912 * | |
13913 * union of closures result_type
13914 * |
13915 * +------------------+---------------+
13916 * | |
13917 * closure1 ... closuerN
13918 * | |
13919 * +----+--+-+--------+-----+ +----+----+---+-----+
13920 * | | | | | | | | |
13921 * var1 var2 var3 ... varN result var1 var2 ... varN result
13922 * |
13923 * +--------+---------+
13924 * | |
13925 * union of closures result_type
13926 * |
13927 * +-----+-------------------+
13928 * | |
13929 * closure1 ... closureN
13930 * | |
13931 * +-----+---+----+----+ +----+---+----+-----+
13932 * | | | | | | | |
13933 * var1 var2 ... varN result var1 var2 ... varN result
13934 */
13935
13936static int add_closure_type(struct compile_state *state,
13937 struct triple *func, struct type *closure_type)
13938{
13939 struct type *type, *ctype, **next;
13940 struct triple *var, *new_var;
13941 int i;
13942
13943#if 0
13944 FILE *fp = state->errout;
13945 fprintf(fp, "original_type: ");
13946 name_of(fp, fresult(state, func)->type);
13947 fprintf(fp, "\n");
13948#endif
13949 /* find the original type */
13950 var = fresult(state, func);
13951 type = var->type;
13952 if (type->elements != 2) {
13953 internal_error(state, var, "bad return type");
13954 }
13955
13956 /* Find the complete closure type and update it */
13957 ctype = type->left->left;
13958 next = &ctype->left;
13959 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13960 next = &(*next)->right;
13961 }
13962 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13963 ctype->elements += 1;
13964
13965#if 0
13966 fprintf(fp, "new_type: ");
13967 name_of(fp, type);
13968 fprintf(fp, "\n");
13969 fprintf(fp, "ctype: %p %d bits: %d ",
13970 ctype, ctype->elements, reg_size_of(state, ctype));
13971 name_of(fp, ctype);
13972 fprintf(fp, "\n");
13973#endif
13974
13975 /* Regenerate the variable with the new type definition */
13976 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13977 new_var->id |= TRIPLE_FLAG_FLATTENED;
13978 for(i = 0; i < new_var->lhs; i++) {
13979 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13980 }
13981
13982 /* Point everyone at the new variable */
13983 propogate_use(state, var, new_var);
13984
13985 /* Release the original variable */
13986 for(i = 0; i < var->lhs; i++) {
13987 release_triple(state, LHS(var, i));
13988 }
13989 release_triple(state, var);
13990
13991 /* Return the index of the added closure type */
13992 return ctype->elements - 1;
13993}
13994
13995static struct triple *closure_expr(struct compile_state *state,
13996 struct triple *func, int closure_idx, int var_idx)
13997{
13998 return deref_index(state,
13999 deref_index(state,
14000 deref_index(state, fresult(state, func), 0),
14001 closure_idx),
14002 var_idx);
14003}
14004
14005
14006static void insert_triple_set(
14007 struct triple_reg_set **head, struct triple *member)
14008{
14009 struct triple_reg_set *new;
14010 new = xcmalloc(sizeof(*new), "triple_set");
14011 new->member = member;
14012 new->new = 0;
14013 new->next = *head;
14014 *head = new;
14015}
14016
14017static int ordered_triple_set(
14018 struct triple_reg_set **head, struct triple *member)
14019{
14020 struct triple_reg_set **ptr;
14021 if (!member)
14022 return 0;
14023 ptr = head;
14024 while(*ptr) {
14025 if (member == (*ptr)->member) {
14026 return 0;
14027 }
14028 /* keep the list ordered */
14029 if (member->id < (*ptr)->member->id) {
14030 break;
14031 }
14032 ptr = &(*ptr)->next;
14033 }
14034 insert_triple_set(ptr, member);
14035 return 1;
14036}
14037
14038
14039static void free_closure_variables(struct compile_state *state,
14040 struct triple_reg_set **enclose)
14041{
14042 struct triple_reg_set *entry, *next;
14043 for(entry = *enclose; entry; entry = next) {
14044 next = entry->next;
14045 do_triple_unset(enclose, entry->member);
14046 }
14047}
14048
14049static int lookup_closure_index(struct compile_state *state,
14050 struct triple *me, struct triple *val)
14051{
14052 struct triple *first, *ins, *next;
14053 first = RHS(me, 0);
14054 ins = next = first;
14055 do {
14056 struct triple *result;
14057 struct triple *index0, *index1, *index2, *read, *write;
14058 ins = next;
14059 next = ins->next;
14060 if (ins->op != OP_CALL) {
14061 continue;
14062 }
14063 /* I am at a previous call point examine it closely */
14064 if (ins->next->op != OP_LABEL) {
14065 internal_error(state, ins, "call not followed by label");
14066 }
14067 /* Does this call does not enclose any variables? */
14068 if ((ins->next->next->op != OP_INDEX) ||
14069 (ins->next->next->u.cval != 0) ||
14070 (result = MISC(ins->next->next, 0)) ||
14071 (result->id & TRIPLE_FLAG_LOCAL)) {
14072 continue;
14073 }
14074 index0 = ins->next->next;
14075 /* The pattern is:
14076 * 0 index result < 0 >
14077 * 1 index 0 < ? >
14078 * 2 index 1 < ? >
14079 * 3 read 2
14080 * 4 write 3 var
14081 */
14082 for(index0 = ins->next->next;
14083 (index0->op == OP_INDEX) &&
14084 (MISC(index0, 0) == result) &&
14085 (index0->u.cval == 0) ;
14086 index0 = write->next)
14087 {
14088 index1 = index0->next;
14089 index2 = index1->next;
14090 read = index2->next;
14091 write = read->next;
14092 if ((index0->op != OP_INDEX) ||
14093 (index1->op != OP_INDEX) ||
14094 (index2->op != OP_INDEX) ||
14095 (read->op != OP_READ) ||
14096 (write->op != OP_WRITE) ||
14097 (MISC(index1, 0) != index0) ||
14098 (MISC(index2, 0) != index1) ||
14099 (RHS(read, 0) != index2) ||
14100 (RHS(write, 0) != read)) {
14101 internal_error(state, index0, "bad var read");
14102 }
14103 if (MISC(write, 0) == val) {
14104 return index2->u.cval;
14105 }
14106 }
14107 } while(next != first);
14108 return -1;
14109}
14110
14111static inline int enclose_triple(struct triple *ins)
14112{
14113 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14114}
14115
14116static void compute_closure_variables(struct compile_state *state,
14117 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14118{
14119 struct triple_reg_set *set, *vars, **last_var;
14120 struct basic_blocks bb;
14121 struct reg_block *rb;
14122 struct block *block;
14123 struct triple *old_result, *first, *ins;
14124 size_t count, idx;
14125 unsigned long used_indicies;
14126 int i, max_index;
14127#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14128#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
14129 struct {
14130 unsigned id;
14131 int index;
14132 } *info;
14133
14134
14135 /* Find the basic blocks of this function */
14136 bb.func = me;
14137 bb.first = RHS(me, 0);
14138 old_result = 0;
14139 if (!triple_is_ret(state, bb.first->prev)) {
14140 bb.func = 0;
14141 } else {
14142 old_result = fresult(state, me);
14143 }
14144 analyze_basic_blocks(state, &bb);
14145
14146 /* Find which variables are currently alive in a given block */
14147 rb = compute_variable_lifetimes(state, &bb);
14148
14149 /* Find the variables that are currently alive */
14150 block = block_of_triple(state, fcall);
14151 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14152 internal_error(state, fcall, "No reg block? block: %p", block);
14153 }
14154
14155#if DEBUG_EXPLICIT_CLOSURES
14156 print_live_variables(state, &bb, rb, state->dbgout);
14157 fflush(state->dbgout);
14158#endif
14159
14160 /* Count the number of triples in the function */
14161 first = RHS(me, 0);
14162 ins = first;
14163 count = 0;
14164 do {
14165 count++;
14166 ins = ins->next;
14167 } while(ins != first);
14168
14169 /* Allocate some memory to temorary hold the id info */
14170 info = xcmalloc(sizeof(*info) * (count +1), "info");
14171
14172 /* Mark the local function */
14173 first = RHS(me, 0);
14174 ins = first;
14175 idx = 1;
14176 do {
14177 info[idx].id = ins->id;
14178 ins->id = TRIPLE_FLAG_LOCAL | idx;
14179 idx++;
14180 ins = ins->next;
14181 } while(ins != first);
14182
14183 /*
14184 * Build the list of variables to enclose.
14185 *
14186 * A target it to put the same variable in the
14187 * same slot for ever call of a given function.
14188 * After coloring this removes all of the variable
14189 * manipulation code.
14190 *
14191 * The list of variables to enclose is built ordered
14192 * program order because except in corner cases this
14193 * gives me the stability of assignment I need.
14194 *
14195 * To gurantee that stability I lookup the variables
14196 * to see where they have been used before and
14197 * I build my final list with the assigned indicies.
14198 */
14199 vars = 0;
14200 if (enclose_triple(old_result)) {
14201 ordered_triple_set(&vars, old_result);
14202 }
14203 for(set = rb[block->vertex].out; set; set = set->next) {
14204 if (!enclose_triple(set->member)) {
14205 continue;
14206 }
14207 if ((set->member == fcall) || (set->member == old_result)) {
14208 continue;
14209 }
14210 if (!local_triple(state, me, set->member)) {
14211 internal_error(state, set->member, "not local?");
14212 }
14213 ordered_triple_set(&vars, set->member);
14214 }
14215
14216 /* Lookup the current indicies of the live varialbe */
14217 used_indicies = 0;
14218 max_index = -1;
14219 for(set = vars; set ; set = set->next) {
14220 struct triple *ins;
14221 int index;
14222 ins = set->member;
14223 index = lookup_closure_index(state, me, ins);
14224 info[ID_BITS(ins->id)].index = index;
14225 if (index < 0) {
14226 continue;
14227 }
14228 if (index >= MAX_INDICIES) {
14229 internal_error(state, ins, "index unexpectedly large");
14230 }
14231 if (used_indicies & (1 << index)) {
14232 internal_error(state, ins, "index previously used?");
14233 }
14234 /* Remember which indicies have been used */
14235 used_indicies |= (1 << index);
14236 if (index > max_index) {
14237 max_index = index;
14238 }
14239 }
14240
14241 /* Walk through the live variables and make certain
14242 * everything is assigned an index.
14243 */
14244 for(set = vars; set; set = set->next) {
14245 struct triple *ins;
14246 int index;
14247 ins = set->member;
14248 index = info[ID_BITS(ins->id)].index;
14249 if (index >= 0) {
14250 continue;
14251 }
14252 /* Find the lowest unused index value */
14253 for(index = 0; index < MAX_INDICIES; index++) {
14254 if (!(used_indicies & (1 << index))) {
14255 break;
14256 }
14257 }
14258 if (index == MAX_INDICIES) {
14259 internal_error(state, ins, "no free indicies?");
14260 }
14261 info[ID_BITS(ins->id)].index = index;
14262 /* Remember which indicies have been used */
14263 used_indicies |= (1 << index);
14264 if (index > max_index) {
14265 max_index = index;
14266 }
14267 }
14268
14269 /* Build the return list of variables with positions matching
14270 * their indicies.
14271 */
14272 *enclose = 0;
14273 last_var = enclose;
14274 for(i = 0; i <= max_index; i++) {
14275 struct triple *var;
14276 var = 0;
14277 if (used_indicies & (1 << i)) {
14278 for(set = vars; set; set = set->next) {
14279 int index;
14280 index = info[ID_BITS(set->member->id)].index;
14281 if (index == i) {
14282 var = set->member;
14283 break;
14284 }
14285 }
14286 if (!var) {
14287 internal_error(state, me, "missing variable");
14288 }
14289 }
14290 insert_triple_set(last_var, var);
14291 last_var = &(*last_var)->next;
14292 }
14293
14294#if DEBUG_EXPLICIT_CLOSURES
14295 /* Print out the variables to be enclosed */
14296 loc(state->dbgout, state, fcall);
14297 fprintf(state->dbgout, "Alive: \n");
14298 for(set = *enclose; set; set = set->next) {
14299 display_triple(state->dbgout, set->member);
14300 }
14301 fflush(state->dbgout);
14302#endif
14303
14304 /* Clear the marks */
14305 ins = first;
14306 do {
14307 ins->id = info[ID_BITS(ins->id)].id;
14308 ins = ins->next;
14309 } while(ins != first);
14310
14311 /* Release the ordered list of live variables */
14312 free_closure_variables(state, &vars);
14313
14314 /* Release the storage of the old ids */
14315 xfree(info);
14316
14317 /* Release the variable lifetime information */
14318 free_variable_lifetimes(state, &bb, rb);
14319
14320 /* Release the basic blocks of this function */
14321 free_basic_blocks(state, &bb);
14322}
14323
14324static void expand_function_call(
14325 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014326{
14327 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014328 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014329 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014330 struct triple *first;
14331 struct type *ptype, *rtype;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014332 struct triple *jmp;
14333 struct triple *ret_addr, *ret_loc, *ret_set;
Eric Biederman90089602004-05-28 14:11:54 +000014334 struct triple_reg_set *enclose, *set;
14335 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014336
Eric Biederman90089602004-05-28 14:11:54 +000014337#if DEBUG_EXPLICIT_CLOSURES
14338 FILE *fp = state->dbgout;
14339 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14340 display_func(state, fp, MISC(fcall, 0));
14341 display_func(state, fp, me);
14342 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14343#endif
14344
Eric Biederman5ade04a2003-10-22 04:03:46 +000014345 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014346 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014347 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014348 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014349 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014350 first = fcall->next;
14351
14352 /* Find what I need to enclose */
14353 compute_closure_variables(state, me, fcall, &enclose);
14354
14355 /* Compute the closure type */
14356 closure_type = new_type(TYPE_TUPLE, 0, 0);
14357 closure_type->elements = 0;
14358 closure_next = &closure_type->left;
14359 for(set = enclose; set ; set = set->next) {
14360 struct type *type;
14361 type = &void_type;
14362 if (set->member) {
14363 type = set->member->type;
14364 }
14365 if (!*closure_next) {
14366 *closure_next = type;
14367 } else {
14368 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
14369 type);
14370 closure_next = &(*closure_next)->right;
14371 }
14372 closure_type->elements += 1;
14373 }
14374 if (closure_type->elements == 0) {
14375 closure_type->type = TYPE_VOID;
14376 }
14377
14378
14379#if DEBUG_EXPLICIT_CLOSURES
14380 fprintf(state->dbgout, "closure type: ");
14381 name_of(state->dbgout, closure_type);
14382 fprintf(state->dbgout, "\n");
14383#endif
14384
14385 /* Update the called functions closure variable */
14386 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014387
14388 /* Generate some needed triples */
14389 ret_loc = label(state);
14390 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14391
14392 /* Pass the parameters to the new function */
14393 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014394 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014395 for(i = 0; i < pvals; i++) {
14396 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014397 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014398 atype = ptype;
14399 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14400 atype = ptype->left;
14401 }
Eric Biederman90089602004-05-28 14:11:54 +000014402 param = farg(state, func, i);
14403 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14404 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014405 }
Eric Biederman90089602004-05-28 14:11:54 +000014406 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014407 flatten(state, first, write_expr(state, param, arg));
14408 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014409 }
Eric Biederman90089602004-05-28 14:11:54 +000014410 rtype = func->type->left;
14411
Eric Biederman5ade04a2003-10-22 04:03:46 +000014412 /* Thread the triples together */
14413 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014414
14415 /* Save the active variables in the result variable */
14416 for(i = 0, set = enclose; set ; set = set->next, i++) {
14417 if (!set->member) {
14418 continue;
14419 }
14420 flatten(state, ret_loc,
14421 write_expr(state,
14422 closure_expr(state, func, closure_idx, i),
14423 read_expr(state, set->member)));
14424 }
14425
14426 /* Initialize the return value */
14427 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14428 flatten(state, ret_loc,
14429 write_expr(state,
14430 deref_index(state, fresult(state, func), 1),
14431 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14432 }
14433
Eric Biederman5ade04a2003-10-22 04:03:46 +000014434 ret_addr = flatten(state, ret_loc, ret_addr);
14435 ret_set = flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14436 jmp = flatten(state, ret_loc,
14437 call(state, retvar, ret_addr, func_first, func_last));
14438
Eric Biederman7dea9552004-06-29 05:38:37 +000014439 /* Find the result */
14440 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14441 struct triple * result;
14442 result = flatten(state, first,
14443 read_expr(state,
14444 deref_index(state, fresult(state, func), 1)));
14445
14446 propogate_use(state, fcall, result);
14447 }
14448
14449 /* Release the original fcall instruction */
14450 release_triple(state, fcall);
14451
Eric Biederman90089602004-05-28 14:11:54 +000014452 /* Restore the active variables from the result variable */
14453 for(i = 0, set = enclose; set ; set = set->next, i++) {
14454 struct triple_set *use, *next;
14455 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014456 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014457 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014458 continue;
14459 }
Eric Biederman90089602004-05-28 14:11:54 +000014460 /* Generate an expression for the value */
14461 new = flatten(state, first,
14462 read_expr(state,
14463 closure_expr(state, func, closure_idx, i)));
14464
14465
14466 /* If the original is an lvalue restore the preserved value */
14467 if (is_lvalue(state, set->member)) {
14468 flatten(state, first,
14469 write_expr(state, set->member, new));
14470 continue;
14471 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014472 /*
14473 * If the original is a value update the dominated uses.
14474 */
14475
14476 /* Analyze the basic blocks so I can see who dominates whom */
14477 bb.func = me;
14478 bb.first = RHS(me, 0);
14479 if (!triple_is_ret(state, bb.first->prev)) {
14480 bb.func = 0;
14481 }
14482 analyze_basic_blocks(state, &bb);
14483
Eric Biederman90089602004-05-28 14:11:54 +000014484
14485#if DEBUG_EXPLICIT_CLOSURES
14486 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14487 set->member, new);
14488#endif
14489 /* If fcall dominates the use update the expression */
14490 for(use = set->member->use; use; use = next) {
14491 /* Replace use modifies the use chain and
14492 * removes use, so I must take a copy of the
14493 * next entry early.
14494 */
14495 next = use->next;
14496 if (!tdominates(state, fcall, use->member)) {
14497 continue;
14498 }
14499 replace_use(state, set->member, new, use->member);
14500 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014501
14502 /* Release the basic blocks, the instructions will be
14503 * different next time, and flatten/insert_triple does
14504 * not update the block values so I can't cache the analysis.
14505 */
14506 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014507 }
14508
Eric Biederman90089602004-05-28 14:11:54 +000014509 /* Release the closure variable list */
14510 free_closure_variables(state, &enclose);
14511
14512 if (state->compiler->debug & DEBUG_INLINE) {
14513 FILE *fp = state->dbgout;
14514 fprintf(fp, "\n");
14515 loc(fp, state, 0);
14516 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14517 display_func(state, fp, func);
14518 display_func(state, fp, me);
14519 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14520 }
14521
14522 return;
14523}
14524
14525static int do_inline(struct compile_state *state, struct triple *func)
14526{
14527 int do_inline;
14528 int policy;
14529
14530 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14531 switch(policy) {
14532 case COMPILER_INLINE_ALWAYS:
14533 do_inline = 1;
14534 if (func->type->type & ATTRIB_NOINLINE) {
14535 error(state, func, "noinline with always_inline compiler option");
14536 }
14537 break;
14538 case COMPILER_INLINE_NEVER:
14539 do_inline = 0;
14540 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14541 error(state, func, "always_inline with noinline compiler option");
14542 }
14543 break;
14544 case COMPILER_INLINE_DEFAULTON:
14545 switch(func->type->type & STOR_MASK) {
14546 case STOR_STATIC | STOR_INLINE:
14547 case STOR_LOCAL | STOR_INLINE:
14548 case STOR_EXTERN | STOR_INLINE:
14549 do_inline = 1;
14550 break;
14551 default:
14552 do_inline = 1;
14553 break;
14554 }
14555 break;
14556 case COMPILER_INLINE_DEFAULTOFF:
14557 switch(func->type->type & STOR_MASK) {
14558 case STOR_STATIC | STOR_INLINE:
14559 case STOR_LOCAL | STOR_INLINE:
14560 case STOR_EXTERN | STOR_INLINE:
14561 do_inline = 1;
14562 break;
14563 default:
14564 do_inline = 0;
14565 break;
14566 }
14567 break;
14568 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014569 switch(func->type->type & STOR_MASK) {
14570 case STOR_STATIC | STOR_INLINE:
14571 case STOR_LOCAL | STOR_INLINE:
14572 case STOR_EXTERN | STOR_INLINE:
14573 do_inline = 1;
14574 break;
14575 default:
14576 do_inline = (func->u.cval == 1);
14577 break;
14578 }
Eric Biederman90089602004-05-28 14:11:54 +000014579 break;
14580 default:
14581 do_inline = 0;
14582 internal_error(state, 0, "Unimplemented inline policy");
14583 break;
14584 }
14585 /* Force inlining */
14586 if (func->type->type & ATTRIB_NOINLINE) {
14587 do_inline = 0;
14588 }
14589 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14590 do_inline = 1;
14591 }
14592 return do_inline;
14593}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014594
Eric Biederman90089602004-05-28 14:11:54 +000014595static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14596{
14597 struct triple *first, *ptr, *next;
14598 /* If the function is not used don't bother */
14599 if (me->u.cval <= 0) {
14600 return;
14601 }
14602 if (state->compiler->debug & DEBUG_CALLS2) {
14603 FILE *fp = state->dbgout;
14604 fprintf(fp, "in: %s\n",
14605 me->type->type_ident->name);
14606 }
14607
14608 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014609 ptr = next = first;
14610 do {
Eric Biederman90089602004-05-28 14:11:54 +000014611 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014612 ptr = next;
14613 prev = ptr->prev;
14614 next = ptr->next;
14615 if (ptr->op != OP_FCALL) {
14616 continue;
14617 }
14618 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014619 /* See if the function should be inlined */
14620 if (!do_inline(state, func)) {
14621 /* Put a label after the fcall */
14622 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14623 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014624 }
Eric Biederman90089602004-05-28 14:11:54 +000014625 if (state->compiler->debug & DEBUG_CALLS) {
14626 FILE *fp = state->dbgout;
14627 if (state->compiler->debug & DEBUG_CALLS2) {
14628 loc(fp, state, ptr);
14629 }
14630 fprintf(fp, "inlining %s\n",
14631 func->type->type_ident->name);
14632 fflush(fp);
14633 }
14634
14635 /* Update the function use counts */
14636 func->u.cval -= 1;
14637
14638 /* Replace the fcall with the called function */
14639 expand_inline_call(state, me, ptr);
14640
14641 next = prev->next;
14642 } while (next != first);
14643
14644 ptr = next = first;
14645 do {
14646 struct triple *prev, *func;
14647 ptr = next;
14648 prev = ptr->prev;
14649 next = ptr->next;
14650 if (ptr->op != OP_FCALL) {
14651 continue;
14652 }
14653 func = MISC(ptr, 0);
14654 if (state->compiler->debug & DEBUG_CALLS) {
14655 FILE *fp = state->dbgout;
14656 if (state->compiler->debug & DEBUG_CALLS2) {
14657 loc(fp, state, ptr);
14658 }
14659 fprintf(fp, "calling %s\n",
14660 func->type->type_ident->name);
14661 fflush(fp);
14662 }
14663 /* Replace the fcall with the instruction sequence
14664 * needed to make the call.
14665 */
14666 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014667 next = prev->next;
14668 } while(next != first);
14669}
Eric Biederman90089602004-05-28 14:11:54 +000014670
14671static void inline_functions(struct compile_state *state, struct triple *func)
14672{
14673 inline_function(state, func, 0);
14674 reverse_walk_functions(state, inline_function, 0);
14675}
14676
Eric Biederman5ade04a2003-10-22 04:03:46 +000014677static void insert_function(struct compile_state *state,
14678 struct triple *func, void *arg)
14679{
14680 struct triple *first, *end, *ffirst, *fend;
14681
14682 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014683 FILE *fp = state->errout;
14684 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014685 func->type->type_ident->name, func->u.cval);
14686 }
14687 if (func->u.cval == 0) {
14688 return;
14689 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014690
14691 /* Find the end points of the lists */
14692 first = arg;
14693 end = first->prev;
14694 ffirst = RHS(func, 0);
14695 fend = ffirst->prev;
14696
14697 /* splice the lists together */
14698 end->next = ffirst;
14699 ffirst->prev = end;
14700 fend->next = first;
14701 first->prev = fend;
14702}
14703
Eric Biederman90089602004-05-28 14:11:54 +000014704struct triple *input_asm(struct compile_state *state)
14705{
14706 struct asm_info *info;
14707 struct triple *def;
14708 int i, out;
14709
14710 info = xcmalloc(sizeof(*info), "asm_info");
14711 info->str = "";
14712
14713 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14714 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14715
14716 def = new_triple(state, OP_ASM, &void_type, out, 0);
14717 def->u.ainfo = info;
14718 def->id |= TRIPLE_FLAG_VOLATILE;
14719
14720 for(i = 0; i < out; i++) {
14721 struct triple *piece;
14722 piece = triple(state, OP_PIECE, &int_type, def, 0);
14723 piece->u.cval = i;
14724 LHS(def, i) = piece;
14725 }
14726
14727 return def;
14728}
14729
14730struct triple *output_asm(struct compile_state *state)
14731{
14732 struct asm_info *info;
14733 struct triple *def;
14734 int in;
14735
14736 info = xcmalloc(sizeof(*info), "asm_info");
14737 info->str = "";
14738
14739 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14740 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14741
14742 def = new_triple(state, OP_ASM, &void_type, 0, in);
14743 def->u.ainfo = info;
14744 def->id |= TRIPLE_FLAG_VOLATILE;
14745
14746 return def;
14747}
14748
Eric Biederman5ade04a2003-10-22 04:03:46 +000014749static void join_functions(struct compile_state *state)
14750{
Eric Biederman90089602004-05-28 14:11:54 +000014751 struct triple *jmp, *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014752 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014753 struct type *pnext, *param;
14754 struct type *result_type, *args_type;
14755 int idx;
14756
14757 /* Be clear the functions have not been joined yet */
14758 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014759
14760 /* Dummy file state to get debug handing right */
14761 memset(&file, 0, sizeof(file));
14762 file.basename = "";
14763 file.line = 0;
14764 file.report_line = 0;
14765 file.report_name = file.basename;
14766 file.prev = state->file;
14767 state->file = &file;
14768 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014769
Eric Biederman41203d92004-11-08 09:31:09 +000014770 if (!state->main_function) {
14771 error(state, 0, "No functions to compile\n");
14772 }
14773
Eric Biederman90089602004-05-28 14:11:54 +000014774 /* The type of arguments */
14775 args_type = state->main_function->type->right;
14776 /* The return type without any specifiers */
14777 result_type = clone_type(0, state->main_function->type->left);
14778
14779
14780 /* Verify the external arguments */
14781 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
14782 error(state, state->main_function,
14783 "Too many external input arguments");
14784 }
14785 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
14786 error(state, state->main_function,
14787 "Too many external output arguments");
14788 }
14789
Eric Biederman5ade04a2003-10-22 04:03:46 +000014790 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014791 end = label(state);
14792 start = label(state);
14793 start = flatten(state, state->first, start);
14794 end = flatten(state, state->first, end);
14795 in = input_asm(state);
14796 out = output_asm(state);
14797 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014798 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014799 in = flatten(state, state->first, in);
14800 call = flatten(state, state->first, call);
14801 out = flatten(state, state->first, out);
14802
14803
14804 /* Read the external input arguments */
14805 pnext = args_type;
14806 idx = 0;
14807 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14808 struct triple *expr;
14809 param = pnext;
14810 pnext = 0;
14811 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14812 pnext = param->right;
14813 param = param->left;
14814 }
14815 if (registers_of(state, param) != 1) {
14816 error(state, state->main_function,
14817 "Arg: %d %s requires multiple registers",
14818 idx + 1, param->field_ident->name);
14819 }
14820 expr = read_expr(state, LHS(in, idx));
14821 RHS(call, idx) = expr;
14822 expr = flatten(state, call, expr);
14823 use_triple(expr, call);
14824
14825 idx++;
14826 }
14827
14828
14829 /* Write the external output arguments */
14830 pnext = result_type;
14831 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14832 pnext = result_type->left;
14833 }
14834 for(idx = 0; idx < out->rhs; idx++) {
14835 struct triple *expr;
14836 param = pnext;
14837 pnext = 0;
14838 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14839 pnext = param->right;
14840 param = param->left;
14841 }
14842 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14843 param = 0;
14844 }
14845 if (param) {
14846 if (registers_of(state, param) != 1) {
14847 error(state, state->main_function,
14848 "Result: %d %s requires multiple registers",
14849 idx, param->field_ident->name);
14850 }
14851 expr = read_expr(state, call);
14852 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14853 expr = deref_field(state, expr, param->field_ident);
14854 }
14855 } else {
14856 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14857 }
14858 flatten(state, out, expr);
14859 RHS(out, idx) = expr;
14860 use_triple(expr, out);
14861 }
14862
14863 /* Allocate a dummy containing function */
14864 func = triple(state, OP_LIST,
14865 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14866 func->type->type_ident = lookup(state, "", 0);
14867 RHS(func, 0) = state->first;
14868 func->u.cval = 1;
14869
Eric Biederman5ade04a2003-10-22 04:03:46 +000014870 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014871 mark_live_functions(state);
14872 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014873 walk_functions(state, insert_function, end);
14874
14875 if (start->next != end) {
14876 jmp = flatten(state, start, branch(state, end, 0));
14877 }
14878
Eric Biederman90089602004-05-28 14:11:54 +000014879 /* OK now the functions have been joined. */
14880 state->functions_joined = 1;
14881
Eric Biederman5ade04a2003-10-22 04:03:46 +000014882 /* Done now cleanup */
14883 state->file = file.prev;
14884 state->function = 0;
14885}
14886
Eric Biedermanb138ac82003-04-22 18:44:01 +000014887/*
14888 * Data structurs for optimation.
14889 */
14890
Eric Biederman5ade04a2003-10-22 04:03:46 +000014891
Eric Biederman83b991a2003-10-11 06:20:25 +000014892static int do_use_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014893 struct block *used, struct block_set **head, struct block *user,
14894 int front)
14895{
14896 struct block_set **ptr, *new;
14897 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014898 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014899 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014900 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014901 ptr = head;
14902 while(*ptr) {
14903 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014904 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014905 }
14906 ptr = &(*ptr)->next;
14907 }
14908 new = xcmalloc(sizeof(*new), "block_set");
14909 new->member = user;
14910 if (front) {
14911 new->next = *head;
14912 *head = new;
14913 }
14914 else {
14915 new->next = 0;
14916 *ptr = new;
14917 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014918 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014919}
Eric Biederman83b991a2003-10-11 06:20:25 +000014920static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014921 struct block *used, struct block_set **head, struct block *unuser)
14922{
14923 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014924 int count;
14925 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014926 ptr = head;
14927 while(*ptr) {
14928 use = *ptr;
14929 if (use->member == unuser) {
14930 *ptr = use->next;
14931 memset(use, -1, sizeof(*use));
14932 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014933 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014934 }
14935 else {
14936 ptr = &use->next;
14937 }
14938 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014939 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014940}
14941
14942static void use_block(struct block *used, struct block *user)
14943{
Eric Biederman83b991a2003-10-11 06:20:25 +000014944 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014945 /* Append new to the head of the list, print_block
14946 * depends on this.
14947 */
Eric Biederman83b991a2003-10-11 06:20:25 +000014948 count = do_use_block(used, &used->use, user, 1);
14949 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014950}
14951static void unuse_block(struct block *used, struct block *unuser)
14952{
Eric Biederman83b991a2003-10-11 06:20:25 +000014953 int count;
14954 count = do_unuse_block(used, &used->use, unuser);
14955 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014956}
14957
Eric Biederman5ade04a2003-10-22 04:03:46 +000014958static void add_block_edge(struct block *block, struct block *edge, int front)
14959{
14960 int count;
14961 count = do_use_block(block, &block->edges, edge, front);
14962 block->edge_count += count;
14963}
14964
14965static void remove_block_edge(struct block *block, struct block *edge)
14966{
14967 int count;
14968 count = do_unuse_block(block, &block->edges, edge);
14969 block->edge_count -= count;
14970}
14971
Eric Biedermanb138ac82003-04-22 18:44:01 +000014972static void idom_block(struct block *idom, struct block *user)
14973{
14974 do_use_block(idom, &idom->idominates, user, 0);
14975}
14976
14977static void unidom_block(struct block *idom, struct block *unuser)
14978{
14979 do_unuse_block(idom, &idom->idominates, unuser);
14980}
14981
14982static void domf_block(struct block *block, struct block *domf)
14983{
14984 do_use_block(block, &block->domfrontier, domf, 0);
14985}
14986
14987static void undomf_block(struct block *block, struct block *undomf)
14988{
14989 do_unuse_block(block, &block->domfrontier, undomf);
14990}
14991
14992static void ipdom_block(struct block *ipdom, struct block *user)
14993{
14994 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14995}
14996
14997static void unipdom_block(struct block *ipdom, struct block *unuser)
14998{
14999 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
15000}
15001
15002static void ipdomf_block(struct block *block, struct block *ipdomf)
15003{
15004 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
15005}
15006
15007static void unipdomf_block(struct block *block, struct block *unipdomf)
15008{
15009 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15010}
15011
Eric Biederman83b991a2003-10-11 06:20:25 +000015012static int walk_triples(
15013 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015014 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15015 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015016{
Eric Biederman83b991a2003-10-11 06:20:25 +000015017 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015018 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015019 ptr = state->first;
15020 do {
Eric Biederman90089602004-05-28 14:11:54 +000015021 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015022 if (ptr->next->prev != ptr) {
15023 internal_error(state, ptr->next, "bad prev");
15024 }
15025 ptr = ptr->next;
15026 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015027 return result;
15028}
15029
Eric Biedermanb138ac82003-04-22 18:44:01 +000015030#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015031static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015032{
Eric Biederman90089602004-05-28 14:11:54 +000015033 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015034 int op;
15035 op = ins->op;
15036 if (op == OP_LIST) {
15037#if !PRINT_LIST
15038 return 0;
15039#endif
15040 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015041 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015042 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015043 }
Eric Biederman90089602004-05-28 14:11:54 +000015044 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015045
Eric Biederman90089602004-05-28 14:11:54 +000015046 if (triple_is_branch(state, ins) && ins->use &&
15047 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015048 internal_error(state, ins, "branch used?");
15049 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015050 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015051 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015052 }
15053 return 0;
15054}
15055
Eric Biedermanb138ac82003-04-22 18:44:01 +000015056static void print_triples(struct compile_state *state)
15057{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015058 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015059 FILE *fp = state->dbgout;
15060 fprintf(fp, "--------------- triples ---------------\n");
15061 walk_triples(state, do_print_triple, fp);
15062 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015063 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015064}
15065
15066struct cf_block {
15067 struct block *block;
15068};
15069static void find_cf_blocks(struct cf_block *cf, struct block *block)
15070{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015071 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015072 if (!block || (cf[block->vertex].block == block)) {
15073 return;
15074 }
15075 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015076 for(edge = block->edges; edge; edge = edge->next) {
15077 find_cf_blocks(cf, edge->member);
15078 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015079}
15080
Eric Biederman90089602004-05-28 14:11:54 +000015081static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015082 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015083{
15084 struct cf_block *cf;
15085 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015086 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015087 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15088 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015089
Eric Biederman90089602004-05-28 14:11:54 +000015090 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015091 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015092 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015093 block = cf[i].block;
15094 if (!block)
15095 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015096 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015097 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015098 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015099 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015100 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015101 }
15102
15103 xfree(cf);
15104}
15105
Eric Biedermanb138ac82003-04-22 18:44:01 +000015106static void free_basic_block(struct compile_state *state, struct block *block)
15107{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015108 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015109 struct block *child;
15110 if (!block) {
15111 return;
15112 }
15113 if (block->vertex == -1) {
15114 return;
15115 }
15116 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015117 for(edge = block->edges; edge; edge = edge->next) {
15118 if (edge->member) {
15119 unuse_block(edge->member, block);
15120 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015121 }
15122 if (block->idom) {
15123 unidom_block(block->idom, block);
15124 }
15125 block->idom = 0;
15126 if (block->ipdom) {
15127 unipdom_block(block->ipdom, block);
15128 }
15129 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015130 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015131 child = entry->member;
15132 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015133 if (child && (child->vertex != -1)) {
15134 for(edge = child->edges; edge; edge = edge->next) {
15135 edge->member = 0;
15136 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015137 }
15138 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015139 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015140 child = entry->member;
15141 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015142 if (child && (child->vertex != -1)) {
15143 child->idom = 0;
15144 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015145 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015146 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015147 child = entry->member;
15148 undomf_block(block, child);
15149 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015150 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015151 child = entry->member;
15152 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015153 if (child && (child->vertex != -1)) {
15154 child->ipdom = 0;
15155 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015156 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015157 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015158 child = entry->member;
15159 unipdomf_block(block, child);
15160 }
15161 if (block->users != 0) {
15162 internal_error(state, 0, "block still has users");
15163 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015164 while((edge = block->edges)) {
15165 child = edge->member;
15166 remove_block_edge(block, child);
15167
15168 if (child && (child->vertex != -1)) {
15169 free_basic_block(state, child);
15170 }
15171 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015172 memset(block, -1, sizeof(*block));
Patrick Georgi26774f22009-11-21 19:54:02 +000015173#ifndef WIN32
Eric Biedermanb138ac82003-04-22 18:44:01 +000015174 xfree(block);
Patrick Georgi26774f22009-11-21 19:54:02 +000015175#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015176}
15177
Eric Biederman90089602004-05-28 14:11:54 +000015178static void free_basic_blocks(struct compile_state *state,
15179 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015180{
15181 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015182 free_basic_block(state, bb->first_block);
15183 bb->last_vertex = 0;
15184 bb->first_block = bb->last_block = 0;
15185 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015186 ins = first;
15187 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015188 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015189 ins->u.block = 0;
15190 }
15191 ins = ins->next;
15192 } while(ins != first);
15193
15194}
15195
Eric Biederman90089602004-05-28 14:11:54 +000015196static struct block *basic_block(struct compile_state *state,
15197 struct basic_blocks *bb, struct triple *first)
15198{
15199 struct block *block;
15200 struct triple *ptr;
15201 if (!triple_is_label(state, first)) {
15202 internal_error(state, first, "block does not start with a label");
15203 }
15204 /* See if this basic block has already been setup */
15205 if (first->u.block != 0) {
15206 return first->u.block;
15207 }
15208 /* Allocate another basic block structure */
15209 bb->last_vertex += 1;
15210 block = xcmalloc(sizeof(*block), "block");
15211 block->first = block->last = first;
15212 block->vertex = bb->last_vertex;
15213 ptr = first;
15214 do {
15215 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
15216 break;
15217 }
15218 block->last = ptr;
15219 /* If ptr->u is not used remember where the baic block is */
15220 if (triple_stores_block(state, ptr)) {
15221 ptr->u.block = block;
15222 }
15223 if (triple_is_branch(state, ptr)) {
15224 break;
15225 }
15226 ptr = ptr->next;
15227 } while (ptr != bb->first);
15228 if ((ptr == bb->first) ||
15229 ((ptr->next == bb->first) && (
15230 triple_is_end(state, ptr) ||
15231 triple_is_ret(state, ptr))))
15232 {
15233 /* The block has no outflowing edges */
15234 }
15235 else if (triple_is_label(state, ptr)) {
15236 struct block *next;
15237 next = basic_block(state, bb, ptr);
15238 add_block_edge(block, next, 0);
15239 use_block(next, block);
15240 }
15241 else if (triple_is_branch(state, ptr)) {
15242 struct triple **expr, *first;
15243 struct block *child;
15244 /* Find the branch targets.
15245 * I special case the first branch as that magically
15246 * avoids some difficult cases for the register allocator.
15247 */
15248 expr = triple_edge_targ(state, ptr, 0);
15249 if (!expr) {
15250 internal_error(state, ptr, "branch without targets");
15251 }
15252 first = *expr;
15253 expr = triple_edge_targ(state, ptr, expr);
15254 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15255 if (!*expr) continue;
15256 child = basic_block(state, bb, *expr);
15257 use_block(child, block);
15258 add_block_edge(block, child, 0);
15259 }
15260 if (first) {
15261 child = basic_block(state, bb, first);
15262 use_block(child, block);
15263 add_block_edge(block, child, 1);
15264
15265 /* Be certain the return block of a call is
15266 * in a basic block. When it is not find
15267 * start of the block, insert a label if
15268 * necessary and build the basic block.
15269 * Then add a fake edge from the start block
15270 * to the return block of the function.
15271 */
15272 if (state->functions_joined && triple_is_call(state, ptr)
15273 && !block_of_triple(state, MISC(ptr, 0))) {
15274 struct block *tail;
15275 struct triple *start;
15276 start = triple_to_block_start(state, MISC(ptr, 0));
15277 if (!triple_is_label(state, start)) {
15278 start = pre_triple(state,
15279 start, OP_LABEL, &void_type, 0, 0);
15280 }
15281 tail = basic_block(state, bb, start);
15282 add_block_edge(child, tail, 0);
15283 use_block(tail, child);
15284 }
15285 }
15286 }
15287 else {
15288 internal_error(state, 0, "Bad basic block split");
15289 }
15290#if 0
15291{
15292 struct block_set *edge;
15293 FILE *fp = state->errout;
15294 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
15295 block, block->vertex,
15296 block->first, block->last);
15297 for(edge = block->edges; edge; edge = edge->next) {
15298 fprintf(fp, " %10p [%2d]",
15299 edge->member ? edge->member->first : 0,
15300 edge->member ? edge->member->vertex : -1);
15301 }
15302 fprintf(fp, "\n");
15303}
15304#endif
15305 return block;
15306}
15307
15308
15309static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15310 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15311 void *arg)
15312{
15313 struct triple *ptr, *first;
15314 struct block *last_block;
15315 last_block = 0;
15316 first = bb->first;
15317 ptr = first;
15318 do {
15319 if (triple_stores_block(state, ptr)) {
15320 struct block *block;
15321 block = ptr->u.block;
15322 if (block && (block != last_block)) {
15323 cb(state, block, arg);
15324 }
15325 last_block = block;
15326 }
15327 ptr = ptr->next;
15328 } while(ptr != first);
15329}
15330
15331static void print_block(
15332 struct compile_state *state, struct block *block, void *arg)
15333{
15334 struct block_set *user, *edge;
15335 struct triple *ptr;
15336 FILE *fp = arg;
15337
15338 fprintf(fp, "\nblock: %p (%d) ",
15339 block,
15340 block->vertex);
15341
15342 for(edge = block->edges; edge; edge = edge->next) {
15343 fprintf(fp, " %p<-%p",
15344 edge->member,
15345 (edge->member && edge->member->use)?
15346 edge->member->use->member : 0);
15347 }
15348 fprintf(fp, "\n");
15349 if (block->first->op == OP_LABEL) {
15350 fprintf(fp, "%p:\n", block->first);
15351 }
15352 for(ptr = block->first; ; ) {
15353 display_triple(fp, ptr);
15354 if (ptr == block->last)
15355 break;
15356 ptr = ptr->next;
15357 if (ptr == block->first) {
15358 internal_error(state, 0, "missing block last?");
15359 }
15360 }
15361 fprintf(fp, "users %d: ", block->users);
15362 for(user = block->use; user; user = user->next) {
15363 fprintf(fp, "%p (%d) ",
15364 user->member,
15365 user->member->vertex);
15366 }
15367 fprintf(fp,"\n\n");
15368}
15369
15370
15371static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15372{
15373 fprintf(fp, "--------------- blocks ---------------\n");
15374 walk_blocks(state, &state->bb, print_block, fp);
15375}
15376static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15377{
15378 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15379 fprintf(fp, "After %s\n", func);
15380 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015381 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15382 print_dominators(state, fp, &state->bb);
15383 print_dominance_frontiers(state, fp, &state->bb);
15384 }
15385 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015386 }
15387}
15388
15389static void prune_nonblock_triples(struct compile_state *state,
15390 struct basic_blocks *bb)
15391{
15392 struct block *block;
15393 struct triple *first, *ins, *next;
15394 /* Delete the triples not in a basic block */
15395 block = 0;
15396 first = bb->first;
15397 ins = first;
15398 do {
15399 next = ins->next;
15400 if (ins->op == OP_LABEL) {
15401 block = ins->u.block;
15402 }
15403 if (!block) {
15404 struct triple_set *use;
15405 for(use = ins->use; use; use = use->next) {
15406 struct block *block;
15407 block = block_of_triple(state, use->member);
15408 if (block != 0) {
15409 internal_error(state, ins, "pruning used ins?");
15410 }
15411 }
15412 release_triple(state, ins);
15413 }
15414 if (block && block->last == ins) {
15415 block = 0;
15416 }
15417 ins = next;
15418 } while(ins != first);
15419}
15420
15421static void setup_basic_blocks(struct compile_state *state,
15422 struct basic_blocks *bb)
15423{
15424 if (!triple_stores_block(state, bb->first)) {
15425 internal_error(state, 0, "ins will not store block?");
15426 }
15427 /* Initialize the state */
15428 bb->first_block = bb->last_block = 0;
15429 bb->last_vertex = 0;
15430 free_basic_blocks(state, bb);
15431
15432 /* Find the basic blocks */
15433 bb->first_block = basic_block(state, bb, bb->first);
15434
15435 /* Be certain the last instruction of a function, or the
15436 * entire program is in a basic block. When it is not find
15437 * the start of the block, insert a label if necessary and build
15438 * basic block. Then add a fake edge from the start block
15439 * to the final block.
15440 */
15441 if (!block_of_triple(state, bb->first->prev)) {
15442 struct triple *start;
15443 struct block *tail;
15444 start = triple_to_block_start(state, bb->first->prev);
15445 if (!triple_is_label(state, start)) {
15446 start = pre_triple(state,
15447 start, OP_LABEL, &void_type, 0, 0);
15448 }
15449 tail = basic_block(state, bb, start);
15450 add_block_edge(bb->first_block, tail, 0);
15451 use_block(tail, bb->first_block);
15452 }
15453
15454 /* Find the last basic block.
15455 */
15456 bb->last_block = block_of_triple(state, bb->first->prev);
15457
15458 /* Delete the triples not in a basic block */
15459 prune_nonblock_triples(state, bb);
15460
15461#if 0
15462 /* If we are debugging print what I have just done */
15463 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15464 print_blocks(state, state->dbgout);
15465 print_control_flow(state, bb);
15466 }
15467#endif
15468}
15469
15470
Eric Biedermanb138ac82003-04-22 18:44:01 +000015471struct sdom_block {
15472 struct block *block;
15473 struct sdom_block *sdominates;
15474 struct sdom_block *sdom_next;
15475 struct sdom_block *sdom;
15476 struct sdom_block *label;
15477 struct sdom_block *parent;
15478 struct sdom_block *ancestor;
15479 int vertex;
15480};
15481
15482
15483static void unsdom_block(struct sdom_block *block)
15484{
15485 struct sdom_block **ptr;
15486 if (!block->sdom_next) {
15487 return;
15488 }
15489 ptr = &block->sdom->sdominates;
15490 while(*ptr) {
15491 if ((*ptr) == block) {
15492 *ptr = block->sdom_next;
15493 return;
15494 }
15495 ptr = &(*ptr)->sdom_next;
15496 }
15497}
15498
15499static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15500{
15501 unsdom_block(block);
15502 block->sdom = sdom;
15503 block->sdom_next = sdom->sdominates;
15504 sdom->sdominates = block;
15505}
15506
15507
15508
15509static int initialize_sdblock(struct sdom_block *sd,
15510 struct block *parent, struct block *block, int vertex)
15511{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015512 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015513 if (!block || (sd[block->vertex].block == block)) {
15514 return vertex;
15515 }
15516 vertex += 1;
15517 /* Renumber the blocks in a convinient fashion */
15518 block->vertex = vertex;
15519 sd[vertex].block = block;
15520 sd[vertex].sdom = &sd[vertex];
15521 sd[vertex].label = &sd[vertex];
15522 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15523 sd[vertex].ancestor = 0;
15524 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015525 for(edge = block->edges; edge; edge = edge->next) {
15526 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15527 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015528 return vertex;
15529}
15530
Eric Biederman83b991a2003-10-11 06:20:25 +000015531static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015532 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015533 struct block *parent, struct block *block, int vertex)
15534{
15535 struct block_set *user;
15536 if (!block || (sd[block->vertex].block == block)) {
15537 return vertex;
15538 }
15539 vertex += 1;
15540 /* Renumber the blocks in a convinient fashion */
15541 block->vertex = vertex;
15542 sd[vertex].block = block;
15543 sd[vertex].sdom = &sd[vertex];
15544 sd[vertex].label = &sd[vertex];
15545 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15546 sd[vertex].ancestor = 0;
15547 sd[vertex].vertex = vertex;
15548 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015549 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015550 }
15551 return vertex;
15552}
15553
Eric Biederman90089602004-05-28 14:11:54 +000015554static int setup_spdblocks(struct compile_state *state,
15555 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015556{
15557 struct block *block;
15558 int vertex;
15559 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015560 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015561
Eric Biederman5ade04a2003-10-22 04:03:46 +000015562 /* Walk through the graph and find unconnected blocks. Add a
15563 * fake edge from the unconnected blocks to the end of the
15564 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015565 */
Eric Biederman90089602004-05-28 14:11:54 +000015566 block = bb->first_block->last->next->u.block;
15567 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015568 if (sd[block->vertex].block == block) {
15569 continue;
15570 }
Eric Biederman530b5192003-07-01 10:05:30 +000015571#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015572 {
15573 FILE *fp = state->errout;
15574 fprintf(fp, "Adding %d\n", vertex +1);
15575 }
Eric Biederman530b5192003-07-01 10:05:30 +000015576#endif
Eric Biederman90089602004-05-28 14:11:54 +000015577 add_block_edge(block, bb->last_block, 0);
15578 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015579
Eric Biederman90089602004-05-28 14:11:54 +000015580 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015581 }
15582 return vertex;
15583}
15584
15585static void compress_ancestors(struct sdom_block *v)
15586{
15587 /* This procedure assumes ancestor(v) != 0 */
15588 /* if (ancestor(ancestor(v)) != 0) {
15589 * compress(ancestor(ancestor(v)));
15590 * if (semi(label(ancestor(v))) < semi(label(v))) {
15591 * label(v) = label(ancestor(v));
15592 * }
15593 * ancestor(v) = ancestor(ancestor(v));
15594 * }
15595 */
15596 if (!v->ancestor) {
15597 return;
15598 }
15599 if (v->ancestor->ancestor) {
15600 compress_ancestors(v->ancestor->ancestor);
15601 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15602 v->label = v->ancestor->label;
15603 }
15604 v->ancestor = v->ancestor->ancestor;
15605 }
15606}
15607
Eric Biederman90089602004-05-28 14:11:54 +000015608static void compute_sdom(struct compile_state *state,
15609 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015610{
15611 int i;
15612 /* // step 2
15613 * for each v <= pred(w) {
15614 * u = EVAL(v);
15615 * if (semi[u] < semi[w] {
15616 * semi[w] = semi[u];
15617 * }
15618 * }
15619 * add w to bucket(vertex(semi[w]));
15620 * LINK(parent(w), w);
15621 *
15622 * // step 3
15623 * for each v <= bucket(parent(w)) {
15624 * delete v from bucket(parent(w));
15625 * u = EVAL(v);
15626 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15627 * }
15628 */
Eric Biederman90089602004-05-28 14:11:54 +000015629 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015630 struct sdom_block *v, *parent, *next;
15631 struct block_set *user;
15632 struct block *block;
15633 block = sd[i].block;
15634 parent = sd[i].parent;
15635 /* Step 2 */
15636 for(user = block->use; user; user = user->next) {
15637 struct sdom_block *v, *u;
15638 v = &sd[user->member->vertex];
15639 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15640 if (u->sdom->vertex < sd[i].sdom->vertex) {
15641 sd[i].sdom = u->sdom;
15642 }
15643 }
15644 sdom_block(sd[i].sdom, &sd[i]);
15645 sd[i].ancestor = parent;
15646 /* Step 3 */
15647 for(v = parent->sdominates; v; v = next) {
15648 struct sdom_block *u;
15649 next = v->sdom_next;
15650 unsdom_block(v);
15651 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15652 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
15653 u->block : parent->block;
15654 }
15655 }
15656}
15657
Eric Biederman90089602004-05-28 14:11:54 +000015658static void compute_spdom(struct compile_state *state,
15659 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015660{
15661 int i;
15662 /* // step 2
15663 * for each v <= pred(w) {
15664 * u = EVAL(v);
15665 * if (semi[u] < semi[w] {
15666 * semi[w] = semi[u];
15667 * }
15668 * }
15669 * add w to bucket(vertex(semi[w]));
15670 * LINK(parent(w), w);
15671 *
15672 * // step 3
15673 * for each v <= bucket(parent(w)) {
15674 * delete v from bucket(parent(w));
15675 * u = EVAL(v);
15676 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15677 * }
15678 */
Eric Biederman90089602004-05-28 14:11:54 +000015679 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015680 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015681 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015682 struct block *block;
15683 block = sd[i].block;
15684 parent = sd[i].parent;
15685 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015686 for(edge = block->edges; edge; edge = edge->next) {
15687 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015688 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15689 if (u->sdom->vertex < sd[i].sdom->vertex) {
15690 sd[i].sdom = u->sdom;
15691 }
15692 }
15693 sdom_block(sd[i].sdom, &sd[i]);
15694 sd[i].ancestor = parent;
15695 /* Step 3 */
15696 for(v = parent->sdominates; v; v = next) {
15697 struct sdom_block *u;
15698 next = v->sdom_next;
15699 unsdom_block(v);
15700 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15701 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
15702 u->block : parent->block;
15703 }
15704 }
15705}
15706
Eric Biederman90089602004-05-28 14:11:54 +000015707static void compute_idom(struct compile_state *state,
15708 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015709{
15710 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015711 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015712 struct block *block;
15713 block = sd[i].block;
15714 if (block->idom->vertex != sd[i].sdom->vertex) {
15715 block->idom = block->idom->idom;
15716 }
15717 idom_block(block->idom, block);
15718 }
15719 sd[1].block->idom = 0;
15720}
15721
Eric Biederman90089602004-05-28 14:11:54 +000015722static void compute_ipdom(struct compile_state *state,
15723 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015724{
15725 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015726 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015727 struct block *block;
15728 block = sd[i].block;
15729 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15730 block->ipdom = block->ipdom->ipdom;
15731 }
15732 ipdom_block(block->ipdom, block);
15733 }
15734 sd[1].block->ipdom = 0;
15735}
15736
15737 /* Theorem 1:
15738 * Every vertex of a flowgraph G = (V, E, r) except r has
15739 * a unique immediate dominator.
15740 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
15741 * rooted at r, called the dominator tree of G, such that
15742 * v dominates w if and only if v is a proper ancestor of w in
15743 * the dominator tree.
15744 */
15745 /* Lemma 1:
15746 * If v and w are vertices of G such that v <= w,
15747 * than any path from v to w must contain a common ancestor
15748 * of v and w in T.
15749 */
15750 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15751 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15752 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15753 /* Theorem 2:
15754 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15755 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15756 */
15757 /* Theorem 3:
15758 * Let w != r and let u be a vertex for which sdom(u) is
15759 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15760 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15761 */
15762 /* Lemma 5: Let vertices v,w satisfy v -> w.
15763 * Then v -> idom(w) or idom(w) -> idom(v)
15764 */
15765
Eric Biederman90089602004-05-28 14:11:54 +000015766static void find_immediate_dominators(struct compile_state *state,
15767 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015768{
15769 struct sdom_block *sd;
15770 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15771 * vi > w for (1 <= i <= k - 1}
15772 */
15773 /* Theorem 4:
15774 * For any vertex w != r.
15775 * sdom(w) = min(
15776 * {v|(v,w) <= E and v < w } U
15777 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15778 */
15779 /* Corollary 1:
15780 * Let w != r and let u be a vertex for which sdom(u) is
15781 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15782 * Then:
15783 * { sdom(w) if sdom(w) = sdom(u),
15784 * idom(w) = {
15785 * { idom(u) otherwise
15786 */
15787 /* The algorithm consists of the following 4 steps.
15788 * Step 1. Carry out a depth-first search of the problem graph.
15789 * Number the vertices from 1 to N as they are reached during
15790 * the search. Initialize the variables used in succeeding steps.
15791 * Step 2. Compute the semidominators of all vertices by applying
15792 * theorem 4. Carry out the computation vertex by vertex in
15793 * decreasing order by number.
15794 * Step 3. Implicitly define the immediate dominator of each vertex
15795 * by applying Corollary 1.
15796 * Step 4. Explicitly define the immediate dominator of each vertex,
15797 * carrying out the computation vertex by vertex in increasing order
15798 * by number.
15799 */
15800 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015801 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15802 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015803#if 0
15804 sd[1].size = 0;
15805 sd[1].label = 0;
15806 sd[1].sdom = 0;
15807#endif
15808 /* Step 2 compute the semidominators */
15809 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015810 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015811 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015812 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015813 xfree(sd);
15814}
15815
Eric Biederman90089602004-05-28 14:11:54 +000015816static void find_post_dominators(struct compile_state *state,
15817 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015818{
15819 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015820 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015821 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015822 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015823
Eric Biederman90089602004-05-28 14:11:54 +000015824 vertex = setup_spdblocks(state, bb, sd);
15825 if (vertex != bb->last_vertex) {
15826 internal_error(state, 0, "missing %d blocks",
15827 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015828 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015829
15830 /* Step 2 compute the semidominators */
15831 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015832 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015833 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015834 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015835 xfree(sd);
15836}
15837
15838
15839
15840static void find_block_domf(struct compile_state *state, struct block *block)
15841{
15842 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015843 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015844 if (block->domfrontier != 0) {
15845 internal_error(state, block->first, "domfrontier present?");
15846 }
15847 for(user = block->idominates; user; user = user->next) {
15848 child = user->member;
15849 if (child->idom != block) {
15850 internal_error(state, block->first, "bad idom");
15851 }
15852 find_block_domf(state, child);
15853 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015854 for(edge = block->edges; edge; edge = edge->next) {
15855 if (edge->member->idom != block) {
15856 domf_block(block, edge->member);
15857 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015858 }
15859 for(user = block->idominates; user; user = user->next) {
15860 struct block_set *frontier;
15861 child = user->member;
15862 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15863 if (frontier->member->idom != block) {
15864 domf_block(block, frontier->member);
15865 }
15866 }
15867 }
15868}
15869
15870static void find_block_ipdomf(struct compile_state *state, struct block *block)
15871{
15872 struct block *child;
15873 struct block_set *user;
15874 if (block->ipdomfrontier != 0) {
15875 internal_error(state, block->first, "ipdomfrontier present?");
15876 }
15877 for(user = block->ipdominates; user; user = user->next) {
15878 child = user->member;
15879 if (child->ipdom != block) {
15880 internal_error(state, block->first, "bad ipdom");
15881 }
15882 find_block_ipdomf(state, child);
15883 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015884 for(user = block->use; user; user = user->next) {
15885 if (user->member->ipdom != block) {
15886 ipdomf_block(block, user->member);
15887 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015888 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015889 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015890 struct block_set *frontier;
15891 child = user->member;
15892 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15893 if (frontier->member->ipdom != block) {
15894 ipdomf_block(block, frontier->member);
15895 }
15896 }
15897 }
15898}
15899
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015900static void print_dominated(
15901 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015902{
15903 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015904 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015905
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015906 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015907 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015908 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015909 if (user->member->idom != block) {
15910 internal_error(state, user->member->first, "bad idom");
15911 }
15912 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015913 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015914}
15915
Eric Biederman5ade04a2003-10-22 04:03:46 +000015916static void print_dominated2(
15917 struct compile_state *state, FILE *fp, int depth, struct block *block)
15918{
15919 struct block_set *user;
15920 struct triple *ins;
15921 struct occurance *ptr, *ptr2;
15922 const char *filename1, *filename2;
15923 int equal_filenames;
15924 int i;
15925 for(i = 0; i < depth; i++) {
15926 fprintf(fp, " ");
15927 }
15928 fprintf(fp, "%3d: %p (%p - %p) @",
15929 block->vertex, block, block->first, block->last);
15930 ins = block->first;
15931 while(ins != block->last && (ins->occurance->line == 0)) {
15932 ins = ins->next;
15933 }
15934 ptr = ins->occurance;
15935 ptr2 = block->last->occurance;
15936 filename1 = ptr->filename? ptr->filename : "";
15937 filename2 = ptr2->filename? ptr2->filename : "";
15938 equal_filenames = (strcmp(filename1, filename2) == 0);
15939 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15940 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15941 } else if (equal_filenames) {
15942 fprintf(fp, " %s:(%d - %d)",
15943 ptr->filename, ptr->line, ptr2->line);
15944 } else {
15945 fprintf(fp, " (%s:%d - %s:%d)",
15946 ptr->filename, ptr->line,
15947 ptr2->filename, ptr2->line);
15948 }
15949 fprintf(fp, "\n");
15950 for(user = block->idominates; user; user = user->next) {
15951 print_dominated2(state, fp, depth + 1, user->member);
15952 }
15953}
15954
Eric Biederman90089602004-05-28 14:11:54 +000015955static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015956{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015957 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015958 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015959 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015960 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015961}
15962
15963
15964static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015965 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015966{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015967 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015968
15969 if (!block || (block->vertex != vertex + 1)) {
15970 return vertex;
15971 }
15972 vertex += 1;
15973
Eric Biederman7dea9552004-06-29 05:38:37 +000015974 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015975 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015976 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015977 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015978 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015979
15980 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015981 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015982 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015983 return vertex;
15984}
Eric Biederman90089602004-05-28 14:11:54 +000015985static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015986 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015987{
Eric Biederman7dea9552004-06-29 05:38:37 +000015988 fprintf(fp, "\ndominance frontiers\n");
15989 print_frontiers(state, fp, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015990
15991}
15992
Eric Biederman90089602004-05-28 14:11:54 +000015993static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015994{
15995 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015996 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015997 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015998 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015999 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016000 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016001 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016002 print_dominance_frontiers(state, state->dbgout, bb);
16003 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016004 }
16005}
16006
16007
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016008static void print_ipdominated(
16009 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016010{
16011 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016012 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016013
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016014 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016015 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016016 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016017 if (user->member->ipdom != block) {
16018 internal_error(state, user->member->first, "bad ipdom");
16019 }
16020 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016021 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016022}
16023
Eric Biederman90089602004-05-28 14:11:54 +000016024static void print_ipdominators(struct compile_state *state, FILE *fp,
16025 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016026{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016027 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016028 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016029}
16030
16031static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016032 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016033{
16034 struct block_set *user;
16035
16036 if (!block || (block->vertex != vertex + 1)) {
16037 return vertex;
16038 }
16039 vertex += 1;
16040
Eric Biederman7dea9552004-06-29 05:38:37 +000016041 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016042 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016043 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016044 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016045 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016046 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016047 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016048 }
16049 return vertex;
16050}
Eric Biederman90089602004-05-28 14:11:54 +000016051static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016052 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016053{
Eric Biederman7dea9552004-06-29 05:38:37 +000016054 fprintf(fp, "\nipdominance frontiers\n");
16055 print_pfrontiers(state, fp, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016056
16057}
16058
Eric Biederman90089602004-05-28 14:11:54 +000016059static void analyze_ipdominators(struct compile_state *state,
16060 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016061{
16062 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016063 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016064 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016065 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016066 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016067 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016068 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016069 print_ipdominance_frontiers(state, state->dbgout, bb);
16070 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016071 }
16072}
16073
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016074static int bdominates(struct compile_state *state,
16075 struct block *dom, struct block *sub)
16076{
16077 while(sub && (sub != dom)) {
16078 sub = sub->idom;
16079 }
16080 return sub == dom;
16081}
16082
16083static int tdominates(struct compile_state *state,
16084 struct triple *dom, struct triple *sub)
16085{
16086 struct block *bdom, *bsub;
16087 int result;
16088 bdom = block_of_triple(state, dom);
16089 bsub = block_of_triple(state, sub);
16090 if (bdom != bsub) {
16091 result = bdominates(state, bdom, bsub);
16092 }
16093 else {
16094 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016095 if (!bdom || !bsub) {
16096 internal_error(state, dom, "huh?");
16097 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016098 ins = sub;
16099 while((ins != bsub->first) && (ins != dom)) {
16100 ins = ins->prev;
16101 }
16102 result = (ins == dom);
16103 }
16104 return result;
16105}
16106
Eric Biederman90089602004-05-28 14:11:54 +000016107static void analyze_basic_blocks(
16108 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016109{
Eric Biederman90089602004-05-28 14:11:54 +000016110 setup_basic_blocks(state, bb);
16111 analyze_idominators(state, bb);
16112 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016113}
16114
Eric Biedermanb138ac82003-04-22 18:44:01 +000016115static void insert_phi_operations(struct compile_state *state)
16116{
16117 size_t size;
16118 struct triple *first;
16119 int *has_already, *work;
16120 struct block *work_list, **work_list_tail;
16121 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016122 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016123
Eric Biederman90089602004-05-28 14:11:54 +000016124 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016125 has_already = xcmalloc(size, "has_already");
16126 work = xcmalloc(size, "work");
16127 iter = 0;
16128
Eric Biederman83b991a2003-10-11 06:20:25 +000016129 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016130 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016131 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016132 struct triple_set *user, *unext;
16133 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016134
16135 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016136 continue;
16137 }
Eric Biederman90089602004-05-28 14:11:54 +000016138
Eric Biedermanb138ac82003-04-22 18:44:01 +000016139 iter += 1;
16140 work_list = 0;
16141 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016142 for(user = var->use; user; user = unext) {
16143 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016144 if (MISC(var, 0) == user->member) {
16145 continue;
16146 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016147 if (user->member->op == OP_READ) {
16148 continue;
16149 }
16150 if (user->member->op != OP_WRITE) {
16151 internal_error(state, user->member,
16152 "bad variable access");
16153 }
16154 block = user->member->u.block;
16155 if (!block) {
16156 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016157 release_triple(state, user->member);
16158 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016159 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016160 if (work[block->vertex] >= iter) {
16161 continue;
16162 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016163 work[block->vertex] = iter;
16164 *work_list_tail = block;
16165 block->work_next = 0;
16166 work_list_tail = &block->work_next;
16167 }
16168 for(block = work_list; block; block = block->work_next) {
16169 struct block_set *df;
16170 for(df = block->domfrontier; df; df = df->next) {
16171 struct triple *phi;
16172 struct block *front;
16173 int in_edges;
16174 front = df->member;
16175
16176 if (has_already[front->vertex] >= iter) {
16177 continue;
16178 }
16179 /* Count how many edges flow into this block */
16180 in_edges = front->users;
16181 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016182 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016183 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016184 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016185 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016186 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016187 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016188 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016189#if 1
16190 if (phi->rhs != in_edges) {
16191 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16192 phi->rhs, in_edges);
16193 }
16194#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016195 /* Insert the phi functions immediately after the label */
16196 insert_triple(state, front->first->next, phi);
16197 if (front->first == front->last) {
16198 front->last = front->first->next;
16199 }
16200 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016201 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016202
Eric Biedermanb138ac82003-04-22 18:44:01 +000016203 /* If necessary plan to visit the basic block */
16204 if (work[front->vertex] >= iter) {
16205 continue;
16206 }
16207 work[front->vertex] = iter;
16208 *work_list_tail = front;
16209 front->work_next = 0;
16210 work_list_tail = &front->work_next;
16211 }
16212 }
16213 }
16214 xfree(has_already);
16215 xfree(work);
16216}
16217
Eric Biederman66fe2222003-07-04 15:14:04 +000016218
Eric Biederman83b991a2003-10-11 06:20:25 +000016219struct stack {
16220 struct triple_set *top;
16221 unsigned orig_id;
16222};
16223
Eric Biederman90089602004-05-28 14:11:54 +000016224static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016225{
16226 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016227 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016228 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016229 ins = first;
16230 do {
Eric Biederman90089602004-05-28 14:11:54 +000016231 if (triple_is_auto_var(state, ins)) {
16232 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016233 }
16234 ins = ins->next;
16235 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016236 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016237}
16238
Eric Biederman90089602004-05-28 14:11:54 +000016239static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016240{
16241 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016242 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016243 first = state->first;
16244 ins = first;
16245 do {
Eric Biederman90089602004-05-28 14:11:54 +000016246 if (triple_is_auto_var(state, ins)) {
16247 auto_vars += 1;
16248 stacks[auto_vars].orig_id = ins->id;
16249 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016250 }
16251 ins = ins->next;
16252 } while(ins != first);
16253}
16254
Eric Biederman90089602004-05-28 14:11:54 +000016255static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016256{
16257 struct triple *first, *ins;
16258 first = state->first;
16259 ins = first;
16260 do {
Eric Biederman90089602004-05-28 14:11:54 +000016261 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016262 ins->id = stacks[ins->id].orig_id;
16263 }
16264 ins = ins->next;
16265 } while(ins != first);
16266}
16267
16268static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016269{
16270 struct triple_set *head;
16271 struct triple *top_val;
16272 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016273 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016274 if (head) {
16275 top_val = head->member;
16276 }
16277 return top_val;
16278}
16279
Eric Biederman83b991a2003-10-11 06:20:25 +000016280static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016281{
16282 struct triple_set *new;
16283 /* Append new to the head of the list,
16284 * it's the only sensible behavoir for a stack.
16285 */
16286 new = xcmalloc(sizeof(*new), "triple_set");
16287 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016288 new->next = stacks[var->id].top;
16289 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016290}
16291
Eric Biederman83b991a2003-10-11 06:20:25 +000016292static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016293{
16294 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016295 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016296 while(*ptr) {
16297 set = *ptr;
16298 if (set->member == oldval) {
16299 *ptr = set->next;
16300 xfree(set);
16301 /* Only free one occurance from the stack */
16302 return;
16303 }
16304 else {
16305 ptr = &set->next;
16306 }
16307 }
16308}
16309
Eric Biedermanb138ac82003-04-22 18:44:01 +000016310/*
16311 * C(V)
16312 * S(V)
16313 */
16314static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016315 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016316{
16317 struct block_set *set;
16318 struct triple *ptr;
16319 int edge;
16320 if (!parent || !block)
16321 return;
16322 /* Find the edge I am coming in on */
16323 edge = 0;
16324 for(set = block->use; set; set = set->next, edge++) {
16325 if (set->member == parent) {
16326 break;
16327 }
16328 }
16329 if (!set) {
16330 internal_error(state, 0, "phi input is not on a control predecessor");
16331 }
16332 for(ptr = block->first; ; ptr = ptr->next) {
16333 if (ptr->op == OP_PHI) {
16334 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016335 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016336 if (!var) {
16337 internal_error(state, ptr, "no var???");
16338 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016339 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016340 val = peek_triple(stacks, var);
16341 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016342 internal_error(state, val, "bad value in phi");
16343 }
Eric Biederman90089602004-05-28 14:11:54 +000016344 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016345 internal_error(state, ptr, "edges > phi rhs");
16346 }
16347 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016348 if ((*slot != 0) && (*slot != val)) {
16349 internal_error(state, ptr, "phi already bound on this edge");
16350 }
16351 *slot = val;
16352 use_triple(val, ptr);
16353 }
16354 if (ptr == block->last) {
16355 break;
16356 }
16357 }
16358}
16359
16360
16361static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016362 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016363{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016364 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016365 struct triple *ptr, *next, *last;
16366 int done;
16367 if (!block)
16368 return;
16369 last = block->first;
16370 done = 0;
16371 for(ptr = block->first; !done; ptr = next) {
16372 next = ptr->next;
16373 if (ptr == block->last) {
16374 done = 1;
16375 }
16376 /* RHS(A) */
16377 if (ptr->op == OP_READ) {
16378 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016379 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016380 if (!triple_is_auto_var(state, var)) {
16381 internal_error(state, ptr, "read of non auto var!");
16382 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016383 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016384 /* Find the current value of the variable */
16385 val = peek_triple(stacks, var);
16386 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016387 /* Let the optimizer at variables that are not initially
16388 * set. But give it a bogus value so things seem to
16389 * work by accident. This is useful for bitfields because
16390 * setting them always involves a read-modify-write.
16391 */
16392 if (TYPE_ARITHMETIC(ptr->type->type)) {
16393 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16394 val->u.cval = 0xdeadbeaf;
16395 } else {
16396 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16397 }
16398 }
16399 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016400 error(state, ptr, "variable used without being set");
16401 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016402 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16403 internal_error(state, val, "bad value in read");
16404 }
16405 propogate_use(state, ptr, val);
16406 release_triple(state, ptr);
16407 continue;
16408 }
16409 /* LHS(A) */
16410 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016411 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016412 var = MISC(ptr, 0);
16413 if (!triple_is_auto_var(state, var)) {
16414 internal_error(state, ptr, "write to non auto var!");
16415 }
16416 tval = val = RHS(ptr, 0);
16417 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16418 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016419 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016420 }
Eric Biederman90089602004-05-28 14:11:54 +000016421 /* Insert a cast if the types differ */
16422 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016423 if (val->op == OP_INTCONST) {
16424 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16425 tval->u.cval = val->u.cval;
16426 }
16427 else {
Eric Biederman90089602004-05-28 14:11:54 +000016428 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016429 use_triple(val, tval);
16430 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016431 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016432 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016433 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016434 use_triple(tval, ptr);
16435 }
16436 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016437 unuse_triple(var, ptr);
16438 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016439 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016440 }
16441 if (ptr->op == OP_PHI) {
16442 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016443 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016444 if (!triple_is_auto_var(state, var)) {
16445 internal_error(state, ptr, "phi references non auto var!");
16446 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016447 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016448 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016449 }
16450 last = ptr;
16451 }
16452 block->last = last;
16453
16454 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016455 for(edge = block->edges; edge; edge = edge->next) {
16456 fixup_block_phi_variables(state, stacks, block, edge->member);
16457 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016458 /* rename variables in the dominated nodes */
16459 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016460 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016461 }
16462 /* pop the renamed variable stack */
16463 last = block->first;
16464 done = 0;
16465 for(ptr = block->first; !done ; ptr = next) {
16466 next = ptr->next;
16467 if (ptr == block->last) {
16468 done = 1;
16469 }
16470 if (ptr->op == OP_WRITE) {
16471 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016472 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016473 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016474 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016475 release_triple(state, ptr);
16476 continue;
16477 }
16478 if (ptr->op == OP_PHI) {
16479 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016480 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016481 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016482 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016483 }
16484 last = ptr;
16485 }
16486 block->last = last;
16487}
16488
Eric Biederman83b991a2003-10-11 06:20:25 +000016489static void rename_variables(struct compile_state *state)
16490{
16491 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016492 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016493
16494 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016495 auto_vars = count_auto_vars(state);
16496 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016497
Eric Biederman90089602004-05-28 14:11:54 +000016498 /* Give each auto_var a stack */
16499 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016500
16501 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016502 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016503
Eric Biederman90089602004-05-28 14:11:54 +000016504 /* Remove the stacks from the auto_vars */
16505 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016506 xfree(stacks);
16507}
16508
Eric Biedermanb138ac82003-04-22 18:44:01 +000016509static void prune_block_variables(struct compile_state *state,
16510 struct block *block)
16511{
16512 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016513 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016514 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016515
Eric Biedermanb138ac82003-04-22 18:44:01 +000016516 done = 0;
16517 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016518 /* Be extremely careful I am deleting the list
16519 * as I walk trhough it.
16520 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016521 next = ptr->next;
16522 if (ptr == block->last) {
16523 done = 1;
16524 }
Eric Biederman90089602004-05-28 14:11:54 +000016525 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016526 struct triple_set *user, *next;
16527 for(user = ptr->use; user; user = next) {
16528 struct triple *use;
16529 next = user->next;
16530 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016531 if (MISC(ptr, 0) == user->member) {
16532 continue;
16533 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016534 if (use->op != OP_PHI) {
16535 internal_error(state, use, "decl still used");
16536 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016537 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016538 internal_error(state, use, "bad phi use of decl");
16539 }
16540 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016541 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016542 }
Eric Biederman90089602004-05-28 14:11:54 +000016543 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16544 /* Delete the adecl */
16545 release_triple(state, MISC(ptr, 0));
16546 /* And the piece */
16547 release_triple(state, ptr);
16548 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016549 continue;
16550 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016551 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016552 for(user = block->idominates; user; user = user->next) {
16553 prune_block_variables(state, user->member);
16554 }
16555}
16556
Eric Biederman66fe2222003-07-04 15:14:04 +000016557struct phi_triple {
16558 struct triple *phi;
16559 unsigned orig_id;
16560 int alive;
16561};
16562
16563static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16564{
16565 struct triple **slot;
16566 int zrhs, i;
16567 if (live[phi->id].alive) {
16568 return;
16569 }
16570 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016571 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016572 slot = &RHS(phi, 0);
16573 for(i = 0; i < zrhs; i++) {
16574 struct triple *used;
16575 used = slot[i];
16576 if (used && (used->op == OP_PHI)) {
16577 keep_phi(state, live, used);
16578 }
16579 }
16580}
16581
16582static void prune_unused_phis(struct compile_state *state)
16583{
16584 struct triple *first, *phi;
16585 struct phi_triple *live;
16586 int phis, i;
16587
Eric Biederman66fe2222003-07-04 15:14:04 +000016588 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016589 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016590
16591 /* Count how many phi functions I need to process */
16592 phis = 0;
16593 for(phi = first->next; phi != first; phi = phi->next) {
16594 if (phi->op == OP_PHI) {
16595 phis += 1;
16596 }
16597 }
16598
16599 /* Mark them all dead */
16600 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16601 phis = 0;
16602 for(phi = first->next; phi != first; phi = phi->next) {
16603 if (phi->op != OP_PHI) {
16604 continue;
16605 }
16606 live[phis].alive = 0;
16607 live[phis].orig_id = phi->id;
16608 live[phis].phi = phi;
16609 phi->id = phis;
16610 phis += 1;
16611 }
16612
16613 /* Mark phis alive that are used by non phis */
16614 for(i = 0; i < phis; i++) {
16615 struct triple_set *set;
16616 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16617 if (set->member->op != OP_PHI) {
16618 keep_phi(state, live, live[i].phi);
16619 break;
16620 }
16621 }
16622 }
16623
16624 /* Delete the extraneous phis */
16625 for(i = 0; i < phis; i++) {
16626 struct triple **slot;
16627 int zrhs, j;
16628 if (!live[i].alive) {
16629 release_triple(state, live[i].phi);
16630 continue;
16631 }
16632 phi = live[i].phi;
16633 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016634 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016635 for(j = 0; j < zrhs; j++) {
16636 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016637 struct triple *unknown;
16638 get_occurance(phi->occurance);
16639 unknown = flatten(state, state->global_pool,
16640 alloc_triple(state, OP_UNKNOWNVAL,
16641 phi->type, 0, 0, phi->occurance));
16642 slot[j] = unknown;
16643 use_triple(unknown, phi);
16644 transform_to_arch_instruction(state, unknown);
16645#if 0
16646 warning(state, phi, "variable not set at index %d on all paths to use", j);
16647#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016648 }
16649 }
16650 }
16651 xfree(live);
16652}
16653
Eric Biedermanb138ac82003-04-22 18:44:01 +000016654static void transform_to_ssa_form(struct compile_state *state)
16655{
16656 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016657 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016658
Eric Biederman90089602004-05-28 14:11:54 +000016659 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016660 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016661
Eric Biederman90089602004-05-28 14:11:54 +000016662 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016663}
16664
16665
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016666static void clear_vertex(
16667 struct compile_state *state, struct block *block, void *arg)
16668{
Eric Biederman83b991a2003-10-11 06:20:25 +000016669 /* Clear the current blocks vertex and the vertex of all
16670 * of the current blocks neighbors in case there are malformed
16671 * blocks with now instructions at this point.
16672 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016673 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016674 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016675 for(edge = block->edges; edge; edge = edge->next) {
16676 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016677 }
16678 for(user = block->use; user; user = user->next) {
16679 user->member->vertex = 0;
16680 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016681}
16682
16683static void mark_live_block(
16684 struct compile_state *state, struct block *block, int *next_vertex)
16685{
16686 /* See if this is a block that has not been marked */
16687 if (block->vertex != 0) {
16688 return;
16689 }
16690 block->vertex = *next_vertex;
16691 *next_vertex += 1;
16692 if (triple_is_branch(state, block->last)) {
16693 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016694 targ = triple_edge_targ(state, block->last, 0);
16695 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016696 if (!*targ) {
16697 continue;
16698 }
16699 if (!triple_stores_block(state, *targ)) {
16700 internal_error(state, 0, "bad targ");
16701 }
16702 mark_live_block(state, (*targ)->u.block, next_vertex);
16703 }
Eric Biederman90089602004-05-28 14:11:54 +000016704 /* Ensure the last block of a function remains alive */
16705 if (triple_is_call(state, block->last)) {
16706 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16707 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016708 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016709 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016710 struct triple *ins;
16711 ins = block->last->next;
16712 if (!triple_stores_block(state, ins)) {
16713 internal_error(state, 0, "bad block start");
16714 }
16715 mark_live_block(state, ins->u.block, next_vertex);
16716 }
16717}
16718
Eric Biedermanb138ac82003-04-22 18:44:01 +000016719static void transform_from_ssa_form(struct compile_state *state)
16720{
16721 /* To get out of ssa form we insert moves on the incoming
16722 * edges to blocks containting phi functions.
16723 */
16724 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016725 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016726 int next_vertex;
16727
16728 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016729 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016730 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016731 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016732
16733 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016734 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016735 for(phi = first->next; phi != first ; phi = next) {
16736 struct block_set *set;
16737 struct block *block;
16738 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016739 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016740 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016741 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016742 next = phi->next;
16743 if (phi->op != OP_PHI) {
16744 continue;
16745 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016746
Eric Biedermanb138ac82003-04-22 18:44:01 +000016747 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016748 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016749
Eric Biederman83b991a2003-10-11 06:20:25 +000016750 /* If this phi is in a dead block just forget it */
16751 if (block->vertex == 0) {
16752 release_triple(state, phi);
16753 continue;
16754 }
16755
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016756 /* Forget uses from code in dead blocks */
16757 for(use = phi->use; use; use = use_next) {
16758 struct block *ublock;
16759 struct triple **expr;
16760 use_next = use->next;
16761 ublock = block_of_triple(state, use->member);
16762 if ((use->member == phi) || (ublock->vertex != 0)) {
16763 continue;
16764 }
16765 expr = triple_rhs(state, use->member, 0);
16766 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16767 if (*expr == phi) {
16768 *expr = 0;
16769 }
16770 }
16771 unuse_triple(phi, use->member);
16772 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016773 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016774 if (registers_of(state, phi->type) != 1) {
16775 internal_error(state, phi, "phi->type does not fit in a single register!");
16776 }
16777 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16778 var = var->next; /* point at the var */
16779
Eric Biederman83b991a2003-10-11 06:20:25 +000016780 /* Replaces use of phi with var */
16781 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016782
Eric Biederman90089602004-05-28 14:11:54 +000016783 /* Count the readers */
16784 readers = 0;
16785 for(use = var->use; use; use = use->next) {
16786 if (use->member != MISC(var, 0)) {
16787 readers++;
16788 }
16789 }
16790
Eric Biedermanb138ac82003-04-22 18:44:01 +000016791 /* Walk all of the incoming edges/blocks and insert moves.
16792 */
Eric Biederman90089602004-05-28 14:11:54 +000016793 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016794 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016795 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016796 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016797 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016798 eblock = set->member;
16799 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016800 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016801 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016802 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016803
Eric Biederman83b991a2003-10-11 06:20:25 +000016804 /* If we don't have a value that belongs in an OP_WRITE
16805 * continue on.
16806 */
Eric Biederman90089602004-05-28 14:11:54 +000016807 if (!val || (val == &unknown_triple) || (val == phi)
16808 || (vblock && (vblock->vertex == 0))) {
16809 continue;
16810 }
16811 /* If the value should never occur error */
16812 if (!vblock) {
16813 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016814 continue;
16815 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016816
16817 /* If the value occurs in a dead block see if a replacement
16818 * block can be found.
16819 */
16820 while(eblock && (eblock->vertex == 0)) {
16821 eblock = eblock->idom;
16822 }
16823 /* If not continue on with the next value. */
16824 if (!eblock || (eblock->vertex == 0)) {
16825 continue;
16826 }
16827
16828 /* If we have an empty incoming block ignore it. */
16829 if (!eblock->first) {
16830 internal_error(state, 0, "empty block?");
16831 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016832
Eric Biederman530b5192003-07-01 10:05:30 +000016833 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016834 /* Walk through the edge block backwards to find an
16835 * appropriate location for the OP_WRITE.
16836 */
16837 for(base = eblock->last; base != eblock->first; base = base->prev) {
16838 struct triple **expr;
16839 if (base->op == OP_PIECE) {
16840 base = MISC(base, 0);
16841 }
16842 if ((base == var) || (base == val)) {
16843 goto out;
16844 }
16845 expr = triple_lhs(state, base, 0);
16846 for(; expr; expr = triple_lhs(state, base, expr)) {
16847 if ((*expr) == val) {
16848 goto out;
16849 }
16850 }
16851 expr = triple_rhs(state, base, 0);
16852 for(; expr; expr = triple_rhs(state, base, expr)) {
16853 if ((*expr) == var) {
16854 goto out;
16855 }
16856 }
Eric Biederman530b5192003-07-01 10:05:30 +000016857 }
Eric Biederman90089602004-05-28 14:11:54 +000016858 out:
16859 if (triple_is_branch(state, base)) {
16860 internal_error(state, base,
16861 "Could not insert write to phi");
16862 }
16863 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016864 use_triple(val, move);
16865 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016866 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016867 }
Eric Biederman90089602004-05-28 14:11:54 +000016868 if (!writers && readers) {
16869 internal_error(state, var, "no value written to in use phi?");
16870 }
16871 /* If var is not used free it */
16872 if (!writers) {
16873 release_triple(state, MISC(var, 0));
16874 release_triple(state, var);
16875 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016876 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016877 release_triple(state, phi);
16878 }
16879
Eric Biederman83b991a2003-10-11 06:20:25 +000016880 /* Walk all of the operations to find the adecls */
16881 for(var = first->next; var != first ; var = var->next) {
16882 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016883 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016884 continue;
16885 }
16886
16887 /* Walk through all of the rhs uses of var and
16888 * replace them with read of var.
16889 */
16890 for(use = var->use; use; use = use_next) {
16891 struct triple *read, *user;
16892 struct triple **slot;
16893 int zrhs, i, used;
16894 use_next = use->next;
16895 user = use->member;
16896
16897 /* Generate a read of var */
16898 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16899 use_triple(var, read);
16900
16901 /* Find the rhs uses and see if they need to be replaced */
16902 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016903 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016904 slot = &RHS(user, 0);
16905 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016906 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016907 slot[i] = read;
16908 used = 1;
16909 }
16910 }
16911 /* If we did use it cleanup the uses */
16912 if (used) {
16913 unuse_triple(var, user);
16914 use_triple(read, user);
16915 }
16916 /* If we didn't use it release the extra triple */
16917 else {
16918 release_triple(state, read);
16919 }
16920 }
16921 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016922}
16923
Eric Biederman5ade04a2003-10-22 04:03:46 +000016924#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016925 FILE *fp = state->dbgout; \
16926 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Eric Biederman5ade04a2003-10-22 04:03:46 +000016927 }
16928
Eric Biederman83b991a2003-10-11 06:20:25 +000016929static void rebuild_ssa_form(struct compile_state *state)
16930{
16931HI();
16932 transform_from_ssa_form(state);
16933HI();
Eric Biederman90089602004-05-28 14:11:54 +000016934 state->bb.first = state->first;
16935 free_basic_blocks(state, &state->bb);
16936 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016937HI();
16938 insert_phi_operations(state);
16939HI();
16940 rename_variables(state);
16941HI();
16942
Eric Biederman90089602004-05-28 14:11:54 +000016943 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016944HI();
16945 prune_unused_phis(state);
16946HI();
16947}
16948#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016949
16950/*
16951 * Register conflict resolution
16952 * =========================================================
16953 */
16954
16955static struct reg_info find_def_color(
16956 struct compile_state *state, struct triple *def)
16957{
16958 struct triple_set *set;
16959 struct reg_info info;
16960 info.reg = REG_UNSET;
16961 info.regcm = 0;
16962 if (!triple_is_def(state, def)) {
16963 return info;
16964 }
16965 info = arch_reg_lhs(state, def, 0);
16966 if (info.reg >= MAX_REGISTERS) {
16967 info.reg = REG_UNSET;
16968 }
16969 for(set = def->use; set; set = set->next) {
16970 struct reg_info tinfo;
16971 int i;
16972 i = find_rhs_use(state, set->member, def);
16973 if (i < 0) {
16974 continue;
16975 }
16976 tinfo = arch_reg_rhs(state, set->member, i);
16977 if (tinfo.reg >= MAX_REGISTERS) {
16978 tinfo.reg = REG_UNSET;
16979 }
16980 if ((tinfo.reg != REG_UNSET) &&
16981 (info.reg != REG_UNSET) &&
16982 (tinfo.reg != info.reg)) {
16983 internal_error(state, def, "register conflict");
16984 }
16985 if ((info.regcm & tinfo.regcm) == 0) {
16986 internal_error(state, def, "regcm conflict %x & %x == 0",
16987 info.regcm, tinfo.regcm);
16988 }
16989 if (info.reg == REG_UNSET) {
16990 info.reg = tinfo.reg;
16991 }
16992 info.regcm &= tinfo.regcm;
16993 }
16994 if (info.reg >= MAX_REGISTERS) {
16995 internal_error(state, def, "register out of range");
16996 }
16997 return info;
16998}
16999
17000static struct reg_info find_lhs_pre_color(
17001 struct compile_state *state, struct triple *ins, int index)
17002{
17003 struct reg_info info;
17004 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000017005 zrhs = ins->rhs;
17006 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017007 if (!zlhs && triple_is_def(state, ins)) {
17008 zlhs = 1;
17009 }
17010 if (index >= zlhs) {
17011 internal_error(state, ins, "Bad lhs %d", index);
17012 }
17013 info = arch_reg_lhs(state, ins, index);
17014 for(i = 0; i < zrhs; i++) {
17015 struct reg_info rinfo;
17016 rinfo = arch_reg_rhs(state, ins, i);
17017 if ((info.reg == rinfo.reg) &&
17018 (rinfo.reg >= MAX_REGISTERS)) {
17019 struct reg_info tinfo;
17020 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17021 info.reg = tinfo.reg;
17022 info.regcm &= tinfo.regcm;
17023 break;
17024 }
17025 }
17026 if (info.reg >= MAX_REGISTERS) {
17027 info.reg = REG_UNSET;
17028 }
17029 return info;
17030}
17031
17032static struct reg_info find_rhs_post_color(
17033 struct compile_state *state, struct triple *ins, int index);
17034
17035static struct reg_info find_lhs_post_color(
17036 struct compile_state *state, struct triple *ins, int index)
17037{
17038 struct triple_set *set;
17039 struct reg_info info;
17040 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017041#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017042 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017043 ins, index);
17044#endif
17045 if ((index == 0) && triple_is_def(state, ins)) {
17046 lhs = ins;
17047 }
Eric Biederman90089602004-05-28 14:11:54 +000017048 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017049 lhs = LHS(ins, index);
17050 }
17051 else {
17052 internal_error(state, ins, "Bad lhs %d", index);
17053 lhs = 0;
17054 }
17055 info = arch_reg_lhs(state, ins, index);
17056 if (info.reg >= MAX_REGISTERS) {
17057 info.reg = REG_UNSET;
17058 }
17059 for(set = lhs->use; set; set = set->next) {
17060 struct reg_info rinfo;
17061 struct triple *user;
17062 int zrhs, i;
17063 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017064 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017065 for(i = 0; i < zrhs; i++) {
17066 if (RHS(user, i) != lhs) {
17067 continue;
17068 }
17069 rinfo = find_rhs_post_color(state, user, i);
17070 if ((info.reg != REG_UNSET) &&
17071 (rinfo.reg != REG_UNSET) &&
17072 (info.reg != rinfo.reg)) {
17073 internal_error(state, ins, "register conflict");
17074 }
17075 if ((info.regcm & rinfo.regcm) == 0) {
17076 internal_error(state, ins, "regcm conflict %x & %x == 0",
17077 info.regcm, rinfo.regcm);
17078 }
17079 if (info.reg == REG_UNSET) {
17080 info.reg = rinfo.reg;
17081 }
17082 info.regcm &= rinfo.regcm;
17083 }
17084 }
Eric Biederman530b5192003-07-01 10:05:30 +000017085#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017086 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017087 ins, index, info.reg, info.regcm);
17088#endif
17089 return info;
17090}
17091
17092static struct reg_info find_rhs_post_color(
17093 struct compile_state *state, struct triple *ins, int index)
17094{
17095 struct reg_info info, rinfo;
17096 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017097#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017098 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017099 ins, index);
17100#endif
17101 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017102 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017103 if (!zlhs && triple_is_def(state, ins)) {
17104 zlhs = 1;
17105 }
17106 info = rinfo;
17107 if (info.reg >= MAX_REGISTERS) {
17108 info.reg = REG_UNSET;
17109 }
17110 for(i = 0; i < zlhs; i++) {
17111 struct reg_info linfo;
17112 linfo = arch_reg_lhs(state, ins, i);
17113 if ((linfo.reg == rinfo.reg) &&
17114 (linfo.reg >= MAX_REGISTERS)) {
17115 struct reg_info tinfo;
17116 tinfo = find_lhs_post_color(state, ins, i);
17117 if (tinfo.reg >= MAX_REGISTERS) {
17118 tinfo.reg = REG_UNSET;
17119 }
Eric Biederman530b5192003-07-01 10:05:30 +000017120 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017121 info.regcm &= tinfo.regcm;
17122 if (info.reg != REG_UNSET) {
17123 internal_error(state, ins, "register conflict");
17124 }
17125 if (info.regcm == 0) {
17126 internal_error(state, ins, "regcm conflict");
17127 }
17128 info.reg = tinfo.reg;
17129 }
17130 }
Eric Biederman530b5192003-07-01 10:05:30 +000017131#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017132 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017133 ins, index, info.reg, info.regcm);
17134#endif
17135 return info;
17136}
17137
17138static struct reg_info find_lhs_color(
17139 struct compile_state *state, struct triple *ins, int index)
17140{
17141 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017142#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017143 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017144 ins, index);
17145#endif
17146 pre = find_lhs_pre_color(state, ins, index);
17147 post = find_lhs_post_color(state, ins, index);
17148 if ((pre.reg != post.reg) &&
17149 (pre.reg != REG_UNSET) &&
17150 (post.reg != REG_UNSET)) {
17151 internal_error(state, ins, "register conflict");
17152 }
17153 info.regcm = pre.regcm & post.regcm;
17154 info.reg = pre.reg;
17155 if (info.reg == REG_UNSET) {
17156 info.reg = post.reg;
17157 }
Eric Biederman530b5192003-07-01 10:05:30 +000017158#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017159 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017160 ins, index, info.reg, info.regcm,
17161 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017162#endif
17163 return info;
17164}
17165
17166static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17167{
17168 struct triple_set *entry, *next;
17169 struct triple *out;
17170 struct reg_info info, rinfo;
17171
17172 info = arch_reg_lhs(state, ins, 0);
17173 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17174 use_triple(RHS(out, 0), out);
17175 /* Get the users of ins to use out instead */
17176 for(entry = ins->use; entry; entry = next) {
17177 int i;
17178 next = entry->next;
17179 if (entry->member == out) {
17180 continue;
17181 }
17182 i = find_rhs_use(state, entry->member, ins);
17183 if (i < 0) {
17184 continue;
17185 }
17186 rinfo = arch_reg_rhs(state, entry->member, i);
17187 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17188 continue;
17189 }
17190 replace_rhs_use(state, ins, out, entry->member);
17191 }
17192 transform_to_arch_instruction(state, out);
17193 return out;
17194}
17195
Eric Biedermand1ea5392003-06-28 06:49:45 +000017196static struct triple *typed_pre_copy(
17197 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017198{
17199 /* Carefully insert enough operations so that I can
17200 * enter any operation with a GPR32.
17201 */
17202 struct triple *in;
17203 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017204 unsigned classes;
17205 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017206 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017207 if (ins->op == OP_PHI) {
17208 internal_error(state, ins, "pre_copy on a phi?");
17209 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017210 classes = arch_type_to_regcm(state, type);
17211 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017212 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017213 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017214 FILE *fp = state->errout;
17215 fprintf(fp, "src_type: ");
17216 name_of(fp, ins->type);
17217 fprintf(fp, "\ndst_type: ");
17218 name_of(fp, type);
17219 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017220 internal_error(state, ins, "pre_copy with no register classes");
17221 }
Eric Biederman90089602004-05-28 14:11:54 +000017222 op = OP_COPY;
17223 if (!equiv_types(type, (*expr)->type)) {
17224 op = OP_CONVERT;
17225 }
17226 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017227 unuse_triple(*expr, ins);
17228 *expr = in;
17229 use_triple(RHS(in, 0), in);
17230 use_triple(in, ins);
17231 transform_to_arch_instruction(state, in);
17232 return in;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017233
17234}
17235static struct triple *pre_copy(
17236 struct compile_state *state, struct triple *ins, int index)
17237{
17238 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017239}
17240
17241
Eric Biedermanb138ac82003-04-22 18:44:01 +000017242static void insert_copies_to_phi(struct compile_state *state)
17243{
17244 /* To get out of ssa form we insert moves on the incoming
17245 * edges to blocks containting phi functions.
17246 */
17247 struct triple *first;
17248 struct triple *phi;
17249
17250 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017251 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017252 for(phi = first->next; phi != first ; phi = phi->next) {
17253 struct block_set *set;
17254 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017255 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017256 int edge;
17257 if (phi->op != OP_PHI) {
17258 continue;
17259 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017260 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017261 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017262 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017263 /* Phi's that feed into mandatory live range joins
17264 * cause nasty complications. Insert a copy of
17265 * the phi value so I never have to deal with
17266 * that in the rest of the code.
17267 */
17268 copy = post_copy(state, phi);
17269 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017270 /* Walk all of the incoming edges/blocks and insert moves.
17271 */
17272 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17273 struct block *eblock;
17274 struct triple *move;
17275 struct triple *val;
17276 struct triple *ptr;
17277 eblock = set->member;
17278 val = slot[edge];
17279
17280 if (val == phi) {
17281 continue;
17282 }
17283
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017284 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017285 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017286 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017287 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017288 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017289 use_triple(val, move);
17290
17291 slot[edge] = move;
17292 unuse_triple(val, phi);
17293 use_triple(move, phi);
17294
Eric Biederman66fe2222003-07-04 15:14:04 +000017295 /* Walk up the dominator tree until I have found the appropriate block */
17296 while(eblock && !tdominates(state, val, eblock->last)) {
17297 eblock = eblock->idom;
17298 }
17299 if (!eblock) {
17300 internal_error(state, phi, "Cannot find block dominated by %p",
17301 val);
17302 }
17303
Eric Biedermanb138ac82003-04-22 18:44:01 +000017304 /* Walk through the block backwards to find
17305 * an appropriate location for the OP_COPY.
17306 */
17307 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17308 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017309 if (ptr->op == OP_PIECE) {
17310 ptr = MISC(ptr, 0);
17311 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017312 if ((ptr == phi) || (ptr == val)) {
17313 goto out;
17314 }
Eric Biederman90089602004-05-28 14:11:54 +000017315 expr = triple_lhs(state, ptr, 0);
17316 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17317 if ((*expr) == val) {
17318 goto out;
17319 }
17320 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017321 expr = triple_rhs(state, ptr, 0);
17322 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17323 if ((*expr) == phi) {
17324 goto out;
17325 }
17326 }
17327 }
17328 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017329 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017330 internal_error(state, ptr,
17331 "Could not insert write to phi");
17332 }
Eric Biederman90089602004-05-28 14:11:54 +000017333 insert_triple(state, after_lhs(state, ptr), move);
17334 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017335 eblock->last = move;
17336 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017337 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017338 }
17339 }
Eric Biederman90089602004-05-28 14:11:54 +000017340 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017341}
17342
Eric Biederman90089602004-05-28 14:11:54 +000017343struct triple_reg_set;
17344struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017345
Eric Biedermanb138ac82003-04-22 18:44:01 +000017346
17347static int do_triple_set(struct triple_reg_set **head,
17348 struct triple *member, struct triple *new_member)
17349{
17350 struct triple_reg_set **ptr, *new;
17351 if (!member)
17352 return 0;
17353 ptr = head;
17354 while(*ptr) {
17355 if ((*ptr)->member == member) {
17356 return 0;
17357 }
17358 ptr = &(*ptr)->next;
17359 }
17360 new = xcmalloc(sizeof(*new), "triple_set");
17361 new->member = member;
17362 new->new = new_member;
17363 new->next = *head;
17364 *head = new;
17365 return 1;
17366}
17367
17368static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17369{
17370 struct triple_reg_set *entry, **ptr;
17371 ptr = head;
17372 while(*ptr) {
17373 entry = *ptr;
17374 if (entry->member == member) {
17375 *ptr = entry->next;
17376 xfree(entry);
17377 return;
17378 }
17379 else {
17380 ptr = &entry->next;
17381 }
17382 }
17383}
17384
17385static int in_triple(struct reg_block *rb, struct triple *in)
17386{
17387 return do_triple_set(&rb->in, in, 0);
17388}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017389
17390#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017391static void unin_triple(struct reg_block *rb, struct triple *unin)
17392{
17393 do_triple_unset(&rb->in, unin);
17394}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017395#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017396
17397static int out_triple(struct reg_block *rb, struct triple *out)
17398{
17399 return do_triple_set(&rb->out, out, 0);
17400}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017401#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017402static void unout_triple(struct reg_block *rb, struct triple *unout)
17403{
17404 do_triple_unset(&rb->out, unout);
17405}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017406#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017407
17408static int initialize_regblock(struct reg_block *blocks,
17409 struct block *block, int vertex)
17410{
17411 struct block_set *user;
17412 if (!block || (blocks[block->vertex].block == block)) {
17413 return vertex;
17414 }
17415 vertex += 1;
17416 /* Renumber the blocks in a convinient fashion */
17417 block->vertex = vertex;
17418 blocks[vertex].block = block;
17419 blocks[vertex].vertex = vertex;
17420 for(user = block->use; user; user = user->next) {
17421 vertex = initialize_regblock(blocks, user->member, vertex);
17422 }
17423 return vertex;
17424}
17425
Eric Biederman90089602004-05-28 14:11:54 +000017426static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17427{
17428/* Part to piece is a best attempt and it cannot be correct all by
17429 * itself. If various values are read as different sizes in different
17430 * parts of the code this function cannot work. Or rather it cannot
17431 * work in conjunction with compute_variable_liftimes. As the
17432 * analysis will get confused.
17433 */
17434 struct triple *base;
17435 unsigned reg;
17436 if (!is_lvalue(state, ins)) {
17437 return ins;
17438 }
17439 base = 0;
17440 reg = 0;
17441 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17442 base = MISC(ins, 0);
17443 switch(ins->op) {
17444 case OP_INDEX:
17445 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17446 break;
17447 case OP_DOT:
17448 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17449 break;
17450 default:
17451 internal_error(state, ins, "unhandled part");
17452 break;
17453 }
17454 ins = base;
17455 }
17456 if (base) {
17457 if (reg > base->lhs) {
17458 internal_error(state, base, "part out of range?");
17459 }
17460 ins = LHS(base, reg);
17461 }
17462 return ins;
17463}
17464
17465static int this_def(struct compile_state *state,
17466 struct triple *ins, struct triple *other)
17467{
17468 if (ins == other) {
17469 return 1;
17470 }
17471 if (ins->op == OP_WRITE) {
17472 ins = part_to_piece(state, MISC(ins, 0));
17473 }
17474 return ins == other;
17475}
17476
Eric Biedermanb138ac82003-04-22 18:44:01 +000017477static int phi_in(struct compile_state *state, struct reg_block *blocks,
17478 struct reg_block *rb, struct block *suc)
17479{
17480 /* Read the conditional input set of a successor block
17481 * (i.e. the input to the phi nodes) and place it in the
17482 * current blocks output set.
17483 */
17484 struct block_set *set;
17485 struct triple *ptr;
17486 int edge;
17487 int done, change;
17488 change = 0;
17489 /* Find the edge I am coming in on */
17490 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17491 if (set->member == rb->block) {
17492 break;
17493 }
17494 }
17495 if (!set) {
17496 internal_error(state, 0, "Not coming on a control edge?");
17497 }
17498 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17499 struct triple **slot, *expr, *ptr2;
17500 int out_change, done2;
17501 done = (ptr == suc->last);
17502 if (ptr->op != OP_PHI) {
17503 continue;
17504 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017505 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017506 expr = slot[edge];
17507 out_change = out_triple(rb, expr);
17508 if (!out_change) {
17509 continue;
17510 }
17511 /* If we don't define the variable also plast it
17512 * in the current blocks input set.
17513 */
17514 ptr2 = rb->block->first;
17515 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017516 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017517 break;
17518 }
17519 done2 = (ptr2 == rb->block->last);
17520 }
17521 if (!done2) {
17522 continue;
17523 }
17524 change |= in_triple(rb, expr);
17525 }
17526 return change;
17527}
17528
17529static int reg_in(struct compile_state *state, struct reg_block *blocks,
17530 struct reg_block *rb, struct block *suc)
17531{
17532 struct triple_reg_set *in_set;
17533 int change;
17534 change = 0;
17535 /* Read the input set of a successor block
17536 * and place it in the current blocks output set.
17537 */
17538 in_set = blocks[suc->vertex].in;
17539 for(; in_set; in_set = in_set->next) {
17540 int out_change, done;
17541 struct triple *first, *last, *ptr;
17542 out_change = out_triple(rb, in_set->member);
17543 if (!out_change) {
17544 continue;
17545 }
17546 /* If we don't define the variable also place it
17547 * in the current blocks input set.
17548 */
17549 first = rb->block->first;
17550 last = rb->block->last;
17551 done = 0;
17552 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017553 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017554 break;
17555 }
17556 done = (ptr == last);
17557 }
17558 if (!done) {
17559 continue;
17560 }
17561 change |= in_triple(rb, in_set->member);
17562 }
17563 change |= phi_in(state, blocks, rb, suc);
17564 return change;
17565}
17566
Eric Biedermanb138ac82003-04-22 18:44:01 +000017567static int use_in(struct compile_state *state, struct reg_block *rb)
17568{
17569 /* Find the variables we use but don't define and add
17570 * it to the current blocks input set.
17571 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017572#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017573#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017574#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017575 struct block *block;
17576 struct triple *ptr;
17577 int done;
17578 int change;
17579 block = rb->block;
17580 change = 0;
17581 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17582 struct triple **expr;
17583 done = (ptr == block->first);
17584 /* The variable a phi function uses depends on the
17585 * control flow, and is handled in phi_in, not
17586 * here.
17587 */
17588 if (ptr->op == OP_PHI) {
17589 continue;
17590 }
17591 expr = triple_rhs(state, ptr, 0);
17592 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17593 struct triple *rhs, *test;
17594 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017595 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017596 if (!rhs) {
17597 continue;
17598 }
Eric Biederman90089602004-05-28 14:11:54 +000017599
17600 /* See if rhs is defined in this block.
17601 * A write counts as a definition.
17602 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017603 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17604 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017605 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017606 rhs = 0;
17607 break;
17608 }
17609 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017610 /* If I still have a valid rhs add it to in */
17611 change |= in_triple(rb, rhs);
17612 }
17613 }
17614 return change;
17615}
17616
17617static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017618 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017619{
17620 struct reg_block *blocks;
17621 int change;
17622 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017623 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17624 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017625 do {
17626 int i;
17627 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017628 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017629 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017630 struct reg_block *rb;
17631 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017632 /* Add the all successor's input set to in */
17633 for(edge = rb->block->edges; edge; edge = edge->next) {
17634 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017635 }
17636 /* Add use to in... */
17637 change |= use_in(state, rb);
17638 }
17639 } while(change);
17640 return blocks;
17641}
17642
Eric Biederman90089602004-05-28 14:11:54 +000017643static void free_variable_lifetimes(struct compile_state *state,
17644 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017645{
17646 int i;
17647 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017648 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017649 struct triple_reg_set *entry, *next;
17650 struct reg_block *rb;
17651 rb = &blocks[i];
17652 for(entry = rb->in; entry ; entry = next) {
17653 next = entry->next;
17654 do_triple_unset(&rb->in, entry->member);
17655 }
17656 for(entry = rb->out; entry; entry = next) {
17657 next = entry->next;
17658 do_triple_unset(&rb->out, entry->member);
17659 }
17660 }
17661 xfree(blocks);
17662
17663}
17664
Eric Biedermanf96a8102003-06-16 16:57:34 +000017665typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000017666 struct compile_state *state,
17667 struct reg_block *blocks, struct triple_reg_set *live,
17668 struct reg_block *rb, struct triple *ins, void *arg);
17669
17670static void walk_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017671 struct basic_blocks *bb, struct reg_block *blocks,
17672 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017673{
17674 int i;
17675
Eric Biederman90089602004-05-28 14:11:54 +000017676 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017677 struct triple_reg_set *live;
17678 struct triple_reg_set *entry, *next;
17679 struct triple *ptr, *prev;
17680 struct reg_block *rb;
17681 struct block *block;
17682 int done;
17683
17684 /* Get the blocks */
17685 rb = &blocks[i];
17686 block = rb->block;
17687
17688 /* Copy out into live */
17689 live = 0;
17690 for(entry = rb->out; entry; entry = next) {
17691 next = entry->next;
17692 do_triple_set(&live, entry->member, entry->new);
17693 }
17694 /* Walk through the basic block calculating live */
17695 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017696 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017697
17698 prev = ptr->prev;
17699 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017700
17701 /* Ensure the current definition is in live */
17702 if (triple_is_def(state, ptr)) {
17703 do_triple_set(&live, ptr, 0);
17704 }
17705
17706 /* Inform the callback function of what is
17707 * going on.
17708 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017709 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017710
17711 /* Remove the current definition from live */
17712 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017713
Eric Biedermanb138ac82003-04-22 18:44:01 +000017714 /* Add the current uses to live.
17715 *
17716 * It is safe to skip phi functions because they do
17717 * not have any block local uses, and the block
17718 * output sets already properly account for what
17719 * control flow depedent uses phi functions do have.
17720 */
17721 if (ptr->op == OP_PHI) {
17722 continue;
17723 }
17724 expr = triple_rhs(state, ptr, 0);
17725 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17726 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017727 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017728 continue;
17729 }
17730 do_triple_set(&live, *expr, 0);
17731 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017732 }
17733 /* Free live */
17734 for(entry = live; entry; entry = next) {
17735 next = entry->next;
17736 do_triple_unset(&live, entry->member);
17737 }
17738 }
17739}
17740
Eric Biederman90089602004-05-28 14:11:54 +000017741struct print_live_variable_info {
17742 struct reg_block *rb;
17743 FILE *fp;
17744};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017745#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017746static void print_live_variables_block(
17747 struct compile_state *state, struct block *block, void *arg)
17748
17749{
17750 struct print_live_variable_info *info = arg;
17751 struct block_set *edge;
17752 FILE *fp = info->fp;
17753 struct reg_block *rb;
17754 struct triple *ptr;
17755 int phi_present;
17756 int done;
17757 rb = &info->rb[block->vertex];
17758
17759 fprintf(fp, "\nblock: %p (%d),",
17760 block, block->vertex);
17761 for(edge = block->edges; edge; edge = edge->next) {
17762 fprintf(fp, " %p<-%p",
17763 edge->member,
17764 edge->member && edge->member->use?edge->member->use->member : 0);
17765 }
17766 fprintf(fp, "\n");
17767 if (rb->in) {
17768 struct triple_reg_set *in_set;
17769 fprintf(fp, " in:");
17770 for(in_set = rb->in; in_set; in_set = in_set->next) {
17771 fprintf(fp, " %-10p", in_set->member);
17772 }
17773 fprintf(fp, "\n");
17774 }
17775 phi_present = 0;
17776 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17777 done = (ptr == block->last);
17778 if (ptr->op == OP_PHI) {
17779 phi_present = 1;
17780 break;
17781 }
17782 }
17783 if (phi_present) {
17784 int edge;
17785 for(edge = 0; edge < block->users; edge++) {
17786 fprintf(fp, " in(%d):", edge);
17787 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17788 struct triple **slot;
17789 done = (ptr == block->last);
17790 if (ptr->op != OP_PHI) {
17791 continue;
17792 }
17793 slot = &RHS(ptr, 0);
17794 fprintf(fp, " %-10p", slot[edge]);
17795 }
17796 fprintf(fp, "\n");
17797 }
17798 }
17799 if (block->first->op == OP_LABEL) {
17800 fprintf(fp, "%p:\n", block->first);
17801 }
17802 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17803 done = (ptr == block->last);
17804 display_triple(fp, ptr);
17805 }
17806 if (rb->out) {
17807 struct triple_reg_set *out_set;
17808 fprintf(fp, " out:");
17809 for(out_set = rb->out; out_set; out_set = out_set->next) {
17810 fprintf(fp, " %-10p", out_set->member);
17811 }
17812 fprintf(fp, "\n");
17813 }
17814 fprintf(fp, "\n");
17815}
17816
17817static void print_live_variables(struct compile_state *state,
17818 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17819{
17820 struct print_live_variable_info info;
17821 info.rb = rb;
17822 info.fp = fp;
17823 fprintf(fp, "\nlive variables by block\n");
17824 walk_blocks(state, bb, print_live_variables_block, &info);
17825
17826}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017827#endif
Eric Biederman90089602004-05-28 14:11:54 +000017828
Eric Biedermanb138ac82003-04-22 18:44:01 +000017829static int count_triples(struct compile_state *state)
17830{
17831 struct triple *first, *ins;
17832 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017833 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017834 ins = first;
17835 do {
17836 triples++;
17837 ins = ins->next;
17838 } while (ins != first);
17839 return triples;
17840}
Eric Biederman66fe2222003-07-04 15:14:04 +000017841
17842
Eric Biedermanb138ac82003-04-22 18:44:01 +000017843struct dead_triple {
17844 struct triple *triple;
17845 struct dead_triple *work_next;
17846 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017847 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017848 int flags;
17849#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017850#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017851};
17852
Eric Biederman90089602004-05-28 14:11:54 +000017853static void print_dead_triples(struct compile_state *state,
17854 struct dead_triple *dtriple)
17855{
17856 struct triple *first, *ins;
17857 struct dead_triple *dt;
17858 FILE *fp;
17859 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17860 return;
17861 }
17862 fp = state->dbgout;
17863 fprintf(fp, "--------------- dtriples ---------------\n");
17864 first = state->first;
17865 ins = first;
17866 do {
17867 dt = &dtriple[ins->id];
17868 if ((ins->op == OP_LABEL) && (ins->use)) {
17869 fprintf(fp, "\n%p:\n", ins);
17870 }
17871 fprintf(fp, "%c",
17872 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17873 display_triple(fp, ins);
17874 if (triple_is_branch(state, ins)) {
17875 fprintf(fp, "\n");
17876 }
17877 ins = ins->next;
17878 } while(ins != first);
17879 fprintf(fp, "\n");
17880}
17881
Eric Biedermanb138ac82003-04-22 18:44:01 +000017882
17883static void awaken(
17884 struct compile_state *state,
17885 struct dead_triple *dtriple, struct triple **expr,
17886 struct dead_triple ***work_list_tail)
17887{
17888 struct triple *triple;
17889 struct dead_triple *dt;
17890 if (!expr) {
17891 return;
17892 }
17893 triple = *expr;
17894 if (!triple) {
17895 return;
17896 }
17897 if (triple->id <= 0) {
17898 internal_error(state, triple, "bad triple id: %d",
17899 triple->id);
17900 }
17901 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017902 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017903 return;
17904 }
17905 dt = &dtriple[triple->id];
17906 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17907 dt->flags |= TRIPLE_FLAG_ALIVE;
17908 if (!dt->work_next) {
17909 **work_list_tail = dt;
17910 *work_list_tail = &dt->work_next;
17911 }
17912 }
17913}
17914
17915static void eliminate_inefectual_code(struct compile_state *state)
17916{
17917 struct block *block;
17918 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17919 int triples, i;
Eric Biederman83b991a2003-10-11 06:20:25 +000017920 struct triple *first, *final, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017921
Eric Biederman5ade04a2003-10-22 04:03:46 +000017922 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17923 return;
17924 }
17925
Eric Biedermanb138ac82003-04-22 18:44:01 +000017926 /* Setup the work list */
17927 work_list = 0;
17928 work_list_tail = &work_list;
17929
Eric Biederman83b991a2003-10-11 06:20:25 +000017930 first = state->first;
17931 final = state->first->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017932
17933 /* Count how many triples I have */
17934 triples = count_triples(state);
17935
17936 /* Now put then in an array and mark all of the triples dead */
17937 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
17938
17939 ins = first;
17940 i = 1;
17941 block = 0;
17942 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017943 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017944 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017945 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017946 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017947 ins->id = i;
17948 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017949 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017950 awaken(state, dtriple, &ins, &work_list_tail);
17951 }
17952 i++;
17953 ins = ins->next;
17954 } while(ins != first);
17955 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017956 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017957 struct dead_triple *dt;
17958 struct block_set *user;
17959 struct triple **expr;
17960 dt = work_list;
17961 work_list = dt->work_next;
17962 if (!work_list) {
17963 work_list_tail = &work_list;
17964 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017965 /* Make certain the block the current instruction is in lives */
17966 block = block_of_triple(state, dt->triple);
17967 awaken(state, dtriple, &block->first, &work_list_tail);
17968 if (triple_is_branch(state, block->last)) {
17969 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017970 } else {
17971 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017972 }
17973
Eric Biedermanb138ac82003-04-22 18:44:01 +000017974 /* Wake up the data depencencies of this triple */
17975 expr = 0;
17976 do {
17977 expr = triple_rhs(state, dt->triple, expr);
17978 awaken(state, dtriple, expr, &work_list_tail);
17979 } while(expr);
17980 do {
17981 expr = triple_lhs(state, dt->triple, expr);
17982 awaken(state, dtriple, expr, &work_list_tail);
17983 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017984 do {
17985 expr = triple_misc(state, dt->triple, expr);
17986 awaken(state, dtriple, expr, &work_list_tail);
17987 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017988 /* Wake up the forward control dependencies */
17989 do {
17990 expr = triple_targ(state, dt->triple, expr);
17991 awaken(state, dtriple, expr, &work_list_tail);
17992 } while(expr);
17993 /* Wake up the reverse control dependencies of this triple */
17994 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017995 struct triple *last;
17996 last = user->member->last;
17997 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017998#if DEBUG_ROMCC_WARNINGS
17999#warning "Should we bring the awakening noops back?"
18000#endif
18001 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018002 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000018003 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018004 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018005 }
18006 }
Eric Biederman90089602004-05-28 14:11:54 +000018007 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018008 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
18009 if ((dt->triple->op == OP_NOOP) &&
18010 (dt->flags & TRIPLE_FLAG_ALIVE)) {
18011 internal_error(state, dt->triple, "noop effective?");
18012 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018013 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018014 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018015 release_triple(state, dt->triple);
18016 }
18017 }
18018 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018019
18020 rebuild_ssa_form(state);
18021
Eric Biederman90089602004-05-28 14:11:54 +000018022 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018023}
18024
18025
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018026static void insert_mandatory_copies(struct compile_state *state)
18027{
18028 struct triple *ins, *first;
18029
18030 /* The object is with a minimum of inserted copies,
18031 * to resolve in fundamental register conflicts between
18032 * register value producers and consumers.
18033 * Theoretically we may be greater than minimal when we
18034 * are inserting copies before instructions but that
18035 * case should be rare.
18036 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018037 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018038 ins = first;
18039 do {
18040 struct triple_set *entry, *next;
18041 struct triple *tmp;
18042 struct reg_info info;
18043 unsigned reg, regcm;
18044 int do_post_copy, do_pre_copy;
18045 tmp = 0;
18046 if (!triple_is_def(state, ins)) {
18047 goto next;
18048 }
18049 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018050 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018051 if (info.reg >= MAX_REGISTERS) {
18052 info.reg = REG_UNSET;
18053 }
Eric Biederman90089602004-05-28 14:11:54 +000018054
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018055 reg = REG_UNSET;
18056 regcm = arch_type_to_regcm(state, ins->type);
18057 do_post_copy = do_pre_copy = 0;
18058
18059 /* Walk through the uses of ins and check for conflicts */
18060 for(entry = ins->use; entry; entry = next) {
18061 struct reg_info rinfo;
18062 int i;
18063 next = entry->next;
18064 i = find_rhs_use(state, entry->member, ins);
18065 if (i < 0) {
18066 continue;
18067 }
18068
18069 /* Find the users color requirements */
18070 rinfo = arch_reg_rhs(state, entry->member, i);
18071 if (rinfo.reg >= MAX_REGISTERS) {
18072 rinfo.reg = REG_UNSET;
18073 }
18074
18075 /* See if I need a pre_copy */
18076 if (rinfo.reg != REG_UNSET) {
18077 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18078 do_pre_copy = 1;
18079 }
18080 reg = rinfo.reg;
18081 }
18082 regcm &= rinfo.regcm;
18083 regcm = arch_regcm_normalize(state, regcm);
18084 if (regcm == 0) {
18085 do_pre_copy = 1;
18086 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018087 /* Always use pre_copies for constants.
18088 * They do not take up any registers until a
18089 * copy places them in one.
18090 */
18091 if ((info.reg == REG_UNNEEDED) &&
18092 (rinfo.reg != REG_UNNEEDED)) {
18093 do_pre_copy = 1;
18094 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018095 }
18096 do_post_copy =
18097 !do_pre_copy &&
18098 (((info.reg != REG_UNSET) &&
18099 (reg != REG_UNSET) &&
18100 (info.reg != reg)) ||
18101 ((info.regcm & regcm) == 0));
18102
18103 reg = info.reg;
18104 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018105 /* 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 +000018106 for(entry = ins->use; entry; entry = next) {
18107 struct reg_info rinfo;
18108 int i;
18109 next = entry->next;
18110 i = find_rhs_use(state, entry->member, ins);
18111 if (i < 0) {
18112 continue;
18113 }
18114
18115 /* Find the users color requirements */
18116 rinfo = arch_reg_rhs(state, entry->member, i);
18117 if (rinfo.reg >= MAX_REGISTERS) {
18118 rinfo.reg = REG_UNSET;
18119 }
18120
18121 /* Now see if it is time to do the pre_copy */
18122 if (rinfo.reg != REG_UNSET) {
18123 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18124 ((regcm & rinfo.regcm) == 0) ||
18125 /* Don't let a mandatory coalesce sneak
18126 * into a operation that is marked to prevent
18127 * coalescing.
18128 */
18129 ((reg != REG_UNNEEDED) &&
18130 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18131 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18132 ) {
18133 if (do_pre_copy) {
18134 struct triple *user;
18135 user = entry->member;
18136 if (RHS(user, i) != ins) {
18137 internal_error(state, user, "bad rhs");
18138 }
18139 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018140 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018141 continue;
18142 } else {
18143 do_post_copy = 1;
18144 }
18145 }
18146 reg = rinfo.reg;
18147 }
18148 if ((regcm & rinfo.regcm) == 0) {
18149 if (do_pre_copy) {
18150 struct triple *user;
18151 user = entry->member;
18152 if (RHS(user, i) != ins) {
18153 internal_error(state, user, "bad rhs");
18154 }
18155 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018156 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018157 continue;
18158 } else {
18159 do_post_copy = 1;
18160 }
18161 }
18162 regcm &= rinfo.regcm;
18163
18164 }
18165 if (do_post_copy) {
18166 struct reg_info pre, post;
18167 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018168 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018169 pre = arch_reg_lhs(state, ins, 0);
18170 post = arch_reg_lhs(state, tmp, 0);
18171 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18172 internal_error(state, tmp, "useless copy");
18173 }
18174 }
18175 next:
18176 ins = ins->next;
18177 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018178
Eric Biederman90089602004-05-28 14:11:54 +000018179 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018180}
18181
18182
Eric Biedermanb138ac82003-04-22 18:44:01 +000018183struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018184struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018185struct live_range {
18186 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018187 struct live_range_def *defs;
18188/* Note. The list pointed to by defs is kept in order.
18189 * That is baring splits in the flow control
18190 * defs dominates defs->next wich dominates defs->next->next
18191 * etc.
18192 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018193 unsigned color;
18194 unsigned classes;
18195 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018196 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018197 struct live_range *group_next, **group_prev;
18198};
18199
18200struct live_range_edge {
18201 struct live_range_edge *next;
18202 struct live_range *node;
18203};
18204
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018205struct live_range_def {
18206 struct live_range_def *next;
18207 struct live_range_def *prev;
18208 struct live_range *lr;
18209 struct triple *def;
18210 unsigned orig_id;
18211};
18212
Eric Biedermanb138ac82003-04-22 18:44:01 +000018213#define LRE_HASH_SIZE 2048
18214struct lre_hash {
18215 struct lre_hash *next;
18216 struct live_range *left;
18217 struct live_range *right;
18218};
18219
18220
18221struct reg_state {
18222 struct lre_hash *hash[LRE_HASH_SIZE];
18223 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018224 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018225 struct live_range *lr;
18226 struct live_range *low, **low_tail;
18227 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018228 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018229 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018230 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018231};
18232
18233
Eric Biedermand1ea5392003-06-28 06:49:45 +000018234struct print_interference_block_info {
18235 struct reg_state *rstate;
18236 FILE *fp;
18237 int need_edges;
18238};
18239static void print_interference_block(
18240 struct compile_state *state, struct block *block, void *arg)
18241
18242{
18243 struct print_interference_block_info *info = arg;
18244 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018245 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018246 FILE *fp = info->fp;
18247 struct reg_block *rb;
18248 struct triple *ptr;
18249 int phi_present;
18250 int done;
18251 rb = &rstate->blocks[block->vertex];
18252
Eric Biederman5ade04a2003-10-22 04:03:46 +000018253 fprintf(fp, "\nblock: %p (%d),",
18254 block, block->vertex);
18255 for(edge = block->edges; edge; edge = edge->next) {
18256 fprintf(fp, " %p<-%p",
18257 edge->member,
18258 edge->member && edge->member->use?edge->member->use->member : 0);
18259 }
18260 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018261 if (rb->in) {
18262 struct triple_reg_set *in_set;
18263 fprintf(fp, " in:");
18264 for(in_set = rb->in; in_set; in_set = in_set->next) {
18265 fprintf(fp, " %-10p", in_set->member);
18266 }
18267 fprintf(fp, "\n");
18268 }
18269 phi_present = 0;
18270 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18271 done = (ptr == block->last);
18272 if (ptr->op == OP_PHI) {
18273 phi_present = 1;
18274 break;
18275 }
18276 }
18277 if (phi_present) {
18278 int edge;
18279 for(edge = 0; edge < block->users; edge++) {
18280 fprintf(fp, " in(%d):", edge);
18281 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18282 struct triple **slot;
18283 done = (ptr == block->last);
18284 if (ptr->op != OP_PHI) {
18285 continue;
18286 }
18287 slot = &RHS(ptr, 0);
18288 fprintf(fp, " %-10p", slot[edge]);
18289 }
18290 fprintf(fp, "\n");
18291 }
18292 }
18293 if (block->first->op == OP_LABEL) {
18294 fprintf(fp, "%p:\n", block->first);
18295 }
18296 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018297 struct live_range *lr;
18298 unsigned id;
18299 int op;
18300 op = ptr->op;
18301 done = (ptr == block->last);
18302 lr = rstate->lrd[ptr->id].lr;
18303
Eric Biedermand1ea5392003-06-28 06:49:45 +000018304 id = ptr->id;
18305 ptr->id = rstate->lrd[id].orig_id;
18306 SET_REG(ptr->id, lr->color);
18307 display_triple(fp, ptr);
18308 ptr->id = id;
18309
18310 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18311 internal_error(state, ptr, "lr has no defs!");
18312 }
18313 if (info->need_edges) {
18314 if (lr->defs) {
18315 struct live_range_def *lrd;
18316 fprintf(fp, " range:");
18317 lrd = lr->defs;
18318 do {
18319 fprintf(fp, " %-10p", lrd->def);
18320 lrd = lrd->next;
18321 } while(lrd != lr->defs);
18322 fprintf(fp, "\n");
18323 }
18324 if (lr->edges > 0) {
18325 struct live_range_edge *edge;
18326 fprintf(fp, " edges:");
18327 for(edge = lr->edges; edge; edge = edge->next) {
18328 struct live_range_def *lrd;
18329 lrd = edge->node->defs;
18330 do {
18331 fprintf(fp, " %-10p", lrd->def);
18332 lrd = lrd->next;
18333 } while(lrd != edge->node->defs);
18334 fprintf(fp, "|");
18335 }
18336 fprintf(fp, "\n");
18337 }
18338 }
18339 /* Do a bunch of sanity checks */
18340 valid_ins(state, ptr);
18341 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18342 internal_error(state, ptr, "Invalid triple id: %d",
18343 ptr->id);
18344 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018345 }
18346 if (rb->out) {
18347 struct triple_reg_set *out_set;
18348 fprintf(fp, " out:");
18349 for(out_set = rb->out; out_set; out_set = out_set->next) {
18350 fprintf(fp, " %-10p", out_set->member);
18351 }
18352 fprintf(fp, "\n");
18353 }
18354 fprintf(fp, "\n");
18355}
18356
18357static void print_interference_blocks(
18358 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18359{
18360 struct print_interference_block_info info;
18361 info.rstate = rstate;
18362 info.fp = fp;
18363 info.need_edges = need_edges;
18364 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018365 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018366
18367}
18368
Eric Biedermanb138ac82003-04-22 18:44:01 +000018369static unsigned regc_max_size(struct compile_state *state, int classes)
18370{
18371 unsigned max_size;
18372 int i;
18373 max_size = 0;
18374 for(i = 0; i < MAX_REGC; i++) {
18375 if (classes & (1 << i)) {
18376 unsigned size;
18377 size = arch_regc_size(state, i);
18378 if (size > max_size) {
18379 max_size = size;
18380 }
18381 }
18382 }
18383 return max_size;
18384}
18385
18386static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18387{
18388 unsigned equivs[MAX_REG_EQUIVS];
18389 int i;
18390 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18391 internal_error(state, 0, "invalid register");
18392 }
18393 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18394 internal_error(state, 0, "invalid register");
18395 }
18396 arch_reg_equivs(state, equivs, reg1);
18397 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18398 if (equivs[i] == reg2) {
18399 return 1;
18400 }
18401 }
18402 return 0;
18403}
18404
18405static void reg_fill_used(struct compile_state *state, char *used, int reg)
18406{
18407 unsigned equivs[MAX_REG_EQUIVS];
18408 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018409 if (reg == REG_UNNEEDED) {
18410 return;
18411 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018412 arch_reg_equivs(state, equivs, reg);
18413 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18414 used[equivs[i]] = 1;
18415 }
18416 return;
18417}
18418
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018419static void reg_inc_used(struct compile_state *state, char *used, int reg)
18420{
18421 unsigned equivs[MAX_REG_EQUIVS];
18422 int i;
18423 if (reg == REG_UNNEEDED) {
18424 return;
18425 }
18426 arch_reg_equivs(state, equivs, reg);
18427 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18428 used[equivs[i]] += 1;
18429 }
18430 return;
18431}
18432
Eric Biedermanb138ac82003-04-22 18:44:01 +000018433static unsigned int hash_live_edge(
18434 struct live_range *left, struct live_range *right)
18435{
18436 unsigned int hash, val;
18437 unsigned long lval, rval;
18438 lval = ((unsigned long)left)/sizeof(struct live_range);
18439 rval = ((unsigned long)right)/sizeof(struct live_range);
18440 hash = 0;
18441 while(lval) {
18442 val = lval & 0xff;
18443 lval >>= 8;
18444 hash = (hash *263) + val;
18445 }
18446 while(rval) {
18447 val = rval & 0xff;
18448 rval >>= 8;
18449 hash = (hash *263) + val;
18450 }
18451 hash = hash & (LRE_HASH_SIZE - 1);
18452 return hash;
18453}
18454
18455static struct lre_hash **lre_probe(struct reg_state *rstate,
18456 struct live_range *left, struct live_range *right)
18457{
18458 struct lre_hash **ptr;
18459 unsigned int index;
18460 /* Ensure left <= right */
18461 if (left > right) {
18462 struct live_range *tmp;
18463 tmp = left;
18464 left = right;
18465 right = tmp;
18466 }
18467 index = hash_live_edge(left, right);
18468
18469 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018470 while(*ptr) {
18471 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18472 break;
18473 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018474 ptr = &(*ptr)->next;
18475 }
18476 return ptr;
18477}
18478
18479static int interfere(struct reg_state *rstate,
18480 struct live_range *left, struct live_range *right)
18481{
18482 struct lre_hash **ptr;
18483 ptr = lre_probe(rstate, left, right);
18484 return ptr && *ptr;
18485}
18486
18487static void add_live_edge(struct reg_state *rstate,
18488 struct live_range *left, struct live_range *right)
18489{
18490 /* FIXME the memory allocation overhead is noticeable here... */
18491 struct lre_hash **ptr, *new_hash;
18492 struct live_range_edge *edge;
18493
18494 if (left == right) {
18495 return;
18496 }
18497 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18498 return;
18499 }
18500 /* Ensure left <= right */
18501 if (left > right) {
18502 struct live_range *tmp;
18503 tmp = left;
18504 left = right;
18505 right = tmp;
18506 }
18507 ptr = lre_probe(rstate, left, right);
18508 if (*ptr) {
18509 return;
18510 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018511#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018512 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018513 left, right);
18514#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018515 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18516 new_hash->next = *ptr;
18517 new_hash->left = left;
18518 new_hash->right = right;
18519 *ptr = new_hash;
18520
18521 edge = xmalloc(sizeof(*edge), "live_range_edge");
18522 edge->next = left->edges;
18523 edge->node = right;
18524 left->edges = edge;
18525 left->degree += 1;
18526
18527 edge = xmalloc(sizeof(*edge), "live_range_edge");
18528 edge->next = right->edges;
18529 edge->node = left;
18530 right->edges = edge;
18531 right->degree += 1;
18532}
18533
18534static void remove_live_edge(struct reg_state *rstate,
18535 struct live_range *left, struct live_range *right)
18536{
18537 struct live_range_edge *edge, **ptr;
18538 struct lre_hash **hptr, *entry;
18539 hptr = lre_probe(rstate, left, right);
18540 if (!hptr || !*hptr) {
18541 return;
18542 }
18543 entry = *hptr;
18544 *hptr = entry->next;
18545 xfree(entry);
18546
18547 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18548 edge = *ptr;
18549 if (edge->node == right) {
18550 *ptr = edge->next;
18551 memset(edge, 0, sizeof(*edge));
18552 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018553 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018554 break;
18555 }
18556 }
18557 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18558 edge = *ptr;
18559 if (edge->node == left) {
18560 *ptr = edge->next;
18561 memset(edge, 0, sizeof(*edge));
18562 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018563 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018564 break;
18565 }
18566 }
18567}
18568
18569static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18570{
18571 struct live_range_edge *edge, *next;
18572 for(edge = range->edges; edge; edge = next) {
18573 next = edge->next;
18574 remove_live_edge(rstate, range, edge->node);
18575 }
18576}
18577
Eric Biederman153ea352003-06-20 14:43:20 +000018578static void transfer_live_edges(struct reg_state *rstate,
18579 struct live_range *dest, struct live_range *src)
18580{
18581 struct live_range_edge *edge, *next;
18582 for(edge = src->edges; edge; edge = next) {
18583 struct live_range *other;
18584 next = edge->next;
18585 other = edge->node;
18586 remove_live_edge(rstate, src, other);
18587 add_live_edge(rstate, dest, other);
18588 }
18589}
18590
Eric Biedermanb138ac82003-04-22 18:44:01 +000018591
18592/* Interference graph...
18593 *
18594 * new(n) --- Return a graph with n nodes but no edges.
18595 * add(g,x,y) --- Return a graph including g with an between x and y
18596 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18597 * x and y in the graph g
18598 * degree(g, x) --- Return the degree of the node x in the graph g
18599 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18600 *
18601 * Implement with a hash table && a set of adjcency vectors.
18602 * The hash table supports constant time implementations of add and interfere.
18603 * The adjacency vectors support an efficient implementation of neighbors.
18604 */
18605
18606/*
18607 * +---------------------------------------------------+
18608 * | +--------------+ |
18609 * v v | |
18610 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
18611 *
18612 * -- In simplify implment optimistic coloring... (No backtracking)
18613 * -- Implement Rematerialization it is the only form of spilling we can perform
18614 * Essentially this means dropping a constant from a register because
18615 * we can regenerate it later.
18616 *
18617 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18618 * coalesce at phi points...
18619 * --- Bias coloring if at all possible do the coalesing a compile time.
18620 *
18621 *
18622 */
18623
Stefan Reinauer50542a82007-10-24 11:14:14 +000018624#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018625static void different_colored(
18626 struct compile_state *state, struct reg_state *rstate,
18627 struct triple *parent, struct triple *ins)
18628{
18629 struct live_range *lr;
18630 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018631 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018632 expr = triple_rhs(state, ins, 0);
18633 for(;expr; expr = triple_rhs(state, ins, expr)) {
18634 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018635 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018636 continue;
18637 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018638 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018639 if (lr->color == lr2->color) {
18640 internal_error(state, ins, "live range too big");
18641 }
18642 }
18643}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018644#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018645
18646static struct live_range *coalesce_ranges(
18647 struct compile_state *state, struct reg_state *rstate,
18648 struct live_range *lr1, struct live_range *lr2)
18649{
18650 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18651 unsigned color;
18652 unsigned classes;
18653 if (lr1 == lr2) {
18654 return lr1;
18655 }
18656 if (!lr1->defs || !lr2->defs) {
18657 internal_error(state, 0,
18658 "cannot coalese dead live ranges");
18659 }
18660 if ((lr1->color == REG_UNNEEDED) ||
18661 (lr2->color == REG_UNNEEDED)) {
18662 internal_error(state, 0,
18663 "cannot coalesce live ranges without a possible color");
18664 }
18665 if ((lr1->color != lr2->color) &&
18666 (lr1->color != REG_UNSET) &&
18667 (lr2->color != REG_UNSET)) {
18668 internal_error(state, lr1->defs->def,
18669 "cannot coalesce live ranges of different colors");
18670 }
18671 color = lr1->color;
18672 if (color == REG_UNSET) {
18673 color = lr2->color;
18674 }
18675 classes = lr1->classes & lr2->classes;
18676 if (!classes) {
18677 internal_error(state, lr1->defs->def,
18678 "cannot coalesce live ranges with dissimilar register classes");
18679 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018680 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018681 FILE *fp = state->errout;
18682 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018683 lrd = lr1->defs;
18684 do {
Eric Biederman90089602004-05-28 14:11:54 +000018685 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018686 lrd = lrd->next;
18687 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018688 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018689 lrd = lr2->defs;
18690 do {
Eric Biederman90089602004-05-28 14:11:54 +000018691 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018692 lrd = lrd->next;
18693 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018694 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018695 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018696 /* If there is a clear dominate live range put it in lr1,
18697 * For purposes of this test phi functions are
18698 * considered dominated by the definitions that feed into
18699 * them.
18700 */
18701 if ((lr1->defs->prev->def->op == OP_PHI) ||
18702 ((lr2->defs->prev->def->op != OP_PHI) &&
18703 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18704 struct live_range *tmp;
18705 tmp = lr1;
18706 lr1 = lr2;
18707 lr2 = tmp;
18708 }
18709#if 0
18710 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018711 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018712 }
18713 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018714 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018715 }
18716 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018717 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018718 }
18719 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018720 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018721 }
18722#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018723#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018724 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018725 lr1->defs->def,
18726 lr1->color,
18727 lr2->defs->def,
18728 lr2->color);
18729#endif
18730
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018731 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018732#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018733#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018734#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018735 /* This FIXME item applies to the correctness of live_range_end
18736 * and to the necessity of making multiple passes of coalesce_live_ranges.
18737 * A failure to find some coalesce opportunities in coaleace_live_ranges
18738 * does not impact the correct of the compiler just the efficiency with
18739 * which registers are allocated.
18740 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018741 head = lr1->defs;
18742 mid1 = lr1->defs->prev;
18743 mid2 = lr2->defs;
18744 end = lr2->defs->prev;
18745
18746 head->prev = end;
18747 end->next = head;
18748
18749 mid1->next = mid2;
18750 mid2->prev = mid1;
18751
18752 /* Fixup the live range in the added live range defs */
18753 lrd = head;
18754 do {
18755 lrd->lr = lr1;
18756 lrd = lrd->next;
18757 } while(lrd != head);
18758
18759 /* Mark lr2 as free. */
18760 lr2->defs = 0;
18761 lr2->color = REG_UNNEEDED;
18762 lr2->classes = 0;
18763
18764 if (!lr1->defs) {
18765 internal_error(state, 0, "lr1->defs == 0 ?");
18766 }
18767
18768 lr1->color = color;
18769 lr1->classes = classes;
18770
Eric Biederman153ea352003-06-20 14:43:20 +000018771 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18772 transfer_live_edges(rstate, lr1, lr2);
18773
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018774 return lr1;
18775}
18776
18777static struct live_range_def *live_range_head(
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;
18785 }
18786 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18787 result = last->next;
18788 }
18789 return result;
18790}
18791
18792static struct live_range_def *live_range_end(
18793 struct compile_state *state, struct live_range *lr,
18794 struct live_range_def *last)
18795{
18796 struct live_range_def *result;
18797 result = 0;
18798 if (last == 0) {
18799 result = lr->defs->prev;
18800 }
18801 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18802 result = last->prev;
18803 }
18804 return result;
18805}
18806
18807
Eric Biedermanb138ac82003-04-22 18:44:01 +000018808static void initialize_live_ranges(
18809 struct compile_state *state, struct reg_state *rstate)
18810{
18811 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018812 size_t count, size;
18813 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018814
Eric Biederman83b991a2003-10-11 06:20:25 +000018815 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018816 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018817 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018818 count = count_triples(state);
18819 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018820 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018821 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018822 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018823 /* Potentially I need one live range for each instruction
18824 * plus an extra for the dummy live range.
18825 */
18826 rstate->ranges = count + 1;
18827 size = sizeof(rstate->lrd[0]) * rstate->defs;
18828 rstate->lrd = xcmalloc(size, "live_range_def");
18829 size = sizeof(rstate->lr[0]) * rstate->ranges;
18830 rstate->lr = xcmalloc(size, "live_range");
18831
Eric Biedermanb138ac82003-04-22 18:44:01 +000018832 /* Setup the dummy live range */
18833 rstate->lr[0].classes = 0;
18834 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018835 rstate->lr[0].defs = 0;
18836 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018837 ins = first;
18838 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018839 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018840 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018841 struct reg_info info;
18842 /* Find the architecture specific color information */
18843 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018844 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018845 rstate->lr[i].defs = &rstate->lrd[j];
18846 rstate->lr[i].color = info.reg;
18847 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018848 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018849 rstate->lrd[j].lr = &rstate->lr[i];
18850 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018851 /* Otherwise give the triple the dummy live range. */
18852 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018853 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018854 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018855
18856 /* Initalize the live_range_def */
18857 rstate->lrd[j].next = &rstate->lrd[j];
18858 rstate->lrd[j].prev = &rstate->lrd[j];
18859 rstate->lrd[j].def = ins;
18860 rstate->lrd[j].orig_id = ins->id;
18861 ins->id = j;
18862
18863 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018864 ins = ins->next;
18865 } while(ins != first);
18866 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018867
Eric Biedermanb138ac82003-04-22 18:44:01 +000018868 /* Make a second pass to handle achitecture specific register
18869 * constraints.
18870 */
18871 ins = first;
18872 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018873 int zlhs, zrhs, i, j;
18874 if (ins->id > rstate->defs) {
18875 internal_error(state, ins, "bad id");
18876 }
18877
18878 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018879 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018880 if ((zlhs == 0) && triple_is_def(state, ins)) {
18881 zlhs = 1;
18882 }
Eric Biederman90089602004-05-28 14:11:54 +000018883 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018884
Eric Biederman5ade04a2003-10-22 04:03:46 +000018885 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018886 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018887 ins, zlhs, zrhs);
18888 }
18889
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018890 for(i = 0; i < zlhs; i++) {
18891 struct reg_info linfo;
18892 struct live_range_def *lhs;
18893 linfo = arch_reg_lhs(state, ins, i);
18894 if (linfo.reg < MAX_REGISTERS) {
18895 continue;
18896 }
18897 if (triple_is_def(state, ins)) {
18898 lhs = &rstate->lrd[ins->id];
18899 } else {
18900 lhs = &rstate->lrd[LHS(ins, i)->id];
18901 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018902
18903 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018904 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018905 i, lhs, linfo.reg);
18906 }
18907
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018908 for(j = 0; j < zrhs; j++) {
18909 struct reg_info rinfo;
18910 struct live_range_def *rhs;
18911 rinfo = arch_reg_rhs(state, ins, j);
18912 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018913 continue;
18914 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018915 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018916
18917 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018918 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018919 j, rhs, rinfo.reg);
18920 }
18921
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018922 if (rinfo.reg == linfo.reg) {
18923 coalesce_ranges(state, rstate,
18924 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018925 }
18926 }
18927 }
18928 ins = ins->next;
18929 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018930}
18931
Eric Biedermanf96a8102003-06-16 16:57:34 +000018932static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018933 struct compile_state *state,
18934 struct reg_block *blocks, struct triple_reg_set *live,
18935 struct reg_block *rb, struct triple *ins, void *arg)
18936{
18937 struct reg_state *rstate = arg;
18938 struct live_range *def;
18939 struct triple_reg_set *entry;
18940
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018941 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018942 * we do not have a definition to add to
18943 * the interference graph.
18944 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018945 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018946 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018947 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018948 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018949
18950 /* Create an edge between ins and everything that is
18951 * alive, unless the live_range cannot share
18952 * a physical register with ins.
18953 */
18954 for(entry = live; entry; entry = entry->next) {
18955 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018956 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18957 internal_error(state, 0, "bad entry?");
18958 }
18959 lr = rstate->lrd[entry->member->id].lr;
18960 if (def == lr) {
18961 continue;
18962 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018963 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18964 continue;
18965 }
18966 add_live_edge(rstate, def, lr);
18967 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018968 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018969}
18970
Stefan Reinauer50542a82007-10-24 11:14:14 +000018971#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018972static struct live_range *get_verify_live_range(
18973 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18974{
18975 struct live_range *lr;
18976 struct live_range_def *lrd;
18977 int ins_found;
18978 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18979 internal_error(state, ins, "bad ins?");
18980 }
18981 lr = rstate->lrd[ins->id].lr;
18982 ins_found = 0;
18983 lrd = lr->defs;
18984 do {
18985 if (lrd->def == ins) {
18986 ins_found = 1;
18987 }
18988 lrd = lrd->next;
18989 } while(lrd != lr->defs);
18990 if (!ins_found) {
18991 internal_error(state, ins, "ins not in live range");
18992 }
18993 return lr;
18994}
18995
18996static void verify_graph_ins(
18997 struct compile_state *state,
18998 struct reg_block *blocks, struct triple_reg_set *live,
18999 struct reg_block *rb, struct triple *ins, void *arg)
19000{
19001 struct reg_state *rstate = arg;
19002 struct triple_reg_set *entry1, *entry2;
19003
19004
19005 /* Compare live against edges and make certain the code is working */
19006 for(entry1 = live; entry1; entry1 = entry1->next) {
19007 struct live_range *lr1;
19008 lr1 = get_verify_live_range(state, rstate, entry1->member);
19009 for(entry2 = live; entry2; entry2 = entry2->next) {
19010 struct live_range *lr2;
19011 struct live_range_edge *edge2;
19012 int lr1_found;
19013 int lr2_degree;
19014 if (entry2 == entry1) {
19015 continue;
19016 }
19017 lr2 = get_verify_live_range(state, rstate, entry2->member);
19018 if (lr1 == lr2) {
19019 internal_error(state, entry2->member,
19020 "live range with 2 values simultaneously alive");
19021 }
19022 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19023 continue;
19024 }
19025 if (!interfere(rstate, lr1, lr2)) {
19026 internal_error(state, entry2->member,
19027 "edges don't interfere?");
19028 }
19029
19030 lr1_found = 0;
19031 lr2_degree = 0;
19032 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19033 lr2_degree++;
19034 if (edge2->node == lr1) {
19035 lr1_found = 1;
19036 }
19037 }
19038 if (lr2_degree != lr2->degree) {
19039 internal_error(state, entry2->member,
19040 "computed degree: %d does not match reported degree: %d\n",
19041 lr2_degree, lr2->degree);
19042 }
19043 if (!lr1_found) {
19044 internal_error(state, entry2->member, "missing edge");
19045 }
19046 }
19047 }
19048 return;
19049}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019050#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019051
Eric Biedermanf96a8102003-06-16 16:57:34 +000019052static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000019053 struct compile_state *state,
19054 struct reg_block *blocks, struct triple_reg_set *live,
19055 struct reg_block *rb, struct triple *ins, void *arg)
19056{
19057 struct reg_state *rstate = arg;
19058 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019059 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019060 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019061
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019062 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019063 id = ins->id;
19064 ins->id = rstate->lrd[id].orig_id;
19065 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019066 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019067 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019068
19069 if (lr->defs) {
19070 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019071 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019072 lrd = lr->defs;
19073 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019074 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019075 lrd = lrd->next;
19076 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019077 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019078 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019079 if (live) {
19080 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019081 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019082 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019083 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019084 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019085 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019086 }
19087 if (lr->edges) {
19088 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019089 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019090 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019091 struct live_range_def *lrd;
19092 lrd = entry->node->defs;
19093 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019094 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019095 lrd = lrd->next;
19096 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019097 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019098 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019099 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019100 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019101 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019102 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019103 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019104 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019105}
19106
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019107static int coalesce_live_ranges(
19108 struct compile_state *state, struct reg_state *rstate)
19109{
19110 /* At the point where a value is moved from one
19111 * register to another that value requires two
19112 * registers, thus increasing register pressure.
19113 * Live range coaleescing reduces the register
19114 * pressure by keeping a value in one register
19115 * longer.
19116 *
19117 * In the case of a phi function all paths leading
19118 * into it must be allocated to the same register
19119 * otherwise the phi function may not be removed.
19120 *
19121 * Forcing a value to stay in a single register
19122 * for an extended period of time does have
19123 * limitations when applied to non homogenous
19124 * register pool.
19125 *
19126 * The two cases I have identified are:
19127 * 1) Two forced register assignments may
19128 * collide.
19129 * 2) Registers may go unused because they
19130 * are only good for storing the value
19131 * and not manipulating it.
19132 *
19133 * Because of this I need to split live ranges,
19134 * even outside of the context of coalesced live
19135 * ranges. The need to split live ranges does
19136 * impose some constraints on live range coalescing.
19137 *
19138 * - Live ranges may not be coalesced across phi
19139 * functions. This creates a 2 headed live
19140 * range that cannot be sanely split.
19141 *
19142 * - phi functions (coalesced in initialize_live_ranges)
19143 * are handled as pre split live ranges so we will
19144 * never attempt to split them.
19145 */
19146 int coalesced;
19147 int i;
19148
19149 coalesced = 0;
19150 for(i = 0; i <= rstate->ranges; i++) {
19151 struct live_range *lr1;
19152 struct live_range_def *lrd1;
19153 lr1 = &rstate->lr[i];
19154 if (!lr1->defs) {
19155 continue;
19156 }
19157 lrd1 = live_range_end(state, lr1, 0);
19158 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19159 struct triple_set *set;
19160 if (lrd1->def->op != OP_COPY) {
19161 continue;
19162 }
19163 /* Skip copies that are the result of a live range split. */
19164 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19165 continue;
19166 }
19167 for(set = lrd1->def->use; set; set = set->next) {
19168 struct live_range_def *lrd2;
19169 struct live_range *lr2, *res;
19170
19171 lrd2 = &rstate->lrd[set->member->id];
19172
19173 /* Don't coalesce with instructions
19174 * that are the result of a live range
19175 * split.
19176 */
19177 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19178 continue;
19179 }
19180 lr2 = rstate->lrd[set->member->id].lr;
19181 if (lr1 == lr2) {
19182 continue;
19183 }
19184 if ((lr1->color != lr2->color) &&
19185 (lr1->color != REG_UNSET) &&
19186 (lr2->color != REG_UNSET)) {
19187 continue;
19188 }
19189 if ((lr1->classes & lr2->classes) == 0) {
19190 continue;
19191 }
19192
19193 if (interfere(rstate, lr1, lr2)) {
19194 continue;
19195 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019196
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019197 res = coalesce_ranges(state, rstate, lr1, lr2);
19198 coalesced += 1;
19199 if (res != lr1) {
19200 goto next;
19201 }
19202 }
19203 }
19204 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019205 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019206 }
19207 return coalesced;
19208}
19209
19210
Eric Biedermanf96a8102003-06-16 16:57:34 +000019211static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019212 struct reg_block *blocks, struct triple_reg_set *live,
19213 struct reg_block *rb, struct triple *ins, void *arg)
19214{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019215 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019216 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019217
19218 /* See if we have a mandatory coalesce operation between
19219 * a lhs and a rhs value. If so and the rhs value is also
19220 * alive then this triple needs to be pre copied. Otherwise
19221 * we would have two definitions in the same live range simultaneously
19222 * alive.
19223 */
Eric Biederman90089602004-05-28 14:11:54 +000019224 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019225 if ((zlhs == 0) && triple_is_def(state, ins)) {
19226 zlhs = 1;
19227 }
Eric Biederman90089602004-05-28 14:11:54 +000019228 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019229 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019230 struct reg_info linfo;
19231 linfo = arch_reg_lhs(state, ins, i);
19232 if (linfo.reg < MAX_REGISTERS) {
19233 continue;
19234 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019235 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019236 struct reg_info rinfo;
19237 struct triple *rhs;
19238 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019239 int found;
19240 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019241 rinfo = arch_reg_rhs(state, ins, j);
19242 if (rinfo.reg != linfo.reg) {
19243 continue;
19244 }
19245 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019246 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019247 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019248 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019249 }
19250 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019251 if (found) {
19252 struct triple *copy;
19253 copy = pre_copy(state, ins, j);
19254 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019255 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019256 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019257 }
19258 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019259 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019260}
19261
Eric Biedermand1ea5392003-06-28 06:49:45 +000019262static int correct_coalesce_conflicts(
19263 struct compile_state *state, struct reg_block *blocks)
19264{
19265 int conflicts;
19266 conflicts = 0;
Eric Biederman90089602004-05-28 14:11:54 +000019267 walk_variable_lifetimes(state, &state->bb, blocks,
19268 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019269 return conflicts;
19270}
19271
Eric Biedermanf96a8102003-06-16 16:57:34 +000019272static void replace_set_use(struct compile_state *state,
19273 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019274{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019275 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019276 for(set = head; set; set = set->next) {
19277 if (set->member == orig) {
19278 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019279 }
19280 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019281}
19282
Eric Biedermanf96a8102003-06-16 16:57:34 +000019283static void replace_block_use(struct compile_state *state,
19284 struct reg_block *blocks, struct triple *orig, struct triple *new)
19285{
19286 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019287#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019288#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019289#endif
Eric Biederman90089602004-05-28 14:11:54 +000019290 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019291 struct reg_block *rb;
19292 rb = &blocks[i];
19293 replace_set_use(state, rb->in, orig, new);
19294 replace_set_use(state, rb->out, orig, new);
19295 }
19296}
19297
19298static void color_instructions(struct compile_state *state)
19299{
19300 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019301 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019302 ins = first;
19303 do {
19304 if (triple_is_def(state, ins)) {
19305 struct reg_info info;
19306 info = find_lhs_color(state, ins, 0);
19307 if (info.reg >= MAX_REGISTERS) {
19308 info.reg = REG_UNSET;
19309 }
19310 SET_INFO(ins->id, info);
19311 }
19312 ins = ins->next;
19313 } while(ins != first);
19314}
19315
19316static struct reg_info read_lhs_color(
19317 struct compile_state *state, struct triple *ins, int index)
19318{
19319 struct reg_info info;
19320 if ((index == 0) && triple_is_def(state, ins)) {
19321 info.reg = ID_REG(ins->id);
19322 info.regcm = ID_REGCM(ins->id);
19323 }
Eric Biederman90089602004-05-28 14:11:54 +000019324 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019325 info = read_lhs_color(state, LHS(ins, index), 0);
19326 }
19327 else {
19328 internal_error(state, ins, "Bad lhs %d", index);
19329 info.reg = REG_UNSET;
19330 info.regcm = 0;
19331 }
19332 return info;
19333}
19334
19335static struct triple *resolve_tangle(
19336 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019337{
19338 struct reg_info info, uinfo;
19339 struct triple_set *set, *next;
19340 struct triple *copy;
19341
Stefan Reinauer50542a82007-10-24 11:14:14 +000019342#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019343#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019344#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019345 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019346 for(set = tangle->use; set; set = next) {
19347 struct triple *user;
19348 int i, zrhs;
19349 next = set->next;
19350 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019351 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019352 for(i = 0; i < zrhs; i++) {
19353 if (RHS(user, i) != tangle) {
19354 continue;
19355 }
19356 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019357 if (uinfo.reg == info.reg) {
19358 copy = pre_copy(state, user, i);
19359 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019360 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019361 }
19362 }
19363 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019364 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019365 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019366 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019367 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019368 copy = post_copy(state, tangle);
19369 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019370 linfo = find_lhs_color(state, copy, 0);
19371 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019372 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019373 info = find_lhs_color(state, tangle, 0);
19374 SET_INFO(tangle->id, info);
19375
19376 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019377}
19378
19379
Eric Biedermanf96a8102003-06-16 16:57:34 +000019380static void fix_tangles(struct compile_state *state,
19381 struct reg_block *blocks, struct triple_reg_set *live,
19382 struct reg_block *rb, struct triple *ins, void *arg)
19383{
Eric Biederman153ea352003-06-20 14:43:20 +000019384 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019385 struct triple *tangle;
19386 do {
19387 char used[MAX_REGISTERS];
19388 struct triple_reg_set *set;
19389 tangle = 0;
19390
19391 /* Find out which registers have multiple uses at this point */
19392 memset(used, 0, sizeof(used));
19393 for(set = live; set; set = set->next) {
19394 struct reg_info info;
19395 info = read_lhs_color(state, set->member, 0);
19396 if (info.reg == REG_UNSET) {
19397 continue;
19398 }
19399 reg_inc_used(state, used, info.reg);
19400 }
19401
19402 /* Now find the least dominated definition of a register in
19403 * conflict I have seen so far.
19404 */
19405 for(set = live; set; set = set->next) {
19406 struct reg_info info;
19407 info = read_lhs_color(state, set->member, 0);
19408 if (used[info.reg] < 2) {
19409 continue;
19410 }
Eric Biederman153ea352003-06-20 14:43:20 +000019411 /* Changing copies that feed into phi functions
19412 * is incorrect.
19413 */
19414 if (set->member->use &&
19415 (set->member->use->member->op == OP_PHI)) {
19416 continue;
19417 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019418 if (!tangle || tdominates(state, set->member, tangle)) {
19419 tangle = set->member;
19420 }
19421 }
19422 /* If I have found a tangle resolve it */
19423 if (tangle) {
19424 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019425 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019426 post_copy = resolve_tangle(state, tangle);
19427 if (post_copy) {
19428 replace_block_use(state, blocks, tangle, post_copy);
19429 }
19430 if (post_copy && (tangle != ins)) {
19431 replace_set_use(state, live, tangle, post_copy);
19432 }
19433 }
19434 } while(tangle);
19435 return;
19436}
19437
Eric Biederman153ea352003-06-20 14:43:20 +000019438static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019439 struct compile_state *state, struct reg_block *blocks)
19440{
Eric Biederman153ea352003-06-20 14:43:20 +000019441 int tangles;
19442 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019443 color_instructions(state);
Eric Biederman90089602004-05-28 14:11:54 +000019444 walk_variable_lifetimes(state, &state->bb, blocks,
19445 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019446 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019447}
19448
Eric Biedermand1ea5392003-06-28 06:49:45 +000019449
19450static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19451static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19452
19453struct triple *find_constrained_def(
19454 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019455{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019456 struct live_range_def *lrd, *lrd_next;
19457 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019458 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019459 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019460 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019461
19462 lrd = lrd_next;
19463 lrd_next = lrd->next;
19464
Eric Biedermand1ea5392003-06-28 06:49:45 +000019465 regcm = arch_type_to_regcm(state, lrd->def->type);
19466 info = find_lhs_color(state, lrd->def, 0);
19467 regcm = arch_regcm_reg_normalize(state, regcm);
19468 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019469 /* If the 2 register class masks are equal then
19470 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019471 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019472 if (regcm == info.regcm) {
19473 continue;
19474 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019475
Eric Biederman5ade04a2003-10-22 04:03:46 +000019476 /* If there is just one use.
19477 * That use cannot accept a larger register class.
19478 * There are no intervening definitions except
19479 * definitions that feed into that use.
19480 * Then a triple is not constrained.
19481 * FIXME handle this case!
19482 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019483#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019484#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019485#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019486
19487
Eric Biedermand1ea5392003-06-28 06:49:45 +000019488 /* Of the constrained live ranges deal with the
19489 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019490 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019491 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019492 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019493 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019494 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019495 if (!constrained ||
19496 tdominates(state, lrd->def, constrained))
19497 {
19498 constrained = lrd->def;
19499 }
19500 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019501 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019502}
19503
Eric Biedermand1ea5392003-06-28 06:49:45 +000019504static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019505 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019506 struct live_range *range)
19507{
19508 /* Walk through the edges in conflict and our current live
19509 * range, and find definitions that are more severly constrained
19510 * than they type of data they contain require.
19511 *
19512 * Then pick one of those ranges and relax the constraints.
19513 */
19514 struct live_range_edge *edge;
19515 struct triple *constrained;
19516
19517 constrained = 0;
19518 for(edge = range->edges; edge; edge = edge->next) {
19519 constrained = find_constrained_def(state, edge->node, constrained);
19520 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019521#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019522#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019523#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019524 if (!constrained) {
19525 constrained = find_constrained_def(state, range, constrained);
19526 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019527
19528 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019529 fprintf(state->errout, "constrained: ");
19530 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019531 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019532 if (constrained) {
19533 ids_from_rstate(state, rstate);
19534 cleanup_rstate(state, rstate);
19535 resolve_tangle(state, constrained);
19536 }
19537 return !!constrained;
19538}
19539
19540static int split_ranges(
19541 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019542 char *used, struct live_range *range)
19543{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019544 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019545 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019546 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019547 rstate->passes, tops(range->defs->def->op), range->defs->def);
19548 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019549 if ((range->color == REG_UNNEEDED) ||
19550 (rstate->passes >= rstate->max_passes)) {
19551 return 0;
19552 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019553 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019554
Eric Biedermand1ea5392003-06-28 06:49:45 +000019555 /* Ideally I would split the live range that will not be used
19556 * for the longest period of time in hopes that this will
19557 * (a) allow me to spill a register or
19558 * (b) allow me to place a value in another register.
19559 *
19560 * So far I don't have a test case for this, the resolving
19561 * of mandatory constraints has solved all of my
19562 * know issues. So I have choosen not to write any
19563 * code until I cat get a better feel for cases where
19564 * it would be useful to have.
19565 *
19566 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019567#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019568#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019569#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019570
19571 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019572 FILE *fp = state->errout;
19573 print_interference_blocks(state, rstate, fp, 0);
19574 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019575 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019576 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019577}
19578
Eric Biederman5ade04a2003-10-22 04:03:46 +000019579static FILE *cgdebug_fp(struct compile_state *state)
19580{
19581 FILE *fp;
19582 fp = 0;
19583 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019584 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019585 }
19586 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019587 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019588 }
19589 return fp;
19590}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019591
Eric Biederman5ade04a2003-10-22 04:03:46 +000019592static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19593{
19594 FILE *fp;
19595 fp = cgdebug_fp(state);
19596 if (fp) {
19597 va_list args;
19598 va_start(args, fmt);
19599 vfprintf(fp, fmt, args);
19600 va_end(args);
19601 }
19602}
19603
19604static void cgdebug_flush(struct compile_state *state)
19605{
19606 FILE *fp;
19607 fp = cgdebug_fp(state);
19608 if (fp) {
19609 fflush(fp);
19610 }
19611}
19612
19613static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19614{
19615 FILE *fp;
19616 fp = cgdebug_fp(state);
19617 if (fp) {
19618 loc(fp, state, ins);
19619 }
19620}
19621
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019622static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019623 struct reg_state *rstate, struct live_range *range)
19624{
19625 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019626 struct live_range_def *lrd;
19627 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019628 struct live_range_edge *edge;
19629 char used[MAX_REGISTERS];
19630 struct triple **expr;
19631
Eric Biedermanb138ac82003-04-22 18:44:01 +000019632 /* Instead of doing just the trivial color select here I try
19633 * a few extra things because a good color selection will help reduce
19634 * copies.
19635 */
19636
19637 /* Find the registers currently in use */
19638 memset(used, 0, sizeof(used));
19639 for(edge = range->edges; edge; edge = edge->next) {
19640 if (edge->node->color == REG_UNSET) {
19641 continue;
19642 }
19643 reg_fill_used(state, used, edge->node->color);
19644 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019645
19646 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019647 int i;
19648 i = 0;
19649 for(edge = range->edges; edge; edge = edge->next) {
19650 i++;
19651 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019652 cgdebug_printf(state, "\n%s edges: %d",
19653 tops(range->defs->def->op), i);
19654 cgdebug_loc(state, range->defs->def);
19655 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019656 for(i = 0; i < MAX_REGISTERS; i++) {
19657 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019658 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019659 arch_reg_str(i));
19660 }
19661 }
19662 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019663
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019664 /* If a color is already assigned see if it will work */
19665 if (range->color != REG_UNSET) {
19666 struct live_range_def *lrd;
19667 if (!used[range->color]) {
19668 return 1;
19669 }
19670 for(edge = range->edges; edge; edge = edge->next) {
19671 if (edge->node->color != range->color) {
19672 continue;
19673 }
19674 warning(state, edge->node->defs->def, "edge: ");
19675 lrd = edge->node->defs;
19676 do {
19677 warning(state, lrd->def, " %p %s",
19678 lrd->def, tops(lrd->def->op));
19679 lrd = lrd->next;
19680 } while(lrd != edge->node->defs);
19681 }
19682 lrd = range->defs;
19683 warning(state, range->defs->def, "def: ");
19684 do {
19685 warning(state, lrd->def, " %p %s",
19686 lrd->def, tops(lrd->def->op));
19687 lrd = lrd->next;
19688 } while(lrd != range->defs);
19689 internal_error(state, range->defs->def,
19690 "live range with already used color %s",
19691 arch_reg_str(range->color));
19692 }
19693
Eric Biedermanb138ac82003-04-22 18:44:01 +000019694 /* If I feed into an expression reuse it's color.
19695 * This should help remove copies in the case of 2 register instructions
19696 * and phi functions.
19697 */
19698 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019699 lrd = live_range_end(state, range, 0);
19700 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19701 entry = lrd->def->use;
19702 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19703 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019704 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019705 insd = &rstate->lrd[entry->member->id];
19706 if (insd->lr->defs == 0) {
19707 continue;
19708 }
19709 if (!phi && (insd->def->op == OP_PHI) &&
19710 !interfere(rstate, range, insd->lr)) {
19711 phi = insd;
19712 }
Eric Biederman530b5192003-07-01 10:05:30 +000019713 if (insd->lr->color == REG_UNSET) {
19714 continue;
19715 }
19716 regcm = insd->lr->classes;
19717 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019718 (used[insd->lr->color])) {
19719 continue;
19720 }
19721 if (interfere(rstate, range, insd->lr)) {
19722 continue;
19723 }
19724 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019725 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019726 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019727 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019728 * of something else that feeds into the phi function.
19729 */
19730 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019731 if (phi->lr->color != REG_UNSET) {
19732 if (used[phi->lr->color]) {
19733 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019734 }
19735 }
19736 else {
19737 expr = triple_rhs(state, phi->def, 0);
19738 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19739 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019740 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019741 if (!*expr) {
19742 continue;
19743 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019744 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019745 if (lr->color == REG_UNSET) {
19746 continue;
19747 }
19748 regcm = lr->classes;
19749 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019750 (used[lr->color])) {
19751 continue;
19752 }
19753 if (interfere(rstate, range, lr)) {
19754 continue;
19755 }
19756 range->color = lr->color;
19757 }
19758 }
19759 }
19760 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019761 lrd = live_range_head(state, range, 0);
19762 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19763 expr = triple_rhs(state, lrd->def, 0);
19764 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019765 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019766 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019767 if (!*expr) {
19768 continue;
19769 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019770 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019771 if (lr->color == REG_UNSET) {
19772 continue;
19773 }
19774 regcm = lr->classes;
19775 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019776 (used[lr->color])) {
19777 continue;
19778 }
19779 if (interfere(rstate, range, lr)) {
19780 continue;
19781 }
19782 range->color = lr->color;
19783 break;
19784 }
19785 }
19786 /* If I have not opportunitically picked a useful color
19787 * pick the first color that is free.
19788 */
19789 if (range->color == REG_UNSET) {
19790 range->color =
19791 arch_select_free_register(state, used, range->classes);
19792 }
19793 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019794 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019795 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019796 if (split_ranges(state, rstate, used, range)) {
19797 return 0;
19798 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019799 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019800 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019801 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019802 lrd = edge->node->defs;
19803 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019804 warning(state, lrd->def, " %s %p",
19805 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019806 lrd = lrd->next;
19807 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019808 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019809 warning(state, range->defs->def, "range: ");
19810 lrd = range->defs;
19811 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019812 warning(state, lrd->def, " %s %p",
19813 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019814 lrd = lrd->next;
19815 } while(lrd != range->defs);
19816
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019817 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019818 range->classes);
19819 for(i = 0; i < MAX_REGISTERS; i++) {
19820 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019821 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019822 arch_reg_str(i));
19823 }
19824 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019825 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019826 }
Eric Biederman530b5192003-07-01 10:05:30 +000019827 range->classes &= arch_reg_regcm(state, range->color);
19828 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019829 internal_error(state, range->defs->def, "select_free_color did not?");
19830 }
19831 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019832}
19833
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019834static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019835{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019836 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019837 struct live_range_edge *edge;
19838 struct live_range *range;
19839 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019840 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019841 range = rstate->low;
19842 if (*range->group_prev != range) {
19843 internal_error(state, 0, "lo: *prev != range?");
19844 }
19845 *range->group_prev = range->group_next;
19846 if (range->group_next) {
19847 range->group_next->group_prev = range->group_prev;
19848 }
19849 if (&range->group_next == rstate->low_tail) {
19850 rstate->low_tail = range->group_prev;
19851 }
19852 if (rstate->low == range) {
19853 internal_error(state, 0, "low: next != prev?");
19854 }
19855 }
19856 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019857 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019858 range = rstate->high;
19859 if (*range->group_prev != range) {
19860 internal_error(state, 0, "hi: *prev != range?");
19861 }
19862 *range->group_prev = range->group_next;
19863 if (range->group_next) {
19864 range->group_next->group_prev = range->group_prev;
19865 }
19866 if (&range->group_next == rstate->high_tail) {
19867 rstate->high_tail = range->group_prev;
19868 }
19869 if (rstate->high == range) {
19870 internal_error(state, 0, "high: next != prev?");
19871 }
19872 }
19873 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019874 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019875 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019876 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019877 range->group_prev = 0;
19878 for(edge = range->edges; edge; edge = edge->next) {
19879 struct live_range *node;
19880 node = edge->node;
19881 /* Move nodes from the high to the low list */
19882 if (node->group_prev && (node->color == REG_UNSET) &&
19883 (node->degree == regc_max_size(state, node->classes))) {
19884 if (*node->group_prev != node) {
19885 internal_error(state, 0, "move: *prev != node?");
19886 }
19887 *node->group_prev = node->group_next;
19888 if (node->group_next) {
19889 node->group_next->group_prev = node->group_prev;
19890 }
19891 if (&node->group_next == rstate->high_tail) {
19892 rstate->high_tail = node->group_prev;
19893 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019894 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019895 node->group_prev = rstate->low_tail;
19896 node->group_next = 0;
19897 *rstate->low_tail = node;
19898 rstate->low_tail = &node->group_next;
19899 if (*node->group_prev != node) {
19900 internal_error(state, 0, "move2: *prev != node?");
19901 }
19902 }
19903 node->degree -= 1;
19904 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019905 colored = color_graph(state, rstate);
19906 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019907 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019908 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019909 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019910 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019911 if (colored) {
19912 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19913 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019914 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019915 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019916}
19917
Eric Biedermana96d6a92003-05-13 20:45:19 +000019918static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19919{
19920 struct live_range *lr;
19921 struct live_range_edge *edge;
19922 struct triple *ins, *first;
19923 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019924 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019925 ins = first;
19926 do {
19927 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019928 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019929 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019930 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019931 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019932 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019933 if (lr->color == REG_UNSET) {
19934 internal_error(state, ins,
19935 "triple without a color");
19936 }
19937 /* Find the registers used by the edges */
19938 memset(used, 0, sizeof(used));
19939 for(edge = lr->edges; edge; edge = edge->next) {
19940 if (edge->node->color == REG_UNSET) {
19941 internal_error(state, 0,
19942 "live range without a color");
19943 }
19944 reg_fill_used(state, used, edge->node->color);
19945 }
19946 if (used[lr->color]) {
19947 internal_error(state, ins,
19948 "triple with already used color");
19949 }
19950 }
19951 ins = ins->next;
19952 } while(ins != first);
19953}
19954
Eric Biedermanb138ac82003-04-22 18:44:01 +000019955static void color_triples(struct compile_state *state, struct reg_state *rstate)
19956{
Eric Biederman90089602004-05-28 14:11:54 +000019957 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019958 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019959 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019960 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019961 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019962 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019963 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019964 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019965 "triple without a live range");
19966 }
Eric Biederman90089602004-05-28 14:11:54 +000019967 lrd = &rstate->lrd[ins->id];
19968 lr = lrd->lr;
19969 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019970 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019971 ins = ins->next;
19972 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019973}
19974
Eric Biedermanb138ac82003-04-22 18:44:01 +000019975static struct live_range *merge_sort_lr(
19976 struct live_range *first, struct live_range *last)
19977{
19978 struct live_range *mid, *join, **join_tail, *pick;
19979 size_t size;
19980 size = (last - first) + 1;
19981 if (size >= 2) {
19982 mid = first + size/2;
19983 first = merge_sort_lr(first, mid -1);
19984 mid = merge_sort_lr(mid, last);
19985
19986 join = 0;
19987 join_tail = &join;
19988 /* merge the two lists */
19989 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019990 if ((first->degree < mid->degree) ||
19991 ((first->degree == mid->degree) &&
19992 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019993 pick = first;
19994 first = first->group_next;
19995 if (first) {
19996 first->group_prev = 0;
19997 }
19998 }
19999 else {
20000 pick = mid;
20001 mid = mid->group_next;
20002 if (mid) {
20003 mid->group_prev = 0;
20004 }
20005 }
20006 pick->group_next = 0;
20007 pick->group_prev = join_tail;
20008 *join_tail = pick;
20009 join_tail = &pick->group_next;
20010 }
20011 /* Splice the remaining list */
20012 pick = (first)? first : mid;
20013 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020014 if (pick) {
20015 pick->group_prev = join_tail;
20016 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020017 }
20018 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020019 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020020 first = 0;
20021 }
20022 join = first;
20023 }
20024 return join;
20025}
20026
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020027static void ids_from_rstate(struct compile_state *state,
20028 struct reg_state *rstate)
20029{
20030 struct triple *ins, *first;
20031 if (!rstate->defs) {
20032 return;
20033 }
20034 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020035 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020036 FILE *fp = state->dbgout;
20037 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020038 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020039 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020040 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020041 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020042 ins = first;
20043 do {
20044 if (ins->id) {
20045 struct live_range_def *lrd;
20046 lrd = &rstate->lrd[ins->id];
20047 ins->id = lrd->orig_id;
20048 }
20049 ins = ins->next;
20050 } while(ins != first);
20051}
20052
20053static void cleanup_live_edges(struct reg_state *rstate)
20054{
20055 int i;
20056 /* Free the edges on each node */
20057 for(i = 1; i <= rstate->ranges; i++) {
20058 remove_live_edges(rstate, &rstate->lr[i]);
20059 }
20060}
20061
20062static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20063{
20064 cleanup_live_edges(rstate);
20065 xfree(rstate->lrd);
20066 xfree(rstate->lr);
20067
20068 /* Free the variable lifetime information */
20069 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020070 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020071 }
20072 rstate->defs = 0;
20073 rstate->ranges = 0;
20074 rstate->lrd = 0;
20075 rstate->lr = 0;
20076 rstate->blocks = 0;
20077}
20078
Eric Biederman153ea352003-06-20 14:43:20 +000020079static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020080static void allocate_registers(struct compile_state *state)
20081{
20082 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020083 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020084
20085 /* Clear out the reg_state */
20086 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020087 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020088
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020089 do {
20090 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000020091 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000020092 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020093 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020094
Eric Biederman5ade04a2003-10-22 04:03:46 +000020095 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020096 FILE *fp = state->errout;
20097 fprintf(fp, "pass: %d\n", rstate.passes);
20098 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020099 }
Eric Biederman153ea352003-06-20 14:43:20 +000020100
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020101 /* Restore ids */
20102 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020103
Eric Biedermanf96a8102003-06-16 16:57:34 +000020104 /* Cleanup the temporary data structures */
20105 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020106
Eric Biedermanf96a8102003-06-16 16:57:34 +000020107 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020108 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020109
Eric Biedermanf96a8102003-06-16 16:57:34 +000020110 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000020111 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020112
Eric Biederman153ea352003-06-20 14:43:20 +000020113 /* Fix two simultaneous uses of the same register.
20114 * In a few pathlogical cases a partial untangle moves
20115 * the tangle to a part of the graph we won't revisit.
20116 * So we keep looping until we have no more tangle fixes
20117 * to apply.
20118 */
20119 do {
20120 tangles = correct_tangles(state, rstate.blocks);
20121 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020122
Eric Biederman5ade04a2003-10-22 04:03:46 +000020123
Eric Biederman90089602004-05-28 14:11:54 +000020124 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020125 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020126
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020127 /* Allocate and initialize the live ranges */
20128 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020129
Eric Biederman90089602004-05-28 14:11:54 +000020130 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020131 * buys me nothing. The code is left this way in case
20132 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020133 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020134 */
20135 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020136 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020137 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020138 }
20139
Eric Biederman153ea352003-06-20 14:43:20 +000020140 /* Remove any previous live edge calculations */
20141 cleanup_live_edges(&rstate);
20142
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020143 /* Compute the interference graph */
20144 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020145 state, &state->bb, rstate.blocks,
20146 graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020147
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020148 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020149 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020150 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020151 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020152 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020153 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020154 print_interference_ins, &rstate);
20155 }
20156
20157 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020158
Eric Biederman5ade04a2003-10-22 04:03:46 +000020159 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020160 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020161 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020162 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020163
Eric Biederman83b991a2003-10-11 06:20:25 +000020164#if DEBUG_CONSISTENCY > 1
20165# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020166 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020167# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020168 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020169 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020170 state, &state->bb, rstate.blocks,
20171 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020172# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020173 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020174#endif
20175#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020176
20177 /* Build the groups low and high. But with the nodes
20178 * first sorted by degree order.
20179 */
20180 rstate.low_tail = &rstate.low;
20181 rstate.high_tail = &rstate.high;
20182 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20183 if (rstate.high) {
20184 rstate.high->group_prev = &rstate.high;
20185 }
20186 for(point = &rstate.high; *point; point = &(*point)->group_next)
20187 ;
20188 rstate.high_tail = point;
20189 /* Walk through the high list and move everything that needs
20190 * to be onto low.
20191 */
20192 for(point = &rstate.high; *point; point = next) {
20193 struct live_range *range;
20194 next = &(*point)->group_next;
20195 range = *point;
20196
20197 /* If it has a low degree or it already has a color
20198 * place the node in low.
20199 */
20200 if ((range->degree < regc_max_size(state, range->classes)) ||
20201 (range->color != REG_UNSET)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020202 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020203 range - rstate.lr, range->degree,
20204 (range->color != REG_UNSET) ? " (colored)": "");
20205 *range->group_prev = range->group_next;
20206 if (range->group_next) {
20207 range->group_next->group_prev = range->group_prev;
20208 }
20209 if (&range->group_next == rstate.high_tail) {
20210 rstate.high_tail = range->group_prev;
20211 }
20212 range->group_prev = rstate.low_tail;
20213 range->group_next = 0;
20214 *rstate.low_tail = range;
20215 rstate.low_tail = &range->group_next;
20216 next = point;
20217 }
20218 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020219 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020220 range - rstate.lr, range->degree,
20221 (range->color != REG_UNSET) ? " (colored)": "");
20222 }
20223 }
20224 /* Color the live_ranges */
20225 colored = color_graph(state, &rstate);
20226 rstate.passes++;
20227 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020228
Eric Biedermana96d6a92003-05-13 20:45:19 +000020229 /* Verify the graph was properly colored */
20230 verify_colors(state, &rstate);
20231
Eric Biedermanb138ac82003-04-22 18:44:01 +000020232 /* Move the colors from the graph to the triples */
20233 color_triples(state, &rstate);
20234
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020235 /* Cleanup the temporary data structures */
20236 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020237
20238 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020239 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020240}
20241
20242/* Sparce Conditional Constant Propogation
20243 * =========================================
20244 */
20245struct ssa_edge;
20246struct flow_block;
20247struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020248 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020249 struct triple *def;
20250 struct ssa_edge *out;
20251 struct flow_block *fblock;
20252 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020253 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020254 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020255 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020256 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020257};
20258struct ssa_edge {
20259 struct lattice_node *src;
20260 struct lattice_node *dst;
20261 struct ssa_edge *work_next;
20262 struct ssa_edge *work_prev;
20263 struct ssa_edge *out_next;
20264};
20265struct flow_edge {
20266 struct flow_block *src;
20267 struct flow_block *dst;
20268 struct flow_edge *work_next;
20269 struct flow_edge *work_prev;
20270 struct flow_edge *in_next;
20271 struct flow_edge *out_next;
20272 int executable;
20273};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020274#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020275struct flow_block {
20276 struct block *block;
20277 struct flow_edge *in;
20278 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020279 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020280};
20281
20282struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020283 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020284 struct lattice_node *lattice;
20285 struct ssa_edge *ssa_edges;
20286 struct flow_block *flow_blocks;
20287 struct flow_edge *flow_work_list;
20288 struct ssa_edge *ssa_work_list;
20289};
20290
20291
Eric Biederman90089602004-05-28 14:11:54 +000020292static int is_scc_const(struct compile_state *state, struct triple *ins)
20293{
20294 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20295}
20296
20297static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20298{
20299 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20300}
20301
20302static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20303{
20304 return is_scc_const(state, lnode->val);
20305}
20306
20307static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20308{
20309 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20310}
20311
Eric Biedermanb138ac82003-04-22 18:44:01 +000020312static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
20313 struct flow_edge *fedge)
20314{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020315 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020316 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020317 fedge,
20318 fedge->src->block?fedge->src->block->last->id: 0,
20319 fedge->dst->block?fedge->dst->block->first->id: 0);
20320 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020321 if ((fedge == scc->flow_work_list) ||
20322 (fedge->work_next != fedge) ||
20323 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020324
20325 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020326 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020327 fedge);
20328 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020329 return;
20330 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020331 if (!scc->flow_work_list) {
20332 scc->flow_work_list = fedge;
20333 fedge->work_next = fedge->work_prev = fedge;
20334 }
20335 else {
20336 struct flow_edge *ftail;
20337 ftail = scc->flow_work_list->work_prev;
20338 fedge->work_next = ftail->work_next;
20339 fedge->work_prev = ftail;
20340 fedge->work_next->work_prev = fedge;
20341 fedge->work_prev->work_next = fedge;
20342 }
20343}
20344
20345static struct flow_edge *scc_next_fedge(
20346 struct compile_state *state, struct scc_state *scc)
20347{
20348 struct flow_edge *fedge;
20349 fedge = scc->flow_work_list;
20350 if (fedge) {
20351 fedge->work_next->work_prev = fedge->work_prev;
20352 fedge->work_prev->work_next = fedge->work_next;
20353 if (fedge->work_next != fedge) {
20354 scc->flow_work_list = fedge->work_next;
20355 } else {
20356 scc->flow_work_list = 0;
20357 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020358 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020359 }
20360 return fedge;
20361}
20362
20363static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20364 struct ssa_edge *sedge)
20365{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020366 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020367 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20368 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020369 sedge->src->def->id,
20370 sedge->dst->def->id);
20371 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020372 if ((sedge == scc->ssa_work_list) ||
20373 (sedge->work_next != sedge) ||
20374 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020375
20376 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020377 fprintf(state->errout, "dupped sedge: %5ld\n",
20378 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020379 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020380 return;
20381 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020382 if (!scc->ssa_work_list) {
20383 scc->ssa_work_list = sedge;
20384 sedge->work_next = sedge->work_prev = sedge;
20385 }
20386 else {
20387 struct ssa_edge *stail;
20388 stail = scc->ssa_work_list->work_prev;
20389 sedge->work_next = stail->work_next;
20390 sedge->work_prev = stail;
20391 sedge->work_next->work_prev = sedge;
20392 sedge->work_prev->work_next = sedge;
20393 }
20394}
20395
20396static struct ssa_edge *scc_next_sedge(
20397 struct compile_state *state, struct scc_state *scc)
20398{
20399 struct ssa_edge *sedge;
20400 sedge = scc->ssa_work_list;
20401 if (sedge) {
20402 sedge->work_next->work_prev = sedge->work_prev;
20403 sedge->work_prev->work_next = sedge->work_next;
20404 if (sedge->work_next != sedge) {
20405 scc->ssa_work_list = sedge->work_next;
20406 } else {
20407 scc->ssa_work_list = 0;
20408 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020409 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020410 }
20411 return sedge;
20412}
20413
20414static void initialize_scc_state(
20415 struct compile_state *state, struct scc_state *scc)
20416{
20417 int ins_count, ssa_edge_count;
20418 int ins_index, ssa_edge_index, fblock_index;
20419 struct triple *first, *ins;
20420 struct block *block;
20421 struct flow_block *fblock;
20422
20423 memset(scc, 0, sizeof(*scc));
20424
20425 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020426 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020427 ins = first;
20428 ins_count = ssa_edge_count = 0;
20429 do {
20430 struct triple_set *edge;
20431 ins_count += 1;
20432 for(edge = ins->use; edge; edge = edge->next) {
20433 ssa_edge_count++;
20434 }
20435 ins = ins->next;
20436 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020437 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020438 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20439 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020440 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020441 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020442 scc->lattice =
20443 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
20444 scc->ssa_edges =
20445 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
20446 scc->flow_blocks =
Eric Biederman90089602004-05-28 14:11:54 +000020447 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020448 "flow_blocks");
20449
20450 /* Initialize pass one collect up the nodes */
20451 fblock = 0;
20452 block = 0;
20453 ins_index = ssa_edge_index = fblock_index = 0;
20454 ins = first;
20455 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020456 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20457 block = ins->u.block;
20458 if (!block) {
20459 internal_error(state, ins, "label without block");
20460 }
20461 fblock_index += 1;
20462 block->vertex = fblock_index;
20463 fblock = &scc->flow_blocks[fblock_index];
20464 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020465 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20466 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020467 }
20468 {
20469 struct lattice_node *lnode;
20470 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020471 lnode = &scc->lattice[ins_index];
20472 lnode->def = ins;
20473 lnode->out = 0;
20474 lnode->fblock = fblock;
20475 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020476 if (lnode->val->op == OP_UNKNOWNVAL) {
20477 lnode->val = 0; /* LATTICE LOW by definition */
20478 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020479 lnode->old_id = ins->id;
20480 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020481 }
20482 ins = ins->next;
20483 } while(ins != first);
20484 /* Initialize pass two collect up the edges */
20485 block = 0;
20486 fblock = 0;
20487 ins = first;
20488 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020489 {
20490 struct triple_set *edge;
20491 struct ssa_edge **stail;
20492 struct lattice_node *lnode;
20493 lnode = &scc->lattice[ins->id];
20494 lnode->out = 0;
20495 stail = &lnode->out;
20496 for(edge = ins->use; edge; edge = edge->next) {
20497 struct ssa_edge *sedge;
20498 ssa_edge_index += 1;
20499 sedge = &scc->ssa_edges[ssa_edge_index];
20500 *stail = sedge;
20501 stail = &sedge->out_next;
20502 sedge->src = lnode;
20503 sedge->dst = &scc->lattice[edge->member->id];
20504 sedge->work_next = sedge->work_prev = sedge;
20505 sedge->out_next = 0;
20506 }
20507 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020508 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20509 struct flow_edge *fedge, **ftail;
20510 struct block_set *bedge;
20511 block = ins->u.block;
20512 fblock = &scc->flow_blocks[block->vertex];
20513 fblock->in = 0;
20514 fblock->out = 0;
20515 ftail = &fblock->out;
20516
20517 fedge = fblock->edges;
20518 bedge = block->edges;
20519 for(; bedge; bedge = bedge->next, fedge++) {
20520 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20521 if (fedge->dst->block != bedge->member) {
20522 internal_error(state, 0, "block mismatch");
20523 }
20524 *ftail = fedge;
20525 ftail = &fedge->out_next;
20526 fedge->out_next = 0;
20527 }
20528 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20529 fedge->src = fblock;
20530 fedge->work_next = fedge->work_prev = fedge;
20531 fedge->executable = 0;
20532 }
20533 }
20534 ins = ins->next;
20535 } while (ins != first);
20536 block = 0;
20537 fblock = 0;
20538 ins = first;
20539 do {
20540 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20541 struct flow_edge **ftail;
20542 struct block_set *bedge;
20543 block = ins->u.block;
20544 fblock = &scc->flow_blocks[block->vertex];
20545 ftail = &fblock->in;
20546 for(bedge = block->use; bedge; bedge = bedge->next) {
20547 struct block *src_block;
20548 struct flow_block *sfblock;
20549 struct flow_edge *sfedge;
20550 src_block = bedge->member;
20551 sfblock = &scc->flow_blocks[src_block->vertex];
20552 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20553 if (sfedge->dst == fblock) {
20554 break;
20555 }
20556 }
20557 if (!sfedge) {
20558 internal_error(state, 0, "edge mismatch");
20559 }
20560 *ftail = sfedge;
20561 ftail = &sfedge->in_next;
20562 sfedge->in_next = 0;
20563 }
20564 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020565 ins = ins->next;
20566 } while(ins != first);
20567 /* Setup a dummy block 0 as a node above the start node */
20568 {
20569 struct flow_block *fblock, *dst;
20570 struct flow_edge *fedge;
20571 fblock = &scc->flow_blocks[0];
20572 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020573 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020574 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020575 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020576 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020577 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020578 fedge->src = fblock;
20579 fedge->dst = dst;
20580 fedge->work_next = fedge;
20581 fedge->work_prev = fedge;
20582 fedge->in_next = fedge->dst->in;
20583 fedge->out_next = 0;
20584 fedge->executable = 0;
20585 fedge->dst->in = fedge;
20586
20587 /* Initialize the work lists */
20588 scc->flow_work_list = 0;
20589 scc->ssa_work_list = 0;
20590 scc_add_fedge(state, scc, fedge);
20591 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020592 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020593 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020594 ins_index, ssa_edge_index, fblock_index);
20595 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020596}
20597
20598
20599static void free_scc_state(
20600 struct compile_state *state, struct scc_state *scc)
20601{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020602 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020603 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020604 struct flow_block *fblock;
20605 fblock = &scc->flow_blocks[i];
20606 if (fblock->edges) {
20607 xfree(fblock->edges);
20608 fblock->edges = 0;
20609 }
20610 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020611 xfree(scc->flow_blocks);
20612 xfree(scc->ssa_edges);
20613 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020614
Eric Biedermanb138ac82003-04-22 18:44:01 +000020615}
20616
20617static struct lattice_node *triple_to_lattice(
20618 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20619{
20620 if (ins->id <= 0) {
20621 internal_error(state, ins, "bad id");
20622 }
20623 return &scc->lattice[ins->id];
20624}
20625
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020626static struct triple *preserve_lval(
20627 struct compile_state *state, struct lattice_node *lnode)
20628{
20629 struct triple *old;
20630 /* Preserve the original value */
20631 if (lnode->val) {
20632 old = dup_triple(state, lnode->val);
20633 if (lnode->val != lnode->def) {
20634 xfree(lnode->val);
20635 }
20636 lnode->val = 0;
20637 } else {
20638 old = 0;
20639 }
20640 return old;
20641}
20642
20643static int lval_changed(struct compile_state *state,
20644 struct triple *old, struct lattice_node *lnode)
20645{
20646 int changed;
20647 /* See if the lattice value has changed */
20648 changed = 1;
20649 if (!old && !lnode->val) {
20650 changed = 0;
20651 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020652 if (changed &&
20653 lnode->val && old &&
20654 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020655 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020656 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20657 changed = 0;
20658 }
20659 if (old) {
20660 xfree(old);
20661 }
20662 return changed;
20663
20664}
20665
Eric Biederman5ade04a2003-10-22 04:03:46 +000020666static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020667 struct compile_state *state, struct scc_state *scc,
20668 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020669{
Eric Biederman90089602004-05-28 14:11:54 +000020670 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20671 display_triple_changes(state->errout, lnode->val, lnode->def);
20672 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020673 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020674 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020675 struct triple *val, **expr;
20676 val = lnode->val? lnode->val : lnode->def;
20677 fprintf(fp, "%p %s %3d %10s (",
20678 lnode->def,
20679 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20680 lnode->def->id,
20681 tops(lnode->def->op));
20682 expr = triple_rhs(state, lnode->def, 0);
20683 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20684 if (*expr) {
20685 fprintf(fp, " %d", (*expr)->id);
20686 }
20687 }
20688 if (val->op == OP_INTCONST) {
20689 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20690 }
20691 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020692 (is_lattice_hi(state, lnode)? "hi":
20693 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020694 changed? "changed" : ""
20695 );
20696 }
20697}
20698
Eric Biedermanb138ac82003-04-22 18:44:01 +000020699static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20700 struct lattice_node *lnode)
20701{
20702 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020703 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020704 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020705 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020706
20707 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020708 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020709
Eric Biedermanb138ac82003-04-22 18:44:01 +000020710 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020711 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020712 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020713 scratch->next = scratch;
20714 scratch->prev = scratch;
20715 scratch->use = 0;
20716
Eric Biederman90089602004-05-28 14:11:54 +000020717 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020718 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020719 dexpr = &lnode->def->param[i];
20720 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020721 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020722 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20723 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020724 *dexpr) {
20725 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020726 tmp = triple_to_lattice(state, scc, *dexpr);
20727 *vexpr = (tmp->val)? tmp->val : tmp->def;
20728 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020729 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020730 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020731 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020732 }
20733 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020734#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020735#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020736#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020737 /* So far it looks like only the strength reduction
20738 * optimization are things I need to worry about.
20739 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020740 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020741 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020742 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020743 internal_error(state, lnode->def, "scratch used?");
20744 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020745 if ((scratch->prev != scratch) ||
20746 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020747 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020748 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020749 internal_error(state, lnode->def, "scratch in list?");
20750 }
20751 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020752 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020753 for(i = 0; i < count; i++) {
20754 vexpr = &scratch->param[i];
20755 if (*vexpr) {
20756 unuse_triple(*vexpr, scratch);
20757 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020758 }
Eric Biederman90089602004-05-28 14:11:54 +000020759 if (lnode->val->op == OP_UNKNOWNVAL) {
20760 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020761 }
Eric Biederman90089602004-05-28 14:11:54 +000020762 /* Find the case when I am lattice high */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020763 if (lnode->val &&
20764 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020765 (memcmp(lnode->val->param, lnode->def->param,
20766 count * sizeof(lnode->val->param[0])) == 0) &&
20767 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020768 lnode->val = lnode->def;
20769 }
Eric Biederman90089602004-05-28 14:11:54 +000020770 /* Only allow lattice high when all of my inputs
20771 * are also lattice high. Occassionally I can
20772 * have constants with a lattice low input, so
20773 * I do not need to check that case.
20774 */
20775 if (is_lattice_hi(state, lnode)) {
20776 struct lattice_node *tmp;
20777 int rhs;
20778 rhs = lnode->val->rhs;
20779 for(i = 0; i < rhs; i++) {
20780 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20781 if (!is_lattice_hi(state, tmp)) {
20782 lnode->val = 0;
20783 break;
20784 }
20785 }
20786 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020787 /* Find the cases that are always lattice lo */
20788 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020789 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020790 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020791 lnode->val = 0;
20792 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020793 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020794 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020795 /* See if this value should not change */
Eric Biederman90089602004-05-28 14:11:54 +000020796 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020797 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020798 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020799 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020800#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020801#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020802#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020803 if (changed) {
20804 internal_warning(state, lnode->def, "non def changes value?");
20805 }
20806 lnode->val = 0;
20807 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020808
Eric Biederman83b991a2003-10-11 06:20:25 +000020809 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020810 if (lnode->val != scratch) {
20811 xfree(scratch);
20812 }
Eric Biederman90089602004-05-28 14:11:54 +000020813
Eric Biedermanb138ac82003-04-22 18:44:01 +000020814 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020815}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020816
Eric Biederman90089602004-05-28 14:11:54 +000020817
20818static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020819 struct lattice_node *lnode)
20820{
20821 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020822 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020823 int changed;
20824
20825 /* Update the branch value */
20826 changed = compute_lnode_val(state, scc, lnode);
20827 scc_debug_lnode(state, scc, lnode, changed);
20828
20829 /* This only applies to conditional branches */
20830 if (!triple_is_cbranch(state, lnode->def)) {
20831 internal_error(state, lnode->def, "not a conditional branch");
20832 }
20833
Eric Biederman5ade04a2003-10-22 04:03:46 +000020834 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020835 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020836 FILE *fp = state->errout;
20837 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020838 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020839 lnode->def->id);
20840
20841 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020842 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020843 }
Eric Biederman90089602004-05-28 14:11:54 +000020844 fprintf(fp, " )");
20845 if (lnode->def->rhs > 0) {
20846 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020847 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020848 }
Eric Biederman90089602004-05-28 14:11:54 +000020849 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020850 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020851 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020852 for(left = cond->fblock->out; left; left = left->out_next) {
20853 if (left->dst->block->first == lnode->def->next) {
20854 break;
20855 }
20856 }
20857 if (!left) {
20858 internal_error(state, lnode->def, "Cannot find left branch edge");
20859 }
20860 for(right = cond->fblock->out; right; right = right->out_next) {
20861 if (right->dst->block->first == TARG(lnode->def, 0)) {
20862 break;
20863 }
20864 }
20865 if (!right) {
20866 internal_error(state, lnode->def, "Cannot find right branch edge");
20867 }
Eric Biederman90089602004-05-28 14:11:54 +000020868 /* I should only come here if the controlling expressions value
20869 * has changed, which means it must be either a constant or lo.
20870 */
20871 if (is_lattice_hi(state, cond)) {
20872 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020873 return;
20874 }
Eric Biederman90089602004-05-28 14:11:54 +000020875 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020876 scc_add_fedge(state, scc, left);
20877 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020878 }
20879 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020880 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020881 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020882 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020883 }
20884
20885}
20886
Eric Biederman90089602004-05-28 14:11:54 +000020887
20888static void scc_add_sedge_dst(struct compile_state *state,
20889 struct scc_state *scc, struct ssa_edge *sedge)
20890{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020891 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020892 scc_visit_cbranch(state, scc, sedge->dst);
20893 }
20894 else if (triple_is_def(state, sedge->dst->def)) {
20895 scc_add_sedge(state, scc, sedge);
20896 }
20897}
20898
20899static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
20900 struct lattice_node *lnode)
20901{
20902 struct lattice_node *tmp;
20903 struct triple **slot, *old;
20904 struct flow_edge *fedge;
20905 int changed;
20906 int index;
20907 if (lnode->def->op != OP_PHI) {
20908 internal_error(state, lnode->def, "not phi");
20909 }
20910 /* Store the original value */
20911 old = preserve_lval(state, lnode);
20912
20913 /* default to lattice high */
20914 lnode->val = lnode->def;
20915 slot = &RHS(lnode->def, 0);
20916 index = 0;
20917 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20918 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20919 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
20920 index,
20921 fedge->dst->block->vertex,
20922 fedge->executable
20923 );
20924 }
20925 if (!fedge->executable) {
20926 continue;
20927 }
20928 if (!slot[index]) {
20929 internal_error(state, lnode->def, "no phi value");
20930 }
20931 tmp = triple_to_lattice(state, scc, slot[index]);
20932 /* meet(X, lattice low) = lattice low */
20933 if (is_lattice_lo(state, tmp)) {
20934 lnode->val = 0;
20935 }
20936 /* meet(X, lattice high) = X */
20937 else if (is_lattice_hi(state, tmp)) {
20938 lnode->val = lnode->val;
20939 }
20940 /* meet(lattice high, X) = X */
20941 else if (is_lattice_hi(state, lnode)) {
20942 lnode->val = dup_triple(state, tmp->val);
20943 /* Only change the type if necessary */
20944 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20945 lnode->val->type = lnode->def->type;
20946 }
20947 }
20948 /* meet(const, const) = const or lattice low */
20949 else if (!constants_equal(state, lnode->val, tmp->val)) {
20950 lnode->val = 0;
20951 }
20952
20953 /* meet(lattice low, X) = lattice low */
20954 if (is_lattice_lo(state, lnode)) {
20955 lnode->val = 0;
20956 break;
20957 }
20958 }
20959 changed = lval_changed(state, old, lnode);
20960 scc_debug_lnode(state, scc, lnode, changed);
20961
20962 /* If the lattice value has changed update the work lists. */
20963 if (changed) {
20964 struct ssa_edge *sedge;
20965 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20966 scc_add_sedge_dst(state, scc, sedge);
20967 }
20968 }
20969}
20970
20971
Eric Biedermanb138ac82003-04-22 18:44:01 +000020972static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20973 struct lattice_node *lnode)
20974{
20975 int changed;
20976
Eric Biederman90089602004-05-28 14:11:54 +000020977 if (!triple_is_def(state, lnode->def)) {
20978 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020979 }
Eric Biederman90089602004-05-28 14:11:54 +000020980 changed = compute_lnode_val(state, scc, lnode);
20981 scc_debug_lnode(state, scc, lnode, changed);
20982
20983 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020984 struct ssa_edge *sedge;
20985 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020986 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020987 }
20988 }
20989}
20990
20991static void scc_writeback_values(
20992 struct compile_state *state, struct scc_state *scc)
20993{
20994 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020995 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020996 ins = first;
20997 do {
20998 struct lattice_node *lnode;
20999 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021000 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000021001 if (is_lattice_hi(state, lnode) &&
21002 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000021003 {
21004 struct flow_edge *fedge;
21005 int executable;
21006 executable = 0;
21007 for(fedge = lnode->fblock->in;
21008 !executable && fedge; fedge = fedge->in_next) {
21009 executable |= fedge->executable;
21010 }
21011 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000021012 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021013 "lattice node %d %s->%s still high?",
21014 ins->id,
21015 tops(lnode->def->op),
21016 tops(lnode->val->op));
21017 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021018 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021019 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021020
Eric Biederman83b991a2003-10-11 06:20:25 +000021021 /* Restore id */
21022 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021023 if (lnode->val && (lnode->val != ins)) {
21024 /* See if it something I know how to write back */
21025 switch(lnode->val->op) {
21026 case OP_INTCONST:
21027 mkconst(state, ins, lnode->val->u.cval);
21028 break;
21029 case OP_ADDRCONST:
21030 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021031 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021032 break;
21033 default:
21034 /* By default don't copy the changes,
21035 * recompute them in place instead.
21036 */
21037 simplify(state, ins);
21038 break;
21039 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021040 if (is_const(lnode->val) &&
21041 !constants_equal(state, lnode->val, ins)) {
21042 internal_error(state, 0, "constants not equal");
21043 }
21044 /* Free the lattice nodes */
21045 xfree(lnode->val);
21046 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021047 }
21048 ins = ins->next;
21049 } while(ins != first);
21050}
21051
21052static void scc_transform(struct compile_state *state)
21053{
21054 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021055 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21056 return;
21057 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021058
21059 initialize_scc_state(state, &scc);
21060
21061 while(scc.flow_work_list || scc.ssa_work_list) {
21062 struct flow_edge *fedge;
21063 struct ssa_edge *sedge;
21064 struct flow_edge *fptr;
21065 while((fedge = scc_next_fedge(state, &scc))) {
21066 struct block *block;
21067 struct triple *ptr;
21068 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021069 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021070 int done;
21071 if (fedge->executable) {
21072 continue;
21073 }
21074 if (!fedge->dst) {
21075 internal_error(state, 0, "fedge without dst");
21076 }
21077 if (!fedge->src) {
21078 internal_error(state, 0, "fedge without src");
21079 }
21080 fedge->executable = 1;
21081 fblock = fedge->dst;
21082 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021083 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021084 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21085 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021086 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021087 }
21088 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021089
Eric Biederman5ade04a2003-10-22 04:03:46 +000021090 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000021091 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021092 block->vertex, reps);
21093 }
21094
Eric Biedermanb138ac82003-04-22 18:44:01 +000021095 done = 0;
21096 for(ptr = block->first; !done; ptr = ptr->next) {
21097 struct lattice_node *lnode;
21098 done = (ptr == block->last);
21099 lnode = &scc.lattice[ptr->id];
21100 if (ptr->op == OP_PHI) {
21101 scc_visit_phi(state, &scc, lnode);
21102 }
Eric Biederman90089602004-05-28 14:11:54 +000021103 else if ((reps == 1) && triple_is_def(state, ptr))
21104 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021105 scc_visit_expr(state, &scc, lnode);
21106 }
21107 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021108 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021109 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021110 struct flow_edge *out;
21111 for(out = fblock->out; out; out = out->out_next) {
21112 scc_add_fedge(state, &scc, out);
21113 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021114 }
21115 }
21116 while((sedge = scc_next_sedge(state, &scc))) {
21117 struct lattice_node *lnode;
21118 struct flow_block *fblock;
21119 lnode = sedge->dst;
21120 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021121
21122 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021123 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021124 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021125 sedge->src->def->id,
21126 sedge->dst->def->id);
21127 }
21128
Eric Biedermanb138ac82003-04-22 18:44:01 +000021129 if (lnode->def->op == OP_PHI) {
21130 scc_visit_phi(state, &scc, lnode);
21131 }
21132 else {
21133 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21134 if (fptr->executable) {
21135 break;
21136 }
21137 }
21138 if (fptr) {
21139 scc_visit_expr(state, &scc, lnode);
21140 }
21141 }
21142 }
21143 }
21144
21145 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021146 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021147 rebuild_ssa_form(state);
21148
Eric Biederman90089602004-05-28 14:11:54 +000021149 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021150}
21151
21152
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021153static void transform_to_arch_instructions(struct compile_state *state)
21154{
21155 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021156 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021157 ins = first;
21158 do {
21159 ins = transform_to_arch_instruction(state, ins);
21160 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021161
Eric Biederman90089602004-05-28 14:11:54 +000021162 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021163}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021164
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021165#if DEBUG_CONSISTENCY
21166static void verify_uses(struct compile_state *state)
21167{
21168 struct triple *first, *ins;
21169 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021170 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021171 ins = first;
21172 do {
21173 struct triple **expr;
21174 expr = triple_rhs(state, ins, 0);
21175 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021176 struct triple *rhs;
21177 rhs = *expr;
21178 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021179 if (set->member == ins) {
21180 break;
21181 }
21182 }
21183 if (!set) {
21184 internal_error(state, ins, "rhs not used");
21185 }
21186 }
21187 expr = triple_lhs(state, ins, 0);
21188 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021189 struct triple *lhs;
21190 lhs = *expr;
21191 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021192 if (set->member == ins) {
21193 break;
21194 }
21195 }
21196 if (!set) {
21197 internal_error(state, ins, "lhs not used");
21198 }
21199 }
Eric Biederman90089602004-05-28 14:11:54 +000021200 expr = triple_misc(state, ins, 0);
21201 if (ins->op != OP_PHI) {
21202 for(; expr; expr = triple_targ(state, ins, expr)) {
21203 struct triple *misc;
21204 misc = *expr;
21205 for(set = misc?misc->use:0; set; set = set->next) {
21206 if (set->member == ins) {
21207 break;
21208 }
21209 }
21210 if (!set) {
21211 internal_error(state, ins, "misc not used");
21212 }
21213 }
21214 }
21215 if (!triple_is_ret(state, ins)) {
21216 expr = triple_targ(state, ins, 0);
21217 for(; expr; expr = triple_targ(state, ins, expr)) {
21218 struct triple *targ;
21219 targ = *expr;
21220 for(set = targ?targ->use:0; set; set = set->next) {
21221 if (set->member == ins) {
21222 break;
21223 }
21224 }
21225 if (!set) {
21226 internal_error(state, ins, "targ not used");
21227 }
21228 }
21229 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021230 ins = ins->next;
21231 } while(ins != first);
21232
21233}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021234static void verify_blocks_present(struct compile_state *state)
21235{
21236 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021237 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021238 return;
21239 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021240 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021241 ins = first;
21242 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021243 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021244 if (triple_stores_block(state, ins)) {
21245 if (!ins->u.block) {
21246 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021247 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021248 }
21249 }
21250 ins = ins->next;
21251 } while(ins != first);
21252
21253
21254}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021255
21256static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21257{
21258 struct block_set *bedge;
21259 struct block *targ;
21260 targ = block_of_triple(state, edge);
21261 for(bedge = block->edges; bedge; bedge = bedge->next) {
21262 if (bedge->member == targ) {
21263 return 1;
21264 }
21265 }
21266 return 0;
21267}
21268
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021269static void verify_blocks(struct compile_state *state)
21270{
21271 struct triple *ins;
21272 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021273 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021274 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021275 if (!block) {
21276 return;
21277 }
Eric Biederman530b5192003-07-01 10:05:30 +000021278 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021279 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021280 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021281 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021282 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021283 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021284 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021285 internal_error(state, ins, "inconsitent block specified");
21286 }
Eric Biederman530b5192003-07-01 10:05:30 +000021287 valid_ins(state, ins);
21288 }
21289 users = 0;
21290 for(user = block->use; user; user = user->next) {
21291 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021292 if (!user->member->first) {
21293 internal_error(state, block->first, "user is empty");
21294 }
Eric Biederman90089602004-05-28 14:11:54 +000021295 if ((block == state->bb.last_block) &&
21296 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021297 continue;
21298 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021299 for(edge = user->member->edges; edge; edge = edge->next) {
21300 if (edge->member == block) {
21301 break;
21302 }
21303 }
21304 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021305 internal_error(state, user->member->first,
21306 "user does not use block");
21307 }
21308 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021309 if (triple_is_branch(state, block->last)) {
21310 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021311 expr = triple_edge_targ(state, block->last, 0);
21312 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021313 if (*expr && !edge_present(state, block, *expr)) {
21314 internal_error(state, block->last, "no edge to targ");
21315 }
21316 }
Eric Biederman530b5192003-07-01 10:05:30 +000021317 }
Eric Biederman90089602004-05-28 14:11:54 +000021318 if (!triple_is_ubranch(state, block->last) &&
21319 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021320 !edge_present(state, block, block->last->next)) {
21321 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021322 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021323 for(edge = block->edges; edge; edge = edge->next) {
21324 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021325 if (user->member == block) {
21326 break;
21327 }
21328 }
21329 if (!user || user->member != block) {
21330 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021331 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021332 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021333 if (!edge->member->first) {
21334 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021335 }
Eric Biederman530b5192003-07-01 10:05:30 +000021336 }
21337 if (block->users != users) {
21338 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021339 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021340 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021341 }
21342 if (!triple_stores_block(state, block->last->next)) {
21343 internal_error(state, block->last->next,
21344 "cannot find next block");
21345 }
21346 block = block->last->next->u.block;
21347 if (!block) {
21348 internal_error(state, block->last->next,
21349 "bad next block");
21350 }
Eric Biederman90089602004-05-28 14:11:54 +000021351 } while(block != state->bb.first_block);
21352 if (blocks != state->bb.last_vertex) {
21353 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21354 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021355 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021356}
21357
21358static void verify_domination(struct compile_state *state)
21359{
21360 struct triple *first, *ins;
21361 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021362 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021363 return;
21364 }
21365
Eric Biederman83b991a2003-10-11 06:20:25 +000021366 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021367 ins = first;
21368 do {
21369 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021370 struct triple **slot;
21371 struct triple *use_point;
21372 int i, zrhs;
21373 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021374 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021375 slot = &RHS(set->member, 0);
21376 /* See if the use is on the right hand side */
21377 for(i = 0; i < zrhs; i++) {
21378 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021379 break;
21380 }
21381 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021382 if (i < zrhs) {
21383 use_point = set->member;
21384 if (set->member->op == OP_PHI) {
21385 struct block_set *bset;
21386 int edge;
21387 bset = set->member->u.block->use;
21388 for(edge = 0; bset && (edge < i); edge++) {
21389 bset = bset->next;
21390 }
21391 if (!bset) {
21392 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021393 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021394 }
21395 use_point = bset->member->last;
21396 }
21397 }
21398 if (use_point &&
21399 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021400 if (is_const(ins)) {
21401 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021402 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021403 }
21404 else {
21405 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021406 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021407 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021408 }
21409 }
21410 ins = ins->next;
21411 } while(ins != first);
21412}
21413
Eric Biederman83b991a2003-10-11 06:20:25 +000021414static void verify_rhs(struct compile_state *state)
21415{
21416 struct triple *first, *ins;
21417 first = state->first;
21418 ins = first;
21419 do {
21420 struct triple **slot;
21421 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021422 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021423 slot = &RHS(ins, 0);
21424 for(i = 0; i < zrhs; i++) {
21425 if (slot[i] == 0) {
21426 internal_error(state, ins,
21427 "missing rhs %d on %s",
21428 i, tops(ins->op));
21429 }
21430 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21431 internal_error(state, ins,
21432 "ins == rhs[%d] on %s",
21433 i, tops(ins->op));
21434 }
21435 }
21436 ins = ins->next;
21437 } while(ins != first);
21438}
21439
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021440static void verify_piece(struct compile_state *state)
21441{
21442 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021443 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021444 ins = first;
21445 do {
21446 struct triple *ptr;
21447 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021448 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021449 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21450 if (ptr != LHS(ins, i)) {
21451 internal_error(state, ins, "malformed lhs on %s",
21452 tops(ins->op));
21453 }
21454 if (ptr->op != OP_PIECE) {
21455 internal_error(state, ins, "bad lhs op %s at %d on %s",
21456 tops(ptr->op), i, tops(ins->op));
21457 }
21458 if (ptr->u.cval != i) {
21459 internal_error(state, ins, "bad u.cval of %d %d expected",
21460 ptr->u.cval, i);
21461 }
21462 }
21463 ins = ins->next;
21464 } while(ins != first);
21465}
Eric Biederman83b991a2003-10-11 06:20:25 +000021466
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021467static void verify_ins_colors(struct compile_state *state)
21468{
21469 struct triple *first, *ins;
21470
Eric Biederman83b991a2003-10-11 06:20:25 +000021471 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021472 ins = first;
21473 do {
21474 ins = ins->next;
21475 } while(ins != first);
21476}
Eric Biederman90089602004-05-28 14:11:54 +000021477
21478static void verify_unknown(struct compile_state *state)
21479{
21480 struct triple *first, *ins;
21481 if ( (unknown_triple.next != &unknown_triple) ||
21482 (unknown_triple.prev != &unknown_triple) ||
21483#if 0
21484 (unknown_triple.use != 0) ||
21485#endif
21486 (unknown_triple.op != OP_UNKNOWNVAL) ||
21487 (unknown_triple.lhs != 0) ||
21488 (unknown_triple.rhs != 0) ||
21489 (unknown_triple.misc != 0) ||
21490 (unknown_triple.targ != 0) ||
21491 (unknown_triple.template_id != 0) ||
21492 (unknown_triple.id != -1) ||
21493 (unknown_triple.type != &unknown_type) ||
21494 (unknown_triple.occurance != &dummy_occurance) ||
21495 (unknown_triple.param[0] != 0) ||
21496 (unknown_triple.param[1] != 0)) {
21497 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21498 }
21499 if ( (dummy_occurance.count != 2) ||
21500 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21501 (strcmp(dummy_occurance.function, "") != 0) ||
21502 (dummy_occurance.col != 0) ||
21503 (dummy_occurance.parent != 0)) {
21504 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21505 }
21506 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21507 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21508 }
21509 first = state->first;
21510 ins = first;
21511 do {
21512 int params, i;
21513 if (ins == &unknown_triple) {
21514 internal_error(state, ins, "unknown triple in list");
21515 }
21516 params = TRIPLE_SIZE(ins);
21517 for(i = 0; i < params; i++) {
21518 if (ins->param[i] == &unknown_triple) {
21519 internal_error(state, ins, "unknown triple used!");
21520 }
21521 }
21522 ins = ins->next;
21523 } while(ins != first);
21524}
21525
21526static void verify_types(struct compile_state *state)
21527{
21528 struct triple *first, *ins;
21529 first = state->first;
21530 ins = first;
21531 do {
21532 struct type *invalid;
21533 invalid = invalid_type(state, ins->type);
21534 if (invalid) {
21535 FILE *fp = state->errout;
21536 fprintf(fp, "type: ");
21537 name_of(fp, ins->type);
21538 fprintf(fp, "\n");
21539 fprintf(fp, "invalid type: ");
21540 name_of(fp, invalid);
21541 fprintf(fp, "\n");
21542 internal_error(state, ins, "invalid ins type");
21543 }
21544 } while(ins != first);
21545}
21546
21547static void verify_copy(struct compile_state *state)
21548{
21549 struct triple *first, *ins, *next;
21550 first = state->first;
21551 next = ins = first;
21552 do {
21553 ins = next;
21554 next = ins->next;
21555 if (ins->op != OP_COPY) {
21556 continue;
21557 }
21558 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21559 FILE *fp = state->errout;
21560 fprintf(fp, "src type: ");
21561 name_of(fp, RHS(ins, 0)->type);
21562 fprintf(fp, "\n");
21563 fprintf(fp, "dst type: ");
21564 name_of(fp, ins->type);
21565 fprintf(fp, "\n");
21566 internal_error(state, ins, "type mismatch in copy");
21567 }
21568 } while(next != first);
21569}
21570
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021571static void verify_consistency(struct compile_state *state)
21572{
Eric Biederman90089602004-05-28 14:11:54 +000021573 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021574 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021575 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021576 verify_blocks(state);
21577 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021578 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021579 verify_piece(state);
21580 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021581 verify_types(state);
21582 verify_copy(state);
21583 if (state->compiler->debug & DEBUG_VERIFICATION) {
21584 fprintf(state->dbgout, "consistency verified\n");
21585 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021586}
21587#else
Eric Biederman153ea352003-06-20 14:43:20 +000021588static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021589#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021590
21591static void optimize(struct compile_state *state)
21592{
Eric Biederman90089602004-05-28 14:11:54 +000021593 /* Join all of the functions into one giant function */
21594 join_functions(state);
21595
Eric Biederman5ade04a2003-10-22 04:03:46 +000021596 /* Dump what the instruction graph intially looks like */
21597 print_triples(state);
21598
Eric Biederman0babc1c2003-05-09 02:39:00 +000021599 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021600 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021601 print_triples(state);
21602
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021603 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021604 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021605 state->bb.first = state->first;
21606 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021607
Eric Biederman530b5192003-07-01 10:05:30 +000021608 /* Transform the code to ssa form. */
21609 /*
21610 * The transformation to ssa form puts a phi function
21611 * on each of edge of a dominance frontier where that
21612 * phi function might be needed. At -O2 if we don't
21613 * eleminate the excess phi functions we can get an
21614 * exponential code size growth. So I kill the extra
21615 * phi functions early and I kill them often.
21616 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021617 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021618 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021619
Eric Biederman83b991a2003-10-11 06:20:25 +000021620 /* Remove dead code */
21621 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021622 verify_consistency(state);
21623
Eric Biedermanb138ac82003-04-22 18:44:01 +000021624 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021625 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021626 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021627 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021628 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021629 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021630#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021631#warning "WISHLIST implement single use constants (least possible register pressure)"
21632#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021633#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021634 /* Select architecture instructions and an initial partial
21635 * coloring based on architecture constraints.
21636 */
21637 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021638 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021639
Eric Biederman83b991a2003-10-11 06:20:25 +000021640 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021641 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021642 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021643
Eric Biedermanb138ac82003-04-22 18:44:01 +000021644 /* Color all of the variables to see if they will fit in registers */
21645 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021646 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021647
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021648 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021649 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021650
Eric Biedermanb138ac82003-04-22 18:44:01 +000021651 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021652 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021653
Eric Biedermanb138ac82003-04-22 18:44:01 +000021654 /* Remove the optimization information.
21655 * This is more to check for memory consistency than to free memory.
21656 */
Eric Biederman90089602004-05-28 14:11:54 +000021657 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021658}
21659
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021660static void print_op_asm(struct compile_state *state,
21661 struct triple *ins, FILE *fp)
21662{
21663 struct asm_info *info;
21664 const char *ptr;
21665 unsigned lhs, rhs, i;
21666 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021667 lhs = ins->lhs;
21668 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021669 /* Don't count the clobbers in lhs */
21670 for(i = 0; i < lhs; i++) {
21671 if (LHS(ins, i)->type == &void_type) {
21672 break;
21673 }
21674 }
21675 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021676 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021677 fputc('\t', fp);
21678 for(ptr = info->str; *ptr; ptr++) {
21679 char *next;
21680 unsigned long param;
21681 struct triple *piece;
21682 if (*ptr != '%') {
21683 fputc(*ptr, fp);
21684 continue;
21685 }
21686 ptr++;
21687 if (*ptr == '%') {
21688 fputc('%', fp);
21689 continue;
21690 }
21691 param = strtoul(ptr, &next, 10);
21692 if (ptr == next) {
21693 error(state, ins, "Invalid asm template");
21694 }
21695 if (param >= (lhs + rhs)) {
21696 error(state, ins, "Invalid param %%%u in asm template",
21697 param);
21698 }
21699 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
21700 fprintf(fp, "%s",
21701 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021702 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021703 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021704 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021705}
21706
21707
21708/* Only use the low x86 byte registers. This allows me
21709 * allocate the entire register when a byte register is used.
21710 */
21711#define X86_4_8BIT_GPRS 1
21712
Eric Biederman83b991a2003-10-11 06:20:25 +000021713/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021714#define X86_MMX_REGS (1<<0)
21715#define X86_XMM_REGS (1<<1)
21716#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021717
Eric Biedermanb138ac82003-04-22 18:44:01 +000021718/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021719#define REGC_FLAGS 0
21720#define REGC_GPR8 1
21721#define REGC_GPR16 2
21722#define REGC_GPR32 3
21723#define REGC_DIVIDEND64 4
21724#define REGC_DIVIDEND32 5
21725#define REGC_MMX 6
21726#define REGC_XMM 7
21727#define REGC_GPR32_8 8
21728#define REGC_GPR16_8 9
21729#define REGC_GPR8_LO 10
21730#define REGC_IMM32 11
21731#define REGC_IMM16 12
21732#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021733#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021734#if LAST_REGC >= MAX_REGC
21735#error "MAX_REGC is to low"
21736#endif
21737
21738/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021739#define REGCM_FLAGS (1 << REGC_FLAGS)
21740#define REGCM_GPR8 (1 << REGC_GPR8)
21741#define REGCM_GPR16 (1 << REGC_GPR16)
21742#define REGCM_GPR32 (1 << REGC_GPR32)
21743#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21744#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21745#define REGCM_MMX (1 << REGC_MMX)
21746#define REGCM_XMM (1 << REGC_XMM)
21747#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21748#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21749#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21750#define REGCM_IMM32 (1 << REGC_IMM32)
21751#define REGCM_IMM16 (1 << REGC_IMM16)
21752#define REGCM_IMM8 (1 << REGC_IMM8)
21753#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021754#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021755
21756/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021757#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021758#define REGC_FLAGS_FIRST REG_EFLAGS
21759#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021760#define REG_AL 3
21761#define REG_BL 4
21762#define REG_CL 5
21763#define REG_DL 6
21764#define REG_AH 7
21765#define REG_BH 8
21766#define REG_CH 9
21767#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021768#define REGC_GPR8_LO_FIRST REG_AL
21769#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021770#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021771#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021772#define REG_AX 11
21773#define REG_BX 12
21774#define REG_CX 13
21775#define REG_DX 14
21776#define REG_SI 15
21777#define REG_DI 16
21778#define REG_BP 17
21779#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021780#define REGC_GPR16_FIRST REG_AX
21781#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021782#define REG_EAX 19
21783#define REG_EBX 20
21784#define REG_ECX 21
21785#define REG_EDX 22
21786#define REG_ESI 23
21787#define REG_EDI 24
21788#define REG_EBP 25
21789#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021790#define REGC_GPR32_FIRST REG_EAX
21791#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021792#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021793#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21794#define REGC_DIVIDEND64_LAST REG_EDXEAX
21795#define REG_DXAX 28
21796#define REGC_DIVIDEND32_FIRST REG_DXAX
21797#define REGC_DIVIDEND32_LAST REG_DXAX
21798#define REG_MMX0 29
21799#define REG_MMX1 30
21800#define REG_MMX2 31
21801#define REG_MMX3 32
21802#define REG_MMX4 33
21803#define REG_MMX5 34
21804#define REG_MMX6 35
21805#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021806#define REGC_MMX_FIRST REG_MMX0
21807#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021808#define REG_XMM0 37
21809#define REG_XMM1 38
21810#define REG_XMM2 39
21811#define REG_XMM3 40
21812#define REG_XMM4 41
21813#define REG_XMM5 42
21814#define REG_XMM6 43
21815#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021816#define REGC_XMM_FIRST REG_XMM0
21817#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021818
21819#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021820#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021821#endif
21822
Eric Biedermanb138ac82003-04-22 18:44:01 +000021823#define LAST_REG REG_XMM7
21824
21825#define REGC_GPR32_8_FIRST REG_EAX
21826#define REGC_GPR32_8_LAST REG_EDX
21827#define REGC_GPR16_8_FIRST REG_AX
21828#define REGC_GPR16_8_LAST REG_DX
21829
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021830#define REGC_IMM8_FIRST -1
21831#define REGC_IMM8_LAST -1
21832#define REGC_IMM16_FIRST -2
21833#define REGC_IMM16_LAST -1
21834#define REGC_IMM32_FIRST -4
21835#define REGC_IMM32_LAST -1
21836
Eric Biedermanb138ac82003-04-22 18:44:01 +000021837#if LAST_REG >= MAX_REGISTERS
21838#error "MAX_REGISTERS to low"
21839#endif
21840
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021841
21842static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021843 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21844 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21845 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21846 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21847 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21848 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21849 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21850 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21851 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21852 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21853 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21854 [REGC_IMM32] = 0,
21855 [REGC_IMM16] = 0,
21856 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021857};
21858
21859static const struct {
21860 int first, last;
21861} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021862 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21863 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21864 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21865 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21866 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21867 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21868 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21869 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21870 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21871 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21872 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21873 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21874 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21875 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021876};
21877
Eric Biederman90089602004-05-28 14:11:54 +000021878#if ARCH_INPUT_REGS != 4
21879#error ARCH_INPUT_REGS size mismatch
21880#endif
21881static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21882 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21883 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21884 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21885 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21886};
21887
21888#if ARCH_OUTPUT_REGS != 4
21889#error ARCH_INPUT_REGS size mismatch
21890#endif
21891static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21892 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21893 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21894 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21895 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21896};
21897
Eric Biederman5ade04a2003-10-22 04:03:46 +000021898static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021899{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021900 memset(arch, 0, sizeof(*arch));
21901 arch->features = 0;
21902}
21903
Eric Biederman90089602004-05-28 14:11:54 +000021904static const struct compiler_flag arch_flags[] = {
21905 { "mmx", X86_MMX_REGS },
21906 { "sse", X86_XMM_REGS },
21907 { "noop-copy", X86_NOOP_COPY },
21908 { 0, 0 },
21909};
21910static const struct compiler_flag arch_cpus[] = {
21911 { "i386", 0 },
21912 { "p2", X86_MMX_REGS },
21913 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21914 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21915 { "k7", X86_MMX_REGS },
21916 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21917 { "c3", X86_MMX_REGS },
21918 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21919 { 0, 0 }
21920};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021921static int arch_encode_flag(struct arch_state *arch, const char *flag)
21922{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021923 int result;
21924 int act;
21925
21926 act = 1;
21927 result = -1;
21928 if (strncmp(flag, "no-", 3) == 0) {
21929 flag += 3;
21930 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021931 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021932 if (act && strncmp(flag, "cpu=", 4) == 0) {
21933 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021934 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021935 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021936 else {
Eric Biederman90089602004-05-28 14:11:54 +000021937 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021938 }
21939 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021940}
21941
Eric Biederman90089602004-05-28 14:11:54 +000021942static void arch_usage(FILE *fp)
21943{
21944 flag_usage(fp, arch_flags, "-m", "-mno-");
21945 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21946}
21947
Eric Biedermanb138ac82003-04-22 18:44:01 +000021948static unsigned arch_regc_size(struct compile_state *state, int class)
21949{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021950 if ((class < 0) || (class > LAST_REGC)) {
21951 return 0;
21952 }
21953 return regc_size[class];
21954}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021955
Eric Biedermanb138ac82003-04-22 18:44:01 +000021956static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21957{
21958 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021959 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21960 REGCM_GPR32_8 | REGCM_GPR32 |
21961 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021962
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021963 /* Special case for the immediates */
21964 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21965 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21966 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21967 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
21968 return 0;
21969 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021970 return (regcm1 & regcm2) ||
21971 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21972}
21973
21974static void arch_reg_equivs(
21975 struct compile_state *state, unsigned *equiv, int reg)
21976{
21977 if ((reg < 0) || (reg > LAST_REG)) {
21978 internal_error(state, 0, "invalid register");
21979 }
21980 *equiv++ = reg;
21981 switch(reg) {
21982 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021983#if X86_4_8BIT_GPRS
21984 *equiv++ = REG_AH;
21985#endif
21986 *equiv++ = REG_AX;
21987 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021988 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021989 *equiv++ = REG_EDXEAX;
21990 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021991 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021992#if X86_4_8BIT_GPRS
21993 *equiv++ = REG_AL;
21994#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021995 *equiv++ = REG_AX;
21996 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021997 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021998 *equiv++ = REG_EDXEAX;
21999 break;
22000 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022001#if X86_4_8BIT_GPRS
22002 *equiv++ = REG_BH;
22003#endif
22004 *equiv++ = REG_BX;
22005 *equiv++ = REG_EBX;
22006 break;
22007
Eric Biedermanb138ac82003-04-22 18:44:01 +000022008 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022009#if X86_4_8BIT_GPRS
22010 *equiv++ = REG_BL;
22011#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022012 *equiv++ = REG_BX;
22013 *equiv++ = REG_EBX;
22014 break;
22015 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022016#if X86_4_8BIT_GPRS
22017 *equiv++ = REG_CH;
22018#endif
22019 *equiv++ = REG_CX;
22020 *equiv++ = REG_ECX;
22021 break;
22022
Eric Biedermanb138ac82003-04-22 18:44:01 +000022023 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022024#if X86_4_8BIT_GPRS
22025 *equiv++ = REG_CL;
22026#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022027 *equiv++ = REG_CX;
22028 *equiv++ = REG_ECX;
22029 break;
22030 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022031#if X86_4_8BIT_GPRS
22032 *equiv++ = REG_DH;
22033#endif
22034 *equiv++ = REG_DX;
22035 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022036 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022037 *equiv++ = REG_EDXEAX;
22038 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022039 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022040#if X86_4_8BIT_GPRS
22041 *equiv++ = REG_DL;
22042#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022043 *equiv++ = REG_DX;
22044 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022045 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022046 *equiv++ = REG_EDXEAX;
22047 break;
22048 case REG_AX:
22049 *equiv++ = REG_AL;
22050 *equiv++ = REG_AH;
22051 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022052 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022053 *equiv++ = REG_EDXEAX;
22054 break;
22055 case REG_BX:
22056 *equiv++ = REG_BL;
22057 *equiv++ = REG_BH;
22058 *equiv++ = REG_EBX;
22059 break;
22060 case REG_CX:
22061 *equiv++ = REG_CL;
22062 *equiv++ = REG_CH;
22063 *equiv++ = REG_ECX;
22064 break;
22065 case REG_DX:
22066 *equiv++ = REG_DL;
22067 *equiv++ = REG_DH;
22068 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022069 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022070 *equiv++ = REG_EDXEAX;
22071 break;
22072 case REG_SI:
22073 *equiv++ = REG_ESI;
22074 break;
22075 case REG_DI:
22076 *equiv++ = REG_EDI;
22077 break;
22078 case REG_BP:
22079 *equiv++ = REG_EBP;
22080 break;
22081 case REG_SP:
22082 *equiv++ = REG_ESP;
22083 break;
22084 case REG_EAX:
22085 *equiv++ = REG_AL;
22086 *equiv++ = REG_AH;
22087 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022088 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022089 *equiv++ = REG_EDXEAX;
22090 break;
22091 case REG_EBX:
22092 *equiv++ = REG_BL;
22093 *equiv++ = REG_BH;
22094 *equiv++ = REG_BX;
22095 break;
22096 case REG_ECX:
22097 *equiv++ = REG_CL;
22098 *equiv++ = REG_CH;
22099 *equiv++ = REG_CX;
22100 break;
22101 case REG_EDX:
22102 *equiv++ = REG_DL;
22103 *equiv++ = REG_DH;
22104 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022105 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022106 *equiv++ = REG_EDXEAX;
22107 break;
22108 case REG_ESI:
22109 *equiv++ = REG_SI;
22110 break;
22111 case REG_EDI:
22112 *equiv++ = REG_DI;
22113 break;
22114 case REG_EBP:
22115 *equiv++ = REG_BP;
22116 break;
22117 case REG_ESP:
22118 *equiv++ = REG_SP;
22119 break;
Eric Biederman530b5192003-07-01 10:05:30 +000022120 case REG_DXAX:
22121 *equiv++ = REG_AL;
22122 *equiv++ = REG_AH;
22123 *equiv++ = REG_DL;
22124 *equiv++ = REG_DH;
22125 *equiv++ = REG_AX;
22126 *equiv++ = REG_DX;
22127 *equiv++ = REG_EAX;
22128 *equiv++ = REG_EDX;
22129 *equiv++ = REG_EDXEAX;
22130 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022131 case REG_EDXEAX:
22132 *equiv++ = REG_AL;
22133 *equiv++ = REG_AH;
22134 *equiv++ = REG_DL;
22135 *equiv++ = REG_DH;
22136 *equiv++ = REG_AX;
22137 *equiv++ = REG_DX;
22138 *equiv++ = REG_EAX;
22139 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022140 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022141 break;
22142 }
22143 *equiv++ = REG_UNSET;
22144}
22145
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022146static unsigned arch_avail_mask(struct compile_state *state)
22147{
22148 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022149 /* REGCM_GPR8 is not available */
22150 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22151 REGCM_GPR32 | REGCM_GPR32_8 |
22152 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022153 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022154 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022155 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022156 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022157 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022158 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022159 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022160 return avail_mask;
22161}
22162
22163static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22164{
22165 unsigned mask, result;
22166 int class, class2;
22167 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022168
22169 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22170 if ((result & mask) == 0) {
22171 continue;
22172 }
22173 if (class > LAST_REGC) {
22174 result &= ~mask;
22175 }
22176 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22177 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22178 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22179 result |= (1 << class2);
22180 }
22181 }
22182 }
Eric Biederman530b5192003-07-01 10:05:30 +000022183 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022184 return result;
22185}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022186
Eric Biedermand1ea5392003-06-28 06:49:45 +000022187static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22188{
22189 /* Like arch_regcm_normalize except immediate register classes are excluded */
22190 regcm = arch_regcm_normalize(state, regcm);
22191 /* Remove the immediate register classes */
22192 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22193 return regcm;
22194
22195}
22196
Eric Biedermanb138ac82003-04-22 18:44:01 +000022197static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22198{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022199 unsigned mask;
22200 int class;
22201 mask = 0;
22202 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022203 if ((reg >= regcm_bound[class].first) &&
22204 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022205 mask |= (1 << class);
22206 }
22207 }
22208 if (!mask) {
22209 internal_error(state, 0, "reg %d not in any class", reg);
22210 }
22211 return mask;
22212}
22213
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022214static struct reg_info arch_reg_constraint(
22215 struct compile_state *state, struct type *type, const char *constraint)
22216{
22217 static const struct {
22218 char class;
22219 unsigned int mask;
22220 unsigned int reg;
22221 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022222 { 'r', REGCM_GPR32, REG_UNSET },
22223 { 'g', REGCM_GPR32, REG_UNSET },
22224 { 'p', REGCM_GPR32, REG_UNSET },
22225 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022226 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022227 { 'x', REGCM_XMM, REG_UNSET },
22228 { 'y', REGCM_MMX, REG_UNSET },
22229 { 'a', REGCM_GPR32, REG_EAX },
22230 { 'b', REGCM_GPR32, REG_EBX },
22231 { 'c', REGCM_GPR32, REG_ECX },
22232 { 'd', REGCM_GPR32, REG_EDX },
22233 { 'D', REGCM_GPR32, REG_EDI },
22234 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022235 { '\0', 0, REG_UNSET },
22236 };
22237 unsigned int regcm;
22238 unsigned int mask, reg;
22239 struct reg_info result;
22240 const char *ptr;
22241 regcm = arch_type_to_regcm(state, type);
22242 reg = REG_UNSET;
22243 mask = 0;
22244 for(ptr = constraint; *ptr; ptr++) {
22245 int i;
22246 if (*ptr == ' ') {
22247 continue;
22248 }
22249 for(i = 0; constraints[i].class != '\0'; i++) {
22250 if (constraints[i].class == *ptr) {
22251 break;
22252 }
22253 }
22254 if (constraints[i].class == '\0') {
22255 error(state, 0, "invalid register constraint ``%c''", *ptr);
22256 break;
22257 }
22258 if ((constraints[i].mask & regcm) == 0) {
22259 error(state, 0, "invalid register class %c specified",
22260 *ptr);
22261 }
22262 mask |= constraints[i].mask;
22263 if (constraints[i].reg != REG_UNSET) {
22264 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22265 error(state, 0, "Only one register may be specified");
22266 }
22267 reg = constraints[i].reg;
22268 }
22269 }
22270 result.reg = reg;
22271 result.regcm = mask;
22272 return result;
22273}
22274
22275static struct reg_info arch_reg_clobber(
22276 struct compile_state *state, const char *clobber)
22277{
22278 struct reg_info result;
22279 if (strcmp(clobber, "memory") == 0) {
22280 result.reg = REG_UNSET;
22281 result.regcm = 0;
22282 }
Eric Biederman90089602004-05-28 14:11:54 +000022283 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022284 result.reg = REG_EAX;
22285 result.regcm = REGCM_GPR32;
22286 }
Eric Biederman90089602004-05-28 14:11:54 +000022287 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022288 result.reg = REG_EBX;
22289 result.regcm = REGCM_GPR32;
22290 }
Eric Biederman90089602004-05-28 14:11:54 +000022291 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022292 result.reg = REG_ECX;
22293 result.regcm = REGCM_GPR32;
22294 }
Eric Biederman90089602004-05-28 14:11:54 +000022295 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022296 result.reg = REG_EDX;
22297 result.regcm = REGCM_GPR32;
22298 }
Eric Biederman90089602004-05-28 14:11:54 +000022299 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022300 result.reg = REG_ESI;
22301 result.regcm = REGCM_GPR32;
22302 }
Eric Biederman90089602004-05-28 14:11:54 +000022303 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022304 result.reg = REG_EDI;
22305 result.regcm = REGCM_GPR32;
22306 }
Eric Biederman90089602004-05-28 14:11:54 +000022307 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022308 result.reg = REG_EBP;
22309 result.regcm = REGCM_GPR32;
22310 }
Eric Biederman90089602004-05-28 14:11:54 +000022311 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022312 result.reg = REG_ESP;
22313 result.regcm = REGCM_GPR32;
22314 }
22315 else if (strcmp(clobber, "cc") == 0) {
22316 result.reg = REG_EFLAGS;
22317 result.regcm = REGCM_FLAGS;
22318 }
22319 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22320 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22321 result.reg = REG_XMM0 + octdigval(clobber[3]);
22322 result.regcm = REGCM_XMM;
22323 }
Eric Biederman90089602004-05-28 14:11:54 +000022324 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022325 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22326 result.reg = REG_MMX0 + octdigval(clobber[3]);
22327 result.regcm = REGCM_MMX;
22328 }
22329 else {
Eric Biederman90089602004-05-28 14:11:54 +000022330 error(state, 0, "unknown register name `%s' in asm",
22331 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022332 result.reg = REG_UNSET;
22333 result.regcm = 0;
22334 }
22335 return result;
22336}
22337
Eric Biedermanb138ac82003-04-22 18:44:01 +000022338static int do_select_reg(struct compile_state *state,
22339 char *used, int reg, unsigned classes)
22340{
22341 unsigned mask;
22342 if (used[reg]) {
22343 return REG_UNSET;
22344 }
22345 mask = arch_reg_regcm(state, reg);
22346 return (classes & mask) ? reg : REG_UNSET;
22347}
22348
22349static int arch_select_free_register(
22350 struct compile_state *state, char *used, int classes)
22351{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022352 /* Live ranges with the most neighbors are colored first.
22353 *
22354 * Generally it does not matter which colors are given
22355 * as the register allocator attempts to color live ranges
22356 * in an order where you are guaranteed not to run out of colors.
22357 *
22358 * Occasionally the register allocator cannot find an order
22359 * of register selection that will find a free color. To
22360 * increase the odds the register allocator will work when
22361 * it guesses first give out registers from register classes
22362 * least likely to run out of registers.
22363 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022364 */
22365 int i, reg;
22366 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022367 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022368 reg = do_select_reg(state, used, i, classes);
22369 }
22370 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22371 reg = do_select_reg(state, used, i, classes);
22372 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022373 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022374 reg = do_select_reg(state, used, i, classes);
22375 }
22376 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22377 reg = do_select_reg(state, used, i, classes);
22378 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022379 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22380 reg = do_select_reg(state, used, i, classes);
22381 }
Eric Biederman530b5192003-07-01 10:05:30 +000022382 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22383 reg = do_select_reg(state, used, i, classes);
22384 }
22385 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22386 reg = do_select_reg(state, used, i, classes);
22387 }
22388 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022389 reg = do_select_reg(state, used, i, classes);
22390 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022391 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22392 reg = do_select_reg(state, used, i, classes);
22393 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022394 return reg;
22395}
22396
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022397
Eric Biedermanb138ac82003-04-22 18:44:01 +000022398static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
22399{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022400
22401#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022402#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022403#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022404 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022405 mask = 0;
22406 switch(type->type & TYPE_MASK) {
22407 case TYPE_ARRAY:
22408 case TYPE_VOID:
22409 mask = 0;
22410 break;
22411 case TYPE_CHAR:
22412 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022413 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022414 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022415 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022416 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022417 REGCM_MMX | REGCM_XMM |
22418 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022419 break;
22420 case TYPE_SHORT:
22421 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022422 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022423 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022424 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022425 REGCM_MMX | REGCM_XMM |
22426 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022427 break;
Eric Biederman90089602004-05-28 14:11:54 +000022428 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022429 case TYPE_INT:
22430 case TYPE_UINT:
22431 case TYPE_LONG:
22432 case TYPE_ULONG:
22433 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022434 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022435 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22436 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022437 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022438 break;
Eric Biederman90089602004-05-28 14:11:54 +000022439 case TYPE_JOIN:
22440 case TYPE_UNION:
22441 mask = arch_type_to_regcm(state, type->left);
22442 break;
22443 case TYPE_OVERLAP:
22444 mask = arch_type_to_regcm(state, type->left) &
22445 arch_type_to_regcm(state, type->right);
22446 break;
22447 case TYPE_BITFIELD:
22448 mask = arch_type_to_regcm(state, type->left);
22449 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022450 default:
Eric Biederman90089602004-05-28 14:11:54 +000022451 fprintf(state->errout, "type: ");
22452 name_of(state->errout, type);
22453 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022454 internal_error(state, 0, "no register class for type");
22455 break;
22456 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022457 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022458 return mask;
22459}
22460
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022461static int is_imm32(struct triple *imm)
22462{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022463 // second condition commented out to prevent compiler warning:
22464 // imm->u.cval is always 32bit unsigned, so the comparison is
22465 // always true.
22466 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022467 (imm->op == OP_ADDRCONST);
22468
22469}
22470static int is_imm16(struct triple *imm)
22471{
22472 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22473}
22474static int is_imm8(struct triple *imm)
22475{
22476 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22477}
22478
22479static int get_imm32(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_imm32(imm)) {
22487 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022488 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022489 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022490 use_triple(imm, ins);
22491 *expr = imm;
22492 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022493}
22494
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022495static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022496{
22497 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022498 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022499 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022500 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022501 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022502 if (!is_imm8(imm)) {
22503 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022504 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022505 unuse_triple(*expr, ins);
22506 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022507 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022508 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022509}
22510
Eric Biederman530b5192003-07-01 10:05:30 +000022511#define TEMPLATE_NOP 0
22512#define TEMPLATE_INTCONST8 1
22513#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022514#define TEMPLATE_UNKNOWNVAL 3
22515#define TEMPLATE_COPY8_REG 5
22516#define TEMPLATE_COPY16_REG 6
22517#define TEMPLATE_COPY32_REG 7
22518#define TEMPLATE_COPY_IMM8 8
22519#define TEMPLATE_COPY_IMM16 9
22520#define TEMPLATE_COPY_IMM32 10
22521#define TEMPLATE_PHI8 11
22522#define TEMPLATE_PHI16 12
22523#define TEMPLATE_PHI32 13
22524#define TEMPLATE_STORE8 14
22525#define TEMPLATE_STORE16 15
22526#define TEMPLATE_STORE32 16
22527#define TEMPLATE_LOAD8 17
22528#define TEMPLATE_LOAD16 18
22529#define TEMPLATE_LOAD32 19
22530#define TEMPLATE_BINARY8_REG 20
22531#define TEMPLATE_BINARY16_REG 21
22532#define TEMPLATE_BINARY32_REG 22
22533#define TEMPLATE_BINARY8_IMM 23
22534#define TEMPLATE_BINARY16_IMM 24
22535#define TEMPLATE_BINARY32_IMM 25
22536#define TEMPLATE_SL8_CL 26
22537#define TEMPLATE_SL16_CL 27
22538#define TEMPLATE_SL32_CL 28
22539#define TEMPLATE_SL8_IMM 29
22540#define TEMPLATE_SL16_IMM 30
22541#define TEMPLATE_SL32_IMM 31
22542#define TEMPLATE_UNARY8 32
22543#define TEMPLATE_UNARY16 33
22544#define TEMPLATE_UNARY32 34
22545#define TEMPLATE_CMP8_REG 35
22546#define TEMPLATE_CMP16_REG 36
22547#define TEMPLATE_CMP32_REG 37
22548#define TEMPLATE_CMP8_IMM 38
22549#define TEMPLATE_CMP16_IMM 39
22550#define TEMPLATE_CMP32_IMM 40
22551#define TEMPLATE_TEST8 41
22552#define TEMPLATE_TEST16 42
22553#define TEMPLATE_TEST32 43
22554#define TEMPLATE_SET 44
22555#define TEMPLATE_JMP 45
22556#define TEMPLATE_RET 46
22557#define TEMPLATE_INB_DX 47
22558#define TEMPLATE_INB_IMM 48
22559#define TEMPLATE_INW_DX 49
22560#define TEMPLATE_INW_IMM 50
22561#define TEMPLATE_INL_DX 51
22562#define TEMPLATE_INL_IMM 52
22563#define TEMPLATE_OUTB_DX 53
22564#define TEMPLATE_OUTB_IMM 54
22565#define TEMPLATE_OUTW_DX 55
22566#define TEMPLATE_OUTW_IMM 56
22567#define TEMPLATE_OUTL_DX 57
22568#define TEMPLATE_OUTL_IMM 58
22569#define TEMPLATE_BSF 59
22570#define TEMPLATE_RDMSR 60
22571#define TEMPLATE_WRMSR 61
22572#define TEMPLATE_UMUL8 62
22573#define TEMPLATE_UMUL16 63
22574#define TEMPLATE_UMUL32 64
22575#define TEMPLATE_DIV8 65
22576#define TEMPLATE_DIV16 66
22577#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022578#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022579#if LAST_TEMPLATE >= MAX_TEMPLATES
22580#error "MAX_TEMPLATES to low"
22581#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022582
Eric Biederman530b5192003-07-01 10:05:30 +000022583#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
22584#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
22585#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022586
Eric Biedermanb138ac82003-04-22 18:44:01 +000022587
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022588static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022589 [TEMPLATE_NOP] = {
22590 .lhs = {
22591 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22638 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22639 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22640 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22641 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22642 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22643 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22644 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22645 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22646 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22647 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22648 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22649 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22650 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22651 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22652 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22653 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22654 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22655 },
22656 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022657 [TEMPLATE_INTCONST8] = {
22658 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22659 },
22660 [TEMPLATE_INTCONST32] = {
22661 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22662 },
Eric Biederman90089602004-05-28 14:11:54 +000022663 [TEMPLATE_UNKNOWNVAL] = {
22664 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22665 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022666 [TEMPLATE_COPY8_REG] = {
22667 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22668 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022669 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022670 [TEMPLATE_COPY16_REG] = {
22671 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22672 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22673 },
22674 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022675 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022676 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022677 },
22678 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022679 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022680 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22681 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022682 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022683 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022684 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22685 },
22686 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022687 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022688 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22689 },
22690 [TEMPLATE_PHI8] = {
22691 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022692 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22693 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022694 [TEMPLATE_PHI16] = {
22695 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022696 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22697 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022698 [TEMPLATE_PHI32] = {
22699 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022700 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22701 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022702 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022703 .rhs = {
22704 [0] = { REG_UNSET, REGCM_GPR32 },
22705 [1] = { REG_UNSET, REGCM_GPR8_LO },
22706 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022707 },
22708 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022709 .rhs = {
22710 [0] = { REG_UNSET, REGCM_GPR32 },
22711 [1] = { REG_UNSET, REGCM_GPR16 },
22712 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022713 },
22714 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022715 .rhs = {
22716 [0] = { REG_UNSET, REGCM_GPR32 },
22717 [1] = { REG_UNSET, REGCM_GPR32 },
22718 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022719 },
22720 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022721 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022722 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22723 },
22724 [TEMPLATE_LOAD16] = {
22725 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22726 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22727 },
22728 [TEMPLATE_LOAD32] = {
22729 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22730 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22731 },
Eric Biederman530b5192003-07-01 10:05:30 +000022732 [TEMPLATE_BINARY8_REG] = {
22733 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22734 .rhs = {
22735 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22736 [1] = { REG_UNSET, REGCM_GPR8_LO },
22737 },
22738 },
22739 [TEMPLATE_BINARY16_REG] = {
22740 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22741 .rhs = {
22742 [0] = { REG_VIRT0, REGCM_GPR16 },
22743 [1] = { REG_UNSET, REGCM_GPR16 },
22744 },
22745 },
22746 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022747 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22748 .rhs = {
22749 [0] = { REG_VIRT0, REGCM_GPR32 },
22750 [1] = { REG_UNSET, REGCM_GPR32 },
22751 },
22752 },
Eric Biederman530b5192003-07-01 10:05:30 +000022753 [TEMPLATE_BINARY8_IMM] = {
22754 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22755 .rhs = {
22756 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22757 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22758 },
22759 },
22760 [TEMPLATE_BINARY16_IMM] = {
22761 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22762 .rhs = {
22763 [0] = { REG_VIRT0, REGCM_GPR16 },
22764 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22765 },
22766 },
22767 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022768 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22769 .rhs = {
22770 [0] = { REG_VIRT0, REGCM_GPR32 },
22771 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22772 },
22773 },
Eric Biederman530b5192003-07-01 10:05:30 +000022774 [TEMPLATE_SL8_CL] = {
22775 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22776 .rhs = {
22777 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22778 [1] = { REG_CL, REGCM_GPR8_LO },
22779 },
22780 },
22781 [TEMPLATE_SL16_CL] = {
22782 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22783 .rhs = {
22784 [0] = { REG_VIRT0, REGCM_GPR16 },
22785 [1] = { REG_CL, REGCM_GPR8_LO },
22786 },
22787 },
22788 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022789 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22790 .rhs = {
22791 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022792 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022793 },
22794 },
Eric Biederman530b5192003-07-01 10:05:30 +000022795 [TEMPLATE_SL8_IMM] = {
22796 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22797 .rhs = {
22798 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22799 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22800 },
22801 },
22802 [TEMPLATE_SL16_IMM] = {
22803 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22804 .rhs = {
22805 [0] = { REG_VIRT0, REGCM_GPR16 },
22806 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22807 },
22808 },
22809 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022810 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22811 .rhs = {
22812 [0] = { REG_VIRT0, REGCM_GPR32 },
22813 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22814 },
22815 },
Eric Biederman530b5192003-07-01 10:05:30 +000022816 [TEMPLATE_UNARY8] = {
22817 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22818 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22819 },
22820 [TEMPLATE_UNARY16] = {
22821 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22822 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22823 },
22824 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022825 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22826 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22827 },
Eric Biederman530b5192003-07-01 10:05:30 +000022828 [TEMPLATE_CMP8_REG] = {
22829 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22830 .rhs = {
22831 [0] = { REG_UNSET, REGCM_GPR8_LO },
22832 [1] = { REG_UNSET, REGCM_GPR8_LO },
22833 },
22834 },
22835 [TEMPLATE_CMP16_REG] = {
22836 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22837 .rhs = {
22838 [0] = { REG_UNSET, REGCM_GPR16 },
22839 [1] = { REG_UNSET, REGCM_GPR16 },
22840 },
22841 },
22842 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022843 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22844 .rhs = {
22845 [0] = { REG_UNSET, REGCM_GPR32 },
22846 [1] = { REG_UNSET, REGCM_GPR32 },
22847 },
22848 },
Eric Biederman530b5192003-07-01 10:05:30 +000022849 [TEMPLATE_CMP8_IMM] = {
22850 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22851 .rhs = {
22852 [0] = { REG_UNSET, REGCM_GPR8_LO },
22853 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22854 },
22855 },
22856 [TEMPLATE_CMP16_IMM] = {
22857 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22858 .rhs = {
22859 [0] = { REG_UNSET, REGCM_GPR16 },
22860 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22861 },
22862 },
22863 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022864 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22865 .rhs = {
22866 [0] = { REG_UNSET, REGCM_GPR32 },
22867 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22868 },
22869 },
Eric Biederman530b5192003-07-01 10:05:30 +000022870 [TEMPLATE_TEST8] = {
22871 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22872 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22873 },
22874 [TEMPLATE_TEST16] = {
22875 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22876 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22877 },
22878 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022879 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22880 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22881 },
22882 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022883 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022884 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22885 },
22886 [TEMPLATE_JMP] = {
22887 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22888 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022889 [TEMPLATE_RET] = {
22890 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22891 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022892 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022893 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022894 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22895 },
22896 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022897 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022898 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22899 },
22900 [TEMPLATE_INW_DX] = {
22901 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22902 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22903 },
22904 [TEMPLATE_INW_IMM] = {
22905 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22906 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22907 },
22908 [TEMPLATE_INL_DX] = {
22909 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22910 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22911 },
22912 [TEMPLATE_INL_IMM] = {
22913 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22914 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22915 },
22916 [TEMPLATE_OUTB_DX] = {
22917 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022918 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022919 [1] = { REG_DX, REGCM_GPR16 },
22920 },
22921 },
22922 [TEMPLATE_OUTB_IMM] = {
22923 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022924 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022925 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22926 },
22927 },
22928 [TEMPLATE_OUTW_DX] = {
22929 .rhs = {
22930 [0] = { REG_AX, REGCM_GPR16 },
22931 [1] = { REG_DX, REGCM_GPR16 },
22932 },
22933 },
22934 [TEMPLATE_OUTW_IMM] = {
22935 .rhs = {
22936 [0] = { REG_AX, REGCM_GPR16 },
22937 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22938 },
22939 },
22940 [TEMPLATE_OUTL_DX] = {
22941 .rhs = {
22942 [0] = { REG_EAX, REGCM_GPR32 },
22943 [1] = { REG_DX, REGCM_GPR16 },
22944 },
22945 },
22946 [TEMPLATE_OUTL_IMM] = {
22947 .rhs = {
22948 [0] = { REG_EAX, REGCM_GPR32 },
22949 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22950 },
22951 },
22952 [TEMPLATE_BSF] = {
22953 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22954 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22955 },
22956 [TEMPLATE_RDMSR] = {
22957 .lhs = {
22958 [0] = { REG_EAX, REGCM_GPR32 },
22959 [1] = { REG_EDX, REGCM_GPR32 },
22960 },
22961 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22962 },
22963 [TEMPLATE_WRMSR] = {
22964 .rhs = {
22965 [0] = { REG_ECX, REGCM_GPR32 },
22966 [1] = { REG_EAX, REGCM_GPR32 },
22967 [2] = { REG_EDX, REGCM_GPR32 },
22968 },
22969 },
Eric Biederman530b5192003-07-01 10:05:30 +000022970 [TEMPLATE_UMUL8] = {
22971 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22972 .rhs = {
22973 [0] = { REG_AL, REGCM_GPR8_LO },
22974 [1] = { REG_UNSET, REGCM_GPR8_LO },
22975 },
22976 },
22977 [TEMPLATE_UMUL16] = {
22978 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
22979 .rhs = {
22980 [0] = { REG_AX, REGCM_GPR16 },
22981 [1] = { REG_UNSET, REGCM_GPR16 },
22982 },
22983 },
22984 [TEMPLATE_UMUL32] = {
22985 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022986 .rhs = {
22987 [0] = { REG_EAX, REGCM_GPR32 },
22988 [1] = { REG_UNSET, REGCM_GPR32 },
22989 },
22990 },
Eric Biederman530b5192003-07-01 10:05:30 +000022991 [TEMPLATE_DIV8] = {
22992 .lhs = {
22993 [0] = { REG_AL, REGCM_GPR8_LO },
22994 [1] = { REG_AH, REGCM_GPR8 },
22995 },
22996 .rhs = {
22997 [0] = { REG_AX, REGCM_GPR16 },
22998 [1] = { REG_UNSET, REGCM_GPR8_LO },
22999 },
23000 },
23001 [TEMPLATE_DIV16] = {
23002 .lhs = {
23003 [0] = { REG_AX, REGCM_GPR16 },
23004 [1] = { REG_DX, REGCM_GPR16 },
23005 },
23006 .rhs = {
23007 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
23008 [1] = { REG_UNSET, REGCM_GPR16 },
23009 },
23010 },
23011 [TEMPLATE_DIV32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023012 .lhs = {
23013 [0] = { REG_EAX, REGCM_GPR32 },
23014 [1] = { REG_EDX, REGCM_GPR32 },
23015 },
23016 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023017 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023018 [1] = { REG_UNSET, REGCM_GPR32 },
23019 },
23020 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023021};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023022
Eric Biederman83b991a2003-10-11 06:20:25 +000023023static void fixup_branch(struct compile_state *state,
23024 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23025 struct triple *left, struct triple *right)
23026{
23027 struct triple *test;
23028 if (!left) {
23029 internal_error(state, branch, "no branch test?");
23030 }
23031 test = pre_triple(state, branch,
23032 cmp_op, cmp_type, left, right);
23033 test->template_id = TEMPLATE_TEST32;
23034 if (cmp_op == OP_CMP) {
23035 test->template_id = TEMPLATE_CMP32_REG;
23036 if (get_imm32(test, &RHS(test, 1))) {
23037 test->template_id = TEMPLATE_CMP32_IMM;
23038 }
23039 }
23040 use_triple(RHS(test, 0), test);
23041 use_triple(RHS(test, 1), test);
23042 unuse_triple(RHS(branch, 0), branch);
23043 RHS(branch, 0) = test;
23044 branch->op = jmp_op;
23045 branch->template_id = TEMPLATE_JMP;
23046 use_triple(RHS(branch, 0), branch);
23047}
23048
Eric Biedermanb138ac82003-04-22 18:44:01 +000023049static void fixup_branches(struct compile_state *state,
23050 struct triple *cmp, struct triple *use, int jmp_op)
23051{
23052 struct triple_set *entry, *next;
23053 for(entry = use->use; entry; entry = next) {
23054 next = entry->next;
23055 if (entry->member->op == OP_COPY) {
23056 fixup_branches(state, cmp, entry->member, jmp_op);
23057 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023058 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023059 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023060 struct triple *left, *right;
23061 left = right = 0;
23062 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023063 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023064 right = RHS(cmp, 1);
23065 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023066 branch = entry->member;
Eric Biederman83b991a2003-10-11 06:20:25 +000023067 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023068 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023069 }
23070 }
23071}
23072
23073static void bool_cmp(struct compile_state *state,
23074 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23075{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023076 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023077 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023078
23079 /* Put a barrier up before the cmp which preceeds the
23080 * copy instruction. If a set actually occurs this gives
23081 * us a chance to move variables in registers out of the way.
23082 */
23083
23084 /* Modify the comparison operator */
23085 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023086 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023087 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023088 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023089 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023090 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023091 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023092 }
23093 /* Generate the instruction sequence that will transform the
23094 * result of the comparison into a logical value.
23095 */
Eric Biederman90089602004-05-28 14:11:54 +000023096 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023097 use_triple(ins, set);
23098 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023099
Eric Biederman90089602004-05-28 14:11:54 +000023100 convert = set;
23101 if (!equiv_types(ins->type, set->type)) {
23102 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23103 use_triple(set, convert);
23104 convert->template_id = TEMPLATE_COPY32_REG;
23105 }
23106
Eric Biedermanb138ac82003-04-22 18:44:01 +000023107 for(entry = ins->use; entry; entry = next) {
23108 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023109 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023110 continue;
23111 }
Eric Biederman90089602004-05-28 14:11:54 +000023112 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023113 }
Eric Biederman90089602004-05-28 14:11:54 +000023114 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023115}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023116
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023117struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23118{
23119 struct ins_template *template;
23120 struct reg_info result;
23121 int zlhs;
23122 if (ins->op == OP_PIECE) {
23123 index = ins->u.cval;
23124 ins = MISC(ins, 0);
23125 }
Eric Biederman90089602004-05-28 14:11:54 +000023126 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023127 if (triple_is_def(state, ins)) {
23128 zlhs = 1;
23129 }
23130 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023131 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023132 index, tops(ins->op));
23133 }
23134 switch(ins->op) {
23135 case OP_ASM:
23136 template = &ins->u.ainfo->tmpl;
23137 break;
23138 default:
23139 if (ins->template_id > LAST_TEMPLATE) {
23140 internal_error(state, ins, "bad template number %d",
23141 ins->template_id);
23142 }
23143 template = &templates[ins->template_id];
23144 break;
23145 }
23146 result = template->lhs[index];
23147 result.regcm = arch_regcm_normalize(state, result.regcm);
23148 if (result.reg != REG_UNNEEDED) {
23149 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23150 }
23151 if (result.regcm == 0) {
23152 internal_error(state, ins, "lhs %d regcm == 0", index);
23153 }
23154 return result;
23155}
23156
23157struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23158{
23159 struct reg_info result;
23160 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023161 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023162 (ins->op == OP_PIECE)) {
23163 internal_error(state, ins, "index %d out of range for %s\n",
23164 index, tops(ins->op));
23165 }
23166 switch(ins->op) {
23167 case OP_ASM:
23168 template = &ins->u.ainfo->tmpl;
23169 break;
Eric Biederman90089602004-05-28 14:11:54 +000023170 case OP_PHI:
23171 index = 0;
23172 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023173 default:
23174 if (ins->template_id > LAST_TEMPLATE) {
23175 internal_error(state, ins, "bad template number %d",
23176 ins->template_id);
23177 }
23178 template = &templates[ins->template_id];
23179 break;
23180 }
23181 result = template->rhs[index];
23182 result.regcm = arch_regcm_normalize(state, result.regcm);
23183 if (result.regcm == 0) {
23184 internal_error(state, ins, "rhs %d regcm == 0", index);
23185 }
23186 return result;
23187}
23188
Eric Biederman530b5192003-07-01 10:05:30 +000023189static struct triple *mod_div(struct compile_state *state,
23190 struct triple *ins, int div_op, int index)
23191{
23192 struct triple *div, *piece0, *piece1;
23193
Eric Biederman530b5192003-07-01 10:05:30 +000023194 /* Generate the appropriate division instruction */
23195 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23196 RHS(div, 0) = RHS(ins, 0);
23197 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023198 piece0 = LHS(div, 0);
23199 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023200 div->template_id = TEMPLATE_DIV32;
23201 use_triple(RHS(div, 0), div);
23202 use_triple(RHS(div, 1), div);
23203 use_triple(LHS(div, 0), div);
23204 use_triple(LHS(div, 1), div);
23205
Eric Biederman530b5192003-07-01 10:05:30 +000023206 /* Replate uses of ins with the appropriate piece of the div */
23207 propogate_use(state, ins, LHS(div, index));
23208 release_triple(state, ins);
23209
23210 /* Return the address of the next instruction */
23211 return piece1->next;
23212}
23213
Eric Biederman90089602004-05-28 14:11:54 +000023214static int noop_adecl(struct triple *adecl)
23215{
23216 struct triple_set *use;
23217 /* It's a noop if it doesn't specify stoorage */
23218 if (adecl->lhs == 0) {
23219 return 1;
23220 }
23221 /* Is the adecl used? If not it's a noop */
23222 for(use = adecl->use; use ; use = use->next) {
23223 if ((use->member->op != OP_PIECE) ||
23224 (MISC(use->member, 0) != adecl)) {
23225 return 0;
23226 }
23227 }
23228 return 1;
23229}
23230
23231static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23232{
23233 struct triple *mask, *nmask, *shift;
23234 struct triple *val, *val_mask, *val_shift;
23235 struct triple *targ, *targ_mask;
23236 struct triple *new;
23237 ulong_t the_mask, the_nmask;
23238
23239 targ = RHS(ins, 0);
23240 val = RHS(ins, 1);
23241
23242 /* Get constant for the mask value */
23243 the_mask = 1;
23244 the_mask <<= ins->u.bitfield.size;
23245 the_mask -= 1;
23246 the_mask <<= ins->u.bitfield.offset;
23247 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23248 mask->u.cval = the_mask;
23249
23250 /* Get the inverted mask value */
23251 the_nmask = ~the_mask;
23252 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23253 nmask->u.cval = the_nmask;
23254
23255 /* Get constant for the shift value */
23256 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23257 shift->u.cval = ins->u.bitfield.offset;
23258
23259 /* Shift and mask the source value */
23260 val_shift = val;
23261 if (shift->u.cval != 0) {
23262 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23263 use_triple(val, val_shift);
23264 use_triple(shift, val_shift);
23265 }
23266 val_mask = val_shift;
23267 if (is_signed(val->type)) {
23268 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23269 use_triple(val_shift, val_mask);
23270 use_triple(mask, val_mask);
23271 }
23272
23273 /* Mask the target value */
23274 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23275 use_triple(targ, targ_mask);
23276 use_triple(nmask, targ_mask);
23277
23278 /* Now combined them together */
23279 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23280 use_triple(targ_mask, new);
23281 use_triple(val_mask, new);
23282
23283 /* Move all of the users over to the new expression */
23284 propogate_use(state, ins, new);
23285
23286 /* Delete the original triple */
23287 release_triple(state, ins);
23288
23289 /* Restart the transformation at mask */
23290 return mask;
23291}
23292
23293static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23294{
23295 struct triple *mask, *shift;
23296 struct triple *val, *val_mask, *val_shift;
23297 ulong_t the_mask;
23298
23299 val = RHS(ins, 0);
23300
23301 /* Get constant for the mask value */
23302 the_mask = 1;
23303 the_mask <<= ins->u.bitfield.size;
23304 the_mask -= 1;
23305 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23306 mask->u.cval = the_mask;
23307
23308 /* Get constant for the right shift value */
23309 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23310 shift->u.cval = ins->u.bitfield.offset;
23311
23312 /* Shift arithmetic right, to correct the sign */
23313 val_shift = val;
23314 if (shift->u.cval != 0) {
23315 int op;
23316 if (ins->op == OP_SEXTRACT) {
23317 op = OP_SSR;
23318 } else {
23319 op = OP_USR;
23320 }
23321 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23322 use_triple(val, val_shift);
23323 use_triple(shift, val_shift);
23324 }
23325
23326 /* Finally mask the value */
23327 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23328 use_triple(val_shift, val_mask);
23329 use_triple(mask, val_mask);
23330
23331 /* Move all of the users over to the new expression */
23332 propogate_use(state, ins, val_mask);
23333
23334 /* Release the original instruction */
23335 release_triple(state, ins);
23336
23337 return mask;
23338
23339}
23340
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023341static struct triple *transform_to_arch_instruction(
23342 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023343{
23344 /* Transform from generic 3 address instructions
23345 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023346 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023347 * Copies are inserted to preserve the register flexibility
23348 * of 3 address instructions.
23349 */
Eric Biederman90089602004-05-28 14:11:54 +000023350 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023351 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023352 next = ins->next;
23353 switch(ins->op) {
23354 case OP_INTCONST:
23355 ins->template_id = TEMPLATE_INTCONST32;
23356 if (ins->u.cval < 256) {
23357 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023358 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023359 break;
23360 case OP_ADDRCONST:
23361 ins->template_id = TEMPLATE_INTCONST32;
23362 break;
Eric Biederman90089602004-05-28 14:11:54 +000023363 case OP_UNKNOWNVAL:
23364 ins->template_id = TEMPLATE_UNKNOWNVAL;
23365 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023366 case OP_NOOP:
23367 case OP_SDECL:
23368 case OP_BLOBCONST:
23369 case OP_LABEL:
23370 ins->template_id = TEMPLATE_NOP;
23371 break;
23372 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023373 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023374 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023375 value = RHS(ins, 0);
23376 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023377 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023378 }
Eric Biederman90089602004-05-28 14:11:54 +000023379 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023380 ins->template_id = TEMPLATE_COPY_IMM16;
23381 }
Eric Biederman90089602004-05-28 14:11:54 +000023382 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023383 ins->template_id = TEMPLATE_COPY_IMM32;
23384 }
Eric Biederman90089602004-05-28 14:11:54 +000023385 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023386 internal_error(state, ins, "bad constant passed to copy");
23387 }
Eric Biederman90089602004-05-28 14:11:54 +000023388 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023389 ins->template_id = TEMPLATE_COPY8_REG;
23390 }
Eric Biederman90089602004-05-28 14:11:54 +000023391 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023392 ins->template_id = TEMPLATE_COPY16_REG;
23393 }
Eric Biederman90089602004-05-28 14:11:54 +000023394 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023395 ins->template_id = TEMPLATE_COPY32_REG;
23396 }
23397 else {
23398 internal_error(state, ins, "bad type passed to copy");
23399 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023400 break;
23401 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023402 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023403 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023404 ins->template_id = TEMPLATE_PHI8;
23405 }
Eric Biederman90089602004-05-28 14:11:54 +000023406 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023407 ins->template_id = TEMPLATE_PHI16;
23408 }
Eric Biederman90089602004-05-28 14:11:54 +000023409 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023410 ins->template_id = TEMPLATE_PHI32;
23411 }
23412 else {
23413 internal_error(state, ins, "bad type passed to phi");
23414 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023415 break;
Eric Biederman90089602004-05-28 14:11:54 +000023416 case OP_ADECL:
23417 /* Adecls should always be treated as dead code and
23418 * removed. If we are not optimizing they may linger.
23419 */
23420 if (!noop_adecl(ins)) {
23421 internal_error(state, ins, "adecl remains?");
23422 }
23423 ins->template_id = TEMPLATE_NOP;
23424 next = after_lhs(state, ins);
23425 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023426 case OP_STORE:
23427 switch(ins->type->type & TYPE_MASK) {
23428 case TYPE_CHAR: case TYPE_UCHAR:
23429 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023430 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023431 case TYPE_SHORT: case TYPE_USHORT:
23432 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023433 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023434 case TYPE_INT: case TYPE_UINT:
23435 case TYPE_LONG: case TYPE_ULONG:
23436 case TYPE_POINTER:
23437 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023438 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023439 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023440 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023441 break;
23442 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023443 break;
23444 case OP_LOAD:
23445 switch(ins->type->type & TYPE_MASK) {
23446 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023447 case TYPE_SHORT: case TYPE_USHORT:
23448 case TYPE_INT: case TYPE_UINT:
23449 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023450 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023451 break;
23452 default:
23453 internal_error(state, ins, "unknown type in load");
23454 break;
23455 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023456 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023457 break;
23458 case OP_ADD:
23459 case OP_SUB:
23460 case OP_AND:
23461 case OP_XOR:
23462 case OP_OR:
23463 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023464 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023465 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023466 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023467 }
23468 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023469 case OP_SDIVT:
23470 case OP_UDIVT:
23471 ins->template_id = TEMPLATE_DIV32;
23472 next = after_lhs(state, ins);
23473 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023474 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023475 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023476 break;
23477 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023478 next = mod_div(state, ins, OP_UDIVT, 0);
23479 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023480 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023481 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023482 break;
23483 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023484 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023485 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023486 case OP_SMOD:
23487 next = mod_div(state, ins, OP_SDIVT, 1);
23488 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023489 case OP_SL:
23490 case OP_SSR:
23491 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023492 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023493 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023494 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023495 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23496 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023497 }
23498 break;
23499 case OP_INVERT:
23500 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023501 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023502 break;
23503 case OP_EQ:
23504 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
23505 break;
23506 case OP_NOTEQ:
23507 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23508 break;
23509 case OP_SLESS:
23510 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23511 break;
23512 case OP_ULESS:
23513 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23514 break;
23515 case OP_SMORE:
23516 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23517 break;
23518 case OP_UMORE:
23519 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23520 break;
23521 case OP_SLESSEQ:
23522 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23523 break;
23524 case OP_ULESSEQ:
23525 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23526 break;
23527 case OP_SMOREEQ:
23528 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23529 break;
23530 case OP_UMOREEQ:
23531 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23532 break;
23533 case OP_LTRUE:
23534 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23535 break;
23536 case OP_LFALSE:
23537 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23538 break;
23539 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023540 ins->op = OP_JMP;
23541 ins->template_id = TEMPLATE_NOP;
23542 break;
23543 case OP_CBRANCH:
23544 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
23545 RHS(ins, 0)->type, RHS(ins, 0), 0);
23546 break;
23547 case OP_CALL:
23548 ins->template_id = TEMPLATE_NOP;
23549 break;
23550 case OP_RET:
23551 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023552 break;
23553 case OP_INB:
23554 case OP_INW:
23555 case OP_INL:
23556 switch(ins->op) {
23557 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23558 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23559 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23560 }
23561 if (get_imm8(ins, &RHS(ins, 0))) {
23562 ins->template_id += 1;
23563 }
23564 break;
23565 case OP_OUTB:
23566 case OP_OUTW:
23567 case OP_OUTL:
23568 switch(ins->op) {
23569 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23570 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23571 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23572 }
23573 if (get_imm8(ins, &RHS(ins, 1))) {
23574 ins->template_id += 1;
23575 }
23576 break;
23577 case OP_BSF:
23578 case OP_BSR:
23579 ins->template_id = TEMPLATE_BSF;
23580 break;
23581 case OP_RDMSR:
23582 ins->template_id = TEMPLATE_RDMSR;
23583 next = after_lhs(state, ins);
23584 break;
23585 case OP_WRMSR:
23586 ins->template_id = TEMPLATE_WRMSR;
23587 break;
23588 case OP_HLT:
23589 ins->template_id = TEMPLATE_NOP;
23590 break;
23591 case OP_ASM:
23592 ins->template_id = TEMPLATE_NOP;
23593 next = after_lhs(state, ins);
23594 break;
23595 /* Already transformed instructions */
23596 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023597 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023598 break;
23599 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023600 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023601 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023602 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023603 }
23604 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023605 case OP_JMP:
23606 ins->template_id = TEMPLATE_NOP;
23607 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023608 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23609 case OP_JMP_SLESS: case OP_JMP_ULESS:
23610 case OP_JMP_SMORE: case OP_JMP_UMORE:
23611 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23612 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23613 ins->template_id = TEMPLATE_JMP;
23614 break;
23615 case OP_SET_EQ: case OP_SET_NOTEQ:
23616 case OP_SET_SLESS: case OP_SET_ULESS:
23617 case OP_SET_SMORE: case OP_SET_UMORE:
23618 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23619 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23620 ins->template_id = TEMPLATE_SET;
23621 break;
Eric Biederman90089602004-05-28 14:11:54 +000023622 case OP_DEPOSIT:
23623 next = x86_deposit(state, ins);
23624 break;
23625 case OP_SEXTRACT:
23626 case OP_UEXTRACT:
23627 next = x86_extract(state, ins);
23628 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023629 /* Unhandled instructions */
23630 case OP_PIECE:
23631 default:
Eric Biederman90089602004-05-28 14:11:54 +000023632 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023633 ins->op, tops(ins->op));
23634 break;
23635 }
23636 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023637}
23638
Eric Biederman530b5192003-07-01 10:05:30 +000023639static long next_label(struct compile_state *state)
23640{
Eric Biederman90089602004-05-28 14:11:54 +000023641 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023642 return ++label_counter;
23643}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023644static void generate_local_labels(struct compile_state *state)
23645{
23646 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023647 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023648 label = first;
23649 do {
23650 if ((label->op == OP_LABEL) ||
23651 (label->op == OP_SDECL)) {
23652 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023653 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023654 } else {
23655 label->u.cval = 0;
23656 }
23657
23658 }
23659 label = label->next;
23660 } while(label != first);
23661}
23662
23663static int check_reg(struct compile_state *state,
23664 struct triple *triple, int classes)
23665{
23666 unsigned mask;
23667 int reg;
23668 reg = ID_REG(triple->id);
23669 if (reg == REG_UNSET) {
23670 internal_error(state, triple, "register not set");
23671 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023672 mask = arch_reg_regcm(state, reg);
23673 if (!(classes & mask)) {
23674 internal_error(state, triple, "reg %d in wrong class",
23675 reg);
23676 }
23677 return reg;
23678}
23679
Eric Biederman90089602004-05-28 14:11:54 +000023680
Eric Biederman530b5192003-07-01 10:05:30 +000023681#if REG_XMM7 != 44
23682#error "Registers have renumberd fix arch_reg_str"
23683#endif
Eric Biederman90089602004-05-28 14:11:54 +000023684static const char *arch_regs[] = {
23685 "%unset",
23686 "%unneeded",
23687 "%eflags",
23688 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23689 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23690 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23691 "%edx:%eax",
23692 "%dx:%ax",
23693 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
23694 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
23695 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23696};
23697static const char *arch_reg_str(int reg)
23698{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023699 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23700 reg = 0;
23701 }
Eric Biederman90089602004-05-28 14:11:54 +000023702 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023703}
23704
23705static const char *reg(struct compile_state *state, struct triple *triple,
23706 int classes)
23707{
23708 int reg;
23709 reg = check_reg(state, triple, classes);
23710 return arch_reg_str(reg);
23711}
23712
Eric Biederman90089602004-05-28 14:11:54 +000023713static int arch_reg_size(int reg)
23714{
23715 int size;
23716 size = 0;
23717 if (reg == REG_EFLAGS) {
23718 size = 32;
23719 }
23720 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23721 size = 8;
23722 }
23723 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23724 size = 16;
23725 }
23726 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23727 size = 32;
23728 }
23729 else if (reg == REG_EDXEAX) {
23730 size = 64;
23731 }
23732 else if (reg == REG_DXAX) {
23733 size = 32;
23734 }
23735 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23736 size = 64;
23737 }
23738 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23739 size = 128;
23740 }
23741 return size;
23742}
23743
23744static int reg_size(struct compile_state *state, struct triple *ins)
23745{
23746 int reg;
23747 reg = ID_REG(ins->id);
23748 if (reg == REG_UNSET) {
23749 internal_error(state, ins, "register not set");
23750 }
23751 return arch_reg_size(reg);
23752}
23753
23754
23755
Eric Biedermanb138ac82003-04-22 18:44:01 +000023756const char *type_suffix(struct compile_state *state, struct type *type)
23757{
23758 const char *suffix;
23759 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023760 case SIZEOF_I8: suffix = "b"; break;
23761 case SIZEOF_I16: suffix = "w"; break;
23762 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023763 default:
23764 internal_error(state, 0, "unknown suffix");
23765 suffix = 0;
23766 break;
23767 }
23768 return suffix;
23769}
23770
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023771static void print_const_val(
23772 struct compile_state *state, struct triple *ins, FILE *fp)
23773{
23774 switch(ins->op) {
23775 case OP_INTCONST:
23776 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023777 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023778 break;
23779 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023780 if ((MISC(ins, 0)->op != OP_SDECL) &&
23781 (MISC(ins, 0)->op != OP_LABEL))
23782 {
Eric Biederman830c9882003-07-04 00:27:33 +000023783 internal_error(state, ins, "bad base for addrconst");
23784 }
23785 if (MISC(ins, 0)->u.cval <= 0) {
23786 internal_error(state, ins, "unlabeled constant");
23787 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023788 fprintf(fp, " $L%s%lu+%lu ",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023789 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023790 (unsigned long)(MISC(ins, 0)->u.cval),
23791 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023792 break;
23793 default:
23794 internal_error(state, ins, "unknown constant type");
23795 break;
23796 }
23797}
23798
Eric Biederman530b5192003-07-01 10:05:30 +000023799static void print_const(struct compile_state *state,
23800 struct triple *ins, FILE *fp)
23801{
23802 switch(ins->op) {
23803 case OP_INTCONST:
23804 switch(ins->type->type & TYPE_MASK) {
23805 case TYPE_CHAR:
23806 case TYPE_UCHAR:
Eric Biederman83b991a2003-10-11 06:20:25 +000023807 fprintf(fp, ".byte 0x%02lx\n",
23808 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023809 break;
23810 case TYPE_SHORT:
23811 case TYPE_USHORT:
Eric Biederman83b991a2003-10-11 06:20:25 +000023812 fprintf(fp, ".short 0x%04lx\n",
23813 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023814 break;
23815 case TYPE_INT:
23816 case TYPE_UINT:
23817 case TYPE_LONG:
23818 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023819 case TYPE_POINTER:
Eric Biederman83b991a2003-10-11 06:20:25 +000023820 fprintf(fp, ".int %lu\n",
23821 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023822 break;
23823 default:
Eric Biederman90089602004-05-28 14:11:54 +000023824 fprintf(state->errout, "type: ");
23825 name_of(state->errout, ins->type);
23826 fprintf(state->errout, "\n");
23827 internal_error(state, ins, "Unknown constant type. Val: %lu",
23828 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023829 }
Eric Biederman90089602004-05-28 14:11:54 +000023830
Eric Biederman530b5192003-07-01 10:05:30 +000023831 break;
23832 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023833 if ((MISC(ins, 0)->op != OP_SDECL) &&
23834 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023835 internal_error(state, ins, "bad base for addrconst");
23836 }
23837 if (MISC(ins, 0)->u.cval <= 0) {
23838 internal_error(state, ins, "unlabeled constant");
23839 }
23840 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023841 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023842 (unsigned long)(MISC(ins, 0)->u.cval),
23843 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023844 break;
23845 case OP_BLOBCONST:
23846 {
23847 unsigned char *blob;
23848 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023849 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023850 blob = ins->u.blob;
23851 for(i = 0; i < size; i++) {
23852 fprintf(fp, ".byte 0x%02x\n",
23853 blob[i]);
23854 }
23855 break;
23856 }
23857 default:
23858 internal_error(state, ins, "Unknown constant type");
23859 break;
23860 }
23861}
23862
23863#define TEXT_SECTION ".rom.text"
23864#define DATA_SECTION ".rom.data"
23865
23866static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023867 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023868{
Eric Biederman90089602004-05-28 14:11:54 +000023869 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023870 long ref;
23871 ref = next_label(state);
23872 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023873 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023874 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023875 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023876 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23877 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023878 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023879 }
Eric Biederman530b5192003-07-01 10:05:30 +000023880 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23881 return ref;
23882}
23883
Eric Biederman90089602004-05-28 14:11:54 +000023884static long get_mask_pool_ref(
23885 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23886{
23887 long ref;
23888 if (mask == 0xff) {
23889 ref = 1;
23890 }
23891 else if (mask == 0xffff) {
23892 ref = 2;
23893 }
23894 else {
23895 ref = 0;
23896 internal_error(state, ins, "unhandled mask value");
23897 }
23898 return ref;
23899}
23900
Eric Biedermanb138ac82003-04-22 18:44:01 +000023901static void print_binary_op(struct compile_state *state,
23902 const char *op, struct triple *ins, FILE *fp)
23903{
23904 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023905 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023906 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023907 internal_error(state, ins, "invalid register assignment");
23908 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023909 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023910 fprintf(fp, "\t%s ", op);
23911 print_const_val(state, RHS(ins, 1), fp);
23912 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023913 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023914 }
23915 else {
23916 unsigned lmask, rmask;
23917 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023918 lreg = check_reg(state, RHS(ins, 0), mask);
23919 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023920 lmask = arch_reg_regcm(state, lreg);
23921 rmask = arch_reg_regcm(state, rreg);
23922 mask = lmask & rmask;
23923 fprintf(fp, "\t%s %s, %s\n",
23924 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023925 reg(state, RHS(ins, 1), mask),
23926 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023927 }
23928}
23929static void print_unary_op(struct compile_state *state,
23930 const char *op, struct triple *ins, FILE *fp)
23931{
23932 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023933 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023934 fprintf(fp, "\t%s %s\n",
23935 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023936 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023937}
23938
23939static void print_op_shift(struct compile_state *state,
23940 const char *op, struct triple *ins, FILE *fp)
23941{
23942 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023943 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023944 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023945 internal_error(state, ins, "invalid register assignment");
23946 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023947 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023948 fprintf(fp, "\t%s ", op);
23949 print_const_val(state, RHS(ins, 1), fp);
23950 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023951 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023952 }
23953 else {
23954 fprintf(fp, "\t%s %s, %s\n",
23955 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023956 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023957 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023958 }
23959}
23960
23961static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23962{
23963 const char *op;
23964 int mask;
23965 int dreg;
23966 mask = 0;
23967 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023968 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023969 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23970 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23971 default:
23972 internal_error(state, ins, "not an in operation");
23973 op = 0;
23974 break;
23975 }
23976 dreg = check_reg(state, ins, mask);
23977 if (!reg_is_reg(state, dreg, REG_EAX)) {
23978 internal_error(state, ins, "dst != %%eax");
23979 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023980 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023981 fprintf(fp, "\t%s ", op);
23982 print_const_val(state, RHS(ins, 0), fp);
23983 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023984 reg(state, ins, mask));
23985 }
23986 else {
23987 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023988 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023989 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23990 internal_error(state, ins, "src != %%dx");
23991 }
23992 fprintf(fp, "\t%s %s, %s\n",
23993 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023994 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023995 reg(state, ins, mask));
23996 }
23997}
23998
23999static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
24000{
24001 const char *op;
24002 int mask;
24003 int lreg;
24004 mask = 0;
24005 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000024006 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024007 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
24008 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
24009 default:
24010 internal_error(state, ins, "not an out operation");
24011 op = 0;
24012 break;
24013 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024014 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024015 if (!reg_is_reg(state, lreg, REG_EAX)) {
24016 internal_error(state, ins, "src != %%eax");
24017 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024018 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024019 fprintf(fp, "\t%s %s,",
24020 op, reg(state, RHS(ins, 0), mask));
24021 print_const_val(state, RHS(ins, 1), fp);
24022 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024023 }
24024 else {
24025 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024026 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024027 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24028 internal_error(state, ins, "dst != %%dx");
24029 }
24030 fprintf(fp, "\t%s %s, %s\n",
24031 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024032 reg(state, RHS(ins, 0), mask),
24033 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024034 }
24035}
24036
24037static void print_op_move(struct compile_state *state,
24038 struct triple *ins, FILE *fp)
24039{
24040 /* op_move is complex because there are many types
24041 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024042 * Because OP_COPY will be introduced in arbitrary locations
24043 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024044 * OP_CONVERT can change the flags and it is the only operation
24045 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024046 */
24047 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24048 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024049 if (state->arch->features & X86_NOOP_COPY) {
24050 omit_copy = 0;
24051 }
24052 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024053 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024054 dst = ins;
24055 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024056 else {
24057 internal_error(state, ins, "unknown move operation");
24058 src = dst = 0;
24059 }
Eric Biederman90089602004-05-28 14:11:54 +000024060 if (reg_size(state, dst) < size_of(state, dst->type)) {
24061 internal_error(state, ins, "Invalid destination register");
24062 }
24063 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24064 fprintf(state->errout, "src type: ");
24065 name_of(state->errout, src->type);
24066 fprintf(state->errout, "\n");
24067 fprintf(state->errout, "dst type: ");
24068 name_of(state->errout, dst->type);
24069 fprintf(state->errout, "\n");
24070 internal_error(state, ins, "Type mismatch for OP_COPY");
24071 }
24072
Eric Biederman0babc1c2003-05-09 02:39:00 +000024073 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024074 int src_reg, dst_reg;
24075 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024076 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024077 dst_reg = ID_REG(dst->id);
24078 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024079 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024080 /* If the class is the same just move the register */
24081 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024082 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024083 if ((src_reg != dst_reg) || !omit_copy) {
24084 fprintf(fp, "\tmov %s, %s\n",
24085 reg(state, src, src_regcm),
24086 reg(state, dst, dst_regcm));
24087 }
24088 }
24089 /* Move 32bit to 16bit */
24090 else if ((src_regcm & REGCM_GPR32) &&
24091 (dst_regcm & REGCM_GPR16)) {
24092 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24093 if ((src_reg != dst_reg) || !omit_copy) {
24094 fprintf(fp, "\tmovw %s, %s\n",
24095 arch_reg_str(src_reg),
24096 arch_reg_str(dst_reg));
24097 }
24098 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024099 /* Move from 32bit gprs to 16bit gprs */
24100 else if ((src_regcm & REGCM_GPR32) &&
24101 (dst_regcm & REGCM_GPR16)) {
24102 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24103 if ((src_reg != dst_reg) || !omit_copy) {
24104 fprintf(fp, "\tmov %s, %s\n",
24105 arch_reg_str(src_reg),
24106 arch_reg_str(dst_reg));
24107 }
24108 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024109 /* Move 32bit to 8bit */
24110 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024111 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024112 {
24113 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24114 if ((src_reg != dst_reg) || !omit_copy) {
24115 fprintf(fp, "\tmovb %s, %s\n",
24116 arch_reg_str(src_reg),
24117 arch_reg_str(dst_reg));
24118 }
24119 }
24120 /* Move 16bit to 8bit */
24121 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024122 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024123 {
24124 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24125 if ((src_reg != dst_reg) || !omit_copy) {
24126 fprintf(fp, "\tmovb %s, %s\n",
24127 arch_reg_str(src_reg),
24128 arch_reg_str(dst_reg));
24129 }
24130 }
24131 /* Move 8/16bit to 16/32bit */
Eric Biederman530b5192003-07-01 10:05:30 +000024132 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024133 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024134 const char *op;
24135 op = is_signed(src->type)? "movsx": "movzx";
24136 fprintf(fp, "\t%s %s, %s\n",
24137 op,
24138 reg(state, src, src_regcm),
24139 reg(state, dst, dst_regcm));
24140 }
24141 /* Move between sse registers */
24142 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24143 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024144 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024145 reg(state, src, src_regcm),
24146 reg(state, dst, dst_regcm));
24147 }
24148 }
Eric Biederman530b5192003-07-01 10:05:30 +000024149 /* Move between mmx registers */
24150 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024151 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024152 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024153 reg(state, src, src_regcm),
24154 reg(state, dst, dst_regcm));
24155 }
24156 }
Eric Biederman530b5192003-07-01 10:05:30 +000024157 /* Move from sse to mmx registers */
24158 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24159 fprintf(fp, "\tmovdq2q %s, %s\n",
24160 reg(state, src, src_regcm),
24161 reg(state, dst, dst_regcm));
24162 }
24163 /* Move from mmx to sse registers */
24164 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24165 fprintf(fp, "\tmovq2dq %s, %s\n",
24166 reg(state, src, src_regcm),
24167 reg(state, dst, dst_regcm));
24168 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024169 /* Move between 32bit gprs & mmx/sse registers */
24170 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24171 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24172 fprintf(fp, "\tmovd %s, %s\n",
24173 reg(state, src, src_regcm),
24174 reg(state, dst, dst_regcm));
24175 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024176 /* Move from 16bit gprs & mmx/sse registers */
24177 else if ((src_regcm & REGCM_GPR16) &&
24178 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24179 const char *op;
24180 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024181 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024182 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24183 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24184 op,
24185 arch_reg_str(src_reg),
24186 arch_reg_str(mid_reg),
24187 arch_reg_str(mid_reg),
24188 arch_reg_str(dst_reg));
24189 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024190 /* Move from mmx/sse registers to 16bit gprs */
24191 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24192 (dst_regcm & REGCM_GPR16)) {
24193 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24194 fprintf(fp, "\tmovd %s, %s\n",
24195 arch_reg_str(src_reg),
24196 arch_reg_str(dst_reg));
24197 }
Eric Biederman530b5192003-07-01 10:05:30 +000024198 /* Move from gpr to 64bit dividend */
24199 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24200 (dst_regcm & REGCM_DIVIDEND64)) {
24201 const char *extend;
24202 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24203 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
24204 arch_reg_str(src_reg),
24205 extend);
24206 }
24207 /* Move from 64bit gpr to gpr */
24208 else if ((src_regcm & REGCM_DIVIDEND64) &&
24209 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24210 if (dst_regcm & REGCM_GPR32) {
24211 src_reg = REG_EAX;
24212 }
24213 else if (dst_regcm & REGCM_GPR16) {
24214 src_reg = REG_AX;
24215 }
24216 else if (dst_regcm & REGCM_GPR8_LO) {
24217 src_reg = REG_AL;
24218 }
24219 fprintf(fp, "\tmov %s, %s\n",
24220 arch_reg_str(src_reg),
24221 arch_reg_str(dst_reg));
24222 }
24223 /* Move from mmx/sse registers to 64bit gpr */
24224 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24225 (dst_regcm & REGCM_DIVIDEND64)) {
24226 const char *extend;
24227 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24228 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24229 arch_reg_str(src_reg),
24230 extend);
24231 }
24232 /* Move from 64bit gpr to mmx/sse register */
24233 else if ((src_regcm & REGCM_DIVIDEND64) &&
24234 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24235 fprintf(fp, "\tmovd %%eax, %s\n",
24236 arch_reg_str(dst_reg));
24237 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024238#if X86_4_8BIT_GPRS
24239 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024240 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024241 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24242 const char *op;
24243 int mid_reg;
24244 op = is_signed(src->type)? "movsx":"movzx";
24245 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24246 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24247 op,
24248 reg(state, src, src_regcm),
24249 arch_reg_str(mid_reg),
24250 arch_reg_str(mid_reg),
24251 reg(state, dst, dst_regcm));
24252 }
24253 /* Move from mmx/sse registers and 8bit gprs */
24254 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024255 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024256 int mid_reg;
24257 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24258 fprintf(fp, "\tmovd %s, %s\n",
24259 reg(state, src, src_regcm),
24260 arch_reg_str(mid_reg));
24261 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024262 /* Move from 32bit gprs to 8bit gprs */
24263 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024264 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024265 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24266 if ((src_reg != dst_reg) || !omit_copy) {
24267 fprintf(fp, "\tmov %s, %s\n",
24268 arch_reg_str(src_reg),
24269 arch_reg_str(dst_reg));
24270 }
24271 }
24272 /* Move from 16bit gprs to 8bit gprs */
24273 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024274 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024275 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24276 if ((src_reg != dst_reg) || !omit_copy) {
24277 fprintf(fp, "\tmov %s, %s\n",
24278 arch_reg_str(src_reg),
24279 arch_reg_str(dst_reg));
24280 }
24281 }
24282#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024283 /* Move from %eax:%edx to %eax:%edx */
24284 else if ((src_regcm & REGCM_DIVIDEND64) &&
24285 (dst_regcm & REGCM_DIVIDEND64) &&
24286 (src_reg == dst_reg)) {
24287 if (!omit_copy) {
24288 fprintf(fp, "\t/*mov %s, %s*/\n",
24289 arch_reg_str(src_reg),
24290 arch_reg_str(dst_reg));
24291 }
24292 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024293 else {
Eric Biederman90089602004-05-28 14:11:54 +000024294 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24295 internal_error(state, ins, "attempt to copy from %%eflags!");
24296 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024297 internal_error(state, ins, "unknown copy type");
24298 }
24299 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024300 else {
Eric Biederman90089602004-05-28 14:11:54 +000024301 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024302 int dst_reg;
24303 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024304 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024305 dst_reg = ID_REG(dst->id);
24306 dst_regcm = arch_reg_regcm(state, dst_reg);
24307 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24308 fprintf(fp, "\tmov ");
24309 print_const_val(state, src, fp);
24310 fprintf(fp, ", %s\n",
24311 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24312 }
24313 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024314 if (dst_size > SIZEOF_I32) {
24315 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024316 }
24317 fprintf(fp, "\tmov $0, %%edx\n");
24318 fprintf(fp, "\tmov ");
24319 print_const_val(state, src, fp);
24320 fprintf(fp, ", %%eax\n");
24321 }
24322 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024323 if (dst_size > SIZEOF_I16) {
24324 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024325 }
24326 fprintf(fp, "\tmov $0, %%dx\n");
24327 fprintf(fp, "\tmov ");
24328 print_const_val(state, src, fp);
24329 fprintf(fp, ", %%ax");
24330 }
24331 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24332 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024333 if (dst_size > SIZEOF_I32) {
24334 internal_error(state, ins, "%d bit constant...", dst_size);
24335 }
24336 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024337 fprintf(fp, "\tmovd L%s%lu, %s\n",
24338 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024339 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24340 }
24341 else {
24342 internal_error(state, ins, "unknown copy immediate type");
24343 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024344 }
Eric Biederman90089602004-05-28 14:11:54 +000024345 /* Leave now if this is not a type conversion */
24346 if (ins->op != OP_CONVERT) {
24347 return;
24348 }
24349 /* Now make certain I have not logically overflowed the destination */
24350 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24351 (size_of(state, dst->type) < reg_size(state, dst)))
24352 {
24353 unsigned long mask;
24354 int dst_reg;
24355 int dst_regcm;
24356 if (size_of(state, dst->type) >= 32) {
24357 fprintf(state->errout, "dst type: ");
24358 name_of(state->errout, dst->type);
24359 fprintf(state->errout, "\n");
24360 internal_error(state, dst, "unhandled dst type size");
24361 }
24362 mask = 1;
24363 mask <<= size_of(state, dst->type);
24364 mask -= 1;
24365
24366 dst_reg = ID_REG(dst->id);
24367 dst_regcm = arch_reg_regcm(state, dst_reg);
24368
24369 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24370 fprintf(fp, "\tand $0x%lx, %s\n",
24371 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24372 }
24373 else if (dst_regcm & REGCM_MMX) {
24374 long ref;
24375 ref = get_mask_pool_ref(state, dst, mask, fp);
24376 fprintf(fp, "\tpand L%s%lu, %s\n",
24377 state->compiler->label_prefix, ref,
24378 reg(state, dst, REGCM_MMX));
24379 }
24380 else if (dst_regcm & REGCM_XMM) {
24381 long ref;
24382 ref = get_mask_pool_ref(state, dst, mask, fp);
24383 fprintf(fp, "\tpand L%s%lu, %s\n",
24384 state->compiler->label_prefix, ref,
24385 reg(state, dst, REGCM_XMM));
24386 }
24387 else {
24388 fprintf(state->errout, "dst type: ");
24389 name_of(state->errout, dst->type);
24390 fprintf(state->errout, "\n");
24391 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24392 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24393 }
24394 }
24395 /* Make certain I am properly sign extended */
24396 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24397 (is_signed(src->type)))
24398 {
24399 int bits, reg_bits, shift_bits;
24400 int dst_reg;
24401 int dst_regcm;
24402
24403 bits = size_of(state, src->type);
24404 reg_bits = reg_size(state, dst);
24405 if (reg_bits > 32) {
24406 reg_bits = 32;
24407 }
24408 shift_bits = reg_bits - size_of(state, src->type);
24409 dst_reg = ID_REG(dst->id);
24410 dst_regcm = arch_reg_regcm(state, dst_reg);
24411
24412 if (shift_bits < 0) {
24413 internal_error(state, dst, "negative shift?");
24414 }
24415
24416 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24417 fprintf(fp, "\tshl $%d, %s\n",
24418 shift_bits,
24419 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24420 fprintf(fp, "\tsar $%d, %s\n",
24421 shift_bits,
24422 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24423 }
24424 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24425 fprintf(fp, "\tpslld $%d, %s\n",
24426 shift_bits,
24427 reg(state, dst, REGCM_MMX | REGCM_XMM));
24428 fprintf(fp, "\tpsrad $%d, %s\n",
24429 shift_bits,
24430 reg(state, dst, REGCM_MMX | REGCM_XMM));
24431 }
24432 else {
24433 fprintf(state->errout, "dst type: ");
24434 name_of(state->errout, dst->type);
24435 fprintf(state->errout, "\n");
24436 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24437 internal_error(state, dst, "failed to signed extend value");
24438 }
24439 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024440}
24441
24442static void print_op_load(struct compile_state *state,
24443 struct triple *ins, FILE *fp)
24444{
24445 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024446 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024447 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024448 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024449 if (is_const(src) || is_const(dst)) {
24450 internal_error(state, ins, "unknown load operation");
24451 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024452 switch(ins->type->type & TYPE_MASK) {
24453 case TYPE_CHAR: op = "movsbl"; break;
24454 case TYPE_UCHAR: op = "movzbl"; break;
24455 case TYPE_SHORT: op = "movswl"; break;
24456 case TYPE_USHORT: op = "movzwl"; break;
24457 case TYPE_INT: case TYPE_UINT:
24458 case TYPE_LONG: case TYPE_ULONG:
24459 case TYPE_POINTER:
24460 op = "movl";
24461 break;
24462 default:
24463 internal_error(state, ins, "unknown type in load");
24464 op = "<invalid opcode>";
24465 break;
24466 }
24467 fprintf(fp, "\t%s (%s), %s\n",
24468 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024469 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024470 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024471}
24472
24473
24474static void print_op_store(struct compile_state *state,
24475 struct triple *ins, FILE *fp)
24476{
24477 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024478 dst = RHS(ins, 0);
24479 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024480 if (is_const(src) && (src->op == OP_INTCONST)) {
24481 long_t value;
24482 value = (long_t)(src->u.cval);
24483 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24484 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024485 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024486 reg(state, dst, REGCM_GPR32));
24487 }
24488 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24489 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24490 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024491 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024492 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024493 }
24494 else {
24495 if (is_const(src) || is_const(dst)) {
24496 internal_error(state, ins, "unknown store operation");
24497 }
24498 fprintf(fp, "\tmov%s %s, (%s)\n",
24499 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024500 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024501 reg(state, dst, REGCM_GPR32));
24502 }
24503
24504
24505}
24506
24507static void print_op_smul(struct compile_state *state,
24508 struct triple *ins, FILE *fp)
24509{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024510 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024511 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024512 reg(state, RHS(ins, 1), REGCM_GPR32),
24513 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024514 }
24515 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024516 fprintf(fp, "\timul ");
24517 print_const_val(state, RHS(ins, 1), fp);
24518 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024519 }
24520}
24521
24522static void print_op_cmp(struct compile_state *state,
24523 struct triple *ins, FILE *fp)
24524{
24525 unsigned mask;
24526 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024527 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024528 dreg = check_reg(state, ins, REGCM_FLAGS);
24529 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24530 internal_error(state, ins, "bad dest register for cmp");
24531 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024532 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024533 fprintf(fp, "\tcmp ");
24534 print_const_val(state, RHS(ins, 1), fp);
24535 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024536 }
24537 else {
24538 unsigned lmask, rmask;
24539 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024540 lreg = check_reg(state, RHS(ins, 0), mask);
24541 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024542 lmask = arch_reg_regcm(state, lreg);
24543 rmask = arch_reg_regcm(state, rreg);
24544 mask = lmask & rmask;
24545 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024546 reg(state, RHS(ins, 1), mask),
24547 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024548 }
24549}
24550
24551static void print_op_test(struct compile_state *state,
24552 struct triple *ins, FILE *fp)
24553{
24554 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024555 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024556 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024557 reg(state, RHS(ins, 0), mask),
24558 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024559}
24560
24561static void print_op_branch(struct compile_state *state,
24562 struct triple *branch, FILE *fp)
24563{
24564 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024565 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024566 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024567 internal_error(state, branch, "jmp with condition?");
24568 }
24569 bop = "jmp";
24570 }
24571 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024572 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024573 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024574 internal_error(state, branch, "jmpcc without condition?");
24575 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024576 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24577 if ((RHS(branch, 0)->op != OP_CMP) &&
24578 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024579 internal_error(state, branch, "bad branch test");
24580 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024581#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024582#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024583#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024584 ptr = RHS(branch, 0);
24585 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24586 if (ptr->op != OP_COPY) {
24587 internal_error(state, branch, "branch does not follow test");
24588 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024589 }
24590 switch(branch->op) {
24591 case OP_JMP_EQ: bop = "jz"; break;
24592 case OP_JMP_NOTEQ: bop = "jnz"; break;
24593 case OP_JMP_SLESS: bop = "jl"; break;
24594 case OP_JMP_ULESS: bop = "jb"; break;
24595 case OP_JMP_SMORE: bop = "jg"; break;
24596 case OP_JMP_UMORE: bop = "ja"; break;
24597 case OP_JMP_SLESSEQ: bop = "jle"; break;
24598 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24599 case OP_JMP_SMOREEQ: bop = "jge"; break;
24600 case OP_JMP_UMOREEQ: bop = "jae"; break;
24601 default:
24602 internal_error(state, branch, "Invalid branch op");
24603 break;
24604 }
24605
24606 }
Eric Biederman90089602004-05-28 14:11:54 +000024607#if 1
24608 if (branch->op == OP_CALL) {
24609 fprintf(fp, "\t/* call */\n");
24610 }
24611#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024612 fprintf(fp, "\t%s L%s%lu\n",
24613 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024614 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024615 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024616}
24617
Eric Biederman5ade04a2003-10-22 04:03:46 +000024618static void print_op_ret(struct compile_state *state,
24619 struct triple *branch, FILE *fp)
24620{
24621 fprintf(fp, "\tjmp *%s\n",
24622 reg(state, RHS(branch, 0), REGCM_GPR32));
24623}
24624
Eric Biedermanb138ac82003-04-22 18:44:01 +000024625static void print_op_set(struct compile_state *state,
24626 struct triple *set, FILE *fp)
24627{
24628 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024629 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024630 internal_error(state, set, "setcc without condition?");
24631 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024632 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24633 if ((RHS(set, 0)->op != OP_CMP) &&
24634 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024635 internal_error(state, set, "bad set test");
24636 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024637 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024638 internal_error(state, set, "set does not follow test");
24639 }
24640 switch(set->op) {
24641 case OP_SET_EQ: sop = "setz"; break;
24642 case OP_SET_NOTEQ: sop = "setnz"; break;
24643 case OP_SET_SLESS: sop = "setl"; break;
24644 case OP_SET_ULESS: sop = "setb"; break;
24645 case OP_SET_SMORE: sop = "setg"; break;
24646 case OP_SET_UMORE: sop = "seta"; break;
24647 case OP_SET_SLESSEQ: sop = "setle"; break;
24648 case OP_SET_ULESSEQ: sop = "setbe"; break;
24649 case OP_SET_SMOREEQ: sop = "setge"; break;
24650 case OP_SET_UMOREEQ: sop = "setae"; break;
24651 default:
24652 internal_error(state, set, "Invalid set op");
24653 break;
24654 }
24655 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024656 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024657}
24658
24659static void print_op_bit_scan(struct compile_state *state,
24660 struct triple *ins, FILE *fp)
24661{
24662 const char *op;
24663 switch(ins->op) {
24664 case OP_BSF: op = "bsf"; break;
24665 case OP_BSR: op = "bsr"; break;
24666 default:
24667 internal_error(state, ins, "unknown bit scan");
24668 op = 0;
24669 break;
24670 }
24671 fprintf(fp,
24672 "\t%s %s, %s\n"
24673 "\tjnz 1f\n"
24674 "\tmovl $-1, %s\n"
24675 "1:\n",
24676 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024677 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024678 reg(state, ins, REGCM_GPR32),
24679 reg(state, ins, REGCM_GPR32));
24680}
24681
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024682
Eric Biedermanb138ac82003-04-22 18:44:01 +000024683static void print_sdecl(struct compile_state *state,
24684 struct triple *ins, FILE *fp)
24685{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024686 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024687 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman83b991a2003-10-11 06:20:25 +000024688 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024689 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024690 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024691 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024692
24693}
24694
24695static void print_instruction(struct compile_state *state,
24696 struct triple *ins, FILE *fp)
24697{
24698 /* Assumption: after I have exted the register allocator
24699 * everything is in a valid register.
24700 */
24701 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024702 case OP_ASM:
24703 print_op_asm(state, ins, fp);
24704 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024705 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24706 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24707 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24708 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24709 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24710 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24711 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24712 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24713 case OP_POS: break;
24714 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24715 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024716 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024717 case OP_INTCONST:
24718 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024719 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024720 /* Don't generate anything here for constants */
24721 case OP_PHI:
24722 /* Don't generate anything for variable declarations. */
24723 break;
Eric Biederman90089602004-05-28 14:11:54 +000024724 case OP_UNKNOWNVAL:
24725 fprintf(fp, " /* unknown %s */\n",
24726 reg(state, ins, REGCM_ALL));
24727 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024728 case OP_SDECL:
24729 print_sdecl(state, ins, fp);
24730 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024731 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024732 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024733 print_op_move(state, ins, fp);
24734 break;
24735 case OP_LOAD:
24736 print_op_load(state, ins, fp);
24737 break;
24738 case OP_STORE:
24739 print_op_store(state, ins, fp);
24740 break;
24741 case OP_SMUL:
24742 print_op_smul(state, ins, fp);
24743 break;
24744 case OP_CMP: print_op_cmp(state, ins, fp); break;
24745 case OP_TEST: print_op_test(state, ins, fp); break;
24746 case OP_JMP:
24747 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24748 case OP_JMP_SLESS: case OP_JMP_ULESS:
24749 case OP_JMP_SMORE: case OP_JMP_UMORE:
24750 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24751 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024752 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024753 print_op_branch(state, ins, fp);
24754 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024755 case OP_RET:
24756 print_op_ret(state, ins, fp);
24757 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024758 case OP_SET_EQ: case OP_SET_NOTEQ:
24759 case OP_SET_SLESS: case OP_SET_ULESS:
24760 case OP_SET_SMORE: case OP_SET_UMORE:
24761 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24762 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24763 print_op_set(state, ins, fp);
24764 break;
24765 case OP_INB: case OP_INW: case OP_INL:
24766 print_op_in(state, ins, fp);
24767 break;
24768 case OP_OUTB: case OP_OUTW: case OP_OUTL:
24769 print_op_out(state, ins, fp);
24770 break;
24771 case OP_BSF:
24772 case OP_BSR:
24773 print_op_bit_scan(state, ins, fp);
24774 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024775 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024776 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024777 fprintf(fp, "\trdmsr\n");
24778 break;
24779 case OP_WRMSR:
24780 fprintf(fp, "\twrmsr\n");
24781 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024782 case OP_HLT:
24783 fprintf(fp, "\thlt\n");
24784 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024785 case OP_SDIVT:
24786 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24787 break;
24788 case OP_UDIVT:
24789 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24790 break;
24791 case OP_UMUL:
24792 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24793 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024794 case OP_LABEL:
24795 if (!ins->use) {
24796 return;
24797 }
Eric Biederman83b991a2003-10-11 06:20:25 +000024798 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024799 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024800 break;
Eric Biederman90089602004-05-28 14:11:54 +000024801 case OP_ADECL:
24802 /* Ignore adecls with no registers error otherwise */
24803 if (!noop_adecl(ins)) {
24804 internal_error(state, ins, "adecl remains?");
24805 }
24806 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024807 /* Ignore OP_PIECE */
24808 case OP_PIECE:
24809 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024810 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024811 case OP_SDIV: case OP_UDIV:
24812 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024813 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24814 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24815 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24816 default:
24817 internal_error(state, ins, "unknown op: %d %s",
24818 ins->op, tops(ins->op));
24819 break;
24820 }
24821}
24822
24823static void print_instructions(struct compile_state *state)
24824{
24825 struct triple *first, *ins;
24826 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024827 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024828 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024829 int max_inline_depth;
24830 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024831 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024832 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024833 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024834 /* Masks for common sizes */
24835 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24836 fprintf(fp, ".balign 16\n");
24837 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24838 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24839 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24840 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024841 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024842 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024843 ins = first;
24844 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024845 if (print_location &&
24846 last_occurance != ins->occurance) {
24847 if (!ins->occurance->parent) {
24848 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024849 ins->occurance->function?ins->occurance->function:"(null)",
24850 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024851 ins->occurance->line,
24852 ins->occurance->col);
24853 }
24854 else {
24855 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024856 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024857 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024858 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024859 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024860 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024861 fprintf(fp, "\t * %s,%s:%d.%d\n",
24862 ptr->function,
24863 ptr->filename,
24864 ptr->line,
24865 ptr->col);
24866 }
24867 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024868 if (inline_depth > max_inline_depth) {
24869 max_inline_depth = inline_depth;
24870 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024871 }
24872 if (last_occurance) {
24873 put_occurance(last_occurance);
24874 }
24875 get_occurance(ins->occurance);
24876 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024877 }
24878
24879 print_instruction(state, ins, fp);
24880 ins = ins->next;
24881 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024882 if (print_location) {
24883 fprintf(fp, "/* max inline depth %d */\n",
24884 max_inline_depth);
24885 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024886}
Eric Biederman530b5192003-07-01 10:05:30 +000024887
Eric Biedermanb138ac82003-04-22 18:44:01 +000024888static void generate_code(struct compile_state *state)
24889{
24890 generate_local_labels(state);
24891 print_instructions(state);
24892
24893}
24894
Eric Biederman90089602004-05-28 14:11:54 +000024895static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024896{
Eric Biederman90089602004-05-28 14:11:54 +000024897 int tok;
24898 FILE *fp;
24899 int line;
24900 const char *filename;
24901 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024902 filename = 0;
24903 line = 0;
24904 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024905 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024906 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024907 const char *token_str;
24908 tok = peek(state);
24909 if (tok == TOK_EOF) {
24910 break;
24911 }
Eric Biederman41203d92004-11-08 09:31:09 +000024912 tk = eat(state, tok);
Eric Biederman90089602004-05-28 14:11:54 +000024913 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024914 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024915 tk->str_len ? tk->val.str :
24916 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024917
24918 file = state->file;
24919 while(file->macro && file->prev) {
24920 file = file->prev;
24921 }
24922 if (!file->macro &&
24923 ((file->line != line) || (file->basename != filename)))
24924 {
Eric Biederman90089602004-05-28 14:11:54 +000024925 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024926 if ((file->basename == filename) &&
24927 (line < file->line)) {
24928 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024929 fprintf(fp, "\n");
24930 line++;
24931 }
24932 }
24933 else {
24934 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024935 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024936 }
Eric Biederman132368b2004-11-09 08:59:23 +000024937 line = file->line;
24938 filename = file->basename;
24939 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024940 for(i = 0; i < col; i++) {
24941 fprintf(fp, " ");
24942 }
24943 }
24944
24945 fprintf(fp, "%s ", token_str);
24946
24947 if (state->compiler->debug & DEBUG_TOKENS) {
24948 loc(state->dbgout, state, 0);
24949 fprintf(state->dbgout, "%s <- `%s'\n",
24950 tokens[tok], token_str);
24951 }
24952 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024953}
24954
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024955static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024956 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024957{
24958 int i;
24959 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024960 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024961 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024962 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024963 state.compiler = compiler;
24964 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024965 state.file = 0;
24966 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24967 memset(&state.token[i], 0, sizeof(state.token[i]));
24968 state.token[i].tok = -1;
24969 }
Eric Biederman90089602004-05-28 14:11:54 +000024970 /* Remember the output descriptors */
24971 state.errout = stderr;
24972 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024973 /* Remember the output filename */
Eric Biederman5ade04a2003-10-22 04:03:46 +000024974 state.output = fopen(state.compiler->ofilename, "w");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024975 if (!state.output) {
24976 error(&state, 0, "Cannot open output file %s\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024977 state.compiler->ofilename);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024978 }
Eric Biederman90089602004-05-28 14:11:54 +000024979 /* Make certain a good cleanup happens */
24980 exit_state = &state;
24981 atexit(exit_cleanup);
24982
Eric Biedermanb138ac82003-04-22 18:44:01 +000024983 /* Prep the preprocessor */
24984 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024985 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024986 /* register the C keywords */
24987 register_keywords(&state);
24988 /* register the keywords the macro preprocessor knows */
24989 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024990 /* generate some builtin macros */
24991 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024992 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024993 state.i_switch = lookup(&state, "switch", 6);
24994 state.i_case = lookup(&state, "case", 4);
24995 state.i_continue = lookup(&state, "continue", 8);
24996 state.i_break = lookup(&state, "break", 5);
24997 state.i_default = lookup(&state, "default", 7);
24998 state.i_return = lookup(&state, "return", 6);
24999 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000025000 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
25001 state.i___FILE__ = lookup(&state, "__FILE__", 8);
25002 state.i___LINE__ = lookup(&state, "__LINE__", 8);
25003 /* Memorize where predefined identifiers are. */
25004 state.i___func__ = lookup(&state, "__func__", 8);
25005 /* Memorize where some attribute keywords are. */
25006 state.i_noinline = lookup(&state, "noinline", 8);
25007 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000025008 state.i_noreturn = lookup(&state, "noreturn", 8);
Eric Biederman90089602004-05-28 14:11:54 +000025009
25010 /* Process the command line macros */
25011 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000025012
25013 /* Allocate beginning bounding labels for the function list */
25014 state.first = label(&state);
25015 state.first->id |= TRIPLE_FLAG_VOLATILE;
25016 use_triple(state.first, state.first);
25017 ptr = label(&state);
25018 ptr->id |= TRIPLE_FLAG_VOLATILE;
25019 use_triple(ptr, ptr);
25020 flatten(&state, state.first, ptr);
25021
Eric Biederman5ade04a2003-10-22 04:03:46 +000025022 /* Allocate a label for the pool of global variables */
25023 state.global_pool = label(&state);
25024 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25025 flatten(&state, state.first, state.global_pool);
25026
Eric Biedermanb138ac82003-04-22 18:44:01 +000025027 /* Enter the globl definition scope */
25028 start_scope(&state);
25029 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025030
Eric Biedermanb138ac82003-04-22 18:44:01 +000025031 compile_file(&state, filename, 1);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025032
25033 while (includes) {
25034 compile_file(&state, includes->filename, 1);
25035 includes=includes->next;
25036 }
Eric Biederman90089602004-05-28 14:11:54 +000025037
25038 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025039 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025040 print_preprocessed_tokens(&state);
25041 return;
25042 }
25043
Eric Biedermanb138ac82003-04-22 18:44:01 +000025044 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025045
Eric Biedermanb138ac82003-04-22 18:44:01 +000025046 /* Exit the global definition scope */
25047 end_scope(&state);
25048
25049 /* Now that basic compilation has happened
25050 * optimize the intermediate code
25051 */
25052 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025053
Eric Biedermanb138ac82003-04-22 18:44:01 +000025054 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025055 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025056 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025057 }
Eric Biederman90089602004-05-28 14:11:54 +000025058 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025059}
25060
Eric Biederman90089602004-05-28 14:11:54 +000025061static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025062{
Eric Biederman90089602004-05-28 14:11:54 +000025063 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025064}
25065
25066static void usage(void)
25067{
Eric Biederman90089602004-05-28 14:11:54 +000025068 FILE *fp = stdout;
25069 version(fp);
25070 fprintf(fp,
25071 "\nUsage: romcc [options] <source>.c\n"
25072 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25073 "Options: \n"
25074 "-o <output file name>\n"
25075 "-f<option> Specify a generic compiler option\n"
25076 "-m<option> Specify a arch dependent option\n"
25077 "-- Specify this is the last option\n"
25078 "\nGeneric compiler options:\n"
25079 );
25080 compiler_usage(fp);
25081 fprintf(fp,
25082 "\nArchitecture compiler options:\n"
25083 );
25084 arch_usage(fp);
25085 fprintf(fp,
25086 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025087 );
25088}
25089
25090static void arg_error(char *fmt, ...)
25091{
25092 va_list args;
25093 va_start(args, fmt);
25094 vfprintf(stderr, fmt, args);
25095 va_end(args);
25096 usage();
25097 exit(1);
25098}
25099
25100int main(int argc, char **argv)
25101{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025102 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025103 struct compiler_state compiler;
25104 struct arch_state arch;
25105 int all_opts;
Eric Biederman90089602004-05-28 14:11:54 +000025106
25107
25108 /* I don't want any surprises */
25109 setlocale(LC_ALL, "C");
25110
Eric Biederman5ade04a2003-10-22 04:03:46 +000025111 init_compiler_state(&compiler);
25112 init_arch_state(&arch);
25113 filename = 0;
25114 all_opts = 0;
25115 while(argc > 1) {
25116 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25117 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025118 argv += 2;
25119 argc -= 2;
25120 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025121 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025122 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025123 result = -1;
25124 if (strcmp(argv[1], "--") == 0) {
25125 result = 0;
25126 all_opts = 1;
25127 }
Eric Biederman90089602004-05-28 14:11:54 +000025128 else if (strncmp(argv[1], "-E", 2) == 0) {
25129 result = compiler_encode_flag(&compiler, argv[1]);
25130 }
25131 else if (strncmp(argv[1], "-O", 2) == 0) {
25132 result = compiler_encode_flag(&compiler, argv[1]);
25133 }
25134 else if (strncmp(argv[1], "-I", 2) == 0) {
25135 result = compiler_encode_flag(&compiler, argv[1]);
25136 }
25137 else if (strncmp(argv[1], "-D", 2) == 0) {
25138 result = compiler_encode_flag(&compiler, argv[1]);
25139 }
25140 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025141 result = compiler_encode_flag(&compiler, argv[1]);
25142 }
25143 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25144 result = compiler_encode_flag(&compiler, argv[1]+2);
25145 }
25146 else if (strncmp(argv[1], "-f", 2) == 0) {
25147 result = compiler_encode_flag(&compiler, argv[1]+2);
25148 }
25149 else if (strncmp(argv[1], "-m", 2) == 0) {
25150 result = arch_encode_flag(&arch, argv[1]+2);
25151 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025152 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025153 struct filelist *old_head = include_filelist;
25154 include_filelist = malloc(sizeof(struct filelist));
25155 if (!include_filelist) {
25156 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025157 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025158 argv++;
25159 argc--;
25160 include_filelist->filename = argv[1];
25161 include_filelist->next = old_head;
25162 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025163 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025164 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025165 arg_error("Invalid option specified: %s\n",
25166 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025167 }
25168 argv++;
25169 argc--;
25170 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025171 else {
25172 if (filename) {
25173 arg_error("Only one filename may be specified\n");
25174 }
25175 filename = argv[1];
25176 argv++;
25177 argc--;
25178 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025179 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025180 if (!filename) {
25181 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025182 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025183 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025184
25185 return 0;
25186}