blob: b961d66c3294fd67def0d600dee19f905bf7a6a6 [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;
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001899 ptr = &used->use;
1900 while(*ptr) {
1901 if ((*ptr)->member == user) {
1902 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001903 }
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +00001904 ptr = &(*ptr)->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001905 }
1906 /* Append new to the head of the list,
1907 * copy_func and rename_block_variables
1908 * depends on this.
1909 */
1910 new = xcmalloc(sizeof(*new), "triple_set");
1911 new->member = user;
1912 new->next = used->use;
1913 used->use = new;
1914}
1915
1916static void unuse_triple(struct triple *used, struct triple *unuser)
1917{
1918 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001919 if (!used) {
1920 return;
1921 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001922 ptr = &used->use;
1923 while(*ptr) {
1924 use = *ptr;
1925 if (use->member == unuser) {
1926 *ptr = use->next;
1927 xfree(use);
1928 }
1929 else {
1930 ptr = &use->next;
1931 }
1932 }
1933}
1934
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001935static void put_occurance(struct occurance *occurance)
1936{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001937 if (occurance) {
1938 occurance->count -= 1;
1939 if (occurance->count <= 0) {
1940 if (occurance->parent) {
1941 put_occurance(occurance->parent);
1942 }
1943 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001944 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001945 }
1946}
1947
1948static void get_occurance(struct occurance *occurance)
1949{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001950 if (occurance) {
1951 occurance->count += 1;
1952 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001953}
1954
1955
1956static struct occurance *new_occurance(struct compile_state *state)
1957{
1958 struct occurance *result, *last;
1959 const char *filename;
1960 const char *function;
1961 int line, col;
1962
1963 function = "";
1964 filename = 0;
1965 line = 0;
1966 col = 0;
1967 if (state->file) {
1968 filename = state->file->report_name;
1969 line = state->file->report_line;
1970 col = get_col(state->file);
1971 }
1972 if (state->function) {
1973 function = state->function;
1974 }
1975 last = state->last_occurance;
1976 if (last &&
1977 (last->col == col) &&
1978 (last->line == line) &&
1979 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001980 ((last->filename == filename) ||
1981 (strcmp(last->filename, filename) == 0)))
1982 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001983 get_occurance(last);
1984 return last;
1985 }
1986 if (last) {
1987 state->last_occurance = 0;
1988 put_occurance(last);
1989 }
1990 result = xmalloc(sizeof(*result), "occurance");
1991 result->count = 2;
1992 result->filename = filename;
1993 result->function = function;
1994 result->line = line;
1995 result->col = col;
1996 result->parent = 0;
1997 state->last_occurance = result;
1998 return result;
1999}
2000
2001static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002002 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002003{
2004 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002005 if (top->parent) {
2006 internal_error(state, 0, "inlining an already inlined function?");
2007 }
2008 /* If I have a null base treat it that way */
2009 if ((base->parent == 0) &&
2010 (base->col == 0) &&
2011 (base->line == 0) &&
2012 (base->function[0] == '\0') &&
2013 (base->filename[0] == '\0')) {
2014 base = 0;
2015 }
2016 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002017 last = state->last_occurance;
2018 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002019 (last->parent == base) &&
2020 (last->col == top->col) &&
2021 (last->line == top->line) &&
2022 (last->function == top->function) &&
2023 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002024 get_occurance(last);
2025 return last;
2026 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002027 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002028 if (last) {
2029 state->last_occurance = 0;
2030 put_occurance(last);
2031 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002032 /* Generate a new occurance structure */
2033 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002034 result = xmalloc(sizeof(*result), "occurance");
2035 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002036 result->filename = top->filename;
2037 result->function = top->function;
2038 result->line = top->line;
2039 result->col = top->col;
2040 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002041 state->last_occurance = result;
2042 return result;
2043}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002044
2045static struct occurance dummy_occurance = {
2046 .count = 2,
2047 .filename = __FILE__,
2048 .function = "",
2049 .line = __LINE__,
2050 .col = 0,
2051 .parent = 0,
2052};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002053
Eric Biederman90089602004-05-28 14:11:54 +00002054/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002055 * from a triple. Having allows certain sanity checks to pass even
2056 * when the original triple that was pointed to is gone.
2057 */
Eric Biederman90089602004-05-28 14:11:54 +00002058static struct triple unknown_triple = {
2059 .next = &unknown_triple,
2060 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002061 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002062 .op = OP_UNKNOWNVAL,
2063 .lhs = 0,
2064 .rhs = 0,
2065 .misc = 0,
2066 .targ = 0,
2067 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002068 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002069 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002070 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002071 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002072};
2073
Eric Biederman0babc1c2003-05-09 02:39:00 +00002074
Eric Biederman90089602004-05-28 14:11:54 +00002075static size_t registers_of(struct compile_state *state, struct type *type);
2076
2077static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002078 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2079 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002080{
Eric Biederman90089602004-05-28 14:11:54 +00002081 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002082 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002083 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002084 dummy.op = op;
2085 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002086 valid_op(state, op);
2087 lhs = table_ops[op].lhs;
2088 rhs = table_ops[op].rhs;
2089 misc = table_ops[op].misc;
2090 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002091
2092 switch(op) {
2093 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002094 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002095 break;
2096 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002097 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002098 break;
2099 case OP_ADECL:
2100 lhs = registers_of(state, type);
2101 break;
2102 case OP_TUPLE:
2103 lhs = registers_of(state, type);
2104 break;
2105 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002106 rhs = rhs_wanted;
2107 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002108 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002109 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002110 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002111 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002112 }
2113 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002114 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002115 }
2116 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002117 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002118 }
2119 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002120 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002121 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002122
2123 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002124 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002125 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2126
2127 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2128 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002129 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002130 ret->lhs = lhs;
2131 ret->rhs = rhs;
2132 ret->misc = misc;
2133 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002134 ret->type = type;
2135 ret->next = ret;
2136 ret->prev = ret;
2137 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002138 /* A simple sanity check */
2139 if ((ret->op != op) ||
2140 (ret->lhs != lhs) ||
2141 (ret->rhs != rhs) ||
2142 (ret->misc != misc) ||
2143 (ret->targ != targ) ||
2144 (ret->type != type) ||
2145 (ret->next != ret) ||
2146 (ret->prev != ret) ||
2147 (ret->occurance != occurance)) {
2148 internal_error(state, ret, "huh?");
2149 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002150 return ret;
2151}
2152
Eric Biederman0babc1c2003-05-09 02:39:00 +00002153struct triple *dup_triple(struct compile_state *state, struct triple *src)
2154{
2155 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002156 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002157 src_lhs = src->lhs;
2158 src_rhs = src->rhs;
2159 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002160 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002161 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002162 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002163 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002164 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002165 return dup;
2166}
2167
Eric Biederman41203d92004-11-08 09:31:09 +00002168static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2169{
2170 struct triple *copy;
2171 copy = dup_triple(state, src);
2172 copy->use = 0;
2173 copy->next = copy->prev = copy;
2174 return copy;
2175}
2176
Eric Biederman0babc1c2003-05-09 02:39:00 +00002177static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002178 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002179{
2180 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002181 struct occurance *occurance;
2182 occurance = new_occurance(state);
2183 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002184 return ret;
2185}
2186
2187static struct triple *build_triple(struct compile_state *state,
2188 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002189 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002190{
2191 struct triple *ret;
2192 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002193 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002194 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002195 if (count > 0) {
2196 ret->param[0] = left;
2197 }
2198 if (count > 1) {
2199 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002200 }
2201 return ret;
2202}
2203
Eric Biederman0babc1c2003-05-09 02:39:00 +00002204static struct triple *triple(struct compile_state *state,
2205 int op, struct type *type, struct triple *left, struct triple *right)
2206{
2207 struct triple *ret;
2208 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002209 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002210 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002211 if (count >= 1) {
2212 ret->param[0] = left;
2213 }
2214 if (count >= 2) {
2215 ret->param[1] = right;
2216 }
2217 return ret;
2218}
2219
2220static struct triple *branch(struct compile_state *state,
2221 struct triple *targ, struct triple *test)
2222{
2223 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002224 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002225 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002226 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002227 } else {
2228 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002229 }
2230 TARG(ret, 0) = targ;
2231 /* record the branch target was used */
2232 if (!targ || (targ->op != OP_LABEL)) {
2233 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002234 }
2235 return ret;
2236}
2237
Eric Biederman90089602004-05-28 14:11:54 +00002238static int triple_is_label(struct compile_state *state, struct triple *ins);
2239static int triple_is_call(struct compile_state *state, struct triple *ins);
2240static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002241static void insert_triple(struct compile_state *state,
2242 struct triple *first, struct triple *ptr)
2243{
2244 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002245 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002246 internal_error(state, ptr, "expression already used");
2247 }
2248 ptr->next = first;
2249 ptr->prev = first->prev;
2250 ptr->prev->next = ptr;
2251 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002252
2253 if (triple_is_cbranch(state, ptr->prev) ||
2254 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002255 unuse_triple(first, ptr->prev);
2256 use_triple(ptr, ptr->prev);
2257 }
2258 }
2259}
2260
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002261static int triple_stores_block(struct compile_state *state, struct triple *ins)
2262{
2263 /* This function is used to determine if u.block
2264 * is utilized to store the current block number.
2265 */
2266 int stores_block;
2267 valid_ins(state, ins);
2268 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2269 return stores_block;
2270}
2271
Eric Biederman90089602004-05-28 14:11:54 +00002272static int triple_is_branch(struct compile_state *state, struct triple *ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002273static struct block *block_of_triple(struct compile_state *state,
2274 struct triple *ins)
2275{
2276 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002277 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002278 return 0;
2279 }
2280 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002281 while(ins != first && !triple_is_branch(state, ins->prev) &&
2282 !triple_stores_block(state, ins))
2283 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002284 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002285 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002286 }
2287 ins = ins->prev;
2288 }
Eric Biederman90089602004-05-28 14:11:54 +00002289 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002290}
2291
Eric Biederman90089602004-05-28 14:11:54 +00002292static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002293static struct triple *pre_triple(struct compile_state *state,
2294 struct triple *base,
2295 int op, struct type *type, struct triple *left, struct triple *right)
2296{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002297 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002298 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002299 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002300 /* If I am an OP_PIECE jump to the real instruction */
2301 if (base->op == OP_PIECE) {
2302 base = MISC(base, 0);
2303 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002304 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002305 get_occurance(base->occurance);
2306 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002307 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002308 if (triple_stores_block(state, ret)) {
2309 ret->u.block = block;
2310 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002311 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002312 for(i = 0; i < ret->lhs; i++) {
2313 struct triple *piece;
2314 piece = LHS(ret, i);
2315 insert_triple(state, base, piece);
2316 use_triple(ret, piece);
2317 use_triple(piece, ret);
2318 }
2319 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002320 block->first = ret;
2321 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002322 return ret;
2323}
2324
2325static struct triple *post_triple(struct compile_state *state,
2326 struct triple *base,
2327 int op, struct type *type, struct triple *left, struct triple *right)
2328{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002329 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002330 struct triple *ret, *next;
2331 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002332 /* If I am an OP_PIECE jump to the real instruction */
2333 if (base->op == OP_PIECE) {
2334 base = MISC(base, 0);
2335 }
2336 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002337 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002338 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002339 base = LHS(base, zlhs - 1);
2340 }
2341
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002342 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002343 get_occurance(base->occurance);
2344 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002345 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002346 if (triple_stores_block(state, ret)) {
2347 ret->u.block = block;
2348 }
Eric Biederman90089602004-05-28 14:11:54 +00002349 next = base->next;
2350 insert_triple(state, next, ret);
2351 zlhs = ret->lhs;
2352 for(i = 0; i < zlhs; i++) {
2353 struct triple *piece;
2354 piece = LHS(ret, i);
2355 insert_triple(state, next, piece);
2356 use_triple(ret, piece);
2357 use_triple(piece, ret);
2358 }
2359 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002360 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002361 if (zlhs) {
2362 block->last = LHS(ret, zlhs - 1);
2363 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002364 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002365 return ret;
2366}
2367
Eric Biederman90089602004-05-28 14:11:54 +00002368static struct type *reg_type(
2369 struct compile_state *state, struct type *type, int reg);
2370
2371static void generate_lhs_piece(
2372 struct compile_state *state, struct triple *ins, int index)
2373{
2374 struct type *piece_type;
2375 struct triple *piece;
2376 get_occurance(ins->occurance);
2377 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2378
2379 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2380 piece_type = piece_type->left;
2381 }
2382#if 0
2383{
2384 static void name_of(FILE *fp, struct type *type);
2385 FILE * fp = state->errout;
2386 fprintf(fp, "piece_type(%d): ", index);
2387 name_of(fp, piece_type);
2388 fprintf(fp, "\n");
2389}
2390#endif
2391 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2392 piece->u.cval = index;
2393 LHS(ins, piece->u.cval) = piece;
2394 MISC(piece, 0) = ins;
2395}
2396
2397static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2398{
2399 int i, zlhs;
2400 zlhs = ins->lhs;
2401 for(i = 0; i < zlhs; i++) {
2402 generate_lhs_piece(state, ins, i);
2403 }
2404}
2405
Eric Biedermanb138ac82003-04-22 18:44:01 +00002406static struct triple *label(struct compile_state *state)
2407{
2408 /* Labels don't get a type */
2409 struct triple *result;
2410 result = triple(state, OP_LABEL, &void_type, 0, 0);
2411 return result;
2412}
2413
Eric Biederman90089602004-05-28 14:11:54 +00002414static struct triple *mkprog(struct compile_state *state, ...)
2415{
2416 struct triple *prog, *head, *arg;
2417 va_list args;
2418 int i;
2419
2420 head = label(state);
2421 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2422 RHS(prog, 0) = head;
2423 va_start(args, state);
2424 i = 0;
2425 while((arg = va_arg(args, struct triple *)) != 0) {
2426 if (++i >= 100) {
2427 internal_error(state, 0, "too many arguments to mkprog");
2428 }
2429 flatten(state, head, arg);
2430 }
2431 va_end(args);
2432 prog->type = head->prev->type;
2433 return prog;
2434}
2435static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002436static void display_triple(FILE *fp, struct triple *ins)
2437{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002438 struct occurance *ptr;
2439 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002440 char pre, post, vol;
2441 pre = post = vol = ' ';
2442 if (ins) {
2443 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2444 pre = '^';
2445 }
2446 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2447 post = ',';
2448 }
2449 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2450 vol = 'v';
2451 }
2452 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002453 }
Eric Biederman90089602004-05-28 14:11:54 +00002454 if (ins == 0) {
2455 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002456 }
Eric Biederman90089602004-05-28 14:11:54 +00002457 else if (ins->op == OP_INTCONST) {
2458 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
2459 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002460 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002461 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002462 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002463 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2464 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2465 MISC(ins, 0), (unsigned long)(ins->u.cval));
2466 }
2467 else if (ins->op == OP_INDEX) {
2468 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2469 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2470 RHS(ins, 0), (unsigned long)(ins->u.cval));
2471 }
2472 else if (ins->op == OP_PIECE) {
2473 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2474 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002475 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002476 }
2477 else {
2478 int i, count;
Eric Biederman90089602004-05-28 14:11:54 +00002479 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
2480 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2481 if (table_ops[ins->op].flags & BITFIELD) {
2482 fprintf(fp, " <%2d-%2d:%2d>",
2483 ins->u.bitfield.offset,
2484 ins->u.bitfield.offset + ins->u.bitfield.size,
2485 ins->u.bitfield.size);
2486 }
2487 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002488 for(i = 0; i < count; i++) {
2489 fprintf(fp, " %-10p", ins->param[i]);
2490 }
2491 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002492 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002493 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002494 }
Eric Biederman90089602004-05-28 14:11:54 +00002495 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002496 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002497#if DEBUG_DISPLAY_TYPES
2498 fprintf(fp, " <");
2499 name_of(fp, ins->type);
2500 fprintf(fp, "> ");
2501#endif
2502#if DEBUG_DISPLAY_USES
2503 fprintf(fp, " [");
2504 for(user = ins->use; user; user = user->next) {
2505 fprintf(fp, " %-10p", user->member);
2506 }
2507 fprintf(fp, " ]");
2508#endif
2509 fprintf(fp, " @");
2510 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2511 fprintf(fp, " %s,%s:%d.%d",
2512 ptr->function,
2513 ptr->filename,
2514 ptr->line,
2515 ptr->col);
2516 }
2517 if (ins->op == OP_ASM) {
2518 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002519 }
2520 }
Eric Biederman90089602004-05-28 14:11:54 +00002521 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002522 fflush(fp);
2523}
2524
Eric Biederman90089602004-05-28 14:11:54 +00002525static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002526static void display_triple_changes(
2527 FILE *fp, const struct triple *new, const struct triple *orig)
2528{
2529
2530 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002531 new_count = TRIPLE_SIZE(new);
2532 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002533 if ((new->op != orig->op) ||
2534 (new_count != orig_count) ||
2535 (memcmp(orig->param, new->param,
2536 orig_count * sizeof(orig->param[0])) != 0) ||
2537 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
2538 {
2539 struct occurance *ptr;
2540 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002541 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002542 if (orig->op == new->op) {
2543 fprintf(fp, " %-11s", tops(orig->op));
2544 } else {
2545 fprintf(fp, " [%-10s %-10s]",
2546 tops(new->op), tops(orig->op));
2547 }
2548 min_count = new_count;
2549 if (min_count > orig_count) {
2550 min_count = orig_count;
2551 }
2552 for(indent = i = 0; i < min_count; i++) {
2553 if (orig->param[i] == new->param[i]) {
2554 fprintf(fp, " %-11p",
2555 orig->param[i]);
2556 indent += 12;
2557 } else {
2558 fprintf(fp, " [%-10p %-10p]",
2559 new->param[i],
2560 orig->param[i]);
2561 indent += 24;
2562 }
2563 }
2564 for(; i < orig_count; i++) {
2565 fprintf(fp, " [%-9p]", orig->param[i]);
2566 indent += 12;
2567 }
2568 for(; i < new_count; i++) {
2569 fprintf(fp, " [%-9p]", new->param[i]);
2570 indent += 12;
2571 }
2572 if ((new->op == OP_INTCONST)||
2573 (new->op == OP_ADDRCONST)) {
2574 fprintf(fp, " <0x%08lx>",
2575 (unsigned long)(new->u.cval));
2576 indent += 13;
2577 }
2578 for(;indent < 36; indent++) {
2579 putc(' ', fp);
2580 }
Eric Biederman90089602004-05-28 14:11:54 +00002581
2582#if DEBUG_DISPLAY_TYPES
2583 fprintf(fp, " <");
2584 name_of(fp, new->type);
2585 if (!equiv_types(new->type, orig->type)) {
2586 fprintf(fp, " -- ");
2587 name_of(fp, orig->type);
2588 }
2589 fprintf(fp, "> ");
2590#endif
2591
Eric Biederman5ade04a2003-10-22 04:03:46 +00002592 fprintf(fp, " @");
2593 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2594 fprintf(fp, " %s,%s:%d.%d",
2595 ptr->function,
2596 ptr->filename,
2597 ptr->line,
2598 ptr->col);
2599
2600 }
2601 fprintf(fp, "\n");
2602 fflush(fp);
2603 }
2604}
2605
Eric Biederman83b991a2003-10-11 06:20:25 +00002606static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002607{
2608 /* Does the triple have no side effects.
2609 * I.e. Rexecuting the triple with the same arguments
2610 * gives the same value.
2611 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002612 unsigned pure;
2613 valid_ins(state, ins);
2614 pure = PURE_BITS(table_ops[ins->op].flags);
2615 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002616 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002617 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002618 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002619 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002620}
2621
Eric Biederman90089602004-05-28 14:11:54 +00002622static int triple_is_branch_type(struct compile_state *state,
2623 struct triple *ins, unsigned type)
2624{
2625 /* Is this one of the passed branch types? */
2626 valid_ins(state, ins);
2627 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2628}
2629
Eric Biederman0babc1c2003-05-09 02:39:00 +00002630static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002631{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002632 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002633 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002634 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002635}
2636
Eric Biederman90089602004-05-28 14:11:54 +00002637static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002638{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002639 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002640 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002641}
2642
Eric Biederman90089602004-05-28 14:11:54 +00002643static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002644{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002645 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002646 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002647 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002648 type = BRANCH_BITS(table_ops[ins->op].flags);
2649 return (type != 0) && (type != CBRANCH);
2650}
2651
2652static int triple_is_call(struct compile_state *state, struct triple *ins)
2653{
2654 /* Is this triple a call instruction? */
2655 return triple_is_branch_type(state, ins, CALLBRANCH);
2656}
2657
2658static int triple_is_ret(struct compile_state *state, struct triple *ins)
2659{
2660 /* Is this triple a return instruction? */
2661 return triple_is_branch_type(state, ins, RETBRANCH);
2662}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002663
2664#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002665static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2666{
2667 /* Is this triple an unconditional branch and not a call or a
2668 * return? */
2669 return triple_is_branch_type(state, ins, UBRANCH);
2670}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002671#endif
Eric Biederman90089602004-05-28 14:11:54 +00002672
2673static int triple_is_end(struct compile_state *state, struct triple *ins)
2674{
2675 return triple_is_branch_type(state, ins, ENDBRANCH);
2676}
2677
2678static int triple_is_label(struct compile_state *state, struct triple *ins)
2679{
2680 valid_ins(state, ins);
2681 return (ins->op == OP_LABEL);
2682}
2683
2684static struct triple *triple_to_block_start(
2685 struct compile_state *state, struct triple *start)
2686{
2687 while(!triple_is_branch(state, start->prev) &&
2688 (!triple_is_label(state, start) || !start->use)) {
2689 start = start->prev;
2690 }
2691 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002692}
2693
Eric Biederman0babc1c2003-05-09 02:39:00 +00002694static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002695{
2696 /* This function is used to determine which triples need
2697 * a register.
2698 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002699 int is_def;
2700 valid_ins(state, ins);
2701 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002702 if (ins->lhs >= 1) {
2703 is_def = 0;
2704 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002705 return is_def;
2706}
2707
Eric Biederman83b991a2003-10-11 06:20:25 +00002708static int triple_is_structural(struct compile_state *state, struct triple *ins)
2709{
2710 int is_structural;
2711 valid_ins(state, ins);
2712 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2713 return is_structural;
2714}
2715
Eric Biederman90089602004-05-28 14:11:54 +00002716static int triple_is_part(struct compile_state *state, struct triple *ins)
2717{
2718 int is_part;
2719 valid_ins(state, ins);
2720 is_part = (table_ops[ins->op].flags & PART) == PART;
2721 return is_part;
2722}
2723
2724static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2725{
2726 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2727}
2728
Eric Biederman0babc1c2003-05-09 02:39:00 +00002729static struct triple **triple_iter(struct compile_state *state,
2730 size_t count, struct triple **vector,
2731 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002732{
2733 struct triple **ret;
2734 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002735 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002736 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002737 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002738 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002739 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002740 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002741 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002742 }
2743 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002744
Eric Biedermanb138ac82003-04-22 18:44:01 +00002745}
2746
2747static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002748 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002749{
Eric Biederman90089602004-05-28 14:11:54 +00002750 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002751 ins, last);
2752}
2753
2754static struct triple **triple_rhs(struct compile_state *state,
2755 struct triple *ins, struct triple **last)
2756{
Eric Biederman90089602004-05-28 14:11:54 +00002757 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002758 ins, last);
2759}
2760
2761static struct triple **triple_misc(struct compile_state *state,
2762 struct triple *ins, struct triple **last)
2763{
Eric Biederman90089602004-05-28 14:11:54 +00002764 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002765 ins, last);
2766}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002767
Eric Biederman90089602004-05-28 14:11:54 +00002768static struct triple **do_triple_targ(struct compile_state *state,
2769 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002770{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002771 size_t count;
2772 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002773 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002774 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002775 count = ins->targ;
2776 next_is_targ = 0;
2777 if (triple_is_cbranch(state, ins)) {
2778 next_is_targ = 1;
2779 }
2780 if (!call_edges && triple_is_call(state, ins)) {
2781 count = 0;
2782 }
2783 if (next_edges && triple_is_call(state, ins)) {
2784 next_is_targ = 1;
2785 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002786 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002787 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002788 if (!last) {
2789 ret = &ins->next;
2790 } else if (last == &ins->next) {
2791 last = 0;
2792 }
2793 }
2794 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002795 if (!last) {
2796 ret = vector;
2797 }
2798 else if ((last >= vector) && (last < (vector + count - 1))) {
2799 ret = last + 1;
2800 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002801 else if (last == vector + count - 1) {
2802 last = 0;
2803 }
2804 }
Eric Biederman90089602004-05-28 14:11:54 +00002805 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002806 struct triple_set *use;
2807 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002808 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002809 continue;
2810 }
2811 if (!last) {
2812 ret = &use->member->next;
2813 break;
2814 }
2815 else if (last == &use->member->next) {
2816 last = 0;
2817 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002818 }
2819 }
2820 return ret;
2821}
2822
Eric Biederman90089602004-05-28 14:11:54 +00002823static struct triple **triple_targ(struct compile_state *state,
2824 struct triple *ins, struct triple **last)
2825{
2826 return do_triple_targ(state, ins, last, 1, 1);
2827}
2828
2829static struct triple **triple_edge_targ(struct compile_state *state,
2830 struct triple *ins, struct triple **last)
2831{
2832 return do_triple_targ(state, ins, last,
2833 state->functions_joined, !state->functions_joined);
2834}
2835
2836static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2837{
2838 struct triple *next;
2839 int lhs, i;
2840 lhs = ins->lhs;
2841 next = ins->next;
2842 for(i = 0; i < lhs; i++) {
2843 struct triple *piece;
2844 piece = LHS(ins, i);
2845 if (next != piece) {
2846 internal_error(state, ins, "malformed lhs on %s",
2847 tops(ins->op));
2848 }
2849 if (next->op != OP_PIECE) {
2850 internal_error(state, ins, "bad lhs op %s at %d on %s",
2851 tops(next->op), i, tops(ins->op));
2852 }
2853 if (next->u.cval != i) {
2854 internal_error(state, ins, "bad u.cval of %d %d expected",
2855 next->u.cval, i);
2856 }
2857 next = next->next;
2858 }
2859 return next;
2860}
2861
2862/* Function piece accessor functions */
2863static struct triple *do_farg(struct compile_state *state,
2864 struct triple *func, unsigned index)
2865{
2866 struct type *ftype;
2867 struct triple *first, *arg;
2868 unsigned i;
2869
2870 ftype = func->type;
2871 if((index < 0) || (index >= (ftype->elements + 2))) {
2872 internal_error(state, func, "bad argument index: %d", index);
2873 }
2874 first = RHS(func, 0);
2875 arg = first->next;
2876 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2877 /* do nothing */
2878 }
2879 if (arg->op != OP_ADECL) {
2880 internal_error(state, 0, "arg not adecl?");
2881 }
2882 return arg;
2883}
2884static struct triple *fresult(struct compile_state *state, struct triple *func)
2885{
2886 return do_farg(state, func, 0);
2887}
2888static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2889{
2890 return do_farg(state, func, 1);
2891}
2892static struct triple *farg(struct compile_state *state,
2893 struct triple *func, unsigned index)
2894{
2895 return do_farg(state, func, index + 2);
2896}
2897
2898
2899static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2900{
2901 struct triple *first, *ins;
2902 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2903 first = ins = RHS(func, 0);
2904 do {
2905 if (triple_is_label(state, ins) && ins->use) {
2906 fprintf(fp, "%p:\n", ins);
2907 }
2908 display_triple(fp, ins);
2909
2910 if (triple_is_branch(state, ins)) {
2911 fprintf(fp, "\n");
2912 }
2913 if (ins->next->prev != ins) {
2914 internal_error(state, ins->next, "bad prev");
2915 }
2916 ins = ins->next;
2917 } while(ins != first);
2918}
2919
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002920static void verify_use(struct compile_state *state,
2921 struct triple *user, struct triple *used)
2922{
2923 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002924 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002925 for(i = 0; i < size; i++) {
2926 if (user->param[i] == used) {
2927 break;
2928 }
2929 }
2930 if (triple_is_branch(state, user)) {
2931 if (user->next == used) {
2932 i = -1;
2933 }
2934 }
2935 if (i == size) {
2936 internal_error(state, user, "%s(%p) does not use %s(%p)",
2937 tops(user->op), user, tops(used->op), used);
2938 }
2939}
2940
2941static int find_rhs_use(struct compile_state *state,
2942 struct triple *user, struct triple *used)
2943{
2944 struct triple **param;
2945 int size, i;
2946 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002947
2948#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002949#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002950#endif
Eric Biederman90089602004-05-28 14:11:54 +00002951 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002952 param = &RHS(user, 0);
2953 for(i = 0; i < size; i++) {
2954 if (param[i] == used) {
2955 return i;
2956 }
2957 }
2958 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002959}
2960
2961static void free_triple(struct compile_state *state, struct triple *ptr)
2962{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002963 size_t size;
2964 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002965 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002966 ptr->prev->next = ptr->next;
2967 ptr->next->prev = ptr->prev;
2968 if (ptr->use) {
2969 internal_error(state, ptr, "ptr->use != 0");
2970 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002971 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002972 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002973 xfree(ptr);
2974}
2975
2976static void release_triple(struct compile_state *state, struct triple *ptr)
2977{
2978 struct triple_set *set, *next;
2979 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002980 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002981 if (ptr == &unknown_triple) {
2982 return;
2983 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002984 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002985 /* Make certain the we are not the first or last element of a block */
2986 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002987 if (block) {
2988 if ((block->last == ptr) && (block->first == ptr)) {
2989 block->last = block->first = 0;
2990 }
2991 else if (block->last == ptr) {
2992 block->last = ptr->prev;
2993 }
2994 else if (block->first == ptr) {
2995 block->first = ptr->next;
2996 }
Eric Biederman66fe2222003-07-04 15:14:04 +00002997 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002998 /* Remove ptr from use chains where it is the user */
2999 expr = triple_rhs(state, ptr, 0);
3000 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3001 if (*expr) {
3002 unuse_triple(*expr, ptr);
3003 }
3004 }
3005 expr = triple_lhs(state, ptr, 0);
3006 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3007 if (*expr) {
3008 unuse_triple(*expr, ptr);
3009 }
3010 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003011 expr = triple_misc(state, ptr, 0);
3012 for(; expr; expr = triple_misc(state, ptr, expr)) {
3013 if (*expr) {
3014 unuse_triple(*expr, ptr);
3015 }
3016 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003017 expr = triple_targ(state, ptr, 0);
3018 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003019 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003020 unuse_triple(*expr, ptr);
3021 }
3022 }
3023 /* Reomve ptr from use chains where it is used */
3024 for(set = ptr->use; set; set = next) {
3025 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003026 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003027 expr = triple_rhs(state, set->member, 0);
3028 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3029 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003030 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003031 }
3032 }
3033 expr = triple_lhs(state, set->member, 0);
3034 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3035 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003036 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003037 }
3038 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003039 expr = triple_misc(state, set->member, 0);
3040 for(; expr; expr = triple_misc(state, set->member, expr)) {
3041 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003042 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003043 }
3044 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003045 expr = triple_targ(state, set->member, 0);
3046 for(; expr; expr = triple_targ(state, set->member, expr)) {
3047 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003048 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003049 }
3050 }
3051 unuse_triple(ptr, set->member);
3052 }
3053 free_triple(state, ptr);
3054}
3055
Eric Biederman5ade04a2003-10-22 04:03:46 +00003056static void print_triples(struct compile_state *state);
3057static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003058
Jason Schildt27b85112005-08-10 14:31:52 +00003059#define TOK_UNKNOWN 0
3060#define TOK_SPACE 1
3061#define TOK_SEMI 2
3062#define TOK_LBRACE 3
3063#define TOK_RBRACE 4
3064#define TOK_COMMA 5
3065#define TOK_EQ 6
3066#define TOK_COLON 7
3067#define TOK_LBRACKET 8
3068#define TOK_RBRACKET 9
3069#define TOK_LPAREN 10
3070#define TOK_RPAREN 11
3071#define TOK_STAR 12
3072#define TOK_DOTS 13
3073#define TOK_MORE 14
3074#define TOK_LESS 15
3075#define TOK_TIMESEQ 16
3076#define TOK_DIVEQ 17
3077#define TOK_MODEQ 18
3078#define TOK_PLUSEQ 19
3079#define TOK_MINUSEQ 20
3080#define TOK_SLEQ 21
3081#define TOK_SREQ 22
3082#define TOK_ANDEQ 23
3083#define TOK_XOREQ 24
3084#define TOK_OREQ 25
3085#define TOK_EQEQ 26
3086#define TOK_NOTEQ 27
3087#define TOK_QUEST 28
3088#define TOK_LOGOR 29
3089#define TOK_LOGAND 30
3090#define TOK_OR 31
3091#define TOK_AND 32
3092#define TOK_XOR 33
3093#define TOK_LESSEQ 34
3094#define TOK_MOREEQ 35
3095#define TOK_SL 36
3096#define TOK_SR 37
3097#define TOK_PLUS 38
3098#define TOK_MINUS 39
3099#define TOK_DIV 40
3100#define TOK_MOD 41
3101#define TOK_PLUSPLUS 42
3102#define TOK_MINUSMINUS 43
3103#define TOK_BANG 44
3104#define TOK_ARROW 45
3105#define TOK_DOT 46
3106#define TOK_TILDE 47
3107#define TOK_LIT_STRING 48
3108#define TOK_LIT_CHAR 49
3109#define TOK_LIT_INT 50
3110#define TOK_LIT_FLOAT 51
3111#define TOK_MACRO 52
3112#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003113
Jason Schildt27b85112005-08-10 14:31:52 +00003114#define TOK_IDENT 54
3115#define TOK_STRUCT_NAME 55
3116#define TOK_ENUM_CONST 56
3117#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003118
Jason Schildt27b85112005-08-10 14:31:52 +00003119#define TOK_AUTO 58
3120#define TOK_BREAK 59
3121#define TOK_CASE 60
3122#define TOK_CHAR 61
3123#define TOK_CONST 62
3124#define TOK_CONTINUE 63
3125#define TOK_DEFAULT 64
3126#define TOK_DO 65
3127#define TOK_DOUBLE 66
3128#define TOK_ELSE 67
3129#define TOK_ENUM 68
3130#define TOK_EXTERN 69
3131#define TOK_FLOAT 70
3132#define TOK_FOR 71
3133#define TOK_GOTO 72
3134#define TOK_IF 73
3135#define TOK_INLINE 74
3136#define TOK_INT 75
3137#define TOK_LONG 76
3138#define TOK_REGISTER 77
3139#define TOK_RESTRICT 78
3140#define TOK_RETURN 79
3141#define TOK_SHORT 80
3142#define TOK_SIGNED 81
3143#define TOK_SIZEOF 82
3144#define TOK_STATIC 83
3145#define TOK_STRUCT 84
3146#define TOK_SWITCH 85
3147#define TOK_TYPEDEF 86
3148#define TOK_UNION 87
3149#define TOK_UNSIGNED 88
3150#define TOK_VOID 89
3151#define TOK_VOLATILE 90
3152#define TOK_WHILE 91
3153#define TOK_ASM 92
3154#define TOK_ATTRIBUTE 93
3155#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003156#define TOK_FIRST_KEYWORD TOK_AUTO
3157#define TOK_LAST_KEYWORD TOK_ALIGNOF
3158
Eric Biedermancb364952004-11-15 10:46:44 +00003159#define TOK_MDEFINE 100
3160#define TOK_MDEFINED 101
3161#define TOK_MUNDEF 102
3162#define TOK_MINCLUDE 103
3163#define TOK_MLINE 104
3164#define TOK_MERROR 105
3165#define TOK_MWARNING 106
3166#define TOK_MPRAGMA 107
3167#define TOK_MIFDEF 108
3168#define TOK_MIFNDEF 109
3169#define TOK_MELIF 110
3170#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003171
Eric Biedermancb364952004-11-15 10:46:44 +00003172#define TOK_FIRST_MACRO TOK_MDEFINE
3173#define TOK_LAST_MACRO TOK_MENDIF
Eric Biedermanb138ac82003-04-22 18:44:01 +00003174
Eric Biedermancb364952004-11-15 10:46:44 +00003175#define TOK_MIF 112
3176#define TOK_MELSE 113
3177#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003178
Eric Biedermancb364952004-11-15 10:46:44 +00003179#define TOK_EOL 115
3180#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003181
3182static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003183[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003184[TOK_SPACE ] = ":space:",
3185[TOK_SEMI ] = ";",
3186[TOK_LBRACE ] = "{",
3187[TOK_RBRACE ] = "}",
3188[TOK_COMMA ] = ",",
3189[TOK_EQ ] = "=",
3190[TOK_COLON ] = ":",
3191[TOK_LBRACKET ] = "[",
3192[TOK_RBRACKET ] = "]",
3193[TOK_LPAREN ] = "(",
3194[TOK_RPAREN ] = ")",
3195[TOK_STAR ] = "*",
3196[TOK_DOTS ] = "...",
3197[TOK_MORE ] = ">",
3198[TOK_LESS ] = "<",
3199[TOK_TIMESEQ ] = "*=",
3200[TOK_DIVEQ ] = "/=",
3201[TOK_MODEQ ] = "%=",
3202[TOK_PLUSEQ ] = "+=",
3203[TOK_MINUSEQ ] = "-=",
3204[TOK_SLEQ ] = "<<=",
3205[TOK_SREQ ] = ">>=",
3206[TOK_ANDEQ ] = "&=",
3207[TOK_XOREQ ] = "^=",
3208[TOK_OREQ ] = "|=",
3209[TOK_EQEQ ] = "==",
3210[TOK_NOTEQ ] = "!=",
3211[TOK_QUEST ] = "?",
3212[TOK_LOGOR ] = "||",
3213[TOK_LOGAND ] = "&&",
3214[TOK_OR ] = "|",
3215[TOK_AND ] = "&",
3216[TOK_XOR ] = "^",
3217[TOK_LESSEQ ] = "<=",
3218[TOK_MOREEQ ] = ">=",
3219[TOK_SL ] = "<<",
3220[TOK_SR ] = ">>",
3221[TOK_PLUS ] = "+",
3222[TOK_MINUS ] = "-",
3223[TOK_DIV ] = "/",
3224[TOK_MOD ] = "%",
3225[TOK_PLUSPLUS ] = "++",
3226[TOK_MINUSMINUS ] = "--",
3227[TOK_BANG ] = "!",
3228[TOK_ARROW ] = "->",
3229[TOK_DOT ] = ".",
3230[TOK_TILDE ] = "~",
3231[TOK_LIT_STRING ] = ":string:",
3232[TOK_IDENT ] = ":ident:",
3233[TOK_TYPE_NAME ] = ":typename:",
3234[TOK_LIT_CHAR ] = ":char:",
3235[TOK_LIT_INT ] = ":integer:",
3236[TOK_LIT_FLOAT ] = ":float:",
3237[TOK_MACRO ] = "#",
3238[TOK_CONCATENATE ] = "##",
3239
3240[TOK_AUTO ] = "auto",
3241[TOK_BREAK ] = "break",
3242[TOK_CASE ] = "case",
3243[TOK_CHAR ] = "char",
3244[TOK_CONST ] = "const",
3245[TOK_CONTINUE ] = "continue",
3246[TOK_DEFAULT ] = "default",
3247[TOK_DO ] = "do",
3248[TOK_DOUBLE ] = "double",
3249[TOK_ELSE ] = "else",
3250[TOK_ENUM ] = "enum",
3251[TOK_EXTERN ] = "extern",
3252[TOK_FLOAT ] = "float",
3253[TOK_FOR ] = "for",
3254[TOK_GOTO ] = "goto",
3255[TOK_IF ] = "if",
3256[TOK_INLINE ] = "inline",
3257[TOK_INT ] = "int",
3258[TOK_LONG ] = "long",
3259[TOK_REGISTER ] = "register",
3260[TOK_RESTRICT ] = "restrict",
3261[TOK_RETURN ] = "return",
3262[TOK_SHORT ] = "short",
3263[TOK_SIGNED ] = "signed",
3264[TOK_SIZEOF ] = "sizeof",
3265[TOK_STATIC ] = "static",
3266[TOK_STRUCT ] = "struct",
3267[TOK_SWITCH ] = "switch",
3268[TOK_TYPEDEF ] = "typedef",
3269[TOK_UNION ] = "union",
3270[TOK_UNSIGNED ] = "unsigned",
3271[TOK_VOID ] = "void",
3272[TOK_VOLATILE ] = "volatile",
3273[TOK_WHILE ] = "while",
3274[TOK_ASM ] = "asm",
3275[TOK_ATTRIBUTE ] = "__attribute__",
3276[TOK_ALIGNOF ] = "__alignof__",
3277
Eric Biederman41203d92004-11-08 09:31:09 +00003278[TOK_MDEFINE ] = "#define",
3279[TOK_MDEFINED ] = "#defined",
3280[TOK_MUNDEF ] = "#undef",
3281[TOK_MINCLUDE ] = "#include",
3282[TOK_MLINE ] = "#line",
3283[TOK_MERROR ] = "#error",
3284[TOK_MWARNING ] = "#warning",
3285[TOK_MPRAGMA ] = "#pragma",
3286[TOK_MIFDEF ] = "#ifdef",
3287[TOK_MIFNDEF ] = "#ifndef",
3288[TOK_MELIF ] = "#elif",
3289[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003290
Eric Biederman41203d92004-11-08 09:31:09 +00003291[TOK_MIF ] = "#if",
3292[TOK_MELSE ] = "#else",
3293[TOK_MIDENT ] = "#:ident:",
3294[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003295[TOK_EOF ] = "EOF",
3296};
3297
3298static unsigned int hash(const char *str, int str_len)
3299{
3300 unsigned int hash;
3301 const char *end;
3302 end = str + str_len;
3303 hash = 0;
3304 for(; str < end; str++) {
3305 hash = (hash *263) + *str;
3306 }
3307 hash = hash & (HASH_TABLE_SIZE -1);
3308 return hash;
3309}
3310
3311static struct hash_entry *lookup(
3312 struct compile_state *state, const char *name, int name_len)
3313{
3314 struct hash_entry *entry;
3315 unsigned int index;
3316 index = hash(name, name_len);
3317 entry = state->hash_table[index];
3318 while(entry &&
3319 ((entry->name_len != name_len) ||
3320 (memcmp(entry->name, name, name_len) != 0))) {
3321 entry = entry->next;
3322 }
3323 if (!entry) {
3324 char *new_name;
3325 /* Get a private copy of the name */
3326 new_name = xmalloc(name_len + 1, "hash_name");
3327 memcpy(new_name, name, name_len);
3328 new_name[name_len] = '\0';
3329
3330 /* Create a new hash entry */
3331 entry = xcmalloc(sizeof(*entry), "hash_entry");
3332 entry->next = state->hash_table[index];
3333 entry->name = new_name;
3334 entry->name_len = name_len;
3335
3336 /* Place the new entry in the hash table */
3337 state->hash_table[index] = entry;
3338 }
3339 return entry;
3340}
3341
3342static void ident_to_keyword(struct compile_state *state, struct token *tk)
3343{
3344 struct hash_entry *entry;
3345 entry = tk->ident;
3346 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3347 (entry->tok == TOK_ENUM_CONST) ||
3348 ((entry->tok >= TOK_FIRST_KEYWORD) &&
3349 (entry->tok <= TOK_LAST_KEYWORD)))) {
3350 tk->tok = entry->tok;
3351 }
3352}
3353
3354static void ident_to_macro(struct compile_state *state, struct token *tk)
3355{
3356 struct hash_entry *entry;
3357 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003358 if (!entry)
3359 return;
3360 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003361 tk->tok = entry->tok;
3362 }
Eric Biederman41203d92004-11-08 09:31:09 +00003363 else if (entry->tok == TOK_IF) {
3364 tk->tok = TOK_MIF;
3365 }
3366 else if (entry->tok == TOK_ELSE) {
3367 tk->tok = TOK_MELSE;
3368 }
3369 else {
3370 tk->tok = TOK_MIDENT;
3371 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003372}
3373
3374static void hash_keyword(
3375 struct compile_state *state, const char *keyword, int tok)
3376{
3377 struct hash_entry *entry;
3378 entry = lookup(state, keyword, strlen(keyword));
3379 if (entry && entry->tok != TOK_UNKNOWN) {
3380 die("keyword %s already hashed", keyword);
3381 }
3382 entry->tok = tok;
3383}
3384
Eric Biederman90089602004-05-28 14:11:54 +00003385static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003386 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003387 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003388{
3389 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003390 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003391 error(state, 0, "%s already defined", ident->name);
3392 }
3393 sym = xcmalloc(sizeof(*sym), "symbol");
3394 sym->ident = ident;
3395 sym->def = def;
3396 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003397 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003398 sym->next = *chain;
3399 *chain = sym;
3400}
3401
Eric Biederman90089602004-05-28 14:11:54 +00003402static void symbol(
3403 struct compile_state *state, struct hash_entry *ident,
3404 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003405{
Eric Biederman90089602004-05-28 14:11:54 +00003406 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3407}
3408
3409static void var_symbol(struct compile_state *state,
3410 struct hash_entry *ident, struct triple *def)
3411{
3412 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3413 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003414 }
Eric Biederman90089602004-05-28 14:11:54 +00003415 symbol(state, ident, &ident->sym_ident, def, def->type);
3416}
3417
3418static void label_symbol(struct compile_state *state,
3419 struct hash_entry *ident, struct triple *label, int depth)
3420{
3421 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003422}
3423
Eric Biedermanb138ac82003-04-22 18:44:01 +00003424static void start_scope(struct compile_state *state)
3425{
3426 state->scope_depth++;
3427}
3428
Eric Biederman90089602004-05-28 14:11:54 +00003429static void end_scope_syms(struct compile_state *state,
3430 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003431{
3432 struct symbol *sym, *next;
3433 sym = *chain;
3434 while(sym && (sym->scope_depth == depth)) {
3435 next = sym->next;
3436 xfree(sym);
3437 sym = next;
3438 }
3439 *chain = sym;
3440}
3441
3442static void end_scope(struct compile_state *state)
3443{
3444 int i;
3445 int depth;
3446 /* Walk through the hash table and remove all symbols
3447 * in the current scope.
3448 */
3449 depth = state->scope_depth;
3450 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3451 struct hash_entry *entry;
3452 entry = state->hash_table[i];
3453 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003454 end_scope_syms(state, &entry->sym_label, depth);
3455 end_scope_syms(state, &entry->sym_tag, depth);
3456 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003457 entry = entry->next;
3458 }
3459 }
3460 state->scope_depth = depth - 1;
3461}
3462
3463static void register_keywords(struct compile_state *state)
3464{
3465 hash_keyword(state, "auto", TOK_AUTO);
3466 hash_keyword(state, "break", TOK_BREAK);
3467 hash_keyword(state, "case", TOK_CASE);
3468 hash_keyword(state, "char", TOK_CHAR);
3469 hash_keyword(state, "const", TOK_CONST);
3470 hash_keyword(state, "continue", TOK_CONTINUE);
3471 hash_keyword(state, "default", TOK_DEFAULT);
3472 hash_keyword(state, "do", TOK_DO);
3473 hash_keyword(state, "double", TOK_DOUBLE);
3474 hash_keyword(state, "else", TOK_ELSE);
3475 hash_keyword(state, "enum", TOK_ENUM);
3476 hash_keyword(state, "extern", TOK_EXTERN);
3477 hash_keyword(state, "float", TOK_FLOAT);
3478 hash_keyword(state, "for", TOK_FOR);
3479 hash_keyword(state, "goto", TOK_GOTO);
3480 hash_keyword(state, "if", TOK_IF);
3481 hash_keyword(state, "inline", TOK_INLINE);
3482 hash_keyword(state, "int", TOK_INT);
3483 hash_keyword(state, "long", TOK_LONG);
3484 hash_keyword(state, "register", TOK_REGISTER);
3485 hash_keyword(state, "restrict", TOK_RESTRICT);
3486 hash_keyword(state, "return", TOK_RETURN);
3487 hash_keyword(state, "short", TOK_SHORT);
3488 hash_keyword(state, "signed", TOK_SIGNED);
3489 hash_keyword(state, "sizeof", TOK_SIZEOF);
3490 hash_keyword(state, "static", TOK_STATIC);
3491 hash_keyword(state, "struct", TOK_STRUCT);
3492 hash_keyword(state, "switch", TOK_SWITCH);
3493 hash_keyword(state, "typedef", TOK_TYPEDEF);
3494 hash_keyword(state, "union", TOK_UNION);
3495 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3496 hash_keyword(state, "void", TOK_VOID);
3497 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003498 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003499 hash_keyword(state, "while", TOK_WHILE);
3500 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003501 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003502 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3503 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3504}
3505
3506static void register_macro_keywords(struct compile_state *state)
3507{
Eric Biederman41203d92004-11-08 09:31:09 +00003508 hash_keyword(state, "define", TOK_MDEFINE);
3509 hash_keyword(state, "defined", TOK_MDEFINED);
3510 hash_keyword(state, "undef", TOK_MUNDEF);
3511 hash_keyword(state, "include", TOK_MINCLUDE);
3512 hash_keyword(state, "line", TOK_MLINE);
3513 hash_keyword(state, "error", TOK_MERROR);
3514 hash_keyword(state, "warning", TOK_MWARNING);
3515 hash_keyword(state, "pragma", TOK_MPRAGMA);
3516 hash_keyword(state, "ifdef", TOK_MIFDEF);
3517 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3518 hash_keyword(state, "elif", TOK_MELIF);
3519 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003520}
3521
Eric Biederman90089602004-05-28 14:11:54 +00003522
3523static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3524{
3525 if (ident->sym_define != 0) {
3526 struct macro *macro;
3527 struct macro_arg *arg, *anext;
3528 macro = ident->sym_define;
3529 ident->sym_define = 0;
3530
3531 /* Free the macro arguments... */
3532 anext = macro->args;
3533 while(anext) {
3534 arg = anext;
3535 anext = arg->next;
3536 xfree(arg);
3537 }
3538
3539 /* Free the macro buffer */
3540 xfree(macro->buf);
3541
3542 /* Now free the macro itself */
3543 xfree(macro);
3544 }
3545}
3546
Eric Biedermancb364952004-11-15 10:46:44 +00003547static void do_define_macro(struct compile_state *state,
3548 struct hash_entry *ident, const char *body,
3549 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003550{
3551 struct macro *macro;
3552 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003553 size_t body_len;
3554
3555 /* Find the length of the body */
3556 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003557 macro = ident->sym_define;
3558 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003559 int identical_bodies, identical_args;
3560 struct macro_arg *oarg;
3561 /* Explicitly allow identical redfinitions of the same macro */
3562 identical_bodies =
3563 (macro->buf_len == body_len) &&
3564 (memcmp(macro->buf, body, body_len) == 0);
3565 identical_args = macro->argc == argc;
3566 oarg = macro->args;
3567 arg = args;
3568 while(identical_args && arg) {
3569 identical_args = oarg->ident == arg->ident;
3570 arg = arg->next;
3571 oarg = oarg->next;
3572 }
3573 if (identical_bodies && identical_args) {
3574 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003575 return;
3576 }
3577 error(state, 0, "macro %s already defined\n", ident->name);
3578 }
3579#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003580 fprintf(state->errout, "#define %s: `%*.*s'\n",
3581 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003582#endif
3583 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003584 macro->ident = ident;
3585 macro->buf = body;
3586 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003587 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003588 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003589
3590 ident->sym_define = macro;
3591}
Eric Biedermancb364952004-11-15 10:46:44 +00003592
3593static void define_macro(
3594 struct compile_state *state,
3595 struct hash_entry *ident,
3596 const char *body, int body_len,
3597 int argc, struct macro_arg *args)
3598{
3599 char *buf;
3600 buf = xmalloc(body_len + 1, "macro buf");
3601 memcpy(buf, body, body_len);
3602 buf[body_len] = '\0';
3603 do_define_macro(state, ident, buf, argc, args);
3604}
Eric Biederman90089602004-05-28 14:11:54 +00003605
3606static void register_builtin_macro(struct compile_state *state,
3607 const char *name, const char *value)
3608{
3609 struct hash_entry *ident;
3610
3611 if (value[0] == '(') {
3612 internal_error(state, 0, "Builtin macros with arguments not supported");
3613 }
3614 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003615 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003616}
3617
3618static void register_builtin_macros(struct compile_state *state)
3619{
3620 char buf[30];
3621 char scratch[30];
3622 time_t now;
3623 struct tm *tm;
3624 now = time(NULL);
3625 tm = localtime(&now);
3626
3627 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3628 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3629 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3630 register_builtin_macro(state, "__LINE__", "54321");
3631
3632 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3633 sprintf(buf, "\"%s\"", scratch);
3634 register_builtin_macro(state, "__DATE__", buf);
3635
3636 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3637 sprintf(buf, "\"%s\"", scratch);
3638 register_builtin_macro(state, "__TIME__", buf);
3639
3640 /* I can't be a conforming implementation of C :( */
3641 register_builtin_macro(state, "__STDC__", "0");
3642 /* In particular I don't conform to C99 */
3643 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
3644
3645}
3646
3647static void process_cmdline_macros(struct compile_state *state)
3648{
3649 const char **macro, *name;
3650 struct hash_entry *ident;
3651 for(macro = state->compiler->defines; (name = *macro); macro++) {
3652 const char *body;
3653 size_t name_len;
3654
3655 name_len = strlen(name);
3656 body = strchr(name, '=');
3657 if (!body) {
3658 body = "\0";
3659 } else {
3660 name_len = body - name;
3661 body++;
3662 }
3663 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003664 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003665 }
3666 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3667 ident = lookup(state, name, strlen(name));
3668 undef_macro(state, ident);
3669 }
3670}
3671
Eric Biedermanb138ac82003-04-22 18:44:01 +00003672static int spacep(int c)
3673{
3674 int ret = 0;
3675 switch(c) {
3676 case ' ':
3677 case '\t':
3678 case '\f':
3679 case '\v':
3680 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003681 ret = 1;
3682 break;
3683 }
3684 return ret;
3685}
3686
Eric Biedermanb138ac82003-04-22 18:44:01 +00003687static int digitp(int c)
3688{
3689 int ret = 0;
3690 switch(c) {
3691 case '0': case '1': case '2': case '3': case '4':
3692 case '5': case '6': case '7': case '8': case '9':
3693 ret = 1;
3694 break;
3695 }
3696 return ret;
3697}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003698static int digval(int c)
3699{
3700 int val = -1;
3701 if ((c >= '0') && (c <= '9')) {
3702 val = c - '0';
3703 }
3704 return val;
3705}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003706
3707static int hexdigitp(int c)
3708{
3709 int ret = 0;
3710 switch(c) {
3711 case '0': case '1': case '2': case '3': case '4':
3712 case '5': case '6': case '7': case '8': case '9':
3713 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3714 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3715 ret = 1;
3716 break;
3717 }
3718 return ret;
3719}
3720static int hexdigval(int c)
3721{
3722 int val = -1;
3723 if ((c >= '0') && (c <= '9')) {
3724 val = c - '0';
3725 }
3726 else if ((c >= 'A') && (c <= 'F')) {
3727 val = 10 + (c - 'A');
3728 }
3729 else if ((c >= 'a') && (c <= 'f')) {
3730 val = 10 + (c - 'a');
3731 }
3732 return val;
3733}
3734
3735static int octdigitp(int c)
3736{
3737 int ret = 0;
3738 switch(c) {
3739 case '0': case '1': case '2': case '3':
3740 case '4': case '5': case '6': case '7':
3741 ret = 1;
3742 break;
3743 }
3744 return ret;
3745}
3746static int octdigval(int c)
3747{
3748 int val = -1;
3749 if ((c >= '0') && (c <= '7')) {
3750 val = c - '0';
3751 }
3752 return val;
3753}
3754
3755static int letterp(int c)
3756{
3757 int ret = 0;
3758 switch(c) {
3759 case 'a': case 'b': case 'c': case 'd': case 'e':
3760 case 'f': case 'g': case 'h': case 'i': case 'j':
3761 case 'k': case 'l': case 'm': case 'n': case 'o':
3762 case 'p': case 'q': case 'r': case 's': case 't':
3763 case 'u': case 'v': case 'w': case 'x': case 'y':
3764 case 'z':
3765 case 'A': case 'B': case 'C': case 'D': case 'E':
3766 case 'F': case 'G': case 'H': case 'I': case 'J':
3767 case 'K': case 'L': case 'M': case 'N': case 'O':
3768 case 'P': case 'Q': case 'R': case 'S': case 'T':
3769 case 'U': case 'V': case 'W': case 'X': case 'Y':
3770 case 'Z':
3771 case '_':
3772 ret = 1;
3773 break;
3774 }
3775 return ret;
3776}
3777
Eric Biederman90089602004-05-28 14:11:54 +00003778static const char *identifier(const char *str, const char *end)
3779{
3780 if (letterp(*str)) {
3781 for(; str < end; str++) {
3782 int c;
3783 c = *str;
3784 if (!letterp(c) && !digitp(c)) {
3785 break;
3786 }
3787 }
3788 }
3789 return str;
3790}
3791
Eric Biedermanb138ac82003-04-22 18:44:01 +00003792static int char_value(struct compile_state *state,
3793 const signed char **strp, const signed char *end)
3794{
3795 const signed char *str;
3796 int c;
3797 str = *strp;
3798 c = *str++;
3799 if ((c == '\\') && (str < end)) {
3800 switch(*str) {
3801 case 'n': c = '\n'; str++; break;
3802 case 't': c = '\t'; str++; break;
3803 case 'v': c = '\v'; str++; break;
3804 case 'b': c = '\b'; str++; break;
3805 case 'r': c = '\r'; str++; break;
3806 case 'f': c = '\f'; str++; break;
3807 case 'a': c = '\a'; str++; break;
3808 case '\\': c = '\\'; str++; break;
3809 case '?': c = '?'; str++; break;
3810 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003811 case '"': c = '"'; str++; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003812 case 'x':
3813 c = 0;
3814 str++;
3815 while((str < end) && hexdigitp(*str)) {
3816 c <<= 4;
3817 c += hexdigval(*str);
3818 str++;
3819 }
3820 break;
3821 case '0': case '1': case '2': case '3':
3822 case '4': case '5': case '6': case '7':
3823 c = 0;
3824 while((str < end) && octdigitp(*str)) {
3825 c <<= 3;
3826 c += octdigval(*str);
3827 str++;
3828 }
3829 break;
3830 default:
3831 error(state, 0, "Invalid character constant");
3832 break;
3833 }
3834 }
3835 *strp = str;
3836 return c;
3837}
3838
Eric Biedermancb364952004-11-15 10:46:44 +00003839static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003840{
Eric Biedermancb364952004-11-15 10:46:44 +00003841 const char *end = file->buf + file->size;
3842 while(pos < end) {
3843 /* Lookup the character */
3844 int size = 1;
3845 int c = *pos;
3846 /* Is this a trigraph? */
3847 if (file->trigraphs &&
3848 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3849 {
3850 switch(pos[2]) {
3851 case '=': c = '#'; break;
3852 case '/': c = '\\'; break;
3853 case '\'': c = '^'; break;
3854 case '(': c = '['; break;
3855 case ')': c = ']'; break;
3856 case '!': c = '!'; break;
3857 case '<': c = '{'; break;
3858 case '>': c = '}'; break;
3859 case '-': c = '~'; break;
3860 }
3861 if (c != '?') {
3862 size = 3;
3863 }
3864 }
3865 /* Is this an escaped newline? */
3866 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003867 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003868 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003869 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003870 /* At the start of a line just eat it */
3871 if (pos == file->pos) {
3872 file->line++;
3873 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003874 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003875 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003876 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003877 }
3878 /* Do I need to ga any farther? */
3879 else if (index == 0) {
3880 break;
3881 }
3882 /* Process a normal character */
3883 else {
3884 pos += size;
3885 index -= 1;
3886 }
3887 }
3888 return pos;
3889}
3890
3891static int get_char(struct file_state *file, const char *pos)
3892{
3893 const char *end = file->buf + file->size;
3894 int c;
3895 c = -1;
3896 pos = next_char(file, pos, 0);
3897 if (pos < end) {
3898 /* Lookup the character */
3899 c = *pos;
3900 /* If it is a trigraph get the trigraph value */
3901 if (file->trigraphs &&
3902 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3903 {
3904 switch(pos[2]) {
3905 case '=': c = '#'; break;
3906 case '/': c = '\\'; break;
3907 case '\'': c = '^'; break;
3908 case '(': c = '['; break;
3909 case ')': c = ']'; break;
3910 case '!': c = '!'; break;
3911 case '<': c = '{'; break;
3912 case '>': c = '}'; break;
3913 case '-': c = '~'; break;
3914 }
3915 }
3916 }
3917 return c;
3918}
3919
3920static void eat_chars(struct file_state *file, const char *targ)
3921{
3922 const char *pos = file->pos;
3923 while(pos < targ) {
3924 /* Do we have a newline? */
3925 if (pos[0] == '\n') {
3926 file->line++;
3927 file->report_line++;
3928 file->line_start = pos + 1;
3929 }
3930 pos++;
3931 }
3932 file->pos = pos;
3933}
3934
3935
3936static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3937{
3938 size_t len;
3939 len = 0;
3940 while(src < end) {
3941 src = next_char(file, src, 1);
3942 len++;
3943 }
3944 return len;
3945}
3946
3947static void char_strcpy(char *dest,
3948 struct file_state *file, const char *src, const char *end)
3949{
3950 while(src < end) {
3951 int c;
3952 c = get_char(file, src);
3953 src = next_char(file, src, 1);
3954 *dest++ = c;
3955 }
3956}
3957
3958static char *char_strdup(struct file_state *file,
3959 const char *start, const char *end, const char *id)
3960{
3961 char *str;
3962 size_t str_len;
3963 str_len = char_strlen(file, start, end);
3964 str = xcmalloc(str_len + 1, id);
3965 char_strcpy(str, file, start, end);
3966 str[str_len] = '\0';
3967 return str;
3968}
3969
3970static const char *after_digits(struct file_state *file, const char *ptr)
3971{
3972 while(digitp(get_char(file, ptr))) {
3973 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003974 }
3975 return ptr;
3976}
3977
Eric Biedermancb364952004-11-15 10:46:44 +00003978static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003979{
Eric Biedermancb364952004-11-15 10:46:44 +00003980 while(octdigitp(get_char(file, ptr))) {
3981 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003982 }
3983 return ptr;
3984}
3985
Eric Biedermancb364952004-11-15 10:46:44 +00003986static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003987{
Eric Biedermancb364952004-11-15 10:46:44 +00003988 while(hexdigitp(get_char(file, ptr))) {
3989 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003990 }
3991 return ptr;
3992}
3993
Eric Biedermancb364952004-11-15 10:46:44 +00003994static const char *after_alnums(struct file_state *file, const char *ptr)
3995{
3996 int c;
3997 c = get_char(file, ptr);
3998 while(letterp(c) || digitp(c)) {
3999 ptr = next_char(file, ptr, 1);
4000 c = get_char(file, ptr);
4001 }
4002 return ptr;
4003}
4004
4005static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004006 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004007{
4008 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004009
Eric Biedermanb138ac82003-04-22 18:44:01 +00004010 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004011 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004012
4013 /* Store the copy in the token */
4014 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004015 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004016}
4017
4018static void raw_next_token(struct compile_state *state,
4019 struct file_state *file, struct token *tk)
4020{
4021 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004022 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004023 const char *tokp;
4024 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004025 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004026
Eric Biedermanb138ac82003-04-22 18:44:01 +00004027 tk->str_len = 0;
4028 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004029 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004030 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004031 c = get_char(file, tokp);
4032 tokp = next_char(file, tokp, 1);
4033 eat = 0;
4034 c1 = get_char(file, tokp);
4035 c2 = get_char(file, next_char(file, tokp, 1));
4036 c3 = get_char(file, next_char(file, tokp, 2));
4037
4038 /* The end of the file */
4039 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004040 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004042 /* Whitespace */
4043 else if (spacep(c)) {
4044 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004045 while (spacep(get_char(file, tokp))) {
4046 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004047 }
4048 }
4049 /* EOL Comments */
4050 else if ((c == '/') && (c1 == '/')) {
4051 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004052 tokp = next_char(file, tokp, 1);
4053 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004054 /* Advance to the next character only after we verify
4055 * the current character is not a newline.
4056 * EOL is special to the preprocessor so we don't
4057 * want to loose any.
4058 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004059 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004060 break;
4061 }
Eric Biederman57183382006-12-02 16:48:48 +00004062 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004063 }
4064 }
4065 /* Comments */
4066 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004067 tokp = next_char(file, tokp, 2);
4068 c = c2;
4069 while((c1 = get_char(file, tokp)) != -1) {
4070 tokp = next_char(file, tokp, 1);
4071 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004072 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004073 break;
4074 }
Eric Biedermancb364952004-11-15 10:46:44 +00004075 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004076 }
4077 if (tok == TOK_UNKNOWN) {
4078 error(state, 0, "unterminated comment");
4079 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004080 }
4081 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004082 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
4083 int wchar, multiline;
4084
Eric Biedermanb138ac82003-04-22 18:44:01 +00004085 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004086 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004087 if (c == 'L') {
4088 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004089 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004090 }
Eric Biedermancb364952004-11-15 10:46:44 +00004091 while((c = get_char(file, tokp)) != -1) {
4092 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004093 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004094 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004095 }
Eric Biedermancb364952004-11-15 10:46:44 +00004096 else if (c == '\\') {
4097 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004098 }
4099 else if (c == '"') {
4100 tok = TOK_LIT_STRING;
4101 break;
4102 }
4103 }
4104 if (tok == TOK_UNKNOWN) {
4105 error(state, 0, "unterminated string constant");
4106 }
Eric Biedermancb364952004-11-15 10:46:44 +00004107 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004108 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004109 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004110
4111 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004112 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004113 }
4114 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004115 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
4116 int wchar, multiline;
4117
Eric Biedermanb138ac82003-04-22 18:44:01 +00004118 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004119 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004120 if (c == 'L') {
4121 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004122 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004123 }
Eric Biedermancb364952004-11-15 10:46:44 +00004124 while((c = get_char(file, tokp)) != -1) {
4125 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004126 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004127 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004128 }
Eric Biedermancb364952004-11-15 10:46:44 +00004129 else if (c == '\\') {
4130 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004131 }
4132 else if (c == '\'') {
4133 tok = TOK_LIT_CHAR;
4134 break;
4135 }
4136 }
4137 if (tok == TOK_UNKNOWN) {
4138 error(state, 0, "unterminated character constant");
4139 }
Eric Biedermancb364952004-11-15 10:46:44 +00004140 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004141 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004142 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004143
4144 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004145 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004146 }
4147 /* integer and floating constants
4148 * Integer Constants
4149 * {digits}
4150 * 0[Xx]{hexdigits}
4151 * 0{octdigit}+
4152 *
4153 * Floating constants
4154 * {digits}.{digits}[Ee][+-]?{digits}
4155 * {digits}.{digits}
4156 * {digits}[Ee][+-]?{digits}
4157 * .{digits}[Ee][+-]?{digits}
4158 * .{digits}
4159 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004160 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004161 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004162 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004163 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004164 is_float = 0;
4165 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004166 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004167 }
4168 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004169 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004170 }
Eric Biedermancb364952004-11-15 10:46:44 +00004171 cn = get_char(file, next);
4172 if (cn == '.') {
4173 next = next_char(file, next, 1);
4174 next = after_digits(file, next);
4175 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004176 }
Eric Biedermancb364952004-11-15 10:46:44 +00004177 cn = get_char(file, next);
4178 if ((cn == 'e') || (cn == 'E')) {
4179 const char *new;
4180 next = next_char(file, next, 1);
4181 cn = get_char(file, next);
4182 if ((cn == '+') || (cn == '-')) {
4183 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004184 }
Eric Biedermancb364952004-11-15 10:46:44 +00004185 new = after_digits(file, next);
4186 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004187 next = new;
4188 }
4189 if (is_float) {
4190 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004191 cn = get_char(file, next);
4192 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4193 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004194 }
4195 }
4196 if (!is_float && digitp(c)) {
4197 tok = TOK_LIT_INT;
4198 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004199 next = next_char(file, tokp, 1);
4200 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004201 }
4202 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004203 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004204 }
4205 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004206 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004207 }
4208 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004209 cn = get_char(file, next);
4210 if ((cn == 'u') || (cn == 'U')) {
4211 next = next_char(file, next, 1);
4212 cn = get_char(file, next);
4213 if ((cn == 'l') || (cn == 'L')) {
4214 next = next_char(file, next, 1);
4215 cn = get_char(file, next);
4216 }
4217 if ((cn == 'l') || (cn == 'L')) {
4218 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004219 }
4220 }
Eric Biedermancb364952004-11-15 10:46:44 +00004221 else if ((cn == 'l') || (cn == 'L')) {
4222 next = next_char(file, next, 1);
4223 cn = get_char(file, next);
4224 if ((cn == 'l') || (cn == 'L')) {
4225 next = next_char(file, next, 1);
4226 cn = get_char(file, next);
4227 }
4228 if ((cn == 'u') || (cn == 'U')) {
4229 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004230 }
4231 }
4232 }
Eric Biedermancb364952004-11-15 10:46:44 +00004233 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004234
4235 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004236 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004237 }
4238 /* identifiers */
4239 else if (letterp(c)) {
4240 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004241
4242 /* Find and save the identifier string */
4243 tokp = after_alnums(file, tokp);
4244 save_string(file, tk, token, tokp, "identifier");
4245
4246 /* Look up to see which identifier it is */
4247 tk->ident = lookup(state, tk->val.str, tk->str_len);
4248
4249 /* Free the identifier string */
4250 tk->str_len = 0;
4251 xfree(tk->val.str);
4252
Eric Biederman90089602004-05-28 14:11:54 +00004253 /* See if this identifier can be macro expanded */
4254 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004255 c = get_char(file, tokp);
4256 if (c == '$') {
4257 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004258 tk->val.notmacro = 1;
4259 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004260 }
4261 /* C99 alternate macro characters */
4262 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004263 eat += 3;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004264 tok = TOK_CONCATENATE;
4265 }
Eric Biedermancb364952004-11-15 10:46:44 +00004266 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4267 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4268 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4269 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4270 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4271 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4272 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4273 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4274 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4275 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4276 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4277 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4278 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4279 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4280 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4281 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4282 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4283 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4284 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4285 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4286 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4287 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4288 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4289 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4290 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4291 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4292 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4293 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004294 else if (c == ';') { tok = TOK_SEMI; }
4295 else if (c == '{') { tok = TOK_LBRACE; }
4296 else if (c == '}') { tok = TOK_RBRACE; }
4297 else if (c == ',') { tok = TOK_COMMA; }
4298 else if (c == '=') { tok = TOK_EQ; }
4299 else if (c == ':') { tok = TOK_COLON; }
4300 else if (c == '[') { tok = TOK_LBRACKET; }
4301 else if (c == ']') { tok = TOK_RBRACKET; }
4302 else if (c == '(') { tok = TOK_LPAREN; }
4303 else if (c == ')') { tok = TOK_RPAREN; }
4304 else if (c == '*') { tok = TOK_STAR; }
4305 else if (c == '>') { tok = TOK_MORE; }
4306 else if (c == '<') { tok = TOK_LESS; }
4307 else if (c == '?') { tok = TOK_QUEST; }
4308 else if (c == '|') { tok = TOK_OR; }
4309 else if (c == '&') { tok = TOK_AND; }
4310 else if (c == '^') { tok = TOK_XOR; }
4311 else if (c == '+') { tok = TOK_PLUS; }
4312 else if (c == '-') { tok = TOK_MINUS; }
4313 else if (c == '/') { tok = TOK_DIV; }
4314 else if (c == '%') { tok = TOK_MOD; }
4315 else if (c == '!') { tok = TOK_BANG; }
4316 else if (c == '.') { tok = TOK_DOT; }
4317 else if (c == '~') { tok = TOK_TILDE; }
4318 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004319 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004320
Eric Biedermancb364952004-11-15 10:46:44 +00004321 tokp = next_char(file, tokp, eat);
4322 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004323 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004324 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004325}
4326
4327static void check_tok(struct compile_state *state, struct token *tk, int tok)
4328{
4329 if (tk->tok != tok) {
4330 const char *name1, *name2;
4331 name1 = tokens[tk->tok];
4332 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004333 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004334 name2 = tk->ident->name;
4335 }
4336 error(state, 0, "\tfound %s %s expected %s",
4337 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004338 }
4339}
4340
Eric Biederman90089602004-05-28 14:11:54 +00004341struct macro_arg_value {
4342 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004343 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004344 size_t len;
4345};
4346static struct macro_arg_value *read_macro_args(
4347 struct compile_state *state, struct macro *macro,
4348 struct file_state *file, struct token *tk)
4349{
4350 struct macro_arg_value *argv;
4351 struct macro_arg *arg;
4352 int paren_depth;
4353 int i;
4354
4355 if (macro->argc == 0) {
4356 do {
4357 raw_next_token(state, file, tk);
4358 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004359 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004360 }
4361 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4362 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4363 argv[i].value = 0;
4364 argv[i].len = 0;
4365 argv[i].ident = arg->ident;
4366 }
4367 paren_depth = 0;
4368 i = 0;
4369
4370 for(;;) {
4371 const char *start;
4372 size_t len;
4373 start = file->pos;
4374 raw_next_token(state, file, tk);
4375
4376 if (!paren_depth && (tk->tok == TOK_COMMA) &&
4377 (argv[i].ident != state->i___VA_ARGS__))
4378 {
4379 i++;
4380 if (i >= macro->argc) {
4381 error(state, 0, "too many args to %s\n",
4382 macro->ident->name);
4383 }
4384 continue;
4385 }
4386
4387 if (tk->tok == TOK_LPAREN) {
4388 paren_depth++;
4389 }
4390
4391 if (tk->tok == TOK_RPAREN) {
4392 if (paren_depth == 0) {
4393 break;
4394 }
4395 paren_depth--;
4396 }
4397 if (tk->tok == TOK_EOF) {
4398 error(state, 0, "End of file encountered while parsing macro arguments");
4399 }
Eric Biedermancb364952004-11-15 10:46:44 +00004400
4401 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004402 argv[i].value = xrealloc(
4403 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004404 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004405 argv[i].len += len;
4406 }
4407 if (i != macro->argc -1) {
4408 error(state, 0, "missing %s arg %d\n",
4409 macro->ident->name, i +2);
4410 }
4411 return argv;
4412}
4413
4414
4415static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4416{
4417 int i;
4418 for(i = 0; i < macro->argc; i++) {
4419 xfree(argv[i].value);
4420 }
4421 xfree(argv);
4422}
4423
4424struct macro_buf {
4425 char *str;
4426 size_t len, pos;
4427};
4428
Eric Biedermancb364952004-11-15 10:46:44 +00004429static void grow_macro_buf(struct compile_state *state,
4430 const char *id, struct macro_buf *buf,
4431 size_t grow)
4432{
4433 if ((buf->pos + grow) >= buf->len) {
4434 buf->str = xrealloc(buf->str, buf->len + grow, id);
4435 buf->len += grow;
4436 }
4437}
4438
Eric Biederman90089602004-05-28 14:11:54 +00004439static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004440 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004441 const char *fstart, size_t flen)
4442{
Eric Biedermancb364952004-11-15 10:46:44 +00004443 grow_macro_buf(state, id, buf, flen);
4444 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004445#if 0
4446 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4447 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004448 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004449#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004450 buf->pos += flen;
4451}
4452
4453
4454static void append_macro_chars(struct compile_state *state,
4455 const char *id, struct macro_buf *buf,
4456 struct file_state *file, const char *start, const char *end)
4457{
4458 size_t flen;
4459 flen = char_strlen(file, start, end);
4460 grow_macro_buf(state, id, buf, flen);
4461 char_strcpy(buf->str + buf->pos, file, start, end);
4462#if 0
4463 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4464 buf->pos, buf->pos, buf->str,
4465 flen, flen, buf->str + buf->pos);
4466#endif
Eric Biederman90089602004-05-28 14:11:54 +00004467 buf->pos += flen;
4468}
4469
4470static int compile_macro(struct compile_state *state,
4471 struct file_state **filep, struct token *tk);
4472
4473static void macro_expand_args(struct compile_state *state,
4474 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4475{
Eric Biedermancb364952004-11-15 10:46:44 +00004476 int i;
Eric Biederman90089602004-05-28 14:11:54 +00004477
4478 for(i = 0; i < macro->argc; i++) {
4479 struct file_state fmacro, *file;
4480 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004481
Eric Biedermancb364952004-11-15 10:46:44 +00004482 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004483 fmacro.basename = argv[i].ident->name;
4484 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004485 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004486 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004487 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004488 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004489 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004490 fmacro.report_line = 1;
4491 fmacro.report_name = fmacro.basename;
4492 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004493 fmacro.macro = 1;
4494 fmacro.trigraphs = 0;
4495 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004496
4497 buf.len = argv[i].len;
4498 buf.str = xmalloc(buf.len, argv[i].ident->name);
4499 buf.pos = 0;
4500
4501 file = &fmacro;
4502 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004503 raw_next_token(state, file, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004504
Eric Biedermancb364952004-11-15 10:46:44 +00004505 /* If we have recursed into another macro body
4506 * get out of it.
4507 */
Eric Biederman90089602004-05-28 14:11:54 +00004508 if (tk->tok == TOK_EOF) {
4509 struct file_state *old;
4510 old = file;
4511 file = file->prev;
4512 if (!file) {
4513 break;
4514 }
4515 /* old->basename is used keep it */
4516 xfree(old->dirname);
4517 xfree(old->buf);
4518 xfree(old);
4519 continue;
4520 }
4521 else if (tk->ident && tk->ident->sym_define) {
4522 if (compile_macro(state, &file, tk)) {
4523 continue;
4524 }
4525 }
4526
Eric Biedermancb364952004-11-15 10:46:44 +00004527 append_macro_chars(state, macro->ident->name, &buf,
4528 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004529 }
4530
4531 xfree(argv[i].value);
4532 argv[i].value = buf.str;
4533 argv[i].len = buf.pos;
4534 }
4535 return;
4536}
4537
4538static void expand_macro(struct compile_state *state,
4539 struct macro *macro, struct macro_buf *buf,
4540 struct macro_arg_value *argv, struct token *tk)
4541{
4542 struct file_state fmacro;
4543 const char space[] = " ";
4544 const char *fstart;
4545 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004546 int i, j;
4547
4548 /* Place the macro body in a dummy file */
4549 fmacro.prev = 0;
4550 fmacro.basename = macro->ident->name;
4551 fmacro.dirname = "";
4552 fmacro.buf = macro->buf;
4553 fmacro.size = macro->buf_len;
4554 fmacro.pos = fmacro.buf;
4555 fmacro.line = 1;
4556 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004557 fmacro.report_line = 1;
4558 fmacro.report_name = fmacro.basename;
4559 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004560 fmacro.macro = 1;
4561 fmacro.trigraphs = 0;
4562 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004563
Eric Biedermancb364952004-11-15 10:46:44 +00004564 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004565 buf->len = macro->buf_len + 3;
4566 buf->str = xmalloc(buf->len, macro->ident->name);
4567 buf->pos = 0;
4568
4569 fstart = fmacro.pos;
4570 raw_next_token(state, &fmacro, tk);
4571 while(tk->tok != TOK_EOF) {
4572 flen = fmacro.pos - fstart;
4573 switch(tk->tok) {
4574 case TOK_IDENT:
4575 for(i = 0; i < macro->argc; i++) {
4576 if (argv[i].ident == tk->ident) {
4577 break;
4578 }
4579 }
4580 if (i >= macro->argc) {
4581 break;
4582 }
4583 /* Substitute macro parameter */
4584 fstart = argv[i].value;
4585 flen = argv[i].len;
4586 break;
4587 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004588 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004589 break;
4590 }
4591 do {
4592 raw_next_token(state, &fmacro, tk);
4593 } while(tk->tok == TOK_SPACE);
4594 check_tok(state, tk, TOK_IDENT);
4595 for(i = 0; i < macro->argc; i++) {
4596 if (argv[i].ident == tk->ident) {
4597 break;
4598 }
4599 }
4600 if (i >= macro->argc) {
4601 error(state, 0, "parameter `%s' not found",
4602 tk->ident->name);
4603 }
4604 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004605 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004606 for(j = 0; j < argv[i].len; j++) {
4607 char *str = argv[i].value + j;
4608 size_t len = 1;
4609 if (*str == '\\') {
4610 str = "\\";
4611 len = 2;
4612 }
4613 else if (*str == '"') {
4614 str = "\\\"";
4615 len = 2;
4616 }
Eric Biedermancb364952004-11-15 10:46:44 +00004617 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004618 }
Eric Biedermancb364952004-11-15 10:46:44 +00004619 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004620 fstart = 0;
4621 flen = 0;
4622 break;
4623 case TOK_CONCATENATE:
4624 /* Concatenate tokens */
4625 /* Delete the previous whitespace token */
4626 if (buf->str[buf->pos - 1] == ' ') {
4627 buf->pos -= 1;
4628 }
4629 /* Skip the next sequence of whitspace tokens */
4630 do {
4631 fstart = fmacro.pos;
4632 raw_next_token(state, &fmacro, tk);
4633 } while(tk->tok == TOK_SPACE);
4634 /* Restart at the top of the loop.
4635 * I need to process the non white space token.
4636 */
4637 continue;
4638 break;
4639 case TOK_SPACE:
4640 /* Collapse multiple spaces into one */
4641 if (buf->str[buf->pos - 1] != ' ') {
4642 fstart = space;
4643 flen = 1;
4644 } else {
4645 fstart = 0;
4646 flen = 0;
4647 }
4648 break;
4649 default:
4650 break;
4651 }
4652
Eric Biedermancb364952004-11-15 10:46:44 +00004653 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004654
4655 fstart = fmacro.pos;
4656 raw_next_token(state, &fmacro, tk);
4657 }
4658}
4659
4660static void tag_macro_name(struct compile_state *state,
4661 struct macro *macro, struct macro_buf *buf,
4662 struct token *tk)
4663{
4664 /* Guard all instances of the macro name in the replacement
4665 * text from further macro expansion.
4666 */
4667 struct file_state fmacro;
4668 const char *fstart;
4669 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004670
4671 /* Put the old macro expansion buffer in a file */
4672 fmacro.prev = 0;
4673 fmacro.basename = macro->ident->name;
4674 fmacro.dirname = "";
4675 fmacro.buf = buf->str;
4676 fmacro.size = buf->pos;
4677 fmacro.pos = fmacro.buf;
4678 fmacro.line = 1;
4679 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004680 fmacro.report_line = 1;
4681 fmacro.report_name = fmacro.basename;
4682 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004683 fmacro.macro = 1;
4684 fmacro.trigraphs = 0;
4685 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004686
Eric Biedermancb364952004-11-15 10:46:44 +00004687 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004688 buf->len = macro->buf_len + 3;
4689 buf->str = xmalloc(buf->len, macro->ident->name);
4690 buf->pos = 0;
4691
4692 fstart = fmacro.pos;
4693 raw_next_token(state, &fmacro, tk);
4694 while(tk->tok != TOK_EOF) {
4695 flen = fmacro.pos - fstart;
4696 if ((tk->tok == TOK_IDENT) &&
4697 (tk->ident == macro->ident) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004698 (tk->val.notmacro == 0))
4699 {
4700 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004701 fstart = "$";
4702 flen = 1;
4703 }
4704
Eric Biedermancb364952004-11-15 10:46:44 +00004705 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004706
4707 fstart = fmacro.pos;
4708 raw_next_token(state, &fmacro, tk);
4709 }
4710 xfree(fmacro.buf);
4711}
Eric Biedermancb364952004-11-15 10:46:44 +00004712
Eric Biederman90089602004-05-28 14:11:54 +00004713static int compile_macro(struct compile_state *state,
4714 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004715{
4716 struct file_state *file;
4717 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004718 struct macro *macro;
4719 struct macro_arg_value *argv;
4720 struct macro_buf buf;
4721
4722#if 0
4723 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4724#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004725 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004726 macro = ident->sym_define;
4727
4728 /* If this token comes from a macro expansion ignore it */
4729 if (tk->val.notmacro) {
4730 return 0;
4731 }
4732 /* If I am a function like macro and the identifier is not followed
4733 * by a left parenthesis, do nothing.
4734 */
Eric Biedermancb364952004-11-15 10:46:44 +00004735 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004736 return 0;
4737 }
4738
4739 /* Read in the macro arguments */
4740 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004741 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004742 raw_next_token(state, *filep, tk);
4743 check_tok(state, tk, TOK_LPAREN);
4744
4745 argv = read_macro_args(state, macro, *filep, tk);
4746
4747 check_tok(state, tk, TOK_RPAREN);
4748 }
4749 /* Macro expand the macro arguments */
4750 macro_expand_args(state, macro, argv, tk);
4751
4752 buf.str = 0;
4753 buf.len = 0;
4754 buf.pos = 0;
4755 if (ident == state->i___FILE__) {
4756 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4757 buf.str = xmalloc(buf.len, ident->name);
4758 sprintf(buf.str, "\"%s\"", state->file->basename);
4759 buf.pos = strlen(buf.str);
4760 }
4761 else if (ident == state->i___LINE__) {
4762 buf.len = 30;
4763 buf.str = xmalloc(buf.len, ident->name);
4764 sprintf(buf.str, "%d", state->file->line);
4765 buf.pos = strlen(buf.str);
4766 }
4767 else {
4768 expand_macro(state, macro, &buf, argv, tk);
4769 }
4770 /* Tag the macro name with a $ so it will no longer
4771 * be regonized as a canidate for macro expansion.
4772 */
4773 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004774
4775#if 0
4776 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4777 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4778#endif
4779
4780 free_macro_args(macro, argv);
4781
Eric Biedermanb138ac82003-04-22 18:44:01 +00004782 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004783 file->prev = *filep;
4784 file->basename = xstrdup(ident->name);
4785 file->dirname = xstrdup("");
4786 file->buf = buf.str;
4787 file->size = buf.pos;
4788 file->pos = file->buf;
4789 file->line = 1;
4790 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004791 file->report_line = 1;
4792 file->report_name = file->basename;
4793 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004794 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004795 file->trigraphs = 0;
4796 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004797 *filep = file;
4798 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004799}
4800
Eric Biederman90089602004-05-28 14:11:54 +00004801static void eat_tokens(struct compile_state *state, int targ_tok)
4802{
4803 if (state->eat_depth > 0) {
4804 internal_error(state, 0, "Already eating...");
4805 }
4806 state->eat_depth = state->if_depth;
4807 state->eat_targ = targ_tok;
4808}
4809static int if_eat(struct compile_state *state)
4810{
4811 return state->eat_depth > 0;
4812}
4813static int if_value(struct compile_state *state)
4814{
4815 int index, offset;
4816 index = state->if_depth / CHAR_BIT;
4817 offset = state->if_depth % CHAR_BIT;
4818 return !!(state->if_bytes[index] & (1 << (offset)));
4819}
4820static void set_if_value(struct compile_state *state, int value)
4821{
4822 int index, offset;
4823 index = state->if_depth / CHAR_BIT;
4824 offset = state->if_depth % CHAR_BIT;
4825
4826 state->if_bytes[index] &= ~(1 << offset);
4827 if (value) {
4828 state->if_bytes[index] |= (1 << offset);
4829 }
4830}
4831static void in_if(struct compile_state *state, const char *name)
4832{
4833 if (state->if_depth <= 0) {
4834 error(state, 0, "%s without #if", name);
4835 }
4836}
4837static void enter_if(struct compile_state *state)
4838{
4839 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004840 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004841 error(state, 0, "#if depth too great");
4842 }
4843}
4844static void reenter_if(struct compile_state *state, const char *name)
4845{
4846 in_if(state, name);
4847 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004848 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004849 state->eat_depth = 0;
4850 state->eat_targ = 0;
4851 }
4852}
4853static void enter_else(struct compile_state *state, const char *name)
4854{
4855 in_if(state, name);
4856 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004857 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004858 state->eat_depth = 0;
4859 state->eat_targ = 0;
4860 }
4861}
4862static void exit_if(struct compile_state *state, const char *name)
4863{
4864 in_if(state, name);
4865 if (state->eat_depth == state->if_depth) {
4866 state->eat_depth = 0;
4867 state->eat_targ = 0;
4868 }
4869 state->if_depth -= 1;
4870}
4871
Eric Biedermancb364952004-11-15 10:46:44 +00004872static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004873{
4874 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004875 int rescan;
4876
Eric Biedermancb364952004-11-15 10:46:44 +00004877 file = state->file;
4878 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004879 do {
4880 rescan = 0;
4881 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004882 /* Exit out of an include directive or macro call */
4883 if ((tk->tok == TOK_EOF) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004884 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004885 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004886 state->file = file->prev;
4887 /* file->basename is used keep it */
4888 xfree(file->dirname);
4889 xfree(file->buf);
4890 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004891 file = 0;
4892 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004893 rescan = 1;
4894 }
Eric Biederman41203d92004-11-08 09:31:09 +00004895 } while(rescan);
4896}
4897
Eric Biedermancb364952004-11-15 10:46:44 +00004898static void pp_token(struct compile_state *state, struct token *tk)
4899{
4900 struct file_state *file;
4901 int rescan;
4902
4903 raw_token(state, tk);
4904 do {
4905 rescan = 0;
4906 file = state->file;
4907 if (tk->tok == TOK_SPACE) {
4908 raw_token(state, tk);
4909 rescan = 1;
4910 }
4911 else if (tk->tok == TOK_IDENT) {
4912 if (state->token_base == 0) {
4913 ident_to_keyword(state, tk);
4914 } else {
4915 ident_to_macro(state, tk);
4916 }
4917 }
4918 } while(rescan);
4919}
4920
Eric Biederman41203d92004-11-08 09:31:09 +00004921static void preprocess(struct compile_state *state, struct token *tk);
4922
4923static void token(struct compile_state *state, struct token *tk)
4924{
4925 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004926 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004927 do {
4928 rescan = 0;
4929 /* Process a macro directive */
4930 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004931 /* Only match preprocessor directives at the start of a line */
4932 const char *ptr;
4933 ptr = state->file->line_start;
4934 while((ptr < tk->pos)
4935 && spacep(get_char(state->file, ptr)))
4936 {
4937 ptr = next_char(state->file, ptr, 1);
4938 }
4939 if (ptr == tk->pos) {
4940 preprocess(state, tk);
4941 rescan = 1;
4942 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004943 }
Eric Biederman41203d92004-11-08 09:31:09 +00004944 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004945 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004946 rescan = compile_macro(state, &state->file, tk);
4947 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004948 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004949 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004950 }
Eric Biedermancb364952004-11-15 10:46:44 +00004951 /* Eat tokens disabled by the preprocessor
4952 * (Unless we are parsing a preprocessor directive
4953 */
Eric Biedermana649a412004-11-09 00:35:39 +00004954 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004955 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004956 rescan = 1;
4957 }
Eric Biedermana649a412004-11-09 00:35:39 +00004958 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004959 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004960 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004961 rescan = 1;
4962 }
Eric Biedermancb364952004-11-15 10:46:44 +00004963 /* Error on unknown tokens */
4964 else if (tk->tok == TOK_UNKNOWN) {
4965 error(state, 0, "unknown token");
4966 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004967 } while(rescan);
4968}
4969
Eric Biederman41203d92004-11-08 09:31:09 +00004970
4971static inline struct token *get_token(struct compile_state *state, int offset)
4972{
4973 int index;
4974 index = state->token_base + offset;
4975 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4976 internal_error(state, 0, "token array to small");
4977 }
4978 return &state->token[index];
4979}
4980
4981static struct token *do_eat_token(struct compile_state *state, int tok)
4982{
4983 struct token *tk;
4984 int i;
4985 check_tok(state, get_token(state, 1), tok);
4986
4987 /* Free the old token value */
4988 tk = get_token(state, 0);
4989 if (tk->str_len) {
4990 memset((void *)tk->val.str, -1, tk->str_len);
4991 xfree(tk->val.str);
4992 }
4993 /* Overwrite the old token with newer tokens */
4994 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4995 state->token[i] = state->token[i + 1];
4996 }
4997 /* Clear the last token */
4998 memset(&state->token[i], 0, sizeof(state->token[i]));
4999 state->token[i].tok = -1;
5000
5001 /* Return the token */
5002 return tk;
5003}
5004
Eric Biedermancb364952004-11-15 10:46:44 +00005005static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005006{
5007 struct token *tk1;
5008 tk1 = get_token(state, 1);
5009 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005010 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005011 }
5012 return tk1->tok;
5013}
5014
Eric Biedermancb364952004-11-15 10:46:44 +00005015static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005016{
Eric Biedermancb364952004-11-15 10:46:44 +00005017 raw_peek(state);
5018 return do_eat_token(state, tok);
5019}
5020
5021static int pp_peek(struct compile_state *state)
5022{
5023 struct token *tk1;
5024 tk1 = get_token(state, 1);
5025 if (tk1->tok == -1) {
5026 pp_token(state, tk1);
5027 }
5028 return tk1->tok;
5029}
5030
5031static struct token *pp_eat(struct compile_state *state, int tok)
5032{
5033 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005034 return do_eat_token(state, tok);
5035}
5036
Eric Biedermanb138ac82003-04-22 18:44:01 +00005037static int peek(struct compile_state *state)
5038{
Eric Biederman41203d92004-11-08 09:31:09 +00005039 struct token *tk1;
5040 tk1 = get_token(state, 1);
5041 if (tk1->tok == -1) {
5042 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005043 }
Eric Biederman41203d92004-11-08 09:31:09 +00005044 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005045}
5046
5047static int peek2(struct compile_state *state)
5048{
Eric Biederman41203d92004-11-08 09:31:09 +00005049 struct token *tk1, *tk2;
5050 tk1 = get_token(state, 1);
5051 tk2 = get_token(state, 2);
5052 if (tk1->tok == -1) {
5053 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005054 }
Eric Biederman41203d92004-11-08 09:31:09 +00005055 if (tk2->tok == -1) {
5056 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005057 }
Eric Biederman41203d92004-11-08 09:31:09 +00005058 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005059}
5060
Eric Biederman41203d92004-11-08 09:31:09 +00005061static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005062{
Eric Biederman90089602004-05-28 14:11:54 +00005063 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005064 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005065}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005066
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005067static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005068{
Eric Biederman90089602004-05-28 14:11:54 +00005069 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005070 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005071 int subdir_len;
5072 struct file_state *file;
5073 char *basename;
5074 file = xmalloc(sizeof(*file), "file_state");
5075
5076 base = strrchr(filename, '/');
5077 subdir = filename;
5078 if (base != 0) {
5079 subdir_len = base - filename;
5080 base++;
5081 }
5082 else {
5083 base = filename;
5084 subdir_len = 0;
5085 }
5086 basename = xmalloc(strlen(base) +1, "basename");
5087 strcpy(basename, base);
5088 file->basename = basename;
5089
5090 if (getcwd(cwd, sizeof(cwd)) == 0) {
5091 die("cwd buffer to small");
5092 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005093 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005094 file->dirname = xmalloc(subdir_len + 1, "dirname");
5095 memcpy(file->dirname, subdir, subdir_len);
5096 file->dirname[subdir_len] = '\0';
5097 }
5098 else {
Eric Biederman90089602004-05-28 14:11:54 +00005099 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005100 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005101 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005102 /* Find the appropriate directory... */
5103 dir = 0;
5104 if (!state->file && exists(cwd, filename)) {
5105 dir = cwd;
5106 }
5107 if (local && state->file && exists(state->file->dirname, filename)) {
5108 dir = state->file->dirname;
5109 }
Eric Biederman90089602004-05-28 14:11:54 +00005110 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005111 if (exists(*path, filename)) {
5112 dir = *path;
5113 }
5114 }
5115 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005116 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005117 }
5118 dirlen = strlen(dir);
5119 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5120 memcpy(file->dirname, dir, dirlen);
5121 file->dirname[dirlen] = '/';
5122 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5123 file->dirname[dirlen + 1 + subdir_len] = '\0';
5124 }
5125 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005126
5127 file->pos = file->buf;
5128 file->line_start = file->pos;
5129 file->line = 1;
5130
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005131 file->report_line = 1;
5132 file->report_name = file->basename;
5133 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005134 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005135 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5136 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005137
Eric Biedermanb138ac82003-04-22 18:44:01 +00005138 file->prev = state->file;
5139 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005140}
5141
Eric Biederman41203d92004-11-08 09:31:09 +00005142static struct triple *constant_expr(struct compile_state *state);
5143static void integral(struct compile_state *state, struct triple *def);
5144
5145static int mcexpr(struct compile_state *state)
5146{
5147 struct triple *cvalue;
5148 cvalue = constant_expr(state);
5149 integral(state, cvalue);
5150 if (cvalue->op != OP_INTCONST) {
5151 error(state, 0, "integer constant expected");
5152 }
5153 return cvalue->u.cval != 0;
5154}
5155
5156static void preprocess(struct compile_state *state, struct token *current_token)
5157{
5158 /* Doing much more with the preprocessor would require
5159 * a parser and a major restructuring.
5160 * Postpone that for later.
5161 */
Eric Biederman41203d92004-11-08 09:31:09 +00005162 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005163 int tok;
5164
Eric Biedermancb364952004-11-15 10:46:44 +00005165 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005166
5167 old_token_base = state->token_base;
5168 state->token_base = current_token - state->token;
5169
Eric Biedermancb364952004-11-15 10:46:44 +00005170 tok = pp_peek(state);
5171 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005172 case TOK_LIT_INT:
5173 {
5174 struct token *tk;
5175 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005176 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005177 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005178 /* I have a preprocessor line marker parse it */
5179 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005180 const char *token, *base;
5181 char *name, *dir;
5182 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005183 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005184 name = xmalloc(tk->str_len, "report_name");
5185 token = tk->val.str + 1;
5186 base = strrchr(token, '/');
5187 name_len = tk->str_len -2;
5188 if (base != 0) {
5189 dir_len = base - token;
5190 base++;
5191 name_len -= base - token;
5192 } else {
5193 dir_len = 0;
5194 base = token;
5195 }
5196 memcpy(name, base, name_len);
5197 name[name_len] = '\0';
5198 dir = xmalloc(dir_len + 1, "report_dir");
5199 memcpy(dir, token, dir_len);
5200 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005201 state->file->report_line = override_line - 1;
5202 state->file->report_name = name;
5203 state->file->report_dir = dir;
5204 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005205 }
5206 break;
5207 }
5208 case TOK_MLINE:
5209 {
5210 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005211 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005212 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005213 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5214 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005215 const char *token, *base;
5216 char *name, *dir;
5217 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005218 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005219 name = xmalloc(tk->str_len, "report_name");
5220 token = tk->val.str + 1;
5221 base = strrchr(token, '/');
5222 name_len = tk->str_len - 2;
5223 if (base != 0) {
5224 dir_len = base - token;
5225 base++;
5226 name_len -= base - token;
5227 } else {
5228 dir_len = 0;
5229 base = token;
5230 }
5231 memcpy(name, base, name_len);
5232 name[name_len] = '\0';
5233 dir = xmalloc(dir_len + 1, "report_dir");
5234 memcpy(dir, token, dir_len);
5235 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005236 state->file->report_name = name;
5237 state->file->report_dir = dir;
5238 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005239 }
5240 break;
5241 }
5242 case TOK_MUNDEF:
5243 {
5244 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005245 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005246 if (if_eat(state)) /* quit early when #if'd out */
5247 break;
5248
Eric Biedermancb364952004-11-15 10:46:44 +00005249 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005250
5251 undef_macro(state, ident);
5252 break;
5253 }
5254 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005255 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005256 if (if_eat(state)) /* quit early when #if'd out */
5257 break;
5258 warning(state, 0, "Ignoring pragma");
5259 break;
5260 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005261 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005262 reenter_if(state, "#elif");
5263 if (if_eat(state)) /* quit early when #if'd out */
5264 break;
5265 /* If the #if was taken the #elif just disables the following code */
5266 if (if_value(state)) {
5267 eat_tokens(state, TOK_MENDIF);
5268 }
5269 /* If the previous #if was not taken see if the #elif enables the
5270 * trailing code.
5271 */
5272 else {
5273 set_if_value(state, mcexpr(state));
5274 if (!if_value(state)) {
5275 eat_tokens(state, TOK_MELSE);
5276 }
5277 }
5278 break;
5279 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005280 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005281 enter_if(state);
5282 if (if_eat(state)) /* quit early when #if'd out */
5283 break;
5284 set_if_value(state, mcexpr(state));
5285 if (!if_value(state)) {
5286 eat_tokens(state, TOK_MELSE);
5287 }
5288 break;
5289 case TOK_MIFNDEF:
5290 {
5291 struct hash_entry *ident;
5292
Eric Biedermancb364952004-11-15 10:46:44 +00005293 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005294 enter_if(state);
5295 if (if_eat(state)) /* quit early when #if'd out */
5296 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005297 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005298 set_if_value(state, ident->sym_define == 0);
5299 if (!if_value(state)) {
5300 eat_tokens(state, TOK_MELSE);
5301 }
5302 break;
5303 }
5304 case TOK_MIFDEF:
5305 {
5306 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005307 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005308 enter_if(state);
5309 if (if_eat(state)) /* quit early when #if'd out */
5310 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005311 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005312 set_if_value(state, ident->sym_define != 0);
5313 if (!if_value(state)) {
5314 eat_tokens(state, TOK_MELSE);
5315 }
5316 break;
5317 }
5318 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005319 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005320 enter_else(state, "#else");
5321 if (!if_eat(state) && if_value(state)) {
5322 eat_tokens(state, TOK_MENDIF);
5323 }
5324 break;
5325 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005326 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005327 exit_if(state, "#endif");
5328 break;
5329 case TOK_MDEFINE:
5330 {
5331 struct hash_entry *ident;
5332 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005333 const char *mstart, *mend;
5334 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005335
Eric Biedermancb364952004-11-15 10:46:44 +00005336 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005337 if (if_eat(state)) /* quit early when #if'd out */
5338 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005339 ident = pp_eat(state, TOK_MIDENT)->ident;
5340 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005341 args = 0;
5342 larg = &args;
5343
Eric Biederman41203d92004-11-08 09:31:09 +00005344 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005345 if (raw_peek(state) == TOK_LPAREN) {
5346 raw_eat(state, TOK_LPAREN);
5347 argc += 1;
5348
Eric Biederman41203d92004-11-08 09:31:09 +00005349 for(;;) {
5350 struct macro_arg *narg, *arg;
5351 struct hash_entry *aident;
5352 int tok;
5353
Eric Biedermancb364952004-11-15 10:46:44 +00005354 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005355 if (!args && (tok == TOK_RPAREN)) {
5356 break;
5357 }
5358 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005359 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005360 aident = state->i___VA_ARGS__;
5361 }
5362 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005363 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005364 }
5365
5366 narg = xcmalloc(sizeof(*arg), "macro arg");
5367 narg->ident = aident;
5368
5369 /* Verify I don't have a duplicate identifier */
5370 for(arg = args; arg; arg = arg->next) {
5371 if (arg->ident == narg->ident) {
5372 error(state, 0, "Duplicate macro arg `%s'",
5373 narg->ident->name);
5374 }
5375 }
5376 /* Add the new argument to the end of the list */
5377 *larg = narg;
5378 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005379 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005380
5381 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005382 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005383 break;
5384 }
Eric Biedermancb364952004-11-15 10:46:44 +00005385 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005386 }
Eric Biedermancb364952004-11-15 10:46:44 +00005387 pp_eat(state, TOK_RPAREN);
5388 }
5389 /* Remove leading whitespace */
5390 while(raw_peek(state) == TOK_SPACE) {
5391 raw_eat(state, TOK_SPACE);
5392 }
Eric Biederman41203d92004-11-08 09:31:09 +00005393
Eric Biedermancb364952004-11-15 10:46:44 +00005394 /* Remember the start of the macro body */
5395 tok = raw_peek(state);
5396 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005397
Eric Biedermancb364952004-11-15 10:46:44 +00005398 /* Find the end of the macro */
5399 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5400 raw_eat(state, tok);
5401 /* Remember the end of the last non space token */
5402 raw_peek(state);
5403 if (tok != TOK_SPACE) {
5404 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005405 }
5406 }
Eric Biedermancb364952004-11-15 10:46:44 +00005407
5408 /* Now that I have found the body defined the token */
5409 do_define_macro(state, ident,
5410 char_strdup(state->file, mstart, mend, "macro buf"),
5411 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005412 break;
5413 }
5414 case TOK_MERROR:
5415 {
Eric Biedermancb364952004-11-15 10:46:44 +00005416 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005417 int len;
5418
Eric Biedermancb364952004-11-15 10:46:44 +00005419 pp_eat(state, TOK_MERROR);
5420 /* Find the start of the line */
5421 raw_peek(state);
5422 start = get_token(state, 1)->pos;
5423
Eric Biederman41203d92004-11-08 09:31:09 +00005424 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005425 while((tok = raw_peek(state)) != TOK_EOL) {
5426 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005427 }
Eric Biedermancb364952004-11-15 10:46:44 +00005428 end = get_token(state, 1)->pos;
5429 len = end - start;
5430 if (!if_eat(state)) {
5431 error(state, 0, "%*.*s", len, len, start);
5432 }
Eric Biederman41203d92004-11-08 09:31:09 +00005433 break;
5434 }
5435 case TOK_MWARNING:
5436 {
Eric Biedermancb364952004-11-15 10:46:44 +00005437 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005438 int len;
5439
Eric Biedermancb364952004-11-15 10:46:44 +00005440 pp_eat(state, TOK_MWARNING);
5441
5442 /* Find the start of the line */
5443 raw_peek(state);
5444 start = get_token(state, 1)->pos;
5445
Eric Biederman41203d92004-11-08 09:31:09 +00005446 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005447 while((tok = raw_peek(state)) != TOK_EOL) {
5448 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005449 }
Eric Biedermancb364952004-11-15 10:46:44 +00005450 end = get_token(state, 1)->pos;
5451 len = end - start;
5452 if (!if_eat(state)) {
5453 warning(state, 0, "%*.*s", len, len, start);
5454 }
Eric Biederman41203d92004-11-08 09:31:09 +00005455 break;
5456 }
5457 case TOK_MINCLUDE:
5458 {
5459 char *name;
5460 int local;
5461 local = 0;
5462 name = 0;
5463
Eric Biedermancb364952004-11-15 10:46:44 +00005464 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005465 if (if_eat(state)) {
5466 /* Find the end of the line */
5467 while((tok = raw_peek(state)) != TOK_EOL) {
5468 raw_eat(state, tok);
5469 }
5470 break;
5471 }
Eric Biederman41203d92004-11-08 09:31:09 +00005472 tok = peek(state);
5473 if (tok == TOK_LIT_STRING) {
5474 struct token *tk;
5475 const char *token;
5476 int name_len;
5477 tk = eat(state, TOK_LIT_STRING);
5478 name = xmalloc(tk->str_len, "include");
5479 token = tk->val.str +1;
5480 name_len = tk->str_len -2;
5481 if (*token == '"') {
5482 token++;
5483 name_len--;
5484 }
5485 memcpy(name, token, name_len);
5486 name[name_len] = '\0';
5487 local = 1;
5488 }
5489 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005490 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005491 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005492
5493 buf.len = 40;
5494 buf.str = xmalloc(buf.len, "include");
5495 buf.pos = 0;
5496
5497 tok = peek(state);
5498 while((tok != TOK_MORE) &&
5499 (tok != TOK_EOL) && (tok != TOK_EOF))
5500 {
5501 struct token *tk;
5502 tk = eat(state, tok);
5503 append_macro_chars(state, "include", &buf,
5504 state->file, tk->pos, state->file->pos);
5505 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005506 }
Eric Biedermancb364952004-11-15 10:46:44 +00005507 append_macro_text(state, "include", &buf, "\0", 1);
5508 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005509 error(state, 0, "Unterminated include directive");
5510 }
Eric Biederman41203d92004-11-08 09:31:09 +00005511 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005512 local = 0;
5513 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005514 }
5515 else {
5516 error(state, 0, "Invalid include directive");
5517 }
5518 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005519 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005520 error(state, 0, "garbage after include directive");
5521 }
5522 if (!if_eat(state)) {
5523 compile_file(state, name, local);
5524 }
5525 xfree(name);
5526 break;
5527 }
5528 case TOK_EOL:
5529 /* Ignore # without a follwing ident */
5530 break;
5531 default:
5532 {
5533 const char *name1, *name2;
5534 name1 = tokens[tok];
5535 name2 = "";
5536 if (tok == TOK_MIDENT) {
5537 name2 = get_token(state, 1)->ident->name;
5538 }
5539 error(state, 0, "Invalid preprocessor directive: %s %s",
5540 name1, name2);
5541 break;
5542 }
5543 }
5544 /* Consume the rest of the macro line */
5545 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005546 tok = pp_peek(state);
5547 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005548 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5549 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005550 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005551 return;
5552}
5553
Eric Biederman0babc1c2003-05-09 02:39:00 +00005554/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005555
5556static struct type *new_type(
5557 unsigned int type, struct type *left, struct type *right)
5558{
5559 struct type *result;
5560 result = xmalloc(sizeof(*result), "type");
5561 result->type = type;
5562 result->left = left;
5563 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005564 result->field_ident = 0;
5565 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005566 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005567 return result;
5568}
5569
5570static struct type *clone_type(unsigned int specifiers, struct type *old)
5571{
5572 struct type *result;
5573 result = xmalloc(sizeof(*result), "type");
5574 memcpy(result, old, sizeof(*result));
5575 result->type &= TYPE_MASK;
5576 result->type |= specifiers;
5577 return result;
5578}
5579
Eric Biederman90089602004-05-28 14:11:54 +00005580static struct type *dup_type(struct compile_state *state, struct type *orig)
5581{
5582 struct type *new;
5583 new = xcmalloc(sizeof(*new), "type");
5584 new->type = orig->type;
5585 new->field_ident = orig->field_ident;
5586 new->type_ident = orig->type_ident;
5587 new->elements = orig->elements;
5588 if (orig->left) {
5589 new->left = dup_type(state, orig->left);
5590 }
5591 if (orig->right) {
5592 new->right = dup_type(state, orig->right);
5593 }
5594 return new;
5595}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005596
Eric Biederman90089602004-05-28 14:11:54 +00005597
5598static struct type *invalid_type(struct compile_state *state, struct type *type)
5599{
5600 struct type *invalid, *member;
5601 invalid = 0;
5602 if (!type) {
5603 internal_error(state, 0, "type missing?");
5604 }
5605 switch(type->type & TYPE_MASK) {
5606 case TYPE_VOID:
5607 case TYPE_CHAR: case TYPE_UCHAR:
5608 case TYPE_SHORT: case TYPE_USHORT:
5609 case TYPE_INT: case TYPE_UINT:
5610 case TYPE_LONG: case TYPE_ULONG:
5611 case TYPE_LLONG: case TYPE_ULLONG:
5612 case TYPE_POINTER:
5613 case TYPE_ENUM:
5614 break;
5615 case TYPE_BITFIELD:
5616 invalid = invalid_type(state, type->left);
5617 break;
5618 case TYPE_ARRAY:
5619 invalid = invalid_type(state, type->left);
5620 break;
5621 case TYPE_STRUCT:
5622 case TYPE_TUPLE:
5623 member = type->left;
5624 while(member && (invalid == 0) &&
5625 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5626 invalid = invalid_type(state, member->left);
5627 member = member->right;
5628 }
5629 if (!invalid) {
5630 invalid = invalid_type(state, member);
5631 }
5632 break;
5633 case TYPE_UNION:
5634 case TYPE_JOIN:
5635 member = type->left;
5636 while(member && (invalid == 0) &&
5637 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5638 invalid = invalid_type(state, member->left);
5639 member = member->right;
5640 }
5641 if (!invalid) {
5642 invalid = invalid_type(state, member);
5643 }
5644 break;
5645 default:
5646 invalid = type;
5647 break;
5648 }
5649 return invalid;
5650
5651}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005652
5653#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005654#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005655static inline ulong_t mask_uint(ulong_t x)
5656{
5657 if (SIZEOF_INT < SIZEOF_LONG) {
Stefan Reinauer50542a82007-10-24 11:14:14 +00005658 ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005659 x &= mask;
5660 }
5661 return x;
5662}
5663#define MASK_UINT(X) (mask_uint(X))
5664#define MASK_ULONG(X) (X)
5665
Eric Biederman90089602004-05-28 14:11:54 +00005666static struct type void_type = { .type = TYPE_VOID };
5667static struct type char_type = { .type = TYPE_CHAR };
5668static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005669#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005670static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005671#endif
Eric Biederman90089602004-05-28 14:11:54 +00005672static struct type ushort_type = { .type = TYPE_USHORT };
5673static struct type int_type = { .type = TYPE_INT };
5674static struct type uint_type = { .type = TYPE_UINT };
5675static struct type long_type = { .type = TYPE_LONG };
5676static struct type ulong_type = { .type = TYPE_ULONG };
5677static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005678
Eric Biederman5ade04a2003-10-22 04:03:46 +00005679static struct type void_ptr_type = {
5680 .type = TYPE_POINTER,
5681 .left = &void_type,
5682};
5683
Stefan Reinauer50542a82007-10-24 11:14:14 +00005684#if DEBUG_ROMCC_WARNING
Eric Biederman5ade04a2003-10-22 04:03:46 +00005685static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005686 .type = TYPE_FUNCTION,
5687 .left = &void_type,
5688 .right = &void_type,
5689};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005690#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005691
Eric Biederman90089602004-05-28 14:11:54 +00005692static size_t bits_to_bytes(size_t size)
5693{
5694 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5695}
5696
Eric Biedermanb138ac82003-04-22 18:44:01 +00005697static struct triple *variable(struct compile_state *state, struct type *type)
5698{
5699 struct triple *result;
5700 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005701 result = triple(state, OP_ADECL, type, 0, 0);
5702 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005703 }
5704 else {
5705 result = triple(state, OP_SDECL, type, 0, 0);
5706 }
5707 return result;
5708}
5709
5710static void stor_of(FILE *fp, struct type *type)
5711{
5712 switch(type->type & STOR_MASK) {
5713 case STOR_AUTO:
5714 fprintf(fp, "auto ");
5715 break;
5716 case STOR_STATIC:
5717 fprintf(fp, "static ");
5718 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005719 case STOR_LOCAL:
5720 fprintf(fp, "local ");
5721 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005722 case STOR_EXTERN:
5723 fprintf(fp, "extern ");
5724 break;
5725 case STOR_REGISTER:
5726 fprintf(fp, "register ");
5727 break;
5728 case STOR_TYPEDEF:
5729 fprintf(fp, "typedef ");
5730 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005731 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005732 fprintf(fp, "inline ");
5733 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005734 case STOR_INLINE | STOR_STATIC:
5735 fprintf(fp, "static inline");
5736 break;
5737 case STOR_INLINE | STOR_EXTERN:
5738 fprintf(fp, "extern inline");
5739 break;
5740 default:
5741 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5742 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005743 }
5744}
5745static void qual_of(FILE *fp, struct type *type)
5746{
5747 if (type->type & QUAL_CONST) {
5748 fprintf(fp, " const");
5749 }
5750 if (type->type & QUAL_VOLATILE) {
5751 fprintf(fp, " volatile");
5752 }
5753 if (type->type & QUAL_RESTRICT) {
5754 fprintf(fp, " restrict");
5755 }
5756}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005757
Eric Biedermanb138ac82003-04-22 18:44:01 +00005758static void name_of(FILE *fp, struct type *type)
5759{
Eric Biederman90089602004-05-28 14:11:54 +00005760 unsigned int base_type;
5761 base_type = type->type & TYPE_MASK;
5762 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5763 stor_of(fp, type);
5764 }
5765 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005766 case TYPE_VOID:
5767 fprintf(fp, "void");
5768 qual_of(fp, type);
5769 break;
5770 case TYPE_CHAR:
5771 fprintf(fp, "signed char");
5772 qual_of(fp, type);
5773 break;
5774 case TYPE_UCHAR:
5775 fprintf(fp, "unsigned char");
5776 qual_of(fp, type);
5777 break;
5778 case TYPE_SHORT:
5779 fprintf(fp, "signed short");
5780 qual_of(fp, type);
5781 break;
5782 case TYPE_USHORT:
5783 fprintf(fp, "unsigned short");
5784 qual_of(fp, type);
5785 break;
5786 case TYPE_INT:
5787 fprintf(fp, "signed int");
5788 qual_of(fp, type);
5789 break;
5790 case TYPE_UINT:
5791 fprintf(fp, "unsigned int");
5792 qual_of(fp, type);
5793 break;
5794 case TYPE_LONG:
5795 fprintf(fp, "signed long");
5796 qual_of(fp, type);
5797 break;
5798 case TYPE_ULONG:
5799 fprintf(fp, "unsigned long");
5800 qual_of(fp, type);
5801 break;
5802 case TYPE_POINTER:
5803 name_of(fp, type->left);
5804 fprintf(fp, " * ");
5805 qual_of(fp, type);
5806 break;
5807 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005808 name_of(fp, type->left);
5809 fprintf(fp, ", ");
5810 name_of(fp, type->right);
5811 break;
Eric Biederman90089602004-05-28 14:11:54 +00005812 case TYPE_OVERLAP:
5813 name_of(fp, type->left);
5814 fprintf(fp, ",| ");
5815 name_of(fp, type->right);
5816 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005817 case TYPE_ENUM:
Eric Biederman90089602004-05-28 14:11:54 +00005818 fprintf(fp, "enum %s",
5819 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005820 qual_of(fp, type);
5821 break;
5822 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005823 fprintf(fp, "struct %s { ",
5824 (type->type_ident)? type->type_ident->name : "");
5825 name_of(fp, type->left);
5826 fprintf(fp, " } ");
5827 qual_of(fp, type);
5828 break;
5829 case TYPE_UNION:
5830 fprintf(fp, "union %s { ",
5831 (type->type_ident)? type->type_ident->name : "");
5832 name_of(fp, type->left);
5833 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005834 qual_of(fp, type);
5835 break;
5836 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005837 name_of(fp, type->left);
5838 fprintf(fp, " (*)(");
5839 name_of(fp, type->right);
5840 fprintf(fp, ")");
5841 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005842 case TYPE_ARRAY:
5843 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005844 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005845 break;
Eric Biederman90089602004-05-28 14:11:54 +00005846 case TYPE_TUPLE:
5847 fprintf(fp, "tuple { ");
5848 name_of(fp, type->left);
5849 fprintf(fp, " } ");
5850 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005851 break;
Eric Biederman90089602004-05-28 14:11:54 +00005852 case TYPE_JOIN:
5853 fprintf(fp, "join { ");
5854 name_of(fp, type->left);
5855 fprintf(fp, " } ");
5856 qual_of(fp, type);
5857 break;
5858 case TYPE_BITFIELD:
5859 name_of(fp, type->left);
5860 fprintf(fp, " : %d ", type->elements);
5861 qual_of(fp, type);
5862 break;
5863 case TYPE_UNKNOWN:
5864 fprintf(fp, "unknown_t");
5865 break;
5866 default:
5867 fprintf(fp, "????: %x", base_type);
5868 break;
5869 }
5870 if (type->field_ident && type->field_ident->name) {
5871 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005872 }
5873}
5874
5875static size_t align_of(struct compile_state *state, struct type *type)
5876{
5877 size_t align;
5878 align = 0;
5879 switch(type->type & TYPE_MASK) {
5880 case TYPE_VOID:
5881 align = 1;
5882 break;
Eric Biederman90089602004-05-28 14:11:54 +00005883 case TYPE_BITFIELD:
5884 align = 1;
5885 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005886 case TYPE_CHAR:
5887 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005888 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005889 break;
5890 case TYPE_SHORT:
5891 case TYPE_USHORT:
5892 align = ALIGNOF_SHORT;
5893 break;
5894 case TYPE_INT:
5895 case TYPE_UINT:
5896 case TYPE_ENUM:
5897 align = ALIGNOF_INT;
5898 break;
5899 case TYPE_LONG:
5900 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005901 align = ALIGNOF_LONG;
5902 break;
Eric Biederman90089602004-05-28 14:11:54 +00005903 case TYPE_POINTER:
5904 align = ALIGNOF_POINTER;
5905 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005906 case TYPE_PRODUCT:
5907 case TYPE_OVERLAP:
5908 {
5909 size_t left_align, right_align;
5910 left_align = align_of(state, type->left);
5911 right_align = align_of(state, type->right);
5912 align = (left_align >= right_align) ? left_align : right_align;
5913 break;
5914 }
5915 case TYPE_ARRAY:
5916 align = align_of(state, type->left);
5917 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005918 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005919 case TYPE_TUPLE:
5920 case TYPE_UNION:
5921 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005922 align = align_of(state, type->left);
5923 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005924 default:
5925 error(state, 0, "alignof not yet defined for type\n");
5926 break;
5927 }
5928 return align;
5929}
5930
Eric Biederman90089602004-05-28 14:11:54 +00005931static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005932{
Eric Biederman90089602004-05-28 14:11:54 +00005933 size_t align;
5934 align = 0;
5935 switch(type->type & TYPE_MASK) {
5936 case TYPE_VOID:
5937 align = 1;
5938 break;
5939 case TYPE_BITFIELD:
5940 align = 1;
5941 break;
5942 case TYPE_CHAR:
5943 case TYPE_UCHAR:
5944 align = REG_ALIGNOF_CHAR;
5945 break;
5946 case TYPE_SHORT:
5947 case TYPE_USHORT:
5948 align = REG_ALIGNOF_SHORT;
5949 break;
5950 case TYPE_INT:
5951 case TYPE_UINT:
5952 case TYPE_ENUM:
5953 align = REG_ALIGNOF_INT;
5954 break;
5955 case TYPE_LONG:
5956 case TYPE_ULONG:
5957 align = REG_ALIGNOF_LONG;
5958 break;
5959 case TYPE_POINTER:
5960 align = REG_ALIGNOF_POINTER;
5961 break;
5962 case TYPE_PRODUCT:
5963 case TYPE_OVERLAP:
5964 {
5965 size_t left_align, right_align;
5966 left_align = reg_align_of(state, type->left);
5967 right_align = reg_align_of(state, type->right);
5968 align = (left_align >= right_align) ? left_align : right_align;
5969 break;
5970 }
5971 case TYPE_ARRAY:
5972 align = reg_align_of(state, type->left);
5973 break;
5974 case TYPE_STRUCT:
5975 case TYPE_UNION:
5976 case TYPE_TUPLE:
5977 case TYPE_JOIN:
5978 align = reg_align_of(state, type->left);
5979 break;
5980 default:
5981 error(state, 0, "alignof not yet defined for type\n");
5982 break;
5983 }
5984 return align;
5985}
5986
5987static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5988{
5989 return bits_to_bytes(align_of(state, type));
5990}
5991static size_t size_of(struct compile_state *state, struct type *type);
5992static size_t reg_size_of(struct compile_state *state, struct type *type);
5993
5994static size_t needed_padding(struct compile_state *state,
5995 struct type *type, size_t offset)
5996{
5997 size_t padding, align;
5998 align = align_of(state, type);
5999 /* Align to the next machine word if the bitfield does completely
6000 * fit into the current word.
6001 */
6002 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
6003 size_t size;
6004 size = size_of(state, type);
6005 if ((offset + type->elements)/size != offset/size) {
6006 align = size;
6007 }
6008 }
Eric Biederman03b59862003-06-24 14:27:37 +00006009 padding = 0;
6010 if (offset % align) {
6011 padding = align - (offset % align);
6012 }
6013 return padding;
6014}
Eric Biederman90089602004-05-28 14:11:54 +00006015
6016static size_t reg_needed_padding(struct compile_state *state,
6017 struct type *type, size_t offset)
6018{
6019 size_t padding, align;
6020 align = reg_align_of(state, type);
6021 /* Align to the next register word if the bitfield does completely
6022 * fit into the current register.
6023 */
6024 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
6025 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
6026 {
6027 align = REG_SIZEOF_REG;
6028 }
6029 padding = 0;
6030 if (offset % align) {
6031 padding = align - (offset % align);
6032 }
6033 return padding;
6034}
6035
Eric Biedermanb138ac82003-04-22 18:44:01 +00006036static size_t size_of(struct compile_state *state, struct type *type)
6037{
6038 size_t size;
6039 size = 0;
6040 switch(type->type & TYPE_MASK) {
6041 case TYPE_VOID:
6042 size = 0;
6043 break;
Eric Biederman90089602004-05-28 14:11:54 +00006044 case TYPE_BITFIELD:
6045 size = type->elements;
6046 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006047 case TYPE_CHAR:
6048 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006049 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006050 break;
6051 case TYPE_SHORT:
6052 case TYPE_USHORT:
6053 size = SIZEOF_SHORT;
6054 break;
6055 case TYPE_INT:
6056 case TYPE_UINT:
6057 case TYPE_ENUM:
6058 size = SIZEOF_INT;
6059 break;
6060 case TYPE_LONG:
6061 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006062 size = SIZEOF_LONG;
6063 break;
Eric Biederman90089602004-05-28 14:11:54 +00006064 case TYPE_POINTER:
6065 size = SIZEOF_POINTER;
6066 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006067 case TYPE_PRODUCT:
6068 {
Eric Biederman90089602004-05-28 14:11:54 +00006069 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006070 size = 0;
6071 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006072 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006073 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006074 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006075 }
Eric Biederman90089602004-05-28 14:11:54 +00006076 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006077 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006078 break;
6079 }
6080 case TYPE_OVERLAP:
6081 {
6082 size_t size_left, size_right;
6083 size_left = size_of(state, type->left);
6084 size_right = size_of(state, type->right);
6085 size = (size_left >= size_right)? size_left : size_right;
6086 break;
6087 }
6088 case TYPE_ARRAY:
6089 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6090 internal_error(state, 0, "Invalid array type");
6091 } else {
6092 size = size_of(state, type->left) * type->elements;
6093 }
6094 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006095 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006096 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006097 {
Eric Biederman90089602004-05-28 14:11:54 +00006098 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006099 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006100 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006101 pad = needed_padding(state, type, size);
6102 size = size + pad;
6103 break;
6104 }
6105 case TYPE_UNION:
6106 case TYPE_JOIN:
6107 {
6108 size_t pad;
6109 size = size_of(state, type->left);
6110 /* Pad unions so their size is a multiple of their alignment */
6111 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006112 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006113 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006114 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006115 default:
Eric Biederman90089602004-05-28 14:11:54 +00006116 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006117 break;
6118 }
6119 return size;
6120}
6121
Eric Biederman90089602004-05-28 14:11:54 +00006122static size_t reg_size_of(struct compile_state *state, struct type *type)
6123{
6124 size_t size;
6125 size = 0;
6126 switch(type->type & TYPE_MASK) {
6127 case TYPE_VOID:
6128 size = 0;
6129 break;
6130 case TYPE_BITFIELD:
6131 size = type->elements;
6132 break;
6133 case TYPE_CHAR:
6134 case TYPE_UCHAR:
6135 size = REG_SIZEOF_CHAR;
6136 break;
6137 case TYPE_SHORT:
6138 case TYPE_USHORT:
6139 size = REG_SIZEOF_SHORT;
6140 break;
6141 case TYPE_INT:
6142 case TYPE_UINT:
6143 case TYPE_ENUM:
6144 size = REG_SIZEOF_INT;
6145 break;
6146 case TYPE_LONG:
6147 case TYPE_ULONG:
6148 size = REG_SIZEOF_LONG;
6149 break;
6150 case TYPE_POINTER:
6151 size = REG_SIZEOF_POINTER;
6152 break;
6153 case TYPE_PRODUCT:
6154 {
6155 size_t pad;
6156 size = 0;
6157 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6158 pad = reg_needed_padding(state, type->left, size);
6159 size = size + pad + reg_size_of(state, type->left);
6160 type = type->right;
6161 }
6162 pad = reg_needed_padding(state, type, size);
6163 size = size + pad + reg_size_of(state, type);
6164 break;
6165 }
6166 case TYPE_OVERLAP:
6167 {
6168 size_t size_left, size_right;
6169 size_left = reg_size_of(state, type->left);
6170 size_right = reg_size_of(state, type->right);
6171 size = (size_left >= size_right)? size_left : size_right;
6172 break;
6173 }
6174 case TYPE_ARRAY:
6175 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6176 internal_error(state, 0, "Invalid array type");
6177 } else {
6178 size = reg_size_of(state, type->left) * type->elements;
6179 }
6180 break;
6181 case TYPE_STRUCT:
6182 case TYPE_TUPLE:
6183 {
6184 size_t pad;
6185 size = reg_size_of(state, type->left);
6186 /* Pad structures so their size is a multiples of their alignment */
6187 pad = reg_needed_padding(state, type, size);
6188 size = size + pad;
6189 break;
6190 }
6191 case TYPE_UNION:
6192 case TYPE_JOIN:
6193 {
6194 size_t pad;
6195 size = reg_size_of(state, type->left);
6196 /* Pad unions so their size is a multiple of their alignment */
6197 pad = reg_needed_padding(state, type, size);
6198 size = size + pad;
6199 break;
6200 }
6201 default:
6202 internal_error(state, 0, "sizeof not yet defined for type");
6203 break;
6204 }
6205 return size;
6206}
6207
6208static size_t registers_of(struct compile_state *state, struct type *type)
6209{
6210 size_t registers;
6211 registers = reg_size_of(state, type);
6212 registers += REG_SIZEOF_REG - 1;
6213 registers /= REG_SIZEOF_REG;
6214 return registers;
6215}
6216
6217static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6218{
6219 return bits_to_bytes(size_of(state, type));
6220}
6221
Eric Biederman0babc1c2003-05-09 02:39:00 +00006222static size_t field_offset(struct compile_state *state,
6223 struct type *type, struct hash_entry *field)
6224{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006225 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006226 size_t size;
6227
Eric Biederman0babc1c2003-05-09 02:39:00 +00006228 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006229 member = 0;
6230 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6231 member = type->left;
6232 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6233 size += needed_padding(state, member->left, size);
6234 if (member->left->field_ident == field) {
6235 member = member->left;
6236 break;
6237 }
6238 size += size_of(state, member->left);
6239 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006240 }
Eric Biederman90089602004-05-28 14:11:54 +00006241 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006242 }
Eric Biederman90089602004-05-28 14:11:54 +00006243 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6244 member = type->left;
6245 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6246 if (member->left->field_ident == field) {
6247 member = member->left;
6248 break;
6249 }
6250 member = member->right;
6251 }
6252 }
6253 else {
6254 internal_error(state, 0, "field_offset only works on structures and unions");
6255 }
6256
6257 if (!member || (member->field_ident != field)) {
6258 error(state, 0, "member %s not present", field->name);
6259 }
6260 return size;
6261}
6262
6263static size_t field_reg_offset(struct compile_state *state,
6264 struct type *type, struct hash_entry *field)
6265{
6266 struct type *member;
6267 size_t size;
6268
6269 size = 0;
6270 member = 0;
6271 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6272 member = type->left;
6273 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6274 size += reg_needed_padding(state, member->left, size);
6275 if (member->left->field_ident == field) {
6276 member = member->left;
6277 break;
6278 }
6279 size += reg_size_of(state, member->left);
6280 member = member->right;
6281 }
6282 }
6283 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6284 member = type->left;
6285 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6286 if (member->left->field_ident == field) {
6287 member = member->left;
6288 break;
6289 }
6290 member = member->right;
6291 }
6292 }
6293 else {
6294 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6295 }
6296
6297 size += reg_needed_padding(state, member, size);
6298 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006299 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006300 }
6301 return size;
6302}
6303
6304static struct type *field_type(struct compile_state *state,
6305 struct type *type, struct hash_entry *field)
6306{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006307 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006308
6309 member = 0;
6310 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6311 member = type->left;
6312 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6313 if (member->left->field_ident == field) {
6314 member = member->left;
6315 break;
6316 }
6317 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006318 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006319 }
Eric Biederman90089602004-05-28 14:11:54 +00006320 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6321 member = type->left;
6322 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6323 if (member->left->field_ident == field) {
6324 member = member->left;
6325 break;
6326 }
6327 member = member->right;
6328 }
6329 }
6330 else {
6331 internal_error(state, 0, "field_type only works on structures and unions");
6332 }
6333
6334 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006335 error(state, 0, "member %s not present", field->name);
6336 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006337 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006338}
6339
Eric Biederman90089602004-05-28 14:11:54 +00006340static size_t index_offset(struct compile_state *state,
6341 struct type *type, ulong_t index)
6342{
6343 struct type *member;
6344 size_t size;
6345 size = 0;
6346 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6347 size = size_of(state, type->left) * index;
6348 }
6349 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6350 ulong_t i;
6351 member = type->left;
6352 i = 0;
6353 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6354 size += needed_padding(state, member->left, size);
6355 if (i == index) {
6356 member = member->left;
6357 break;
6358 }
6359 size += size_of(state, member->left);
6360 i++;
6361 member = member->right;
6362 }
6363 size += needed_padding(state, member, size);
6364 if (i != index) {
6365 internal_error(state, 0, "Missing member index: %u", index);
6366 }
6367 }
6368 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6369 ulong_t i;
6370 size = 0;
6371 member = type->left;
6372 i = 0;
6373 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6374 if (i == index) {
6375 member = member->left;
6376 break;
6377 }
6378 i++;
6379 member = member->right;
6380 }
6381 if (i != index) {
6382 internal_error(state, 0, "Missing member index: %u", index);
6383 }
6384 }
6385 else {
6386 internal_error(state, 0,
6387 "request for index %u in something not an array, tuple or join",
6388 index);
6389 }
6390 return size;
6391}
6392
6393static size_t index_reg_offset(struct compile_state *state,
6394 struct type *type, ulong_t index)
6395{
6396 struct type *member;
6397 size_t size;
6398 size = 0;
6399 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6400 size = reg_size_of(state, type->left) * index;
6401 }
6402 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6403 ulong_t i;
6404 member = type->left;
6405 i = 0;
6406 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6407 size += reg_needed_padding(state, member->left, size);
6408 if (i == index) {
6409 member = member->left;
6410 break;
6411 }
6412 size += reg_size_of(state, member->left);
6413 i++;
6414 member = member->right;
6415 }
6416 size += reg_needed_padding(state, member, size);
6417 if (i != index) {
6418 internal_error(state, 0, "Missing member index: %u", index);
6419 }
6420
6421 }
6422 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6423 ulong_t i;
6424 size = 0;
6425 member = type->left;
6426 i = 0;
6427 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6428 if (i == index) {
6429 member = member->left;
6430 break;
6431 }
6432 i++;
6433 member = member->right;
6434 }
6435 if (i != index) {
6436 internal_error(state, 0, "Missing member index: %u", index);
6437 }
6438 }
6439 else {
6440 internal_error(state, 0,
6441 "request for index %u in something not an array, tuple or join",
6442 index);
6443 }
6444 return size;
6445}
6446
6447static struct type *index_type(struct compile_state *state,
6448 struct type *type, ulong_t index)
6449{
6450 struct type *member;
6451 if (index >= type->elements) {
6452 internal_error(state, 0, "Invalid element %u requested", index);
6453 }
6454 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6455 member = type->left;
6456 }
6457 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6458 ulong_t i;
6459 member = type->left;
6460 i = 0;
6461 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6462 if (i == index) {
6463 member = member->left;
6464 break;
6465 }
6466 i++;
6467 member = member->right;
6468 }
6469 if (i != index) {
6470 internal_error(state, 0, "Missing member index: %u", index);
6471 }
6472 }
6473 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6474 ulong_t i;
6475 member = type->left;
6476 i = 0;
6477 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6478 if (i == index) {
6479 member = member->left;
6480 break;
6481 }
6482 i++;
6483 member = member->right;
6484 }
6485 if (i != index) {
6486 internal_error(state, 0, "Missing member index: %u", index);
6487 }
6488 }
6489 else {
6490 member = 0;
6491 internal_error(state, 0,
6492 "request for index %u in something not an array, tuple or join",
6493 index);
6494 }
6495 return member;
6496}
6497
6498static struct type *unpack_type(struct compile_state *state, struct type *type)
6499{
6500 /* If I have a single register compound type not a bit-field
6501 * find the real type.
6502 */
6503 struct type *start_type;
6504 size_t size;
6505 /* Get out early if I need multiple registers for this type */
6506 size = reg_size_of(state, type);
6507 if (size > REG_SIZEOF_REG) {
6508 return type;
6509 }
6510 /* Get out early if I don't need any registers for this type */
6511 if (size == 0) {
6512 return &void_type;
6513 }
6514 /* Loop until I have no more layers I can remove */
6515 do {
6516 start_type = type;
6517 switch(type->type & TYPE_MASK) {
6518 case TYPE_ARRAY:
6519 /* If I have a single element the unpacked type
6520 * is that element.
6521 */
6522 if (type->elements == 1) {
6523 type = type->left;
6524 }
6525 break;
6526 case TYPE_STRUCT:
6527 case TYPE_TUPLE:
6528 /* If I have a single element the unpacked type
6529 * is that element.
6530 */
6531 if (type->elements == 1) {
6532 type = type->left;
6533 }
6534 /* If I have multiple elements the unpacked
6535 * type is the non-void element.
6536 */
6537 else {
6538 struct type *next, *member;
6539 struct type *sub_type;
6540 sub_type = 0;
6541 next = type->left;
6542 while(next) {
6543 member = next;
6544 next = 0;
6545 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6546 next = member->right;
6547 member = member->left;
6548 }
6549 if (reg_size_of(state, member) > 0) {
6550 if (sub_type) {
6551 internal_error(state, 0, "true compound type in a register");
6552 }
6553 sub_type = member;
6554 }
6555 }
6556 if (sub_type) {
6557 type = sub_type;
6558 }
6559 }
6560 break;
6561
6562 case TYPE_UNION:
6563 case TYPE_JOIN:
6564 /* If I have a single element the unpacked type
6565 * is that element.
6566 */
6567 if (type->elements == 1) {
6568 type = type->left;
6569 }
6570 /* I can't in general unpack union types */
6571 break;
6572 default:
6573 /* If I'm not a compound type I can't unpack it */
6574 break;
6575 }
6576 } while(start_type != type);
6577 switch(type->type & TYPE_MASK) {
6578 case TYPE_STRUCT:
6579 case TYPE_ARRAY:
6580 case TYPE_TUPLE:
6581 internal_error(state, 0, "irredicible type?");
6582 break;
6583 }
6584 return type;
6585}
6586
6587static int equiv_types(struct type *left, struct type *right);
6588static int is_compound_type(struct type *type);
6589
6590static struct type *reg_type(
6591 struct compile_state *state, struct type *type, int reg_offset)
6592{
6593 struct type *member;
6594 size_t size;
6595#if 1
6596 struct type *invalid;
6597 invalid = invalid_type(state, type);
6598 if (invalid) {
6599 fprintf(state->errout, "type: ");
6600 name_of(state->errout, type);
6601 fprintf(state->errout, "\n");
6602 fprintf(state->errout, "invalid: ");
6603 name_of(state->errout, invalid);
6604 fprintf(state->errout, "\n");
6605 internal_error(state, 0, "bad input type?");
6606 }
6607#endif
6608
6609 size = reg_size_of(state, type);
6610 if (reg_offset > size) {
6611 member = 0;
6612 fprintf(state->errout, "type: ");
6613 name_of(state->errout, type);
6614 fprintf(state->errout, "\n");
6615 internal_error(state, 0, "offset outside of type");
6616 }
6617 else {
6618 switch(type->type & TYPE_MASK) {
6619 /* Don't do anything with the basic types */
6620 case TYPE_VOID:
6621 case TYPE_CHAR: case TYPE_UCHAR:
6622 case TYPE_SHORT: case TYPE_USHORT:
6623 case TYPE_INT: case TYPE_UINT:
6624 case TYPE_LONG: case TYPE_ULONG:
6625 case TYPE_LLONG: case TYPE_ULLONG:
6626 case TYPE_FLOAT: case TYPE_DOUBLE:
6627 case TYPE_LDOUBLE:
6628 case TYPE_POINTER:
6629 case TYPE_ENUM:
6630 case TYPE_BITFIELD:
6631 member = type;
6632 break;
6633 case TYPE_ARRAY:
6634 member = type->left;
6635 size = reg_size_of(state, member);
6636 if (size > REG_SIZEOF_REG) {
6637 member = reg_type(state, member, reg_offset % size);
6638 }
6639 break;
6640 case TYPE_STRUCT:
6641 case TYPE_TUPLE:
6642 {
6643 size_t offset;
6644 offset = 0;
6645 member = type->left;
6646 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6647 size = reg_size_of(state, member->left);
6648 offset += reg_needed_padding(state, member->left, offset);
6649 if ((offset + size) > reg_offset) {
6650 member = member->left;
6651 break;
6652 }
6653 offset += size;
6654 member = member->right;
6655 }
6656 offset += reg_needed_padding(state, member, offset);
6657 member = reg_type(state, member, reg_offset - offset);
6658 break;
6659 }
6660 case TYPE_UNION:
6661 case TYPE_JOIN:
6662 {
6663 struct type *join, **jnext, *mnext;
6664 join = new_type(TYPE_JOIN, 0, 0);
6665 jnext = &join->left;
6666 mnext = type->left;
6667 while(mnext) {
6668 size_t size;
6669 member = mnext;
6670 mnext = 0;
6671 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6672 mnext = member->right;
6673 member = member->left;
6674 }
6675 size = reg_size_of(state, member);
6676 if (size > reg_offset) {
6677 struct type *part, *hunt;
6678 part = reg_type(state, member, reg_offset);
6679 /* See if this type is already in the union */
6680 hunt = join->left;
6681 while(hunt) {
6682 struct type *test = hunt;
6683 hunt = 0;
6684 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6685 hunt = test->right;
6686 test = test->left;
6687 }
6688 if (equiv_types(part, test)) {
6689 goto next;
6690 }
6691 }
6692 /* Nope add it */
6693 if (!*jnext) {
6694 *jnext = part;
6695 } else {
6696 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6697 jnext = &(*jnext)->right;
6698 }
6699 join->elements++;
6700 }
6701 next:
6702 ;
6703 }
6704 if (join->elements == 0) {
6705 internal_error(state, 0, "No elements?");
6706 }
6707 member = join;
6708 break;
6709 }
6710 default:
6711 member = 0;
6712 fprintf(state->errout, "type: ");
6713 name_of(state->errout, type);
6714 fprintf(state->errout, "\n");
6715 internal_error(state, 0, "reg_type not yet defined for type");
6716
6717 }
6718 }
6719 /* If I have a single register compound type not a bit-field
6720 * find the real type.
6721 */
6722 member = unpack_type(state, member);
6723 ;
6724 size = reg_size_of(state, member);
6725 if (size > REG_SIZEOF_REG) {
6726 internal_error(state, 0, "Cannot find type of single register");
6727 }
6728#if 1
6729 invalid = invalid_type(state, member);
6730 if (invalid) {
6731 fprintf(state->errout, "type: ");
6732 name_of(state->errout, member);
6733 fprintf(state->errout, "\n");
6734 fprintf(state->errout, "invalid: ");
6735 name_of(state->errout, invalid);
6736 fprintf(state->errout, "\n");
6737 internal_error(state, 0, "returning bad type?");
6738 }
6739#endif
6740 return member;
6741}
6742
Eric Biederman03b59862003-06-24 14:27:37 +00006743static struct type *next_field(struct compile_state *state,
6744 struct type *type, struct type *prev_member)
6745{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006746 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006747 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6748 internal_error(state, 0, "next_field only works on structures");
6749 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006750 member = type->left;
6751 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006752 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006753 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006754 break;
6755 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006756 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006757 prev_member = 0;
6758 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006759 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006760 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006761 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006762 prev_member = 0;
6763 }
6764 if (prev_member) {
6765 internal_error(state, 0, "prev_member %s not present",
6766 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006767 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006768 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006769}
6770
Eric Biederman90089602004-05-28 14:11:54 +00006771typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
6772 size_t ret_offset, size_t mem_offset, void *arg);
6773
6774static void walk_type_fields(struct compile_state *state,
6775 struct type *type, size_t reg_offset, size_t mem_offset,
6776 walk_type_fields_cb_t cb, void *arg);
6777
6778static void walk_struct_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)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006781{
Eric Biederman90089602004-05-28 14:11:54 +00006782 struct type *tptr;
6783 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006784 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006785 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006786 }
Eric Biederman90089602004-05-28 14:11:54 +00006787 tptr = type->left;
6788 for(i = 0; i < type->elements; i++) {
6789 struct type *mtype;
6790 mtype = tptr;
6791 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6792 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006793 }
Eric Biederman90089602004-05-28 14:11:54 +00006794 walk_type_fields(state, mtype,
6795 reg_offset +
6796 field_reg_offset(state, type, mtype->field_ident),
6797 mem_offset +
6798 field_offset(state, type, mtype->field_ident),
6799 cb, arg);
6800 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006801 }
Eric Biederman90089602004-05-28 14:11:54 +00006802
6803}
6804
6805static void walk_type_fields(struct compile_state *state,
6806 struct type *type, size_t reg_offset, size_t mem_offset,
6807 walk_type_fields_cb_t cb, void *arg)
6808{
6809 switch(type->type & TYPE_MASK) {
6810 case TYPE_STRUCT:
6811 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6812 break;
6813 case TYPE_CHAR:
6814 case TYPE_UCHAR:
6815 case TYPE_SHORT:
6816 case TYPE_USHORT:
6817 case TYPE_INT:
6818 case TYPE_UINT:
6819 case TYPE_LONG:
6820 case TYPE_ULONG:
6821 cb(state, type, reg_offset, mem_offset, arg);
6822 break;
6823 case TYPE_VOID:
6824 break;
6825 default:
6826 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006827 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006828}
6829
Eric Biedermanb138ac82003-04-22 18:44:01 +00006830static void arrays_complete(struct compile_state *state, struct type *type)
6831{
6832 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6833 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6834 error(state, 0, "array size not specified");
6835 }
6836 arrays_complete(state, type->left);
6837 }
6838}
6839
Eric Biederman90089602004-05-28 14:11:54 +00006840static unsigned int get_basic_type(struct type *type)
6841{
6842 unsigned int basic;
6843 basic = type->type & TYPE_MASK;
6844 /* Convert enums to ints */
6845 if (basic == TYPE_ENUM) {
6846 basic = TYPE_INT;
6847 }
6848 /* Convert bitfields to standard types */
6849 else if (basic == TYPE_BITFIELD) {
6850 if (type->elements <= SIZEOF_CHAR) {
6851 basic = TYPE_CHAR;
6852 }
6853 else if (type->elements <= SIZEOF_SHORT) {
6854 basic = TYPE_SHORT;
6855 }
6856 else if (type->elements <= SIZEOF_INT) {
6857 basic = TYPE_INT;
6858 }
6859 else if (type->elements <= SIZEOF_LONG) {
6860 basic = TYPE_LONG;
6861 }
6862 if (!TYPE_SIGNED(type->left->type)) {
6863 basic += 1;
6864 }
6865 }
6866 return basic;
6867}
6868
Eric Biedermanb138ac82003-04-22 18:44:01 +00006869static unsigned int do_integral_promotion(unsigned int type)
6870{
Eric Biederman83b991a2003-10-11 06:20:25 +00006871 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006872 type = TYPE_INT;
6873 }
6874 return type;
6875}
6876
6877static unsigned int do_arithmetic_conversion(
6878 unsigned int left, unsigned int right)
6879{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006880 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6881 return TYPE_LDOUBLE;
6882 }
6883 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6884 return TYPE_DOUBLE;
6885 }
6886 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6887 return TYPE_FLOAT;
6888 }
6889 left = do_integral_promotion(left);
6890 right = do_integral_promotion(right);
6891 /* If both operands have the same size done */
6892 if (left == right) {
6893 return left;
6894 }
6895 /* If both operands have the same signedness pick the larger */
6896 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6897 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6898 }
6899 /* If the signed type can hold everything use it */
6900 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6901 return left;
6902 }
6903 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6904 return right;
6905 }
6906 /* Convert to the unsigned type with the same rank as the signed type */
6907 else if (TYPE_SIGNED(left)) {
6908 return TYPE_MKUNSIGNED(left);
6909 }
6910 else {
6911 return TYPE_MKUNSIGNED(right);
6912 }
6913}
6914
6915/* see if two types are the same except for qualifiers */
6916static int equiv_types(struct type *left, struct type *right)
6917{
6918 unsigned int type;
6919 /* Error if the basic types do not match */
6920 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6921 return 0;
6922 }
6923 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006924 /* If the basic types match and it is a void type we are done */
6925 if (type == TYPE_VOID) {
6926 return 1;
6927 }
Eric Biederman90089602004-05-28 14:11:54 +00006928 /* For bitfields we need to compare the sizes */
6929 else if (type == TYPE_BITFIELD) {
6930 return (left->elements == right->elements) &&
6931 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6932 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006933 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006934 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006935 return 1;
6936 }
6937 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006938 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006939 return equiv_types(left->left, right->left);
6940 }
6941 else if (type == TYPE_ARRAY) {
6942 return (left->elements == right->elements) &&
6943 equiv_types(left->left, right->left);
6944 }
Eric Biederman90089602004-05-28 14:11:54 +00006945 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006946 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006947 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006948 }
Eric Biederman90089602004-05-28 14:11:54 +00006949 /* test for union equality */
6950 else if (type == TYPE_UNION) {
6951 return left->type_ident == right->type_ident;
6952 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006953 /* Test for equivalent functions */
6954 else if (type == TYPE_FUNCTION) {
6955 return equiv_types(left->left, right->left) &&
6956 equiv_types(left->right, right->right);
6957 }
6958 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006959 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006960 else if (type == TYPE_PRODUCT) {
6961 return equiv_types(left->left, right->left) &&
6962 equiv_types(left->right, right->right);
6963 }
Eric Biederman90089602004-05-28 14:11:54 +00006964 /* We should see TYPE_OVERLAP when comparing joins */
6965 else if (type == TYPE_OVERLAP) {
6966 return equiv_types(left->left, right->left) &&
6967 equiv_types(left->right, right->right);
6968 }
6969 /* Test for equivalence of tuples */
6970 else if (type == TYPE_TUPLE) {
6971 return (left->elements == right->elements) &&
6972 equiv_types(left->left, right->left);
6973 }
6974 /* Test for equivalence of joins */
6975 else if (type == TYPE_JOIN) {
6976 return (left->elements == right->elements) &&
6977 equiv_types(left->left, right->left);
6978 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006979 else {
6980 return 0;
6981 }
6982}
6983
6984static int equiv_ptrs(struct type *left, struct type *right)
6985{
6986 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6987 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6988 return 0;
6989 }
6990 return equiv_types(left->left, right->left);
6991}
6992
6993static struct type *compatible_types(struct type *left, struct type *right)
6994{
6995 struct type *result;
6996 unsigned int type, qual_type;
6997 /* Error if the basic types do not match */
6998 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6999 return 0;
7000 }
7001 type = left->type & TYPE_MASK;
7002 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7003 result = 0;
7004 /* if the basic types match and it is an arithmetic type we are done */
7005 if (TYPE_ARITHMETIC(type)) {
7006 result = new_type(qual_type, 0, 0);
7007 }
7008 /* If it is a pointer type recurse and keep testing */
7009 else if (type == TYPE_POINTER) {
7010 result = compatible_types(left->left, right->left);
7011 if (result) {
7012 result = new_type(qual_type, result, 0);
7013 }
7014 }
Eric Biederman90089602004-05-28 14:11:54 +00007015 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007016 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007017 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007018 result = left;
7019 }
7020 }
Eric Biederman90089602004-05-28 14:11:54 +00007021 /* test for union equality */
7022 else if (type == TYPE_UNION) {
7023 if (left->type_ident == right->type_ident) {
7024 result = left;
7025 }
7026 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007027 /* Test for equivalent functions */
7028 else if (type == TYPE_FUNCTION) {
7029 struct type *lf, *rf;
7030 lf = compatible_types(left->left, right->left);
7031 rf = compatible_types(left->right, right->right);
7032 if (lf && rf) {
7033 result = new_type(qual_type, lf, rf);
7034 }
7035 }
7036 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7037 else if (type == TYPE_PRODUCT) {
7038 struct type *lf, *rf;
7039 lf = compatible_types(left->left, right->left);
7040 rf = compatible_types(left->right, right->right);
7041 if (lf && rf) {
7042 result = new_type(qual_type, lf, rf);
7043 }
7044 }
7045 else {
7046 /* Nothing else is compatible */
7047 }
7048 return result;
7049}
7050
Eric Biederman90089602004-05-28 14:11:54 +00007051/* See if left is a equivalent to right or right is a union member of left */
7052static int is_subset_type(struct type *left, struct type *right)
7053{
7054 if (equiv_types(left, right)) {
7055 return 1;
7056 }
7057 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7058 struct type *member, *mnext;
7059 mnext = left->left;
7060 while(mnext) {
7061 member = mnext;
7062 mnext = 0;
7063 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7064 mnext = member->right;
7065 member = member->left;
7066 }
7067 if (is_subset_type( member, right)) {
7068 return 1;
7069 }
7070 }
7071 }
7072 return 0;
7073}
7074
Eric Biedermanb138ac82003-04-22 18:44:01 +00007075static struct type *compatible_ptrs(struct type *left, struct type *right)
7076{
7077 struct type *result;
7078 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7079 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7080 return 0;
7081 }
7082 result = compatible_types(left->left, right->left);
7083 if (result) {
7084 unsigned int qual_type;
7085 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7086 result = new_type(qual_type, result, 0);
7087 }
7088 return result;
7089
7090}
7091static struct triple *integral_promotion(
7092 struct compile_state *state, struct triple *def)
7093{
7094 struct type *type;
7095 type = def->type;
7096 /* As all operations are carried out in registers
7097 * the values are converted on load I just convert
7098 * logical type of the operand.
7099 */
7100 if (TYPE_INTEGER(type->type)) {
7101 unsigned int int_type;
7102 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007103 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007104 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007105 if (def->op != OP_LOAD) {
7106 def->type = new_type(int_type, 0, 0);
7107 }
7108 else {
Eric Biederman90089602004-05-28 14:11:54 +00007109 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007110 new_type(int_type, 0, 0), def, 0);
7111 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007112 }
7113 }
7114 return def;
7115}
7116
7117
7118static void arithmetic(struct compile_state *state, struct triple *def)
7119{
7120 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007121 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007122 }
7123}
7124
7125static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7126{
7127 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7128 error(state, def, "pointer or arithmetic type expected");
7129 }
7130}
7131
7132static int is_integral(struct triple *ins)
7133{
7134 return TYPE_INTEGER(ins->type->type);
7135}
7136
7137static void integral(struct compile_state *state, struct triple *def)
7138{
7139 if (!is_integral(def)) {
7140 error(state, 0, "integral type expected");
7141 }
7142}
7143
7144
7145static void bool(struct compile_state *state, struct triple *def)
7146{
7147 if (!TYPE_ARITHMETIC(def->type->type) &&
7148 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7149 error(state, 0, "arithmetic or pointer type expected");
7150 }
7151}
7152
7153static int is_signed(struct type *type)
7154{
Eric Biederman90089602004-05-28 14:11:54 +00007155 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7156 type = type->left;
7157 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007158 return !!TYPE_SIGNED(type->type);
7159}
Eric Biederman90089602004-05-28 14:11:54 +00007160static int is_compound_type(struct type *type)
7161{
7162 int is_compound;
7163 switch((type->type & TYPE_MASK)) {
7164 case TYPE_ARRAY:
7165 case TYPE_STRUCT:
7166 case TYPE_TUPLE:
7167 case TYPE_UNION:
7168 case TYPE_JOIN:
7169 is_compound = 1;
7170 break;
7171 default:
7172 is_compound = 0;
7173 break;
7174 }
7175 return is_compound;
7176}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007177
Eric Biederman0babc1c2003-05-09 02:39:00 +00007178/* Is this value located in a register otherwise it must be in memory */
7179static int is_in_reg(struct compile_state *state, struct triple *def)
7180{
7181 int in_reg;
7182 if (def->op == OP_ADECL) {
7183 in_reg = 1;
7184 }
7185 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7186 in_reg = 0;
7187 }
Eric Biederman90089602004-05-28 14:11:54 +00007188 else if (triple_is_part(state, def)) {
7189 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007190 }
7191 else {
Eric Biederman90089602004-05-28 14:11:54 +00007192 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007193 in_reg = -1;
7194 }
7195 return in_reg;
7196}
7197
Eric Biederman90089602004-05-28 14:11:54 +00007198/* Is this an auto or static variable location? Something that can
7199 * be assigned to. Otherwise it must must be a pure value, a temporary.
7200 */
7201static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007202{
7203 int ret;
7204 ret = 0;
7205 if (!def) {
7206 return 0;
7207 }
7208 if ((def->op == OP_ADECL) ||
7209 (def->op == OP_SDECL) ||
7210 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007211 (def->op == OP_BLOBCONST) ||
7212 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007213 ret = 1;
7214 }
Eric Biederman90089602004-05-28 14:11:54 +00007215 else if (triple_is_part(state, def)) {
7216 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007217 }
7218 return ret;
7219}
7220
Eric Biederman00443072003-06-24 12:34:45 +00007221static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007222{
7223 if (!def) {
7224 internal_error(state, def, "nothing where lvalue expected?");
7225 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007226 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007227 error(state, def, "lvalue expected");
7228 }
7229}
Eric Biederman00443072003-06-24 12:34:45 +00007230static void lvalue(struct compile_state *state, struct triple *def)
7231{
7232 clvalue(state, def);
7233 if (def->type->type & QUAL_CONST) {
7234 error(state, def, "modifable lvalue expected");
7235 }
7236}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007237
7238static int is_pointer(struct triple *def)
7239{
7240 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7241}
7242
7243static void pointer(struct compile_state *state, struct triple *def)
7244{
7245 if (!is_pointer(def)) {
7246 error(state, def, "pointer expected");
7247 }
7248}
7249
7250static struct triple *int_const(
7251 struct compile_state *state, struct type *type, ulong_t value)
7252{
7253 struct triple *result;
7254 switch(type->type & TYPE_MASK) {
7255 case TYPE_CHAR:
7256 case TYPE_INT: case TYPE_UINT:
7257 case TYPE_LONG: case TYPE_ULONG:
7258 break;
7259 default:
Eric Biederman90089602004-05-28 14:11:54 +00007260 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007261 }
7262 result = triple(state, OP_INTCONST, type, 0, 0);
7263 result->u.cval = value;
7264 return result;
7265}
7266
7267
Eric Biederman83b991a2003-10-11 06:20:25 +00007268static struct triple *read_expr(struct compile_state *state, struct triple *def);
7269
Eric Biederman0babc1c2003-05-09 02:39:00 +00007270static struct triple *do_mk_addr_expr(struct compile_state *state,
7271 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007272{
7273 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007274 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007275 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007276
Eric Biederman90089602004-05-28 14:11:54 +00007277 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007278
Eric Biederman90089602004-05-28 14:11:54 +00007279
Eric Biedermanb138ac82003-04-22 18:44:01 +00007280 result = 0;
7281 if (expr->op == OP_ADECL) {
7282 error(state, expr, "address of auto variables not supported");
7283 }
7284 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007285 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007286 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007287 result->u.cval = offset;
7288 }
7289 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007290 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007291 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007292 int_const(state, &ulong_type, offset));
7293 }
Eric Biederman90089602004-05-28 14:11:54 +00007294 else if (expr->op == OP_BLOBCONST) {
7295 FINISHME();
7296 internal_error(state, expr, "not yet implemented");
7297 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007298 else if (expr->op == OP_LIST) {
7299 error(state, 0, "Function addresses not supported");
7300 }
Eric Biederman90089602004-05-28 14:11:54 +00007301 else if (triple_is_part(state, expr)) {
7302 struct triple *part;
7303 part = expr;
7304 expr = MISC(expr, 0);
7305 if (part->op == OP_DOT) {
7306 offset += bits_to_bytes(
7307 field_offset(state, expr->type, part->u.field));
7308 }
7309 else if (part->op == OP_INDEX) {
7310 offset += bits_to_bytes(
7311 index_offset(state, expr->type, part->u.cval));
7312 }
7313 else {
7314 internal_error(state, part, "unhandled part type");
7315 }
7316 result = do_mk_addr_expr(state, expr, type, offset);
7317 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007318 if (!result) {
7319 internal_error(state, expr, "cannot take address of expression");
7320 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007321 return result;
7322}
7323
Eric Biederman0babc1c2003-05-09 02:39:00 +00007324static struct triple *mk_addr_expr(
7325 struct compile_state *state, struct triple *expr, ulong_t offset)
7326{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007327 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007328}
7329
Eric Biedermanb138ac82003-04-22 18:44:01 +00007330static struct triple *mk_deref_expr(
7331 struct compile_state *state, struct triple *expr)
7332{
7333 struct type *base_type;
7334 pointer(state, expr);
7335 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007336 return triple(state, OP_DEREF, base_type, expr, 0);
7337}
7338
Eric Biederman90089602004-05-28 14:11:54 +00007339/* lvalue conversions always apply except when certain operators
7340 * are applied. So I apply apply it when I know no more
7341 * operators will be applied.
7342 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007343static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007344{
Eric Biederman5cd81732004-03-11 15:01:31 +00007345 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007346 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7347 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007348 type = new_type(
7349 TYPE_POINTER | (def->type->type & QUAL_MASK),
7350 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007351 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007352 struct triple *addrconst;
7353 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7354 internal_error(state, def, "bad array constant");
7355 }
7356 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7357 MISC(addrconst, 0) = def;
7358 def = addrconst;
7359 }
7360 else {
Eric Biederman90089602004-05-28 14:11:54 +00007361 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007362 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007363 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007364 /* Transform a function to a pointer to it */
7365 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7366 def = mk_addr_expr(state, def, 0);
7367 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007368 return def;
7369}
7370
Eric Biederman0babc1c2003-05-09 02:39:00 +00007371static struct triple *deref_field(
7372 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7373{
7374 struct triple *result;
7375 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007376 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007377 if (!field) {
7378 internal_error(state, 0, "No field passed to deref_field");
7379 }
7380 result = 0;
7381 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007382 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7383 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007384 error(state, 0, "request for member %s in something not a struct or union",
7385 field->name);
7386 }
Eric Biederman03b59862003-06-24 14:27:37 +00007387 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007388 if ((type->type & STOR_MASK) == STOR_PERM) {
7389 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007390 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007391 result = do_mk_addr_expr(state, expr, member, offset);
7392 result = mk_deref_expr(state, result);
7393 }
7394 else {
7395 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007396 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007397 result->u.field = field;
7398 }
7399 return result;
7400}
7401
Eric Biederman90089602004-05-28 14:11:54 +00007402static struct triple *deref_index(
7403 struct compile_state *state, struct triple *expr, size_t index)
7404{
7405 struct triple *result;
7406 struct type *type, *member;
7407 ulong_t offset;
7408
7409 result = 0;
7410 type = expr->type;
7411 member = index_type(state, type, index);
7412
7413 if ((type->type & STOR_MASK) == STOR_PERM) {
7414 offset = bits_to_bytes(index_offset(state, type, index));
7415 result = do_mk_addr_expr(state, expr, member, offset);
7416 result = mk_deref_expr(state, result);
7417 }
7418 else {
7419 result = triple(state, OP_INDEX, member, expr, 0);
7420 result->u.cval = index;
7421 }
7422 return result;
7423}
7424
Eric Biedermanb138ac82003-04-22 18:44:01 +00007425static struct triple *read_expr(struct compile_state *state, struct triple *def)
7426{
7427 int op;
7428 if (!def) {
7429 return 0;
7430 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007431#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007432#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007433#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007434 /* Transform lvalues into something we can read */
7435 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007436 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007437 return def;
7438 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007439 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007440 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007441 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007442 if (def->op == OP_SDECL) {
7443 def = mk_addr_expr(state, def, 0);
7444 def = mk_deref_expr(state, def);
7445 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007446 op = OP_LOAD;
7447 }
Eric Biederman90089602004-05-28 14:11:54 +00007448 def = triple(state, op, def->type, def, 0);
7449 if (def->type->type & QUAL_VOLATILE) {
7450 def->id |= TRIPLE_FLAG_VOLATILE;
7451 }
7452 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007453}
7454
Eric Biedermane058a1e2003-07-12 01:21:31 +00007455int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007456 struct type *dest, struct type *rval)
7457{
7458 int compatible = 0;
7459 /* Both operands have arithmetic type */
7460 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7461 compatible = 1;
7462 }
7463 /* One operand is a pointer and the other is a pointer to void */
7464 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7465 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7466 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7467 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7468 compatible = 1;
7469 }
7470 /* If both types are the same without qualifiers we are good */
7471 else if (equiv_ptrs(dest, rval)) {
7472 compatible = 1;
7473 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007474 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007475 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007476 compatible = 1;
7477 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007478 return compatible;
7479}
7480
Eric Biedermane058a1e2003-07-12 01:21:31 +00007481static void write_compatible(struct compile_state *state,
7482 struct type *dest, struct type *rval)
7483{
7484 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007485 FILE *fp = state->errout;
7486 fprintf(fp, "dest: ");
7487 name_of(fp, dest);
7488 fprintf(fp,"\nrval: ");
7489 name_of(fp, rval);
7490 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007491 error(state, 0, "Incompatible types in assignment");
7492 }
7493}
7494
Eric Biedermane058a1e2003-07-12 01:21:31 +00007495static int is_init_compatible(struct compile_state *state,
7496 struct type *dest, struct type *rval)
7497{
7498 int compatible = 0;
7499 if (is_write_compatible(state, dest, rval)) {
7500 compatible = 1;
7501 }
7502 else if (equiv_types(dest, rval)) {
7503 compatible = 1;
7504 }
7505 return compatible;
7506}
7507
Eric Biedermanb138ac82003-04-22 18:44:01 +00007508static struct triple *write_expr(
7509 struct compile_state *state, struct triple *dest, struct triple *rval)
7510{
7511 struct triple *def;
7512 int op;
7513
7514 def = 0;
7515 if (!rval) {
7516 internal_error(state, 0, "missing rval");
7517 }
7518
7519 if (rval->op == OP_LIST) {
7520 internal_error(state, 0, "expression of type OP_LIST?");
7521 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007522 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007523 internal_error(state, 0, "writing to a non lvalue?");
7524 }
Eric Biederman00443072003-06-24 12:34:45 +00007525 if (dest->type->type & QUAL_CONST) {
7526 internal_error(state, 0, "modifable lvalue expexted");
7527 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007528
7529 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007530 if (!equiv_types(dest->type, rval->type)) {
7531 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7532 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007533
7534 /* Now figure out which assignment operator to use */
7535 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007536 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007537 def = triple(state, OP_WRITE, dest->type, rval, dest);
7538 if (MISC(def, 0) != dest) {
7539 internal_error(state, def, "huh?");
7540 }
7541 if (RHS(def, 0) != rval) {
7542 internal_error(state, def, "huh?");
7543 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007544 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007545 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007546 }
Eric Biederman90089602004-05-28 14:11:54 +00007547 if (def->type->type & QUAL_VOLATILE) {
7548 def->id |= TRIPLE_FLAG_VOLATILE;
7549 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007550 return def;
7551}
7552
7553static struct triple *init_expr(
7554 struct compile_state *state, struct triple *dest, struct triple *rval)
7555{
7556 struct triple *def;
7557
7558 def = 0;
7559 if (!rval) {
7560 internal_error(state, 0, "missing rval");
7561 }
7562 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7563 rval = read_expr(state, rval);
7564 def = write_expr(state, dest, rval);
7565 }
7566 else {
7567 /* Fill in the array size if necessary */
7568 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7569 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7570 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7571 dest->type->elements = rval->type->elements;
7572 }
7573 }
7574 if (!equiv_types(dest->type, rval->type)) {
7575 error(state, 0, "Incompatible types in inializer");
7576 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007577 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007578 insert_triple(state, dest, rval);
7579 rval->id |= TRIPLE_FLAG_FLATTENED;
7580 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007581 }
7582 return def;
7583}
7584
7585struct type *arithmetic_result(
7586 struct compile_state *state, struct triple *left, struct triple *right)
7587{
7588 struct type *type;
7589 /* Sanity checks to ensure I am working with arithmetic types */
7590 arithmetic(state, left);
7591 arithmetic(state, right);
7592 type = new_type(
7593 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007594 get_basic_type(left->type),
7595 get_basic_type(right->type)),
7596 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007597 return type;
7598}
7599
7600struct type *ptr_arithmetic_result(
7601 struct compile_state *state, struct triple *left, struct triple *right)
7602{
7603 struct type *type;
7604 /* Sanity checks to ensure I am working with the proper types */
7605 ptr_arithmetic(state, left);
7606 arithmetic(state, right);
7607 if (TYPE_ARITHMETIC(left->type->type) &&
7608 TYPE_ARITHMETIC(right->type->type)) {
7609 type = arithmetic_result(state, left, right);
7610 }
7611 else if (TYPE_PTR(left->type->type)) {
7612 type = left->type;
7613 }
7614 else {
7615 internal_error(state, 0, "huh?");
7616 type = 0;
7617 }
7618 return type;
7619}
7620
Eric Biedermanb138ac82003-04-22 18:44:01 +00007621/* boolean helper function */
7622
7623static struct triple *ltrue_expr(struct compile_state *state,
7624 struct triple *expr)
7625{
7626 switch(expr->op) {
7627 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7628 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7629 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7630 /* If the expression is already boolean do nothing */
7631 break;
7632 default:
7633 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7634 break;
7635 }
7636 return expr;
7637}
7638
7639static struct triple *lfalse_expr(struct compile_state *state,
7640 struct triple *expr)
7641{
7642 return triple(state, OP_LFALSE, &int_type, expr, 0);
7643}
7644
Eric Biederman90089602004-05-28 14:11:54 +00007645static struct triple *mkland_expr(
7646 struct compile_state *state,
7647 struct triple *left, struct triple *right)
7648{
7649 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007650 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007651
7652 /* Generate some intermediate triples */
7653 end = label(state);
7654 var = variable(state, &int_type);
7655
7656 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007657 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007658
7659 /* Jump if the value is false */
7660 jmp = branch(state, end,
7661 lfalse_expr(state, read_expr(state, var)));
7662 mid = label(state);
7663
7664 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007665 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007666
7667 /* An expression for the computed value */
7668 val = read_expr(state, var);
7669
7670 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007671 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007672
7673 return def;
7674}
7675
7676static struct triple *mklor_expr(
7677 struct compile_state *state,
7678 struct triple *left, struct triple *right)
7679{
7680 struct triple *def, *val, *var, *jmp, *mid, *end;
7681
7682 /* Generate some intermediate triples */
7683 end = label(state);
7684 var = variable(state, &int_type);
7685
7686 /* Store the left hand side value */
7687 left = write_expr(state, var, left);
7688
7689 /* Jump if the value is true */
7690 jmp = branch(state, end, read_expr(state, var));
7691 mid = label(state);
7692
7693 /* Store the right hand side value */
7694 right = write_expr(state, var, right);
7695
7696 /* An expression for the computed value*/
7697 val = read_expr(state, var);
7698
7699 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007700 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007701
7702 return def;
7703}
7704
7705static struct triple *mkcond_expr(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007706 struct compile_state *state,
7707 struct triple *test, struct triple *left, struct triple *right)
7708{
Eric Biederman90089602004-05-28 14:11:54 +00007709 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007710 struct type *result_type;
7711 unsigned int left_type, right_type;
7712 bool(state, test);
7713 left_type = left->type->type;
7714 right_type = right->type->type;
7715 result_type = 0;
7716 /* Both operands have arithmetic type */
7717 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7718 result_type = arithmetic_result(state, left, right);
7719 }
7720 /* Both operands have void type */
7721 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7722 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7723 result_type = &void_type;
7724 }
7725 /* pointers to the same type... */
7726 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7727 ;
7728 }
7729 /* Both operands are pointers and left is a pointer to void */
7730 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7731 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7732 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7733 result_type = right->type;
7734 }
7735 /* Both operands are pointers and right is a pointer to void */
7736 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7737 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7738 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7739 result_type = left->type;
7740 }
7741 if (!result_type) {
7742 error(state, 0, "Incompatible types in conditional expression");
7743 }
Eric Biederman90089602004-05-28 14:11:54 +00007744 /* Generate some intermediate triples */
7745 mid = label(state);
7746 end = label(state);
7747 var = variable(state, result_type);
7748
7749 /* Branch if the test is false */
7750 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7751 top = label(state);
7752
7753 /* Store the left hand side value */
7754 left = write_expr(state, var, left);
7755
7756 /* Branch to the end */
7757 jmp2 = branch(state, end, 0);
7758
7759 /* Store the right hand side value */
7760 right = write_expr(state, var, right);
7761
7762 /* An expression for the computed value */
7763 val = read_expr(state, var);
7764
7765 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007766 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007767
Eric Biedermanb138ac82003-04-22 18:44:01 +00007768 return def;
7769}
7770
7771
Eric Biederman0babc1c2003-05-09 02:39:00 +00007772static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007773{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007774#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007775#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007776#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007777 int count;
7778 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007779 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7780 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007781 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007782 else if (ins->op == OP_DEREF) {
7783 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007784 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007785 else if (ins->op == OP_VAL) {
7786 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007787 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007788 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007789 /* Don't figure the depth of a call just guess it is huge */
7790 count = 1000;
7791 }
7792 else {
7793 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007794 expr = triple_rhs(state, ins, 0);
7795 for(;expr; expr = triple_rhs(state, ins, expr)) {
7796 if (*expr) {
7797 int depth;
7798 depth = expr_depth(state, *expr);
7799 if (depth > count) {
7800 count = depth;
7801 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007802 }
7803 }
7804 }
7805 return count + 1;
7806}
7807
Eric Biederman0babc1c2003-05-09 02:39:00 +00007808static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007809 struct compile_state *state, struct triple *first, struct triple *ptr,
7810 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007811{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007812 struct rhs_vector {
7813 int depth;
7814 struct triple **ins;
7815 } vector[MAX_RHS];
7816 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007817 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007818 rhs = ptr->rhs;
7819 lhs = ptr->lhs;
7820 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007821 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007822 ptr->op, tops(ptr->op));
7823 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007824 /* Find the depth of the rhs elements */
7825 for(i = 0; i < rhs; i++) {
7826 vector[i].ins = &RHS(ptr, i);
7827 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007828 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007829 /* Selection sort the rhs */
7830 for(i = 0; i < rhs; i++) {
7831 int j, max = i;
7832 for(j = i + 1; j < rhs; j++ ) {
7833 if (vector[j].depth > vector[max].depth) {
7834 max = j;
7835 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007836 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007837 if (max != i) {
7838 struct rhs_vector tmp;
7839 tmp = vector[i];
7840 vector[i] = vector[max];
7841 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007842 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007843 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007844 /* Now flatten the rhs elements */
7845 for(i = 0; i < rhs; i++) {
7846 *vector[i].ins = flatten(state, first, *vector[i].ins);
7847 use_triple(*vector[i].ins, ptr);
7848 }
Eric Biederman90089602004-05-28 14:11:54 +00007849 if (lhs) {
7850 insert_triple(state, first, ptr);
7851 ptr->id |= TRIPLE_FLAG_FLATTENED;
7852 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7853
7854 /* Now flatten the lhs elements */
7855 for(i = 0; i < lhs; i++) {
7856 struct triple **ins = &LHS(ptr, i);
7857 *ins = flatten(state, first, *ins);
7858 use_triple(*ins, ptr);
7859 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007860 }
7861 return ptr;
7862}
7863
Eric Biederman90089602004-05-28 14:11:54 +00007864static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007865 struct compile_state *state, struct triple *first, struct triple *ptr)
7866{
Eric Biederman90089602004-05-28 14:11:54 +00007867 struct triple *head, *body, *val;
7868 head = RHS(ptr, 0);
7869 RHS(ptr, 0) = 0;
7870 val = head->prev;
7871 body = head->next;
7872 release_triple(state, head);
7873 release_triple(state, ptr);
7874 val->next = first;
7875 body->prev = first->prev;
7876 body->prev->next = body;
7877 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007878
Eric Biederman90089602004-05-28 14:11:54 +00007879 if (triple_is_cbranch(state, body->prev) ||
7880 triple_is_call(state, body->prev)) {
7881 unuse_triple(first, body->prev);
7882 use_triple(body, body->prev);
7883 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007884
Eric Biederman90089602004-05-28 14:11:54 +00007885 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7886 internal_error(state, val, "val not flattened?");
7887 }
7888
7889 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007890}
7891
Eric Biederman90089602004-05-28 14:11:54 +00007892
7893static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007894 struct compile_state *state, struct triple *first, struct triple *ptr)
7895{
Eric Biederman90089602004-05-28 14:11:54 +00007896 if (!triple_is_part(state, ptr)) {
7897 internal_error(state, ptr, "not a part");
7898 }
7899 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7900 internal_error(state, ptr, "unexpected args for: %d %s",
7901 ptr->op, tops(ptr->op));
7902 }
7903 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7904 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007905 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007906}
7907
7908static struct triple *flatten(
7909 struct compile_state *state, struct triple *first, struct triple *ptr)
7910{
7911 struct triple *orig_ptr;
7912 if (!ptr)
7913 return 0;
7914 do {
7915 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007916 /* Only flatten triples once */
7917 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7918 return ptr;
7919 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007920 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007921 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007922 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7923 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007924 break;
Eric Biederman90089602004-05-28 14:11:54 +00007925 case OP_PROG:
7926 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007927 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007928 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007929 ptr = flatten_generic(state, first, ptr, 1);
7930 insert_triple(state, first, ptr);
7931 ptr->id |= TRIPLE_FLAG_FLATTENED;
7932 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7933 if (ptr->next != ptr) {
7934 use_triple(ptr->next, ptr);
7935 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007936 break;
7937 case OP_READ:
7938 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007939 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7940 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007941 break;
Eric Biederman90089602004-05-28 14:11:54 +00007942 case OP_WRITE:
7943 ptr = flatten_generic(state, first, ptr, 1);
7944 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7945 use_triple(MISC(ptr, 0), ptr);
7946 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007947 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007948 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007949 break;
7950 case OP_CBRANCH:
7951 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7952 use_triple(RHS(ptr, 0), ptr);
7953 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007954 insert_triple(state, first, ptr);
7955 ptr->id |= TRIPLE_FLAG_FLATTENED;
7956 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007957 if (ptr->next != ptr) {
7958 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007959 }
7960 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007961 case OP_CALL:
7962 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7963 use_triple(MISC(ptr, 0), ptr);
7964 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007965 insert_triple(state, first, ptr);
7966 ptr->id |= TRIPLE_FLAG_FLATTENED;
7967 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007968 if (ptr->next != ptr) {
7969 use_triple(ptr->next, ptr);
7970 }
7971 break;
7972 case OP_RET:
7973 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7974 use_triple(RHS(ptr, 0), ptr);
7975 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007976 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007977 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007978 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007979 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007980 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007981 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007982 break;
7983 case OP_DEREF:
7984 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007985 ptr = RHS(ptr, 0);
7986 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007987 free_triple(state, orig_ptr);
7988 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007989 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007990 if (RHS(ptr, 0)->op == OP_DEREF) {
7991 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007992 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007993 base = MISC(ptr, 0);
7994 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007995 left = RHS(base, 0);
7996 ptr = triple(state, OP_ADD, left->type,
7997 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007998 int_const(state, &ulong_type, offset));
7999 free_triple(state, base);
8000 }
Eric Biederman90089602004-05-28 14:11:54 +00008001 else {
8002 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008003 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008004 break;
Eric Biederman90089602004-05-28 14:11:54 +00008005 case OP_INDEX:
8006 if (RHS(ptr, 0)->op == OP_DEREF) {
8007 struct triple *base, *left;
8008 ulong_t offset;
8009 base = MISC(ptr, 0);
8010 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8011 left = RHS(base, 0);
8012 ptr = triple(state, OP_ADD, left->type,
8013 read_expr(state, left),
8014 int_const(state, &long_type, offset));
8015 free_triple(state, base);
8016 }
8017 else {
8018 ptr = flatten_part(state, first, ptr);
8019 }
8020 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008021 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008022 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008023 use_triple(ptr, MISC(ptr, 0));
8024 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008025 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008026 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8027 use_triple(MISC(ptr, 0), ptr);
8028 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008029 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008030 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008031 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8032 use_triple(MISC(ptr, 0), ptr);
8033 insert_triple(state, first, ptr);
8034 ptr->id |= TRIPLE_FLAG_FLATTENED;
8035 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8036 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008037 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008038 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008039 break;
8040 default:
8041 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008042 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008043 break;
8044 }
8045 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008046 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008047 insert_triple(state, first, ptr);
8048 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008049 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008050 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008051 return ptr;
8052}
8053
8054static void release_expr(struct compile_state *state, struct triple *expr)
8055{
8056 struct triple *head;
8057 head = label(state);
8058 flatten(state, head, expr);
8059 while(head->next != head) {
8060 release_triple(state, head->next);
8061 }
8062 free_triple(state, head);
8063}
8064
8065static int replace_rhs_use(struct compile_state *state,
8066 struct triple *orig, struct triple *new, struct triple *use)
8067{
8068 struct triple **expr;
8069 int found;
8070 found = 0;
8071 expr = triple_rhs(state, use, 0);
8072 for(;expr; expr = triple_rhs(state, use, expr)) {
8073 if (*expr == orig) {
8074 *expr = new;
8075 found = 1;
8076 }
8077 }
8078 if (found) {
8079 unuse_triple(orig, use);
8080 use_triple(new, use);
8081 }
8082 return found;
8083}
8084
8085static int replace_lhs_use(struct compile_state *state,
8086 struct triple *orig, struct triple *new, struct triple *use)
8087{
8088 struct triple **expr;
8089 int found;
8090 found = 0;
8091 expr = triple_lhs(state, use, 0);
8092 for(;expr; expr = triple_lhs(state, use, expr)) {
8093 if (*expr == orig) {
8094 *expr = new;
8095 found = 1;
8096 }
8097 }
8098 if (found) {
8099 unuse_triple(orig, use);
8100 use_triple(new, use);
8101 }
8102 return found;
8103}
8104
Eric Biederman90089602004-05-28 14:11:54 +00008105static int replace_misc_use(struct compile_state *state,
8106 struct triple *orig, struct triple *new, struct triple *use)
8107{
8108 struct triple **expr;
8109 int found;
8110 found = 0;
8111 expr = triple_misc(state, use, 0);
8112 for(;expr; expr = triple_misc(state, use, expr)) {
8113 if (*expr == orig) {
8114 *expr = new;
8115 found = 1;
8116 }
8117 }
8118 if (found) {
8119 unuse_triple(orig, use);
8120 use_triple(new, use);
8121 }
8122 return found;
8123}
8124
8125static int replace_targ_use(struct compile_state *state,
8126 struct triple *orig, struct triple *new, struct triple *use)
8127{
8128 struct triple **expr;
8129 int found;
8130 found = 0;
8131 expr = triple_targ(state, use, 0);
8132 for(;expr; expr = triple_targ(state, use, expr)) {
8133 if (*expr == orig) {
8134 *expr = new;
8135 found = 1;
8136 }
8137 }
8138 if (found) {
8139 unuse_triple(orig, use);
8140 use_triple(new, use);
8141 }
8142 return found;
8143}
8144
8145static void replace_use(struct compile_state *state,
8146 struct triple *orig, struct triple *new, struct triple *use)
8147{
8148 int found;
8149 found = 0;
8150 found |= replace_rhs_use(state, orig, new, use);
8151 found |= replace_lhs_use(state, orig, new, use);
8152 found |= replace_misc_use(state, orig, new, use);
8153 found |= replace_targ_use(state, orig, new, use);
8154 if (!found) {
8155 internal_error(state, use, "use without use");
8156 }
8157}
8158
Eric Biedermanb138ac82003-04-22 18:44:01 +00008159static void propogate_use(struct compile_state *state,
8160 struct triple *orig, struct triple *new)
8161{
8162 struct triple_set *user, *next;
8163 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008164 /* Careful replace_use modifies the use chain and
8165 * removes use. So we must get a copy of the next
8166 * entry early.
8167 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008168 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008169 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008170 }
8171 if (orig->use) {
8172 internal_error(state, orig, "used after propogate_use");
8173 }
8174}
8175
8176/*
8177 * Code generators
8178 * ===========================
8179 */
8180
Eric Biederman90089602004-05-28 14:11:54 +00008181static struct triple *mk_cast_expr(
8182 struct compile_state *state, struct type *type, struct triple *expr)
8183{
8184 struct triple *def;
8185 def = read_expr(state, expr);
8186 def = triple(state, OP_CONVERT, type, def, 0);
8187 return def;
8188}
8189
Eric Biedermanb138ac82003-04-22 18:44:01 +00008190static struct triple *mk_add_expr(
8191 struct compile_state *state, struct triple *left, struct triple *right)
8192{
8193 struct type *result_type;
8194 /* Put pointer operands on the left */
8195 if (is_pointer(right)) {
8196 struct triple *tmp;
8197 tmp = left;
8198 left = right;
8199 right = tmp;
8200 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008201 left = read_expr(state, left);
8202 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008203 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008204 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008205 struct type *ptr_math;
8206 int op;
8207 if (is_signed(right->type)) {
8208 ptr_math = &long_type;
8209 op = OP_SMUL;
8210 } else {
8211 ptr_math = &ulong_type;
8212 op = OP_UMUL;
8213 }
8214 if (!equiv_types(right->type, ptr_math)) {
8215 right = mk_cast_expr(state, ptr_math, right);
8216 }
8217 right = triple(state, op, ptr_math, right,
8218 int_const(state, ptr_math,
8219 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008220 }
8221 return triple(state, OP_ADD, result_type, left, right);
8222}
8223
8224static struct triple *mk_sub_expr(
8225 struct compile_state *state, struct triple *left, struct triple *right)
8226{
8227 struct type *result_type;
8228 result_type = ptr_arithmetic_result(state, left, right);
8229 left = read_expr(state, left);
8230 right = read_expr(state, right);
8231 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008232 struct type *ptr_math;
8233 int op;
8234 if (is_signed(right->type)) {
8235 ptr_math = &long_type;
8236 op = OP_SMUL;
8237 } else {
8238 ptr_math = &ulong_type;
8239 op = OP_UMUL;
8240 }
8241 if (!equiv_types(right->type, ptr_math)) {
8242 right = mk_cast_expr(state, ptr_math, right);
8243 }
8244 right = triple(state, op, ptr_math, right,
8245 int_const(state, ptr_math,
8246 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008247 }
8248 return triple(state, OP_SUB, result_type, left, right);
8249}
8250
8251static struct triple *mk_pre_inc_expr(
8252 struct compile_state *state, struct triple *def)
8253{
8254 struct triple *val;
8255 lvalue(state, def);
8256 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8257 return triple(state, OP_VAL, def->type,
8258 write_expr(state, def, val),
8259 val);
8260}
8261
8262static struct triple *mk_pre_dec_expr(
8263 struct compile_state *state, struct triple *def)
8264{
8265 struct triple *val;
8266 lvalue(state, def);
8267 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8268 return triple(state, OP_VAL, def->type,
8269 write_expr(state, def, val),
8270 val);
8271}
8272
8273static struct triple *mk_post_inc_expr(
8274 struct compile_state *state, struct triple *def)
8275{
8276 struct triple *val;
8277 lvalue(state, def);
8278 val = read_expr(state, def);
8279 return triple(state, OP_VAL, def->type,
8280 write_expr(state, def,
8281 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8282 , val);
8283}
8284
8285static struct triple *mk_post_dec_expr(
8286 struct compile_state *state, struct triple *def)
8287{
8288 struct triple *val;
8289 lvalue(state, def);
8290 val = read_expr(state, def);
8291 return triple(state, OP_VAL, def->type,
8292 write_expr(state, def,
8293 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8294 , val);
8295}
8296
8297static struct triple *mk_subscript_expr(
8298 struct compile_state *state, struct triple *left, struct triple *right)
8299{
8300 left = read_expr(state, left);
8301 right = read_expr(state, right);
8302 if (!is_pointer(left) && !is_pointer(right)) {
8303 error(state, left, "subscripted value is not a pointer");
8304 }
8305 return mk_deref_expr(state, mk_add_expr(state, left, right));
8306}
8307
Eric Biedermane058a1e2003-07-12 01:21:31 +00008308
Eric Biedermanb138ac82003-04-22 18:44:01 +00008309/*
8310 * Compile time evaluation
8311 * ===========================
8312 */
8313static int is_const(struct triple *ins)
8314{
8315 return IS_CONST_OP(ins->op);
8316}
8317
Eric Biederman83b991a2003-10-11 06:20:25 +00008318static int is_simple_const(struct triple *ins)
8319{
Eric Biederman90089602004-05-28 14:11:54 +00008320 /* Is this a constant that u.cval has the value.
8321 * Or equivalently is this a constant that read_const
8322 * works on.
8323 * So far only OP_INTCONST qualifies.
8324 */
8325 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008326}
8327
Eric Biedermanb138ac82003-04-22 18:44:01 +00008328static int constants_equal(struct compile_state *state,
8329 struct triple *left, struct triple *right)
8330{
8331 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008332 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8333 equal = 0;
8334 }
8335 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008336 equal = 0;
8337 }
8338 else if (left->op != right->op) {
8339 equal = 0;
8340 }
8341 else if (!equiv_types(left->type, right->type)) {
8342 equal = 0;
8343 }
8344 else {
8345 equal = 0;
8346 switch(left->op) {
8347 case OP_INTCONST:
8348 if (left->u.cval == right->u.cval) {
8349 equal = 1;
8350 }
8351 break;
8352 case OP_BLOBCONST:
8353 {
Eric Biederman90089602004-05-28 14:11:54 +00008354 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008355 lsize = size_of(state, left->type);
8356 rsize = size_of(state, right->type);
8357 if (lsize != rsize) {
8358 break;
8359 }
Eric Biederman90089602004-05-28 14:11:54 +00008360 bytes = bits_to_bytes(lsize);
8361 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008362 equal = 1;
8363 }
8364 break;
8365 }
8366 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008367 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008368 (left->u.cval == right->u.cval)) {
8369 equal = 1;
8370 }
8371 break;
8372 default:
8373 internal_error(state, left, "uknown constant type");
8374 break;
8375 }
8376 }
8377 return equal;
8378}
8379
8380static int is_zero(struct triple *ins)
8381{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008382 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008383}
8384
8385static int is_one(struct triple *ins)
8386{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008387 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008388}
8389
Stefan Reinauer50542a82007-10-24 11:14:14 +00008390#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008391static long_t bit_count(ulong_t value)
8392{
8393 int count;
8394 int i;
8395 count = 0;
8396 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8397 ulong_t mask;
8398 mask = 1;
8399 mask <<= i;
8400 if (value & mask) {
8401 count++;
8402 }
8403 }
8404 return count;
8405
8406}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008407#endif
8408
Eric Biedermanb138ac82003-04-22 18:44:01 +00008409static long_t bsr(ulong_t value)
8410{
8411 int i;
8412 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8413 ulong_t mask;
8414 mask = 1;
8415 mask <<= i;
8416 if (value & mask) {
8417 return i;
8418 }
8419 }
8420 return -1;
8421}
8422
8423static long_t bsf(ulong_t value)
8424{
8425 int i;
8426 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8427 ulong_t mask;
8428 mask = 1;
8429 mask <<= 1;
8430 if (value & mask) {
8431 return i;
8432 }
8433 }
8434 return -1;
8435}
8436
Eric Biedermancb364952004-11-15 10:46:44 +00008437static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008438{
8439 return bsr(value);
8440}
8441
8442static long_t tlog2(struct triple *ins)
8443{
Eric Biedermancb364952004-11-15 10:46:44 +00008444 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008445}
8446
8447static int is_pow2(struct triple *ins)
8448{
8449 ulong_t value, mask;
8450 long_t log;
8451 if (!is_const(ins)) {
8452 return 0;
8453 }
8454 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008455 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008456 if (log == -1) {
8457 return 0;
8458 }
8459 mask = 1;
8460 mask <<= log;
8461 return ((value & mask) == value);
8462}
8463
8464static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008465 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008466{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008467 switch(rhs->type->type &TYPE_MASK) {
8468 case TYPE_CHAR:
8469 case TYPE_SHORT:
8470 case TYPE_INT:
8471 case TYPE_LONG:
8472 case TYPE_UCHAR:
8473 case TYPE_USHORT:
8474 case TYPE_UINT:
8475 case TYPE_ULONG:
8476 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008477 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008478 break;
8479 default:
Eric Biederman90089602004-05-28 14:11:54 +00008480 fprintf(state->errout, "type: ");
8481 name_of(state->errout, rhs->type);
8482 fprintf(state->errout, "\n");
8483 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008484 break;
8485 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008486 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008487 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008488 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008489 return rhs->u.cval;
8490}
8491
Eric Biederman5ade04a2003-10-22 04:03:46 +00008492static long_t read_sconst(struct compile_state *state,
8493 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008494{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008495 return (long_t)(rhs->u.cval);
8496}
8497
Eric Biederman5ade04a2003-10-22 04:03:46 +00008498int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8499{
8500 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008501 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008502 }
8503 return !is_zero(rhs);
8504}
8505
8506int const_eq(struct compile_state *state, struct triple *ins,
8507 struct triple *left, struct triple *right)
8508{
8509 int result;
8510 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008511 internal_warning(state, ins, "non const passed to const_eq");
8512 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008513 }
8514 else if (left == right) {
8515 result = 1;
8516 }
8517 else if (is_simple_const(left) && is_simple_const(right)) {
8518 ulong_t lval, rval;
8519 lval = read_const(state, ins, left);
8520 rval = read_const(state, ins, right);
8521 result = (lval == rval);
8522 }
8523 else if ((left->op == OP_ADDRCONST) &&
8524 (right->op == OP_ADDRCONST)) {
8525 result = (MISC(left, 0) == MISC(right, 0)) &&
8526 (left->u.cval == right->u.cval);
8527 }
8528 else {
Eric Biederman90089602004-05-28 14:11:54 +00008529 internal_warning(state, ins, "incomparable constants passed to const_eq");
8530 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008531 }
8532 return result;
8533
8534}
8535
8536int const_ucmp(struct compile_state *state, struct triple *ins,
8537 struct triple *left, struct triple *right)
8538{
8539 int result;
8540 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008541 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008542 result = -2;
8543 }
8544 else if (left == right) {
8545 result = 0;
8546 }
8547 else if (is_simple_const(left) && is_simple_const(right)) {
8548 ulong_t lval, rval;
8549 lval = read_const(state, ins, left);
8550 rval = read_const(state, ins, right);
8551 result = 0;
8552 if (lval > rval) {
8553 result = 1;
8554 } else if (rval > lval) {
8555 result = -1;
8556 }
8557 }
8558 else if ((left->op == OP_ADDRCONST) &&
8559 (right->op == OP_ADDRCONST) &&
8560 (MISC(left, 0) == MISC(right, 0))) {
8561 result = 0;
8562 if (left->u.cval > right->u.cval) {
8563 result = 1;
8564 } else if (left->u.cval < right->u.cval) {
8565 result = -1;
8566 }
8567 }
8568 else {
Eric Biederman90089602004-05-28 14:11:54 +00008569 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008570 result = -2;
8571 }
8572 return result;
8573}
8574
8575int const_scmp(struct compile_state *state, struct triple *ins,
8576 struct triple *left, struct triple *right)
8577{
8578 int result;
8579 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008580 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008581 result = -2;
8582 }
8583 else if (left == right) {
8584 result = 0;
8585 }
8586 else if (is_simple_const(left) && is_simple_const(right)) {
8587 long_t lval, rval;
8588 lval = read_sconst(state, ins, left);
8589 rval = read_sconst(state, ins, right);
8590 result = 0;
8591 if (lval > rval) {
8592 result = 1;
8593 } else if (rval > lval) {
8594 result = -1;
8595 }
8596 }
8597 else {
Eric Biederman90089602004-05-28 14:11:54 +00008598 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008599 result = -2;
8600 }
8601 return result;
8602}
8603
Eric Biedermanb138ac82003-04-22 18:44:01 +00008604static void unuse_rhs(struct compile_state *state, struct triple *ins)
8605{
8606 struct triple **expr;
8607 expr = triple_rhs(state, ins, 0);
8608 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008609 if (*expr) {
8610 unuse_triple(*expr, ins);
8611 *expr = 0;
8612 }
8613 }
8614}
8615
8616static void unuse_lhs(struct compile_state *state, struct triple *ins)
8617{
8618 struct triple **expr;
8619 expr = triple_lhs(state, ins, 0);
8620 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008621 unuse_triple(*expr, ins);
8622 *expr = 0;
8623 }
8624}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008625
Stefan Reinauer50542a82007-10-24 11:14:14 +00008626#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008627static void unuse_misc(struct compile_state *state, struct triple *ins)
8628{
8629 struct triple **expr;
8630 expr = triple_misc(state, ins, 0);
8631 for(;expr;expr = triple_misc(state, ins, expr)) {
8632 unuse_triple(*expr, ins);
8633 *expr = 0;
8634 }
8635}
8636
8637static void unuse_targ(struct compile_state *state, struct triple *ins)
8638{
8639 int i;
8640 struct triple **slot;
8641 slot = &TARG(ins, 0);
8642 for(i = 0; i < ins->targ; i++) {
8643 unuse_triple(slot[i], ins);
8644 slot[i] = 0;
8645 }
8646}
8647
Eric Biedermanb138ac82003-04-22 18:44:01 +00008648static void check_lhs(struct compile_state *state, struct triple *ins)
8649{
8650 struct triple **expr;
8651 expr = triple_lhs(state, ins, 0);
8652 for(;expr;expr = triple_lhs(state, ins, expr)) {
8653 internal_error(state, ins, "unexpected lhs");
8654 }
8655
8656}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008657#endif
Eric Biederman90089602004-05-28 14:11:54 +00008658
8659static void check_misc(struct compile_state *state, struct triple *ins)
8660{
8661 struct triple **expr;
8662 expr = triple_misc(state, ins, 0);
8663 for(;expr;expr = triple_misc(state, ins, expr)) {
8664 if (*expr) {
8665 internal_error(state, ins, "unexpected misc");
8666 }
8667 }
8668}
8669
Eric Biedermanb138ac82003-04-22 18:44:01 +00008670static void check_targ(struct compile_state *state, struct triple *ins)
8671{
8672 struct triple **expr;
8673 expr = triple_targ(state, ins, 0);
8674 for(;expr;expr = triple_targ(state, ins, expr)) {
8675 internal_error(state, ins, "unexpected targ");
8676 }
8677}
8678
8679static void wipe_ins(struct compile_state *state, struct triple *ins)
8680{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008681 /* Becareful which instructions you replace the wiped
8682 * instruction with, as there are not enough slots
8683 * in all instructions to hold all others.
8684 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008685 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008686 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008687 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008688 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008689 ins->lhs = 0;
8690 ins->rhs = 0;
8691 ins->misc = 0;
8692 ins->targ = 0;
8693}
8694
Stefan Reinauer50542a82007-10-24 11:14:14 +00008695#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008696static void wipe_branch(struct compile_state *state, struct triple *ins)
8697{
8698 /* Becareful which instructions you replace the wiped
8699 * instruction with, as there are not enough slots
8700 * in all instructions to hold all others.
8701 */
8702 unuse_rhs(state, ins);
8703 unuse_lhs(state, ins);
8704 unuse_misc(state, ins);
8705 unuse_targ(state, ins);
8706 ins->lhs = 0;
8707 ins->rhs = 0;
8708 ins->misc = 0;
8709 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008710}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008711#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008712
8713static void mkcopy(struct compile_state *state,
8714 struct triple *ins, struct triple *rhs)
8715{
Eric Biederman83b991a2003-10-11 06:20:25 +00008716 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008717 if (!equiv_types(ins->type, rhs->type)) {
8718 FILE *fp = state->errout;
8719 fprintf(fp, "src type: ");
8720 name_of(fp, rhs->type);
8721 fprintf(fp, "\ndst type: ");
8722 name_of(fp, ins->type);
8723 fprintf(fp, "\n");
8724 internal_error(state, ins, "mkcopy type mismatch");
8725 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008726 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008727 wipe_ins(state, ins);
8728 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008729 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008730 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008731 RHS(ins, 0) = rhs;
8732 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008733}
8734
8735static void mkconst(struct compile_state *state,
8736 struct triple *ins, ulong_t value)
8737{
8738 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008739 fprintf(state->errout, "type: ");
8740 name_of(state->errout, ins->type);
8741 fprintf(state->errout, "\n");
8742 internal_error(state, ins, "unknown type to make constant value: %ld",
8743 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008744 }
8745 wipe_ins(state, ins);
8746 ins->op = OP_INTCONST;
8747 ins->u.cval = value;
8748}
8749
8750static void mkaddr_const(struct compile_state *state,
8751 struct triple *ins, struct triple *sdecl, ulong_t value)
8752{
Eric Biederman90089602004-05-28 14:11:54 +00008753 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008754 internal_error(state, ins, "bad base for addrconst");
8755 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008756 wipe_ins(state, ins);
8757 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008758 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008759 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008760 ins->u.cval = value;
8761 use_triple(sdecl, ins);
8762}
8763
Eric Biederman90089602004-05-28 14:11:54 +00008764#if DEBUG_DECOMPOSE_PRINT_TUPLES
8765static void print_tuple(struct compile_state *state,
8766 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008767{
Eric Biederman90089602004-05-28 14:11:54 +00008768 FILE *fp = state->dbgout;
8769 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8770 name_of(fp, tuple->type);
8771 if (tuple->lhs > 0) {
8772 fprintf(fp, " lhs: ");
8773 name_of(fp, LHS(tuple, 0)->type);
8774 }
8775 fprintf(fp, "\n");
8776
8777}
8778#endif
8779
8780static struct triple *decompose_with_tuple(struct compile_state *state,
8781 struct triple *ins, struct triple *tuple)
8782{
8783 struct triple *next;
8784 next = ins->next;
8785 flatten(state, next, tuple);
8786#if DEBUG_DECOMPOSE_PRINT_TUPLES
8787 print_tuple(state, ins, tuple);
8788#endif
8789
8790 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8791 struct triple *tmp;
8792 if (tuple->lhs != 1) {
8793 internal_error(state, tuple, "plain type in multiple registers?");
8794 }
8795 tmp = LHS(tuple, 0);
8796 release_triple(state, tuple);
8797 tuple = tmp;
8798 }
8799
8800 propogate_use(state, ins, tuple);
8801 release_triple(state, ins);
8802
8803 return next;
8804}
8805
8806static struct triple *decompose_unknownval(struct compile_state *state,
8807 struct triple *ins)
8808{
8809 struct triple *tuple;
8810 ulong_t i;
8811
8812#if DEBUG_DECOMPOSE_HIRES
8813 FILE *fp = state->dbgout;
8814 fprintf(fp, "unknown type: ");
8815 name_of(fp, ins->type);
8816 fprintf(fp, "\n");
8817#endif
8818
8819 get_occurance(ins->occurance);
8820 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8821 ins->occurance);
8822
8823 for(i = 0; i < tuple->lhs; i++) {
8824 struct type *piece_type;
8825 struct triple *unknown;
8826
8827 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8828 get_occurance(tuple->occurance);
8829 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8830 tuple->occurance);
8831 LHS(tuple, i) = unknown;
8832 }
8833 return decompose_with_tuple(state, ins, tuple);
8834}
8835
8836
8837static struct triple *decompose_read(struct compile_state *state,
8838 struct triple *ins)
8839{
8840 struct triple *tuple, *lval;
8841 ulong_t i;
8842
8843 lval = RHS(ins, 0);
8844
8845 if (lval->op == OP_PIECE) {
8846 return ins->next;
8847 }
8848 get_occurance(ins->occurance);
8849 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8850 ins->occurance);
8851
8852 if ((tuple->lhs != lval->lhs) &&
8853 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
8854 {
8855 internal_error(state, ins, "lhs size inconsistency?");
8856 }
8857 for(i = 0; i < tuple->lhs; i++) {
8858 struct triple *piece, *read, *bitref;
8859 if ((i != 0) || !triple_is_def(state, lval)) {
8860 piece = LHS(lval, i);
8861 } else {
8862 piece = lval;
8863 }
8864
8865 /* See if the piece is really a bitref */
8866 bitref = 0;
8867 if (piece->op == OP_BITREF) {
8868 bitref = piece;
8869 piece = RHS(bitref, 0);
8870 }
8871
8872 get_occurance(tuple->occurance);
8873 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8874 tuple->occurance);
8875 RHS(read, 0) = piece;
8876
8877 if (bitref) {
8878 struct triple *extract;
8879 int op;
8880 if (is_signed(bitref->type->left)) {
8881 op = OP_SEXTRACT;
8882 } else {
8883 op = OP_UEXTRACT;
8884 }
8885 get_occurance(tuple->occurance);
8886 extract = alloc_triple(state, op, bitref->type, -1, -1,
8887 tuple->occurance);
8888 RHS(extract, 0) = read;
8889 extract->u.bitfield.size = bitref->u.bitfield.size;
8890 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8891
8892 read = extract;
8893 }
8894
8895 LHS(tuple, i) = read;
8896 }
8897 return decompose_with_tuple(state, ins, tuple);
8898}
8899
8900static struct triple *decompose_write(struct compile_state *state,
8901 struct triple *ins)
8902{
8903 struct triple *tuple, *lval, *val;
8904 ulong_t i;
8905
8906 lval = MISC(ins, 0);
8907 val = RHS(ins, 0);
8908 get_occurance(ins->occurance);
8909 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8910 ins->occurance);
8911
8912 if ((tuple->lhs != lval->lhs) &&
8913 (!triple_is_def(state, lval) || tuple->lhs != 1))
8914 {
8915 internal_error(state, ins, "lhs size inconsistency?");
8916 }
8917 for(i = 0; i < tuple->lhs; i++) {
8918 struct triple *piece, *write, *pval, *bitref;
8919 if ((i != 0) || !triple_is_def(state, lval)) {
8920 piece = LHS(lval, i);
8921 } else {
8922 piece = lval;
8923 }
8924 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8925 pval = val;
8926 }
8927 else {
8928 if (i > val->lhs) {
8929 internal_error(state, ins, "lhs size inconsistency?");
8930 }
8931 pval = LHS(val, i);
8932 }
8933
8934 /* See if the piece is really a bitref */
8935 bitref = 0;
8936 if (piece->op == OP_BITREF) {
8937 struct triple *read, *deposit;
8938 bitref = piece;
8939 piece = RHS(bitref, 0);
8940
8941 /* Read the destination register */
8942 get_occurance(tuple->occurance);
8943 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8944 tuple->occurance);
8945 RHS(read, 0) = piece;
8946
8947 /* Deposit the new bitfield value */
8948 get_occurance(tuple->occurance);
8949 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8950 tuple->occurance);
8951 RHS(deposit, 0) = read;
8952 RHS(deposit, 1) = pval;
8953 deposit->u.bitfield.size = bitref->u.bitfield.size;
8954 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8955
8956 /* Now write the newly generated value */
8957 pval = deposit;
8958 }
8959
8960 get_occurance(tuple->occurance);
8961 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
8962 tuple->occurance);
8963 MISC(write, 0) = piece;
8964 RHS(write, 0) = pval;
8965 LHS(tuple, i) = write;
8966 }
8967 return decompose_with_tuple(state, ins, tuple);
8968}
8969
8970struct decompose_load_info {
8971 struct occurance *occurance;
8972 struct triple *lval;
8973 struct triple *tuple;
8974};
8975static void decompose_load_cb(struct compile_state *state,
8976 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8977{
8978 struct decompose_load_info *info = arg;
8979 struct triple *load;
8980
8981 if (reg_offset > info->tuple->lhs) {
8982 internal_error(state, info->tuple, "lhs to small?");
8983 }
8984 get_occurance(info->occurance);
8985 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8986 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8987 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8988}
8989
8990static struct triple *decompose_load(struct compile_state *state,
8991 struct triple *ins)
8992{
8993 struct triple *tuple;
8994 struct decompose_load_info info;
8995
8996 if (!is_compound_type(ins->type)) {
8997 return ins->next;
8998 }
8999 get_occurance(ins->occurance);
9000 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9001 ins->occurance);
9002
9003 info.occurance = ins->occurance;
9004 info.lval = RHS(ins, 0);
9005 info.tuple = tuple;
9006 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
9007
9008 return decompose_with_tuple(state, ins, tuple);
9009}
9010
9011
9012struct decompose_store_info {
9013 struct occurance *occurance;
9014 struct triple *lval;
9015 struct triple *val;
9016 struct triple *tuple;
9017};
9018static void decompose_store_cb(struct compile_state *state,
9019 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9020{
9021 struct decompose_store_info *info = arg;
9022 struct triple *store;
9023
9024 if (reg_offset > info->tuple->lhs) {
9025 internal_error(state, info->tuple, "lhs to small?");
9026 }
9027 get_occurance(info->occurance);
9028 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9029 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9030 RHS(store, 1) = LHS(info->val, reg_offset);
9031 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9032}
9033
9034static struct triple *decompose_store(struct compile_state *state,
9035 struct triple *ins)
9036{
9037 struct triple *tuple;
9038 struct decompose_store_info info;
9039
9040 if (!is_compound_type(ins->type)) {
9041 return ins->next;
9042 }
9043 get_occurance(ins->occurance);
9044 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9045 ins->occurance);
9046
9047 info.occurance = ins->occurance;
9048 info.lval = RHS(ins, 0);
9049 info.val = RHS(ins, 1);
9050 info.tuple = tuple;
9051 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9052
9053 return decompose_with_tuple(state, ins, tuple);
9054}
9055
9056static struct triple *decompose_dot(struct compile_state *state,
9057 struct triple *ins)
9058{
9059 struct triple *tuple, *lval;
9060 struct type *type;
9061 size_t reg_offset;
9062 int i, idx;
9063
9064 lval = MISC(ins, 0);
9065 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9066 idx = reg_offset/REG_SIZEOF_REG;
9067 type = field_type(state, lval->type, ins->u.field);
9068#if DEBUG_DECOMPOSE_HIRES
9069 {
9070 FILE *fp = state->dbgout;
9071 fprintf(fp, "field type: ");
9072 name_of(fp, type);
9073 fprintf(fp, "\n");
9074 }
9075#endif
9076
9077 get_occurance(ins->occurance);
9078 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9079 ins->occurance);
9080
9081 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9082 (tuple->lhs != 1))
9083 {
9084 internal_error(state, ins, "multi register bitfield?");
9085 }
9086
9087 for(i = 0; i < tuple->lhs; i++, idx++) {
9088 struct triple *piece;
9089 if (!triple_is_def(state, lval)) {
9090 if (idx > lval->lhs) {
9091 internal_error(state, ins, "inconsistent lhs count");
9092 }
9093 piece = LHS(lval, idx);
9094 } else {
9095 if (idx != 0) {
9096 internal_error(state, ins, "bad reg_offset into def");
9097 }
9098 if (i != 0) {
9099 internal_error(state, ins, "bad reg count from def");
9100 }
9101 piece = lval;
9102 }
9103
9104 /* Remember the offset of the bitfield */
9105 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9106 get_occurance(ins->occurance);
9107 piece = build_triple(state, OP_BITREF, type, piece, 0,
9108 ins->occurance);
9109 piece->u.bitfield.size = size_of(state, type);
9110 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9111 }
9112 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
9113 internal_error(state, ins,
9114 "request for a nonbitfield sub register?");
9115 }
9116
9117 LHS(tuple, i) = piece;
9118 }
9119
9120 return decompose_with_tuple(state, ins, tuple);
9121}
9122
9123static struct triple *decompose_index(struct compile_state *state,
9124 struct triple *ins)
9125{
9126 struct triple *tuple, *lval;
9127 struct type *type;
9128 int i, idx;
9129
9130 lval = MISC(ins, 0);
9131 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9132 type = index_type(state, lval->type, ins->u.cval);
9133#if DEBUG_DECOMPOSE_HIRES
9134{
9135 FILE *fp = state->dbgout;
9136 fprintf(fp, "index type: ");
9137 name_of(fp, type);
9138 fprintf(fp, "\n");
9139}
9140#endif
9141
9142 get_occurance(ins->occurance);
9143 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9144 ins->occurance);
9145
9146 for(i = 0; i < tuple->lhs; i++, idx++) {
9147 struct triple *piece;
9148 if (!triple_is_def(state, lval)) {
9149 if (idx > lval->lhs) {
9150 internal_error(state, ins, "inconsistent lhs count");
9151 }
9152 piece = LHS(lval, idx);
9153 } else {
9154 if (idx != 0) {
9155 internal_error(state, ins, "bad reg_offset into def");
9156 }
9157 if (i != 0) {
9158 internal_error(state, ins, "bad reg count from def");
9159 }
9160 piece = lval;
9161 }
9162 LHS(tuple, i) = piece;
9163 }
9164
9165 return decompose_with_tuple(state, ins, tuple);
9166}
9167
9168static void decompose_compound_types(struct compile_state *state)
9169{
9170 struct triple *ins, *next, *first;
9171 FILE *fp;
9172 fp = state->dbgout;
Eric Biederman83b991a2003-10-11 06:20:25 +00009173 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009174 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009175
9176 /* Pass one expand compound values into pseudo registers.
9177 */
9178 next = first;
9179 do {
9180 ins = next;
9181 next = ins->next;
9182 switch(ins->op) {
9183 case OP_UNKNOWNVAL:
9184 next = decompose_unknownval(state, ins);
9185 break;
9186
9187 case OP_READ:
9188 next = decompose_read(state, ins);
9189 break;
9190
9191 case OP_WRITE:
9192 next = decompose_write(state, ins);
9193 break;
9194
9195
9196 /* Be very careful with the load/store logic. These
9197 * operations must convert from the in register layout
9198 * to the in memory layout, which is nontrivial.
9199 */
9200 case OP_LOAD:
9201 next = decompose_load(state, ins);
9202 break;
9203 case OP_STORE:
9204 next = decompose_store(state, ins);
9205 break;
9206
9207 case OP_DOT:
9208 next = decompose_dot(state, ins);
9209 break;
9210 case OP_INDEX:
9211 next = decompose_index(state, ins);
9212 break;
9213
9214 }
9215#if DEBUG_DECOMPOSE_HIRES
9216 fprintf(fp, "decompose next: %p \n", next);
9217 fflush(fp);
9218 fprintf(fp, "next->op: %d %s\n",
9219 next->op, tops(next->op));
9220 /* High resolution debugging mode */
9221 print_triples(state);
9222#endif
9223 } while (next != first);
9224
9225 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009226 */
9227 ins = first;
9228 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009229 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009230 if (ins->op == OP_TUPLE) {
9231 if (ins->use) {
9232 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009233 }
Eric Biederman90089602004-05-28 14:11:54 +00009234 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009235 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009236 }
Eric Biederman90089602004-05-28 14:11:54 +00009237 }
9238 ins = next;
9239 } while(ins != first);
9240 ins = first;
9241 do {
9242 next = ins->next;
9243 if (ins->op == OP_BITREF) {
9244 if (ins->use) {
9245 internal_error(state, ins, "bitref used");
9246 }
9247 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009248 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009249 }
9250 }
9251 ins = next;
9252 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009253
Eric Biederman0babc1c2003-05-09 02:39:00 +00009254 /* Pass three verify the state and set ->id to 0.
9255 */
Eric Biederman90089602004-05-28 14:11:54 +00009256 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009257 do {
Eric Biederman90089602004-05-28 14:11:54 +00009258 ins = next;
9259 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009260 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009261 if (triple_stores_block(state, ins)) {
9262 ins->u.block = 0;
9263 }
9264 if (triple_is_def(state, ins)) {
9265 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9266 internal_error(state, ins, "multi register value remains?");
9267 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009268 }
9269 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009270 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009271 }
Eric Biederman90089602004-05-28 14:11:54 +00009272 if (ins->op == OP_INDEX) {
9273 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009274 }
Eric Biederman90089602004-05-28 14:11:54 +00009275 if (ins->op == OP_BITREF) {
9276 internal_error(state, ins, "OP_BITREF remains?");
9277 }
9278 if (ins->op == OP_TUPLE) {
9279 internal_error(state, ins, "OP_TUPLE remains?");
9280 }
9281 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009282}
9283
Eric Biedermanb138ac82003-04-22 18:44:01 +00009284/* For those operations that cannot be simplified */
9285static void simplify_noop(struct compile_state *state, struct triple *ins)
9286{
9287 return;
9288}
9289
9290static void simplify_smul(struct compile_state *state, struct triple *ins)
9291{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009292 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009293 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009294 tmp = RHS(ins, 0);
9295 RHS(ins, 0) = RHS(ins, 1);
9296 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009297 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009298 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009299 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009300 left = read_sconst(state, ins, RHS(ins, 0));
9301 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009302 mkconst(state, ins, left * right);
9303 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009304 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009305 mkconst(state, ins, 0);
9306 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009307 else if (is_one(RHS(ins, 1))) {
9308 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009309 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009310 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009311 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009312 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009313 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009314 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009315 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009316 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009317 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009318 }
9319}
9320
9321static void simplify_umul(struct compile_state *state, struct triple *ins)
9322{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009323 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009324 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009325 tmp = RHS(ins, 0);
9326 RHS(ins, 0) = RHS(ins, 1);
9327 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009328 }
Eric Biederman90089602004-05-28 14:11:54 +00009329 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009330 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009331 left = read_const(state, ins, RHS(ins, 0));
9332 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009333 mkconst(state, ins, left * right);
9334 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009335 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009336 mkconst(state, ins, 0);
9337 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009338 else if (is_one(RHS(ins, 1))) {
9339 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009340 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009341 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009342 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009343 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009344 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009345 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009346 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009347 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009348 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009349 }
9350}
9351
9352static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9353{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009354 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009355 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009356 left = read_sconst(state, ins, RHS(ins, 0));
9357 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009358 mkconst(state, ins, left / right);
9359 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009360 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009361 mkconst(state, ins, 0);
9362 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009363 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009364 error(state, ins, "division by zero");
9365 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009366 else if (is_one(RHS(ins, 1))) {
9367 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009368 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009369 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009370 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009371 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009372 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009373 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009374 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009375 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009376 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009377 }
9378}
9379
9380static void simplify_udiv(struct compile_state *state, struct triple *ins)
9381{
Eric Biederman90089602004-05-28 14:11:54 +00009382 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009383 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009384 left = read_const(state, ins, RHS(ins, 0));
9385 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009386 mkconst(state, ins, left / right);
9387 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009388 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009389 mkconst(state, ins, 0);
9390 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009391 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009392 error(state, ins, "division by zero");
9393 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009394 else if (is_one(RHS(ins, 1))) {
9395 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009396 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009397 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009398 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009399 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009400 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009401 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009402 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009403 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009404 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009405 }
9406}
9407
9408static void simplify_smod(struct compile_state *state, struct triple *ins)
9409{
Eric Biederman90089602004-05-28 14:11:54 +00009410 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009411 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009412 left = read_const(state, ins, RHS(ins, 0));
9413 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009414 mkconst(state, ins, left % right);
9415 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009416 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009417 mkconst(state, ins, 0);
9418 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009419 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009420 error(state, ins, "division by zero");
9421 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009422 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009423 mkconst(state, ins, 0);
9424 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009425 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009426 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009427 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009428 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009429 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009430 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009431 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009432 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009433 }
9434}
Eric Biederman83b991a2003-10-11 06:20:25 +00009435
Eric Biedermanb138ac82003-04-22 18:44:01 +00009436static void simplify_umod(struct compile_state *state, struct triple *ins)
9437{
Eric Biederman90089602004-05-28 14:11:54 +00009438 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009439 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009440 left = read_const(state, ins, RHS(ins, 0));
9441 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009442 mkconst(state, ins, left % right);
9443 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009444 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009445 mkconst(state, ins, 0);
9446 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009447 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009448 error(state, ins, "division by zero");
9449 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009450 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009451 mkconst(state, ins, 0);
9452 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009453 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009454 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009455 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009456 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009457 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009458 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009459 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009460 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009461 }
9462}
9463
9464static void simplify_add(struct compile_state *state, struct triple *ins)
9465{
9466 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009467 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009468 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009469 tmp = RHS(ins, 0);
9470 RHS(ins, 0) = RHS(ins, 1);
9471 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009472 }
Eric Biederman90089602004-05-28 14:11:54 +00009473 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009474 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009475 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009476 left = read_const(state, ins, RHS(ins, 0));
9477 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009478 mkconst(state, ins, left + right);
9479 }
Eric Biederman530b5192003-07-01 10:05:30 +00009480 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009481 struct triple *sdecl;
9482 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009483 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009484 left = RHS(ins, 0)->u.cval;
9485 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009486 mkaddr_const(state, ins, sdecl, left + right);
9487 }
Eric Biederman530b5192003-07-01 10:05:30 +00009488 else {
9489 internal_warning(state, ins, "Optimize me!");
9490 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009491 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009492 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009493 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009494 tmp = RHS(ins, 1);
9495 RHS(ins, 1) = RHS(ins, 0);
9496 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009497 }
9498}
9499
9500static void simplify_sub(struct compile_state *state, struct triple *ins)
9501{
Eric Biederman90089602004-05-28 14:11:54 +00009502 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009503 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009504 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009505 left = read_const(state, ins, RHS(ins, 0));
9506 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009507 mkconst(state, ins, left - right);
9508 }
Eric Biederman530b5192003-07-01 10:05:30 +00009509 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009510 struct triple *sdecl;
9511 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009512 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009513 left = RHS(ins, 0)->u.cval;
9514 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009515 mkaddr_const(state, ins, sdecl, left - right);
9516 }
Eric Biederman530b5192003-07-01 10:05:30 +00009517 else {
9518 internal_warning(state, ins, "Optimize me!");
9519 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009520 }
9521}
9522
9523static void simplify_sl(struct compile_state *state, struct triple *ins)
9524{
Eric Biederman90089602004-05-28 14:11:54 +00009525 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009526 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009527 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009528 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009529 warning(state, ins, "left shift count >= width of type");
9530 }
9531 }
Eric Biederman90089602004-05-28 14:11:54 +00009532 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009533 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009534 left = read_const(state, ins, RHS(ins, 0));
9535 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009536 mkconst(state, ins, left << right);
9537 }
9538}
9539
9540static void simplify_usr(struct compile_state *state, struct triple *ins)
9541{
Eric Biederman90089602004-05-28 14:11:54 +00009542 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009543 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009544 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009545 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009546 warning(state, ins, "right shift count >= width of type");
9547 }
9548 }
Eric Biederman90089602004-05-28 14:11:54 +00009549 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009550 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009551 left = read_const(state, ins, RHS(ins, 0));
9552 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009553 mkconst(state, ins, left >> right);
9554 }
9555}
9556
9557static void simplify_ssr(struct compile_state *state, struct triple *ins)
9558{
Eric Biederman90089602004-05-28 14:11:54 +00009559 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009560 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009561 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009562 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009563 warning(state, ins, "right shift count >= width of type");
9564 }
9565 }
Eric Biederman90089602004-05-28 14:11:54 +00009566 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009567 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009568 left = read_sconst(state, ins, RHS(ins, 0));
9569 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009570 mkconst(state, ins, left >> right);
9571 }
9572}
9573
9574static void simplify_and(struct compile_state *state, struct triple *ins)
9575{
Eric Biederman90089602004-05-28 14:11:54 +00009576 struct triple *left, *right;
9577 left = RHS(ins, 0);
9578 right = RHS(ins, 1);
9579
9580 if (is_simple_const(left) && is_simple_const(right)) {
9581 ulong_t lval, rval;
9582 lval = read_const(state, ins, left);
9583 rval = read_const(state, ins, right);
9584 mkconst(state, ins, lval & rval);
9585 }
9586 else if (is_zero(right) || is_zero(left)) {
9587 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009588 }
9589}
9590
9591static void simplify_or(struct compile_state *state, struct triple *ins)
9592{
Eric Biederman90089602004-05-28 14:11:54 +00009593 struct triple *left, *right;
9594 left = RHS(ins, 0);
9595 right = RHS(ins, 1);
9596
9597 if (is_simple_const(left) && is_simple_const(right)) {
9598 ulong_t lval, rval;
9599 lval = read_const(state, ins, left);
9600 rval = read_const(state, ins, right);
9601 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009602 }
Eric Biederman90089602004-05-28 14:11:54 +00009603#if 0 /* I need to handle type mismatches here... */
9604 else if (is_zero(right)) {
9605 mkcopy(state, ins, left);
9606 }
9607 else if (is_zero(left)) {
9608 mkcopy(state, ins, right);
9609 }
9610#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009611}
9612
9613static void simplify_xor(struct compile_state *state, struct triple *ins)
9614{
Eric Biederman90089602004-05-28 14:11:54 +00009615 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009616 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009617 left = read_const(state, ins, RHS(ins, 0));
9618 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009619 mkconst(state, ins, left ^ right);
9620 }
9621}
9622
9623static void simplify_pos(struct compile_state *state, struct triple *ins)
9624{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009625 if (is_const(RHS(ins, 0))) {
9626 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009627 }
9628 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009629 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009630 }
9631}
9632
9633static void simplify_neg(struct compile_state *state, struct triple *ins)
9634{
Eric Biederman90089602004-05-28 14:11:54 +00009635 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009636 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009637 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009638 mkconst(state, ins, -left);
9639 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009640 else if (RHS(ins, 0)->op == OP_NEG) {
9641 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009642 }
9643}
9644
9645static void simplify_invert(struct compile_state *state, struct triple *ins)
9646{
Eric Biederman90089602004-05-28 14:11:54 +00009647 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009648 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009649 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009650 mkconst(state, ins, ~left);
9651 }
9652}
9653
9654static void simplify_eq(struct compile_state *state, struct triple *ins)
9655{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009656 struct triple *left, *right;
9657 left = RHS(ins, 0);
9658 right = RHS(ins, 1);
9659
9660 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009661 int val;
9662 val = const_eq(state, ins, left, right);
9663 if (val >= 0) {
9664 mkconst(state, ins, val == 1);
9665 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009666 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009667 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009668 mkconst(state, ins, 1);
9669 }
9670}
9671
9672static void simplify_noteq(struct compile_state *state, struct triple *ins)
9673{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009674 struct triple *left, *right;
9675 left = RHS(ins, 0);
9676 right = RHS(ins, 1);
9677
9678 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009679 int val;
9680 val = const_eq(state, ins, left, right);
9681 if (val >= 0) {
9682 mkconst(state, ins, val != 1);
9683 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009684 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009685 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009686 mkconst(state, ins, 0);
9687 }
9688}
9689
9690static void simplify_sless(struct compile_state *state, struct triple *ins)
9691{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009692 struct triple *left, *right;
9693 left = RHS(ins, 0);
9694 right = RHS(ins, 1);
9695
9696 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009697 int val;
9698 val = const_scmp(state, ins, left, right);
9699 if ((val >= -1) && (val <= 1)) {
9700 mkconst(state, ins, val < 0);
9701 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009702 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009703 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009704 mkconst(state, ins, 0);
9705 }
9706}
9707
9708static void simplify_uless(struct compile_state *state, struct triple *ins)
9709{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009710 struct triple *left, *right;
9711 left = RHS(ins, 0);
9712 right = RHS(ins, 1);
9713
9714 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009715 int val;
9716 val = const_ucmp(state, ins, left, right);
9717 if ((val >= -1) && (val <= 1)) {
9718 mkconst(state, ins, val < 0);
9719 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009720 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009721 else if (is_zero(right)) {
9722 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009723 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009724 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009725 mkconst(state, ins, 0);
9726 }
9727}
9728
9729static void simplify_smore(struct compile_state *state, struct triple *ins)
9730{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009731 struct triple *left, *right;
9732 left = RHS(ins, 0);
9733 right = RHS(ins, 1);
9734
9735 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009736 int val;
9737 val = const_scmp(state, ins, left, right);
9738 if ((val >= -1) && (val <= 1)) {
9739 mkconst(state, ins, val > 0);
9740 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009741 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009742 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009743 mkconst(state, ins, 0);
9744 }
9745}
9746
9747static void simplify_umore(struct compile_state *state, struct triple *ins)
9748{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009749 struct triple *left, *right;
9750 left = RHS(ins, 0);
9751 right = RHS(ins, 1);
9752
9753 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009754 int val;
9755 val = const_ucmp(state, ins, left, right);
9756 if ((val >= -1) && (val <= 1)) {
9757 mkconst(state, ins, val > 0);
9758 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009759 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009760 else if (is_zero(left)) {
9761 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009762 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009763 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009764 mkconst(state, ins, 0);
9765 }
9766}
9767
9768
9769static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9770{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009771 struct triple *left, *right;
9772 left = RHS(ins, 0);
9773 right = RHS(ins, 1);
9774
9775 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009776 int val;
9777 val = const_scmp(state, ins, left, right);
9778 if ((val >= -1) && (val <= 1)) {
9779 mkconst(state, ins, val <= 0);
9780 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009781 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009782 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009783 mkconst(state, ins, 1);
9784 }
9785}
9786
9787static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9788{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009789 struct triple *left, *right;
9790 left = RHS(ins, 0);
9791 right = RHS(ins, 1);
9792
9793 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009794 int val;
9795 val = const_ucmp(state, ins, left, right);
9796 if ((val >= -1) && (val <= 1)) {
9797 mkconst(state, ins, val <= 0);
9798 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009799 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009800 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009801 mkconst(state, ins, 1);
9802 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009803 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009804 mkconst(state, ins, 1);
9805 }
9806}
9807
9808static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9809{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009810 struct triple *left, *right;
9811 left = RHS(ins, 0);
9812 right = RHS(ins, 1);
9813
9814 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009815 int val;
9816 val = const_scmp(state, ins, left, right);
9817 if ((val >= -1) && (val <= 1)) {
9818 mkconst(state, ins, val >= 0);
9819 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009820 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009821 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009822 mkconst(state, ins, 1);
9823 }
9824}
9825
9826static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9827{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009828 struct triple *left, *right;
9829 left = RHS(ins, 0);
9830 right = RHS(ins, 1);
9831
9832 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009833 int val;
9834 val = const_ucmp(state, ins, left, right);
9835 if ((val >= -1) && (val <= 1)) {
9836 mkconst(state, ins, val >= 0);
9837 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009838 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009839 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009840 mkconst(state, ins, 1);
9841 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009842 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009843 mkconst(state, ins, 1);
9844 }
9845}
9846
9847static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9848{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009849 struct triple *rhs;
9850 rhs = RHS(ins, 0);
9851
9852 if (is_const(rhs)) {
9853 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009854 }
9855 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009856 else if ((rhs->use) &&
9857 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009858 int need_copy = 1;
9859 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009860 switch(rhs->op) {
9861 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9862 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9863 case OP_EQ: rhs->op = OP_NOTEQ; break;
9864 case OP_NOTEQ: rhs->op = OP_EQ; break;
9865 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9866 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9867 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9868 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9869 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9870 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9871 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9872 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009873 default:
9874 need_copy = 0;
9875 break;
9876 }
9877 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009878 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009879 }
9880 }
9881}
9882
9883static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9884{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009885 struct triple *rhs;
9886 rhs = RHS(ins, 0);
9887
9888 if (is_const(rhs)) {
9889 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009890 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009891 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009892 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9893 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9894 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009895 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009896 }
9897
9898}
9899
Eric Biederman90089602004-05-28 14:11:54 +00009900static void simplify_load(struct compile_state *state, struct triple *ins)
9901{
9902 struct triple *addr, *sdecl, *blob;
9903
9904 /* If I am doing a load with a constant pointer from a constant
9905 * table get the value.
9906 */
9907 addr = RHS(ins, 0);
9908 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9909 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9910 (blob->op == OP_BLOBCONST)) {
9911 unsigned char buffer[SIZEOF_WORD];
9912 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009913 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009914 ulong_t val;
9915 reg_size = reg_size_of(state, ins->type);
9916 if (reg_size > REG_SIZEOF_REG) {
9917 internal_error(state, ins, "load size greater than register");
9918 }
9919 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009920 end = blob->u.blob;
9921 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009922 src = blob->u.blob;
9923 src += addr->u.cval;
9924
Eric Biederman7dea9552004-06-29 05:38:37 +00009925 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009926 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009927 }
9928
Eric Biederman90089602004-05-28 14:11:54 +00009929 memset(buffer, 0, sizeof(buffer));
9930 memcpy(buffer, src, bits_to_bytes(mem_size));
9931
9932 switch(mem_size) {
9933 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9934 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9935 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9936 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9937 default:
9938 internal_error(state, ins, "mem_size: %d not handled",
9939 mem_size);
9940 val = 0;
9941 break;
9942 }
9943 mkconst(state, ins, val);
9944 }
9945}
9946
9947static void simplify_uextract(struct compile_state *state, struct triple *ins)
9948{
9949 if (is_simple_const(RHS(ins, 0))) {
9950 ulong_t val;
9951 ulong_t mask;
9952 val = read_const(state, ins, RHS(ins, 0));
9953 mask = 1;
9954 mask <<= ins->u.bitfield.size;
9955 mask -= 1;
9956 val >>= ins->u.bitfield.offset;
9957 val &= mask;
9958 mkconst(state, ins, val);
9959 }
9960}
9961
9962static void simplify_sextract(struct compile_state *state, struct triple *ins)
9963{
9964 if (is_simple_const(RHS(ins, 0))) {
9965 ulong_t val;
9966 ulong_t mask;
9967 long_t sval;
9968 val = read_const(state, ins, RHS(ins, 0));
9969 mask = 1;
9970 mask <<= ins->u.bitfield.size;
9971 mask -= 1;
9972 val >>= ins->u.bitfield.offset;
9973 val &= mask;
9974 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9975 sval = val;
9976 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
9977 mkconst(state, ins, sval);
9978 }
9979}
9980
9981static void simplify_deposit(struct compile_state *state, struct triple *ins)
9982{
9983 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9984 ulong_t targ, val;
9985 ulong_t mask;
9986 targ = read_const(state, ins, RHS(ins, 0));
9987 val = read_const(state, ins, RHS(ins, 1));
9988 mask = 1;
9989 mask <<= ins->u.bitfield.size;
9990 mask -= 1;
9991 mask <<= ins->u.bitfield.offset;
9992 targ &= ~mask;
9993 val <<= ins->u.bitfield.offset;
9994 val &= mask;
9995 targ |= val;
9996 mkconst(state, ins, targ);
9997 }
9998}
9999
Eric Biedermanb138ac82003-04-22 18:44:01 +000010000static void simplify_copy(struct compile_state *state, struct triple *ins)
10001{
Eric Biederman90089602004-05-28 14:11:54 +000010002 struct triple *right;
10003 right = RHS(ins, 0);
10004 if (is_subset_type(ins->type, right->type)) {
10005 ins->type = right->type;
10006 }
10007 if (equiv_types(ins->type, right->type)) {
10008 ins->op = OP_COPY;/* I don't need to convert if the types match */
10009 } else {
10010 if (ins->op == OP_COPY) {
10011 internal_error(state, ins, "type mismatch on copy");
10012 }
10013 }
10014 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10015 struct triple *sdecl;
10016 ulong_t offset;
10017 sdecl = MISC(right, 0);
10018 offset = right->u.cval;
10019 mkaddr_const(state, ins, sdecl, offset);
10020 }
10021 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10022 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010023 case OP_INTCONST:
10024 {
10025 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010026 left = read_const(state, ins, right);
10027 /* Ensure I have not overflowed the destination. */
10028 if (size_of(state, right->type) > size_of(state, ins->type)) {
10029 ulong_t mask;
10030 mask = 1;
10031 mask <<= size_of(state, ins->type);
10032 mask -= 1;
10033 left &= mask;
10034 }
10035 /* Ensure I am properly sign extended */
10036 if (size_of(state, right->type) < size_of(state, ins->type) &&
10037 is_signed(right->type)) {
10038 long_t val;
10039 int shift;
10040 shift = SIZEOF_LONG - size_of(state, right->type);
10041 val = left;
10042 val <<= shift;
10043 val >>= shift;
10044 left = val;
10045 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010046 mkconst(state, ins, left);
10047 break;
10048 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010049 default:
10050 internal_error(state, ins, "uknown constant");
10051 break;
10052 }
10053 }
10054}
10055
Eric Biederman83b991a2003-10-11 06:20:25 +000010056static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010057{
10058 struct triple *ptr;
10059 if (!block) {
10060 return 0;
10061 }
10062 ptr = block->first;
10063 do {
10064 if (ptr->op == OP_PHI) {
10065 return 1;
10066 }
10067 ptr = ptr->next;
10068 } while(ptr != block->last);
10069 return 0;
10070}
10071
Eric Biederman83b991a2003-10-11 06:20:25 +000010072static int phi_dependency(struct block *block)
10073{
10074 /* A block has a phi dependency if a phi function
10075 * depends on that block to exist, and makes a block
10076 * that is otherwise useless unsafe to remove.
10077 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010078 if (block) {
10079 struct block_set *edge;
10080 for(edge = block->edges; edge; edge = edge->next) {
10081 if (phi_present(edge->member)) {
10082 return 1;
10083 }
10084 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010085 }
10086 return 0;
10087}
10088
10089static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10090{
10091 struct triple *targ;
10092 targ = TARG(ins, 0);
10093 /* During scc_transform temporary triples are allocated that
10094 * loop back onto themselves. If I see one don't advance the
10095 * target.
10096 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010097 while(triple_is_structural(state, targ) &&
10098 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010099 targ = targ->next;
10100 }
10101 return targ;
10102}
10103
10104
10105static void simplify_branch(struct compile_state *state, struct triple *ins)
10106{
Eric Biederman90089602004-05-28 14:11:54 +000010107 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010108 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010109 internal_error(state, ins, "not branch");
10110 }
10111 if (ins->use != 0) {
10112 internal_error(state, ins, "branch use");
10113 }
10114 /* The challenge here with simplify branch is that I need to
10115 * make modifications to the control flow graph as well
10116 * as to the branch instruction itself. That is handled
10117 * by rebuilding the basic blocks after simplify all is called.
10118 */
10119
10120 /* If we have a branch to an unconditional branch update
10121 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010122 * functions.
10123 * Also only do this a limited number of times so
10124 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010125 */
Eric Biederman90089602004-05-28 14:11:54 +000010126 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010127 do {
10128 struct triple *targ;
10129 simplified = 0;
10130 targ = branch_target(state, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010131 if ((targ != ins) && (targ->op == OP_BRANCH) &&
10132 !phi_dependency(targ->u.block))
10133 {
10134 unuse_triple(TARG(ins, 0), ins);
10135 TARG(ins, 0) = TARG(targ, 0);
10136 use_triple(TARG(ins, 0), ins);
10137 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010138 }
Eric Biederman90089602004-05-28 14:11:54 +000010139 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010140
10141 /* If we have a conditional branch with a constant condition
10142 * make it an unconditional branch.
10143 */
Eric Biederman90089602004-05-28 14:11:54 +000010144 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010145 struct triple *targ;
10146 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010147 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010148 unuse_triple(RHS(ins, 0), ins);
10149 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010150 ins->rhs = 0;
10151 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010152 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010153 if (value) {
10154 unuse_triple(ins->next, ins);
10155 TARG(ins, 0) = targ;
10156 }
10157 else {
10158 unuse_triple(targ, ins);
10159 TARG(ins, 0) = ins->next;
10160 }
10161 }
Eric Biederman90089602004-05-28 14:11:54 +000010162
10163 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010164 * make it a noop.
10165 */
10166 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010167 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010168 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010169 unuse_triple(RHS(ins, 0), ins);
10170 unuse_triple(ins->next, ins);
10171 }
Eric Biederman90089602004-05-28 14:11:54 +000010172 ins->lhs = 0;
10173 ins->rhs = 0;
10174 ins->misc = 0;
10175 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010176 ins->op = OP_NOOP;
10177 if (ins->use) {
10178 internal_error(state, ins, "noop use != 0");
10179 }
10180 }
10181}
10182
Eric Biederman530b5192003-07-01 10:05:30 +000010183static void simplify_label(struct compile_state *state, struct triple *ins)
10184{
Eric Biederman83b991a2003-10-11 06:20:25 +000010185 /* Ignore volatile labels */
10186 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010187 return;
10188 }
10189 if (ins->use == 0) {
10190 ins->op = OP_NOOP;
10191 }
10192 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010193 /* In general it is not safe to merge one label that
10194 * imediately follows another. The problem is that the empty
10195 * looking block may have phi functions that depend on it.
10196 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010197 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010198 struct triple_set *user, *next;
10199 ins->op = OP_NOOP;
10200 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010201 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010202 next = user->next;
10203 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010204 expr = triple_targ(state, use, 0);
10205 for(;expr; expr = triple_targ(state, use, expr)) {
10206 if (*expr == ins) {
10207 *expr = ins->prev;
10208 unuse_triple(ins, use);
10209 use_triple(ins->prev, use);
10210 }
10211
Eric Biederman530b5192003-07-01 10:05:30 +000010212 }
10213 }
10214 if (ins->use) {
10215 internal_error(state, ins, "noop use != 0");
10216 }
10217 }
10218 }
10219}
10220
Eric Biedermanb138ac82003-04-22 18:44:01 +000010221static void simplify_phi(struct compile_state *state, struct triple *ins)
10222{
Eric Biederman83b991a2003-10-11 06:20:25 +000010223 struct triple **slot;
10224 struct triple *value;
10225 int zrhs, i;
10226 ulong_t cvalue;
10227 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010228 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010229 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010230 return;
10231 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010232 /* See if all of the rhs members of a phi have the same value */
10233 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010234 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010235 for(i = 1; i < zrhs; i++) {
10236 if ( !slot[i] ||
10237 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010238 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010239 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010240 break;
10241 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010242 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010243 if (i == zrhs) {
10244 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010245 return;
10246 }
10247 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010248
10249 /* See if all of rhs members of a phi are the same */
10250 value = slot[0];
10251 for(i = 1; i < zrhs; i++) {
10252 if (slot[i] != value) {
10253 break;
10254 }
10255 }
10256 if (i == zrhs) {
10257 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010258 if (!is_subset_type(ins->type, value->type)) {
10259 internal_error(state, ins, "bad input type to phi");
10260 }
10261 /* Make the types match */
10262 if (!equiv_types(ins->type, value->type)) {
10263 ins->type = value->type;
10264 }
10265 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010266 mkcopy(state, ins, value);
10267 return;
10268 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010269}
10270
10271
10272static void simplify_bsf(struct compile_state *state, struct triple *ins)
10273{
Eric Biederman90089602004-05-28 14:11:54 +000010274 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010275 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010276 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010277 mkconst(state, ins, bsf(left));
10278 }
10279}
10280
10281static void simplify_bsr(struct compile_state *state, struct triple *ins)
10282{
Eric Biederman90089602004-05-28 14:11:54 +000010283 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010284 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010285 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010286 mkconst(state, ins, bsr(left));
10287 }
10288}
10289
10290
10291typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010292static const struct simplify_table {
10293 simplify_t func;
10294 unsigned long flag;
10295} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010296#define simplify_sdivt simplify_noop
10297#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010298#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010299
Eric Biederman5ade04a2003-10-22 04:03:46 +000010300[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10301[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10302[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10303[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10304[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10305[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10306[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10307[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10308[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10309[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10310[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10311[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10312[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10313[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10314[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10315[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10316[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10317[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10318[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010319
Eric Biederman5ade04a2003-10-22 04:03:46 +000010320[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10321[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10322[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10323[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10324[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10325[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10326[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10327[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10328[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10329[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10330[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10331[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010332
Eric Biederman90089602004-05-28 14:11:54 +000010333[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010334[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010335
Eric Biederman90089602004-05-28 14:11:54 +000010336[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10337[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10338[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10339
Eric Biederman5ade04a2003-10-22 04:03:46 +000010340[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010341
Eric Biederman5ade04a2003-10-22 04:03:46 +000010342[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10343[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10344[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010345[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010346
Eric Biederman5ade04a2003-10-22 04:03:46 +000010347[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10348[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10349[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010350[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010351[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10352[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010353
Eric Biederman5ade04a2003-10-22 04:03:46 +000010354[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010355[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010356
Eric Biederman5ade04a2003-10-22 04:03:46 +000010357[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10358[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10359[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10360[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10361[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10362[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10363[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10364[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10365[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010366
Eric Biederman5ade04a2003-10-22 04:03:46 +000010367[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10368[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10369[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10370[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10371[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10372[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10373[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10374[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10375[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10376[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10377[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010378};
10379
Eric Biederman90089602004-05-28 14:11:54 +000010380static inline void debug_simplify(struct compile_state *state,
10381 simplify_t do_simplify, struct triple *ins)
10382{
10383#if DEBUG_SIMPLIFY_HIRES
10384 if (state->functions_joined && (do_simplify != simplify_noop)) {
10385 /* High resolution debugging mode */
10386 fprintf(state->dbgout, "simplifing: ");
10387 display_triple(state->dbgout, ins);
10388 }
10389#endif
10390 do_simplify(state, ins);
10391#if DEBUG_SIMPLIFY_HIRES
10392 if (state->functions_joined && (do_simplify != simplify_noop)) {
10393 /* High resolution debugging mode */
10394 fprintf(state->dbgout, "simplified: ");
10395 display_triple(state->dbgout, ins);
10396 }
10397#endif
10398}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010399static void simplify(struct compile_state *state, struct triple *ins)
10400{
10401 int op;
10402 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010403 if (ins == &unknown_triple) {
10404 internal_error(state, ins, "simplifying the unknown triple?");
10405 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010406 do {
10407 op = ins->op;
10408 do_simplify = 0;
10409 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10410 do_simplify = 0;
10411 }
Eric Biederman90089602004-05-28 14:11:54 +000010412 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010413 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010414 }
Eric Biederman90089602004-05-28 14:11:54 +000010415 if (do_simplify &&
10416 !(state->compiler->flags & table_simplify[op].flag)) {
10417 do_simplify = simplify_noop;
10418 }
10419 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010420 do_simplify = simplify_noop;
10421 }
10422
Eric Biedermanb138ac82003-04-22 18:44:01 +000010423 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010424 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010425 op, tops(op));
10426 return;
10427 }
Eric Biederman90089602004-05-28 14:11:54 +000010428 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010429 } while(ins->op != op);
10430}
10431
Eric Biederman5ade04a2003-10-22 04:03:46 +000010432static void rebuild_ssa_form(struct compile_state *state);
10433
Eric Biedermanb138ac82003-04-22 18:44:01 +000010434static void simplify_all(struct compile_state *state)
10435{
10436 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010437 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10438 return;
10439 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010440 first = state->first;
10441 ins = first->prev;
10442 do {
10443 simplify(state, ins);
10444 ins = ins->prev;
10445 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010446 ins = first;
10447 do {
10448 simplify(state, ins);
10449 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010450 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010451 rebuild_ssa_form(state);
10452
Eric Biederman90089602004-05-28 14:11:54 +000010453 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010454}
10455
10456/*
10457 * Builtins....
10458 * ============================
10459 */
10460
Eric Biederman0babc1c2003-05-09 02:39:00 +000010461static void register_builtin_function(struct compile_state *state,
10462 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010463{
Eric Biederman90089602004-05-28 14:11:54 +000010464 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010465 struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010466 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010467 struct file_state file;
10468 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010469 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010470 va_list args;
10471 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010472
10473 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010474 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010475 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010476 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010477 file.report_line = 1;
10478 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010479 file.prev = state->file;
10480 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010481 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010482
10483 /* Find the Parameter count */
10484 valid_op(state, op);
10485 parameters = table_ops[op].rhs;
10486 if (parameters < 0 ) {
10487 internal_error(state, 0, "Invalid builtin parameter count");
10488 }
10489
10490 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010491 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010492 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010493 next = &ftype->right;
10494 va_start(args, rtype);
10495 for(i = 0; i < parameters; i++) {
10496 atype = va_arg(args, struct type *);
10497 if (!*next) {
10498 *next = atype;
10499 } else {
10500 *next = new_type(TYPE_PRODUCT, *next, atype);
10501 next = &((*next)->right);
10502 }
10503 }
10504 if (!*next) {
10505 *next = &void_type;
10506 }
10507 va_end(args);
10508
Eric Biederman90089602004-05-28 14:11:54 +000010509 /* Get the initial closure type */
10510 ctype = new_type(TYPE_JOIN, &void_type, 0);
10511 ctype->elements = 1;
10512
10513 /* Get the return type */
10514 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10515 crtype->elements = 2;
10516
Eric Biedermanb138ac82003-04-22 18:44:01 +000010517 /* Generate the needed triples */
10518 def = triple(state, OP_LIST, ftype, 0, 0);
10519 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010520 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010521 result = flatten(state, first, variable(state, crtype));
10522 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010523 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010524
10525 /* Now string them together */
10526 param = ftype->right;
10527 for(i = 0; i < parameters; i++) {
10528 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10529 atype = param->left;
10530 } else {
10531 atype = param;
10532 }
10533 arg = flatten(state, first, variable(state, atype));
10534 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010535 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010536 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010537 generate_lhs_pieces(state, work);
10538 for(i = 0; i < parameters; i++) {
10539 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010540 }
Eric Biederman90089602004-05-28 14:11:54 +000010541 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10542 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010543 }
10544 work = flatten(state, first, work);
10545 last = flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010546 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010547 name_len = strlen(name);
10548 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010549 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010550 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010551
Eric Biedermanb138ac82003-04-22 18:44:01 +000010552 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010553 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010554 state->main_function = 0;
10555
Eric Biederman5ade04a2003-10-22 04:03:46 +000010556 if (!state->functions) {
10557 state->functions = def;
10558 } else {
10559 insert_triple(state, state->functions, def);
10560 }
10561 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010562 FILE *fp = state->dbgout;
10563 fprintf(fp, "\n");
10564 loc(fp, state, 0);
10565 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10566 display_func(state, fp, def);
10567 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010568 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010569}
10570
Eric Biederman0babc1c2003-05-09 02:39:00 +000010571static struct type *partial_struct(struct compile_state *state,
10572 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010573{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010574 struct hash_entry *field_ident;
10575 struct type *result;
10576 int field_name_len;
10577
10578 field_name_len = strlen(field_name);
10579 field_ident = lookup(state, field_name, field_name_len);
10580
10581 result = clone_type(0, type);
10582 result->field_ident = field_ident;
10583
10584 if (rest) {
10585 result = new_type(TYPE_PRODUCT, result, rest);
10586 }
10587 return result;
10588}
10589
10590static struct type *register_builtin_type(struct compile_state *state,
10591 const char *name, struct type *type)
10592{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010593 struct hash_entry *ident;
10594 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010595
Eric Biedermanb138ac82003-04-22 18:44:01 +000010596 name_len = strlen(name);
10597 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010598
10599 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10600 ulong_t elements = 0;
10601 struct type *field;
10602 type = new_type(TYPE_STRUCT, type, 0);
10603 field = type->left;
10604 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10605 elements++;
10606 field = field->right;
10607 }
10608 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010609 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010610 type->type_ident = ident;
10611 type->elements = elements;
10612 }
10613 symbol(state, ident, &ident->sym_ident, 0, type);
10614 ident->tok = TOK_TYPE_NAME;
10615 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010616}
10617
Eric Biederman0babc1c2003-05-09 02:39:00 +000010618
Eric Biedermanb138ac82003-04-22 18:44:01 +000010619static void register_builtins(struct compile_state *state)
10620{
Eric Biederman530b5192003-07-01 10:05:30 +000010621 struct type *div_type, *ldiv_type;
10622 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010623 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010624
Eric Biederman530b5192003-07-01 10:05:30 +000010625 div_type = register_builtin_type(state, "__builtin_div_t",
10626 partial_struct(state, "quot", &int_type,
10627 partial_struct(state, "rem", &int_type, 0)));
10628 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10629 partial_struct(state, "quot", &long_type,
10630 partial_struct(state, "rem", &long_type, 0)));
10631 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10632 partial_struct(state, "quot", &uint_type,
10633 partial_struct(state, "rem", &uint_type, 0)));
10634 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10635 partial_struct(state, "quot", &ulong_type,
10636 partial_struct(state, "rem", &ulong_type, 0)));
10637
10638 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10639 &int_type, &int_type);
10640 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10641 &long_type, &long_type);
10642 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10643 &uint_type, &uint_type);
10644 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10645 &ulong_type, &ulong_type);
10646
Eric Biederman0babc1c2003-05-09 02:39:00 +000010647 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
10648 &ushort_type);
10649 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10650 &ushort_type);
10651 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
10652 &ushort_type);
10653
10654 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
10655 &uchar_type, &ushort_type);
10656 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
10657 &ushort_type, &ushort_type);
10658 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
10659 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010660
Eric Biederman0babc1c2003-05-09 02:39:00 +000010661 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
10662 &int_type);
10663 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
10664 &int_type);
10665
10666 msr_type = register_builtin_type(state, "__builtin_msr_t",
10667 partial_struct(state, "lo", &ulong_type,
10668 partial_struct(state, "hi", &ulong_type, 0)));
10669
10670 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10671 &ulong_type);
10672 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10673 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010674
Eric Biederman0babc1c2003-05-09 02:39:00 +000010675 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
10676 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010677}
10678
10679static struct type *declarator(
10680 struct compile_state *state, struct type *type,
10681 struct hash_entry **ident, int need_ident);
10682static void decl(struct compile_state *state, struct triple *first);
10683static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010684#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010685static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010686#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010687static struct type *decl_specifiers(struct compile_state *state);
10688static int istype(int tok);
10689static struct triple *expr(struct compile_state *state);
10690static struct triple *assignment_expr(struct compile_state *state);
10691static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010692static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010693
10694static struct triple *call_expr(
10695 struct compile_state *state, struct triple *func)
10696{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010697 struct triple *def;
10698 struct type *param, *type;
10699 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010700
10701 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10702 error(state, 0, "Called object is not a function");
10703 }
10704 if (func->op != OP_LIST) {
10705 internal_error(state, 0, "improper function");
10706 }
10707 eat(state, TOK_LPAREN);
10708 /* Find the return type without any specifiers */
10709 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010710 /* Count the number of rhs entries for OP_FCALL */
10711 param = func->type->right;
10712 pvals = 0;
10713 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10714 pvals++;
10715 param = param->right;
10716 }
10717 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10718 pvals++;
10719 }
10720 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010721 MISC(def, 0) = func;
10722
10723 param = func->type->right;
10724 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010725 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010726 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010727 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010728 arg_type = param;
10729 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10730 arg_type = param->left;
10731 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010732 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010733 RHS(def, index) = val;
10734 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010735 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010736 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010737 }
10738 }
10739 eat(state, TOK_RPAREN);
10740 return def;
10741}
10742
10743
10744static struct triple *character_constant(struct compile_state *state)
10745{
10746 struct triple *def;
10747 struct token *tk;
10748 const signed char *str, *end;
10749 int c;
10750 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010751 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010752 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010753 str_len = tk->str_len - 2;
10754 if (str_len <= 0) {
10755 error(state, 0, "empty character constant");
10756 }
10757 end = str + str_len;
10758 c = char_value(state, &str, end);
10759 if (str != end) {
10760 error(state, 0, "multibyte character constant not supported");
10761 }
10762 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10763 return def;
10764}
10765
10766static struct triple *string_constant(struct compile_state *state)
10767{
10768 struct triple *def;
10769 struct token *tk;
10770 struct type *type;
10771 const signed char *str, *end;
10772 signed char *buf, *ptr;
10773 int str_len;
10774
10775 buf = 0;
10776 type = new_type(TYPE_ARRAY, &char_type, 0);
10777 type->elements = 0;
10778 /* The while loop handles string concatenation */
10779 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010780 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010781 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010782 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010783 if (str_len < 0) {
10784 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010785 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010786 /* ignore empty string tokens */
Peter Stuge01708ca2010-02-12 21:28:15 +000010787 if ('"' == *str && 0 == str[1])
Patrick Georgi78fbb512010-02-11 11:13:32 +000010788 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010789 end = str + str_len;
10790 ptr = buf;
10791 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10792 memcpy(buf, ptr, type->elements);
10793 ptr = buf + type->elements;
10794 do {
10795 *ptr++ = char_value(state, &str, end);
10796 } while(str < end);
10797 type->elements = ptr - buf;
10798 } while(peek(state) == TOK_LIT_STRING);
10799 *ptr = '\0';
10800 type->elements += 1;
10801 def = triple(state, OP_BLOBCONST, type, 0, 0);
10802 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010803
Eric Biedermanb138ac82003-04-22 18:44:01 +000010804 return def;
10805}
10806
10807
10808static struct triple *integer_constant(struct compile_state *state)
10809{
10810 struct triple *def;
10811 unsigned long val;
10812 struct token *tk;
10813 char *end;
10814 int u, l, decimal;
10815 struct type *type;
10816
Eric Biederman41203d92004-11-08 09:31:09 +000010817 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010818 errno = 0;
10819 decimal = (tk->val.str[0] != '0');
10820 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010821 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010822 error(state, 0, "Integer constant to large");
10823 }
10824 u = l = 0;
10825 if ((*end == 'u') || (*end == 'U')) {
10826 u = 1;
10827 end++;
10828 }
10829 if ((*end == 'l') || (*end == 'L')) {
10830 l = 1;
10831 end++;
10832 }
10833 if ((*end == 'u') || (*end == 'U')) {
10834 u = 1;
10835 end++;
10836 }
10837 if (*end) {
10838 error(state, 0, "Junk at end of integer constant");
10839 }
10840 if (u && l) {
10841 type = &ulong_type;
10842 }
10843 else if (l) {
10844 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010845 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010846 type = &ulong_type;
10847 }
10848 }
10849 else if (u) {
10850 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010851 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010852 type = &ulong_type;
10853 }
10854 }
10855 else {
10856 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010857 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010858 type = &uint_type;
10859 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010860 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010861 type = &ulong_type;
10862 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010863 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010864 type = &long_type;
10865 }
10866 }
10867 def = int_const(state, type, val);
10868 return def;
10869}
10870
10871static struct triple *primary_expr(struct compile_state *state)
10872{
10873 struct triple *def;
10874 int tok;
10875 tok = peek(state);
10876 switch(tok) {
10877 case TOK_IDENT:
10878 {
10879 struct hash_entry *ident;
10880 /* Here ident is either:
10881 * a varable name
10882 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010883 */
Eric Biederman41203d92004-11-08 09:31:09 +000010884 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010885 if (!ident->sym_ident) {
10886 error(state, 0, "%s undeclared", ident->name);
10887 }
10888 def = ident->sym_ident->def;
10889 break;
10890 }
10891 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010892 {
10893 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010894 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010895 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010896 if (!ident->sym_ident) {
10897 error(state, 0, "%s undeclared", ident->name);
10898 }
10899 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010900 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010901 }
Eric Biederman41203d92004-11-08 09:31:09 +000010902 case TOK_MIDENT:
10903 {
10904 struct hash_entry *ident;
10905 ident = eat(state, TOK_MIDENT)->ident;
10906 warning(state, 0, "Replacing undefined macro: %s with 0",
10907 ident->name);
10908 def = int_const(state, &int_type, 0);
10909 break;
10910 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010911 case TOK_LPAREN:
10912 eat(state, TOK_LPAREN);
10913 def = expr(state);
10914 eat(state, TOK_RPAREN);
10915 break;
10916 case TOK_LIT_INT:
10917 def = integer_constant(state);
10918 break;
10919 case TOK_LIT_FLOAT:
10920 eat(state, TOK_LIT_FLOAT);
10921 error(state, 0, "Floating point constants not supported");
10922 def = 0;
10923 FINISHME();
10924 break;
10925 case TOK_LIT_CHAR:
10926 def = character_constant(state);
10927 break;
10928 case TOK_LIT_STRING:
10929 def = string_constant(state);
10930 break;
10931 default:
10932 def = 0;
10933 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10934 }
10935 return def;
10936}
10937
10938static struct triple *postfix_expr(struct compile_state *state)
10939{
10940 struct triple *def;
10941 int postfix;
10942 def = primary_expr(state);
10943 do {
10944 struct triple *left;
10945 int tok;
10946 postfix = 1;
10947 left = def;
10948 switch((tok = peek(state))) {
10949 case TOK_LBRACKET:
10950 eat(state, TOK_LBRACKET);
10951 def = mk_subscript_expr(state, left, expr(state));
10952 eat(state, TOK_RBRACKET);
10953 break;
10954 case TOK_LPAREN:
10955 def = call_expr(state, def);
10956 break;
10957 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010958 {
10959 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010960 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010961 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010962 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010963 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010964 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010965 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010966 {
10967 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010968 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010969 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010970 def = mk_deref_expr(state, read_expr(state, def));
10971 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010972 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010973 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010974 case TOK_PLUSPLUS:
10975 eat(state, TOK_PLUSPLUS);
10976 def = mk_post_inc_expr(state, left);
10977 break;
10978 case TOK_MINUSMINUS:
10979 eat(state, TOK_MINUSMINUS);
10980 def = mk_post_dec_expr(state, left);
10981 break;
10982 default:
10983 postfix = 0;
10984 break;
10985 }
10986 } while(postfix);
10987 return def;
10988}
10989
10990static struct triple *cast_expr(struct compile_state *state);
10991
10992static struct triple *unary_expr(struct compile_state *state)
10993{
10994 struct triple *def, *right;
10995 int tok;
10996 switch((tok = peek(state))) {
10997 case TOK_PLUSPLUS:
10998 eat(state, TOK_PLUSPLUS);
10999 def = mk_pre_inc_expr(state, unary_expr(state));
11000 break;
11001 case TOK_MINUSMINUS:
11002 eat(state, TOK_MINUSMINUS);
11003 def = mk_pre_dec_expr(state, unary_expr(state));
11004 break;
11005 case TOK_AND:
11006 eat(state, TOK_AND);
11007 def = mk_addr_expr(state, cast_expr(state), 0);
11008 break;
11009 case TOK_STAR:
11010 eat(state, TOK_STAR);
11011 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11012 break;
11013 case TOK_PLUS:
11014 eat(state, TOK_PLUS);
11015 right = read_expr(state, cast_expr(state));
11016 arithmetic(state, right);
11017 def = integral_promotion(state, right);
11018 break;
11019 case TOK_MINUS:
11020 eat(state, TOK_MINUS);
11021 right = read_expr(state, cast_expr(state));
11022 arithmetic(state, right);
11023 def = integral_promotion(state, right);
11024 def = triple(state, OP_NEG, def->type, def, 0);
11025 break;
11026 case TOK_TILDE:
11027 eat(state, TOK_TILDE);
11028 right = read_expr(state, cast_expr(state));
11029 integral(state, right);
11030 def = integral_promotion(state, right);
11031 def = triple(state, OP_INVERT, def->type, def, 0);
11032 break;
11033 case TOK_BANG:
11034 eat(state, TOK_BANG);
11035 right = read_expr(state, cast_expr(state));
11036 bool(state, right);
11037 def = lfalse_expr(state, right);
11038 break;
11039 case TOK_SIZEOF:
11040 {
11041 struct type *type;
11042 int tok1, tok2;
11043 eat(state, TOK_SIZEOF);
11044 tok1 = peek(state);
11045 tok2 = peek2(state);
11046 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11047 eat(state, TOK_LPAREN);
11048 type = type_name(state);
11049 eat(state, TOK_RPAREN);
11050 }
11051 else {
11052 struct triple *expr;
11053 expr = unary_expr(state);
11054 type = expr->type;
11055 release_expr(state, expr);
11056 }
Eric Biederman90089602004-05-28 14:11:54 +000011057 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011058 break;
11059 }
11060 case TOK_ALIGNOF:
11061 {
11062 struct type *type;
11063 int tok1, tok2;
11064 eat(state, TOK_ALIGNOF);
11065 tok1 = peek(state);
11066 tok2 = peek2(state);
11067 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11068 eat(state, TOK_LPAREN);
11069 type = type_name(state);
11070 eat(state, TOK_RPAREN);
11071 }
11072 else {
11073 struct triple *expr;
11074 expr = unary_expr(state);
11075 type = expr->type;
11076 release_expr(state, expr);
11077 }
Eric Biederman90089602004-05-28 14:11:54 +000011078 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011079 break;
11080 }
Eric Biederman41203d92004-11-08 09:31:09 +000011081 case TOK_MDEFINED:
11082 {
11083 /* We only come here if we are called from the preprocessor */
11084 struct hash_entry *ident;
11085 int parens;
11086 eat(state, TOK_MDEFINED);
11087 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011088 if (pp_peek(state) == TOK_LPAREN) {
11089 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011090 parens = 1;
11091 }
Eric Biedermancb364952004-11-15 10:46:44 +000011092 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011093 if (parens) {
11094 eat(state, TOK_RPAREN);
11095 }
11096 def = int_const(state, &int_type, ident->sym_define != 0);
11097 break;
11098 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011099 default:
11100 def = postfix_expr(state);
11101 break;
11102 }
11103 return def;
11104}
11105
11106static struct triple *cast_expr(struct compile_state *state)
11107{
11108 struct triple *def;
11109 int tok1, tok2;
11110 tok1 = peek(state);
11111 tok2 = peek2(state);
11112 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11113 struct type *type;
11114 eat(state, TOK_LPAREN);
11115 type = type_name(state);
11116 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011117 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011118 }
11119 else {
11120 def = unary_expr(state);
11121 }
11122 return def;
11123}
11124
11125static struct triple *mult_expr(struct compile_state *state)
11126{
11127 struct triple *def;
11128 int done;
11129 def = cast_expr(state);
11130 do {
11131 struct triple *left, *right;
11132 struct type *result_type;
11133 int tok, op, sign;
11134 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011135 tok = peek(state);
11136 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011137 case TOK_STAR:
11138 case TOK_DIV:
11139 case TOK_MOD:
11140 left = read_expr(state, def);
11141 arithmetic(state, left);
11142
11143 eat(state, tok);
11144
11145 right = read_expr(state, cast_expr(state));
11146 arithmetic(state, right);
11147
11148 result_type = arithmetic_result(state, left, right);
11149 sign = is_signed(result_type);
11150 op = -1;
11151 switch(tok) {
11152 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11153 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11154 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11155 }
11156 def = triple(state, op, result_type, left, right);
11157 break;
11158 default:
11159 done = 1;
11160 break;
11161 }
11162 } while(!done);
11163 return def;
11164}
11165
11166static struct triple *add_expr(struct compile_state *state)
11167{
11168 struct triple *def;
11169 int done;
11170 def = mult_expr(state);
11171 do {
11172 done = 0;
11173 switch( peek(state)) {
11174 case TOK_PLUS:
11175 eat(state, TOK_PLUS);
11176 def = mk_add_expr(state, def, mult_expr(state));
11177 break;
11178 case TOK_MINUS:
11179 eat(state, TOK_MINUS);
11180 def = mk_sub_expr(state, def, mult_expr(state));
11181 break;
11182 default:
11183 done = 1;
11184 break;
11185 }
11186 } while(!done);
11187 return def;
11188}
11189
11190static struct triple *shift_expr(struct compile_state *state)
11191{
11192 struct triple *def;
11193 int done;
11194 def = add_expr(state);
11195 do {
11196 struct triple *left, *right;
11197 int tok, op;
11198 done = 0;
11199 switch((tok = peek(state))) {
11200 case TOK_SL:
11201 case TOK_SR:
11202 left = read_expr(state, def);
11203 integral(state, left);
11204 left = integral_promotion(state, left);
11205
11206 eat(state, tok);
11207
11208 right = read_expr(state, add_expr(state));
11209 integral(state, right);
11210 right = integral_promotion(state, right);
11211
11212 op = (tok == TOK_SL)? OP_SL :
11213 is_signed(left->type)? OP_SSR: OP_USR;
11214
11215 def = triple(state, op, left->type, left, right);
11216 break;
11217 default:
11218 done = 1;
11219 break;
11220 }
11221 } while(!done);
11222 return def;
11223}
11224
11225static struct triple *relational_expr(struct compile_state *state)
11226{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011227#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011228#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011229#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011230 struct triple *def;
11231 int done;
11232 def = shift_expr(state);
11233 do {
11234 struct triple *left, *right;
11235 struct type *arg_type;
11236 int tok, op, sign;
11237 done = 0;
11238 switch((tok = peek(state))) {
11239 case TOK_LESS:
11240 case TOK_MORE:
11241 case TOK_LESSEQ:
11242 case TOK_MOREEQ:
11243 left = read_expr(state, def);
11244 arithmetic(state, left);
11245
11246 eat(state, tok);
11247
11248 right = read_expr(state, shift_expr(state));
11249 arithmetic(state, right);
11250
11251 arg_type = arithmetic_result(state, left, right);
11252 sign = is_signed(arg_type);
11253 op = -1;
11254 switch(tok) {
11255 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11256 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11257 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11258 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11259 }
11260 def = triple(state, op, &int_type, left, right);
11261 break;
11262 default:
11263 done = 1;
11264 break;
11265 }
11266 } while(!done);
11267 return def;
11268}
11269
11270static struct triple *equality_expr(struct compile_state *state)
11271{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011272#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011273#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011274#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011275 struct triple *def;
11276 int done;
11277 def = relational_expr(state);
11278 do {
11279 struct triple *left, *right;
11280 int tok, op;
11281 done = 0;
11282 switch((tok = peek(state))) {
11283 case TOK_EQEQ:
11284 case TOK_NOTEQ:
11285 left = read_expr(state, def);
11286 arithmetic(state, left);
11287 eat(state, tok);
11288 right = read_expr(state, relational_expr(state));
11289 arithmetic(state, right);
11290 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11291 def = triple(state, op, &int_type, left, right);
11292 break;
11293 default:
11294 done = 1;
11295 break;
11296 }
11297 } while(!done);
11298 return def;
11299}
11300
11301static struct triple *and_expr(struct compile_state *state)
11302{
11303 struct triple *def;
11304 def = equality_expr(state);
11305 while(peek(state) == TOK_AND) {
11306 struct triple *left, *right;
11307 struct type *result_type;
11308 left = read_expr(state, def);
11309 integral(state, left);
11310 eat(state, TOK_AND);
11311 right = read_expr(state, equality_expr(state));
11312 integral(state, right);
11313 result_type = arithmetic_result(state, left, right);
11314 def = triple(state, OP_AND, result_type, left, right);
11315 }
11316 return def;
11317}
11318
11319static struct triple *xor_expr(struct compile_state *state)
11320{
11321 struct triple *def;
11322 def = and_expr(state);
11323 while(peek(state) == TOK_XOR) {
11324 struct triple *left, *right;
11325 struct type *result_type;
11326 left = read_expr(state, def);
11327 integral(state, left);
11328 eat(state, TOK_XOR);
11329 right = read_expr(state, and_expr(state));
11330 integral(state, right);
11331 result_type = arithmetic_result(state, left, right);
11332 def = triple(state, OP_XOR, result_type, left, right);
11333 }
11334 return def;
11335}
11336
11337static struct triple *or_expr(struct compile_state *state)
11338{
11339 struct triple *def;
11340 def = xor_expr(state);
11341 while(peek(state) == TOK_OR) {
11342 struct triple *left, *right;
11343 struct type *result_type;
11344 left = read_expr(state, def);
11345 integral(state, left);
11346 eat(state, TOK_OR);
11347 right = read_expr(state, xor_expr(state));
11348 integral(state, right);
11349 result_type = arithmetic_result(state, left, right);
11350 def = triple(state, OP_OR, result_type, left, right);
11351 }
11352 return def;
11353}
11354
11355static struct triple *land_expr(struct compile_state *state)
11356{
11357 struct triple *def;
11358 def = or_expr(state);
11359 while(peek(state) == TOK_LOGAND) {
11360 struct triple *left, *right;
11361 left = read_expr(state, def);
11362 bool(state, left);
11363 eat(state, TOK_LOGAND);
11364 right = read_expr(state, or_expr(state));
11365 bool(state, right);
11366
Eric Biederman90089602004-05-28 14:11:54 +000011367 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011368 ltrue_expr(state, left),
11369 ltrue_expr(state, right));
11370 }
11371 return def;
11372}
11373
11374static struct triple *lor_expr(struct compile_state *state)
11375{
11376 struct triple *def;
11377 def = land_expr(state);
11378 while(peek(state) == TOK_LOGOR) {
11379 struct triple *left, *right;
11380 left = read_expr(state, def);
11381 bool(state, left);
11382 eat(state, TOK_LOGOR);
11383 right = read_expr(state, land_expr(state));
11384 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011385
11386 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011387 ltrue_expr(state, left),
11388 ltrue_expr(state, right));
11389 }
11390 return def;
11391}
11392
11393static struct triple *conditional_expr(struct compile_state *state)
11394{
11395 struct triple *def;
11396 def = lor_expr(state);
11397 if (peek(state) == TOK_QUEST) {
11398 struct triple *test, *left, *right;
11399 bool(state, def);
11400 test = ltrue_expr(state, read_expr(state, def));
11401 eat(state, TOK_QUEST);
11402 left = read_expr(state, expr(state));
11403 eat(state, TOK_COLON);
11404 right = read_expr(state, conditional_expr(state));
11405
Eric Biederman90089602004-05-28 14:11:54 +000011406 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011407 }
11408 return def;
11409}
11410
Eric Biederman41203d92004-11-08 09:31:09 +000011411struct cv_triple {
11412 struct triple *val;
11413 int id;
11414};
11415
11416static void set_cv(struct compile_state *state, struct cv_triple *cv,
11417 struct triple *dest, struct triple *val)
11418{
11419 if (cv[dest->id].val) {
11420 free_triple(state, cv[dest->id].val);
11421 }
11422 cv[dest->id].val = val;
11423}
11424static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11425 struct triple *src)
11426{
11427 return cv[src->id].val;
11428}
11429
Eric Biedermanb138ac82003-04-22 18:44:01 +000011430static struct triple *eval_const_expr(
11431 struct compile_state *state, struct triple *expr)
11432{
11433 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011434 if (is_const(expr)) {
11435 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011436 }
Eric Biederman00443072003-06-24 12:34:45 +000011437 else {
11438 /* If we don't start out as a constant simplify into one */
11439 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011440 struct cv_triple *cv;
11441 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011442 head = label(state); /* dummy initial triple */
11443 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011444 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011445 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011446 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011447 }
Eric Biederman41203d92004-11-08 09:31:09 +000011448 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11449 i = 1;
11450 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11451 cv[i].val = 0;
11452 cv[i].id = ptr->id;
11453 ptr->id = i;
11454 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011455 }
Eric Biederman41203d92004-11-08 09:31:09 +000011456 ptr = head->next;
11457 do {
11458 valid_ins(state, ptr);
11459 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
11460 internal_error(state, ptr,
11461 "unexpected %s in constant expression",
11462 tops(ptr->op));
11463 }
11464 else if (ptr->op == OP_LIST) {
11465 }
11466 else if (triple_is_structural(state, ptr)) {
11467 ptr = ptr->next;
11468 }
11469 else if (triple_is_ubranch(state, ptr)) {
11470 ptr = TARG(ptr, 0);
11471 }
11472 else if (triple_is_cbranch(state, ptr)) {
11473 struct triple *cond_val;
11474 cond_val = get_cv(state, cv, RHS(ptr, 0));
11475 if (!cond_val || !is_const(cond_val) ||
11476 (cond_val->op != OP_INTCONST))
11477 {
11478 internal_error(state, ptr, "bad branch condition");
11479 }
11480 if (cond_val->u.cval == 0) {
11481 ptr = ptr->next;
11482 } else {
11483 ptr = TARG(ptr, 0);
11484 }
11485 }
11486 else if (triple_is_branch(state, ptr)) {
11487 error(state, ptr, "bad branch type in constant expression");
11488 }
11489 else if (ptr->op == OP_WRITE) {
11490 struct triple *val;
11491 val = get_cv(state, cv, RHS(ptr, 0));
11492
11493 set_cv(state, cv, MISC(ptr, 0),
11494 copy_triple(state, val));
11495 set_cv(state, cv, ptr,
11496 copy_triple(state, val));
11497 ptr = ptr->next;
11498 }
11499 else if (ptr->op == OP_READ) {
11500 set_cv(state, cv, ptr,
11501 copy_triple(state,
11502 get_cv(state, cv, RHS(ptr, 0))));
11503 ptr = ptr->next;
11504 }
11505 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11506 struct triple *val, **rhs;
11507 val = copy_triple(state, ptr);
11508 rhs = triple_rhs(state, val, 0);
11509 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11510 if (!*rhs) {
11511 internal_error(state, ptr, "Missing rhs");
11512 }
11513 *rhs = get_cv(state, cv, *rhs);
11514 }
11515 simplify(state, val);
11516 set_cv(state, cv, ptr, val);
11517 ptr = ptr->next;
11518 }
11519 else {
11520 error(state, ptr, "impure operation in constant expression");
11521 }
11522
11523 } while(ptr != head);
11524
11525 /* Get the result value */
11526 def = get_cv(state, cv, head->prev);
11527 cv[head->prev->id].val = 0;
11528
11529 /* Free the temporary values */
11530 for(i = 0; i < count; i++) {
11531 if (cv[i].val) {
11532 free_triple(state, cv[i].val);
11533 cv[i].val = 0;
11534 }
11535 }
11536 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011537 /* Free the intermediate expressions */
11538 while(head->next != head) {
11539 release_triple(state, head->next);
11540 }
11541 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011542 }
Eric Biederman41203d92004-11-08 09:31:09 +000011543 if (!is_const(def)) {
11544 error(state, expr, "Not a constant expression");
11545 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011546 return def;
11547}
11548
11549static struct triple *constant_expr(struct compile_state *state)
11550{
11551 return eval_const_expr(state, conditional_expr(state));
11552}
11553
11554static struct triple *assignment_expr(struct compile_state *state)
11555{
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011556 struct triple *def, *left, *right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011557 int tok, op, sign;
11558 /* The C grammer in K&R shows assignment expressions
11559 * only taking unary expressions as input on their
11560 * left hand side. But specifies the precedence of
11561 * assignemnt as the lowest operator except for comma.
11562 *
11563 * Allowing conditional expressions on the left hand side
11564 * of an assignement results in a grammar that accepts
11565 * a larger set of statements than standard C. As long
11566 * as the subset of the grammar that is standard C behaves
11567 * correctly this should cause no problems.
11568 *
11569 * For the extra token strings accepted by the grammar
11570 * none of them should produce a valid lvalue, so they
11571 * should not produce functioning programs.
11572 *
11573 * GCC has this bug as well, so surprises should be minimal.
11574 */
11575 def = conditional_expr(state);
11576 left = def;
11577 switch((tok = peek(state))) {
11578 case TOK_EQ:
11579 lvalue(state, left);
11580 eat(state, TOK_EQ);
11581 def = write_expr(state, left,
11582 read_expr(state, assignment_expr(state)));
11583 break;
11584 case TOK_TIMESEQ:
11585 case TOK_DIVEQ:
11586 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011587 lvalue(state, left);
11588 arithmetic(state, left);
11589 eat(state, tok);
11590 right = read_expr(state, assignment_expr(state));
11591 arithmetic(state, right);
11592
11593 sign = is_signed(left->type);
11594 op = -1;
11595 switch(tok) {
11596 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11597 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11598 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011599 }
11600 def = write_expr(state, left,
11601 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011602 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011603 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011604 case TOK_PLUSEQ:
11605 lvalue(state, left);
11606 eat(state, TOK_PLUSEQ);
11607 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011608 mk_add_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011609 break;
11610 case TOK_MINUSEQ:
11611 lvalue(state, left);
11612 eat(state, TOK_MINUSEQ);
11613 def = write_expr(state, left,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011614 mk_sub_expr(state, left, assignment_expr(state)));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011615 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011616 case TOK_SLEQ:
11617 case TOK_SREQ:
11618 case TOK_ANDEQ:
11619 case TOK_XOREQ:
11620 case TOK_OREQ:
11621 lvalue(state, left);
11622 integral(state, left);
11623 eat(state, tok);
11624 right = read_expr(state, assignment_expr(state));
11625 integral(state, right);
11626 right = integral_promotion(state, right);
11627 sign = is_signed(left->type);
11628 op = -1;
11629 switch(tok) {
11630 case TOK_SLEQ: op = OP_SL; break;
11631 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11632 case TOK_ANDEQ: op = OP_AND; break;
11633 case TOK_XOREQ: op = OP_XOR; break;
11634 case TOK_OREQ: op = OP_OR; break;
11635 }
11636 def = write_expr(state, left,
11637 triple(state, op, left->type,
Stefan Reinauerc6b0e7e2010-03-16 00:58:36 +000011638 read_expr(state, left), right));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011639 break;
11640 }
11641 return def;
11642}
11643
11644static struct triple *expr(struct compile_state *state)
11645{
11646 struct triple *def;
11647 def = assignment_expr(state);
11648 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011649 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011650 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011651 }
11652 return def;
11653}
11654
11655static void expr_statement(struct compile_state *state, struct triple *first)
11656{
11657 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011658 /* lvalue conversions always apply except when certian operators
11659 * are applied. I apply the lvalue conversions here
11660 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011661 */
11662 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011663 }
11664 eat(state, TOK_SEMI);
11665}
11666
11667static void if_statement(struct compile_state *state, struct triple *first)
11668{
11669 struct triple *test, *jmp1, *jmp2, *middle, *end;
11670
11671 jmp1 = jmp2 = middle = 0;
11672 eat(state, TOK_IF);
11673 eat(state, TOK_LPAREN);
11674 test = expr(state);
11675 bool(state, test);
11676 /* Cleanup and invert the test */
11677 test = lfalse_expr(state, read_expr(state, test));
11678 eat(state, TOK_RPAREN);
11679 /* Generate the needed pieces */
11680 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011681 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011682 /* Thread the pieces together */
11683 flatten(state, first, test);
11684 flatten(state, first, jmp1);
11685 flatten(state, first, label(state));
11686 statement(state, first);
11687 if (peek(state) == TOK_ELSE) {
11688 eat(state, TOK_ELSE);
11689 /* Generate the rest of the pieces */
11690 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011691 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011692 /* Thread them together */
11693 flatten(state, first, jmp2);
11694 flatten(state, first, middle);
11695 statement(state, first);
11696 flatten(state, first, end);
11697 }
11698 else {
11699 flatten(state, first, middle);
11700 }
11701}
11702
11703static void for_statement(struct compile_state *state, struct triple *first)
11704{
11705 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11706 struct triple *label1, *label2, *label3;
11707 struct hash_entry *ident;
11708
11709 eat(state, TOK_FOR);
11710 eat(state, TOK_LPAREN);
11711 head = test = tail = jmp1 = jmp2 = 0;
11712 if (peek(state) != TOK_SEMI) {
11713 head = expr(state);
11714 }
11715 eat(state, TOK_SEMI);
11716 if (peek(state) != TOK_SEMI) {
11717 test = expr(state);
11718 bool(state, test);
11719 test = ltrue_expr(state, read_expr(state, test));
11720 }
11721 eat(state, TOK_SEMI);
11722 if (peek(state) != TOK_RPAREN) {
11723 tail = expr(state);
11724 }
11725 eat(state, TOK_RPAREN);
11726 /* Generate the needed pieces */
11727 label1 = label(state);
11728 label2 = label(state);
11729 label3 = label(state);
11730 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011731 jmp1 = branch(state, label3, 0);
11732 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011733 }
11734 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011735 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011736 }
11737 end = label(state);
11738 /* Remember where break and continue go */
11739 start_scope(state);
11740 ident = state->i_break;
11741 symbol(state, ident, &ident->sym_ident, end, end->type);
11742 ident = state->i_continue;
11743 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11744 /* Now include the body */
11745 flatten(state, first, head);
11746 flatten(state, first, jmp1);
11747 flatten(state, first, label1);
11748 statement(state, first);
11749 flatten(state, first, label2);
11750 flatten(state, first, tail);
11751 flatten(state, first, label3);
11752 flatten(state, first, test);
11753 flatten(state, first, jmp2);
11754 flatten(state, first, end);
11755 /* Cleanup the break/continue scope */
11756 end_scope(state);
11757}
11758
11759static void while_statement(struct compile_state *state, struct triple *first)
11760{
11761 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11762 struct hash_entry *ident;
11763 eat(state, TOK_WHILE);
11764 eat(state, TOK_LPAREN);
11765 test = expr(state);
11766 bool(state, test);
11767 test = ltrue_expr(state, read_expr(state, test));
11768 eat(state, TOK_RPAREN);
11769 /* Generate the needed pieces */
11770 label1 = label(state);
11771 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011772 jmp1 = branch(state, label2, 0);
11773 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011774 end = label(state);
11775 /* Remember where break and continue go */
11776 start_scope(state);
11777 ident = state->i_break;
11778 symbol(state, ident, &ident->sym_ident, end, end->type);
11779 ident = state->i_continue;
11780 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11781 /* Thread them together */
11782 flatten(state, first, jmp1);
11783 flatten(state, first, label1);
11784 statement(state, first);
11785 flatten(state, first, label2);
11786 flatten(state, first, test);
11787 flatten(state, first, jmp2);
11788 flatten(state, first, end);
11789 /* Cleanup the break/continue scope */
11790 end_scope(state);
11791}
11792
11793static void do_statement(struct compile_state *state, struct triple *first)
11794{
11795 struct triple *label1, *label2, *test, *end;
11796 struct hash_entry *ident;
11797 eat(state, TOK_DO);
11798 /* Generate the needed pieces */
11799 label1 = label(state);
11800 label2 = label(state);
11801 end = label(state);
11802 /* Remember where break and continue go */
11803 start_scope(state);
11804 ident = state->i_break;
11805 symbol(state, ident, &ident->sym_ident, end, end->type);
11806 ident = state->i_continue;
11807 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11808 /* Now include the body */
11809 flatten(state, first, label1);
11810 statement(state, first);
11811 /* Cleanup the break/continue scope */
11812 end_scope(state);
11813 /* Eat the rest of the loop */
11814 eat(state, TOK_WHILE);
11815 eat(state, TOK_LPAREN);
11816 test = read_expr(state, expr(state));
11817 bool(state, test);
11818 eat(state, TOK_RPAREN);
11819 eat(state, TOK_SEMI);
11820 /* Thread the pieces together */
11821 test = ltrue_expr(state, test);
11822 flatten(state, first, label2);
11823 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011824 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011825 flatten(state, first, end);
11826}
11827
11828
11829static void return_statement(struct compile_state *state, struct triple *first)
11830{
11831 struct triple *jmp, *mv, *dest, *var, *val;
11832 int last;
11833 eat(state, TOK_RETURN);
11834
Stefan Reinauer50542a82007-10-24 11:14:14 +000011835#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011836#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011837#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011838 val = 0;
11839 /* If we have a return value do some more work */
11840 if (peek(state) != TOK_SEMI) {
11841 val = read_expr(state, expr(state));
11842 }
11843 eat(state, TOK_SEMI);
11844
11845 /* See if this last statement in a function */
11846 last = ((peek(state) == TOK_RBRACE) &&
11847 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11848
11849 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011850 var = fresult(state, state->main_function);
11851
Eric Biedermanb138ac82003-04-22 18:44:01 +000011852 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011853 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011854 mv = jmp = 0;
11855 /* If needed generate a jump instruction */
11856 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011857 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011858 }
11859 /* If needed generate an assignment instruction */
11860 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011861 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011862 }
11863 /* Now put the code together */
11864 if (mv) {
11865 flatten(state, first, mv);
11866 flatten(state, first, jmp);
11867 }
11868 else if (jmp) {
11869 flatten(state, first, jmp);
11870 }
11871}
11872
11873static void break_statement(struct compile_state *state, struct triple *first)
11874{
11875 struct triple *dest;
11876 eat(state, TOK_BREAK);
11877 eat(state, TOK_SEMI);
11878 if (!state->i_break->sym_ident) {
11879 error(state, 0, "break statement not within loop or switch");
11880 }
11881 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011882 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011883}
11884
11885static void continue_statement(struct compile_state *state, struct triple *first)
11886{
11887 struct triple *dest;
11888 eat(state, TOK_CONTINUE);
11889 eat(state, TOK_SEMI);
11890 if (!state->i_continue->sym_ident) {
11891 error(state, 0, "continue statement outside of a loop");
11892 }
11893 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011894 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011895}
11896
11897static void goto_statement(struct compile_state *state, struct triple *first)
11898{
Eric Biederman153ea352003-06-20 14:43:20 +000011899 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011900 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011901 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011902 if (!ident->sym_label) {
11903 /* If this is a forward branch allocate the label now,
11904 * it will be flattend in the appropriate location later.
11905 */
11906 struct triple *ins;
11907 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011908 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011909 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011910 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011911
11912 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011913}
11914
11915static void labeled_statement(struct compile_state *state, struct triple *first)
11916{
Eric Biederman153ea352003-06-20 14:43:20 +000011917 struct triple *ins;
11918 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011919
Eric Biederman41203d92004-11-08 09:31:09 +000011920 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011921 if (ident->sym_label && ident->sym_label->def) {
11922 ins = ident->sym_label->def;
11923 put_occurance(ins->occurance);
11924 ins->occurance = new_occurance(state);
11925 }
11926 else {
11927 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011928 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011929 }
11930 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11931 error(state, 0, "label %s already defined", ident->name);
11932 }
11933 flatten(state, first, ins);
11934
Eric Biedermanb138ac82003-04-22 18:44:01 +000011935 eat(state, TOK_COLON);
11936 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011937}
11938
11939static void switch_statement(struct compile_state *state, struct triple *first)
11940{
Eric Biederman83b991a2003-10-11 06:20:25 +000011941 struct triple *value, *top, *end, *dbranch;
11942 struct hash_entry *ident;
11943
11944 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011945 eat(state, TOK_SWITCH);
11946 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011947 value = expr(state);
11948 integral(state, value);
11949 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011950 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011951 /* Generate the needed pieces */
11952 top = label(state);
11953 end = label(state);
11954 dbranch = branch(state, end, 0);
11955 /* Remember where case branches and break goes */
11956 start_scope(state);
11957 ident = state->i_switch;
11958 symbol(state, ident, &ident->sym_ident, value, value->type);
11959 ident = state->i_case;
11960 symbol(state, ident, &ident->sym_ident, top, top->type);
11961 ident = state->i_break;
11962 symbol(state, ident, &ident->sym_ident, end, end->type);
11963 ident = state->i_default;
11964 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11965 /* Thread them together */
11966 flatten(state, first, value);
11967 flatten(state, first, top);
11968 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011969 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011970 flatten(state, first, end);
11971 /* Cleanup the switch scope */
11972 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011973}
11974
11975static void case_statement(struct compile_state *state, struct triple *first)
11976{
Eric Biederman83b991a2003-10-11 06:20:25 +000011977 struct triple *cvalue, *dest, *test, *jmp;
11978 struct triple *ptr, *value, *top, *dbranch;
11979
11980 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011981 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011982 cvalue = constant_expr(state);
11983 integral(state, cvalue);
11984 if (cvalue->op != OP_INTCONST) {
11985 error(state, 0, "integer constant expected");
11986 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011987 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011988 if (!state->i_case->sym_ident) {
11989 error(state, 0, "case statement not within a switch");
11990 }
11991
11992 /* Lookup the interesting pieces */
11993 top = state->i_case->sym_ident->def;
11994 value = state->i_switch->sym_ident->def;
11995 dbranch = state->i_default->sym_ident->def;
11996
11997 /* See if this case label has already been used */
11998 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11999 if (ptr->op != OP_EQ) {
12000 continue;
12001 }
12002 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
12003 error(state, 0, "duplicate case %d statement",
12004 cvalue->u.cval);
12005 }
12006 }
12007 /* Generate the needed pieces */
12008 dest = label(state);
12009 test = triple(state, OP_EQ, &int_type, value, cvalue);
12010 jmp = branch(state, dest, test);
12011 /* Thread the pieces together */
12012 flatten(state, dbranch, test);
12013 flatten(state, dbranch, jmp);
12014 flatten(state, dbranch, label(state));
12015 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012016 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012017}
12018
12019static void default_statement(struct compile_state *state, struct triple *first)
12020{
Eric Biederman83b991a2003-10-11 06:20:25 +000012021 struct triple *dest;
12022 struct triple *dbranch, *end;
12023
12024 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012025 eat(state, TOK_DEFAULT);
12026 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012027
12028 if (!state->i_case->sym_ident) {
12029 error(state, 0, "default statement not within a switch");
12030 }
12031
12032 /* Lookup the interesting pieces */
12033 dbranch = state->i_default->sym_ident->def;
12034 end = state->i_break->sym_ident->def;
12035
12036 /* See if a default statement has already happened */
12037 if (TARG(dbranch, 0) != end) {
12038 error(state, 0, "duplicate default statement");
12039 }
12040
12041 /* Generate the needed pieces */
12042 dest = label(state);
12043
Eric Biederman90089602004-05-28 14:11:54 +000012044 /* Blame the branch on the default statement */
12045 put_occurance(dbranch->occurance);
12046 dbranch->occurance = new_occurance(state);
12047
Eric Biederman83b991a2003-10-11 06:20:25 +000012048 /* Thread the pieces together */
12049 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012050 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012051 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012052 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012053}
12054
12055static void asm_statement(struct compile_state *state, struct triple *first)
12056{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012057 struct asm_info *info;
12058 struct {
12059 struct triple *constraint;
12060 struct triple *expr;
12061 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12062 struct triple *def, *asm_str;
12063 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012064 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012065
Eric Biederman90089602004-05-28 14:11:54 +000012066 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012067 eat(state, TOK_ASM);
12068 /* For now ignore the qualifiers */
12069 switch(peek(state)) {
12070 case TOK_CONST:
12071 eat(state, TOK_CONST);
12072 break;
12073 case TOK_VOLATILE:
12074 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012075 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012076 break;
12077 }
12078 eat(state, TOK_LPAREN);
12079 asm_str = string_constant(state);
12080
12081 colons = 0;
12082 out = in = clobbers = 0;
12083 /* Outputs */
12084 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12085 eat(state, TOK_COLON);
12086 colons++;
12087 more = (peek(state) == TOK_LIT_STRING);
12088 while(more) {
12089 struct triple *var;
12090 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012091 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012092 more = 0;
12093 if (out > MAX_LHS) {
12094 error(state, 0, "Maximum output count exceeded.");
12095 }
12096 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012097 str = constraint->u.blob;
12098 if (str[0] != '=') {
12099 error(state, 0, "Output constraint does not start with =");
12100 }
12101 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012102 eat(state, TOK_LPAREN);
12103 var = conditional_expr(state);
12104 eat(state, TOK_RPAREN);
12105
12106 lvalue(state, var);
12107 out_param[out].constraint = constraint;
12108 out_param[out].expr = var;
12109 if (peek(state) == TOK_COMMA) {
12110 eat(state, TOK_COMMA);
12111 more = 1;
12112 }
12113 out++;
12114 }
12115 }
12116 /* Inputs */
12117 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12118 eat(state, TOK_COLON);
12119 colons++;
12120 more = (peek(state) == TOK_LIT_STRING);
12121 while(more) {
12122 struct triple *val;
12123 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012124 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012125 more = 0;
12126 if (in > MAX_RHS) {
12127 error(state, 0, "Maximum input count exceeded.");
12128 }
12129 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012130 str = constraint->u.blob;
12131 if (digitp(str[0] && str[1] == '\0')) {
12132 int val;
12133 val = digval(str[0]);
12134 if ((val < 0) || (val >= out)) {
12135 error(state, 0, "Invalid input constraint %d", val);
12136 }
12137 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012138 eat(state, TOK_LPAREN);
12139 val = conditional_expr(state);
12140 eat(state, TOK_RPAREN);
12141
12142 in_param[in].constraint = constraint;
12143 in_param[in].expr = val;
12144 if (peek(state) == TOK_COMMA) {
12145 eat(state, TOK_COMMA);
12146 more = 1;
12147 }
12148 in++;
12149 }
12150 }
12151
12152 /* Clobber */
12153 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12154 eat(state, TOK_COLON);
12155 colons++;
12156 more = (peek(state) == TOK_LIT_STRING);
12157 while(more) {
12158 struct triple *clobber;
12159 more = 0;
12160 if ((clobbers + out) > MAX_LHS) {
12161 error(state, 0, "Maximum clobber limit exceeded.");
12162 }
12163 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012164
12165 clob_param[clobbers].constraint = clobber;
12166 if (peek(state) == TOK_COMMA) {
12167 eat(state, TOK_COMMA);
12168 more = 1;
12169 }
12170 clobbers++;
12171 }
12172 }
12173 eat(state, TOK_RPAREN);
12174 eat(state, TOK_SEMI);
12175
12176
12177 info = xcmalloc(sizeof(*info), "asm_info");
12178 info->str = asm_str->u.blob;
12179 free_triple(state, asm_str);
12180
12181 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12182 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012183 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012184
12185 /* Find the register constraints */
12186 for(i = 0; i < out; i++) {
12187 struct triple *constraint;
12188 constraint = out_param[i].constraint;
12189 info->tmpl.lhs[i] = arch_reg_constraint(state,
12190 out_param[i].expr->type, constraint->u.blob);
12191 free_triple(state, constraint);
12192 }
12193 for(; i - out < clobbers; i++) {
12194 struct triple *constraint;
12195 constraint = clob_param[i - out].constraint;
12196 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12197 free_triple(state, constraint);
12198 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012199 for(i = 0; i < in; i++) {
12200 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012201 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012202 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012203 str = constraint->u.blob;
12204 if (digitp(str[0]) && str[1] == '\0') {
12205 struct reg_info cinfo;
12206 int val;
12207 val = digval(str[0]);
12208 cinfo.reg = info->tmpl.lhs[val].reg;
12209 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12210 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12211 if (cinfo.reg == REG_UNSET) {
12212 cinfo.reg = REG_VIRT0 + val;
12213 }
12214 if (cinfo.regcm == 0) {
12215 error(state, 0, "No registers for %d", val);
12216 }
12217 info->tmpl.lhs[val] = cinfo;
12218 info->tmpl.rhs[i] = cinfo;
12219
12220 } else {
12221 info->tmpl.rhs[i] = arch_reg_constraint(state,
12222 in_param[i].expr->type, str);
12223 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012224 free_triple(state, constraint);
12225 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012226
12227 /* Now build the helper expressions */
12228 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012229 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012230 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012231 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012232 for(i = 0; i < (out + clobbers); i++) {
12233 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012234 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012235 if (i < out) {
12236 type = out_param[i].expr->type;
12237 } else {
12238 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12239 if (size >= SIZEOF_LONG) {
12240 type = &ulong_type;
12241 }
12242 else if (size >= SIZEOF_INT) {
12243 type = &uint_type;
12244 }
12245 else if (size >= SIZEOF_SHORT) {
12246 type = &ushort_type;
12247 }
12248 else {
12249 type = &uchar_type;
12250 }
12251 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012252 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012253 piece->u.cval = i;
12254 LHS(def, i) = piece;
12255 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012256 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012257 /* And write the helpers to their destinations */
12258 for(i = 0; i < out; i++) {
12259 struct triple *piece;
12260 piece = LHS(def, i);
12261 flatten(state, first,
12262 write_expr(state, out_param[i].expr, piece));
12263 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012264}
12265
12266
12267static int isdecl(int tok)
12268{
12269 switch(tok) {
12270 case TOK_AUTO:
12271 case TOK_REGISTER:
12272 case TOK_STATIC:
12273 case TOK_EXTERN:
12274 case TOK_TYPEDEF:
12275 case TOK_CONST:
12276 case TOK_RESTRICT:
12277 case TOK_VOLATILE:
12278 case TOK_VOID:
12279 case TOK_CHAR:
12280 case TOK_SHORT:
12281 case TOK_INT:
12282 case TOK_LONG:
12283 case TOK_FLOAT:
12284 case TOK_DOUBLE:
12285 case TOK_SIGNED:
12286 case TOK_UNSIGNED:
12287 case TOK_STRUCT:
12288 case TOK_UNION:
12289 case TOK_ENUM:
12290 case TOK_TYPE_NAME: /* typedef name */
12291 return 1;
12292 default:
12293 return 0;
12294 }
12295}
12296
12297static void compound_statement(struct compile_state *state, struct triple *first)
12298{
12299 eat(state, TOK_LBRACE);
12300 start_scope(state);
12301
12302 /* statement-list opt */
12303 while (peek(state) != TOK_RBRACE) {
12304 statement(state, first);
12305 }
12306 end_scope(state);
12307 eat(state, TOK_RBRACE);
12308}
12309
12310static void statement(struct compile_state *state, struct triple *first)
12311{
12312 int tok;
12313 tok = peek(state);
12314 if (tok == TOK_LBRACE) {
12315 compound_statement(state, first);
12316 }
12317 else if (tok == TOK_IF) {
12318 if_statement(state, first);
12319 }
12320 else if (tok == TOK_FOR) {
12321 for_statement(state, first);
12322 }
12323 else if (tok == TOK_WHILE) {
12324 while_statement(state, first);
12325 }
12326 else if (tok == TOK_DO) {
12327 do_statement(state, first);
12328 }
12329 else if (tok == TOK_RETURN) {
12330 return_statement(state, first);
12331 }
12332 else if (tok == TOK_BREAK) {
12333 break_statement(state, first);
12334 }
12335 else if (tok == TOK_CONTINUE) {
12336 continue_statement(state, first);
12337 }
12338 else if (tok == TOK_GOTO) {
12339 goto_statement(state, first);
12340 }
12341 else if (tok == TOK_SWITCH) {
12342 switch_statement(state, first);
12343 }
12344 else if (tok == TOK_ASM) {
12345 asm_statement(state, first);
12346 }
12347 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
12348 labeled_statement(state, first);
12349 }
12350 else if (tok == TOK_CASE) {
12351 case_statement(state, first);
12352 }
12353 else if (tok == TOK_DEFAULT) {
12354 default_statement(state, first);
12355 }
12356 else if (isdecl(tok)) {
12357 /* This handles C99 intermixing of statements and decls */
12358 decl(state, first);
12359 }
12360 else {
12361 expr_statement(state, first);
12362 }
12363}
12364
12365static struct type *param_decl(struct compile_state *state)
12366{
12367 struct type *type;
12368 struct hash_entry *ident;
12369 /* Cheat so the declarator will know we are not global */
12370 start_scope(state);
12371 ident = 0;
12372 type = decl_specifiers(state);
12373 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012374 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012375 end_scope(state);
12376 return type;
12377}
12378
12379static struct type *param_type_list(struct compile_state *state, struct type *type)
12380{
12381 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012382 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012383 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012384 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012385 while(peek(state) == TOK_COMMA) {
12386 eat(state, TOK_COMMA);
12387 if (peek(state) == TOK_DOTS) {
12388 eat(state, TOK_DOTS);
12389 error(state, 0, "variadic functions not supported");
12390 }
12391 else {
12392 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12393 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012394 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012395 }
12396 }
12397 return ftype;
12398}
12399
Eric Biedermanb138ac82003-04-22 18:44:01 +000012400static struct type *type_name(struct compile_state *state)
12401{
12402 struct type *type;
12403 type = specifier_qualifier_list(state);
12404 /* abstract-declarator (may consume no tokens) */
12405 type = declarator(state, type, 0, 0);
12406 return type;
12407}
12408
12409static struct type *direct_declarator(
12410 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012411 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012412{
Eric Biederman41203d92004-11-08 09:31:09 +000012413 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012414 struct type *outer;
12415 int op;
12416 outer = 0;
12417 arrays_complete(state, type);
12418 switch(peek(state)) {
12419 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012420 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012421 if (!ident) {
12422 error(state, 0, "Unexpected identifier found");
12423 }
12424 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012425 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012426 break;
12427 case TOK_LPAREN:
12428 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012429 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012430 eat(state, TOK_RPAREN);
12431 break;
12432 default:
12433 if (need_ident) {
12434 error(state, 0, "Identifier expected");
12435 }
12436 break;
12437 }
12438 do {
12439 op = 1;
12440 arrays_complete(state, type);
12441 switch(peek(state)) {
12442 case TOK_LPAREN:
12443 eat(state, TOK_LPAREN);
12444 type = param_type_list(state, type);
12445 eat(state, TOK_RPAREN);
12446 break;
12447 case TOK_LBRACKET:
12448 {
12449 unsigned int qualifiers;
12450 struct triple *value;
12451 value = 0;
12452 eat(state, TOK_LBRACKET);
12453 if (peek(state) != TOK_RBRACKET) {
12454 value = constant_expr(state);
12455 integral(state, value);
12456 }
12457 eat(state, TOK_RBRACKET);
12458
12459 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12460 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12461 if (value) {
12462 type->elements = value->u.cval;
12463 free_triple(state, value);
12464 } else {
12465 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12466 op = 0;
12467 }
12468 }
12469 break;
12470 default:
12471 op = 0;
12472 break;
12473 }
12474 } while(op);
12475 if (outer) {
12476 struct type *inner;
12477 arrays_complete(state, type);
12478 FINISHME();
12479 for(inner = outer; inner->left; inner = inner->left)
12480 ;
12481 inner->left = type;
12482 type = outer;
12483 }
12484 return type;
12485}
12486
12487static struct type *declarator(
12488 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012489 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012490{
12491 while(peek(state) == TOK_STAR) {
12492 eat(state, TOK_STAR);
12493 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12494 }
Eric Biederman41203d92004-11-08 09:31:09 +000012495 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012496 return type;
12497}
12498
Eric Biedermanb138ac82003-04-22 18:44:01 +000012499static struct type *typedef_name(
12500 struct compile_state *state, unsigned int specifiers)
12501{
12502 struct hash_entry *ident;
12503 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012504 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012505 type = ident->sym_ident->type;
12506 specifiers |= type->type & QUAL_MASK;
12507 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
12508 (type->type & (STOR_MASK | QUAL_MASK))) {
12509 type = clone_type(specifiers, type);
12510 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012511 return type;
12512}
12513
12514static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012515 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012516{
Eric Biederman83b991a2003-10-11 06:20:25 +000012517 struct hash_entry *ident;
12518 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012519 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012520 struct type *enum_type;
12521 enum_type = 0;
12522 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012523 eat(state, TOK_ENUM);
12524 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012525 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012526 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012527 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012528 base = 0;
12529 if (!ident || (peek(state) == TOK_LBRACE)) {
12530 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012531 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012532 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12533 enum_type->type_ident = ident;
12534 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012535 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012536 struct hash_entry *eident;
12537 struct triple *value;
12538 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012539 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012540 if (eident->sym_ident) {
12541 error(state, 0, "%s already declared",
12542 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012543 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012544 eident->tok = TOK_ENUM_CONST;
12545 if (peek(state) == TOK_EQ) {
12546 struct triple *val;
12547 eat(state, TOK_EQ);
12548 val = constant_expr(state);
12549 integral(state, val);
12550 base = val->u.cval;
12551 }
12552 value = int_const(state, &int_type, base);
12553 symbol(state, eident, &eident->sym_ident, value, &int_type);
12554 entry = new_type(TYPE_LIST, 0, 0);
12555 entry->field_ident = eident;
12556 *next = entry;
12557 next = &entry->right;
12558 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012559 if (peek(state) == TOK_COMMA) {
12560 eat(state, TOK_COMMA);
12561 }
12562 } while(peek(state) != TOK_RBRACE);
12563 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012564 if (ident) {
12565 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12566 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012567 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012568 if (ident && ident->sym_tag &&
12569 ident->sym_tag->type &&
12570 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12571 enum_type = clone_type(spec, ident->sym_tag->type);
12572 }
12573 else if (ident && !enum_type) {
12574 error(state, 0, "enum %s undeclared", ident->name);
12575 }
12576 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012577}
12578
Eric Biedermanb138ac82003-04-22 18:44:01 +000012579static struct type *struct_declarator(
12580 struct compile_state *state, struct type *type, struct hash_entry **ident)
12581{
Eric Biederman90089602004-05-28 14:11:54 +000012582 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012583 type = declarator(state, type, ident, 1);
12584 }
Eric Biederman90089602004-05-28 14:11:54 +000012585 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012586 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012587 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012588 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012589 if (value->op != OP_INTCONST) {
12590 error(state, 0, "Invalid constant expression");
12591 }
12592 if (value->u.cval > size_of(state, type)) {
12593 error(state, 0, "bitfield larger than base type");
12594 }
12595 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12596 error(state, 0, "bitfield base not an integer type");
12597 }
12598 type = new_type(TYPE_BITFIELD, type, 0);
12599 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012600 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012601 return type;
12602}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012603
12604static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012605 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012606{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012607 struct type *struct_type;
12608 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012609 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012610 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012611 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012612 struct_type = 0;
12613 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012614 switch(peek(state)) {
12615 case TOK_STRUCT:
12616 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012617 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012618 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012619 break;
12620 case TOK_UNION:
12621 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012622 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012623 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012624 break;
12625 default:
12626 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012627 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012628 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012629 break;
12630 }
12631 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012632 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012633 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012634 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012635 if (!ident || (peek(state) == TOK_LBRACE)) {
12636 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012637 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012638 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012639 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012640 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012641 do {
12642 struct type *base_type;
12643 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012644 base_type = specifier_qualifier_list(state);
12645 do {
12646 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012647 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012648 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012649 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012650 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012651 if (peek(state) == TOK_COMMA) {
12652 done = 0;
12653 eat(state, TOK_COMMA);
12654 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012655 type = clone_type(0, type);
12656 type->field_ident = fident;
12657 if (*next) {
12658 *next = new_type(type_join, *next, type);
12659 next = &((*next)->right);
12660 } else {
12661 *next = type;
12662 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012663 } while(!done);
12664 eat(state, TOK_SEMI);
12665 } while(peek(state) != TOK_RBRACE);
12666 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012667 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012668 struct_type->type_ident = ident;
12669 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012670 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012671 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012672 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012673 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012674 if (ident && ident->sym_tag &&
12675 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012676 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012677 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012678 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012679 else if (ident && !struct_type) {
Eric Biederman90089602004-05-28 14:11:54 +000012680 error(state, 0, "%s %s undeclared",
12681 (type_main == TYPE_STRUCT)?"struct" : "union",
12682 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012683 }
12684 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012685}
12686
12687static unsigned int storage_class_specifier_opt(struct compile_state *state)
12688{
12689 unsigned int specifiers;
12690 switch(peek(state)) {
12691 case TOK_AUTO:
12692 eat(state, TOK_AUTO);
12693 specifiers = STOR_AUTO;
12694 break;
12695 case TOK_REGISTER:
12696 eat(state, TOK_REGISTER);
12697 specifiers = STOR_REGISTER;
12698 break;
12699 case TOK_STATIC:
12700 eat(state, TOK_STATIC);
12701 specifiers = STOR_STATIC;
12702 break;
12703 case TOK_EXTERN:
12704 eat(state, TOK_EXTERN);
12705 specifiers = STOR_EXTERN;
12706 break;
12707 case TOK_TYPEDEF:
12708 eat(state, TOK_TYPEDEF);
12709 specifiers = STOR_TYPEDEF;
12710 break;
12711 default:
12712 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012713 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012714 }
12715 else {
12716 specifiers = STOR_AUTO;
12717 }
12718 }
12719 return specifiers;
12720}
12721
12722static unsigned int function_specifier_opt(struct compile_state *state)
12723{
12724 /* Ignore the inline keyword */
12725 unsigned int specifiers;
12726 specifiers = 0;
12727 switch(peek(state)) {
12728 case TOK_INLINE:
12729 eat(state, TOK_INLINE);
12730 specifiers = STOR_INLINE;
12731 }
12732 return specifiers;
12733}
12734
Eric Biederman90089602004-05-28 14:11:54 +000012735static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12736{
12737 int tok = peek(state);
12738 switch(tok) {
12739 case TOK_COMMA:
12740 case TOK_LPAREN:
12741 /* The empty attribute ignore it */
12742 break;
12743 case TOK_IDENT:
12744 case TOK_ENUM_CONST:
12745 case TOK_TYPE_NAME:
12746 {
12747 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012748 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012749
12750 if (ident == state->i_noinline) {
12751 if (attributes & ATTRIB_ALWAYS_INLINE) {
12752 error(state, 0, "both always_inline and noinline attribtes");
12753 }
12754 attributes |= ATTRIB_NOINLINE;
12755 }
12756 else if (ident == state->i_always_inline) {
12757 if (attributes & ATTRIB_NOINLINE) {
12758 error(state, 0, "both noinline and always_inline attribtes");
12759 }
12760 attributes |= ATTRIB_ALWAYS_INLINE;
12761 }
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000012762 else if (ident == state->i_noreturn) {
12763 // attribute((noreturn)) does nothing (yet?)
12764 }
Eric Biederman90089602004-05-28 14:11:54 +000012765 else {
12766 error(state, 0, "Unknown attribute:%s", ident->name);
12767 }
12768 break;
12769 }
12770 default:
12771 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12772 break;
12773 }
12774 return attributes;
12775}
12776
12777static unsigned int attribute_list(struct compile_state *state, unsigned type)
12778{
12779 type = attrib(state, type);
12780 while(peek(state) == TOK_COMMA) {
12781 eat(state, TOK_COMMA);
12782 type = attrib(state, type);
12783 }
12784 return type;
12785}
12786
12787static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12788{
12789 if (peek(state) == TOK_ATTRIBUTE) {
12790 eat(state, TOK_ATTRIBUTE);
12791 eat(state, TOK_LPAREN);
12792 eat(state, TOK_LPAREN);
12793 type = attribute_list(state, type);
12794 eat(state, TOK_RPAREN);
12795 eat(state, TOK_RPAREN);
12796 }
12797 return type;
12798}
12799
Eric Biedermanb138ac82003-04-22 18:44:01 +000012800static unsigned int type_qualifiers(struct compile_state *state)
12801{
12802 unsigned int specifiers;
12803 int done;
12804 done = 0;
12805 specifiers = QUAL_NONE;
12806 do {
12807 switch(peek(state)) {
12808 case TOK_CONST:
12809 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012810 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012811 break;
12812 case TOK_VOLATILE:
12813 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012814 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012815 break;
12816 case TOK_RESTRICT:
12817 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012818 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012819 break;
12820 default:
12821 done = 1;
12822 break;
12823 }
12824 } while(!done);
12825 return specifiers;
12826}
12827
12828static struct type *type_specifier(
12829 struct compile_state *state, unsigned int spec)
12830{
12831 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012832 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012833 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012834 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012835 case TOK_VOID:
12836 eat(state, TOK_VOID);
12837 type = new_type(TYPE_VOID | spec, 0, 0);
12838 break;
12839 case TOK_CHAR:
12840 eat(state, TOK_CHAR);
12841 type = new_type(TYPE_CHAR | spec, 0, 0);
12842 break;
12843 case TOK_SHORT:
12844 eat(state, TOK_SHORT);
12845 if (peek(state) == TOK_INT) {
12846 eat(state, TOK_INT);
12847 }
12848 type = new_type(TYPE_SHORT | spec, 0, 0);
12849 break;
12850 case TOK_INT:
12851 eat(state, TOK_INT);
12852 type = new_type(TYPE_INT | spec, 0, 0);
12853 break;
12854 case TOK_LONG:
12855 eat(state, TOK_LONG);
12856 switch(peek(state)) {
12857 case TOK_LONG:
12858 eat(state, TOK_LONG);
12859 error(state, 0, "long long not supported");
12860 break;
12861 case TOK_DOUBLE:
12862 eat(state, TOK_DOUBLE);
12863 error(state, 0, "long double not supported");
12864 break;
12865 case TOK_INT:
12866 eat(state, TOK_INT);
12867 type = new_type(TYPE_LONG | spec, 0, 0);
12868 break;
12869 default:
12870 type = new_type(TYPE_LONG | spec, 0, 0);
12871 break;
12872 }
12873 break;
12874 case TOK_FLOAT:
12875 eat(state, TOK_FLOAT);
12876 error(state, 0, "type float not supported");
12877 break;
12878 case TOK_DOUBLE:
12879 eat(state, TOK_DOUBLE);
12880 error(state, 0, "type double not supported");
12881 break;
12882 case TOK_SIGNED:
12883 eat(state, TOK_SIGNED);
12884 switch(peek(state)) {
12885 case TOK_LONG:
12886 eat(state, TOK_LONG);
12887 switch(peek(state)) {
12888 case TOK_LONG:
12889 eat(state, TOK_LONG);
12890 error(state, 0, "type long long not supported");
12891 break;
12892 case TOK_INT:
12893 eat(state, TOK_INT);
12894 type = new_type(TYPE_LONG | spec, 0, 0);
12895 break;
12896 default:
12897 type = new_type(TYPE_LONG | spec, 0, 0);
12898 break;
12899 }
12900 break;
12901 case TOK_INT:
12902 eat(state, TOK_INT);
12903 type = new_type(TYPE_INT | spec, 0, 0);
12904 break;
12905 case TOK_SHORT:
12906 eat(state, TOK_SHORT);
12907 type = new_type(TYPE_SHORT | spec, 0, 0);
12908 break;
12909 case TOK_CHAR:
12910 eat(state, TOK_CHAR);
12911 type = new_type(TYPE_CHAR | spec, 0, 0);
12912 break;
12913 default:
12914 type = new_type(TYPE_INT | spec, 0, 0);
12915 break;
12916 }
12917 break;
12918 case TOK_UNSIGNED:
12919 eat(state, TOK_UNSIGNED);
12920 switch(peek(state)) {
12921 case TOK_LONG:
12922 eat(state, TOK_LONG);
12923 switch(peek(state)) {
12924 case TOK_LONG:
12925 eat(state, TOK_LONG);
12926 error(state, 0, "unsigned long long not supported");
12927 break;
12928 case TOK_INT:
12929 eat(state, TOK_INT);
12930 type = new_type(TYPE_ULONG | spec, 0, 0);
12931 break;
12932 default:
12933 type = new_type(TYPE_ULONG | spec, 0, 0);
12934 break;
12935 }
12936 break;
12937 case TOK_INT:
12938 eat(state, TOK_INT);
12939 type = new_type(TYPE_UINT | spec, 0, 0);
12940 break;
12941 case TOK_SHORT:
12942 eat(state, TOK_SHORT);
12943 type = new_type(TYPE_USHORT | spec, 0, 0);
12944 break;
12945 case TOK_CHAR:
12946 eat(state, TOK_CHAR);
12947 type = new_type(TYPE_UCHAR | spec, 0, 0);
12948 break;
12949 default:
12950 type = new_type(TYPE_UINT | spec, 0, 0);
12951 break;
12952 }
12953 break;
12954 /* struct or union specifier */
12955 case TOK_STRUCT:
12956 case TOK_UNION:
12957 type = struct_or_union_specifier(state, spec);
12958 break;
12959 /* enum-spefifier */
12960 case TOK_ENUM:
12961 type = enum_specifier(state, spec);
12962 break;
12963 /* typedef name */
12964 case TOK_TYPE_NAME:
12965 type = typedef_name(state, spec);
12966 break;
12967 default:
12968 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012969 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012970 break;
12971 }
12972 return type;
12973}
12974
12975static int istype(int tok)
12976{
12977 switch(tok) {
12978 case TOK_CONST:
12979 case TOK_RESTRICT:
12980 case TOK_VOLATILE:
12981 case TOK_VOID:
12982 case TOK_CHAR:
12983 case TOK_SHORT:
12984 case TOK_INT:
12985 case TOK_LONG:
12986 case TOK_FLOAT:
12987 case TOK_DOUBLE:
12988 case TOK_SIGNED:
12989 case TOK_UNSIGNED:
12990 case TOK_STRUCT:
12991 case TOK_UNION:
12992 case TOK_ENUM:
12993 case TOK_TYPE_NAME:
12994 return 1;
12995 default:
12996 return 0;
12997 }
12998}
12999
13000
13001static struct type *specifier_qualifier_list(struct compile_state *state)
13002{
13003 struct type *type;
13004 unsigned int specifiers = 0;
13005
13006 /* type qualifiers */
13007 specifiers |= type_qualifiers(state);
13008
13009 /* type specifier */
13010 type = type_specifier(state, specifiers);
13011
13012 return type;
13013}
13014
Stefan Reinauer50542a82007-10-24 11:14:14 +000013015#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013016static int isdecl_specifier(int tok)
13017{
13018 switch(tok) {
13019 /* storage class specifier */
13020 case TOK_AUTO:
13021 case TOK_REGISTER:
13022 case TOK_STATIC:
13023 case TOK_EXTERN:
13024 case TOK_TYPEDEF:
13025 /* type qualifier */
13026 case TOK_CONST:
13027 case TOK_RESTRICT:
13028 case TOK_VOLATILE:
13029 /* type specifiers */
13030 case TOK_VOID:
13031 case TOK_CHAR:
13032 case TOK_SHORT:
13033 case TOK_INT:
13034 case TOK_LONG:
13035 case TOK_FLOAT:
13036 case TOK_DOUBLE:
13037 case TOK_SIGNED:
13038 case TOK_UNSIGNED:
13039 /* struct or union specifier */
13040 case TOK_STRUCT:
13041 case TOK_UNION:
13042 /* enum-spefifier */
13043 case TOK_ENUM:
13044 /* typedef name */
13045 case TOK_TYPE_NAME:
13046 /* function specifiers */
13047 case TOK_INLINE:
13048 return 1;
13049 default:
13050 return 0;
13051 }
13052}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013053#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013054
13055static struct type *decl_specifiers(struct compile_state *state)
13056{
13057 struct type *type;
13058 unsigned int specifiers;
13059 /* I am overly restrictive in the arragement of specifiers supported.
13060 * C is overly flexible in this department it makes interpreting
13061 * the parse tree difficult.
13062 */
13063 specifiers = 0;
13064
13065 /* storage class specifier */
13066 specifiers |= storage_class_specifier_opt(state);
13067
13068 /* function-specifier */
13069 specifiers |= function_specifier_opt(state);
13070
Eric Biederman90089602004-05-28 14:11:54 +000013071 /* attributes */
13072 specifiers |= attributes_opt(state, 0);
13073
Eric Biedermanb138ac82003-04-22 18:44:01 +000013074 /* type qualifier */
13075 specifiers |= type_qualifiers(state);
13076
13077 /* type specifier */
13078 type = type_specifier(state, specifiers);
13079 return type;
13080}
13081
Eric Biederman00443072003-06-24 12:34:45 +000013082struct field_info {
13083 struct type *type;
13084 size_t offset;
13085};
13086
13087static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013088{
13089 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013090 struct field_info info;
13091 info.offset = ~0U;
13092 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013093 do {
13094 switch(peek(state)) {
13095 case TOK_LBRACKET:
13096 {
13097 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013098 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13099 error(state, 0, "Array designator not in array initializer");
13100 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013101 eat(state, TOK_LBRACKET);
13102 value = constant_expr(state);
13103 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013104
13105 info.type = type->left;
13106 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013107 break;
13108 }
13109 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013110 {
13111 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013112 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13113 ((type->type & TYPE_MASK) != TYPE_UNION))
13114 {
Eric Biederman00443072003-06-24 12:34:45 +000013115 error(state, 0, "Struct designator not in struct initializer");
13116 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013117 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013118 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013119 info.offset = field_offset(state, type, field);
13120 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013121 break;
Eric Biederman00443072003-06-24 12:34:45 +000013122 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013123 default:
13124 error(state, 0, "Invalid designator");
13125 }
13126 tok = peek(state);
13127 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13128 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013129 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013130}
13131
13132static struct triple *initializer(
13133 struct compile_state *state, struct type *type)
13134{
13135 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013136#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013137#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013138#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013139 if (peek(state) != TOK_LBRACE) {
13140 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013141 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13142 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13143 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13144 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13145 (equiv_types(type->left, result->type->left))) {
13146 type->elements = result->type->elements;
13147 }
Eric Biederman90089602004-05-28 14:11:54 +000013148 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013149 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13150 (type->type & TYPE_MASK) != TYPE_ARRAY)
13151 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013152 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013153 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013154 if (!is_init_compatible(state, type, result->type)) {
13155 error(state, 0, "Incompatible types in initializer");
13156 }
13157 if (!equiv_types(type, result->type)) {
13158 result = mk_cast_expr(state, type, result);
13159 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013160 }
13161 else {
13162 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013163 size_t max_offset;
13164 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013165 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013166 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13167 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13168 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013169 }
Eric Biederman00443072003-06-24 12:34:45 +000013170 info.offset = 0;
13171 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013172 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13173 info.type = next_field(state, type, 0);
13174 }
Eric Biederman00443072003-06-24 12:34:45 +000013175 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13176 max_offset = 0;
13177 } else {
13178 max_offset = size_of(state, type);
13179 }
Eric Biederman90089602004-05-28 14:11:54 +000013180 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013181 eat(state, TOK_LBRACE);
13182 do {
13183 struct triple *value;
13184 struct type *value_type;
13185 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013186 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013187 int tok;
13188 comma = 0;
13189 tok = peek(state);
13190 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013191 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013192 }
Eric Biederman00443072003-06-24 12:34:45 +000013193 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13194 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013195 error(state, 0, "element beyond bounds");
13196 }
Eric Biederman00443072003-06-24 12:34:45 +000013197 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013198 value = eval_const_expr(state, initializer(state, value_type));
13199 value_size = size_of(state, value_type);
13200 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013201 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13202 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013203 void *old_buf;
13204 size_t old_size;
13205 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013206 old_size = max_offset;
13207 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013208 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13209 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013210 xfree(old_buf);
13211 }
Eric Biederman90089602004-05-28 14:11:54 +000013212 dest = ((char *)buf) + bits_to_bytes(info.offset);
13213#if DEBUG_INITIALIZER
13214 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
13215 dest - buf,
13216 bits_to_bytes(max_offset),
13217 bits_to_bytes(value_size),
13218 value->op);
13219#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013220 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013221 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013222 }
Eric Biederman90089602004-05-28 14:11:54 +000013223 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13224#if DEBUG_INITIALIZER
13225 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13226#endif
Eric Biederman00443072003-06-24 12:34:45 +000013227 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013228 }
Eric Biederman90089602004-05-28 14:11:54 +000013229 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013230 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013231 }
Eric Biederman90089602004-05-28 14:11:54 +000013232 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013233 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013234 }
13235 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013236 internal_error(state, 0, "unhandled constant initializer");
13237 }
Eric Biederman00443072003-06-24 12:34:45 +000013238 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013239 if (peek(state) == TOK_COMMA) {
13240 eat(state, TOK_COMMA);
13241 comma = 1;
13242 }
Eric Biederman00443072003-06-24 12:34:45 +000013243 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013244 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13245 info.type = next_field(state, type, info.type);
13246 info.offset = field_offset(state, type,
13247 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013248 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013249 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013250 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13251 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13252 type->elements = max_offset / size_of(state, type->left);
13253 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013254 eat(state, TOK_RBRACE);
13255 result = triple(state, OP_BLOBCONST, type, 0, 0);
13256 result->u.blob = buf;
13257 }
13258 return result;
13259}
13260
Eric Biederman90089602004-05-28 14:11:54 +000013261static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013262{
13263 /* Make a second pass and finish anything outstanding
13264 * with respect to branches. The only outstanding item
13265 * is to see if there are goto to labels that have not
13266 * been defined and to error about them.
13267 */
13268 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013269 struct triple *ins;
13270 /* Also error on branches that do not use their targets */
13271 ins = first;
13272 do {
13273 if (!triple_is_ret(state, ins)) {
13274 struct triple **expr ;
13275 struct triple_set *set;
13276 expr = triple_targ(state, ins, 0);
13277 for(; expr; expr = triple_targ(state, ins, expr)) {
13278 struct triple *targ;
13279 targ = *expr;
13280 for(set = targ?targ->use:0; set; set = set->next) {
13281 if (set->member == ins) {
13282 break;
13283 }
13284 }
13285 if (!set) {
13286 internal_error(state, ins, "targ not used");
13287 }
13288 }
13289 }
13290 ins = ins->next;
13291 } while(ins != first);
13292 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013293 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13294 struct hash_entry *entry;
13295 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13296 struct triple *ins;
13297 if (!entry->sym_label) {
13298 continue;
13299 }
13300 ins = entry->sym_label->def;
13301 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13302 error(state, ins, "label `%s' used but not defined",
13303 entry->name);
13304 }
13305 }
13306 }
13307}
13308
Eric Biedermanb138ac82003-04-22 18:44:01 +000013309static struct triple *function_definition(
13310 struct compile_state *state, struct type *type)
13311{
Eric Biederman90089602004-05-28 14:11:54 +000013312 struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13313 struct triple *fname;
13314 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013315 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013316 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013317 int i;
13318 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13319 error(state, 0, "Invalid function header");
13320 }
13321
13322 /* Verify the function type */
13323 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13324 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013325 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013326 error(state, 0, "Invalid function parameters");
13327 }
13328 param = type->right;
13329 i = 0;
13330 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13331 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013332 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013333 error(state, 0, "No identifier for parameter %d\n", i);
13334 }
13335 param = param->right;
13336 }
13337 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013338 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013339 error(state, 0, "No identifier for paramter %d\n", i);
13340 }
13341
13342 /* Get a list of statements for this function. */
13343 def = triple(state, OP_LIST, type, 0, 0);
13344
13345 /* Start a new scope for the passed parameters */
13346 start_scope(state);
13347
13348 /* Put a label at the very start of a function */
13349 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013350 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013351
13352 /* Put a label at the very end of a function */
13353 end = label(state);
13354 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013355 /* Remember where return goes */
13356 ident = state->i_return;
13357 symbol(state, ident, &ident->sym_ident, end, end->type);
13358
Eric Biederman90089602004-05-28 14:11:54 +000013359 /* Get the initial closure type */
13360 ctype = new_type(TYPE_JOIN, &void_type, 0);
13361 ctype->elements = 1;
13362
13363 /* Add a variable for the return value */
13364 crtype = new_type(TYPE_TUPLE,
13365 /* Remove all type qualifiers from the return type */
13366 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13367 crtype->elements = 2;
13368 result = flatten(state, end, variable(state, crtype));
13369
Eric Biederman5ade04a2003-10-22 04:03:46 +000013370 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013371 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013372
13373 /* Add in the return instruction */
13374 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13375 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013376
13377 /* Walk through the parameters and create symbol table entries
13378 * for them.
13379 */
13380 param = type->right;
13381 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013382 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013383 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013384 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013385 flatten(state, end, tmp);
13386 param = param->right;
13387 }
13388 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13389 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013390 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013391 tmp = variable(state, param);
13392 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13393 flatten(state, end, tmp);
13394 }
Eric Biederman90089602004-05-28 14:11:54 +000013395
13396 /* Add the declaration static const char __func__ [] = "func-name" */
13397 fname_type = new_type(TYPE_ARRAY,
13398 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13399 fname_type->type |= QUAL_CONST | STOR_STATIC;
13400 fname_type->elements = strlen(state->function) + 1;
13401
13402 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13403 fname->u.blob = (void *)state->function;
13404 fname = flatten(state, end, fname);
13405
13406 ident = state->i___func__;
13407 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013408
13409 /* Remember which function I am compiling.
13410 * Also assume the last defined function is the main function.
13411 */
13412 state->main_function = def;
13413
13414 /* Now get the actual function definition */
13415 compound_statement(state, end);
13416
Eric Biederman153ea352003-06-20 14:43:20 +000013417 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013418 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013419
Eric Biedermanb138ac82003-04-22 18:44:01 +000013420 /* Remove the parameter scope */
13421 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013422
Eric Biederman5ade04a2003-10-22 04:03:46 +000013423
13424 /* Remember I have defined a function */
13425 if (!state->functions) {
13426 state->functions = def;
13427 } else {
13428 insert_triple(state, state->functions, def);
13429 }
13430 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013431 FILE *fp = state->dbgout;
13432 fprintf(fp, "\n");
13433 loc(fp, state, 0);
13434 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13435 display_func(state, fp, def);
13436 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013437 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013438
13439 return def;
13440}
13441
13442static struct triple *do_decl(struct compile_state *state,
13443 struct type *type, struct hash_entry *ident)
13444{
13445 struct triple *def;
13446 def = 0;
13447 /* Clean up the storage types used */
13448 switch (type->type & STOR_MASK) {
13449 case STOR_AUTO:
13450 case STOR_STATIC:
13451 /* These are the good types I am aiming for */
13452 break;
13453 case STOR_REGISTER:
13454 type->type &= ~STOR_MASK;
13455 type->type |= STOR_AUTO;
13456 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013457 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013458 case STOR_EXTERN:
13459 type->type &= ~STOR_MASK;
13460 type->type |= STOR_STATIC;
13461 break;
13462 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013463 if (!ident) {
13464 error(state, 0, "typedef without name");
13465 }
13466 symbol(state, ident, &ident->sym_ident, 0, type);
13467 ident->tok = TOK_TYPE_NAME;
13468 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013469 break;
13470 default:
13471 internal_error(state, 0, "Undefined storage class");
13472 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013473 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13474 error(state, 0, "Function prototypes not supported");
13475 }
Eric Biederman00443072003-06-24 12:34:45 +000013476 if (ident &&
13477 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013478 ((type->type & QUAL_CONST) == 0)) {
13479 error(state, 0, "non const static variables not supported");
13480 }
13481 if (ident) {
13482 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013483 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013484 }
13485 return def;
13486}
13487
13488static void decl(struct compile_state *state, struct triple *first)
13489{
13490 struct type *base_type, *type;
13491 struct hash_entry *ident;
13492 struct triple *def;
13493 int global;
13494 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13495 base_type = decl_specifiers(state);
13496 ident = 0;
13497 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013498 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013499 if (global && ident && (peek(state) == TOK_LBRACE)) {
13500 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013501 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013502 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013503 def = function_definition(state, type);
13504 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013505 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013506 }
13507 else {
13508 int done;
13509 flatten(state, first, do_decl(state, type, ident));
13510 /* type or variable definition */
13511 do {
13512 done = 1;
13513 if (peek(state) == TOK_EQ) {
13514 if (!ident) {
13515 error(state, 0, "cannot assign to a type");
13516 }
13517 eat(state, TOK_EQ);
13518 flatten(state, first,
13519 init_expr(state,
13520 ident->sym_ident->def,
13521 initializer(state, type)));
13522 }
13523 arrays_complete(state, type);
13524 if (peek(state) == TOK_COMMA) {
13525 eat(state, TOK_COMMA);
13526 ident = 0;
13527 type = declarator(state, base_type, &ident, 0);
13528 flatten(state, first, do_decl(state, type, ident));
13529 done = 0;
13530 }
13531 } while(!done);
13532 eat(state, TOK_SEMI);
13533 }
13534}
13535
13536static void decls(struct compile_state *state)
13537{
13538 struct triple *list;
13539 int tok;
13540 list = label(state);
13541 while(1) {
13542 tok = peek(state);
13543 if (tok == TOK_EOF) {
13544 return;
13545 }
13546 if (tok == TOK_SPACE) {
13547 eat(state, TOK_SPACE);
13548 }
13549 decl(state, list);
13550 if (list->next != list) {
13551 error(state, 0, "global variables not supported");
13552 }
13553 }
13554}
13555
Eric Biederman5ade04a2003-10-22 04:03:46 +000013556/*
13557 * Function inlining
13558 */
Eric Biederman90089602004-05-28 14:11:54 +000013559struct triple_reg_set {
13560 struct triple_reg_set *next;
13561 struct triple *member;
13562 struct triple *new;
13563};
13564struct reg_block {
13565 struct block *block;
13566 struct triple_reg_set *in;
13567 struct triple_reg_set *out;
13568 int vertex;
13569};
13570static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13571static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13572static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13573static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13574static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13575 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13576 void *arg);
13577static void print_block(
13578 struct compile_state *state, struct block *block, void *arg);
13579static int do_triple_set(struct triple_reg_set **head,
13580 struct triple *member, struct triple *new_member);
13581static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13582static struct reg_block *compute_variable_lifetimes(
13583 struct compile_state *state, struct basic_blocks *bb);
13584static void free_variable_lifetimes(struct compile_state *state,
13585 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013586#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000013587static void print_live_variables(struct compile_state *state,
13588 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013589#endif
Eric Biederman90089602004-05-28 14:11:54 +000013590
Eric Biederman5ade04a2003-10-22 04:03:46 +000013591
13592static struct triple *call(struct compile_state *state,
13593 struct triple *retvar, struct triple *ret_addr,
13594 struct triple *targ, struct triple *ret)
13595{
13596 struct triple *call;
13597
13598 if (!retvar || !is_lvalue(state, retvar)) {
13599 internal_error(state, 0, "writing to a non lvalue?");
13600 }
13601 write_compatible(state, retvar->type, &void_ptr_type);
13602
13603 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13604 TARG(call, 0) = targ;
13605 MISC(call, 0) = ret;
13606 if (!targ || (targ->op != OP_LABEL)) {
13607 internal_error(state, 0, "call not to a label");
13608 }
13609 if (!ret || (ret->op != OP_RET)) {
13610 internal_error(state, 0, "call not matched with return");
13611 }
13612 return call;
13613}
13614
Eric Biederman5ade04a2003-10-22 04:03:46 +000013615static void walk_functions(struct compile_state *state,
13616 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13617 void *arg)
13618{
13619 struct triple *func, *first;
13620 func = first = state->functions;
13621 do {
13622 cb(state, func, arg);
13623 func = func->next;
13624 } while(func != first);
13625}
13626
Eric Biederman90089602004-05-28 14:11:54 +000013627static void reverse_walk_functions(struct compile_state *state,
13628 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13629 void *arg)
13630{
13631 struct triple *func, *first;
13632 func = first = state->functions;
13633 do {
13634 func = func->prev;
13635 cb(state, func, arg);
13636 } while(func != first);
13637}
13638
13639
13640static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13641{
13642 struct triple *ptr, *first;
13643 if (func->u.cval == 0) {
13644 return;
13645 }
13646 ptr = first = RHS(func, 0);
13647 do {
13648 if (ptr->op == OP_FCALL) {
13649 struct triple *called_func;
13650 called_func = MISC(ptr, 0);
13651 /* Mark the called function as used */
13652 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13653 called_func->u.cval++;
13654 }
13655 /* Remove the called function from the list */
13656 called_func->prev->next = called_func->next;
13657 called_func->next->prev = called_func->prev;
13658
13659 /* Place the called function before me on the list */
13660 called_func->next = func;
13661 called_func->prev = func->prev;
13662 called_func->prev->next = called_func;
13663 called_func->next->prev = called_func;
13664 }
13665 ptr = ptr->next;
13666 } while(ptr != first);
13667 func->id |= TRIPLE_FLAG_FLATTENED;
13668}
13669
13670static void mark_live_functions(struct compile_state *state)
13671{
13672 /* Ensure state->main_function is the last function in
13673 * the list of functions.
13674 */
13675 if ((state->main_function->next != state->functions) ||
13676 (state->functions->prev != state->main_function)) {
13677 internal_error(state, 0,
13678 "state->main_function is not at the end of the function list ");
13679 }
13680 state->main_function->u.cval = 1;
13681 reverse_walk_functions(state, mark_live, 0);
13682}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013683
13684static int local_triple(struct compile_state *state,
13685 struct triple *func, struct triple *ins)
13686{
13687 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13688#if 0
13689 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013690 FILE *fp = state->errout;
13691 fprintf(fp, "global: ");
13692 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013693 }
13694#endif
13695 return local;
13696}
13697
13698struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
13699 struct occurance *base_occurance)
13700{
13701 struct triple *nfunc;
13702 struct triple *nfirst, *ofirst;
13703 struct triple *new, *old;
13704
13705 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013706 FILE *fp = state->dbgout;
13707 fprintf(fp, "\n");
13708 loc(fp, state, 0);
13709 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13710 display_func(state, fp, ofunc);
13711 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013712 }
13713
13714 /* Make a new copy of the old function */
13715 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13716 nfirst = 0;
13717 ofirst = old = RHS(ofunc, 0);
13718 do {
13719 struct triple *new;
13720 struct occurance *occurance;
13721 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013722 old_lhs = old->lhs;
13723 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013724 occurance = inline_occurance(state, base_occurance, old->occurance);
13725 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13726 MISC(old, 0)->u.cval += 1;
13727 }
13728 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13729 occurance);
13730 if (!triple_stores_block(state, new)) {
13731 memcpy(&new->u, &old->u, sizeof(new->u));
13732 }
13733 if (!nfirst) {
13734 RHS(nfunc, 0) = nfirst = new;
13735 }
13736 else {
13737 insert_triple(state, nfirst, new);
13738 }
13739 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013740 new->id |= old->id & TRIPLE_FLAG_COPY;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013741
13742 /* During the copy remember new as user of old */
13743 use_triple(old, new);
13744
Eric Biederman5ade04a2003-10-22 04:03:46 +000013745 /* Remember which instructions are local */
13746 old->id |= TRIPLE_FLAG_LOCAL;
13747 old = old->next;
13748 } while(old != ofirst);
13749
13750 /* Make a second pass to fix up any unresolved references */
13751 old = ofirst;
13752 new = nfirst;
13753 do {
13754 struct triple **oexpr, **nexpr;
13755 int count, i;
13756 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013757 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013758 for(i = 0; i < count; i++) {
13759 oexpr = &old->param[i];
13760 nexpr = &new->param[i];
13761 if (*oexpr && !*nexpr) {
13762 if (!local_triple(state, ofunc, *oexpr)) {
13763 *nexpr = *oexpr;
13764 }
13765 else if ((*oexpr)->use) {
13766 *nexpr = (*oexpr)->use->member;
13767 }
13768 if (*nexpr == old) {
13769 internal_error(state, 0, "new == old?");
13770 }
13771 use_triple(*nexpr, new);
13772 }
13773 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013774 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013775 }
13776 }
13777 old = old->next;
13778 new = new->next;
13779 } while((old != ofirst) && (new != nfirst));
13780
13781 /* Make a third pass to cleanup the extra useses */
13782 old = ofirst;
13783 new = nfirst;
13784 do {
13785 unuse_triple(old, new);
13786 /* Forget which instructions are local */
13787 old->id &= ~TRIPLE_FLAG_LOCAL;
13788 old = old->next;
13789 new = new->next;
13790 } while ((old != ofirst) && (new != nfirst));
13791 return nfunc;
13792}
13793
Eric Biederman90089602004-05-28 14:11:54 +000013794static void expand_inline_call(
13795 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013796{
13797 /* Inline the function call */
13798 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013799 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013800 struct triple *end, *nend;
13801 int pvals, i;
13802
13803 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013804 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013805 if (ofunc->op != OP_LIST) {
13806 internal_error(state, 0, "improper function");
13807 }
Eric Biederman90089602004-05-28 14:11:54 +000013808 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013809 /* Prepend the parameter reading into the new function list */
13810 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013811 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013812 for(i = 0; i < pvals; i++) {
13813 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013814 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013815 atype = ptype;
13816 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13817 atype = ptype->left;
13818 }
Eric Biederman90089602004-05-28 14:11:54 +000013819 param = farg(state, nfunc, i);
13820 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13821 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013822 }
Eric Biederman90089602004-05-28 14:11:54 +000013823 arg = RHS(fcall, i);
13824 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013825 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013826 }
13827 result = 0;
13828 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman90089602004-05-28 14:11:54 +000013829 result = read_expr(state,
13830 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013831 }
13832 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013833 FILE *fp = state->dbgout;
13834 fprintf(fp, "\n");
13835 loc(fp, state, 0);
13836 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13837 display_func(state, fp, nfunc);
13838 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013839 }
13840
Eric Biederman90089602004-05-28 14:11:54 +000013841 /*
13842 * Get rid of the extra triples
13843 */
13844 /* Remove the read of the return address */
13845 ins = RHS(nfunc, 0)->prev->prev;
13846 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13847 internal_error(state, ins, "Not return addres read?");
13848 }
13849 release_triple(state, ins);
13850 /* Remove the return instruction */
13851 ins = RHS(nfunc, 0)->prev;
13852 if (ins->op != OP_RET) {
13853 internal_error(state, ins, "Not return?");
13854 }
13855 release_triple(state, ins);
13856 /* Remove the retaddres variable */
13857 retvar = fretaddr(state, nfunc);
13858 if ((retvar->lhs != 1) ||
13859 (retvar->op != OP_ADECL) ||
13860 (retvar->next->op != OP_PIECE) ||
13861 (MISC(retvar->next, 0) != retvar)) {
13862 internal_error(state, retvar, "Not the return address?");
13863 }
13864 release_triple(state, retvar->next);
13865 release_triple(state, retvar);
13866
13867 /* Remove the label at the start of the function */
13868 ins = RHS(nfunc, 0);
13869 if (ins->op != OP_LABEL) {
13870 internal_error(state, ins, "Not label?");
13871 }
13872 nfirst = ins->next;
13873 free_triple(state, ins);
13874 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013875 RHS(nfunc, 0) = 0;
13876 free_triple(state, nfunc);
13877
13878 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013879 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013880 nend = nfirst->prev;
13881 end->next = nfirst;
13882 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013883 nend->next = fcall;
13884 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013885
Eric Biederman90089602004-05-28 14:11:54 +000013886 /* Now the result reading code */
13887 if (result) {
13888 result = flatten(state, fcall, result);
13889 propogate_use(state, fcall, result);
13890 }
13891
13892 /* Release the original fcall instruction */
13893 release_triple(state, fcall);
13894
13895 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013896}
13897
Eric Biederman90089602004-05-28 14:11:54 +000013898/*
13899 *
13900 * Type of the result variable.
13901 *
13902 * result
13903 * |
13904 * +----------+------------+
13905 * | |
13906 * union of closures result_type
13907 * |
13908 * +------------------+---------------+
13909 * | |
13910 * closure1 ... closuerN
13911 * | |
13912 * +----+--+-+--------+-----+ +----+----+---+-----+
13913 * | | | | | | | | |
13914 * var1 var2 var3 ... varN result var1 var2 ... varN result
13915 * |
13916 * +--------+---------+
13917 * | |
13918 * union of closures result_type
13919 * |
13920 * +-----+-------------------+
13921 * | |
13922 * closure1 ... closureN
13923 * | |
13924 * +-----+---+----+----+ +----+---+----+-----+
13925 * | | | | | | | |
13926 * var1 var2 ... varN result var1 var2 ... varN result
13927 */
13928
13929static int add_closure_type(struct compile_state *state,
13930 struct triple *func, struct type *closure_type)
13931{
13932 struct type *type, *ctype, **next;
13933 struct triple *var, *new_var;
13934 int i;
13935
13936#if 0
13937 FILE *fp = state->errout;
13938 fprintf(fp, "original_type: ");
13939 name_of(fp, fresult(state, func)->type);
13940 fprintf(fp, "\n");
13941#endif
13942 /* find the original type */
13943 var = fresult(state, func);
13944 type = var->type;
13945 if (type->elements != 2) {
13946 internal_error(state, var, "bad return type");
13947 }
13948
13949 /* Find the complete closure type and update it */
13950 ctype = type->left->left;
13951 next = &ctype->left;
13952 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13953 next = &(*next)->right;
13954 }
13955 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13956 ctype->elements += 1;
13957
13958#if 0
13959 fprintf(fp, "new_type: ");
13960 name_of(fp, type);
13961 fprintf(fp, "\n");
13962 fprintf(fp, "ctype: %p %d bits: %d ",
13963 ctype, ctype->elements, reg_size_of(state, ctype));
13964 name_of(fp, ctype);
13965 fprintf(fp, "\n");
13966#endif
13967
13968 /* Regenerate the variable with the new type definition */
13969 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13970 new_var->id |= TRIPLE_FLAG_FLATTENED;
13971 for(i = 0; i < new_var->lhs; i++) {
13972 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13973 }
13974
13975 /* Point everyone at the new variable */
13976 propogate_use(state, var, new_var);
13977
13978 /* Release the original variable */
13979 for(i = 0; i < var->lhs; i++) {
13980 release_triple(state, LHS(var, i));
13981 }
13982 release_triple(state, var);
13983
13984 /* Return the index of the added closure type */
13985 return ctype->elements - 1;
13986}
13987
13988static struct triple *closure_expr(struct compile_state *state,
13989 struct triple *func, int closure_idx, int var_idx)
13990{
13991 return deref_index(state,
13992 deref_index(state,
13993 deref_index(state, fresult(state, func), 0),
13994 closure_idx),
13995 var_idx);
13996}
13997
13998
13999static void insert_triple_set(
14000 struct triple_reg_set **head, struct triple *member)
14001{
14002 struct triple_reg_set *new;
14003 new = xcmalloc(sizeof(*new), "triple_set");
14004 new->member = member;
14005 new->new = 0;
14006 new->next = *head;
14007 *head = new;
14008}
14009
14010static int ordered_triple_set(
14011 struct triple_reg_set **head, struct triple *member)
14012{
14013 struct triple_reg_set **ptr;
14014 if (!member)
14015 return 0;
14016 ptr = head;
14017 while(*ptr) {
14018 if (member == (*ptr)->member) {
14019 return 0;
14020 }
14021 /* keep the list ordered */
14022 if (member->id < (*ptr)->member->id) {
14023 break;
14024 }
14025 ptr = &(*ptr)->next;
14026 }
14027 insert_triple_set(ptr, member);
14028 return 1;
14029}
14030
14031
14032static void free_closure_variables(struct compile_state *state,
14033 struct triple_reg_set **enclose)
14034{
14035 struct triple_reg_set *entry, *next;
14036 for(entry = *enclose; entry; entry = next) {
14037 next = entry->next;
14038 do_triple_unset(enclose, entry->member);
14039 }
14040}
14041
14042static int lookup_closure_index(struct compile_state *state,
14043 struct triple *me, struct triple *val)
14044{
14045 struct triple *first, *ins, *next;
14046 first = RHS(me, 0);
14047 ins = next = first;
14048 do {
14049 struct triple *result;
14050 struct triple *index0, *index1, *index2, *read, *write;
14051 ins = next;
14052 next = ins->next;
14053 if (ins->op != OP_CALL) {
14054 continue;
14055 }
14056 /* I am at a previous call point examine it closely */
14057 if (ins->next->op != OP_LABEL) {
14058 internal_error(state, ins, "call not followed by label");
14059 }
14060 /* Does this call does not enclose any variables? */
14061 if ((ins->next->next->op != OP_INDEX) ||
14062 (ins->next->next->u.cval != 0) ||
14063 (result = MISC(ins->next->next, 0)) ||
14064 (result->id & TRIPLE_FLAG_LOCAL)) {
14065 continue;
14066 }
14067 index0 = ins->next->next;
14068 /* The pattern is:
14069 * 0 index result < 0 >
14070 * 1 index 0 < ? >
14071 * 2 index 1 < ? >
14072 * 3 read 2
14073 * 4 write 3 var
14074 */
14075 for(index0 = ins->next->next;
14076 (index0->op == OP_INDEX) &&
14077 (MISC(index0, 0) == result) &&
14078 (index0->u.cval == 0) ;
14079 index0 = write->next)
14080 {
14081 index1 = index0->next;
14082 index2 = index1->next;
14083 read = index2->next;
14084 write = read->next;
14085 if ((index0->op != OP_INDEX) ||
14086 (index1->op != OP_INDEX) ||
14087 (index2->op != OP_INDEX) ||
14088 (read->op != OP_READ) ||
14089 (write->op != OP_WRITE) ||
14090 (MISC(index1, 0) != index0) ||
14091 (MISC(index2, 0) != index1) ||
14092 (RHS(read, 0) != index2) ||
14093 (RHS(write, 0) != read)) {
14094 internal_error(state, index0, "bad var read");
14095 }
14096 if (MISC(write, 0) == val) {
14097 return index2->u.cval;
14098 }
14099 }
14100 } while(next != first);
14101 return -1;
14102}
14103
14104static inline int enclose_triple(struct triple *ins)
14105{
14106 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14107}
14108
14109static void compute_closure_variables(struct compile_state *state,
14110 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14111{
14112 struct triple_reg_set *set, *vars, **last_var;
14113 struct basic_blocks bb;
14114 struct reg_block *rb;
14115 struct block *block;
14116 struct triple *old_result, *first, *ins;
14117 size_t count, idx;
14118 unsigned long used_indicies;
14119 int i, max_index;
14120#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14121#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
14122 struct {
14123 unsigned id;
14124 int index;
14125 } *info;
14126
14127
14128 /* Find the basic blocks of this function */
14129 bb.func = me;
14130 bb.first = RHS(me, 0);
14131 old_result = 0;
14132 if (!triple_is_ret(state, bb.first->prev)) {
14133 bb.func = 0;
14134 } else {
14135 old_result = fresult(state, me);
14136 }
14137 analyze_basic_blocks(state, &bb);
14138
14139 /* Find which variables are currently alive in a given block */
14140 rb = compute_variable_lifetimes(state, &bb);
14141
14142 /* Find the variables that are currently alive */
14143 block = block_of_triple(state, fcall);
14144 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14145 internal_error(state, fcall, "No reg block? block: %p", block);
14146 }
14147
14148#if DEBUG_EXPLICIT_CLOSURES
14149 print_live_variables(state, &bb, rb, state->dbgout);
14150 fflush(state->dbgout);
14151#endif
14152
14153 /* Count the number of triples in the function */
14154 first = RHS(me, 0);
14155 ins = first;
14156 count = 0;
14157 do {
14158 count++;
14159 ins = ins->next;
14160 } while(ins != first);
14161
14162 /* Allocate some memory to temorary hold the id info */
14163 info = xcmalloc(sizeof(*info) * (count +1), "info");
14164
14165 /* Mark the local function */
14166 first = RHS(me, 0);
14167 ins = first;
14168 idx = 1;
14169 do {
14170 info[idx].id = ins->id;
14171 ins->id = TRIPLE_FLAG_LOCAL | idx;
14172 idx++;
14173 ins = ins->next;
14174 } while(ins != first);
14175
14176 /*
14177 * Build the list of variables to enclose.
14178 *
14179 * A target it to put the same variable in the
14180 * same slot for ever call of a given function.
14181 * After coloring this removes all of the variable
14182 * manipulation code.
14183 *
14184 * The list of variables to enclose is built ordered
14185 * program order because except in corner cases this
14186 * gives me the stability of assignment I need.
14187 *
14188 * To gurantee that stability I lookup the variables
14189 * to see where they have been used before and
14190 * I build my final list with the assigned indicies.
14191 */
14192 vars = 0;
14193 if (enclose_triple(old_result)) {
14194 ordered_triple_set(&vars, old_result);
14195 }
14196 for(set = rb[block->vertex].out; set; set = set->next) {
14197 if (!enclose_triple(set->member)) {
14198 continue;
14199 }
14200 if ((set->member == fcall) || (set->member == old_result)) {
14201 continue;
14202 }
14203 if (!local_triple(state, me, set->member)) {
14204 internal_error(state, set->member, "not local?");
14205 }
14206 ordered_triple_set(&vars, set->member);
14207 }
14208
14209 /* Lookup the current indicies of the live varialbe */
14210 used_indicies = 0;
14211 max_index = -1;
14212 for(set = vars; set ; set = set->next) {
14213 struct triple *ins;
14214 int index;
14215 ins = set->member;
14216 index = lookup_closure_index(state, me, ins);
14217 info[ID_BITS(ins->id)].index = index;
14218 if (index < 0) {
14219 continue;
14220 }
14221 if (index >= MAX_INDICIES) {
14222 internal_error(state, ins, "index unexpectedly large");
14223 }
14224 if (used_indicies & (1 << index)) {
14225 internal_error(state, ins, "index previously used?");
14226 }
14227 /* Remember which indicies have been used */
14228 used_indicies |= (1 << index);
14229 if (index > max_index) {
14230 max_index = index;
14231 }
14232 }
14233
14234 /* Walk through the live variables and make certain
14235 * everything is assigned an index.
14236 */
14237 for(set = vars; set; set = set->next) {
14238 struct triple *ins;
14239 int index;
14240 ins = set->member;
14241 index = info[ID_BITS(ins->id)].index;
14242 if (index >= 0) {
14243 continue;
14244 }
14245 /* Find the lowest unused index value */
14246 for(index = 0; index < MAX_INDICIES; index++) {
14247 if (!(used_indicies & (1 << index))) {
14248 break;
14249 }
14250 }
14251 if (index == MAX_INDICIES) {
14252 internal_error(state, ins, "no free indicies?");
14253 }
14254 info[ID_BITS(ins->id)].index = index;
14255 /* Remember which indicies have been used */
14256 used_indicies |= (1 << index);
14257 if (index > max_index) {
14258 max_index = index;
14259 }
14260 }
14261
14262 /* Build the return list of variables with positions matching
14263 * their indicies.
14264 */
14265 *enclose = 0;
14266 last_var = enclose;
14267 for(i = 0; i <= max_index; i++) {
14268 struct triple *var;
14269 var = 0;
14270 if (used_indicies & (1 << i)) {
14271 for(set = vars; set; set = set->next) {
14272 int index;
14273 index = info[ID_BITS(set->member->id)].index;
14274 if (index == i) {
14275 var = set->member;
14276 break;
14277 }
14278 }
14279 if (!var) {
14280 internal_error(state, me, "missing variable");
14281 }
14282 }
14283 insert_triple_set(last_var, var);
14284 last_var = &(*last_var)->next;
14285 }
14286
14287#if DEBUG_EXPLICIT_CLOSURES
14288 /* Print out the variables to be enclosed */
14289 loc(state->dbgout, state, fcall);
14290 fprintf(state->dbgout, "Alive: \n");
14291 for(set = *enclose; set; set = set->next) {
14292 display_triple(state->dbgout, set->member);
14293 }
14294 fflush(state->dbgout);
14295#endif
14296
14297 /* Clear the marks */
14298 ins = first;
14299 do {
14300 ins->id = info[ID_BITS(ins->id)].id;
14301 ins = ins->next;
14302 } while(ins != first);
14303
14304 /* Release the ordered list of live variables */
14305 free_closure_variables(state, &vars);
14306
14307 /* Release the storage of the old ids */
14308 xfree(info);
14309
14310 /* Release the variable lifetime information */
14311 free_variable_lifetimes(state, &bb, rb);
14312
14313 /* Release the basic blocks of this function */
14314 free_basic_blocks(state, &bb);
14315}
14316
14317static void expand_function_call(
14318 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014319{
14320 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014321 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014322 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014323 struct triple *first;
14324 struct type *ptype, *rtype;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014325 struct triple *jmp;
14326 struct triple *ret_addr, *ret_loc, *ret_set;
Eric Biederman90089602004-05-28 14:11:54 +000014327 struct triple_reg_set *enclose, *set;
14328 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014329
Eric Biederman90089602004-05-28 14:11:54 +000014330#if DEBUG_EXPLICIT_CLOSURES
14331 FILE *fp = state->dbgout;
14332 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14333 display_func(state, fp, MISC(fcall, 0));
14334 display_func(state, fp, me);
14335 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14336#endif
14337
Eric Biederman5ade04a2003-10-22 04:03:46 +000014338 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014339 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014340 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014341 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014342 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014343 first = fcall->next;
14344
14345 /* Find what I need to enclose */
14346 compute_closure_variables(state, me, fcall, &enclose);
14347
14348 /* Compute the closure type */
14349 closure_type = new_type(TYPE_TUPLE, 0, 0);
14350 closure_type->elements = 0;
14351 closure_next = &closure_type->left;
14352 for(set = enclose; set ; set = set->next) {
14353 struct type *type;
14354 type = &void_type;
14355 if (set->member) {
14356 type = set->member->type;
14357 }
14358 if (!*closure_next) {
14359 *closure_next = type;
14360 } else {
14361 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
14362 type);
14363 closure_next = &(*closure_next)->right;
14364 }
14365 closure_type->elements += 1;
14366 }
14367 if (closure_type->elements == 0) {
14368 closure_type->type = TYPE_VOID;
14369 }
14370
14371
14372#if DEBUG_EXPLICIT_CLOSURES
14373 fprintf(state->dbgout, "closure type: ");
14374 name_of(state->dbgout, closure_type);
14375 fprintf(state->dbgout, "\n");
14376#endif
14377
14378 /* Update the called functions closure variable */
14379 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014380
14381 /* Generate some needed triples */
14382 ret_loc = label(state);
14383 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14384
14385 /* Pass the parameters to the new function */
14386 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014387 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014388 for(i = 0; i < pvals; i++) {
14389 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014390 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014391 atype = ptype;
14392 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14393 atype = ptype->left;
14394 }
Eric Biederman90089602004-05-28 14:11:54 +000014395 param = farg(state, func, i);
14396 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14397 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014398 }
Eric Biederman90089602004-05-28 14:11:54 +000014399 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014400 flatten(state, first, write_expr(state, param, arg));
14401 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014402 }
Eric Biederman90089602004-05-28 14:11:54 +000014403 rtype = func->type->left;
14404
Eric Biederman5ade04a2003-10-22 04:03:46 +000014405 /* Thread the triples together */
14406 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014407
14408 /* Save the active variables in the result variable */
14409 for(i = 0, set = enclose; set ; set = set->next, i++) {
14410 if (!set->member) {
14411 continue;
14412 }
14413 flatten(state, ret_loc,
14414 write_expr(state,
14415 closure_expr(state, func, closure_idx, i),
14416 read_expr(state, set->member)));
14417 }
14418
14419 /* Initialize the return value */
14420 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14421 flatten(state, ret_loc,
14422 write_expr(state,
14423 deref_index(state, fresult(state, func), 1),
14424 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14425 }
14426
Eric Biederman5ade04a2003-10-22 04:03:46 +000014427 ret_addr = flatten(state, ret_loc, ret_addr);
14428 ret_set = flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14429 jmp = flatten(state, ret_loc,
14430 call(state, retvar, ret_addr, func_first, func_last));
14431
Eric Biederman7dea9552004-06-29 05:38:37 +000014432 /* Find the result */
14433 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14434 struct triple * result;
14435 result = flatten(state, first,
14436 read_expr(state,
14437 deref_index(state, fresult(state, func), 1)));
14438
14439 propogate_use(state, fcall, result);
14440 }
14441
14442 /* Release the original fcall instruction */
14443 release_triple(state, fcall);
14444
Eric Biederman90089602004-05-28 14:11:54 +000014445 /* Restore the active variables from the result variable */
14446 for(i = 0, set = enclose; set ; set = set->next, i++) {
14447 struct triple_set *use, *next;
14448 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014449 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014450 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014451 continue;
14452 }
Eric Biederman90089602004-05-28 14:11:54 +000014453 /* Generate an expression for the value */
14454 new = flatten(state, first,
14455 read_expr(state,
14456 closure_expr(state, func, closure_idx, i)));
14457
14458
14459 /* If the original is an lvalue restore the preserved value */
14460 if (is_lvalue(state, set->member)) {
14461 flatten(state, first,
14462 write_expr(state, set->member, new));
14463 continue;
14464 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014465 /*
14466 * If the original is a value update the dominated uses.
14467 */
14468
14469 /* Analyze the basic blocks so I can see who dominates whom */
14470 bb.func = me;
14471 bb.first = RHS(me, 0);
14472 if (!triple_is_ret(state, bb.first->prev)) {
14473 bb.func = 0;
14474 }
14475 analyze_basic_blocks(state, &bb);
14476
Eric Biederman90089602004-05-28 14:11:54 +000014477
14478#if DEBUG_EXPLICIT_CLOSURES
14479 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14480 set->member, new);
14481#endif
14482 /* If fcall dominates the use update the expression */
14483 for(use = set->member->use; use; use = next) {
14484 /* Replace use modifies the use chain and
14485 * removes use, so I must take a copy of the
14486 * next entry early.
14487 */
14488 next = use->next;
14489 if (!tdominates(state, fcall, use->member)) {
14490 continue;
14491 }
14492 replace_use(state, set->member, new, use->member);
14493 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014494
14495 /* Release the basic blocks, the instructions will be
14496 * different next time, and flatten/insert_triple does
14497 * not update the block values so I can't cache the analysis.
14498 */
14499 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014500 }
14501
Eric Biederman90089602004-05-28 14:11:54 +000014502 /* Release the closure variable list */
14503 free_closure_variables(state, &enclose);
14504
14505 if (state->compiler->debug & DEBUG_INLINE) {
14506 FILE *fp = state->dbgout;
14507 fprintf(fp, "\n");
14508 loc(fp, state, 0);
14509 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14510 display_func(state, fp, func);
14511 display_func(state, fp, me);
14512 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14513 }
14514
14515 return;
14516}
14517
14518static int do_inline(struct compile_state *state, struct triple *func)
14519{
14520 int do_inline;
14521 int policy;
14522
14523 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14524 switch(policy) {
14525 case COMPILER_INLINE_ALWAYS:
14526 do_inline = 1;
14527 if (func->type->type & ATTRIB_NOINLINE) {
14528 error(state, func, "noinline with always_inline compiler option");
14529 }
14530 break;
14531 case COMPILER_INLINE_NEVER:
14532 do_inline = 0;
14533 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14534 error(state, func, "always_inline with noinline compiler option");
14535 }
14536 break;
14537 case COMPILER_INLINE_DEFAULTON:
14538 switch(func->type->type & STOR_MASK) {
14539 case STOR_STATIC | STOR_INLINE:
14540 case STOR_LOCAL | STOR_INLINE:
14541 case STOR_EXTERN | STOR_INLINE:
14542 do_inline = 1;
14543 break;
14544 default:
14545 do_inline = 1;
14546 break;
14547 }
14548 break;
14549 case COMPILER_INLINE_DEFAULTOFF:
14550 switch(func->type->type & STOR_MASK) {
14551 case STOR_STATIC | STOR_INLINE:
14552 case STOR_LOCAL | STOR_INLINE:
14553 case STOR_EXTERN | STOR_INLINE:
14554 do_inline = 1;
14555 break;
14556 default:
14557 do_inline = 0;
14558 break;
14559 }
14560 break;
14561 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014562 switch(func->type->type & STOR_MASK) {
14563 case STOR_STATIC | STOR_INLINE:
14564 case STOR_LOCAL | STOR_INLINE:
14565 case STOR_EXTERN | STOR_INLINE:
14566 do_inline = 1;
14567 break;
14568 default:
14569 do_inline = (func->u.cval == 1);
14570 break;
14571 }
Eric Biederman90089602004-05-28 14:11:54 +000014572 break;
14573 default:
14574 do_inline = 0;
14575 internal_error(state, 0, "Unimplemented inline policy");
14576 break;
14577 }
14578 /* Force inlining */
14579 if (func->type->type & ATTRIB_NOINLINE) {
14580 do_inline = 0;
14581 }
14582 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14583 do_inline = 1;
14584 }
14585 return do_inline;
14586}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014587
Eric Biederman90089602004-05-28 14:11:54 +000014588static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14589{
14590 struct triple *first, *ptr, *next;
14591 /* If the function is not used don't bother */
14592 if (me->u.cval <= 0) {
14593 return;
14594 }
14595 if (state->compiler->debug & DEBUG_CALLS2) {
14596 FILE *fp = state->dbgout;
14597 fprintf(fp, "in: %s\n",
14598 me->type->type_ident->name);
14599 }
14600
14601 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014602 ptr = next = first;
14603 do {
Eric Biederman90089602004-05-28 14:11:54 +000014604 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014605 ptr = next;
14606 prev = ptr->prev;
14607 next = ptr->next;
14608 if (ptr->op != OP_FCALL) {
14609 continue;
14610 }
14611 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014612 /* See if the function should be inlined */
14613 if (!do_inline(state, func)) {
14614 /* Put a label after the fcall */
14615 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14616 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014617 }
Eric Biederman90089602004-05-28 14:11:54 +000014618 if (state->compiler->debug & DEBUG_CALLS) {
14619 FILE *fp = state->dbgout;
14620 if (state->compiler->debug & DEBUG_CALLS2) {
14621 loc(fp, state, ptr);
14622 }
14623 fprintf(fp, "inlining %s\n",
14624 func->type->type_ident->name);
14625 fflush(fp);
14626 }
14627
14628 /* Update the function use counts */
14629 func->u.cval -= 1;
14630
14631 /* Replace the fcall with the called function */
14632 expand_inline_call(state, me, ptr);
14633
14634 next = prev->next;
14635 } while (next != first);
14636
14637 ptr = next = first;
14638 do {
14639 struct triple *prev, *func;
14640 ptr = next;
14641 prev = ptr->prev;
14642 next = ptr->next;
14643 if (ptr->op != OP_FCALL) {
14644 continue;
14645 }
14646 func = MISC(ptr, 0);
14647 if (state->compiler->debug & DEBUG_CALLS) {
14648 FILE *fp = state->dbgout;
14649 if (state->compiler->debug & DEBUG_CALLS2) {
14650 loc(fp, state, ptr);
14651 }
14652 fprintf(fp, "calling %s\n",
14653 func->type->type_ident->name);
14654 fflush(fp);
14655 }
14656 /* Replace the fcall with the instruction sequence
14657 * needed to make the call.
14658 */
14659 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014660 next = prev->next;
14661 } while(next != first);
14662}
Eric Biederman90089602004-05-28 14:11:54 +000014663
14664static void inline_functions(struct compile_state *state, struct triple *func)
14665{
14666 inline_function(state, func, 0);
14667 reverse_walk_functions(state, inline_function, 0);
14668}
14669
Eric Biederman5ade04a2003-10-22 04:03:46 +000014670static void insert_function(struct compile_state *state,
14671 struct triple *func, void *arg)
14672{
14673 struct triple *first, *end, *ffirst, *fend;
14674
14675 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014676 FILE *fp = state->errout;
14677 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014678 func->type->type_ident->name, func->u.cval);
14679 }
14680 if (func->u.cval == 0) {
14681 return;
14682 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014683
14684 /* Find the end points of the lists */
14685 first = arg;
14686 end = first->prev;
14687 ffirst = RHS(func, 0);
14688 fend = ffirst->prev;
14689
14690 /* splice the lists together */
14691 end->next = ffirst;
14692 ffirst->prev = end;
14693 fend->next = first;
14694 first->prev = fend;
14695}
14696
Eric Biederman90089602004-05-28 14:11:54 +000014697struct triple *input_asm(struct compile_state *state)
14698{
14699 struct asm_info *info;
14700 struct triple *def;
14701 int i, out;
14702
14703 info = xcmalloc(sizeof(*info), "asm_info");
14704 info->str = "";
14705
14706 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14707 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14708
14709 def = new_triple(state, OP_ASM, &void_type, out, 0);
14710 def->u.ainfo = info;
14711 def->id |= TRIPLE_FLAG_VOLATILE;
14712
14713 for(i = 0; i < out; i++) {
14714 struct triple *piece;
14715 piece = triple(state, OP_PIECE, &int_type, def, 0);
14716 piece->u.cval = i;
14717 LHS(def, i) = piece;
14718 }
14719
14720 return def;
14721}
14722
14723struct triple *output_asm(struct compile_state *state)
14724{
14725 struct asm_info *info;
14726 struct triple *def;
14727 int in;
14728
14729 info = xcmalloc(sizeof(*info), "asm_info");
14730 info->str = "";
14731
14732 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14733 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14734
14735 def = new_triple(state, OP_ASM, &void_type, 0, in);
14736 def->u.ainfo = info;
14737 def->id |= TRIPLE_FLAG_VOLATILE;
14738
14739 return def;
14740}
14741
Eric Biederman5ade04a2003-10-22 04:03:46 +000014742static void join_functions(struct compile_state *state)
14743{
Eric Biederman90089602004-05-28 14:11:54 +000014744 struct triple *jmp, *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014745 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014746 struct type *pnext, *param;
14747 struct type *result_type, *args_type;
14748 int idx;
14749
14750 /* Be clear the functions have not been joined yet */
14751 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014752
14753 /* Dummy file state to get debug handing right */
14754 memset(&file, 0, sizeof(file));
14755 file.basename = "";
14756 file.line = 0;
14757 file.report_line = 0;
14758 file.report_name = file.basename;
14759 file.prev = state->file;
14760 state->file = &file;
14761 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014762
Eric Biederman41203d92004-11-08 09:31:09 +000014763 if (!state->main_function) {
14764 error(state, 0, "No functions to compile\n");
14765 }
14766
Eric Biederman90089602004-05-28 14:11:54 +000014767 /* The type of arguments */
14768 args_type = state->main_function->type->right;
14769 /* The return type without any specifiers */
14770 result_type = clone_type(0, state->main_function->type->left);
14771
14772
14773 /* Verify the external arguments */
14774 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
14775 error(state, state->main_function,
14776 "Too many external input arguments");
14777 }
14778 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
14779 error(state, state->main_function,
14780 "Too many external output arguments");
14781 }
14782
Eric Biederman5ade04a2003-10-22 04:03:46 +000014783 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014784 end = label(state);
14785 start = label(state);
14786 start = flatten(state, state->first, start);
14787 end = flatten(state, state->first, end);
14788 in = input_asm(state);
14789 out = output_asm(state);
14790 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014791 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014792 in = flatten(state, state->first, in);
14793 call = flatten(state, state->first, call);
14794 out = flatten(state, state->first, out);
14795
14796
14797 /* Read the external input arguments */
14798 pnext = args_type;
14799 idx = 0;
14800 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14801 struct triple *expr;
14802 param = pnext;
14803 pnext = 0;
14804 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14805 pnext = param->right;
14806 param = param->left;
14807 }
14808 if (registers_of(state, param) != 1) {
14809 error(state, state->main_function,
14810 "Arg: %d %s requires multiple registers",
14811 idx + 1, param->field_ident->name);
14812 }
14813 expr = read_expr(state, LHS(in, idx));
14814 RHS(call, idx) = expr;
14815 expr = flatten(state, call, expr);
14816 use_triple(expr, call);
14817
14818 idx++;
14819 }
14820
14821
14822 /* Write the external output arguments */
14823 pnext = result_type;
14824 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14825 pnext = result_type->left;
14826 }
14827 for(idx = 0; idx < out->rhs; idx++) {
14828 struct triple *expr;
14829 param = pnext;
14830 pnext = 0;
14831 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14832 pnext = param->right;
14833 param = param->left;
14834 }
14835 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14836 param = 0;
14837 }
14838 if (param) {
14839 if (registers_of(state, param) != 1) {
14840 error(state, state->main_function,
14841 "Result: %d %s requires multiple registers",
14842 idx, param->field_ident->name);
14843 }
14844 expr = read_expr(state, call);
14845 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14846 expr = deref_field(state, expr, param->field_ident);
14847 }
14848 } else {
14849 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14850 }
14851 flatten(state, out, expr);
14852 RHS(out, idx) = expr;
14853 use_triple(expr, out);
14854 }
14855
14856 /* Allocate a dummy containing function */
14857 func = triple(state, OP_LIST,
14858 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14859 func->type->type_ident = lookup(state, "", 0);
14860 RHS(func, 0) = state->first;
14861 func->u.cval = 1;
14862
Eric Biederman5ade04a2003-10-22 04:03:46 +000014863 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014864 mark_live_functions(state);
14865 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014866 walk_functions(state, insert_function, end);
14867
14868 if (start->next != end) {
14869 jmp = flatten(state, start, branch(state, end, 0));
14870 }
14871
Eric Biederman90089602004-05-28 14:11:54 +000014872 /* OK now the functions have been joined. */
14873 state->functions_joined = 1;
14874
Eric Biederman5ade04a2003-10-22 04:03:46 +000014875 /* Done now cleanup */
14876 state->file = file.prev;
14877 state->function = 0;
14878}
14879
Eric Biedermanb138ac82003-04-22 18:44:01 +000014880/*
14881 * Data structurs for optimation.
14882 */
14883
Eric Biederman5ade04a2003-10-22 04:03:46 +000014884
Eric Biederman83b991a2003-10-11 06:20:25 +000014885static int do_use_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014886 struct block *used, struct block_set **head, struct block *user,
14887 int front)
14888{
14889 struct block_set **ptr, *new;
14890 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014891 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014892 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014893 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014894 ptr = head;
14895 while(*ptr) {
14896 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014897 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014898 }
14899 ptr = &(*ptr)->next;
14900 }
14901 new = xcmalloc(sizeof(*new), "block_set");
14902 new->member = user;
14903 if (front) {
14904 new->next = *head;
14905 *head = new;
14906 }
14907 else {
14908 new->next = 0;
14909 *ptr = new;
14910 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014911 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014912}
Eric Biederman83b991a2003-10-11 06:20:25 +000014913static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014914 struct block *used, struct block_set **head, struct block *unuser)
14915{
14916 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014917 int count;
14918 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014919 ptr = head;
14920 while(*ptr) {
14921 use = *ptr;
14922 if (use->member == unuser) {
14923 *ptr = use->next;
14924 memset(use, -1, sizeof(*use));
14925 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014926 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014927 }
14928 else {
14929 ptr = &use->next;
14930 }
14931 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014932 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014933}
14934
14935static void use_block(struct block *used, struct block *user)
14936{
Eric Biederman83b991a2003-10-11 06:20:25 +000014937 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014938 /* Append new to the head of the list, print_block
14939 * depends on this.
14940 */
Eric Biederman83b991a2003-10-11 06:20:25 +000014941 count = do_use_block(used, &used->use, user, 1);
14942 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014943}
14944static void unuse_block(struct block *used, struct block *unuser)
14945{
Eric Biederman83b991a2003-10-11 06:20:25 +000014946 int count;
14947 count = do_unuse_block(used, &used->use, unuser);
14948 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014949}
14950
Eric Biederman5ade04a2003-10-22 04:03:46 +000014951static void add_block_edge(struct block *block, struct block *edge, int front)
14952{
14953 int count;
14954 count = do_use_block(block, &block->edges, edge, front);
14955 block->edge_count += count;
14956}
14957
14958static void remove_block_edge(struct block *block, struct block *edge)
14959{
14960 int count;
14961 count = do_unuse_block(block, &block->edges, edge);
14962 block->edge_count -= count;
14963}
14964
Eric Biedermanb138ac82003-04-22 18:44:01 +000014965static void idom_block(struct block *idom, struct block *user)
14966{
14967 do_use_block(idom, &idom->idominates, user, 0);
14968}
14969
14970static void unidom_block(struct block *idom, struct block *unuser)
14971{
14972 do_unuse_block(idom, &idom->idominates, unuser);
14973}
14974
14975static void domf_block(struct block *block, struct block *domf)
14976{
14977 do_use_block(block, &block->domfrontier, domf, 0);
14978}
14979
14980static void undomf_block(struct block *block, struct block *undomf)
14981{
14982 do_unuse_block(block, &block->domfrontier, undomf);
14983}
14984
14985static void ipdom_block(struct block *ipdom, struct block *user)
14986{
14987 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14988}
14989
14990static void unipdom_block(struct block *ipdom, struct block *unuser)
14991{
14992 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14993}
14994
14995static void ipdomf_block(struct block *block, struct block *ipdomf)
14996{
14997 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14998}
14999
15000static void unipdomf_block(struct block *block, struct block *unipdomf)
15001{
15002 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
15003}
15004
Eric Biederman83b991a2003-10-11 06:20:25 +000015005static int walk_triples(
15006 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015007 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15008 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015009{
Eric Biederman83b991a2003-10-11 06:20:25 +000015010 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015011 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015012 ptr = state->first;
15013 do {
Eric Biederman90089602004-05-28 14:11:54 +000015014 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015015 if (ptr->next->prev != ptr) {
15016 internal_error(state, ptr->next, "bad prev");
15017 }
15018 ptr = ptr->next;
15019 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015020 return result;
15021}
15022
Eric Biedermanb138ac82003-04-22 18:44:01 +000015023#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015024static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015025{
Eric Biederman90089602004-05-28 14:11:54 +000015026 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015027 int op;
15028 op = ins->op;
15029 if (op == OP_LIST) {
15030#if !PRINT_LIST
15031 return 0;
15032#endif
15033 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015034 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015035 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015036 }
Eric Biederman90089602004-05-28 14:11:54 +000015037 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015038
Eric Biederman90089602004-05-28 14:11:54 +000015039 if (triple_is_branch(state, ins) && ins->use &&
15040 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015041 internal_error(state, ins, "branch used?");
15042 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015043 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015044 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015045 }
15046 return 0;
15047}
15048
Eric Biedermanb138ac82003-04-22 18:44:01 +000015049static void print_triples(struct compile_state *state)
15050{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015051 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015052 FILE *fp = state->dbgout;
15053 fprintf(fp, "--------------- triples ---------------\n");
15054 walk_triples(state, do_print_triple, fp);
15055 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015056 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015057}
15058
15059struct cf_block {
15060 struct block *block;
15061};
15062static void find_cf_blocks(struct cf_block *cf, struct block *block)
15063{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015064 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015065 if (!block || (cf[block->vertex].block == block)) {
15066 return;
15067 }
15068 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015069 for(edge = block->edges; edge; edge = edge->next) {
15070 find_cf_blocks(cf, edge->member);
15071 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015072}
15073
Eric Biederman90089602004-05-28 14:11:54 +000015074static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015075 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015076{
15077 struct cf_block *cf;
15078 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015079 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015080 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15081 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015082
Eric Biederman90089602004-05-28 14:11:54 +000015083 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015084 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015085 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015086 block = cf[i].block;
15087 if (!block)
15088 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015089 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015090 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015091 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015092 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015093 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015094 }
15095
15096 xfree(cf);
15097}
15098
Eric Biedermanb138ac82003-04-22 18:44:01 +000015099static void free_basic_block(struct compile_state *state, struct block *block)
15100{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015101 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015102 struct block *child;
15103 if (!block) {
15104 return;
15105 }
15106 if (block->vertex == -1) {
15107 return;
15108 }
15109 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015110 for(edge = block->edges; edge; edge = edge->next) {
15111 if (edge->member) {
15112 unuse_block(edge->member, block);
15113 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015114 }
15115 if (block->idom) {
15116 unidom_block(block->idom, block);
15117 }
15118 block->idom = 0;
15119 if (block->ipdom) {
15120 unipdom_block(block->ipdom, block);
15121 }
15122 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015123 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015124 child = entry->member;
15125 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015126 if (child && (child->vertex != -1)) {
15127 for(edge = child->edges; edge; edge = edge->next) {
15128 edge->member = 0;
15129 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015130 }
15131 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015132 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015133 child = entry->member;
15134 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015135 if (child && (child->vertex != -1)) {
15136 child->idom = 0;
15137 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015138 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015139 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015140 child = entry->member;
15141 undomf_block(block, child);
15142 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015143 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015144 child = entry->member;
15145 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015146 if (child && (child->vertex != -1)) {
15147 child->ipdom = 0;
15148 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015149 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015150 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015151 child = entry->member;
15152 unipdomf_block(block, child);
15153 }
15154 if (block->users != 0) {
15155 internal_error(state, 0, "block still has users");
15156 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015157 while((edge = block->edges)) {
15158 child = edge->member;
15159 remove_block_edge(block, child);
15160
15161 if (child && (child->vertex != -1)) {
15162 free_basic_block(state, child);
15163 }
15164 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015165 memset(block, -1, sizeof(*block));
Patrick Georgi26774f22009-11-21 19:54:02 +000015166#ifndef WIN32
Eric Biedermanb138ac82003-04-22 18:44:01 +000015167 xfree(block);
Patrick Georgi26774f22009-11-21 19:54:02 +000015168#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015169}
15170
Eric Biederman90089602004-05-28 14:11:54 +000015171static void free_basic_blocks(struct compile_state *state,
15172 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015173{
15174 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015175 free_basic_block(state, bb->first_block);
15176 bb->last_vertex = 0;
15177 bb->first_block = bb->last_block = 0;
15178 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015179 ins = first;
15180 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015181 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015182 ins->u.block = 0;
15183 }
15184 ins = ins->next;
15185 } while(ins != first);
15186
15187}
15188
Eric Biederman90089602004-05-28 14:11:54 +000015189static struct block *basic_block(struct compile_state *state,
15190 struct basic_blocks *bb, struct triple *first)
15191{
15192 struct block *block;
15193 struct triple *ptr;
15194 if (!triple_is_label(state, first)) {
15195 internal_error(state, first, "block does not start with a label");
15196 }
15197 /* See if this basic block has already been setup */
15198 if (first->u.block != 0) {
15199 return first->u.block;
15200 }
15201 /* Allocate another basic block structure */
15202 bb->last_vertex += 1;
15203 block = xcmalloc(sizeof(*block), "block");
15204 block->first = block->last = first;
15205 block->vertex = bb->last_vertex;
15206 ptr = first;
15207 do {
15208 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
15209 break;
15210 }
15211 block->last = ptr;
15212 /* If ptr->u is not used remember where the baic block is */
15213 if (triple_stores_block(state, ptr)) {
15214 ptr->u.block = block;
15215 }
15216 if (triple_is_branch(state, ptr)) {
15217 break;
15218 }
15219 ptr = ptr->next;
15220 } while (ptr != bb->first);
15221 if ((ptr == bb->first) ||
15222 ((ptr->next == bb->first) && (
15223 triple_is_end(state, ptr) ||
15224 triple_is_ret(state, ptr))))
15225 {
15226 /* The block has no outflowing edges */
15227 }
15228 else if (triple_is_label(state, ptr)) {
15229 struct block *next;
15230 next = basic_block(state, bb, ptr);
15231 add_block_edge(block, next, 0);
15232 use_block(next, block);
15233 }
15234 else if (triple_is_branch(state, ptr)) {
15235 struct triple **expr, *first;
15236 struct block *child;
15237 /* Find the branch targets.
15238 * I special case the first branch as that magically
15239 * avoids some difficult cases for the register allocator.
15240 */
15241 expr = triple_edge_targ(state, ptr, 0);
15242 if (!expr) {
15243 internal_error(state, ptr, "branch without targets");
15244 }
15245 first = *expr;
15246 expr = triple_edge_targ(state, ptr, expr);
15247 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15248 if (!*expr) continue;
15249 child = basic_block(state, bb, *expr);
15250 use_block(child, block);
15251 add_block_edge(block, child, 0);
15252 }
15253 if (first) {
15254 child = basic_block(state, bb, first);
15255 use_block(child, block);
15256 add_block_edge(block, child, 1);
15257
15258 /* Be certain the return block of a call is
15259 * in a basic block. When it is not find
15260 * start of the block, insert a label if
15261 * necessary and build the basic block.
15262 * Then add a fake edge from the start block
15263 * to the return block of the function.
15264 */
15265 if (state->functions_joined && triple_is_call(state, ptr)
15266 && !block_of_triple(state, MISC(ptr, 0))) {
15267 struct block *tail;
15268 struct triple *start;
15269 start = triple_to_block_start(state, MISC(ptr, 0));
15270 if (!triple_is_label(state, start)) {
15271 start = pre_triple(state,
15272 start, OP_LABEL, &void_type, 0, 0);
15273 }
15274 tail = basic_block(state, bb, start);
15275 add_block_edge(child, tail, 0);
15276 use_block(tail, child);
15277 }
15278 }
15279 }
15280 else {
15281 internal_error(state, 0, "Bad basic block split");
15282 }
15283#if 0
15284{
15285 struct block_set *edge;
15286 FILE *fp = state->errout;
15287 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
15288 block, block->vertex,
15289 block->first, block->last);
15290 for(edge = block->edges; edge; edge = edge->next) {
15291 fprintf(fp, " %10p [%2d]",
15292 edge->member ? edge->member->first : 0,
15293 edge->member ? edge->member->vertex : -1);
15294 }
15295 fprintf(fp, "\n");
15296}
15297#endif
15298 return block;
15299}
15300
15301
15302static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15303 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15304 void *arg)
15305{
15306 struct triple *ptr, *first;
15307 struct block *last_block;
15308 last_block = 0;
15309 first = bb->first;
15310 ptr = first;
15311 do {
15312 if (triple_stores_block(state, ptr)) {
15313 struct block *block;
15314 block = ptr->u.block;
15315 if (block && (block != last_block)) {
15316 cb(state, block, arg);
15317 }
15318 last_block = block;
15319 }
15320 ptr = ptr->next;
15321 } while(ptr != first);
15322}
15323
15324static void print_block(
15325 struct compile_state *state, struct block *block, void *arg)
15326{
15327 struct block_set *user, *edge;
15328 struct triple *ptr;
15329 FILE *fp = arg;
15330
15331 fprintf(fp, "\nblock: %p (%d) ",
15332 block,
15333 block->vertex);
15334
15335 for(edge = block->edges; edge; edge = edge->next) {
15336 fprintf(fp, " %p<-%p",
15337 edge->member,
15338 (edge->member && edge->member->use)?
15339 edge->member->use->member : 0);
15340 }
15341 fprintf(fp, "\n");
15342 if (block->first->op == OP_LABEL) {
15343 fprintf(fp, "%p:\n", block->first);
15344 }
15345 for(ptr = block->first; ; ) {
15346 display_triple(fp, ptr);
15347 if (ptr == block->last)
15348 break;
15349 ptr = ptr->next;
15350 if (ptr == block->first) {
15351 internal_error(state, 0, "missing block last?");
15352 }
15353 }
15354 fprintf(fp, "users %d: ", block->users);
15355 for(user = block->use; user; user = user->next) {
15356 fprintf(fp, "%p (%d) ",
15357 user->member,
15358 user->member->vertex);
15359 }
15360 fprintf(fp,"\n\n");
15361}
15362
15363
15364static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15365{
15366 fprintf(fp, "--------------- blocks ---------------\n");
15367 walk_blocks(state, &state->bb, print_block, fp);
15368}
15369static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15370{
15371 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15372 fprintf(fp, "After %s\n", func);
15373 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015374 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15375 print_dominators(state, fp, &state->bb);
15376 print_dominance_frontiers(state, fp, &state->bb);
15377 }
15378 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015379 }
15380}
15381
15382static void prune_nonblock_triples(struct compile_state *state,
15383 struct basic_blocks *bb)
15384{
15385 struct block *block;
15386 struct triple *first, *ins, *next;
15387 /* Delete the triples not in a basic block */
15388 block = 0;
15389 first = bb->first;
15390 ins = first;
15391 do {
15392 next = ins->next;
15393 if (ins->op == OP_LABEL) {
15394 block = ins->u.block;
15395 }
15396 if (!block) {
15397 struct triple_set *use;
15398 for(use = ins->use; use; use = use->next) {
15399 struct block *block;
15400 block = block_of_triple(state, use->member);
15401 if (block != 0) {
15402 internal_error(state, ins, "pruning used ins?");
15403 }
15404 }
15405 release_triple(state, ins);
15406 }
15407 if (block && block->last == ins) {
15408 block = 0;
15409 }
15410 ins = next;
15411 } while(ins != first);
15412}
15413
15414static void setup_basic_blocks(struct compile_state *state,
15415 struct basic_blocks *bb)
15416{
15417 if (!triple_stores_block(state, bb->first)) {
15418 internal_error(state, 0, "ins will not store block?");
15419 }
15420 /* Initialize the state */
15421 bb->first_block = bb->last_block = 0;
15422 bb->last_vertex = 0;
15423 free_basic_blocks(state, bb);
15424
15425 /* Find the basic blocks */
15426 bb->first_block = basic_block(state, bb, bb->first);
15427
15428 /* Be certain the last instruction of a function, or the
15429 * entire program is in a basic block. When it is not find
15430 * the start of the block, insert a label if necessary and build
15431 * basic block. Then add a fake edge from the start block
15432 * to the final block.
15433 */
15434 if (!block_of_triple(state, bb->first->prev)) {
15435 struct triple *start;
15436 struct block *tail;
15437 start = triple_to_block_start(state, bb->first->prev);
15438 if (!triple_is_label(state, start)) {
15439 start = pre_triple(state,
15440 start, OP_LABEL, &void_type, 0, 0);
15441 }
15442 tail = basic_block(state, bb, start);
15443 add_block_edge(bb->first_block, tail, 0);
15444 use_block(tail, bb->first_block);
15445 }
15446
15447 /* Find the last basic block.
15448 */
15449 bb->last_block = block_of_triple(state, bb->first->prev);
15450
15451 /* Delete the triples not in a basic block */
15452 prune_nonblock_triples(state, bb);
15453
15454#if 0
15455 /* If we are debugging print what I have just done */
15456 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15457 print_blocks(state, state->dbgout);
15458 print_control_flow(state, bb);
15459 }
15460#endif
15461}
15462
15463
Eric Biedermanb138ac82003-04-22 18:44:01 +000015464struct sdom_block {
15465 struct block *block;
15466 struct sdom_block *sdominates;
15467 struct sdom_block *sdom_next;
15468 struct sdom_block *sdom;
15469 struct sdom_block *label;
15470 struct sdom_block *parent;
15471 struct sdom_block *ancestor;
15472 int vertex;
15473};
15474
15475
15476static void unsdom_block(struct sdom_block *block)
15477{
15478 struct sdom_block **ptr;
15479 if (!block->sdom_next) {
15480 return;
15481 }
15482 ptr = &block->sdom->sdominates;
15483 while(*ptr) {
15484 if ((*ptr) == block) {
15485 *ptr = block->sdom_next;
15486 return;
15487 }
15488 ptr = &(*ptr)->sdom_next;
15489 }
15490}
15491
15492static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15493{
15494 unsdom_block(block);
15495 block->sdom = sdom;
15496 block->sdom_next = sdom->sdominates;
15497 sdom->sdominates = block;
15498}
15499
15500
15501
15502static int initialize_sdblock(struct sdom_block *sd,
15503 struct block *parent, struct block *block, int vertex)
15504{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015505 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015506 if (!block || (sd[block->vertex].block == block)) {
15507 return vertex;
15508 }
15509 vertex += 1;
15510 /* Renumber the blocks in a convinient fashion */
15511 block->vertex = vertex;
15512 sd[vertex].block = block;
15513 sd[vertex].sdom = &sd[vertex];
15514 sd[vertex].label = &sd[vertex];
15515 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15516 sd[vertex].ancestor = 0;
15517 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015518 for(edge = block->edges; edge; edge = edge->next) {
15519 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15520 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015521 return vertex;
15522}
15523
Eric Biederman83b991a2003-10-11 06:20:25 +000015524static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015525 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015526 struct block *parent, struct block *block, int vertex)
15527{
15528 struct block_set *user;
15529 if (!block || (sd[block->vertex].block == block)) {
15530 return vertex;
15531 }
15532 vertex += 1;
15533 /* Renumber the blocks in a convinient fashion */
15534 block->vertex = vertex;
15535 sd[vertex].block = block;
15536 sd[vertex].sdom = &sd[vertex];
15537 sd[vertex].label = &sd[vertex];
15538 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15539 sd[vertex].ancestor = 0;
15540 sd[vertex].vertex = vertex;
15541 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015542 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015543 }
15544 return vertex;
15545}
15546
Eric Biederman90089602004-05-28 14:11:54 +000015547static int setup_spdblocks(struct compile_state *state,
15548 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015549{
15550 struct block *block;
15551 int vertex;
15552 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015553 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015554
Eric Biederman5ade04a2003-10-22 04:03:46 +000015555 /* Walk through the graph and find unconnected blocks. Add a
15556 * fake edge from the unconnected blocks to the end of the
15557 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015558 */
Eric Biederman90089602004-05-28 14:11:54 +000015559 block = bb->first_block->last->next->u.block;
15560 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015561 if (sd[block->vertex].block == block) {
15562 continue;
15563 }
Eric Biederman530b5192003-07-01 10:05:30 +000015564#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015565 {
15566 FILE *fp = state->errout;
15567 fprintf(fp, "Adding %d\n", vertex +1);
15568 }
Eric Biederman530b5192003-07-01 10:05:30 +000015569#endif
Eric Biederman90089602004-05-28 14:11:54 +000015570 add_block_edge(block, bb->last_block, 0);
15571 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015572
Eric Biederman90089602004-05-28 14:11:54 +000015573 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015574 }
15575 return vertex;
15576}
15577
15578static void compress_ancestors(struct sdom_block *v)
15579{
15580 /* This procedure assumes ancestor(v) != 0 */
15581 /* if (ancestor(ancestor(v)) != 0) {
15582 * compress(ancestor(ancestor(v)));
15583 * if (semi(label(ancestor(v))) < semi(label(v))) {
15584 * label(v) = label(ancestor(v));
15585 * }
15586 * ancestor(v) = ancestor(ancestor(v));
15587 * }
15588 */
15589 if (!v->ancestor) {
15590 return;
15591 }
15592 if (v->ancestor->ancestor) {
15593 compress_ancestors(v->ancestor->ancestor);
15594 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15595 v->label = v->ancestor->label;
15596 }
15597 v->ancestor = v->ancestor->ancestor;
15598 }
15599}
15600
Eric Biederman90089602004-05-28 14:11:54 +000015601static void compute_sdom(struct compile_state *state,
15602 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015603{
15604 int i;
15605 /* // step 2
15606 * for each v <= pred(w) {
15607 * u = EVAL(v);
15608 * if (semi[u] < semi[w] {
15609 * semi[w] = semi[u];
15610 * }
15611 * }
15612 * add w to bucket(vertex(semi[w]));
15613 * LINK(parent(w), w);
15614 *
15615 * // step 3
15616 * for each v <= bucket(parent(w)) {
15617 * delete v from bucket(parent(w));
15618 * u = EVAL(v);
15619 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15620 * }
15621 */
Eric Biederman90089602004-05-28 14:11:54 +000015622 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015623 struct sdom_block *v, *parent, *next;
15624 struct block_set *user;
15625 struct block *block;
15626 block = sd[i].block;
15627 parent = sd[i].parent;
15628 /* Step 2 */
15629 for(user = block->use; user; user = user->next) {
15630 struct sdom_block *v, *u;
15631 v = &sd[user->member->vertex];
15632 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15633 if (u->sdom->vertex < sd[i].sdom->vertex) {
15634 sd[i].sdom = u->sdom;
15635 }
15636 }
15637 sdom_block(sd[i].sdom, &sd[i]);
15638 sd[i].ancestor = parent;
15639 /* Step 3 */
15640 for(v = parent->sdominates; v; v = next) {
15641 struct sdom_block *u;
15642 next = v->sdom_next;
15643 unsdom_block(v);
15644 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15645 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
15646 u->block : parent->block;
15647 }
15648 }
15649}
15650
Eric Biederman90089602004-05-28 14:11:54 +000015651static void compute_spdom(struct compile_state *state,
15652 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015653{
15654 int i;
15655 /* // step 2
15656 * for each v <= pred(w) {
15657 * u = EVAL(v);
15658 * if (semi[u] < semi[w] {
15659 * semi[w] = semi[u];
15660 * }
15661 * }
15662 * add w to bucket(vertex(semi[w]));
15663 * LINK(parent(w), w);
15664 *
15665 * // step 3
15666 * for each v <= bucket(parent(w)) {
15667 * delete v from bucket(parent(w));
15668 * u = EVAL(v);
15669 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15670 * }
15671 */
Eric Biederman90089602004-05-28 14:11:54 +000015672 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015673 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015674 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015675 struct block *block;
15676 block = sd[i].block;
15677 parent = sd[i].parent;
15678 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015679 for(edge = block->edges; edge; edge = edge->next) {
15680 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015681 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15682 if (u->sdom->vertex < sd[i].sdom->vertex) {
15683 sd[i].sdom = u->sdom;
15684 }
15685 }
15686 sdom_block(sd[i].sdom, &sd[i]);
15687 sd[i].ancestor = parent;
15688 /* Step 3 */
15689 for(v = parent->sdominates; v; v = next) {
15690 struct sdom_block *u;
15691 next = v->sdom_next;
15692 unsdom_block(v);
15693 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15694 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
15695 u->block : parent->block;
15696 }
15697 }
15698}
15699
Eric Biederman90089602004-05-28 14:11:54 +000015700static void compute_idom(struct compile_state *state,
15701 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015702{
15703 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015704 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015705 struct block *block;
15706 block = sd[i].block;
15707 if (block->idom->vertex != sd[i].sdom->vertex) {
15708 block->idom = block->idom->idom;
15709 }
15710 idom_block(block->idom, block);
15711 }
15712 sd[1].block->idom = 0;
15713}
15714
Eric Biederman90089602004-05-28 14:11:54 +000015715static void compute_ipdom(struct compile_state *state,
15716 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015717{
15718 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015719 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015720 struct block *block;
15721 block = sd[i].block;
15722 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15723 block->ipdom = block->ipdom->ipdom;
15724 }
15725 ipdom_block(block->ipdom, block);
15726 }
15727 sd[1].block->ipdom = 0;
15728}
15729
15730 /* Theorem 1:
15731 * Every vertex of a flowgraph G = (V, E, r) except r has
15732 * a unique immediate dominator.
15733 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
15734 * rooted at r, called the dominator tree of G, such that
15735 * v dominates w if and only if v is a proper ancestor of w in
15736 * the dominator tree.
15737 */
15738 /* Lemma 1:
15739 * If v and w are vertices of G such that v <= w,
15740 * than any path from v to w must contain a common ancestor
15741 * of v and w in T.
15742 */
15743 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15744 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15745 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15746 /* Theorem 2:
15747 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15748 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15749 */
15750 /* Theorem 3:
15751 * Let w != r and let u be a vertex for which sdom(u) is
15752 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15753 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15754 */
15755 /* Lemma 5: Let vertices v,w satisfy v -> w.
15756 * Then v -> idom(w) or idom(w) -> idom(v)
15757 */
15758
Eric Biederman90089602004-05-28 14:11:54 +000015759static void find_immediate_dominators(struct compile_state *state,
15760 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015761{
15762 struct sdom_block *sd;
15763 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15764 * vi > w for (1 <= i <= k - 1}
15765 */
15766 /* Theorem 4:
15767 * For any vertex w != r.
15768 * sdom(w) = min(
15769 * {v|(v,w) <= E and v < w } U
15770 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15771 */
15772 /* Corollary 1:
15773 * Let w != r and let u be a vertex for which sdom(u) is
15774 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15775 * Then:
15776 * { sdom(w) if sdom(w) = sdom(u),
15777 * idom(w) = {
15778 * { idom(u) otherwise
15779 */
15780 /* The algorithm consists of the following 4 steps.
15781 * Step 1. Carry out a depth-first search of the problem graph.
15782 * Number the vertices from 1 to N as they are reached during
15783 * the search. Initialize the variables used in succeeding steps.
15784 * Step 2. Compute the semidominators of all vertices by applying
15785 * theorem 4. Carry out the computation vertex by vertex in
15786 * decreasing order by number.
15787 * Step 3. Implicitly define the immediate dominator of each vertex
15788 * by applying Corollary 1.
15789 * Step 4. Explicitly define the immediate dominator of each vertex,
15790 * carrying out the computation vertex by vertex in increasing order
15791 * by number.
15792 */
15793 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015794 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15795 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015796#if 0
15797 sd[1].size = 0;
15798 sd[1].label = 0;
15799 sd[1].sdom = 0;
15800#endif
15801 /* Step 2 compute the semidominators */
15802 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015803 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015804 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015805 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015806 xfree(sd);
15807}
15808
Eric Biederman90089602004-05-28 14:11:54 +000015809static void find_post_dominators(struct compile_state *state,
15810 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015811{
15812 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015813 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015814 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015815 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015816
Eric Biederman90089602004-05-28 14:11:54 +000015817 vertex = setup_spdblocks(state, bb, sd);
15818 if (vertex != bb->last_vertex) {
15819 internal_error(state, 0, "missing %d blocks",
15820 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015821 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015822
15823 /* Step 2 compute the semidominators */
15824 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015825 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015826 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015827 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015828 xfree(sd);
15829}
15830
15831
15832
15833static void find_block_domf(struct compile_state *state, struct block *block)
15834{
15835 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015836 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015837 if (block->domfrontier != 0) {
15838 internal_error(state, block->first, "domfrontier present?");
15839 }
15840 for(user = block->idominates; user; user = user->next) {
15841 child = user->member;
15842 if (child->idom != block) {
15843 internal_error(state, block->first, "bad idom");
15844 }
15845 find_block_domf(state, child);
15846 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015847 for(edge = block->edges; edge; edge = edge->next) {
15848 if (edge->member->idom != block) {
15849 domf_block(block, edge->member);
15850 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015851 }
15852 for(user = block->idominates; user; user = user->next) {
15853 struct block_set *frontier;
15854 child = user->member;
15855 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15856 if (frontier->member->idom != block) {
15857 domf_block(block, frontier->member);
15858 }
15859 }
15860 }
15861}
15862
15863static void find_block_ipdomf(struct compile_state *state, struct block *block)
15864{
15865 struct block *child;
15866 struct block_set *user;
15867 if (block->ipdomfrontier != 0) {
15868 internal_error(state, block->first, "ipdomfrontier present?");
15869 }
15870 for(user = block->ipdominates; user; user = user->next) {
15871 child = user->member;
15872 if (child->ipdom != block) {
15873 internal_error(state, block->first, "bad ipdom");
15874 }
15875 find_block_ipdomf(state, child);
15876 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015877 for(user = block->use; user; user = user->next) {
15878 if (user->member->ipdom != block) {
15879 ipdomf_block(block, user->member);
15880 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015881 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015882 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015883 struct block_set *frontier;
15884 child = user->member;
15885 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15886 if (frontier->member->ipdom != block) {
15887 ipdomf_block(block, frontier->member);
15888 }
15889 }
15890 }
15891}
15892
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015893static void print_dominated(
15894 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015895{
15896 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015897 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015898
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015899 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015900 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015901 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015902 if (user->member->idom != block) {
15903 internal_error(state, user->member->first, "bad idom");
15904 }
15905 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015906 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015907}
15908
Eric Biederman5ade04a2003-10-22 04:03:46 +000015909static void print_dominated2(
15910 struct compile_state *state, FILE *fp, int depth, struct block *block)
15911{
15912 struct block_set *user;
15913 struct triple *ins;
15914 struct occurance *ptr, *ptr2;
15915 const char *filename1, *filename2;
15916 int equal_filenames;
15917 int i;
15918 for(i = 0; i < depth; i++) {
15919 fprintf(fp, " ");
15920 }
15921 fprintf(fp, "%3d: %p (%p - %p) @",
15922 block->vertex, block, block->first, block->last);
15923 ins = block->first;
15924 while(ins != block->last && (ins->occurance->line == 0)) {
15925 ins = ins->next;
15926 }
15927 ptr = ins->occurance;
15928 ptr2 = block->last->occurance;
15929 filename1 = ptr->filename? ptr->filename : "";
15930 filename2 = ptr2->filename? ptr2->filename : "";
15931 equal_filenames = (strcmp(filename1, filename2) == 0);
15932 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15933 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15934 } else if (equal_filenames) {
15935 fprintf(fp, " %s:(%d - %d)",
15936 ptr->filename, ptr->line, ptr2->line);
15937 } else {
15938 fprintf(fp, " (%s:%d - %s:%d)",
15939 ptr->filename, ptr->line,
15940 ptr2->filename, ptr2->line);
15941 }
15942 fprintf(fp, "\n");
15943 for(user = block->idominates; user; user = user->next) {
15944 print_dominated2(state, fp, depth + 1, user->member);
15945 }
15946}
15947
Eric Biederman90089602004-05-28 14:11:54 +000015948static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015949{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015950 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015951 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015952 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015953 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015954}
15955
15956
15957static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015958 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015959{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015960 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015961
15962 if (!block || (block->vertex != vertex + 1)) {
15963 return vertex;
15964 }
15965 vertex += 1;
15966
Eric Biederman7dea9552004-06-29 05:38:37 +000015967 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015968 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015969 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015970 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015971 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015972
15973 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015974 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015975 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015976 return vertex;
15977}
Eric Biederman90089602004-05-28 14:11:54 +000015978static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015979 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015980{
Eric Biederman7dea9552004-06-29 05:38:37 +000015981 fprintf(fp, "\ndominance frontiers\n");
15982 print_frontiers(state, fp, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015983
15984}
15985
Eric Biederman90089602004-05-28 14:11:54 +000015986static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015987{
15988 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015989 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015990 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015991 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015992 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015993 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015994 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015995 print_dominance_frontiers(state, state->dbgout, bb);
15996 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015997 }
15998}
15999
16000
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016001static void print_ipdominated(
16002 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016003{
16004 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016005 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016006
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016007 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016008 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016009 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016010 if (user->member->ipdom != block) {
16011 internal_error(state, user->member->first, "bad ipdom");
16012 }
16013 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016014 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016015}
16016
Eric Biederman90089602004-05-28 14:11:54 +000016017static void print_ipdominators(struct compile_state *state, FILE *fp,
16018 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016019{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016020 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016021 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016022}
16023
16024static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016025 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016026{
16027 struct block_set *user;
16028
16029 if (!block || (block->vertex != vertex + 1)) {
16030 return vertex;
16031 }
16032 vertex += 1;
16033
Eric Biederman7dea9552004-06-29 05:38:37 +000016034 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016035 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016036 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016037 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016038 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016039 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016040 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016041 }
16042 return vertex;
16043}
Eric Biederman90089602004-05-28 14:11:54 +000016044static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016045 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016046{
Eric Biederman7dea9552004-06-29 05:38:37 +000016047 fprintf(fp, "\nipdominance frontiers\n");
16048 print_pfrontiers(state, fp, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016049
16050}
16051
Eric Biederman90089602004-05-28 14:11:54 +000016052static void analyze_ipdominators(struct compile_state *state,
16053 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016054{
16055 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016056 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016057 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016058 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016059 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016060 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016061 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016062 print_ipdominance_frontiers(state, state->dbgout, bb);
16063 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016064 }
16065}
16066
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016067static int bdominates(struct compile_state *state,
16068 struct block *dom, struct block *sub)
16069{
16070 while(sub && (sub != dom)) {
16071 sub = sub->idom;
16072 }
16073 return sub == dom;
16074}
16075
16076static int tdominates(struct compile_state *state,
16077 struct triple *dom, struct triple *sub)
16078{
16079 struct block *bdom, *bsub;
16080 int result;
16081 bdom = block_of_triple(state, dom);
16082 bsub = block_of_triple(state, sub);
16083 if (bdom != bsub) {
16084 result = bdominates(state, bdom, bsub);
16085 }
16086 else {
16087 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016088 if (!bdom || !bsub) {
16089 internal_error(state, dom, "huh?");
16090 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016091 ins = sub;
16092 while((ins != bsub->first) && (ins != dom)) {
16093 ins = ins->prev;
16094 }
16095 result = (ins == dom);
16096 }
16097 return result;
16098}
16099
Eric Biederman90089602004-05-28 14:11:54 +000016100static void analyze_basic_blocks(
16101 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016102{
Eric Biederman90089602004-05-28 14:11:54 +000016103 setup_basic_blocks(state, bb);
16104 analyze_idominators(state, bb);
16105 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016106}
16107
Eric Biedermanb138ac82003-04-22 18:44:01 +000016108static void insert_phi_operations(struct compile_state *state)
16109{
16110 size_t size;
16111 struct triple *first;
16112 int *has_already, *work;
16113 struct block *work_list, **work_list_tail;
16114 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016115 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016116
Eric Biederman90089602004-05-28 14:11:54 +000016117 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016118 has_already = xcmalloc(size, "has_already");
16119 work = xcmalloc(size, "work");
16120 iter = 0;
16121
Eric Biederman83b991a2003-10-11 06:20:25 +000016122 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016123 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016124 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016125 struct triple_set *user, *unext;
16126 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016127
16128 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016129 continue;
16130 }
Eric Biederman90089602004-05-28 14:11:54 +000016131
Eric Biedermanb138ac82003-04-22 18:44:01 +000016132 iter += 1;
16133 work_list = 0;
16134 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016135 for(user = var->use; user; user = unext) {
16136 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016137 if (MISC(var, 0) == user->member) {
16138 continue;
16139 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016140 if (user->member->op == OP_READ) {
16141 continue;
16142 }
16143 if (user->member->op != OP_WRITE) {
16144 internal_error(state, user->member,
16145 "bad variable access");
16146 }
16147 block = user->member->u.block;
16148 if (!block) {
16149 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016150 release_triple(state, user->member);
16151 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016152 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016153 if (work[block->vertex] >= iter) {
16154 continue;
16155 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016156 work[block->vertex] = iter;
16157 *work_list_tail = block;
16158 block->work_next = 0;
16159 work_list_tail = &block->work_next;
16160 }
16161 for(block = work_list; block; block = block->work_next) {
16162 struct block_set *df;
16163 for(df = block->domfrontier; df; df = df->next) {
16164 struct triple *phi;
16165 struct block *front;
16166 int in_edges;
16167 front = df->member;
16168
16169 if (has_already[front->vertex] >= iter) {
16170 continue;
16171 }
16172 /* Count how many edges flow into this block */
16173 in_edges = front->users;
16174 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016175 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016176 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016177 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016178 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016179 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016180 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016181 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016182#if 1
16183 if (phi->rhs != in_edges) {
16184 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16185 phi->rhs, in_edges);
16186 }
16187#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016188 /* Insert the phi functions immediately after the label */
16189 insert_triple(state, front->first->next, phi);
16190 if (front->first == front->last) {
16191 front->last = front->first->next;
16192 }
16193 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016194 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016195
Eric Biedermanb138ac82003-04-22 18:44:01 +000016196 /* If necessary plan to visit the basic block */
16197 if (work[front->vertex] >= iter) {
16198 continue;
16199 }
16200 work[front->vertex] = iter;
16201 *work_list_tail = front;
16202 front->work_next = 0;
16203 work_list_tail = &front->work_next;
16204 }
16205 }
16206 }
16207 xfree(has_already);
16208 xfree(work);
16209}
16210
Eric Biederman66fe2222003-07-04 15:14:04 +000016211
Eric Biederman83b991a2003-10-11 06:20:25 +000016212struct stack {
16213 struct triple_set *top;
16214 unsigned orig_id;
16215};
16216
Eric Biederman90089602004-05-28 14:11:54 +000016217static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016218{
16219 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016220 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016221 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016222 ins = first;
16223 do {
Eric Biederman90089602004-05-28 14:11:54 +000016224 if (triple_is_auto_var(state, ins)) {
16225 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016226 }
16227 ins = ins->next;
16228 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016229 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016230}
16231
Eric Biederman90089602004-05-28 14:11:54 +000016232static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016233{
16234 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016235 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016236 first = state->first;
16237 ins = first;
16238 do {
Eric Biederman90089602004-05-28 14:11:54 +000016239 if (triple_is_auto_var(state, ins)) {
16240 auto_vars += 1;
16241 stacks[auto_vars].orig_id = ins->id;
16242 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016243 }
16244 ins = ins->next;
16245 } while(ins != first);
16246}
16247
Eric Biederman90089602004-05-28 14:11:54 +000016248static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016249{
16250 struct triple *first, *ins;
16251 first = state->first;
16252 ins = first;
16253 do {
Eric Biederman90089602004-05-28 14:11:54 +000016254 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016255 ins->id = stacks[ins->id].orig_id;
16256 }
16257 ins = ins->next;
16258 } while(ins != first);
16259}
16260
16261static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016262{
16263 struct triple_set *head;
16264 struct triple *top_val;
16265 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016266 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016267 if (head) {
16268 top_val = head->member;
16269 }
16270 return top_val;
16271}
16272
Eric Biederman83b991a2003-10-11 06:20:25 +000016273static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016274{
16275 struct triple_set *new;
16276 /* Append new to the head of the list,
16277 * it's the only sensible behavoir for a stack.
16278 */
16279 new = xcmalloc(sizeof(*new), "triple_set");
16280 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016281 new->next = stacks[var->id].top;
16282 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016283}
16284
Eric Biederman83b991a2003-10-11 06:20:25 +000016285static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016286{
16287 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016288 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016289 while(*ptr) {
16290 set = *ptr;
16291 if (set->member == oldval) {
16292 *ptr = set->next;
16293 xfree(set);
16294 /* Only free one occurance from the stack */
16295 return;
16296 }
16297 else {
16298 ptr = &set->next;
16299 }
16300 }
16301}
16302
Eric Biedermanb138ac82003-04-22 18:44:01 +000016303/*
16304 * C(V)
16305 * S(V)
16306 */
16307static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016308 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016309{
16310 struct block_set *set;
16311 struct triple *ptr;
16312 int edge;
16313 if (!parent || !block)
16314 return;
16315 /* Find the edge I am coming in on */
16316 edge = 0;
16317 for(set = block->use; set; set = set->next, edge++) {
16318 if (set->member == parent) {
16319 break;
16320 }
16321 }
16322 if (!set) {
16323 internal_error(state, 0, "phi input is not on a control predecessor");
16324 }
16325 for(ptr = block->first; ; ptr = ptr->next) {
16326 if (ptr->op == OP_PHI) {
16327 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016328 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016329 if (!var) {
16330 internal_error(state, ptr, "no var???");
16331 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016332 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016333 val = peek_triple(stacks, var);
16334 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016335 internal_error(state, val, "bad value in phi");
16336 }
Eric Biederman90089602004-05-28 14:11:54 +000016337 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016338 internal_error(state, ptr, "edges > phi rhs");
16339 }
16340 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016341 if ((*slot != 0) && (*slot != val)) {
16342 internal_error(state, ptr, "phi already bound on this edge");
16343 }
16344 *slot = val;
16345 use_triple(val, ptr);
16346 }
16347 if (ptr == block->last) {
16348 break;
16349 }
16350 }
16351}
16352
16353
16354static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016355 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016356{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016357 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016358 struct triple *ptr, *next, *last;
16359 int done;
16360 if (!block)
16361 return;
16362 last = block->first;
16363 done = 0;
16364 for(ptr = block->first; !done; ptr = next) {
16365 next = ptr->next;
16366 if (ptr == block->last) {
16367 done = 1;
16368 }
16369 /* RHS(A) */
16370 if (ptr->op == OP_READ) {
16371 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016372 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016373 if (!triple_is_auto_var(state, var)) {
16374 internal_error(state, ptr, "read of non auto var!");
16375 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016376 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016377 /* Find the current value of the variable */
16378 val = peek_triple(stacks, var);
16379 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016380 /* Let the optimizer at variables that are not initially
16381 * set. But give it a bogus value so things seem to
16382 * work by accident. This is useful for bitfields because
16383 * setting them always involves a read-modify-write.
16384 */
16385 if (TYPE_ARITHMETIC(ptr->type->type)) {
16386 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16387 val->u.cval = 0xdeadbeaf;
16388 } else {
16389 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16390 }
16391 }
16392 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016393 error(state, ptr, "variable used without being set");
16394 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016395 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16396 internal_error(state, val, "bad value in read");
16397 }
16398 propogate_use(state, ptr, val);
16399 release_triple(state, ptr);
16400 continue;
16401 }
16402 /* LHS(A) */
16403 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016404 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016405 var = MISC(ptr, 0);
16406 if (!triple_is_auto_var(state, var)) {
16407 internal_error(state, ptr, "write to non auto var!");
16408 }
16409 tval = val = RHS(ptr, 0);
16410 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16411 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016412 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016413 }
Eric Biederman90089602004-05-28 14:11:54 +000016414 /* Insert a cast if the types differ */
16415 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016416 if (val->op == OP_INTCONST) {
16417 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16418 tval->u.cval = val->u.cval;
16419 }
16420 else {
Eric Biederman90089602004-05-28 14:11:54 +000016421 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016422 use_triple(val, tval);
16423 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016424 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016425 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016426 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016427 use_triple(tval, ptr);
16428 }
16429 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016430 unuse_triple(var, ptr);
16431 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016432 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016433 }
16434 if (ptr->op == OP_PHI) {
16435 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016436 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016437 if (!triple_is_auto_var(state, var)) {
16438 internal_error(state, ptr, "phi references non auto var!");
16439 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016440 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016441 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016442 }
16443 last = ptr;
16444 }
16445 block->last = last;
16446
16447 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016448 for(edge = block->edges; edge; edge = edge->next) {
16449 fixup_block_phi_variables(state, stacks, block, edge->member);
16450 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016451 /* rename variables in the dominated nodes */
16452 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016453 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016454 }
16455 /* pop the renamed variable stack */
16456 last = block->first;
16457 done = 0;
16458 for(ptr = block->first; !done ; ptr = next) {
16459 next = ptr->next;
16460 if (ptr == block->last) {
16461 done = 1;
16462 }
16463 if (ptr->op == OP_WRITE) {
16464 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016465 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016466 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016467 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016468 release_triple(state, ptr);
16469 continue;
16470 }
16471 if (ptr->op == OP_PHI) {
16472 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016473 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016474 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016475 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016476 }
16477 last = ptr;
16478 }
16479 block->last = last;
16480}
16481
Eric Biederman83b991a2003-10-11 06:20:25 +000016482static void rename_variables(struct compile_state *state)
16483{
16484 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016485 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016486
16487 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016488 auto_vars = count_auto_vars(state);
16489 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016490
Eric Biederman90089602004-05-28 14:11:54 +000016491 /* Give each auto_var a stack */
16492 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016493
16494 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016495 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016496
Eric Biederman90089602004-05-28 14:11:54 +000016497 /* Remove the stacks from the auto_vars */
16498 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016499 xfree(stacks);
16500}
16501
Eric Biedermanb138ac82003-04-22 18:44:01 +000016502static void prune_block_variables(struct compile_state *state,
16503 struct block *block)
16504{
16505 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016506 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016507 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016508
Eric Biedermanb138ac82003-04-22 18:44:01 +000016509 done = 0;
16510 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016511 /* Be extremely careful I am deleting the list
16512 * as I walk trhough it.
16513 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016514 next = ptr->next;
16515 if (ptr == block->last) {
16516 done = 1;
16517 }
Eric Biederman90089602004-05-28 14:11:54 +000016518 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016519 struct triple_set *user, *next;
16520 for(user = ptr->use; user; user = next) {
16521 struct triple *use;
16522 next = user->next;
16523 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016524 if (MISC(ptr, 0) == user->member) {
16525 continue;
16526 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016527 if (use->op != OP_PHI) {
16528 internal_error(state, use, "decl still used");
16529 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016530 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016531 internal_error(state, use, "bad phi use of decl");
16532 }
16533 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016534 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016535 }
Eric Biederman90089602004-05-28 14:11:54 +000016536 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16537 /* Delete the adecl */
16538 release_triple(state, MISC(ptr, 0));
16539 /* And the piece */
16540 release_triple(state, ptr);
16541 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016542 continue;
16543 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016544 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016545 for(user = block->idominates; user; user = user->next) {
16546 prune_block_variables(state, user->member);
16547 }
16548}
16549
Eric Biederman66fe2222003-07-04 15:14:04 +000016550struct phi_triple {
16551 struct triple *phi;
16552 unsigned orig_id;
16553 int alive;
16554};
16555
16556static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16557{
16558 struct triple **slot;
16559 int zrhs, i;
16560 if (live[phi->id].alive) {
16561 return;
16562 }
16563 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016564 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016565 slot = &RHS(phi, 0);
16566 for(i = 0; i < zrhs; i++) {
16567 struct triple *used;
16568 used = slot[i];
16569 if (used && (used->op == OP_PHI)) {
16570 keep_phi(state, live, used);
16571 }
16572 }
16573}
16574
16575static void prune_unused_phis(struct compile_state *state)
16576{
16577 struct triple *first, *phi;
16578 struct phi_triple *live;
16579 int phis, i;
16580
Eric Biederman66fe2222003-07-04 15:14:04 +000016581 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016582 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016583
16584 /* Count how many phi functions I need to process */
16585 phis = 0;
16586 for(phi = first->next; phi != first; phi = phi->next) {
16587 if (phi->op == OP_PHI) {
16588 phis += 1;
16589 }
16590 }
16591
16592 /* Mark them all dead */
16593 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16594 phis = 0;
16595 for(phi = first->next; phi != first; phi = phi->next) {
16596 if (phi->op != OP_PHI) {
16597 continue;
16598 }
16599 live[phis].alive = 0;
16600 live[phis].orig_id = phi->id;
16601 live[phis].phi = phi;
16602 phi->id = phis;
16603 phis += 1;
16604 }
16605
16606 /* Mark phis alive that are used by non phis */
16607 for(i = 0; i < phis; i++) {
16608 struct triple_set *set;
16609 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16610 if (set->member->op != OP_PHI) {
16611 keep_phi(state, live, live[i].phi);
16612 break;
16613 }
16614 }
16615 }
16616
16617 /* Delete the extraneous phis */
16618 for(i = 0; i < phis; i++) {
16619 struct triple **slot;
16620 int zrhs, j;
16621 if (!live[i].alive) {
16622 release_triple(state, live[i].phi);
16623 continue;
16624 }
16625 phi = live[i].phi;
16626 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016627 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016628 for(j = 0; j < zrhs; j++) {
16629 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016630 struct triple *unknown;
16631 get_occurance(phi->occurance);
16632 unknown = flatten(state, state->global_pool,
16633 alloc_triple(state, OP_UNKNOWNVAL,
16634 phi->type, 0, 0, phi->occurance));
16635 slot[j] = unknown;
16636 use_triple(unknown, phi);
16637 transform_to_arch_instruction(state, unknown);
16638#if 0
16639 warning(state, phi, "variable not set at index %d on all paths to use", j);
16640#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016641 }
16642 }
16643 }
16644 xfree(live);
16645}
16646
Eric Biedermanb138ac82003-04-22 18:44:01 +000016647static void transform_to_ssa_form(struct compile_state *state)
16648{
16649 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016650 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016651
Eric Biederman90089602004-05-28 14:11:54 +000016652 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016653 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016654
Eric Biederman90089602004-05-28 14:11:54 +000016655 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016656}
16657
16658
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016659static void clear_vertex(
16660 struct compile_state *state, struct block *block, void *arg)
16661{
Eric Biederman83b991a2003-10-11 06:20:25 +000016662 /* Clear the current blocks vertex and the vertex of all
16663 * of the current blocks neighbors in case there are malformed
16664 * blocks with now instructions at this point.
16665 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016666 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016667 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016668 for(edge = block->edges; edge; edge = edge->next) {
16669 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016670 }
16671 for(user = block->use; user; user = user->next) {
16672 user->member->vertex = 0;
16673 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016674}
16675
16676static void mark_live_block(
16677 struct compile_state *state, struct block *block, int *next_vertex)
16678{
16679 /* See if this is a block that has not been marked */
16680 if (block->vertex != 0) {
16681 return;
16682 }
16683 block->vertex = *next_vertex;
16684 *next_vertex += 1;
16685 if (triple_is_branch(state, block->last)) {
16686 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016687 targ = triple_edge_targ(state, block->last, 0);
16688 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016689 if (!*targ) {
16690 continue;
16691 }
16692 if (!triple_stores_block(state, *targ)) {
16693 internal_error(state, 0, "bad targ");
16694 }
16695 mark_live_block(state, (*targ)->u.block, next_vertex);
16696 }
Eric Biederman90089602004-05-28 14:11:54 +000016697 /* Ensure the last block of a function remains alive */
16698 if (triple_is_call(state, block->last)) {
16699 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16700 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016701 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016702 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016703 struct triple *ins;
16704 ins = block->last->next;
16705 if (!triple_stores_block(state, ins)) {
16706 internal_error(state, 0, "bad block start");
16707 }
16708 mark_live_block(state, ins->u.block, next_vertex);
16709 }
16710}
16711
Eric Biedermanb138ac82003-04-22 18:44:01 +000016712static void transform_from_ssa_form(struct compile_state *state)
16713{
16714 /* To get out of ssa form we insert moves on the incoming
16715 * edges to blocks containting phi functions.
16716 */
16717 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016718 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016719 int next_vertex;
16720
16721 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016722 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016723 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016724 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016725
16726 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016727 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016728 for(phi = first->next; phi != first ; phi = next) {
16729 struct block_set *set;
16730 struct block *block;
16731 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016732 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016733 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016734 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016735 next = phi->next;
16736 if (phi->op != OP_PHI) {
16737 continue;
16738 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016739
Eric Biedermanb138ac82003-04-22 18:44:01 +000016740 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016741 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016742
Eric Biederman83b991a2003-10-11 06:20:25 +000016743 /* If this phi is in a dead block just forget it */
16744 if (block->vertex == 0) {
16745 release_triple(state, phi);
16746 continue;
16747 }
16748
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016749 /* Forget uses from code in dead blocks */
16750 for(use = phi->use; use; use = use_next) {
16751 struct block *ublock;
16752 struct triple **expr;
16753 use_next = use->next;
16754 ublock = block_of_triple(state, use->member);
16755 if ((use->member == phi) || (ublock->vertex != 0)) {
16756 continue;
16757 }
16758 expr = triple_rhs(state, use->member, 0);
16759 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16760 if (*expr == phi) {
16761 *expr = 0;
16762 }
16763 }
16764 unuse_triple(phi, use->member);
16765 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016766 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016767 if (registers_of(state, phi->type) != 1) {
16768 internal_error(state, phi, "phi->type does not fit in a single register!");
16769 }
16770 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16771 var = var->next; /* point at the var */
16772
Eric Biederman83b991a2003-10-11 06:20:25 +000016773 /* Replaces use of phi with var */
16774 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016775
Eric Biederman90089602004-05-28 14:11:54 +000016776 /* Count the readers */
16777 readers = 0;
16778 for(use = var->use; use; use = use->next) {
16779 if (use->member != MISC(var, 0)) {
16780 readers++;
16781 }
16782 }
16783
Eric Biedermanb138ac82003-04-22 18:44:01 +000016784 /* Walk all of the incoming edges/blocks and insert moves.
16785 */
Eric Biederman90089602004-05-28 14:11:54 +000016786 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016787 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016788 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016789 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016790 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016791 eblock = set->member;
16792 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016793 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016794 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016795 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016796
Eric Biederman83b991a2003-10-11 06:20:25 +000016797 /* If we don't have a value that belongs in an OP_WRITE
16798 * continue on.
16799 */
Eric Biederman90089602004-05-28 14:11:54 +000016800 if (!val || (val == &unknown_triple) || (val == phi)
16801 || (vblock && (vblock->vertex == 0))) {
16802 continue;
16803 }
16804 /* If the value should never occur error */
16805 if (!vblock) {
16806 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016807 continue;
16808 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016809
16810 /* If the value occurs in a dead block see if a replacement
16811 * block can be found.
16812 */
16813 while(eblock && (eblock->vertex == 0)) {
16814 eblock = eblock->idom;
16815 }
16816 /* If not continue on with the next value. */
16817 if (!eblock || (eblock->vertex == 0)) {
16818 continue;
16819 }
16820
16821 /* If we have an empty incoming block ignore it. */
16822 if (!eblock->first) {
16823 internal_error(state, 0, "empty block?");
16824 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016825
Eric Biederman530b5192003-07-01 10:05:30 +000016826 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016827 /* Walk through the edge block backwards to find an
16828 * appropriate location for the OP_WRITE.
16829 */
16830 for(base = eblock->last; base != eblock->first; base = base->prev) {
16831 struct triple **expr;
16832 if (base->op == OP_PIECE) {
16833 base = MISC(base, 0);
16834 }
16835 if ((base == var) || (base == val)) {
16836 goto out;
16837 }
16838 expr = triple_lhs(state, base, 0);
16839 for(; expr; expr = triple_lhs(state, base, expr)) {
16840 if ((*expr) == val) {
16841 goto out;
16842 }
16843 }
16844 expr = triple_rhs(state, base, 0);
16845 for(; expr; expr = triple_rhs(state, base, expr)) {
16846 if ((*expr) == var) {
16847 goto out;
16848 }
16849 }
Eric Biederman530b5192003-07-01 10:05:30 +000016850 }
Eric Biederman90089602004-05-28 14:11:54 +000016851 out:
16852 if (triple_is_branch(state, base)) {
16853 internal_error(state, base,
16854 "Could not insert write to phi");
16855 }
16856 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016857 use_triple(val, move);
16858 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016859 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016860 }
Eric Biederman90089602004-05-28 14:11:54 +000016861 if (!writers && readers) {
16862 internal_error(state, var, "no value written to in use phi?");
16863 }
16864 /* If var is not used free it */
16865 if (!writers) {
16866 release_triple(state, MISC(var, 0));
16867 release_triple(state, var);
16868 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016869 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016870 release_triple(state, phi);
16871 }
16872
Eric Biederman83b991a2003-10-11 06:20:25 +000016873 /* Walk all of the operations to find the adecls */
16874 for(var = first->next; var != first ; var = var->next) {
16875 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016876 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016877 continue;
16878 }
16879
16880 /* Walk through all of the rhs uses of var and
16881 * replace them with read of var.
16882 */
16883 for(use = var->use; use; use = use_next) {
16884 struct triple *read, *user;
16885 struct triple **slot;
16886 int zrhs, i, used;
16887 use_next = use->next;
16888 user = use->member;
16889
16890 /* Generate a read of var */
16891 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16892 use_triple(var, read);
16893
16894 /* Find the rhs uses and see if they need to be replaced */
16895 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016896 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016897 slot = &RHS(user, 0);
16898 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016899 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016900 slot[i] = read;
16901 used = 1;
16902 }
16903 }
16904 /* If we did use it cleanup the uses */
16905 if (used) {
16906 unuse_triple(var, user);
16907 use_triple(read, user);
16908 }
16909 /* If we didn't use it release the extra triple */
16910 else {
16911 release_triple(state, read);
16912 }
16913 }
16914 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016915}
16916
Eric Biederman5ade04a2003-10-22 04:03:46 +000016917#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016918 FILE *fp = state->dbgout; \
16919 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Eric Biederman5ade04a2003-10-22 04:03:46 +000016920 }
16921
Eric Biederman83b991a2003-10-11 06:20:25 +000016922static void rebuild_ssa_form(struct compile_state *state)
16923{
16924HI();
16925 transform_from_ssa_form(state);
16926HI();
Eric Biederman90089602004-05-28 14:11:54 +000016927 state->bb.first = state->first;
16928 free_basic_blocks(state, &state->bb);
16929 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016930HI();
16931 insert_phi_operations(state);
16932HI();
16933 rename_variables(state);
16934HI();
16935
Eric Biederman90089602004-05-28 14:11:54 +000016936 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016937HI();
16938 prune_unused_phis(state);
16939HI();
16940}
16941#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016942
16943/*
16944 * Register conflict resolution
16945 * =========================================================
16946 */
16947
16948static struct reg_info find_def_color(
16949 struct compile_state *state, struct triple *def)
16950{
16951 struct triple_set *set;
16952 struct reg_info info;
16953 info.reg = REG_UNSET;
16954 info.regcm = 0;
16955 if (!triple_is_def(state, def)) {
16956 return info;
16957 }
16958 info = arch_reg_lhs(state, def, 0);
16959 if (info.reg >= MAX_REGISTERS) {
16960 info.reg = REG_UNSET;
16961 }
16962 for(set = def->use; set; set = set->next) {
16963 struct reg_info tinfo;
16964 int i;
16965 i = find_rhs_use(state, set->member, def);
16966 if (i < 0) {
16967 continue;
16968 }
16969 tinfo = arch_reg_rhs(state, set->member, i);
16970 if (tinfo.reg >= MAX_REGISTERS) {
16971 tinfo.reg = REG_UNSET;
16972 }
16973 if ((tinfo.reg != REG_UNSET) &&
16974 (info.reg != REG_UNSET) &&
16975 (tinfo.reg != info.reg)) {
16976 internal_error(state, def, "register conflict");
16977 }
16978 if ((info.regcm & tinfo.regcm) == 0) {
16979 internal_error(state, def, "regcm conflict %x & %x == 0",
16980 info.regcm, tinfo.regcm);
16981 }
16982 if (info.reg == REG_UNSET) {
16983 info.reg = tinfo.reg;
16984 }
16985 info.regcm &= tinfo.regcm;
16986 }
16987 if (info.reg >= MAX_REGISTERS) {
16988 internal_error(state, def, "register out of range");
16989 }
16990 return info;
16991}
16992
16993static struct reg_info find_lhs_pre_color(
16994 struct compile_state *state, struct triple *ins, int index)
16995{
16996 struct reg_info info;
16997 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016998 zrhs = ins->rhs;
16999 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017000 if (!zlhs && triple_is_def(state, ins)) {
17001 zlhs = 1;
17002 }
17003 if (index >= zlhs) {
17004 internal_error(state, ins, "Bad lhs %d", index);
17005 }
17006 info = arch_reg_lhs(state, ins, index);
17007 for(i = 0; i < zrhs; i++) {
17008 struct reg_info rinfo;
17009 rinfo = arch_reg_rhs(state, ins, i);
17010 if ((info.reg == rinfo.reg) &&
17011 (rinfo.reg >= MAX_REGISTERS)) {
17012 struct reg_info tinfo;
17013 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17014 info.reg = tinfo.reg;
17015 info.regcm &= tinfo.regcm;
17016 break;
17017 }
17018 }
17019 if (info.reg >= MAX_REGISTERS) {
17020 info.reg = REG_UNSET;
17021 }
17022 return info;
17023}
17024
17025static struct reg_info find_rhs_post_color(
17026 struct compile_state *state, struct triple *ins, int index);
17027
17028static struct reg_info find_lhs_post_color(
17029 struct compile_state *state, struct triple *ins, int index)
17030{
17031 struct triple_set *set;
17032 struct reg_info info;
17033 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017034#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017035 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017036 ins, index);
17037#endif
17038 if ((index == 0) && triple_is_def(state, ins)) {
17039 lhs = ins;
17040 }
Eric Biederman90089602004-05-28 14:11:54 +000017041 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017042 lhs = LHS(ins, index);
17043 }
17044 else {
17045 internal_error(state, ins, "Bad lhs %d", index);
17046 lhs = 0;
17047 }
17048 info = arch_reg_lhs(state, ins, index);
17049 if (info.reg >= MAX_REGISTERS) {
17050 info.reg = REG_UNSET;
17051 }
17052 for(set = lhs->use; set; set = set->next) {
17053 struct reg_info rinfo;
17054 struct triple *user;
17055 int zrhs, i;
17056 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017057 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017058 for(i = 0; i < zrhs; i++) {
17059 if (RHS(user, i) != lhs) {
17060 continue;
17061 }
17062 rinfo = find_rhs_post_color(state, user, i);
17063 if ((info.reg != REG_UNSET) &&
17064 (rinfo.reg != REG_UNSET) &&
17065 (info.reg != rinfo.reg)) {
17066 internal_error(state, ins, "register conflict");
17067 }
17068 if ((info.regcm & rinfo.regcm) == 0) {
17069 internal_error(state, ins, "regcm conflict %x & %x == 0",
17070 info.regcm, rinfo.regcm);
17071 }
17072 if (info.reg == REG_UNSET) {
17073 info.reg = rinfo.reg;
17074 }
17075 info.regcm &= rinfo.regcm;
17076 }
17077 }
Eric Biederman530b5192003-07-01 10:05:30 +000017078#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017079 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017080 ins, index, info.reg, info.regcm);
17081#endif
17082 return info;
17083}
17084
17085static struct reg_info find_rhs_post_color(
17086 struct compile_state *state, struct triple *ins, int index)
17087{
17088 struct reg_info info, rinfo;
17089 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017090#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017091 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017092 ins, index);
17093#endif
17094 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017095 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017096 if (!zlhs && triple_is_def(state, ins)) {
17097 zlhs = 1;
17098 }
17099 info = rinfo;
17100 if (info.reg >= MAX_REGISTERS) {
17101 info.reg = REG_UNSET;
17102 }
17103 for(i = 0; i < zlhs; i++) {
17104 struct reg_info linfo;
17105 linfo = arch_reg_lhs(state, ins, i);
17106 if ((linfo.reg == rinfo.reg) &&
17107 (linfo.reg >= MAX_REGISTERS)) {
17108 struct reg_info tinfo;
17109 tinfo = find_lhs_post_color(state, ins, i);
17110 if (tinfo.reg >= MAX_REGISTERS) {
17111 tinfo.reg = REG_UNSET;
17112 }
Eric Biederman530b5192003-07-01 10:05:30 +000017113 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017114 info.regcm &= tinfo.regcm;
17115 if (info.reg != REG_UNSET) {
17116 internal_error(state, ins, "register conflict");
17117 }
17118 if (info.regcm == 0) {
17119 internal_error(state, ins, "regcm conflict");
17120 }
17121 info.reg = tinfo.reg;
17122 }
17123 }
Eric Biederman530b5192003-07-01 10:05:30 +000017124#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017125 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017126 ins, index, info.reg, info.regcm);
17127#endif
17128 return info;
17129}
17130
17131static struct reg_info find_lhs_color(
17132 struct compile_state *state, struct triple *ins, int index)
17133{
17134 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017135#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017136 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017137 ins, index);
17138#endif
17139 pre = find_lhs_pre_color(state, ins, index);
17140 post = find_lhs_post_color(state, ins, index);
17141 if ((pre.reg != post.reg) &&
17142 (pre.reg != REG_UNSET) &&
17143 (post.reg != REG_UNSET)) {
17144 internal_error(state, ins, "register conflict");
17145 }
17146 info.regcm = pre.regcm & post.regcm;
17147 info.reg = pre.reg;
17148 if (info.reg == REG_UNSET) {
17149 info.reg = post.reg;
17150 }
Eric Biederman530b5192003-07-01 10:05:30 +000017151#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017152 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017153 ins, index, info.reg, info.regcm,
17154 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017155#endif
17156 return info;
17157}
17158
17159static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17160{
17161 struct triple_set *entry, *next;
17162 struct triple *out;
17163 struct reg_info info, rinfo;
17164
17165 info = arch_reg_lhs(state, ins, 0);
17166 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17167 use_triple(RHS(out, 0), out);
17168 /* Get the users of ins to use out instead */
17169 for(entry = ins->use; entry; entry = next) {
17170 int i;
17171 next = entry->next;
17172 if (entry->member == out) {
17173 continue;
17174 }
17175 i = find_rhs_use(state, entry->member, ins);
17176 if (i < 0) {
17177 continue;
17178 }
17179 rinfo = arch_reg_rhs(state, entry->member, i);
17180 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17181 continue;
17182 }
17183 replace_rhs_use(state, ins, out, entry->member);
17184 }
17185 transform_to_arch_instruction(state, out);
17186 return out;
17187}
17188
Eric Biedermand1ea5392003-06-28 06:49:45 +000017189static struct triple *typed_pre_copy(
17190 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017191{
17192 /* Carefully insert enough operations so that I can
17193 * enter any operation with a GPR32.
17194 */
17195 struct triple *in;
17196 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017197 unsigned classes;
17198 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017199 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017200 if (ins->op == OP_PHI) {
17201 internal_error(state, ins, "pre_copy on a phi?");
17202 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017203 classes = arch_type_to_regcm(state, type);
17204 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017205 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017206 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017207 FILE *fp = state->errout;
17208 fprintf(fp, "src_type: ");
17209 name_of(fp, ins->type);
17210 fprintf(fp, "\ndst_type: ");
17211 name_of(fp, type);
17212 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017213 internal_error(state, ins, "pre_copy with no register classes");
17214 }
Eric Biederman90089602004-05-28 14:11:54 +000017215 op = OP_COPY;
17216 if (!equiv_types(type, (*expr)->type)) {
17217 op = OP_CONVERT;
17218 }
17219 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017220 unuse_triple(*expr, ins);
17221 *expr = in;
17222 use_triple(RHS(in, 0), in);
17223 use_triple(in, ins);
17224 transform_to_arch_instruction(state, in);
17225 return in;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017226
17227}
17228static struct triple *pre_copy(
17229 struct compile_state *state, struct triple *ins, int index)
17230{
17231 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017232}
17233
17234
Eric Biedermanb138ac82003-04-22 18:44:01 +000017235static void insert_copies_to_phi(struct compile_state *state)
17236{
17237 /* To get out of ssa form we insert moves on the incoming
17238 * edges to blocks containting phi functions.
17239 */
17240 struct triple *first;
17241 struct triple *phi;
17242
17243 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017244 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017245 for(phi = first->next; phi != first ; phi = phi->next) {
17246 struct block_set *set;
17247 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017248 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017249 int edge;
17250 if (phi->op != OP_PHI) {
17251 continue;
17252 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017253 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017254 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017255 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017256 /* Phi's that feed into mandatory live range joins
17257 * cause nasty complications. Insert a copy of
17258 * the phi value so I never have to deal with
17259 * that in the rest of the code.
17260 */
17261 copy = post_copy(state, phi);
17262 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017263 /* Walk all of the incoming edges/blocks and insert moves.
17264 */
17265 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17266 struct block *eblock;
17267 struct triple *move;
17268 struct triple *val;
17269 struct triple *ptr;
17270 eblock = set->member;
17271 val = slot[edge];
17272
17273 if (val == phi) {
17274 continue;
17275 }
17276
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017277 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017278 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017279 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017280 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017281 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017282 use_triple(val, move);
17283
17284 slot[edge] = move;
17285 unuse_triple(val, phi);
17286 use_triple(move, phi);
17287
Eric Biederman66fe2222003-07-04 15:14:04 +000017288 /* Walk up the dominator tree until I have found the appropriate block */
17289 while(eblock && !tdominates(state, val, eblock->last)) {
17290 eblock = eblock->idom;
17291 }
17292 if (!eblock) {
17293 internal_error(state, phi, "Cannot find block dominated by %p",
17294 val);
17295 }
17296
Eric Biedermanb138ac82003-04-22 18:44:01 +000017297 /* Walk through the block backwards to find
17298 * an appropriate location for the OP_COPY.
17299 */
17300 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17301 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017302 if (ptr->op == OP_PIECE) {
17303 ptr = MISC(ptr, 0);
17304 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017305 if ((ptr == phi) || (ptr == val)) {
17306 goto out;
17307 }
Eric Biederman90089602004-05-28 14:11:54 +000017308 expr = triple_lhs(state, ptr, 0);
17309 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17310 if ((*expr) == val) {
17311 goto out;
17312 }
17313 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017314 expr = triple_rhs(state, ptr, 0);
17315 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17316 if ((*expr) == phi) {
17317 goto out;
17318 }
17319 }
17320 }
17321 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017322 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017323 internal_error(state, ptr,
17324 "Could not insert write to phi");
17325 }
Eric Biederman90089602004-05-28 14:11:54 +000017326 insert_triple(state, after_lhs(state, ptr), move);
17327 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017328 eblock->last = move;
17329 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017330 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017331 }
17332 }
Eric Biederman90089602004-05-28 14:11:54 +000017333 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017334}
17335
Eric Biederman90089602004-05-28 14:11:54 +000017336struct triple_reg_set;
17337struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017338
Eric Biedermanb138ac82003-04-22 18:44:01 +000017339
17340static int do_triple_set(struct triple_reg_set **head,
17341 struct triple *member, struct triple *new_member)
17342{
17343 struct triple_reg_set **ptr, *new;
17344 if (!member)
17345 return 0;
17346 ptr = head;
17347 while(*ptr) {
17348 if ((*ptr)->member == member) {
17349 return 0;
17350 }
17351 ptr = &(*ptr)->next;
17352 }
17353 new = xcmalloc(sizeof(*new), "triple_set");
17354 new->member = member;
17355 new->new = new_member;
17356 new->next = *head;
17357 *head = new;
17358 return 1;
17359}
17360
17361static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17362{
17363 struct triple_reg_set *entry, **ptr;
17364 ptr = head;
17365 while(*ptr) {
17366 entry = *ptr;
17367 if (entry->member == member) {
17368 *ptr = entry->next;
17369 xfree(entry);
17370 return;
17371 }
17372 else {
17373 ptr = &entry->next;
17374 }
17375 }
17376}
17377
17378static int in_triple(struct reg_block *rb, struct triple *in)
17379{
17380 return do_triple_set(&rb->in, in, 0);
17381}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017382
17383#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017384static void unin_triple(struct reg_block *rb, struct triple *unin)
17385{
17386 do_triple_unset(&rb->in, unin);
17387}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017388#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017389
17390static int out_triple(struct reg_block *rb, struct triple *out)
17391{
17392 return do_triple_set(&rb->out, out, 0);
17393}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017394#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017395static void unout_triple(struct reg_block *rb, struct triple *unout)
17396{
17397 do_triple_unset(&rb->out, unout);
17398}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017399#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017400
17401static int initialize_regblock(struct reg_block *blocks,
17402 struct block *block, int vertex)
17403{
17404 struct block_set *user;
17405 if (!block || (blocks[block->vertex].block == block)) {
17406 return vertex;
17407 }
17408 vertex += 1;
17409 /* Renumber the blocks in a convinient fashion */
17410 block->vertex = vertex;
17411 blocks[vertex].block = block;
17412 blocks[vertex].vertex = vertex;
17413 for(user = block->use; user; user = user->next) {
17414 vertex = initialize_regblock(blocks, user->member, vertex);
17415 }
17416 return vertex;
17417}
17418
Eric Biederman90089602004-05-28 14:11:54 +000017419static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17420{
17421/* Part to piece is a best attempt and it cannot be correct all by
17422 * itself. If various values are read as different sizes in different
17423 * parts of the code this function cannot work. Or rather it cannot
17424 * work in conjunction with compute_variable_liftimes. As the
17425 * analysis will get confused.
17426 */
17427 struct triple *base;
17428 unsigned reg;
17429 if (!is_lvalue(state, ins)) {
17430 return ins;
17431 }
17432 base = 0;
17433 reg = 0;
17434 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17435 base = MISC(ins, 0);
17436 switch(ins->op) {
17437 case OP_INDEX:
17438 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17439 break;
17440 case OP_DOT:
17441 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17442 break;
17443 default:
17444 internal_error(state, ins, "unhandled part");
17445 break;
17446 }
17447 ins = base;
17448 }
17449 if (base) {
17450 if (reg > base->lhs) {
17451 internal_error(state, base, "part out of range?");
17452 }
17453 ins = LHS(base, reg);
17454 }
17455 return ins;
17456}
17457
17458static int this_def(struct compile_state *state,
17459 struct triple *ins, struct triple *other)
17460{
17461 if (ins == other) {
17462 return 1;
17463 }
17464 if (ins->op == OP_WRITE) {
17465 ins = part_to_piece(state, MISC(ins, 0));
17466 }
17467 return ins == other;
17468}
17469
Eric Biedermanb138ac82003-04-22 18:44:01 +000017470static int phi_in(struct compile_state *state, struct reg_block *blocks,
17471 struct reg_block *rb, struct block *suc)
17472{
17473 /* Read the conditional input set of a successor block
17474 * (i.e. the input to the phi nodes) and place it in the
17475 * current blocks output set.
17476 */
17477 struct block_set *set;
17478 struct triple *ptr;
17479 int edge;
17480 int done, change;
17481 change = 0;
17482 /* Find the edge I am coming in on */
17483 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17484 if (set->member == rb->block) {
17485 break;
17486 }
17487 }
17488 if (!set) {
17489 internal_error(state, 0, "Not coming on a control edge?");
17490 }
17491 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17492 struct triple **slot, *expr, *ptr2;
17493 int out_change, done2;
17494 done = (ptr == suc->last);
17495 if (ptr->op != OP_PHI) {
17496 continue;
17497 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017498 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017499 expr = slot[edge];
17500 out_change = out_triple(rb, expr);
17501 if (!out_change) {
17502 continue;
17503 }
17504 /* If we don't define the variable also plast it
17505 * in the current blocks input set.
17506 */
17507 ptr2 = rb->block->first;
17508 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017509 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017510 break;
17511 }
17512 done2 = (ptr2 == rb->block->last);
17513 }
17514 if (!done2) {
17515 continue;
17516 }
17517 change |= in_triple(rb, expr);
17518 }
17519 return change;
17520}
17521
17522static int reg_in(struct compile_state *state, struct reg_block *blocks,
17523 struct reg_block *rb, struct block *suc)
17524{
17525 struct triple_reg_set *in_set;
17526 int change;
17527 change = 0;
17528 /* Read the input set of a successor block
17529 * and place it in the current blocks output set.
17530 */
17531 in_set = blocks[suc->vertex].in;
17532 for(; in_set; in_set = in_set->next) {
17533 int out_change, done;
17534 struct triple *first, *last, *ptr;
17535 out_change = out_triple(rb, in_set->member);
17536 if (!out_change) {
17537 continue;
17538 }
17539 /* If we don't define the variable also place it
17540 * in the current blocks input set.
17541 */
17542 first = rb->block->first;
17543 last = rb->block->last;
17544 done = 0;
17545 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017546 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017547 break;
17548 }
17549 done = (ptr == last);
17550 }
17551 if (!done) {
17552 continue;
17553 }
17554 change |= in_triple(rb, in_set->member);
17555 }
17556 change |= phi_in(state, blocks, rb, suc);
17557 return change;
17558}
17559
Eric Biedermanb138ac82003-04-22 18:44:01 +000017560static int use_in(struct compile_state *state, struct reg_block *rb)
17561{
17562 /* Find the variables we use but don't define and add
17563 * it to the current blocks input set.
17564 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017565#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017566#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017567#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017568 struct block *block;
17569 struct triple *ptr;
17570 int done;
17571 int change;
17572 block = rb->block;
17573 change = 0;
17574 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17575 struct triple **expr;
17576 done = (ptr == block->first);
17577 /* The variable a phi function uses depends on the
17578 * control flow, and is handled in phi_in, not
17579 * here.
17580 */
17581 if (ptr->op == OP_PHI) {
17582 continue;
17583 }
17584 expr = triple_rhs(state, ptr, 0);
17585 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17586 struct triple *rhs, *test;
17587 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017588 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017589 if (!rhs) {
17590 continue;
17591 }
Eric Biederman90089602004-05-28 14:11:54 +000017592
17593 /* See if rhs is defined in this block.
17594 * A write counts as a definition.
17595 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017596 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17597 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017598 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017599 rhs = 0;
17600 break;
17601 }
17602 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017603 /* If I still have a valid rhs add it to in */
17604 change |= in_triple(rb, rhs);
17605 }
17606 }
17607 return change;
17608}
17609
17610static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017611 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017612{
17613 struct reg_block *blocks;
17614 int change;
17615 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017616 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17617 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017618 do {
17619 int i;
17620 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017621 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017622 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017623 struct reg_block *rb;
17624 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017625 /* Add the all successor's input set to in */
17626 for(edge = rb->block->edges; edge; edge = edge->next) {
17627 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017628 }
17629 /* Add use to in... */
17630 change |= use_in(state, rb);
17631 }
17632 } while(change);
17633 return blocks;
17634}
17635
Eric Biederman90089602004-05-28 14:11:54 +000017636static void free_variable_lifetimes(struct compile_state *state,
17637 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017638{
17639 int i;
17640 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017641 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017642 struct triple_reg_set *entry, *next;
17643 struct reg_block *rb;
17644 rb = &blocks[i];
17645 for(entry = rb->in; entry ; entry = next) {
17646 next = entry->next;
17647 do_triple_unset(&rb->in, entry->member);
17648 }
17649 for(entry = rb->out; entry; entry = next) {
17650 next = entry->next;
17651 do_triple_unset(&rb->out, entry->member);
17652 }
17653 }
17654 xfree(blocks);
17655
17656}
17657
Eric Biedermanf96a8102003-06-16 16:57:34 +000017658typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000017659 struct compile_state *state,
17660 struct reg_block *blocks, struct triple_reg_set *live,
17661 struct reg_block *rb, struct triple *ins, void *arg);
17662
17663static void walk_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017664 struct basic_blocks *bb, struct reg_block *blocks,
17665 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017666{
17667 int i;
17668
Eric Biederman90089602004-05-28 14:11:54 +000017669 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017670 struct triple_reg_set *live;
17671 struct triple_reg_set *entry, *next;
17672 struct triple *ptr, *prev;
17673 struct reg_block *rb;
17674 struct block *block;
17675 int done;
17676
17677 /* Get the blocks */
17678 rb = &blocks[i];
17679 block = rb->block;
17680
17681 /* Copy out into live */
17682 live = 0;
17683 for(entry = rb->out; entry; entry = next) {
17684 next = entry->next;
17685 do_triple_set(&live, entry->member, entry->new);
17686 }
17687 /* Walk through the basic block calculating live */
17688 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017689 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017690
17691 prev = ptr->prev;
17692 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017693
17694 /* Ensure the current definition is in live */
17695 if (triple_is_def(state, ptr)) {
17696 do_triple_set(&live, ptr, 0);
17697 }
17698
17699 /* Inform the callback function of what is
17700 * going on.
17701 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017702 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017703
17704 /* Remove the current definition from live */
17705 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017706
Eric Biedermanb138ac82003-04-22 18:44:01 +000017707 /* Add the current uses to live.
17708 *
17709 * It is safe to skip phi functions because they do
17710 * not have any block local uses, and the block
17711 * output sets already properly account for what
17712 * control flow depedent uses phi functions do have.
17713 */
17714 if (ptr->op == OP_PHI) {
17715 continue;
17716 }
17717 expr = triple_rhs(state, ptr, 0);
17718 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17719 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017720 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017721 continue;
17722 }
17723 do_triple_set(&live, *expr, 0);
17724 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017725 }
17726 /* Free live */
17727 for(entry = live; entry; entry = next) {
17728 next = entry->next;
17729 do_triple_unset(&live, entry->member);
17730 }
17731 }
17732}
17733
Eric Biederman90089602004-05-28 14:11:54 +000017734struct print_live_variable_info {
17735 struct reg_block *rb;
17736 FILE *fp;
17737};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017738#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017739static void print_live_variables_block(
17740 struct compile_state *state, struct block *block, void *arg)
17741
17742{
17743 struct print_live_variable_info *info = arg;
17744 struct block_set *edge;
17745 FILE *fp = info->fp;
17746 struct reg_block *rb;
17747 struct triple *ptr;
17748 int phi_present;
17749 int done;
17750 rb = &info->rb[block->vertex];
17751
17752 fprintf(fp, "\nblock: %p (%d),",
17753 block, block->vertex);
17754 for(edge = block->edges; edge; edge = edge->next) {
17755 fprintf(fp, " %p<-%p",
17756 edge->member,
17757 edge->member && edge->member->use?edge->member->use->member : 0);
17758 }
17759 fprintf(fp, "\n");
17760 if (rb->in) {
17761 struct triple_reg_set *in_set;
17762 fprintf(fp, " in:");
17763 for(in_set = rb->in; in_set; in_set = in_set->next) {
17764 fprintf(fp, " %-10p", in_set->member);
17765 }
17766 fprintf(fp, "\n");
17767 }
17768 phi_present = 0;
17769 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17770 done = (ptr == block->last);
17771 if (ptr->op == OP_PHI) {
17772 phi_present = 1;
17773 break;
17774 }
17775 }
17776 if (phi_present) {
17777 int edge;
17778 for(edge = 0; edge < block->users; edge++) {
17779 fprintf(fp, " in(%d):", edge);
17780 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17781 struct triple **slot;
17782 done = (ptr == block->last);
17783 if (ptr->op != OP_PHI) {
17784 continue;
17785 }
17786 slot = &RHS(ptr, 0);
17787 fprintf(fp, " %-10p", slot[edge]);
17788 }
17789 fprintf(fp, "\n");
17790 }
17791 }
17792 if (block->first->op == OP_LABEL) {
17793 fprintf(fp, "%p:\n", block->first);
17794 }
17795 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17796 done = (ptr == block->last);
17797 display_triple(fp, ptr);
17798 }
17799 if (rb->out) {
17800 struct triple_reg_set *out_set;
17801 fprintf(fp, " out:");
17802 for(out_set = rb->out; out_set; out_set = out_set->next) {
17803 fprintf(fp, " %-10p", out_set->member);
17804 }
17805 fprintf(fp, "\n");
17806 }
17807 fprintf(fp, "\n");
17808}
17809
17810static void print_live_variables(struct compile_state *state,
17811 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17812{
17813 struct print_live_variable_info info;
17814 info.rb = rb;
17815 info.fp = fp;
17816 fprintf(fp, "\nlive variables by block\n");
17817 walk_blocks(state, bb, print_live_variables_block, &info);
17818
17819}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017820#endif
Eric Biederman90089602004-05-28 14:11:54 +000017821
Eric Biedermanb138ac82003-04-22 18:44:01 +000017822static int count_triples(struct compile_state *state)
17823{
17824 struct triple *first, *ins;
17825 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017826 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017827 ins = first;
17828 do {
17829 triples++;
17830 ins = ins->next;
17831 } while (ins != first);
17832 return triples;
17833}
Eric Biederman66fe2222003-07-04 15:14:04 +000017834
17835
Eric Biedermanb138ac82003-04-22 18:44:01 +000017836struct dead_triple {
17837 struct triple *triple;
17838 struct dead_triple *work_next;
17839 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017840 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017841 int flags;
17842#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017843#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017844};
17845
Eric Biederman90089602004-05-28 14:11:54 +000017846static void print_dead_triples(struct compile_state *state,
17847 struct dead_triple *dtriple)
17848{
17849 struct triple *first, *ins;
17850 struct dead_triple *dt;
17851 FILE *fp;
17852 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17853 return;
17854 }
17855 fp = state->dbgout;
17856 fprintf(fp, "--------------- dtriples ---------------\n");
17857 first = state->first;
17858 ins = first;
17859 do {
17860 dt = &dtriple[ins->id];
17861 if ((ins->op == OP_LABEL) && (ins->use)) {
17862 fprintf(fp, "\n%p:\n", ins);
17863 }
17864 fprintf(fp, "%c",
17865 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17866 display_triple(fp, ins);
17867 if (triple_is_branch(state, ins)) {
17868 fprintf(fp, "\n");
17869 }
17870 ins = ins->next;
17871 } while(ins != first);
17872 fprintf(fp, "\n");
17873}
17874
Eric Biedermanb138ac82003-04-22 18:44:01 +000017875
17876static void awaken(
17877 struct compile_state *state,
17878 struct dead_triple *dtriple, struct triple **expr,
17879 struct dead_triple ***work_list_tail)
17880{
17881 struct triple *triple;
17882 struct dead_triple *dt;
17883 if (!expr) {
17884 return;
17885 }
17886 triple = *expr;
17887 if (!triple) {
17888 return;
17889 }
17890 if (triple->id <= 0) {
17891 internal_error(state, triple, "bad triple id: %d",
17892 triple->id);
17893 }
17894 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017895 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017896 return;
17897 }
17898 dt = &dtriple[triple->id];
17899 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17900 dt->flags |= TRIPLE_FLAG_ALIVE;
17901 if (!dt->work_next) {
17902 **work_list_tail = dt;
17903 *work_list_tail = &dt->work_next;
17904 }
17905 }
17906}
17907
17908static void eliminate_inefectual_code(struct compile_state *state)
17909{
17910 struct block *block;
17911 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17912 int triples, i;
Eric Biederman83b991a2003-10-11 06:20:25 +000017913 struct triple *first, *final, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017914
Eric Biederman5ade04a2003-10-22 04:03:46 +000017915 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17916 return;
17917 }
17918
Eric Biedermanb138ac82003-04-22 18:44:01 +000017919 /* Setup the work list */
17920 work_list = 0;
17921 work_list_tail = &work_list;
17922
Eric Biederman83b991a2003-10-11 06:20:25 +000017923 first = state->first;
17924 final = state->first->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017925
17926 /* Count how many triples I have */
17927 triples = count_triples(state);
17928
17929 /* Now put then in an array and mark all of the triples dead */
17930 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
17931
17932 ins = first;
17933 i = 1;
17934 block = 0;
17935 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017936 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017937 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017938 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017939 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017940 ins->id = i;
17941 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017942 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017943 awaken(state, dtriple, &ins, &work_list_tail);
17944 }
17945 i++;
17946 ins = ins->next;
17947 } while(ins != first);
17948 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017949 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017950 struct dead_triple *dt;
17951 struct block_set *user;
17952 struct triple **expr;
17953 dt = work_list;
17954 work_list = dt->work_next;
17955 if (!work_list) {
17956 work_list_tail = &work_list;
17957 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017958 /* Make certain the block the current instruction is in lives */
17959 block = block_of_triple(state, dt->triple);
17960 awaken(state, dtriple, &block->first, &work_list_tail);
17961 if (triple_is_branch(state, block->last)) {
17962 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017963 } else {
17964 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017965 }
17966
Eric Biedermanb138ac82003-04-22 18:44:01 +000017967 /* Wake up the data depencencies of this triple */
17968 expr = 0;
17969 do {
17970 expr = triple_rhs(state, dt->triple, expr);
17971 awaken(state, dtriple, expr, &work_list_tail);
17972 } while(expr);
17973 do {
17974 expr = triple_lhs(state, dt->triple, expr);
17975 awaken(state, dtriple, expr, &work_list_tail);
17976 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017977 do {
17978 expr = triple_misc(state, dt->triple, expr);
17979 awaken(state, dtriple, expr, &work_list_tail);
17980 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017981 /* Wake up the forward control dependencies */
17982 do {
17983 expr = triple_targ(state, dt->triple, expr);
17984 awaken(state, dtriple, expr, &work_list_tail);
17985 } while(expr);
17986 /* Wake up the reverse control dependencies of this triple */
17987 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017988 struct triple *last;
17989 last = user->member->last;
17990 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017991#if DEBUG_ROMCC_WARNINGS
17992#warning "Should we bring the awakening noops back?"
17993#endif
17994 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017995 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017996 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017997 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017998 }
17999 }
Eric Biederman90089602004-05-28 14:11:54 +000018000 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018001 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
18002 if ((dt->triple->op == OP_NOOP) &&
18003 (dt->flags & TRIPLE_FLAG_ALIVE)) {
18004 internal_error(state, dt->triple, "noop effective?");
18005 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018006 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018007 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018008 release_triple(state, dt->triple);
18009 }
18010 }
18011 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018012
18013 rebuild_ssa_form(state);
18014
Eric Biederman90089602004-05-28 14:11:54 +000018015 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018016}
18017
18018
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018019static void insert_mandatory_copies(struct compile_state *state)
18020{
18021 struct triple *ins, *first;
18022
18023 /* The object is with a minimum of inserted copies,
18024 * to resolve in fundamental register conflicts between
18025 * register value producers and consumers.
18026 * Theoretically we may be greater than minimal when we
18027 * are inserting copies before instructions but that
18028 * case should be rare.
18029 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018030 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018031 ins = first;
18032 do {
18033 struct triple_set *entry, *next;
18034 struct triple *tmp;
18035 struct reg_info info;
18036 unsigned reg, regcm;
18037 int do_post_copy, do_pre_copy;
18038 tmp = 0;
18039 if (!triple_is_def(state, ins)) {
18040 goto next;
18041 }
18042 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018043 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018044 if (info.reg >= MAX_REGISTERS) {
18045 info.reg = REG_UNSET;
18046 }
Eric Biederman90089602004-05-28 14:11:54 +000018047
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018048 reg = REG_UNSET;
18049 regcm = arch_type_to_regcm(state, ins->type);
18050 do_post_copy = do_pre_copy = 0;
18051
18052 /* Walk through the uses of ins and check for conflicts */
18053 for(entry = ins->use; entry; entry = next) {
18054 struct reg_info rinfo;
18055 int i;
18056 next = entry->next;
18057 i = find_rhs_use(state, entry->member, ins);
18058 if (i < 0) {
18059 continue;
18060 }
18061
18062 /* Find the users color requirements */
18063 rinfo = arch_reg_rhs(state, entry->member, i);
18064 if (rinfo.reg >= MAX_REGISTERS) {
18065 rinfo.reg = REG_UNSET;
18066 }
18067
18068 /* See if I need a pre_copy */
18069 if (rinfo.reg != REG_UNSET) {
18070 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18071 do_pre_copy = 1;
18072 }
18073 reg = rinfo.reg;
18074 }
18075 regcm &= rinfo.regcm;
18076 regcm = arch_regcm_normalize(state, regcm);
18077 if (regcm == 0) {
18078 do_pre_copy = 1;
18079 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018080 /* Always use pre_copies for constants.
18081 * They do not take up any registers until a
18082 * copy places them in one.
18083 */
18084 if ((info.reg == REG_UNNEEDED) &&
18085 (rinfo.reg != REG_UNNEEDED)) {
18086 do_pre_copy = 1;
18087 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018088 }
18089 do_post_copy =
18090 !do_pre_copy &&
18091 (((info.reg != REG_UNSET) &&
18092 (reg != REG_UNSET) &&
18093 (info.reg != reg)) ||
18094 ((info.regcm & regcm) == 0));
18095
18096 reg = info.reg;
18097 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018098 /* 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 +000018099 for(entry = ins->use; entry; entry = next) {
18100 struct reg_info rinfo;
18101 int i;
18102 next = entry->next;
18103 i = find_rhs_use(state, entry->member, ins);
18104 if (i < 0) {
18105 continue;
18106 }
18107
18108 /* Find the users color requirements */
18109 rinfo = arch_reg_rhs(state, entry->member, i);
18110 if (rinfo.reg >= MAX_REGISTERS) {
18111 rinfo.reg = REG_UNSET;
18112 }
18113
18114 /* Now see if it is time to do the pre_copy */
18115 if (rinfo.reg != REG_UNSET) {
18116 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18117 ((regcm & rinfo.regcm) == 0) ||
18118 /* Don't let a mandatory coalesce sneak
18119 * into a operation that is marked to prevent
18120 * coalescing.
18121 */
18122 ((reg != REG_UNNEEDED) &&
18123 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18124 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18125 ) {
18126 if (do_pre_copy) {
18127 struct triple *user;
18128 user = entry->member;
18129 if (RHS(user, i) != ins) {
18130 internal_error(state, user, "bad rhs");
18131 }
18132 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018133 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018134 continue;
18135 } else {
18136 do_post_copy = 1;
18137 }
18138 }
18139 reg = rinfo.reg;
18140 }
18141 if ((regcm & rinfo.regcm) == 0) {
18142 if (do_pre_copy) {
18143 struct triple *user;
18144 user = entry->member;
18145 if (RHS(user, i) != ins) {
18146 internal_error(state, user, "bad rhs");
18147 }
18148 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018149 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018150 continue;
18151 } else {
18152 do_post_copy = 1;
18153 }
18154 }
18155 regcm &= rinfo.regcm;
18156
18157 }
18158 if (do_post_copy) {
18159 struct reg_info pre, post;
18160 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018161 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018162 pre = arch_reg_lhs(state, ins, 0);
18163 post = arch_reg_lhs(state, tmp, 0);
18164 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18165 internal_error(state, tmp, "useless copy");
18166 }
18167 }
18168 next:
18169 ins = ins->next;
18170 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018171
Eric Biederman90089602004-05-28 14:11:54 +000018172 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018173}
18174
18175
Eric Biedermanb138ac82003-04-22 18:44:01 +000018176struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018177struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018178struct live_range {
18179 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018180 struct live_range_def *defs;
18181/* Note. The list pointed to by defs is kept in order.
18182 * That is baring splits in the flow control
18183 * defs dominates defs->next wich dominates defs->next->next
18184 * etc.
18185 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018186 unsigned color;
18187 unsigned classes;
18188 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018189 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018190 struct live_range *group_next, **group_prev;
18191};
18192
18193struct live_range_edge {
18194 struct live_range_edge *next;
18195 struct live_range *node;
18196};
18197
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018198struct live_range_def {
18199 struct live_range_def *next;
18200 struct live_range_def *prev;
18201 struct live_range *lr;
18202 struct triple *def;
18203 unsigned orig_id;
18204};
18205
Eric Biedermanb138ac82003-04-22 18:44:01 +000018206#define LRE_HASH_SIZE 2048
18207struct lre_hash {
18208 struct lre_hash *next;
18209 struct live_range *left;
18210 struct live_range *right;
18211};
18212
18213
18214struct reg_state {
18215 struct lre_hash *hash[LRE_HASH_SIZE];
18216 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018217 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018218 struct live_range *lr;
18219 struct live_range *low, **low_tail;
18220 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018221 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018222 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018223 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018224};
18225
18226
Eric Biedermand1ea5392003-06-28 06:49:45 +000018227struct print_interference_block_info {
18228 struct reg_state *rstate;
18229 FILE *fp;
18230 int need_edges;
18231};
18232static void print_interference_block(
18233 struct compile_state *state, struct block *block, void *arg)
18234
18235{
18236 struct print_interference_block_info *info = arg;
18237 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018238 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018239 FILE *fp = info->fp;
18240 struct reg_block *rb;
18241 struct triple *ptr;
18242 int phi_present;
18243 int done;
18244 rb = &rstate->blocks[block->vertex];
18245
Eric Biederman5ade04a2003-10-22 04:03:46 +000018246 fprintf(fp, "\nblock: %p (%d),",
18247 block, block->vertex);
18248 for(edge = block->edges; edge; edge = edge->next) {
18249 fprintf(fp, " %p<-%p",
18250 edge->member,
18251 edge->member && edge->member->use?edge->member->use->member : 0);
18252 }
18253 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018254 if (rb->in) {
18255 struct triple_reg_set *in_set;
18256 fprintf(fp, " in:");
18257 for(in_set = rb->in; in_set; in_set = in_set->next) {
18258 fprintf(fp, " %-10p", in_set->member);
18259 }
18260 fprintf(fp, "\n");
18261 }
18262 phi_present = 0;
18263 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18264 done = (ptr == block->last);
18265 if (ptr->op == OP_PHI) {
18266 phi_present = 1;
18267 break;
18268 }
18269 }
18270 if (phi_present) {
18271 int edge;
18272 for(edge = 0; edge < block->users; edge++) {
18273 fprintf(fp, " in(%d):", edge);
18274 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18275 struct triple **slot;
18276 done = (ptr == block->last);
18277 if (ptr->op != OP_PHI) {
18278 continue;
18279 }
18280 slot = &RHS(ptr, 0);
18281 fprintf(fp, " %-10p", slot[edge]);
18282 }
18283 fprintf(fp, "\n");
18284 }
18285 }
18286 if (block->first->op == OP_LABEL) {
18287 fprintf(fp, "%p:\n", block->first);
18288 }
18289 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018290 struct live_range *lr;
18291 unsigned id;
18292 int op;
18293 op = ptr->op;
18294 done = (ptr == block->last);
18295 lr = rstate->lrd[ptr->id].lr;
18296
Eric Biedermand1ea5392003-06-28 06:49:45 +000018297 id = ptr->id;
18298 ptr->id = rstate->lrd[id].orig_id;
18299 SET_REG(ptr->id, lr->color);
18300 display_triple(fp, ptr);
18301 ptr->id = id;
18302
18303 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18304 internal_error(state, ptr, "lr has no defs!");
18305 }
18306 if (info->need_edges) {
18307 if (lr->defs) {
18308 struct live_range_def *lrd;
18309 fprintf(fp, " range:");
18310 lrd = lr->defs;
18311 do {
18312 fprintf(fp, " %-10p", lrd->def);
18313 lrd = lrd->next;
18314 } while(lrd != lr->defs);
18315 fprintf(fp, "\n");
18316 }
18317 if (lr->edges > 0) {
18318 struct live_range_edge *edge;
18319 fprintf(fp, " edges:");
18320 for(edge = lr->edges; edge; edge = edge->next) {
18321 struct live_range_def *lrd;
18322 lrd = edge->node->defs;
18323 do {
18324 fprintf(fp, " %-10p", lrd->def);
18325 lrd = lrd->next;
18326 } while(lrd != edge->node->defs);
18327 fprintf(fp, "|");
18328 }
18329 fprintf(fp, "\n");
18330 }
18331 }
18332 /* Do a bunch of sanity checks */
18333 valid_ins(state, ptr);
18334 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18335 internal_error(state, ptr, "Invalid triple id: %d",
18336 ptr->id);
18337 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018338 }
18339 if (rb->out) {
18340 struct triple_reg_set *out_set;
18341 fprintf(fp, " out:");
18342 for(out_set = rb->out; out_set; out_set = out_set->next) {
18343 fprintf(fp, " %-10p", out_set->member);
18344 }
18345 fprintf(fp, "\n");
18346 }
18347 fprintf(fp, "\n");
18348}
18349
18350static void print_interference_blocks(
18351 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18352{
18353 struct print_interference_block_info info;
18354 info.rstate = rstate;
18355 info.fp = fp;
18356 info.need_edges = need_edges;
18357 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018358 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018359
18360}
18361
Eric Biedermanb138ac82003-04-22 18:44:01 +000018362static unsigned regc_max_size(struct compile_state *state, int classes)
18363{
18364 unsigned max_size;
18365 int i;
18366 max_size = 0;
18367 for(i = 0; i < MAX_REGC; i++) {
18368 if (classes & (1 << i)) {
18369 unsigned size;
18370 size = arch_regc_size(state, i);
18371 if (size > max_size) {
18372 max_size = size;
18373 }
18374 }
18375 }
18376 return max_size;
18377}
18378
18379static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18380{
18381 unsigned equivs[MAX_REG_EQUIVS];
18382 int i;
18383 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18384 internal_error(state, 0, "invalid register");
18385 }
18386 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18387 internal_error(state, 0, "invalid register");
18388 }
18389 arch_reg_equivs(state, equivs, reg1);
18390 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18391 if (equivs[i] == reg2) {
18392 return 1;
18393 }
18394 }
18395 return 0;
18396}
18397
18398static void reg_fill_used(struct compile_state *state, char *used, int reg)
18399{
18400 unsigned equivs[MAX_REG_EQUIVS];
18401 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018402 if (reg == REG_UNNEEDED) {
18403 return;
18404 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018405 arch_reg_equivs(state, equivs, reg);
18406 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18407 used[equivs[i]] = 1;
18408 }
18409 return;
18410}
18411
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018412static void reg_inc_used(struct compile_state *state, char *used, int reg)
18413{
18414 unsigned equivs[MAX_REG_EQUIVS];
18415 int i;
18416 if (reg == REG_UNNEEDED) {
18417 return;
18418 }
18419 arch_reg_equivs(state, equivs, reg);
18420 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18421 used[equivs[i]] += 1;
18422 }
18423 return;
18424}
18425
Eric Biedermanb138ac82003-04-22 18:44:01 +000018426static unsigned int hash_live_edge(
18427 struct live_range *left, struct live_range *right)
18428{
18429 unsigned int hash, val;
18430 unsigned long lval, rval;
18431 lval = ((unsigned long)left)/sizeof(struct live_range);
18432 rval = ((unsigned long)right)/sizeof(struct live_range);
18433 hash = 0;
18434 while(lval) {
18435 val = lval & 0xff;
18436 lval >>= 8;
18437 hash = (hash *263) + val;
18438 }
18439 while(rval) {
18440 val = rval & 0xff;
18441 rval >>= 8;
18442 hash = (hash *263) + val;
18443 }
18444 hash = hash & (LRE_HASH_SIZE - 1);
18445 return hash;
18446}
18447
18448static struct lre_hash **lre_probe(struct reg_state *rstate,
18449 struct live_range *left, struct live_range *right)
18450{
18451 struct lre_hash **ptr;
18452 unsigned int index;
18453 /* Ensure left <= right */
18454 if (left > right) {
18455 struct live_range *tmp;
18456 tmp = left;
18457 left = right;
18458 right = tmp;
18459 }
18460 index = hash_live_edge(left, right);
18461
18462 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018463 while(*ptr) {
18464 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18465 break;
18466 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018467 ptr = &(*ptr)->next;
18468 }
18469 return ptr;
18470}
18471
18472static int interfere(struct reg_state *rstate,
18473 struct live_range *left, struct live_range *right)
18474{
18475 struct lre_hash **ptr;
18476 ptr = lre_probe(rstate, left, right);
18477 return ptr && *ptr;
18478}
18479
18480static void add_live_edge(struct reg_state *rstate,
18481 struct live_range *left, struct live_range *right)
18482{
18483 /* FIXME the memory allocation overhead is noticeable here... */
18484 struct lre_hash **ptr, *new_hash;
18485 struct live_range_edge *edge;
18486
18487 if (left == right) {
18488 return;
18489 }
18490 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18491 return;
18492 }
18493 /* Ensure left <= right */
18494 if (left > right) {
18495 struct live_range *tmp;
18496 tmp = left;
18497 left = right;
18498 right = tmp;
18499 }
18500 ptr = lre_probe(rstate, left, right);
18501 if (*ptr) {
18502 return;
18503 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018504#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018505 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018506 left, right);
18507#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018508 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18509 new_hash->next = *ptr;
18510 new_hash->left = left;
18511 new_hash->right = right;
18512 *ptr = new_hash;
18513
18514 edge = xmalloc(sizeof(*edge), "live_range_edge");
18515 edge->next = left->edges;
18516 edge->node = right;
18517 left->edges = edge;
18518 left->degree += 1;
18519
18520 edge = xmalloc(sizeof(*edge), "live_range_edge");
18521 edge->next = right->edges;
18522 edge->node = left;
18523 right->edges = edge;
18524 right->degree += 1;
18525}
18526
18527static void remove_live_edge(struct reg_state *rstate,
18528 struct live_range *left, struct live_range *right)
18529{
18530 struct live_range_edge *edge, **ptr;
18531 struct lre_hash **hptr, *entry;
18532 hptr = lre_probe(rstate, left, right);
18533 if (!hptr || !*hptr) {
18534 return;
18535 }
18536 entry = *hptr;
18537 *hptr = entry->next;
18538 xfree(entry);
18539
18540 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18541 edge = *ptr;
18542 if (edge->node == right) {
18543 *ptr = edge->next;
18544 memset(edge, 0, sizeof(*edge));
18545 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018546 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018547 break;
18548 }
18549 }
18550 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18551 edge = *ptr;
18552 if (edge->node == left) {
18553 *ptr = edge->next;
18554 memset(edge, 0, sizeof(*edge));
18555 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018556 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018557 break;
18558 }
18559 }
18560}
18561
18562static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18563{
18564 struct live_range_edge *edge, *next;
18565 for(edge = range->edges; edge; edge = next) {
18566 next = edge->next;
18567 remove_live_edge(rstate, range, edge->node);
18568 }
18569}
18570
Eric Biederman153ea352003-06-20 14:43:20 +000018571static void transfer_live_edges(struct reg_state *rstate,
18572 struct live_range *dest, struct live_range *src)
18573{
18574 struct live_range_edge *edge, *next;
18575 for(edge = src->edges; edge; edge = next) {
18576 struct live_range *other;
18577 next = edge->next;
18578 other = edge->node;
18579 remove_live_edge(rstate, src, other);
18580 add_live_edge(rstate, dest, other);
18581 }
18582}
18583
Eric Biedermanb138ac82003-04-22 18:44:01 +000018584
18585/* Interference graph...
18586 *
18587 * new(n) --- Return a graph with n nodes but no edges.
18588 * add(g,x,y) --- Return a graph including g with an between x and y
18589 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18590 * x and y in the graph g
18591 * degree(g, x) --- Return the degree of the node x in the graph g
18592 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18593 *
18594 * Implement with a hash table && a set of adjcency vectors.
18595 * The hash table supports constant time implementations of add and interfere.
18596 * The adjacency vectors support an efficient implementation of neighbors.
18597 */
18598
18599/*
18600 * +---------------------------------------------------+
18601 * | +--------------+ |
18602 * v v | |
18603 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
18604 *
18605 * -- In simplify implment optimistic coloring... (No backtracking)
18606 * -- Implement Rematerialization it is the only form of spilling we can perform
18607 * Essentially this means dropping a constant from a register because
18608 * we can regenerate it later.
18609 *
18610 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18611 * coalesce at phi points...
18612 * --- Bias coloring if at all possible do the coalesing a compile time.
18613 *
18614 *
18615 */
18616
Stefan Reinauer50542a82007-10-24 11:14:14 +000018617#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018618static void different_colored(
18619 struct compile_state *state, struct reg_state *rstate,
18620 struct triple *parent, struct triple *ins)
18621{
18622 struct live_range *lr;
18623 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018624 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018625 expr = triple_rhs(state, ins, 0);
18626 for(;expr; expr = triple_rhs(state, ins, expr)) {
18627 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018628 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018629 continue;
18630 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018631 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018632 if (lr->color == lr2->color) {
18633 internal_error(state, ins, "live range too big");
18634 }
18635 }
18636}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018637#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018638
18639static struct live_range *coalesce_ranges(
18640 struct compile_state *state, struct reg_state *rstate,
18641 struct live_range *lr1, struct live_range *lr2)
18642{
18643 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18644 unsigned color;
18645 unsigned classes;
18646 if (lr1 == lr2) {
18647 return lr1;
18648 }
18649 if (!lr1->defs || !lr2->defs) {
18650 internal_error(state, 0,
18651 "cannot coalese dead live ranges");
18652 }
18653 if ((lr1->color == REG_UNNEEDED) ||
18654 (lr2->color == REG_UNNEEDED)) {
18655 internal_error(state, 0,
18656 "cannot coalesce live ranges without a possible color");
18657 }
18658 if ((lr1->color != lr2->color) &&
18659 (lr1->color != REG_UNSET) &&
18660 (lr2->color != REG_UNSET)) {
18661 internal_error(state, lr1->defs->def,
18662 "cannot coalesce live ranges of different colors");
18663 }
18664 color = lr1->color;
18665 if (color == REG_UNSET) {
18666 color = lr2->color;
18667 }
18668 classes = lr1->classes & lr2->classes;
18669 if (!classes) {
18670 internal_error(state, lr1->defs->def,
18671 "cannot coalesce live ranges with dissimilar register classes");
18672 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018673 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018674 FILE *fp = state->errout;
18675 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018676 lrd = lr1->defs;
18677 do {
Eric Biederman90089602004-05-28 14:11:54 +000018678 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018679 lrd = lrd->next;
18680 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018681 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018682 lrd = lr2->defs;
18683 do {
Eric Biederman90089602004-05-28 14:11:54 +000018684 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018685 lrd = lrd->next;
18686 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018687 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018688 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018689 /* If there is a clear dominate live range put it in lr1,
18690 * For purposes of this test phi functions are
18691 * considered dominated by the definitions that feed into
18692 * them.
18693 */
18694 if ((lr1->defs->prev->def->op == OP_PHI) ||
18695 ((lr2->defs->prev->def->op != OP_PHI) &&
18696 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18697 struct live_range *tmp;
18698 tmp = lr1;
18699 lr1 = lr2;
18700 lr2 = tmp;
18701 }
18702#if 0
18703 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018704 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018705 }
18706 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018707 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018708 }
18709 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018710 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018711 }
18712 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018713 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018714 }
18715#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018716#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018717 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018718 lr1->defs->def,
18719 lr1->color,
18720 lr2->defs->def,
18721 lr2->color);
18722#endif
18723
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018724 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018725#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018726#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018727#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018728 /* This FIXME item applies to the correctness of live_range_end
18729 * and to the necessity of making multiple passes of coalesce_live_ranges.
18730 * A failure to find some coalesce opportunities in coaleace_live_ranges
18731 * does not impact the correct of the compiler just the efficiency with
18732 * which registers are allocated.
18733 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018734 head = lr1->defs;
18735 mid1 = lr1->defs->prev;
18736 mid2 = lr2->defs;
18737 end = lr2->defs->prev;
18738
18739 head->prev = end;
18740 end->next = head;
18741
18742 mid1->next = mid2;
18743 mid2->prev = mid1;
18744
18745 /* Fixup the live range in the added live range defs */
18746 lrd = head;
18747 do {
18748 lrd->lr = lr1;
18749 lrd = lrd->next;
18750 } while(lrd != head);
18751
18752 /* Mark lr2 as free. */
18753 lr2->defs = 0;
18754 lr2->color = REG_UNNEEDED;
18755 lr2->classes = 0;
18756
18757 if (!lr1->defs) {
18758 internal_error(state, 0, "lr1->defs == 0 ?");
18759 }
18760
18761 lr1->color = color;
18762 lr1->classes = classes;
18763
Eric Biederman153ea352003-06-20 14:43:20 +000018764 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18765 transfer_live_edges(rstate, lr1, lr2);
18766
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018767 return lr1;
18768}
18769
18770static struct live_range_def *live_range_head(
18771 struct compile_state *state, struct live_range *lr,
18772 struct live_range_def *last)
18773{
18774 struct live_range_def *result;
18775 result = 0;
18776 if (last == 0) {
18777 result = lr->defs;
18778 }
18779 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18780 result = last->next;
18781 }
18782 return result;
18783}
18784
18785static struct live_range_def *live_range_end(
18786 struct compile_state *state, struct live_range *lr,
18787 struct live_range_def *last)
18788{
18789 struct live_range_def *result;
18790 result = 0;
18791 if (last == 0) {
18792 result = lr->defs->prev;
18793 }
18794 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18795 result = last->prev;
18796 }
18797 return result;
18798}
18799
18800
Eric Biedermanb138ac82003-04-22 18:44:01 +000018801static void initialize_live_ranges(
18802 struct compile_state *state, struct reg_state *rstate)
18803{
18804 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018805 size_t count, size;
18806 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018807
Eric Biederman83b991a2003-10-11 06:20:25 +000018808 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018809 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018810 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018811 count = count_triples(state);
18812 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018813 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018814 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018815 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018816 /* Potentially I need one live range for each instruction
18817 * plus an extra for the dummy live range.
18818 */
18819 rstate->ranges = count + 1;
18820 size = sizeof(rstate->lrd[0]) * rstate->defs;
18821 rstate->lrd = xcmalloc(size, "live_range_def");
18822 size = sizeof(rstate->lr[0]) * rstate->ranges;
18823 rstate->lr = xcmalloc(size, "live_range");
18824
Eric Biedermanb138ac82003-04-22 18:44:01 +000018825 /* Setup the dummy live range */
18826 rstate->lr[0].classes = 0;
18827 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018828 rstate->lr[0].defs = 0;
18829 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018830 ins = first;
18831 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018832 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018833 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018834 struct reg_info info;
18835 /* Find the architecture specific color information */
18836 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018837 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018838 rstate->lr[i].defs = &rstate->lrd[j];
18839 rstate->lr[i].color = info.reg;
18840 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018841 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018842 rstate->lrd[j].lr = &rstate->lr[i];
18843 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018844 /* Otherwise give the triple the dummy live range. */
18845 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018846 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018847 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018848
18849 /* Initalize the live_range_def */
18850 rstate->lrd[j].next = &rstate->lrd[j];
18851 rstate->lrd[j].prev = &rstate->lrd[j];
18852 rstate->lrd[j].def = ins;
18853 rstate->lrd[j].orig_id = ins->id;
18854 ins->id = j;
18855
18856 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018857 ins = ins->next;
18858 } while(ins != first);
18859 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018860
Eric Biedermanb138ac82003-04-22 18:44:01 +000018861 /* Make a second pass to handle achitecture specific register
18862 * constraints.
18863 */
18864 ins = first;
18865 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018866 int zlhs, zrhs, i, j;
18867 if (ins->id > rstate->defs) {
18868 internal_error(state, ins, "bad id");
18869 }
18870
18871 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018872 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018873 if ((zlhs == 0) && triple_is_def(state, ins)) {
18874 zlhs = 1;
18875 }
Eric Biederman90089602004-05-28 14:11:54 +000018876 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018877
Eric Biederman5ade04a2003-10-22 04:03:46 +000018878 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018879 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018880 ins, zlhs, zrhs);
18881 }
18882
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018883 for(i = 0; i < zlhs; i++) {
18884 struct reg_info linfo;
18885 struct live_range_def *lhs;
18886 linfo = arch_reg_lhs(state, ins, i);
18887 if (linfo.reg < MAX_REGISTERS) {
18888 continue;
18889 }
18890 if (triple_is_def(state, ins)) {
18891 lhs = &rstate->lrd[ins->id];
18892 } else {
18893 lhs = &rstate->lrd[LHS(ins, i)->id];
18894 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018895
18896 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018897 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018898 i, lhs, linfo.reg);
18899 }
18900
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018901 for(j = 0; j < zrhs; j++) {
18902 struct reg_info rinfo;
18903 struct live_range_def *rhs;
18904 rinfo = arch_reg_rhs(state, ins, j);
18905 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018906 continue;
18907 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018908 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018909
18910 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018911 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018912 j, rhs, rinfo.reg);
18913 }
18914
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018915 if (rinfo.reg == linfo.reg) {
18916 coalesce_ranges(state, rstate,
18917 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018918 }
18919 }
18920 }
18921 ins = ins->next;
18922 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018923}
18924
Eric Biedermanf96a8102003-06-16 16:57:34 +000018925static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018926 struct compile_state *state,
18927 struct reg_block *blocks, struct triple_reg_set *live,
18928 struct reg_block *rb, struct triple *ins, void *arg)
18929{
18930 struct reg_state *rstate = arg;
18931 struct live_range *def;
18932 struct triple_reg_set *entry;
18933
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018934 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018935 * we do not have a definition to add to
18936 * the interference graph.
18937 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018938 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018939 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018940 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018941 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018942
18943 /* Create an edge between ins and everything that is
18944 * alive, unless the live_range cannot share
18945 * a physical register with ins.
18946 */
18947 for(entry = live; entry; entry = entry->next) {
18948 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018949 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18950 internal_error(state, 0, "bad entry?");
18951 }
18952 lr = rstate->lrd[entry->member->id].lr;
18953 if (def == lr) {
18954 continue;
18955 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018956 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18957 continue;
18958 }
18959 add_live_edge(rstate, def, lr);
18960 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018961 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018962}
18963
Stefan Reinauer50542a82007-10-24 11:14:14 +000018964#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018965static struct live_range *get_verify_live_range(
18966 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18967{
18968 struct live_range *lr;
18969 struct live_range_def *lrd;
18970 int ins_found;
18971 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18972 internal_error(state, ins, "bad ins?");
18973 }
18974 lr = rstate->lrd[ins->id].lr;
18975 ins_found = 0;
18976 lrd = lr->defs;
18977 do {
18978 if (lrd->def == ins) {
18979 ins_found = 1;
18980 }
18981 lrd = lrd->next;
18982 } while(lrd != lr->defs);
18983 if (!ins_found) {
18984 internal_error(state, ins, "ins not in live range");
18985 }
18986 return lr;
18987}
18988
18989static void verify_graph_ins(
18990 struct compile_state *state,
18991 struct reg_block *blocks, struct triple_reg_set *live,
18992 struct reg_block *rb, struct triple *ins, void *arg)
18993{
18994 struct reg_state *rstate = arg;
18995 struct triple_reg_set *entry1, *entry2;
18996
18997
18998 /* Compare live against edges and make certain the code is working */
18999 for(entry1 = live; entry1; entry1 = entry1->next) {
19000 struct live_range *lr1;
19001 lr1 = get_verify_live_range(state, rstate, entry1->member);
19002 for(entry2 = live; entry2; entry2 = entry2->next) {
19003 struct live_range *lr2;
19004 struct live_range_edge *edge2;
19005 int lr1_found;
19006 int lr2_degree;
19007 if (entry2 == entry1) {
19008 continue;
19009 }
19010 lr2 = get_verify_live_range(state, rstate, entry2->member);
19011 if (lr1 == lr2) {
19012 internal_error(state, entry2->member,
19013 "live range with 2 values simultaneously alive");
19014 }
19015 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19016 continue;
19017 }
19018 if (!interfere(rstate, lr1, lr2)) {
19019 internal_error(state, entry2->member,
19020 "edges don't interfere?");
19021 }
19022
19023 lr1_found = 0;
19024 lr2_degree = 0;
19025 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19026 lr2_degree++;
19027 if (edge2->node == lr1) {
19028 lr1_found = 1;
19029 }
19030 }
19031 if (lr2_degree != lr2->degree) {
19032 internal_error(state, entry2->member,
19033 "computed degree: %d does not match reported degree: %d\n",
19034 lr2_degree, lr2->degree);
19035 }
19036 if (!lr1_found) {
19037 internal_error(state, entry2->member, "missing edge");
19038 }
19039 }
19040 }
19041 return;
19042}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019043#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019044
Eric Biedermanf96a8102003-06-16 16:57:34 +000019045static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000019046 struct compile_state *state,
19047 struct reg_block *blocks, struct triple_reg_set *live,
19048 struct reg_block *rb, struct triple *ins, void *arg)
19049{
19050 struct reg_state *rstate = arg;
19051 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019052 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019053 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019054
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019055 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019056 id = ins->id;
19057 ins->id = rstate->lrd[id].orig_id;
19058 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019059 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019060 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019061
19062 if (lr->defs) {
19063 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019064 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019065 lrd = lr->defs;
19066 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019067 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019068 lrd = lrd->next;
19069 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019070 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019071 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019072 if (live) {
19073 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019074 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019075 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019076 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019077 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019078 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019079 }
19080 if (lr->edges) {
19081 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019082 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019083 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019084 struct live_range_def *lrd;
19085 lrd = entry->node->defs;
19086 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019087 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019088 lrd = lrd->next;
19089 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019090 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019091 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019092 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019093 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019094 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019095 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019096 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019097 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019098}
19099
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019100static int coalesce_live_ranges(
19101 struct compile_state *state, struct reg_state *rstate)
19102{
19103 /* At the point where a value is moved from one
19104 * register to another that value requires two
19105 * registers, thus increasing register pressure.
19106 * Live range coaleescing reduces the register
19107 * pressure by keeping a value in one register
19108 * longer.
19109 *
19110 * In the case of a phi function all paths leading
19111 * into it must be allocated to the same register
19112 * otherwise the phi function may not be removed.
19113 *
19114 * Forcing a value to stay in a single register
19115 * for an extended period of time does have
19116 * limitations when applied to non homogenous
19117 * register pool.
19118 *
19119 * The two cases I have identified are:
19120 * 1) Two forced register assignments may
19121 * collide.
19122 * 2) Registers may go unused because they
19123 * are only good for storing the value
19124 * and not manipulating it.
19125 *
19126 * Because of this I need to split live ranges,
19127 * even outside of the context of coalesced live
19128 * ranges. The need to split live ranges does
19129 * impose some constraints on live range coalescing.
19130 *
19131 * - Live ranges may not be coalesced across phi
19132 * functions. This creates a 2 headed live
19133 * range that cannot be sanely split.
19134 *
19135 * - phi functions (coalesced in initialize_live_ranges)
19136 * are handled as pre split live ranges so we will
19137 * never attempt to split them.
19138 */
19139 int coalesced;
19140 int i;
19141
19142 coalesced = 0;
19143 for(i = 0; i <= rstate->ranges; i++) {
19144 struct live_range *lr1;
19145 struct live_range_def *lrd1;
19146 lr1 = &rstate->lr[i];
19147 if (!lr1->defs) {
19148 continue;
19149 }
19150 lrd1 = live_range_end(state, lr1, 0);
19151 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19152 struct triple_set *set;
19153 if (lrd1->def->op != OP_COPY) {
19154 continue;
19155 }
19156 /* Skip copies that are the result of a live range split. */
19157 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19158 continue;
19159 }
19160 for(set = lrd1->def->use; set; set = set->next) {
19161 struct live_range_def *lrd2;
19162 struct live_range *lr2, *res;
19163
19164 lrd2 = &rstate->lrd[set->member->id];
19165
19166 /* Don't coalesce with instructions
19167 * that are the result of a live range
19168 * split.
19169 */
19170 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19171 continue;
19172 }
19173 lr2 = rstate->lrd[set->member->id].lr;
19174 if (lr1 == lr2) {
19175 continue;
19176 }
19177 if ((lr1->color != lr2->color) &&
19178 (lr1->color != REG_UNSET) &&
19179 (lr2->color != REG_UNSET)) {
19180 continue;
19181 }
19182 if ((lr1->classes & lr2->classes) == 0) {
19183 continue;
19184 }
19185
19186 if (interfere(rstate, lr1, lr2)) {
19187 continue;
19188 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019189
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019190 res = coalesce_ranges(state, rstate, lr1, lr2);
19191 coalesced += 1;
19192 if (res != lr1) {
19193 goto next;
19194 }
19195 }
19196 }
19197 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019198 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019199 }
19200 return coalesced;
19201}
19202
19203
Eric Biedermanf96a8102003-06-16 16:57:34 +000019204static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019205 struct reg_block *blocks, struct triple_reg_set *live,
19206 struct reg_block *rb, struct triple *ins, void *arg)
19207{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019208 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019209 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019210
19211 /* See if we have a mandatory coalesce operation between
19212 * a lhs and a rhs value. If so and the rhs value is also
19213 * alive then this triple needs to be pre copied. Otherwise
19214 * we would have two definitions in the same live range simultaneously
19215 * alive.
19216 */
Eric Biederman90089602004-05-28 14:11:54 +000019217 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019218 if ((zlhs == 0) && triple_is_def(state, ins)) {
19219 zlhs = 1;
19220 }
Eric Biederman90089602004-05-28 14:11:54 +000019221 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019222 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019223 struct reg_info linfo;
19224 linfo = arch_reg_lhs(state, ins, i);
19225 if (linfo.reg < MAX_REGISTERS) {
19226 continue;
19227 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019228 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019229 struct reg_info rinfo;
19230 struct triple *rhs;
19231 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019232 int found;
19233 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019234 rinfo = arch_reg_rhs(state, ins, j);
19235 if (rinfo.reg != linfo.reg) {
19236 continue;
19237 }
19238 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019239 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019240 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019241 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019242 }
19243 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019244 if (found) {
19245 struct triple *copy;
19246 copy = pre_copy(state, ins, j);
19247 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019248 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019249 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019250 }
19251 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019252 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019253}
19254
Eric Biedermand1ea5392003-06-28 06:49:45 +000019255static int correct_coalesce_conflicts(
19256 struct compile_state *state, struct reg_block *blocks)
19257{
19258 int conflicts;
19259 conflicts = 0;
Eric Biederman90089602004-05-28 14:11:54 +000019260 walk_variable_lifetimes(state, &state->bb, blocks,
19261 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019262 return conflicts;
19263}
19264
Eric Biedermanf96a8102003-06-16 16:57:34 +000019265static void replace_set_use(struct compile_state *state,
19266 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019267{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019268 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019269 for(set = head; set; set = set->next) {
19270 if (set->member == orig) {
19271 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019272 }
19273 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019274}
19275
Eric Biedermanf96a8102003-06-16 16:57:34 +000019276static void replace_block_use(struct compile_state *state,
19277 struct reg_block *blocks, struct triple *orig, struct triple *new)
19278{
19279 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019280#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019281#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019282#endif
Eric Biederman90089602004-05-28 14:11:54 +000019283 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019284 struct reg_block *rb;
19285 rb = &blocks[i];
19286 replace_set_use(state, rb->in, orig, new);
19287 replace_set_use(state, rb->out, orig, new);
19288 }
19289}
19290
19291static void color_instructions(struct compile_state *state)
19292{
19293 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019294 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019295 ins = first;
19296 do {
19297 if (triple_is_def(state, ins)) {
19298 struct reg_info info;
19299 info = find_lhs_color(state, ins, 0);
19300 if (info.reg >= MAX_REGISTERS) {
19301 info.reg = REG_UNSET;
19302 }
19303 SET_INFO(ins->id, info);
19304 }
19305 ins = ins->next;
19306 } while(ins != first);
19307}
19308
19309static struct reg_info read_lhs_color(
19310 struct compile_state *state, struct triple *ins, int index)
19311{
19312 struct reg_info info;
19313 if ((index == 0) && triple_is_def(state, ins)) {
19314 info.reg = ID_REG(ins->id);
19315 info.regcm = ID_REGCM(ins->id);
19316 }
Eric Biederman90089602004-05-28 14:11:54 +000019317 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019318 info = read_lhs_color(state, LHS(ins, index), 0);
19319 }
19320 else {
19321 internal_error(state, ins, "Bad lhs %d", index);
19322 info.reg = REG_UNSET;
19323 info.regcm = 0;
19324 }
19325 return info;
19326}
19327
19328static struct triple *resolve_tangle(
19329 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019330{
19331 struct reg_info info, uinfo;
19332 struct triple_set *set, *next;
19333 struct triple *copy;
19334
Stefan Reinauer50542a82007-10-24 11:14:14 +000019335#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019336#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019337#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019338 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019339 for(set = tangle->use; set; set = next) {
19340 struct triple *user;
19341 int i, zrhs;
19342 next = set->next;
19343 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019344 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019345 for(i = 0; i < zrhs; i++) {
19346 if (RHS(user, i) != tangle) {
19347 continue;
19348 }
19349 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019350 if (uinfo.reg == info.reg) {
19351 copy = pre_copy(state, user, i);
19352 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019353 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019354 }
19355 }
19356 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019357 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019358 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019359 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019360 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019361 copy = post_copy(state, tangle);
19362 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019363 linfo = find_lhs_color(state, copy, 0);
19364 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019365 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019366 info = find_lhs_color(state, tangle, 0);
19367 SET_INFO(tangle->id, info);
19368
19369 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019370}
19371
19372
Eric Biedermanf96a8102003-06-16 16:57:34 +000019373static void fix_tangles(struct compile_state *state,
19374 struct reg_block *blocks, struct triple_reg_set *live,
19375 struct reg_block *rb, struct triple *ins, void *arg)
19376{
Eric Biederman153ea352003-06-20 14:43:20 +000019377 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019378 struct triple *tangle;
19379 do {
19380 char used[MAX_REGISTERS];
19381 struct triple_reg_set *set;
19382 tangle = 0;
19383
19384 /* Find out which registers have multiple uses at this point */
19385 memset(used, 0, sizeof(used));
19386 for(set = live; set; set = set->next) {
19387 struct reg_info info;
19388 info = read_lhs_color(state, set->member, 0);
19389 if (info.reg == REG_UNSET) {
19390 continue;
19391 }
19392 reg_inc_used(state, used, info.reg);
19393 }
19394
19395 /* Now find the least dominated definition of a register in
19396 * conflict I have seen so far.
19397 */
19398 for(set = live; set; set = set->next) {
19399 struct reg_info info;
19400 info = read_lhs_color(state, set->member, 0);
19401 if (used[info.reg] < 2) {
19402 continue;
19403 }
Eric Biederman153ea352003-06-20 14:43:20 +000019404 /* Changing copies that feed into phi functions
19405 * is incorrect.
19406 */
19407 if (set->member->use &&
19408 (set->member->use->member->op == OP_PHI)) {
19409 continue;
19410 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019411 if (!tangle || tdominates(state, set->member, tangle)) {
19412 tangle = set->member;
19413 }
19414 }
19415 /* If I have found a tangle resolve it */
19416 if (tangle) {
19417 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019418 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019419 post_copy = resolve_tangle(state, tangle);
19420 if (post_copy) {
19421 replace_block_use(state, blocks, tangle, post_copy);
19422 }
19423 if (post_copy && (tangle != ins)) {
19424 replace_set_use(state, live, tangle, post_copy);
19425 }
19426 }
19427 } while(tangle);
19428 return;
19429}
19430
Eric Biederman153ea352003-06-20 14:43:20 +000019431static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019432 struct compile_state *state, struct reg_block *blocks)
19433{
Eric Biederman153ea352003-06-20 14:43:20 +000019434 int tangles;
19435 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019436 color_instructions(state);
Eric Biederman90089602004-05-28 14:11:54 +000019437 walk_variable_lifetimes(state, &state->bb, blocks,
19438 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019439 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019440}
19441
Eric Biedermand1ea5392003-06-28 06:49:45 +000019442
19443static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19444static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19445
19446struct triple *find_constrained_def(
19447 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019448{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019449 struct live_range_def *lrd, *lrd_next;
19450 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019451 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019452 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019453 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019454
19455 lrd = lrd_next;
19456 lrd_next = lrd->next;
19457
Eric Biedermand1ea5392003-06-28 06:49:45 +000019458 regcm = arch_type_to_regcm(state, lrd->def->type);
19459 info = find_lhs_color(state, lrd->def, 0);
19460 regcm = arch_regcm_reg_normalize(state, regcm);
19461 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019462 /* If the 2 register class masks are equal then
19463 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019464 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019465 if (regcm == info.regcm) {
19466 continue;
19467 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019468
Eric Biederman5ade04a2003-10-22 04:03:46 +000019469 /* If there is just one use.
19470 * That use cannot accept a larger register class.
19471 * There are no intervening definitions except
19472 * definitions that feed into that use.
19473 * Then a triple is not constrained.
19474 * FIXME handle this case!
19475 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019476#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019477#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019478#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019479
19480
Eric Biedermand1ea5392003-06-28 06:49:45 +000019481 /* Of the constrained live ranges deal with the
19482 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019483 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019484 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019485 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019486 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019487 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019488 if (!constrained ||
19489 tdominates(state, lrd->def, constrained))
19490 {
19491 constrained = lrd->def;
19492 }
19493 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019494 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019495}
19496
Eric Biedermand1ea5392003-06-28 06:49:45 +000019497static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019498 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019499 struct live_range *range)
19500{
19501 /* Walk through the edges in conflict and our current live
19502 * range, and find definitions that are more severly constrained
19503 * than they type of data they contain require.
19504 *
19505 * Then pick one of those ranges and relax the constraints.
19506 */
19507 struct live_range_edge *edge;
19508 struct triple *constrained;
19509
19510 constrained = 0;
19511 for(edge = range->edges; edge; edge = edge->next) {
19512 constrained = find_constrained_def(state, edge->node, constrained);
19513 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019514#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019515#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019516#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019517 if (!constrained) {
19518 constrained = find_constrained_def(state, range, constrained);
19519 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019520
19521 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019522 fprintf(state->errout, "constrained: ");
19523 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019524 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019525 if (constrained) {
19526 ids_from_rstate(state, rstate);
19527 cleanup_rstate(state, rstate);
19528 resolve_tangle(state, constrained);
19529 }
19530 return !!constrained;
19531}
19532
19533static int split_ranges(
19534 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019535 char *used, struct live_range *range)
19536{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019537 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019538 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019539 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019540 rstate->passes, tops(range->defs->def->op), range->defs->def);
19541 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019542 if ((range->color == REG_UNNEEDED) ||
19543 (rstate->passes >= rstate->max_passes)) {
19544 return 0;
19545 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019546 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019547
Eric Biedermand1ea5392003-06-28 06:49:45 +000019548 /* Ideally I would split the live range that will not be used
19549 * for the longest period of time in hopes that this will
19550 * (a) allow me to spill a register or
19551 * (b) allow me to place a value in another register.
19552 *
19553 * So far I don't have a test case for this, the resolving
19554 * of mandatory constraints has solved all of my
19555 * know issues. So I have choosen not to write any
19556 * code until I cat get a better feel for cases where
19557 * it would be useful to have.
19558 *
19559 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019560#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019561#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019562#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019563
19564 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019565 FILE *fp = state->errout;
19566 print_interference_blocks(state, rstate, fp, 0);
19567 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019568 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019569 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019570}
19571
Eric Biederman5ade04a2003-10-22 04:03:46 +000019572static FILE *cgdebug_fp(struct compile_state *state)
19573{
19574 FILE *fp;
19575 fp = 0;
19576 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019577 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019578 }
19579 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019580 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019581 }
19582 return fp;
19583}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019584
Eric Biederman5ade04a2003-10-22 04:03:46 +000019585static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19586{
19587 FILE *fp;
19588 fp = cgdebug_fp(state);
19589 if (fp) {
19590 va_list args;
19591 va_start(args, fmt);
19592 vfprintf(fp, fmt, args);
19593 va_end(args);
19594 }
19595}
19596
19597static void cgdebug_flush(struct compile_state *state)
19598{
19599 FILE *fp;
19600 fp = cgdebug_fp(state);
19601 if (fp) {
19602 fflush(fp);
19603 }
19604}
19605
19606static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19607{
19608 FILE *fp;
19609 fp = cgdebug_fp(state);
19610 if (fp) {
19611 loc(fp, state, ins);
19612 }
19613}
19614
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019615static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019616 struct reg_state *rstate, struct live_range *range)
19617{
19618 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019619 struct live_range_def *lrd;
19620 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019621 struct live_range_edge *edge;
19622 char used[MAX_REGISTERS];
19623 struct triple **expr;
19624
Eric Biedermanb138ac82003-04-22 18:44:01 +000019625 /* Instead of doing just the trivial color select here I try
19626 * a few extra things because a good color selection will help reduce
19627 * copies.
19628 */
19629
19630 /* Find the registers currently in use */
19631 memset(used, 0, sizeof(used));
19632 for(edge = range->edges; edge; edge = edge->next) {
19633 if (edge->node->color == REG_UNSET) {
19634 continue;
19635 }
19636 reg_fill_used(state, used, edge->node->color);
19637 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019638
19639 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019640 int i;
19641 i = 0;
19642 for(edge = range->edges; edge; edge = edge->next) {
19643 i++;
19644 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019645 cgdebug_printf(state, "\n%s edges: %d",
19646 tops(range->defs->def->op), i);
19647 cgdebug_loc(state, range->defs->def);
19648 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019649 for(i = 0; i < MAX_REGISTERS; i++) {
19650 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019651 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019652 arch_reg_str(i));
19653 }
19654 }
19655 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019656
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019657 /* If a color is already assigned see if it will work */
19658 if (range->color != REG_UNSET) {
19659 struct live_range_def *lrd;
19660 if (!used[range->color]) {
19661 return 1;
19662 }
19663 for(edge = range->edges; edge; edge = edge->next) {
19664 if (edge->node->color != range->color) {
19665 continue;
19666 }
19667 warning(state, edge->node->defs->def, "edge: ");
19668 lrd = edge->node->defs;
19669 do {
19670 warning(state, lrd->def, " %p %s",
19671 lrd->def, tops(lrd->def->op));
19672 lrd = lrd->next;
19673 } while(lrd != edge->node->defs);
19674 }
19675 lrd = range->defs;
19676 warning(state, range->defs->def, "def: ");
19677 do {
19678 warning(state, lrd->def, " %p %s",
19679 lrd->def, tops(lrd->def->op));
19680 lrd = lrd->next;
19681 } while(lrd != range->defs);
19682 internal_error(state, range->defs->def,
19683 "live range with already used color %s",
19684 arch_reg_str(range->color));
19685 }
19686
Eric Biedermanb138ac82003-04-22 18:44:01 +000019687 /* If I feed into an expression reuse it's color.
19688 * This should help remove copies in the case of 2 register instructions
19689 * and phi functions.
19690 */
19691 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019692 lrd = live_range_end(state, range, 0);
19693 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19694 entry = lrd->def->use;
19695 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19696 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019697 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019698 insd = &rstate->lrd[entry->member->id];
19699 if (insd->lr->defs == 0) {
19700 continue;
19701 }
19702 if (!phi && (insd->def->op == OP_PHI) &&
19703 !interfere(rstate, range, insd->lr)) {
19704 phi = insd;
19705 }
Eric Biederman530b5192003-07-01 10:05:30 +000019706 if (insd->lr->color == REG_UNSET) {
19707 continue;
19708 }
19709 regcm = insd->lr->classes;
19710 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019711 (used[insd->lr->color])) {
19712 continue;
19713 }
19714 if (interfere(rstate, range, insd->lr)) {
19715 continue;
19716 }
19717 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019718 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019719 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019720 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019721 * of something else that feeds into the phi function.
19722 */
19723 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019724 if (phi->lr->color != REG_UNSET) {
19725 if (used[phi->lr->color]) {
19726 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019727 }
19728 }
19729 else {
19730 expr = triple_rhs(state, phi->def, 0);
19731 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19732 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019733 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019734 if (!*expr) {
19735 continue;
19736 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019737 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019738 if (lr->color == REG_UNSET) {
19739 continue;
19740 }
19741 regcm = lr->classes;
19742 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019743 (used[lr->color])) {
19744 continue;
19745 }
19746 if (interfere(rstate, range, lr)) {
19747 continue;
19748 }
19749 range->color = lr->color;
19750 }
19751 }
19752 }
19753 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019754 lrd = live_range_head(state, range, 0);
19755 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19756 expr = triple_rhs(state, lrd->def, 0);
19757 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019758 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019759 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019760 if (!*expr) {
19761 continue;
19762 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019763 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019764 if (lr->color == REG_UNSET) {
19765 continue;
19766 }
19767 regcm = lr->classes;
19768 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019769 (used[lr->color])) {
19770 continue;
19771 }
19772 if (interfere(rstate, range, lr)) {
19773 continue;
19774 }
19775 range->color = lr->color;
19776 break;
19777 }
19778 }
19779 /* If I have not opportunitically picked a useful color
19780 * pick the first color that is free.
19781 */
19782 if (range->color == REG_UNSET) {
19783 range->color =
19784 arch_select_free_register(state, used, range->classes);
19785 }
19786 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019787 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019788 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019789 if (split_ranges(state, rstate, used, range)) {
19790 return 0;
19791 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019792 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019793 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019794 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019795 lrd = edge->node->defs;
19796 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019797 warning(state, lrd->def, " %s %p",
19798 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019799 lrd = lrd->next;
19800 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019801 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019802 warning(state, range->defs->def, "range: ");
19803 lrd = range->defs;
19804 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019805 warning(state, lrd->def, " %s %p",
19806 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019807 lrd = lrd->next;
19808 } while(lrd != range->defs);
19809
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019810 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019811 range->classes);
19812 for(i = 0; i < MAX_REGISTERS; i++) {
19813 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019814 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019815 arch_reg_str(i));
19816 }
19817 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019818 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019819 }
Eric Biederman530b5192003-07-01 10:05:30 +000019820 range->classes &= arch_reg_regcm(state, range->color);
19821 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019822 internal_error(state, range->defs->def, "select_free_color did not?");
19823 }
19824 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019825}
19826
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019827static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019828{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019829 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019830 struct live_range_edge *edge;
19831 struct live_range *range;
19832 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019833 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019834 range = rstate->low;
19835 if (*range->group_prev != range) {
19836 internal_error(state, 0, "lo: *prev != range?");
19837 }
19838 *range->group_prev = range->group_next;
19839 if (range->group_next) {
19840 range->group_next->group_prev = range->group_prev;
19841 }
19842 if (&range->group_next == rstate->low_tail) {
19843 rstate->low_tail = range->group_prev;
19844 }
19845 if (rstate->low == range) {
19846 internal_error(state, 0, "low: next != prev?");
19847 }
19848 }
19849 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019850 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019851 range = rstate->high;
19852 if (*range->group_prev != range) {
19853 internal_error(state, 0, "hi: *prev != range?");
19854 }
19855 *range->group_prev = range->group_next;
19856 if (range->group_next) {
19857 range->group_next->group_prev = range->group_prev;
19858 }
19859 if (&range->group_next == rstate->high_tail) {
19860 rstate->high_tail = range->group_prev;
19861 }
19862 if (rstate->high == range) {
19863 internal_error(state, 0, "high: next != prev?");
19864 }
19865 }
19866 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019867 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019868 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019869 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019870 range->group_prev = 0;
19871 for(edge = range->edges; edge; edge = edge->next) {
19872 struct live_range *node;
19873 node = edge->node;
19874 /* Move nodes from the high to the low list */
19875 if (node->group_prev && (node->color == REG_UNSET) &&
19876 (node->degree == regc_max_size(state, node->classes))) {
19877 if (*node->group_prev != node) {
19878 internal_error(state, 0, "move: *prev != node?");
19879 }
19880 *node->group_prev = node->group_next;
19881 if (node->group_next) {
19882 node->group_next->group_prev = node->group_prev;
19883 }
19884 if (&node->group_next == rstate->high_tail) {
19885 rstate->high_tail = node->group_prev;
19886 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019887 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019888 node->group_prev = rstate->low_tail;
19889 node->group_next = 0;
19890 *rstate->low_tail = node;
19891 rstate->low_tail = &node->group_next;
19892 if (*node->group_prev != node) {
19893 internal_error(state, 0, "move2: *prev != node?");
19894 }
19895 }
19896 node->degree -= 1;
19897 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019898 colored = color_graph(state, rstate);
19899 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019900 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019901 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019902 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019903 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019904 if (colored) {
19905 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19906 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019907 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019908 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019909}
19910
Eric Biedermana96d6a92003-05-13 20:45:19 +000019911static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19912{
19913 struct live_range *lr;
19914 struct live_range_edge *edge;
19915 struct triple *ins, *first;
19916 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019917 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019918 ins = first;
19919 do {
19920 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019921 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019922 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019923 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019924 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019925 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019926 if (lr->color == REG_UNSET) {
19927 internal_error(state, ins,
19928 "triple without a color");
19929 }
19930 /* Find the registers used by the edges */
19931 memset(used, 0, sizeof(used));
19932 for(edge = lr->edges; edge; edge = edge->next) {
19933 if (edge->node->color == REG_UNSET) {
19934 internal_error(state, 0,
19935 "live range without a color");
19936 }
19937 reg_fill_used(state, used, edge->node->color);
19938 }
19939 if (used[lr->color]) {
19940 internal_error(state, ins,
19941 "triple with already used color");
19942 }
19943 }
19944 ins = ins->next;
19945 } while(ins != first);
19946}
19947
Eric Biedermanb138ac82003-04-22 18:44:01 +000019948static void color_triples(struct compile_state *state, struct reg_state *rstate)
19949{
Eric Biederman90089602004-05-28 14:11:54 +000019950 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019951 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019952 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019953 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019954 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019955 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019956 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019957 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019958 "triple without a live range");
19959 }
Eric Biederman90089602004-05-28 14:11:54 +000019960 lrd = &rstate->lrd[ins->id];
19961 lr = lrd->lr;
19962 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019963 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019964 ins = ins->next;
19965 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019966}
19967
Eric Biedermanb138ac82003-04-22 18:44:01 +000019968static struct live_range *merge_sort_lr(
19969 struct live_range *first, struct live_range *last)
19970{
19971 struct live_range *mid, *join, **join_tail, *pick;
19972 size_t size;
19973 size = (last - first) + 1;
19974 if (size >= 2) {
19975 mid = first + size/2;
19976 first = merge_sort_lr(first, mid -1);
19977 mid = merge_sort_lr(mid, last);
19978
19979 join = 0;
19980 join_tail = &join;
19981 /* merge the two lists */
19982 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019983 if ((first->degree < mid->degree) ||
19984 ((first->degree == mid->degree) &&
19985 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019986 pick = first;
19987 first = first->group_next;
19988 if (first) {
19989 first->group_prev = 0;
19990 }
19991 }
19992 else {
19993 pick = mid;
19994 mid = mid->group_next;
19995 if (mid) {
19996 mid->group_prev = 0;
19997 }
19998 }
19999 pick->group_next = 0;
20000 pick->group_prev = join_tail;
20001 *join_tail = pick;
20002 join_tail = &pick->group_next;
20003 }
20004 /* Splice the remaining list */
20005 pick = (first)? first : mid;
20006 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020007 if (pick) {
20008 pick->group_prev = join_tail;
20009 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020010 }
20011 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020012 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020013 first = 0;
20014 }
20015 join = first;
20016 }
20017 return join;
20018}
20019
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020020static void ids_from_rstate(struct compile_state *state,
20021 struct reg_state *rstate)
20022{
20023 struct triple *ins, *first;
20024 if (!rstate->defs) {
20025 return;
20026 }
20027 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020028 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020029 FILE *fp = state->dbgout;
20030 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020031 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020032 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020033 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020034 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020035 ins = first;
20036 do {
20037 if (ins->id) {
20038 struct live_range_def *lrd;
20039 lrd = &rstate->lrd[ins->id];
20040 ins->id = lrd->orig_id;
20041 }
20042 ins = ins->next;
20043 } while(ins != first);
20044}
20045
20046static void cleanup_live_edges(struct reg_state *rstate)
20047{
20048 int i;
20049 /* Free the edges on each node */
20050 for(i = 1; i <= rstate->ranges; i++) {
20051 remove_live_edges(rstate, &rstate->lr[i]);
20052 }
20053}
20054
20055static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20056{
20057 cleanup_live_edges(rstate);
20058 xfree(rstate->lrd);
20059 xfree(rstate->lr);
20060
20061 /* Free the variable lifetime information */
20062 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020063 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020064 }
20065 rstate->defs = 0;
20066 rstate->ranges = 0;
20067 rstate->lrd = 0;
20068 rstate->lr = 0;
20069 rstate->blocks = 0;
20070}
20071
Eric Biederman153ea352003-06-20 14:43:20 +000020072static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020073static void allocate_registers(struct compile_state *state)
20074{
20075 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020076 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020077
20078 /* Clear out the reg_state */
20079 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020080 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020081
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020082 do {
20083 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000020084 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000020085 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020086 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020087
Eric Biederman5ade04a2003-10-22 04:03:46 +000020088 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020089 FILE *fp = state->errout;
20090 fprintf(fp, "pass: %d\n", rstate.passes);
20091 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020092 }
Eric Biederman153ea352003-06-20 14:43:20 +000020093
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020094 /* Restore ids */
20095 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020096
Eric Biedermanf96a8102003-06-16 16:57:34 +000020097 /* Cleanup the temporary data structures */
20098 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020099
Eric Biedermanf96a8102003-06-16 16:57:34 +000020100 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020101 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020102
Eric Biedermanf96a8102003-06-16 16:57:34 +000020103 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000020104 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020105
Eric Biederman153ea352003-06-20 14:43:20 +000020106 /* Fix two simultaneous uses of the same register.
20107 * In a few pathlogical cases a partial untangle moves
20108 * the tangle to a part of the graph we won't revisit.
20109 * So we keep looping until we have no more tangle fixes
20110 * to apply.
20111 */
20112 do {
20113 tangles = correct_tangles(state, rstate.blocks);
20114 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020115
Eric Biederman5ade04a2003-10-22 04:03:46 +000020116
Eric Biederman90089602004-05-28 14:11:54 +000020117 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020118 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020119
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020120 /* Allocate and initialize the live ranges */
20121 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020122
Eric Biederman90089602004-05-28 14:11:54 +000020123 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020124 * buys me nothing. The code is left this way in case
20125 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020126 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020127 */
20128 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020129 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020130 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020131 }
20132
Eric Biederman153ea352003-06-20 14:43:20 +000020133 /* Remove any previous live edge calculations */
20134 cleanup_live_edges(&rstate);
20135
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020136 /* Compute the interference graph */
20137 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020138 state, &state->bb, rstate.blocks,
20139 graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020140
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020141 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020142 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020143 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020144 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020145 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020146 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020147 print_interference_ins, &rstate);
20148 }
20149
20150 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020151
Eric Biederman5ade04a2003-10-22 04:03:46 +000020152 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020153 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020154 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020155 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020156
Eric Biederman83b991a2003-10-11 06:20:25 +000020157#if DEBUG_CONSISTENCY > 1
20158# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020159 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020160# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020161 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020162 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020163 state, &state->bb, rstate.blocks,
20164 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020165# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020166 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020167#endif
20168#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020169
20170 /* Build the groups low and high. But with the nodes
20171 * first sorted by degree order.
20172 */
20173 rstate.low_tail = &rstate.low;
20174 rstate.high_tail = &rstate.high;
20175 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20176 if (rstate.high) {
20177 rstate.high->group_prev = &rstate.high;
20178 }
20179 for(point = &rstate.high; *point; point = &(*point)->group_next)
20180 ;
20181 rstate.high_tail = point;
20182 /* Walk through the high list and move everything that needs
20183 * to be onto low.
20184 */
20185 for(point = &rstate.high; *point; point = next) {
20186 struct live_range *range;
20187 next = &(*point)->group_next;
20188 range = *point;
20189
20190 /* If it has a low degree or it already has a color
20191 * place the node in low.
20192 */
20193 if ((range->degree < regc_max_size(state, range->classes)) ||
20194 (range->color != REG_UNSET)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020195 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020196 range - rstate.lr, range->degree,
20197 (range->color != REG_UNSET) ? " (colored)": "");
20198 *range->group_prev = range->group_next;
20199 if (range->group_next) {
20200 range->group_next->group_prev = range->group_prev;
20201 }
20202 if (&range->group_next == rstate.high_tail) {
20203 rstate.high_tail = range->group_prev;
20204 }
20205 range->group_prev = rstate.low_tail;
20206 range->group_next = 0;
20207 *rstate.low_tail = range;
20208 rstate.low_tail = &range->group_next;
20209 next = point;
20210 }
20211 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020212 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020213 range - rstate.lr, range->degree,
20214 (range->color != REG_UNSET) ? " (colored)": "");
20215 }
20216 }
20217 /* Color the live_ranges */
20218 colored = color_graph(state, &rstate);
20219 rstate.passes++;
20220 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020221
Eric Biedermana96d6a92003-05-13 20:45:19 +000020222 /* Verify the graph was properly colored */
20223 verify_colors(state, &rstate);
20224
Eric Biedermanb138ac82003-04-22 18:44:01 +000020225 /* Move the colors from the graph to the triples */
20226 color_triples(state, &rstate);
20227
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020228 /* Cleanup the temporary data structures */
20229 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020230
20231 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020232 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020233}
20234
20235/* Sparce Conditional Constant Propogation
20236 * =========================================
20237 */
20238struct ssa_edge;
20239struct flow_block;
20240struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020241 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020242 struct triple *def;
20243 struct ssa_edge *out;
20244 struct flow_block *fblock;
20245 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020246 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020247 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020248 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020249 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020250};
20251struct ssa_edge {
20252 struct lattice_node *src;
20253 struct lattice_node *dst;
20254 struct ssa_edge *work_next;
20255 struct ssa_edge *work_prev;
20256 struct ssa_edge *out_next;
20257};
20258struct flow_edge {
20259 struct flow_block *src;
20260 struct flow_block *dst;
20261 struct flow_edge *work_next;
20262 struct flow_edge *work_prev;
20263 struct flow_edge *in_next;
20264 struct flow_edge *out_next;
20265 int executable;
20266};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020267#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020268struct flow_block {
20269 struct block *block;
20270 struct flow_edge *in;
20271 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020272 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020273};
20274
20275struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020276 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020277 struct lattice_node *lattice;
20278 struct ssa_edge *ssa_edges;
20279 struct flow_block *flow_blocks;
20280 struct flow_edge *flow_work_list;
20281 struct ssa_edge *ssa_work_list;
20282};
20283
20284
Eric Biederman90089602004-05-28 14:11:54 +000020285static int is_scc_const(struct compile_state *state, struct triple *ins)
20286{
20287 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20288}
20289
20290static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20291{
20292 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20293}
20294
20295static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20296{
20297 return is_scc_const(state, lnode->val);
20298}
20299
20300static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20301{
20302 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20303}
20304
Eric Biedermanb138ac82003-04-22 18:44:01 +000020305static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
20306 struct flow_edge *fedge)
20307{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020308 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020309 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020310 fedge,
20311 fedge->src->block?fedge->src->block->last->id: 0,
20312 fedge->dst->block?fedge->dst->block->first->id: 0);
20313 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020314 if ((fedge == scc->flow_work_list) ||
20315 (fedge->work_next != fedge) ||
20316 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020317
20318 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020319 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020320 fedge);
20321 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020322 return;
20323 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020324 if (!scc->flow_work_list) {
20325 scc->flow_work_list = fedge;
20326 fedge->work_next = fedge->work_prev = fedge;
20327 }
20328 else {
20329 struct flow_edge *ftail;
20330 ftail = scc->flow_work_list->work_prev;
20331 fedge->work_next = ftail->work_next;
20332 fedge->work_prev = ftail;
20333 fedge->work_next->work_prev = fedge;
20334 fedge->work_prev->work_next = fedge;
20335 }
20336}
20337
20338static struct flow_edge *scc_next_fedge(
20339 struct compile_state *state, struct scc_state *scc)
20340{
20341 struct flow_edge *fedge;
20342 fedge = scc->flow_work_list;
20343 if (fedge) {
20344 fedge->work_next->work_prev = fedge->work_prev;
20345 fedge->work_prev->work_next = fedge->work_next;
20346 if (fedge->work_next != fedge) {
20347 scc->flow_work_list = fedge->work_next;
20348 } else {
20349 scc->flow_work_list = 0;
20350 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020351 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020352 }
20353 return fedge;
20354}
20355
20356static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20357 struct ssa_edge *sedge)
20358{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020359 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020360 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20361 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020362 sedge->src->def->id,
20363 sedge->dst->def->id);
20364 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020365 if ((sedge == scc->ssa_work_list) ||
20366 (sedge->work_next != sedge) ||
20367 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020368
20369 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020370 fprintf(state->errout, "dupped sedge: %5ld\n",
20371 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020372 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020373 return;
20374 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020375 if (!scc->ssa_work_list) {
20376 scc->ssa_work_list = sedge;
20377 sedge->work_next = sedge->work_prev = sedge;
20378 }
20379 else {
20380 struct ssa_edge *stail;
20381 stail = scc->ssa_work_list->work_prev;
20382 sedge->work_next = stail->work_next;
20383 sedge->work_prev = stail;
20384 sedge->work_next->work_prev = sedge;
20385 sedge->work_prev->work_next = sedge;
20386 }
20387}
20388
20389static struct ssa_edge *scc_next_sedge(
20390 struct compile_state *state, struct scc_state *scc)
20391{
20392 struct ssa_edge *sedge;
20393 sedge = scc->ssa_work_list;
20394 if (sedge) {
20395 sedge->work_next->work_prev = sedge->work_prev;
20396 sedge->work_prev->work_next = sedge->work_next;
20397 if (sedge->work_next != sedge) {
20398 scc->ssa_work_list = sedge->work_next;
20399 } else {
20400 scc->ssa_work_list = 0;
20401 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020402 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020403 }
20404 return sedge;
20405}
20406
20407static void initialize_scc_state(
20408 struct compile_state *state, struct scc_state *scc)
20409{
20410 int ins_count, ssa_edge_count;
20411 int ins_index, ssa_edge_index, fblock_index;
20412 struct triple *first, *ins;
20413 struct block *block;
20414 struct flow_block *fblock;
20415
20416 memset(scc, 0, sizeof(*scc));
20417
20418 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020419 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020420 ins = first;
20421 ins_count = ssa_edge_count = 0;
20422 do {
20423 struct triple_set *edge;
20424 ins_count += 1;
20425 for(edge = ins->use; edge; edge = edge->next) {
20426 ssa_edge_count++;
20427 }
20428 ins = ins->next;
20429 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020430 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020431 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20432 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020433 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020434 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020435 scc->lattice =
20436 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
20437 scc->ssa_edges =
20438 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
20439 scc->flow_blocks =
Eric Biederman90089602004-05-28 14:11:54 +000020440 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020441 "flow_blocks");
20442
20443 /* Initialize pass one collect up the nodes */
20444 fblock = 0;
20445 block = 0;
20446 ins_index = ssa_edge_index = fblock_index = 0;
20447 ins = first;
20448 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020449 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20450 block = ins->u.block;
20451 if (!block) {
20452 internal_error(state, ins, "label without block");
20453 }
20454 fblock_index += 1;
20455 block->vertex = fblock_index;
20456 fblock = &scc->flow_blocks[fblock_index];
20457 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020458 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20459 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020460 }
20461 {
20462 struct lattice_node *lnode;
20463 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020464 lnode = &scc->lattice[ins_index];
20465 lnode->def = ins;
20466 lnode->out = 0;
20467 lnode->fblock = fblock;
20468 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020469 if (lnode->val->op == OP_UNKNOWNVAL) {
20470 lnode->val = 0; /* LATTICE LOW by definition */
20471 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020472 lnode->old_id = ins->id;
20473 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020474 }
20475 ins = ins->next;
20476 } while(ins != first);
20477 /* Initialize pass two collect up the edges */
20478 block = 0;
20479 fblock = 0;
20480 ins = first;
20481 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020482 {
20483 struct triple_set *edge;
20484 struct ssa_edge **stail;
20485 struct lattice_node *lnode;
20486 lnode = &scc->lattice[ins->id];
20487 lnode->out = 0;
20488 stail = &lnode->out;
20489 for(edge = ins->use; edge; edge = edge->next) {
20490 struct ssa_edge *sedge;
20491 ssa_edge_index += 1;
20492 sedge = &scc->ssa_edges[ssa_edge_index];
20493 *stail = sedge;
20494 stail = &sedge->out_next;
20495 sedge->src = lnode;
20496 sedge->dst = &scc->lattice[edge->member->id];
20497 sedge->work_next = sedge->work_prev = sedge;
20498 sedge->out_next = 0;
20499 }
20500 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020501 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20502 struct flow_edge *fedge, **ftail;
20503 struct block_set *bedge;
20504 block = ins->u.block;
20505 fblock = &scc->flow_blocks[block->vertex];
20506 fblock->in = 0;
20507 fblock->out = 0;
20508 ftail = &fblock->out;
20509
20510 fedge = fblock->edges;
20511 bedge = block->edges;
20512 for(; bedge; bedge = bedge->next, fedge++) {
20513 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20514 if (fedge->dst->block != bedge->member) {
20515 internal_error(state, 0, "block mismatch");
20516 }
20517 *ftail = fedge;
20518 ftail = &fedge->out_next;
20519 fedge->out_next = 0;
20520 }
20521 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20522 fedge->src = fblock;
20523 fedge->work_next = fedge->work_prev = fedge;
20524 fedge->executable = 0;
20525 }
20526 }
20527 ins = ins->next;
20528 } while (ins != first);
20529 block = 0;
20530 fblock = 0;
20531 ins = first;
20532 do {
20533 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20534 struct flow_edge **ftail;
20535 struct block_set *bedge;
20536 block = ins->u.block;
20537 fblock = &scc->flow_blocks[block->vertex];
20538 ftail = &fblock->in;
20539 for(bedge = block->use; bedge; bedge = bedge->next) {
20540 struct block *src_block;
20541 struct flow_block *sfblock;
20542 struct flow_edge *sfedge;
20543 src_block = bedge->member;
20544 sfblock = &scc->flow_blocks[src_block->vertex];
20545 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20546 if (sfedge->dst == fblock) {
20547 break;
20548 }
20549 }
20550 if (!sfedge) {
20551 internal_error(state, 0, "edge mismatch");
20552 }
20553 *ftail = sfedge;
20554 ftail = &sfedge->in_next;
20555 sfedge->in_next = 0;
20556 }
20557 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020558 ins = ins->next;
20559 } while(ins != first);
20560 /* Setup a dummy block 0 as a node above the start node */
20561 {
20562 struct flow_block *fblock, *dst;
20563 struct flow_edge *fedge;
20564 fblock = &scc->flow_blocks[0];
20565 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020566 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020567 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020568 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020569 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020570 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020571 fedge->src = fblock;
20572 fedge->dst = dst;
20573 fedge->work_next = fedge;
20574 fedge->work_prev = fedge;
20575 fedge->in_next = fedge->dst->in;
20576 fedge->out_next = 0;
20577 fedge->executable = 0;
20578 fedge->dst->in = fedge;
20579
20580 /* Initialize the work lists */
20581 scc->flow_work_list = 0;
20582 scc->ssa_work_list = 0;
20583 scc_add_fedge(state, scc, fedge);
20584 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020585 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020586 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020587 ins_index, ssa_edge_index, fblock_index);
20588 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020589}
20590
20591
20592static void free_scc_state(
20593 struct compile_state *state, struct scc_state *scc)
20594{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020595 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020596 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020597 struct flow_block *fblock;
20598 fblock = &scc->flow_blocks[i];
20599 if (fblock->edges) {
20600 xfree(fblock->edges);
20601 fblock->edges = 0;
20602 }
20603 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020604 xfree(scc->flow_blocks);
20605 xfree(scc->ssa_edges);
20606 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020607
Eric Biedermanb138ac82003-04-22 18:44:01 +000020608}
20609
20610static struct lattice_node *triple_to_lattice(
20611 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20612{
20613 if (ins->id <= 0) {
20614 internal_error(state, ins, "bad id");
20615 }
20616 return &scc->lattice[ins->id];
20617}
20618
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020619static struct triple *preserve_lval(
20620 struct compile_state *state, struct lattice_node *lnode)
20621{
20622 struct triple *old;
20623 /* Preserve the original value */
20624 if (lnode->val) {
20625 old = dup_triple(state, lnode->val);
20626 if (lnode->val != lnode->def) {
20627 xfree(lnode->val);
20628 }
20629 lnode->val = 0;
20630 } else {
20631 old = 0;
20632 }
20633 return old;
20634}
20635
20636static int lval_changed(struct compile_state *state,
20637 struct triple *old, struct lattice_node *lnode)
20638{
20639 int changed;
20640 /* See if the lattice value has changed */
20641 changed = 1;
20642 if (!old && !lnode->val) {
20643 changed = 0;
20644 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020645 if (changed &&
20646 lnode->val && old &&
20647 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020648 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020649 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20650 changed = 0;
20651 }
20652 if (old) {
20653 xfree(old);
20654 }
20655 return changed;
20656
20657}
20658
Eric Biederman5ade04a2003-10-22 04:03:46 +000020659static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020660 struct compile_state *state, struct scc_state *scc,
20661 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020662{
Eric Biederman90089602004-05-28 14:11:54 +000020663 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20664 display_triple_changes(state->errout, lnode->val, lnode->def);
20665 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020666 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020667 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020668 struct triple *val, **expr;
20669 val = lnode->val? lnode->val : lnode->def;
20670 fprintf(fp, "%p %s %3d %10s (",
20671 lnode->def,
20672 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20673 lnode->def->id,
20674 tops(lnode->def->op));
20675 expr = triple_rhs(state, lnode->def, 0);
20676 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20677 if (*expr) {
20678 fprintf(fp, " %d", (*expr)->id);
20679 }
20680 }
20681 if (val->op == OP_INTCONST) {
20682 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20683 }
20684 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020685 (is_lattice_hi(state, lnode)? "hi":
20686 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020687 changed? "changed" : ""
20688 );
20689 }
20690}
20691
Eric Biedermanb138ac82003-04-22 18:44:01 +000020692static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20693 struct lattice_node *lnode)
20694{
20695 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020696 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020697 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020698 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020699
20700 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020701 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020702
Eric Biedermanb138ac82003-04-22 18:44:01 +000020703 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020704 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020705 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020706 scratch->next = scratch;
20707 scratch->prev = scratch;
20708 scratch->use = 0;
20709
Eric Biederman90089602004-05-28 14:11:54 +000020710 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020711 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020712 dexpr = &lnode->def->param[i];
20713 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020714 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020715 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20716 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020717 *dexpr) {
20718 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020719 tmp = triple_to_lattice(state, scc, *dexpr);
20720 *vexpr = (tmp->val)? tmp->val : tmp->def;
20721 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020722 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020723 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020724 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020725 }
20726 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020727#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020728#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020729#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020730 /* So far it looks like only the strength reduction
20731 * optimization are things I need to worry about.
20732 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020733 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020734 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020735 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020736 internal_error(state, lnode->def, "scratch used?");
20737 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020738 if ((scratch->prev != scratch) ||
20739 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020740 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020741 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020742 internal_error(state, lnode->def, "scratch in list?");
20743 }
20744 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020745 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020746 for(i = 0; i < count; i++) {
20747 vexpr = &scratch->param[i];
20748 if (*vexpr) {
20749 unuse_triple(*vexpr, scratch);
20750 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020751 }
Eric Biederman90089602004-05-28 14:11:54 +000020752 if (lnode->val->op == OP_UNKNOWNVAL) {
20753 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020754 }
Eric Biederman90089602004-05-28 14:11:54 +000020755 /* Find the case when I am lattice high */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020756 if (lnode->val &&
20757 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020758 (memcmp(lnode->val->param, lnode->def->param,
20759 count * sizeof(lnode->val->param[0])) == 0) &&
20760 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020761 lnode->val = lnode->def;
20762 }
Eric Biederman90089602004-05-28 14:11:54 +000020763 /* Only allow lattice high when all of my inputs
20764 * are also lattice high. Occassionally I can
20765 * have constants with a lattice low input, so
20766 * I do not need to check that case.
20767 */
20768 if (is_lattice_hi(state, lnode)) {
20769 struct lattice_node *tmp;
20770 int rhs;
20771 rhs = lnode->val->rhs;
20772 for(i = 0; i < rhs; i++) {
20773 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20774 if (!is_lattice_hi(state, tmp)) {
20775 lnode->val = 0;
20776 break;
20777 }
20778 }
20779 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020780 /* Find the cases that are always lattice lo */
20781 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020782 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020783 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020784 lnode->val = 0;
20785 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020786 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020787 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020788 /* See if this value should not change */
Eric Biederman90089602004-05-28 14:11:54 +000020789 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020790 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020791 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020792 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020793#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020794#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020795#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020796 if (changed) {
20797 internal_warning(state, lnode->def, "non def changes value?");
20798 }
20799 lnode->val = 0;
20800 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020801
Eric Biederman83b991a2003-10-11 06:20:25 +000020802 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020803 if (lnode->val != scratch) {
20804 xfree(scratch);
20805 }
Eric Biederman90089602004-05-28 14:11:54 +000020806
Eric Biedermanb138ac82003-04-22 18:44:01 +000020807 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020808}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020809
Eric Biederman90089602004-05-28 14:11:54 +000020810
20811static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020812 struct lattice_node *lnode)
20813{
20814 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020815 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020816 int changed;
20817
20818 /* Update the branch value */
20819 changed = compute_lnode_val(state, scc, lnode);
20820 scc_debug_lnode(state, scc, lnode, changed);
20821
20822 /* This only applies to conditional branches */
20823 if (!triple_is_cbranch(state, lnode->def)) {
20824 internal_error(state, lnode->def, "not a conditional branch");
20825 }
20826
Eric Biederman5ade04a2003-10-22 04:03:46 +000020827 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020828 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020829 FILE *fp = state->errout;
20830 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020831 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020832 lnode->def->id);
20833
20834 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020835 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020836 }
Eric Biederman90089602004-05-28 14:11:54 +000020837 fprintf(fp, " )");
20838 if (lnode->def->rhs > 0) {
20839 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020840 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020841 }
Eric Biederman90089602004-05-28 14:11:54 +000020842 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020843 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020844 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020845 for(left = cond->fblock->out; left; left = left->out_next) {
20846 if (left->dst->block->first == lnode->def->next) {
20847 break;
20848 }
20849 }
20850 if (!left) {
20851 internal_error(state, lnode->def, "Cannot find left branch edge");
20852 }
20853 for(right = cond->fblock->out; right; right = right->out_next) {
20854 if (right->dst->block->first == TARG(lnode->def, 0)) {
20855 break;
20856 }
20857 }
20858 if (!right) {
20859 internal_error(state, lnode->def, "Cannot find right branch edge");
20860 }
Eric Biederman90089602004-05-28 14:11:54 +000020861 /* I should only come here if the controlling expressions value
20862 * has changed, which means it must be either a constant or lo.
20863 */
20864 if (is_lattice_hi(state, cond)) {
20865 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020866 return;
20867 }
Eric Biederman90089602004-05-28 14:11:54 +000020868 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020869 scc_add_fedge(state, scc, left);
20870 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020871 }
20872 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020873 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020874 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020875 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020876 }
20877
20878}
20879
Eric Biederman90089602004-05-28 14:11:54 +000020880
20881static void scc_add_sedge_dst(struct compile_state *state,
20882 struct scc_state *scc, struct ssa_edge *sedge)
20883{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020884 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020885 scc_visit_cbranch(state, scc, sedge->dst);
20886 }
20887 else if (triple_is_def(state, sedge->dst->def)) {
20888 scc_add_sedge(state, scc, sedge);
20889 }
20890}
20891
20892static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
20893 struct lattice_node *lnode)
20894{
20895 struct lattice_node *tmp;
20896 struct triple **slot, *old;
20897 struct flow_edge *fedge;
20898 int changed;
20899 int index;
20900 if (lnode->def->op != OP_PHI) {
20901 internal_error(state, lnode->def, "not phi");
20902 }
20903 /* Store the original value */
20904 old = preserve_lval(state, lnode);
20905
20906 /* default to lattice high */
20907 lnode->val = lnode->def;
20908 slot = &RHS(lnode->def, 0);
20909 index = 0;
20910 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20911 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20912 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
20913 index,
20914 fedge->dst->block->vertex,
20915 fedge->executable
20916 );
20917 }
20918 if (!fedge->executable) {
20919 continue;
20920 }
20921 if (!slot[index]) {
20922 internal_error(state, lnode->def, "no phi value");
20923 }
20924 tmp = triple_to_lattice(state, scc, slot[index]);
20925 /* meet(X, lattice low) = lattice low */
20926 if (is_lattice_lo(state, tmp)) {
20927 lnode->val = 0;
20928 }
20929 /* meet(X, lattice high) = X */
20930 else if (is_lattice_hi(state, tmp)) {
20931 lnode->val = lnode->val;
20932 }
20933 /* meet(lattice high, X) = X */
20934 else if (is_lattice_hi(state, lnode)) {
20935 lnode->val = dup_triple(state, tmp->val);
20936 /* Only change the type if necessary */
20937 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20938 lnode->val->type = lnode->def->type;
20939 }
20940 }
20941 /* meet(const, const) = const or lattice low */
20942 else if (!constants_equal(state, lnode->val, tmp->val)) {
20943 lnode->val = 0;
20944 }
20945
20946 /* meet(lattice low, X) = lattice low */
20947 if (is_lattice_lo(state, lnode)) {
20948 lnode->val = 0;
20949 break;
20950 }
20951 }
20952 changed = lval_changed(state, old, lnode);
20953 scc_debug_lnode(state, scc, lnode, changed);
20954
20955 /* If the lattice value has changed update the work lists. */
20956 if (changed) {
20957 struct ssa_edge *sedge;
20958 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20959 scc_add_sedge_dst(state, scc, sedge);
20960 }
20961 }
20962}
20963
20964
Eric Biedermanb138ac82003-04-22 18:44:01 +000020965static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20966 struct lattice_node *lnode)
20967{
20968 int changed;
20969
Eric Biederman90089602004-05-28 14:11:54 +000020970 if (!triple_is_def(state, lnode->def)) {
20971 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020972 }
Eric Biederman90089602004-05-28 14:11:54 +000020973 changed = compute_lnode_val(state, scc, lnode);
20974 scc_debug_lnode(state, scc, lnode, changed);
20975
20976 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020977 struct ssa_edge *sedge;
20978 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020979 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020980 }
20981 }
20982}
20983
20984static void scc_writeback_values(
20985 struct compile_state *state, struct scc_state *scc)
20986{
20987 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020988 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020989 ins = first;
20990 do {
20991 struct lattice_node *lnode;
20992 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020993 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020994 if (is_lattice_hi(state, lnode) &&
20995 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020996 {
20997 struct flow_edge *fedge;
20998 int executable;
20999 executable = 0;
21000 for(fedge = lnode->fblock->in;
21001 !executable && fedge; fedge = fedge->in_next) {
21002 executable |= fedge->executable;
21003 }
21004 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000021005 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021006 "lattice node %d %s->%s still high?",
21007 ins->id,
21008 tops(lnode->def->op),
21009 tops(lnode->val->op));
21010 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021011 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021012 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021013
Eric Biederman83b991a2003-10-11 06:20:25 +000021014 /* Restore id */
21015 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021016 if (lnode->val && (lnode->val != ins)) {
21017 /* See if it something I know how to write back */
21018 switch(lnode->val->op) {
21019 case OP_INTCONST:
21020 mkconst(state, ins, lnode->val->u.cval);
21021 break;
21022 case OP_ADDRCONST:
21023 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021024 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021025 break;
21026 default:
21027 /* By default don't copy the changes,
21028 * recompute them in place instead.
21029 */
21030 simplify(state, ins);
21031 break;
21032 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021033 if (is_const(lnode->val) &&
21034 !constants_equal(state, lnode->val, ins)) {
21035 internal_error(state, 0, "constants not equal");
21036 }
21037 /* Free the lattice nodes */
21038 xfree(lnode->val);
21039 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021040 }
21041 ins = ins->next;
21042 } while(ins != first);
21043}
21044
21045static void scc_transform(struct compile_state *state)
21046{
21047 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021048 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21049 return;
21050 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021051
21052 initialize_scc_state(state, &scc);
21053
21054 while(scc.flow_work_list || scc.ssa_work_list) {
21055 struct flow_edge *fedge;
21056 struct ssa_edge *sedge;
21057 struct flow_edge *fptr;
21058 while((fedge = scc_next_fedge(state, &scc))) {
21059 struct block *block;
21060 struct triple *ptr;
21061 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021062 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021063 int done;
21064 if (fedge->executable) {
21065 continue;
21066 }
21067 if (!fedge->dst) {
21068 internal_error(state, 0, "fedge without dst");
21069 }
21070 if (!fedge->src) {
21071 internal_error(state, 0, "fedge without src");
21072 }
21073 fedge->executable = 1;
21074 fblock = fedge->dst;
21075 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021076 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021077 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21078 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021079 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021080 }
21081 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021082
Eric Biederman5ade04a2003-10-22 04:03:46 +000021083 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000021084 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021085 block->vertex, reps);
21086 }
21087
Eric Biedermanb138ac82003-04-22 18:44:01 +000021088 done = 0;
21089 for(ptr = block->first; !done; ptr = ptr->next) {
21090 struct lattice_node *lnode;
21091 done = (ptr == block->last);
21092 lnode = &scc.lattice[ptr->id];
21093 if (ptr->op == OP_PHI) {
21094 scc_visit_phi(state, &scc, lnode);
21095 }
Eric Biederman90089602004-05-28 14:11:54 +000021096 else if ((reps == 1) && triple_is_def(state, ptr))
21097 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021098 scc_visit_expr(state, &scc, lnode);
21099 }
21100 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021101 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021102 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021103 struct flow_edge *out;
21104 for(out = fblock->out; out; out = out->out_next) {
21105 scc_add_fedge(state, &scc, out);
21106 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021107 }
21108 }
21109 while((sedge = scc_next_sedge(state, &scc))) {
21110 struct lattice_node *lnode;
21111 struct flow_block *fblock;
21112 lnode = sedge->dst;
21113 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021114
21115 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021116 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Peter Stuge01708ca2010-02-12 21:28:15 +000021117 (unsigned long)sedge - (unsigned long)scc.ssa_edges,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021118 sedge->src->def->id,
21119 sedge->dst->def->id);
21120 }
21121
Eric Biedermanb138ac82003-04-22 18:44:01 +000021122 if (lnode->def->op == OP_PHI) {
21123 scc_visit_phi(state, &scc, lnode);
21124 }
21125 else {
21126 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21127 if (fptr->executable) {
21128 break;
21129 }
21130 }
21131 if (fptr) {
21132 scc_visit_expr(state, &scc, lnode);
21133 }
21134 }
21135 }
21136 }
21137
21138 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021139 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021140 rebuild_ssa_form(state);
21141
Eric Biederman90089602004-05-28 14:11:54 +000021142 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021143}
21144
21145
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021146static void transform_to_arch_instructions(struct compile_state *state)
21147{
21148 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021149 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021150 ins = first;
21151 do {
21152 ins = transform_to_arch_instruction(state, ins);
21153 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021154
Eric Biederman90089602004-05-28 14:11:54 +000021155 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021156}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021157
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021158#if DEBUG_CONSISTENCY
21159static void verify_uses(struct compile_state *state)
21160{
21161 struct triple *first, *ins;
21162 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021163 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021164 ins = first;
21165 do {
21166 struct triple **expr;
21167 expr = triple_rhs(state, ins, 0);
21168 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021169 struct triple *rhs;
21170 rhs = *expr;
21171 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021172 if (set->member == ins) {
21173 break;
21174 }
21175 }
21176 if (!set) {
21177 internal_error(state, ins, "rhs not used");
21178 }
21179 }
21180 expr = triple_lhs(state, ins, 0);
21181 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021182 struct triple *lhs;
21183 lhs = *expr;
21184 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021185 if (set->member == ins) {
21186 break;
21187 }
21188 }
21189 if (!set) {
21190 internal_error(state, ins, "lhs not used");
21191 }
21192 }
Eric Biederman90089602004-05-28 14:11:54 +000021193 expr = triple_misc(state, ins, 0);
21194 if (ins->op != OP_PHI) {
21195 for(; expr; expr = triple_targ(state, ins, expr)) {
21196 struct triple *misc;
21197 misc = *expr;
21198 for(set = misc?misc->use:0; set; set = set->next) {
21199 if (set->member == ins) {
21200 break;
21201 }
21202 }
21203 if (!set) {
21204 internal_error(state, ins, "misc not used");
21205 }
21206 }
21207 }
21208 if (!triple_is_ret(state, ins)) {
21209 expr = triple_targ(state, ins, 0);
21210 for(; expr; expr = triple_targ(state, ins, expr)) {
21211 struct triple *targ;
21212 targ = *expr;
21213 for(set = targ?targ->use:0; set; set = set->next) {
21214 if (set->member == ins) {
21215 break;
21216 }
21217 }
21218 if (!set) {
21219 internal_error(state, ins, "targ not used");
21220 }
21221 }
21222 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021223 ins = ins->next;
21224 } while(ins != first);
21225
21226}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021227static void verify_blocks_present(struct compile_state *state)
21228{
21229 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021230 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021231 return;
21232 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021233 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021234 ins = first;
21235 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021236 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021237 if (triple_stores_block(state, ins)) {
21238 if (!ins->u.block) {
21239 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021240 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021241 }
21242 }
21243 ins = ins->next;
21244 } while(ins != first);
21245
21246
21247}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021248
21249static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21250{
21251 struct block_set *bedge;
21252 struct block *targ;
21253 targ = block_of_triple(state, edge);
21254 for(bedge = block->edges; bedge; bedge = bedge->next) {
21255 if (bedge->member == targ) {
21256 return 1;
21257 }
21258 }
21259 return 0;
21260}
21261
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021262static void verify_blocks(struct compile_state *state)
21263{
21264 struct triple *ins;
21265 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021266 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021267 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021268 if (!block) {
21269 return;
21270 }
Eric Biederman530b5192003-07-01 10:05:30 +000021271 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021272 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021273 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021274 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021275 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021276 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021277 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021278 internal_error(state, ins, "inconsitent block specified");
21279 }
Eric Biederman530b5192003-07-01 10:05:30 +000021280 valid_ins(state, ins);
21281 }
21282 users = 0;
21283 for(user = block->use; user; user = user->next) {
21284 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021285 if (!user->member->first) {
21286 internal_error(state, block->first, "user is empty");
21287 }
Eric Biederman90089602004-05-28 14:11:54 +000021288 if ((block == state->bb.last_block) &&
21289 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021290 continue;
21291 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021292 for(edge = user->member->edges; edge; edge = edge->next) {
21293 if (edge->member == block) {
21294 break;
21295 }
21296 }
21297 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021298 internal_error(state, user->member->first,
21299 "user does not use block");
21300 }
21301 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021302 if (triple_is_branch(state, block->last)) {
21303 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021304 expr = triple_edge_targ(state, block->last, 0);
21305 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021306 if (*expr && !edge_present(state, block, *expr)) {
21307 internal_error(state, block->last, "no edge to targ");
21308 }
21309 }
Eric Biederman530b5192003-07-01 10:05:30 +000021310 }
Eric Biederman90089602004-05-28 14:11:54 +000021311 if (!triple_is_ubranch(state, block->last) &&
21312 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021313 !edge_present(state, block, block->last->next)) {
21314 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021315 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021316 for(edge = block->edges; edge; edge = edge->next) {
21317 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021318 if (user->member == block) {
21319 break;
21320 }
21321 }
21322 if (!user || user->member != block) {
21323 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021324 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021325 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021326 if (!edge->member->first) {
21327 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021328 }
Eric Biederman530b5192003-07-01 10:05:30 +000021329 }
21330 if (block->users != users) {
21331 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021332 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021333 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021334 }
21335 if (!triple_stores_block(state, block->last->next)) {
21336 internal_error(state, block->last->next,
21337 "cannot find next block");
21338 }
21339 block = block->last->next->u.block;
21340 if (!block) {
21341 internal_error(state, block->last->next,
21342 "bad next block");
21343 }
Eric Biederman90089602004-05-28 14:11:54 +000021344 } while(block != state->bb.first_block);
21345 if (blocks != state->bb.last_vertex) {
21346 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21347 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021348 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021349}
21350
21351static void verify_domination(struct compile_state *state)
21352{
21353 struct triple *first, *ins;
21354 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021355 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021356 return;
21357 }
21358
Eric Biederman83b991a2003-10-11 06:20:25 +000021359 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021360 ins = first;
21361 do {
21362 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021363 struct triple **slot;
21364 struct triple *use_point;
21365 int i, zrhs;
21366 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021367 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021368 slot = &RHS(set->member, 0);
21369 /* See if the use is on the right hand side */
21370 for(i = 0; i < zrhs; i++) {
21371 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021372 break;
21373 }
21374 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021375 if (i < zrhs) {
21376 use_point = set->member;
21377 if (set->member->op == OP_PHI) {
21378 struct block_set *bset;
21379 int edge;
21380 bset = set->member->u.block->use;
21381 for(edge = 0; bset && (edge < i); edge++) {
21382 bset = bset->next;
21383 }
21384 if (!bset) {
21385 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021386 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021387 }
21388 use_point = bset->member->last;
21389 }
21390 }
21391 if (use_point &&
21392 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021393 if (is_const(ins)) {
21394 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021395 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021396 }
21397 else {
21398 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021399 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021400 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021401 }
21402 }
21403 ins = ins->next;
21404 } while(ins != first);
21405}
21406
Eric Biederman83b991a2003-10-11 06:20:25 +000021407static void verify_rhs(struct compile_state *state)
21408{
21409 struct triple *first, *ins;
21410 first = state->first;
21411 ins = first;
21412 do {
21413 struct triple **slot;
21414 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021415 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021416 slot = &RHS(ins, 0);
21417 for(i = 0; i < zrhs; i++) {
21418 if (slot[i] == 0) {
21419 internal_error(state, ins,
21420 "missing rhs %d on %s",
21421 i, tops(ins->op));
21422 }
21423 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21424 internal_error(state, ins,
21425 "ins == rhs[%d] on %s",
21426 i, tops(ins->op));
21427 }
21428 }
21429 ins = ins->next;
21430 } while(ins != first);
21431}
21432
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021433static void verify_piece(struct compile_state *state)
21434{
21435 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021436 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021437 ins = first;
21438 do {
21439 struct triple *ptr;
21440 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021441 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021442 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21443 if (ptr != LHS(ins, i)) {
21444 internal_error(state, ins, "malformed lhs on %s",
21445 tops(ins->op));
21446 }
21447 if (ptr->op != OP_PIECE) {
21448 internal_error(state, ins, "bad lhs op %s at %d on %s",
21449 tops(ptr->op), i, tops(ins->op));
21450 }
21451 if (ptr->u.cval != i) {
21452 internal_error(state, ins, "bad u.cval of %d %d expected",
21453 ptr->u.cval, i);
21454 }
21455 }
21456 ins = ins->next;
21457 } while(ins != first);
21458}
Eric Biederman83b991a2003-10-11 06:20:25 +000021459
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021460static void verify_ins_colors(struct compile_state *state)
21461{
21462 struct triple *first, *ins;
21463
Eric Biederman83b991a2003-10-11 06:20:25 +000021464 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021465 ins = first;
21466 do {
21467 ins = ins->next;
21468 } while(ins != first);
21469}
Eric Biederman90089602004-05-28 14:11:54 +000021470
21471static void verify_unknown(struct compile_state *state)
21472{
21473 struct triple *first, *ins;
21474 if ( (unknown_triple.next != &unknown_triple) ||
21475 (unknown_triple.prev != &unknown_triple) ||
21476#if 0
21477 (unknown_triple.use != 0) ||
21478#endif
21479 (unknown_triple.op != OP_UNKNOWNVAL) ||
21480 (unknown_triple.lhs != 0) ||
21481 (unknown_triple.rhs != 0) ||
21482 (unknown_triple.misc != 0) ||
21483 (unknown_triple.targ != 0) ||
21484 (unknown_triple.template_id != 0) ||
21485 (unknown_triple.id != -1) ||
21486 (unknown_triple.type != &unknown_type) ||
21487 (unknown_triple.occurance != &dummy_occurance) ||
21488 (unknown_triple.param[0] != 0) ||
21489 (unknown_triple.param[1] != 0)) {
21490 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21491 }
21492 if ( (dummy_occurance.count != 2) ||
21493 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21494 (strcmp(dummy_occurance.function, "") != 0) ||
21495 (dummy_occurance.col != 0) ||
21496 (dummy_occurance.parent != 0)) {
21497 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21498 }
21499 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21500 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21501 }
21502 first = state->first;
21503 ins = first;
21504 do {
21505 int params, i;
21506 if (ins == &unknown_triple) {
21507 internal_error(state, ins, "unknown triple in list");
21508 }
21509 params = TRIPLE_SIZE(ins);
21510 for(i = 0; i < params; i++) {
21511 if (ins->param[i] == &unknown_triple) {
21512 internal_error(state, ins, "unknown triple used!");
21513 }
21514 }
21515 ins = ins->next;
21516 } while(ins != first);
21517}
21518
21519static void verify_types(struct compile_state *state)
21520{
21521 struct triple *first, *ins;
21522 first = state->first;
21523 ins = first;
21524 do {
21525 struct type *invalid;
21526 invalid = invalid_type(state, ins->type);
21527 if (invalid) {
21528 FILE *fp = state->errout;
21529 fprintf(fp, "type: ");
21530 name_of(fp, ins->type);
21531 fprintf(fp, "\n");
21532 fprintf(fp, "invalid type: ");
21533 name_of(fp, invalid);
21534 fprintf(fp, "\n");
21535 internal_error(state, ins, "invalid ins type");
21536 }
21537 } while(ins != first);
21538}
21539
21540static void verify_copy(struct compile_state *state)
21541{
21542 struct triple *first, *ins, *next;
21543 first = state->first;
21544 next = ins = first;
21545 do {
21546 ins = next;
21547 next = ins->next;
21548 if (ins->op != OP_COPY) {
21549 continue;
21550 }
21551 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21552 FILE *fp = state->errout;
21553 fprintf(fp, "src type: ");
21554 name_of(fp, RHS(ins, 0)->type);
21555 fprintf(fp, "\n");
21556 fprintf(fp, "dst type: ");
21557 name_of(fp, ins->type);
21558 fprintf(fp, "\n");
21559 internal_error(state, ins, "type mismatch in copy");
21560 }
21561 } while(next != first);
21562}
21563
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021564static void verify_consistency(struct compile_state *state)
21565{
Eric Biederman90089602004-05-28 14:11:54 +000021566 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021567 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021568 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021569 verify_blocks(state);
21570 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021571 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021572 verify_piece(state);
21573 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021574 verify_types(state);
21575 verify_copy(state);
21576 if (state->compiler->debug & DEBUG_VERIFICATION) {
21577 fprintf(state->dbgout, "consistency verified\n");
21578 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021579}
21580#else
Eric Biederman153ea352003-06-20 14:43:20 +000021581static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021582#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021583
21584static void optimize(struct compile_state *state)
21585{
Eric Biederman90089602004-05-28 14:11:54 +000021586 /* Join all of the functions into one giant function */
21587 join_functions(state);
21588
Eric Biederman5ade04a2003-10-22 04:03:46 +000021589 /* Dump what the instruction graph intially looks like */
21590 print_triples(state);
21591
Eric Biederman0babc1c2003-05-09 02:39:00 +000021592 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021593 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021594 print_triples(state);
21595
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021596 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021597 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021598 state->bb.first = state->first;
21599 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021600
Eric Biederman530b5192003-07-01 10:05:30 +000021601 /* Transform the code to ssa form. */
21602 /*
21603 * The transformation to ssa form puts a phi function
21604 * on each of edge of a dominance frontier where that
21605 * phi function might be needed. At -O2 if we don't
21606 * eleminate the excess phi functions we can get an
21607 * exponential code size growth. So I kill the extra
21608 * phi functions early and I kill them often.
21609 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021610 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021611 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021612
Eric Biederman83b991a2003-10-11 06:20:25 +000021613 /* Remove dead code */
21614 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021615 verify_consistency(state);
21616
Eric Biedermanb138ac82003-04-22 18:44:01 +000021617 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021618 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021619 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021620 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021621 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021622 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021623#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021624#warning "WISHLIST implement single use constants (least possible register pressure)"
21625#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021626#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021627 /* Select architecture instructions and an initial partial
21628 * coloring based on architecture constraints.
21629 */
21630 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021631 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021632
Eric Biederman83b991a2003-10-11 06:20:25 +000021633 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021634 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021635 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021636
Eric Biedermanb138ac82003-04-22 18:44:01 +000021637 /* Color all of the variables to see if they will fit in registers */
21638 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021639 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021640
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021641 insert_mandatory_copies(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 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021645 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021646
Eric Biedermanb138ac82003-04-22 18:44:01 +000021647 /* Remove the optimization information.
21648 * This is more to check for memory consistency than to free memory.
21649 */
Eric Biederman90089602004-05-28 14:11:54 +000021650 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021651}
21652
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021653static void print_op_asm(struct compile_state *state,
21654 struct triple *ins, FILE *fp)
21655{
21656 struct asm_info *info;
21657 const char *ptr;
21658 unsigned lhs, rhs, i;
21659 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021660 lhs = ins->lhs;
21661 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021662 /* Don't count the clobbers in lhs */
21663 for(i = 0; i < lhs; i++) {
21664 if (LHS(ins, i)->type == &void_type) {
21665 break;
21666 }
21667 }
21668 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021669 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021670 fputc('\t', fp);
21671 for(ptr = info->str; *ptr; ptr++) {
21672 char *next;
21673 unsigned long param;
21674 struct triple *piece;
21675 if (*ptr != '%') {
21676 fputc(*ptr, fp);
21677 continue;
21678 }
21679 ptr++;
21680 if (*ptr == '%') {
21681 fputc('%', fp);
21682 continue;
21683 }
21684 param = strtoul(ptr, &next, 10);
21685 if (ptr == next) {
21686 error(state, ins, "Invalid asm template");
21687 }
21688 if (param >= (lhs + rhs)) {
21689 error(state, ins, "Invalid param %%%u in asm template",
21690 param);
21691 }
21692 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
21693 fprintf(fp, "%s",
21694 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021695 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021696 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021697 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021698}
21699
21700
21701/* Only use the low x86 byte registers. This allows me
21702 * allocate the entire register when a byte register is used.
21703 */
21704#define X86_4_8BIT_GPRS 1
21705
Eric Biederman83b991a2003-10-11 06:20:25 +000021706/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021707#define X86_MMX_REGS (1<<0)
21708#define X86_XMM_REGS (1<<1)
21709#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021710
Eric Biedermanb138ac82003-04-22 18:44:01 +000021711/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021712#define REGC_FLAGS 0
21713#define REGC_GPR8 1
21714#define REGC_GPR16 2
21715#define REGC_GPR32 3
21716#define REGC_DIVIDEND64 4
21717#define REGC_DIVIDEND32 5
21718#define REGC_MMX 6
21719#define REGC_XMM 7
21720#define REGC_GPR32_8 8
21721#define REGC_GPR16_8 9
21722#define REGC_GPR8_LO 10
21723#define REGC_IMM32 11
21724#define REGC_IMM16 12
21725#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021726#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021727#if LAST_REGC >= MAX_REGC
21728#error "MAX_REGC is to low"
21729#endif
21730
21731/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021732#define REGCM_FLAGS (1 << REGC_FLAGS)
21733#define REGCM_GPR8 (1 << REGC_GPR8)
21734#define REGCM_GPR16 (1 << REGC_GPR16)
21735#define REGCM_GPR32 (1 << REGC_GPR32)
21736#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21737#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21738#define REGCM_MMX (1 << REGC_MMX)
21739#define REGCM_XMM (1 << REGC_XMM)
21740#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21741#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21742#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21743#define REGCM_IMM32 (1 << REGC_IMM32)
21744#define REGCM_IMM16 (1 << REGC_IMM16)
21745#define REGCM_IMM8 (1 << REGC_IMM8)
21746#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021747#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021748
21749/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021750#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021751#define REGC_FLAGS_FIRST REG_EFLAGS
21752#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021753#define REG_AL 3
21754#define REG_BL 4
21755#define REG_CL 5
21756#define REG_DL 6
21757#define REG_AH 7
21758#define REG_BH 8
21759#define REG_CH 9
21760#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021761#define REGC_GPR8_LO_FIRST REG_AL
21762#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021763#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021764#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021765#define REG_AX 11
21766#define REG_BX 12
21767#define REG_CX 13
21768#define REG_DX 14
21769#define REG_SI 15
21770#define REG_DI 16
21771#define REG_BP 17
21772#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021773#define REGC_GPR16_FIRST REG_AX
21774#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021775#define REG_EAX 19
21776#define REG_EBX 20
21777#define REG_ECX 21
21778#define REG_EDX 22
21779#define REG_ESI 23
21780#define REG_EDI 24
21781#define REG_EBP 25
21782#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021783#define REGC_GPR32_FIRST REG_EAX
21784#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021785#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021786#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21787#define REGC_DIVIDEND64_LAST REG_EDXEAX
21788#define REG_DXAX 28
21789#define REGC_DIVIDEND32_FIRST REG_DXAX
21790#define REGC_DIVIDEND32_LAST REG_DXAX
21791#define REG_MMX0 29
21792#define REG_MMX1 30
21793#define REG_MMX2 31
21794#define REG_MMX3 32
21795#define REG_MMX4 33
21796#define REG_MMX5 34
21797#define REG_MMX6 35
21798#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021799#define REGC_MMX_FIRST REG_MMX0
21800#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021801#define REG_XMM0 37
21802#define REG_XMM1 38
21803#define REG_XMM2 39
21804#define REG_XMM3 40
21805#define REG_XMM4 41
21806#define REG_XMM5 42
21807#define REG_XMM6 43
21808#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021809#define REGC_XMM_FIRST REG_XMM0
21810#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021811
21812#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021813#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021814#endif
21815
Eric Biedermanb138ac82003-04-22 18:44:01 +000021816#define LAST_REG REG_XMM7
21817
21818#define REGC_GPR32_8_FIRST REG_EAX
21819#define REGC_GPR32_8_LAST REG_EDX
21820#define REGC_GPR16_8_FIRST REG_AX
21821#define REGC_GPR16_8_LAST REG_DX
21822
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021823#define REGC_IMM8_FIRST -1
21824#define REGC_IMM8_LAST -1
21825#define REGC_IMM16_FIRST -2
21826#define REGC_IMM16_LAST -1
21827#define REGC_IMM32_FIRST -4
21828#define REGC_IMM32_LAST -1
21829
Eric Biedermanb138ac82003-04-22 18:44:01 +000021830#if LAST_REG >= MAX_REGISTERS
21831#error "MAX_REGISTERS to low"
21832#endif
21833
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021834
21835static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021836 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21837 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21838 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21839 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21840 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21841 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21842 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21843 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21844 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21845 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21846 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21847 [REGC_IMM32] = 0,
21848 [REGC_IMM16] = 0,
21849 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021850};
21851
21852static const struct {
21853 int first, last;
21854} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021855 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21856 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21857 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21858 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21859 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21860 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21861 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21862 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21863 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21864 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21865 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21866 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21867 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21868 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021869};
21870
Eric Biederman90089602004-05-28 14:11:54 +000021871#if ARCH_INPUT_REGS != 4
21872#error ARCH_INPUT_REGS size mismatch
21873#endif
21874static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21875 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21876 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21877 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21878 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21879};
21880
21881#if ARCH_OUTPUT_REGS != 4
21882#error ARCH_INPUT_REGS size mismatch
21883#endif
21884static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21885 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21886 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21887 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21888 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21889};
21890
Eric Biederman5ade04a2003-10-22 04:03:46 +000021891static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021892{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021893 memset(arch, 0, sizeof(*arch));
21894 arch->features = 0;
21895}
21896
Eric Biederman90089602004-05-28 14:11:54 +000021897static const struct compiler_flag arch_flags[] = {
21898 { "mmx", X86_MMX_REGS },
21899 { "sse", X86_XMM_REGS },
21900 { "noop-copy", X86_NOOP_COPY },
21901 { 0, 0 },
21902};
21903static const struct compiler_flag arch_cpus[] = {
21904 { "i386", 0 },
21905 { "p2", X86_MMX_REGS },
21906 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21907 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21908 { "k7", X86_MMX_REGS },
21909 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21910 { "c3", X86_MMX_REGS },
21911 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21912 { 0, 0 }
21913};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021914static int arch_encode_flag(struct arch_state *arch, const char *flag)
21915{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021916 int result;
21917 int act;
21918
21919 act = 1;
21920 result = -1;
21921 if (strncmp(flag, "no-", 3) == 0) {
21922 flag += 3;
21923 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021924 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021925 if (act && strncmp(flag, "cpu=", 4) == 0) {
21926 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021927 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021928 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021929 else {
Eric Biederman90089602004-05-28 14:11:54 +000021930 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021931 }
21932 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021933}
21934
Eric Biederman90089602004-05-28 14:11:54 +000021935static void arch_usage(FILE *fp)
21936{
21937 flag_usage(fp, arch_flags, "-m", "-mno-");
21938 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21939}
21940
Eric Biedermanb138ac82003-04-22 18:44:01 +000021941static unsigned arch_regc_size(struct compile_state *state, int class)
21942{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021943 if ((class < 0) || (class > LAST_REGC)) {
21944 return 0;
21945 }
21946 return regc_size[class];
21947}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021948
Eric Biedermanb138ac82003-04-22 18:44:01 +000021949static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21950{
21951 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021952 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21953 REGCM_GPR32_8 | REGCM_GPR32 |
21954 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021955
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021956 /* Special case for the immediates */
21957 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21958 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21959 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21960 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
21961 return 0;
21962 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021963 return (regcm1 & regcm2) ||
21964 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21965}
21966
21967static void arch_reg_equivs(
21968 struct compile_state *state, unsigned *equiv, int reg)
21969{
21970 if ((reg < 0) || (reg > LAST_REG)) {
21971 internal_error(state, 0, "invalid register");
21972 }
21973 *equiv++ = reg;
21974 switch(reg) {
21975 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021976#if X86_4_8BIT_GPRS
21977 *equiv++ = REG_AH;
21978#endif
21979 *equiv++ = REG_AX;
21980 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021981 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021982 *equiv++ = REG_EDXEAX;
21983 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021984 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021985#if X86_4_8BIT_GPRS
21986 *equiv++ = REG_AL;
21987#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021988 *equiv++ = REG_AX;
21989 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021990 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021991 *equiv++ = REG_EDXEAX;
21992 break;
21993 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021994#if X86_4_8BIT_GPRS
21995 *equiv++ = REG_BH;
21996#endif
21997 *equiv++ = REG_BX;
21998 *equiv++ = REG_EBX;
21999 break;
22000
Eric Biedermanb138ac82003-04-22 18:44:01 +000022001 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022002#if X86_4_8BIT_GPRS
22003 *equiv++ = REG_BL;
22004#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022005 *equiv++ = REG_BX;
22006 *equiv++ = REG_EBX;
22007 break;
22008 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022009#if X86_4_8BIT_GPRS
22010 *equiv++ = REG_CH;
22011#endif
22012 *equiv++ = REG_CX;
22013 *equiv++ = REG_ECX;
22014 break;
22015
Eric Biedermanb138ac82003-04-22 18:44:01 +000022016 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022017#if X86_4_8BIT_GPRS
22018 *equiv++ = REG_CL;
22019#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022020 *equiv++ = REG_CX;
22021 *equiv++ = REG_ECX;
22022 break;
22023 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022024#if X86_4_8BIT_GPRS
22025 *equiv++ = REG_DH;
22026#endif
22027 *equiv++ = REG_DX;
22028 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022029 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022030 *equiv++ = REG_EDXEAX;
22031 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022032 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022033#if X86_4_8BIT_GPRS
22034 *equiv++ = REG_DL;
22035#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022036 *equiv++ = REG_DX;
22037 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022038 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022039 *equiv++ = REG_EDXEAX;
22040 break;
22041 case REG_AX:
22042 *equiv++ = REG_AL;
22043 *equiv++ = REG_AH;
22044 *equiv++ = REG_EAX;
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_BX:
22049 *equiv++ = REG_BL;
22050 *equiv++ = REG_BH;
22051 *equiv++ = REG_EBX;
22052 break;
22053 case REG_CX:
22054 *equiv++ = REG_CL;
22055 *equiv++ = REG_CH;
22056 *equiv++ = REG_ECX;
22057 break;
22058 case REG_DX:
22059 *equiv++ = REG_DL;
22060 *equiv++ = REG_DH;
22061 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022062 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022063 *equiv++ = REG_EDXEAX;
22064 break;
22065 case REG_SI:
22066 *equiv++ = REG_ESI;
22067 break;
22068 case REG_DI:
22069 *equiv++ = REG_EDI;
22070 break;
22071 case REG_BP:
22072 *equiv++ = REG_EBP;
22073 break;
22074 case REG_SP:
22075 *equiv++ = REG_ESP;
22076 break;
22077 case REG_EAX:
22078 *equiv++ = REG_AL;
22079 *equiv++ = REG_AH;
22080 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022081 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022082 *equiv++ = REG_EDXEAX;
22083 break;
22084 case REG_EBX:
22085 *equiv++ = REG_BL;
22086 *equiv++ = REG_BH;
22087 *equiv++ = REG_BX;
22088 break;
22089 case REG_ECX:
22090 *equiv++ = REG_CL;
22091 *equiv++ = REG_CH;
22092 *equiv++ = REG_CX;
22093 break;
22094 case REG_EDX:
22095 *equiv++ = REG_DL;
22096 *equiv++ = REG_DH;
22097 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022098 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022099 *equiv++ = REG_EDXEAX;
22100 break;
22101 case REG_ESI:
22102 *equiv++ = REG_SI;
22103 break;
22104 case REG_EDI:
22105 *equiv++ = REG_DI;
22106 break;
22107 case REG_EBP:
22108 *equiv++ = REG_BP;
22109 break;
22110 case REG_ESP:
22111 *equiv++ = REG_SP;
22112 break;
Eric Biederman530b5192003-07-01 10:05:30 +000022113 case REG_DXAX:
22114 *equiv++ = REG_AL;
22115 *equiv++ = REG_AH;
22116 *equiv++ = REG_DL;
22117 *equiv++ = REG_DH;
22118 *equiv++ = REG_AX;
22119 *equiv++ = REG_DX;
22120 *equiv++ = REG_EAX;
22121 *equiv++ = REG_EDX;
22122 *equiv++ = REG_EDXEAX;
22123 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022124 case REG_EDXEAX:
22125 *equiv++ = REG_AL;
22126 *equiv++ = REG_AH;
22127 *equiv++ = REG_DL;
22128 *equiv++ = REG_DH;
22129 *equiv++ = REG_AX;
22130 *equiv++ = REG_DX;
22131 *equiv++ = REG_EAX;
22132 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022133 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022134 break;
22135 }
22136 *equiv++ = REG_UNSET;
22137}
22138
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022139static unsigned arch_avail_mask(struct compile_state *state)
22140{
22141 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022142 /* REGCM_GPR8 is not available */
22143 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22144 REGCM_GPR32 | REGCM_GPR32_8 |
22145 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022146 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022147 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022148 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022149 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022150 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022151 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022152 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022153 return avail_mask;
22154}
22155
22156static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22157{
22158 unsigned mask, result;
22159 int class, class2;
22160 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022161
22162 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22163 if ((result & mask) == 0) {
22164 continue;
22165 }
22166 if (class > LAST_REGC) {
22167 result &= ~mask;
22168 }
22169 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22170 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22171 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22172 result |= (1 << class2);
22173 }
22174 }
22175 }
Eric Biederman530b5192003-07-01 10:05:30 +000022176 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022177 return result;
22178}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022179
Eric Biedermand1ea5392003-06-28 06:49:45 +000022180static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22181{
22182 /* Like arch_regcm_normalize except immediate register classes are excluded */
22183 regcm = arch_regcm_normalize(state, regcm);
22184 /* Remove the immediate register classes */
22185 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22186 return regcm;
22187
22188}
22189
Eric Biedermanb138ac82003-04-22 18:44:01 +000022190static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22191{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022192 unsigned mask;
22193 int class;
22194 mask = 0;
22195 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022196 if ((reg >= regcm_bound[class].first) &&
22197 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022198 mask |= (1 << class);
22199 }
22200 }
22201 if (!mask) {
22202 internal_error(state, 0, "reg %d not in any class", reg);
22203 }
22204 return mask;
22205}
22206
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022207static struct reg_info arch_reg_constraint(
22208 struct compile_state *state, struct type *type, const char *constraint)
22209{
22210 static const struct {
22211 char class;
22212 unsigned int mask;
22213 unsigned int reg;
22214 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022215 { 'r', REGCM_GPR32, REG_UNSET },
22216 { 'g', REGCM_GPR32, REG_UNSET },
22217 { 'p', REGCM_GPR32, REG_UNSET },
22218 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022219 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022220 { 'x', REGCM_XMM, REG_UNSET },
22221 { 'y', REGCM_MMX, REG_UNSET },
22222 { 'a', REGCM_GPR32, REG_EAX },
22223 { 'b', REGCM_GPR32, REG_EBX },
22224 { 'c', REGCM_GPR32, REG_ECX },
22225 { 'd', REGCM_GPR32, REG_EDX },
22226 { 'D', REGCM_GPR32, REG_EDI },
22227 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022228 { '\0', 0, REG_UNSET },
22229 };
22230 unsigned int regcm;
22231 unsigned int mask, reg;
22232 struct reg_info result;
22233 const char *ptr;
22234 regcm = arch_type_to_regcm(state, type);
22235 reg = REG_UNSET;
22236 mask = 0;
22237 for(ptr = constraint; *ptr; ptr++) {
22238 int i;
22239 if (*ptr == ' ') {
22240 continue;
22241 }
22242 for(i = 0; constraints[i].class != '\0'; i++) {
22243 if (constraints[i].class == *ptr) {
22244 break;
22245 }
22246 }
22247 if (constraints[i].class == '\0') {
22248 error(state, 0, "invalid register constraint ``%c''", *ptr);
22249 break;
22250 }
22251 if ((constraints[i].mask & regcm) == 0) {
22252 error(state, 0, "invalid register class %c specified",
22253 *ptr);
22254 }
22255 mask |= constraints[i].mask;
22256 if (constraints[i].reg != REG_UNSET) {
22257 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22258 error(state, 0, "Only one register may be specified");
22259 }
22260 reg = constraints[i].reg;
22261 }
22262 }
22263 result.reg = reg;
22264 result.regcm = mask;
22265 return result;
22266}
22267
22268static struct reg_info arch_reg_clobber(
22269 struct compile_state *state, const char *clobber)
22270{
22271 struct reg_info result;
22272 if (strcmp(clobber, "memory") == 0) {
22273 result.reg = REG_UNSET;
22274 result.regcm = 0;
22275 }
Eric Biederman90089602004-05-28 14:11:54 +000022276 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022277 result.reg = REG_EAX;
22278 result.regcm = REGCM_GPR32;
22279 }
Eric Biederman90089602004-05-28 14:11:54 +000022280 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022281 result.reg = REG_EBX;
22282 result.regcm = REGCM_GPR32;
22283 }
Eric Biederman90089602004-05-28 14:11:54 +000022284 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022285 result.reg = REG_ECX;
22286 result.regcm = REGCM_GPR32;
22287 }
Eric Biederman90089602004-05-28 14:11:54 +000022288 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022289 result.reg = REG_EDX;
22290 result.regcm = REGCM_GPR32;
22291 }
Eric Biederman90089602004-05-28 14:11:54 +000022292 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022293 result.reg = REG_ESI;
22294 result.regcm = REGCM_GPR32;
22295 }
Eric Biederman90089602004-05-28 14:11:54 +000022296 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022297 result.reg = REG_EDI;
22298 result.regcm = REGCM_GPR32;
22299 }
Eric Biederman90089602004-05-28 14:11:54 +000022300 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022301 result.reg = REG_EBP;
22302 result.regcm = REGCM_GPR32;
22303 }
Eric Biederman90089602004-05-28 14:11:54 +000022304 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022305 result.reg = REG_ESP;
22306 result.regcm = REGCM_GPR32;
22307 }
22308 else if (strcmp(clobber, "cc") == 0) {
22309 result.reg = REG_EFLAGS;
22310 result.regcm = REGCM_FLAGS;
22311 }
22312 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22313 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22314 result.reg = REG_XMM0 + octdigval(clobber[3]);
22315 result.regcm = REGCM_XMM;
22316 }
Eric Biederman90089602004-05-28 14:11:54 +000022317 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022318 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22319 result.reg = REG_MMX0 + octdigval(clobber[3]);
22320 result.regcm = REGCM_MMX;
22321 }
22322 else {
Eric Biederman90089602004-05-28 14:11:54 +000022323 error(state, 0, "unknown register name `%s' in asm",
22324 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022325 result.reg = REG_UNSET;
22326 result.regcm = 0;
22327 }
22328 return result;
22329}
22330
Eric Biedermanb138ac82003-04-22 18:44:01 +000022331static int do_select_reg(struct compile_state *state,
22332 char *used, int reg, unsigned classes)
22333{
22334 unsigned mask;
22335 if (used[reg]) {
22336 return REG_UNSET;
22337 }
22338 mask = arch_reg_regcm(state, reg);
22339 return (classes & mask) ? reg : REG_UNSET;
22340}
22341
22342static int arch_select_free_register(
22343 struct compile_state *state, char *used, int classes)
22344{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022345 /* Live ranges with the most neighbors are colored first.
22346 *
22347 * Generally it does not matter which colors are given
22348 * as the register allocator attempts to color live ranges
22349 * in an order where you are guaranteed not to run out of colors.
22350 *
22351 * Occasionally the register allocator cannot find an order
22352 * of register selection that will find a free color. To
22353 * increase the odds the register allocator will work when
22354 * it guesses first give out registers from register classes
22355 * least likely to run out of registers.
22356 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022357 */
22358 int i, reg;
22359 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022360 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022361 reg = do_select_reg(state, used, i, classes);
22362 }
22363 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22364 reg = do_select_reg(state, used, i, classes);
22365 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022366 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022367 reg = do_select_reg(state, used, i, classes);
22368 }
22369 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22370 reg = do_select_reg(state, used, i, classes);
22371 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022372 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22373 reg = do_select_reg(state, used, i, classes);
22374 }
Eric Biederman530b5192003-07-01 10:05:30 +000022375 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22376 reg = do_select_reg(state, used, i, classes);
22377 }
22378 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22379 reg = do_select_reg(state, used, i, classes);
22380 }
22381 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022382 reg = do_select_reg(state, used, i, classes);
22383 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022384 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22385 reg = do_select_reg(state, used, i, classes);
22386 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022387 return reg;
22388}
22389
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022390
Eric Biedermanb138ac82003-04-22 18:44:01 +000022391static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
22392{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022393
22394#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022395#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022396#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022397 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022398 mask = 0;
22399 switch(type->type & TYPE_MASK) {
22400 case TYPE_ARRAY:
22401 case TYPE_VOID:
22402 mask = 0;
22403 break;
22404 case TYPE_CHAR:
22405 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022406 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022407 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022408 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022409 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022410 REGCM_MMX | REGCM_XMM |
22411 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022412 break;
22413 case TYPE_SHORT:
22414 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022415 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022416 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022417 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022418 REGCM_MMX | REGCM_XMM |
22419 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022420 break;
Eric Biederman90089602004-05-28 14:11:54 +000022421 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022422 case TYPE_INT:
22423 case TYPE_UINT:
22424 case TYPE_LONG:
22425 case TYPE_ULONG:
22426 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022427 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022428 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22429 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022430 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022431 break;
Eric Biederman90089602004-05-28 14:11:54 +000022432 case TYPE_JOIN:
22433 case TYPE_UNION:
22434 mask = arch_type_to_regcm(state, type->left);
22435 break;
22436 case TYPE_OVERLAP:
22437 mask = arch_type_to_regcm(state, type->left) &
22438 arch_type_to_regcm(state, type->right);
22439 break;
22440 case TYPE_BITFIELD:
22441 mask = arch_type_to_regcm(state, type->left);
22442 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022443 default:
Eric Biederman90089602004-05-28 14:11:54 +000022444 fprintf(state->errout, "type: ");
22445 name_of(state->errout, type);
22446 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022447 internal_error(state, 0, "no register class for type");
22448 break;
22449 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022450 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022451 return mask;
22452}
22453
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022454static int is_imm32(struct triple *imm)
22455{
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022456 // second condition commented out to prevent compiler warning:
22457 // imm->u.cval is always 32bit unsigned, so the comparison is
22458 // always true.
22459 return ((imm->op == OP_INTCONST) /* && (imm->u.cval <= 0xffffffffUL) */ ) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022460 (imm->op == OP_ADDRCONST);
22461
22462}
22463static int is_imm16(struct triple *imm)
22464{
22465 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22466}
22467static int is_imm8(struct triple *imm)
22468{
22469 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22470}
22471
22472static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022473{
22474 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022475 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022476 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022477 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022478 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022479 if (!is_imm32(imm)) {
22480 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022481 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022482 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022483 use_triple(imm, ins);
22484 *expr = imm;
22485 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022486}
22487
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022488static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022489{
22490 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022491 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022492 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022493 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022494 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022495 if (!is_imm8(imm)) {
22496 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022497 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022498 unuse_triple(*expr, ins);
22499 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022500 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022501 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022502}
22503
Eric Biederman530b5192003-07-01 10:05:30 +000022504#define TEMPLATE_NOP 0
22505#define TEMPLATE_INTCONST8 1
22506#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022507#define TEMPLATE_UNKNOWNVAL 3
22508#define TEMPLATE_COPY8_REG 5
22509#define TEMPLATE_COPY16_REG 6
22510#define TEMPLATE_COPY32_REG 7
22511#define TEMPLATE_COPY_IMM8 8
22512#define TEMPLATE_COPY_IMM16 9
22513#define TEMPLATE_COPY_IMM32 10
22514#define TEMPLATE_PHI8 11
22515#define TEMPLATE_PHI16 12
22516#define TEMPLATE_PHI32 13
22517#define TEMPLATE_STORE8 14
22518#define TEMPLATE_STORE16 15
22519#define TEMPLATE_STORE32 16
22520#define TEMPLATE_LOAD8 17
22521#define TEMPLATE_LOAD16 18
22522#define TEMPLATE_LOAD32 19
22523#define TEMPLATE_BINARY8_REG 20
22524#define TEMPLATE_BINARY16_REG 21
22525#define TEMPLATE_BINARY32_REG 22
22526#define TEMPLATE_BINARY8_IMM 23
22527#define TEMPLATE_BINARY16_IMM 24
22528#define TEMPLATE_BINARY32_IMM 25
22529#define TEMPLATE_SL8_CL 26
22530#define TEMPLATE_SL16_CL 27
22531#define TEMPLATE_SL32_CL 28
22532#define TEMPLATE_SL8_IMM 29
22533#define TEMPLATE_SL16_IMM 30
22534#define TEMPLATE_SL32_IMM 31
22535#define TEMPLATE_UNARY8 32
22536#define TEMPLATE_UNARY16 33
22537#define TEMPLATE_UNARY32 34
22538#define TEMPLATE_CMP8_REG 35
22539#define TEMPLATE_CMP16_REG 36
22540#define TEMPLATE_CMP32_REG 37
22541#define TEMPLATE_CMP8_IMM 38
22542#define TEMPLATE_CMP16_IMM 39
22543#define TEMPLATE_CMP32_IMM 40
22544#define TEMPLATE_TEST8 41
22545#define TEMPLATE_TEST16 42
22546#define TEMPLATE_TEST32 43
22547#define TEMPLATE_SET 44
22548#define TEMPLATE_JMP 45
22549#define TEMPLATE_RET 46
22550#define TEMPLATE_INB_DX 47
22551#define TEMPLATE_INB_IMM 48
22552#define TEMPLATE_INW_DX 49
22553#define TEMPLATE_INW_IMM 50
22554#define TEMPLATE_INL_DX 51
22555#define TEMPLATE_INL_IMM 52
22556#define TEMPLATE_OUTB_DX 53
22557#define TEMPLATE_OUTB_IMM 54
22558#define TEMPLATE_OUTW_DX 55
22559#define TEMPLATE_OUTW_IMM 56
22560#define TEMPLATE_OUTL_DX 57
22561#define TEMPLATE_OUTL_IMM 58
22562#define TEMPLATE_BSF 59
22563#define TEMPLATE_RDMSR 60
22564#define TEMPLATE_WRMSR 61
22565#define TEMPLATE_UMUL8 62
22566#define TEMPLATE_UMUL16 63
22567#define TEMPLATE_UMUL32 64
22568#define TEMPLATE_DIV8 65
22569#define TEMPLATE_DIV16 66
22570#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022571#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022572#if LAST_TEMPLATE >= MAX_TEMPLATES
22573#error "MAX_TEMPLATES to low"
22574#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022575
Eric Biederman530b5192003-07-01 10:05:30 +000022576#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
22577#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
22578#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022579
Eric Biedermanb138ac82003-04-22 18:44:01 +000022580
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022581static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022582 [TEMPLATE_NOP] = {
22583 .lhs = {
22584 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22638 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22639 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22640 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22641 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22642 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22643 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22644 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22645 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22646 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22647 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22648 },
22649 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022650 [TEMPLATE_INTCONST8] = {
22651 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22652 },
22653 [TEMPLATE_INTCONST32] = {
22654 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22655 },
Eric Biederman90089602004-05-28 14:11:54 +000022656 [TEMPLATE_UNKNOWNVAL] = {
22657 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22658 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022659 [TEMPLATE_COPY8_REG] = {
22660 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22661 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022662 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022663 [TEMPLATE_COPY16_REG] = {
22664 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22665 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22666 },
22667 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022668 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022669 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022670 },
22671 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022672 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022673 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22674 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022675 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022676 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022677 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22678 },
22679 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022680 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022681 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22682 },
22683 [TEMPLATE_PHI8] = {
22684 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022685 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22686 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022687 [TEMPLATE_PHI16] = {
22688 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022689 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22690 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022691 [TEMPLATE_PHI32] = {
22692 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022693 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22694 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022695 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022696 .rhs = {
22697 [0] = { REG_UNSET, REGCM_GPR32 },
22698 [1] = { REG_UNSET, REGCM_GPR8_LO },
22699 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022700 },
22701 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022702 .rhs = {
22703 [0] = { REG_UNSET, REGCM_GPR32 },
22704 [1] = { REG_UNSET, REGCM_GPR16 },
22705 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022706 },
22707 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022708 .rhs = {
22709 [0] = { REG_UNSET, REGCM_GPR32 },
22710 [1] = { REG_UNSET, REGCM_GPR32 },
22711 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022712 },
22713 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022714 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022715 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22716 },
22717 [TEMPLATE_LOAD16] = {
22718 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22719 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22720 },
22721 [TEMPLATE_LOAD32] = {
22722 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22723 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22724 },
Eric Biederman530b5192003-07-01 10:05:30 +000022725 [TEMPLATE_BINARY8_REG] = {
22726 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22727 .rhs = {
22728 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22729 [1] = { REG_UNSET, REGCM_GPR8_LO },
22730 },
22731 },
22732 [TEMPLATE_BINARY16_REG] = {
22733 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22734 .rhs = {
22735 [0] = { REG_VIRT0, REGCM_GPR16 },
22736 [1] = { REG_UNSET, REGCM_GPR16 },
22737 },
22738 },
22739 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022740 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22741 .rhs = {
22742 [0] = { REG_VIRT0, REGCM_GPR32 },
22743 [1] = { REG_UNSET, REGCM_GPR32 },
22744 },
22745 },
Eric Biederman530b5192003-07-01 10:05:30 +000022746 [TEMPLATE_BINARY8_IMM] = {
22747 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22748 .rhs = {
22749 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22750 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22751 },
22752 },
22753 [TEMPLATE_BINARY16_IMM] = {
22754 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22755 .rhs = {
22756 [0] = { REG_VIRT0, REGCM_GPR16 },
22757 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22758 },
22759 },
22760 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022761 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22762 .rhs = {
22763 [0] = { REG_VIRT0, REGCM_GPR32 },
22764 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22765 },
22766 },
Eric Biederman530b5192003-07-01 10:05:30 +000022767 [TEMPLATE_SL8_CL] = {
22768 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22769 .rhs = {
22770 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22771 [1] = { REG_CL, REGCM_GPR8_LO },
22772 },
22773 },
22774 [TEMPLATE_SL16_CL] = {
22775 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22776 .rhs = {
22777 [0] = { REG_VIRT0, REGCM_GPR16 },
22778 [1] = { REG_CL, REGCM_GPR8_LO },
22779 },
22780 },
22781 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022782 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22783 .rhs = {
22784 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022785 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022786 },
22787 },
Eric Biederman530b5192003-07-01 10:05:30 +000022788 [TEMPLATE_SL8_IMM] = {
22789 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22790 .rhs = {
22791 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22792 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22793 },
22794 },
22795 [TEMPLATE_SL16_IMM] = {
22796 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22797 .rhs = {
22798 [0] = { REG_VIRT0, REGCM_GPR16 },
22799 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22800 },
22801 },
22802 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022803 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22804 .rhs = {
22805 [0] = { REG_VIRT0, REGCM_GPR32 },
22806 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22807 },
22808 },
Eric Biederman530b5192003-07-01 10:05:30 +000022809 [TEMPLATE_UNARY8] = {
22810 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22811 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22812 },
22813 [TEMPLATE_UNARY16] = {
22814 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22815 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22816 },
22817 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022818 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22819 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22820 },
Eric Biederman530b5192003-07-01 10:05:30 +000022821 [TEMPLATE_CMP8_REG] = {
22822 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22823 .rhs = {
22824 [0] = { REG_UNSET, REGCM_GPR8_LO },
22825 [1] = { REG_UNSET, REGCM_GPR8_LO },
22826 },
22827 },
22828 [TEMPLATE_CMP16_REG] = {
22829 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22830 .rhs = {
22831 [0] = { REG_UNSET, REGCM_GPR16 },
22832 [1] = { REG_UNSET, REGCM_GPR16 },
22833 },
22834 },
22835 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022836 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22837 .rhs = {
22838 [0] = { REG_UNSET, REGCM_GPR32 },
22839 [1] = { REG_UNSET, REGCM_GPR32 },
22840 },
22841 },
Eric Biederman530b5192003-07-01 10:05:30 +000022842 [TEMPLATE_CMP8_IMM] = {
22843 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22844 .rhs = {
22845 [0] = { REG_UNSET, REGCM_GPR8_LO },
22846 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22847 },
22848 },
22849 [TEMPLATE_CMP16_IMM] = {
22850 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22851 .rhs = {
22852 [0] = { REG_UNSET, REGCM_GPR16 },
22853 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22854 },
22855 },
22856 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022857 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22858 .rhs = {
22859 [0] = { REG_UNSET, REGCM_GPR32 },
22860 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22861 },
22862 },
Eric Biederman530b5192003-07-01 10:05:30 +000022863 [TEMPLATE_TEST8] = {
22864 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22865 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22866 },
22867 [TEMPLATE_TEST16] = {
22868 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22869 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22870 },
22871 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022872 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22873 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22874 },
22875 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022876 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022877 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22878 },
22879 [TEMPLATE_JMP] = {
22880 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22881 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022882 [TEMPLATE_RET] = {
22883 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22884 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022885 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022886 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022887 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22888 },
22889 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022890 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022891 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22892 },
22893 [TEMPLATE_INW_DX] = {
22894 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22895 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22896 },
22897 [TEMPLATE_INW_IMM] = {
22898 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22899 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22900 },
22901 [TEMPLATE_INL_DX] = {
22902 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22903 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22904 },
22905 [TEMPLATE_INL_IMM] = {
22906 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22907 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22908 },
22909 [TEMPLATE_OUTB_DX] = {
22910 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022911 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022912 [1] = { REG_DX, REGCM_GPR16 },
22913 },
22914 },
22915 [TEMPLATE_OUTB_IMM] = {
22916 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022917 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022918 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22919 },
22920 },
22921 [TEMPLATE_OUTW_DX] = {
22922 .rhs = {
22923 [0] = { REG_AX, REGCM_GPR16 },
22924 [1] = { REG_DX, REGCM_GPR16 },
22925 },
22926 },
22927 [TEMPLATE_OUTW_IMM] = {
22928 .rhs = {
22929 [0] = { REG_AX, REGCM_GPR16 },
22930 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22931 },
22932 },
22933 [TEMPLATE_OUTL_DX] = {
22934 .rhs = {
22935 [0] = { REG_EAX, REGCM_GPR32 },
22936 [1] = { REG_DX, REGCM_GPR16 },
22937 },
22938 },
22939 [TEMPLATE_OUTL_IMM] = {
22940 .rhs = {
22941 [0] = { REG_EAX, REGCM_GPR32 },
22942 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22943 },
22944 },
22945 [TEMPLATE_BSF] = {
22946 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22947 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22948 },
22949 [TEMPLATE_RDMSR] = {
22950 .lhs = {
22951 [0] = { REG_EAX, REGCM_GPR32 },
22952 [1] = { REG_EDX, REGCM_GPR32 },
22953 },
22954 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22955 },
22956 [TEMPLATE_WRMSR] = {
22957 .rhs = {
22958 [0] = { REG_ECX, REGCM_GPR32 },
22959 [1] = { REG_EAX, REGCM_GPR32 },
22960 [2] = { REG_EDX, REGCM_GPR32 },
22961 },
22962 },
Eric Biederman530b5192003-07-01 10:05:30 +000022963 [TEMPLATE_UMUL8] = {
22964 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22965 .rhs = {
22966 [0] = { REG_AL, REGCM_GPR8_LO },
22967 [1] = { REG_UNSET, REGCM_GPR8_LO },
22968 },
22969 },
22970 [TEMPLATE_UMUL16] = {
22971 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
22972 .rhs = {
22973 [0] = { REG_AX, REGCM_GPR16 },
22974 [1] = { REG_UNSET, REGCM_GPR16 },
22975 },
22976 },
22977 [TEMPLATE_UMUL32] = {
22978 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022979 .rhs = {
22980 [0] = { REG_EAX, REGCM_GPR32 },
22981 [1] = { REG_UNSET, REGCM_GPR32 },
22982 },
22983 },
Eric Biederman530b5192003-07-01 10:05:30 +000022984 [TEMPLATE_DIV8] = {
22985 .lhs = {
22986 [0] = { REG_AL, REGCM_GPR8_LO },
22987 [1] = { REG_AH, REGCM_GPR8 },
22988 },
22989 .rhs = {
22990 [0] = { REG_AX, REGCM_GPR16 },
22991 [1] = { REG_UNSET, REGCM_GPR8_LO },
22992 },
22993 },
22994 [TEMPLATE_DIV16] = {
22995 .lhs = {
22996 [0] = { REG_AX, REGCM_GPR16 },
22997 [1] = { REG_DX, REGCM_GPR16 },
22998 },
22999 .rhs = {
23000 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
23001 [1] = { REG_UNSET, REGCM_GPR16 },
23002 },
23003 },
23004 [TEMPLATE_DIV32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023005 .lhs = {
23006 [0] = { REG_EAX, REGCM_GPR32 },
23007 [1] = { REG_EDX, REGCM_GPR32 },
23008 },
23009 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023010 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023011 [1] = { REG_UNSET, REGCM_GPR32 },
23012 },
23013 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023014};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023015
Eric Biederman83b991a2003-10-11 06:20:25 +000023016static void fixup_branch(struct compile_state *state,
23017 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23018 struct triple *left, struct triple *right)
23019{
23020 struct triple *test;
23021 if (!left) {
23022 internal_error(state, branch, "no branch test?");
23023 }
23024 test = pre_triple(state, branch,
23025 cmp_op, cmp_type, left, right);
23026 test->template_id = TEMPLATE_TEST32;
23027 if (cmp_op == OP_CMP) {
23028 test->template_id = TEMPLATE_CMP32_REG;
23029 if (get_imm32(test, &RHS(test, 1))) {
23030 test->template_id = TEMPLATE_CMP32_IMM;
23031 }
23032 }
23033 use_triple(RHS(test, 0), test);
23034 use_triple(RHS(test, 1), test);
23035 unuse_triple(RHS(branch, 0), branch);
23036 RHS(branch, 0) = test;
23037 branch->op = jmp_op;
23038 branch->template_id = TEMPLATE_JMP;
23039 use_triple(RHS(branch, 0), branch);
23040}
23041
Eric Biedermanb138ac82003-04-22 18:44:01 +000023042static void fixup_branches(struct compile_state *state,
23043 struct triple *cmp, struct triple *use, int jmp_op)
23044{
23045 struct triple_set *entry, *next;
23046 for(entry = use->use; entry; entry = next) {
23047 next = entry->next;
23048 if (entry->member->op == OP_COPY) {
23049 fixup_branches(state, cmp, entry->member, jmp_op);
23050 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023051 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023052 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023053 struct triple *left, *right;
23054 left = right = 0;
23055 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023056 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023057 right = RHS(cmp, 1);
23058 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023059 branch = entry->member;
Eric Biederman83b991a2003-10-11 06:20:25 +000023060 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023061 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023062 }
23063 }
23064}
23065
23066static void bool_cmp(struct compile_state *state,
23067 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23068{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023069 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023070 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023071
23072 /* Put a barrier up before the cmp which preceeds the
23073 * copy instruction. If a set actually occurs this gives
23074 * us a chance to move variables in registers out of the way.
23075 */
23076
23077 /* Modify the comparison operator */
23078 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023079 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023080 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023081 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023082 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023083 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023084 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023085 }
23086 /* Generate the instruction sequence that will transform the
23087 * result of the comparison into a logical value.
23088 */
Eric Biederman90089602004-05-28 14:11:54 +000023089 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023090 use_triple(ins, set);
23091 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023092
Eric Biederman90089602004-05-28 14:11:54 +000023093 convert = set;
23094 if (!equiv_types(ins->type, set->type)) {
23095 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23096 use_triple(set, convert);
23097 convert->template_id = TEMPLATE_COPY32_REG;
23098 }
23099
Eric Biedermanb138ac82003-04-22 18:44:01 +000023100 for(entry = ins->use; entry; entry = next) {
23101 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023102 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023103 continue;
23104 }
Eric Biederman90089602004-05-28 14:11:54 +000023105 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023106 }
Eric Biederman90089602004-05-28 14:11:54 +000023107 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023108}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023109
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023110struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23111{
23112 struct ins_template *template;
23113 struct reg_info result;
23114 int zlhs;
23115 if (ins->op == OP_PIECE) {
23116 index = ins->u.cval;
23117 ins = MISC(ins, 0);
23118 }
Eric Biederman90089602004-05-28 14:11:54 +000023119 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023120 if (triple_is_def(state, ins)) {
23121 zlhs = 1;
23122 }
23123 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023124 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023125 index, tops(ins->op));
23126 }
23127 switch(ins->op) {
23128 case OP_ASM:
23129 template = &ins->u.ainfo->tmpl;
23130 break;
23131 default:
23132 if (ins->template_id > LAST_TEMPLATE) {
23133 internal_error(state, ins, "bad template number %d",
23134 ins->template_id);
23135 }
23136 template = &templates[ins->template_id];
23137 break;
23138 }
23139 result = template->lhs[index];
23140 result.regcm = arch_regcm_normalize(state, result.regcm);
23141 if (result.reg != REG_UNNEEDED) {
23142 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23143 }
23144 if (result.regcm == 0) {
23145 internal_error(state, ins, "lhs %d regcm == 0", index);
23146 }
23147 return result;
23148}
23149
23150struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23151{
23152 struct reg_info result;
23153 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023154 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023155 (ins->op == OP_PIECE)) {
23156 internal_error(state, ins, "index %d out of range for %s\n",
23157 index, tops(ins->op));
23158 }
23159 switch(ins->op) {
23160 case OP_ASM:
23161 template = &ins->u.ainfo->tmpl;
23162 break;
Eric Biederman90089602004-05-28 14:11:54 +000023163 case OP_PHI:
23164 index = 0;
23165 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023166 default:
23167 if (ins->template_id > LAST_TEMPLATE) {
23168 internal_error(state, ins, "bad template number %d",
23169 ins->template_id);
23170 }
23171 template = &templates[ins->template_id];
23172 break;
23173 }
23174 result = template->rhs[index];
23175 result.regcm = arch_regcm_normalize(state, result.regcm);
23176 if (result.regcm == 0) {
23177 internal_error(state, ins, "rhs %d regcm == 0", index);
23178 }
23179 return result;
23180}
23181
Eric Biederman530b5192003-07-01 10:05:30 +000023182static struct triple *mod_div(struct compile_state *state,
23183 struct triple *ins, int div_op, int index)
23184{
23185 struct triple *div, *piece0, *piece1;
23186
Eric Biederman530b5192003-07-01 10:05:30 +000023187 /* Generate the appropriate division instruction */
23188 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23189 RHS(div, 0) = RHS(ins, 0);
23190 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023191 piece0 = LHS(div, 0);
23192 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023193 div->template_id = TEMPLATE_DIV32;
23194 use_triple(RHS(div, 0), div);
23195 use_triple(RHS(div, 1), div);
23196 use_triple(LHS(div, 0), div);
23197 use_triple(LHS(div, 1), div);
23198
Eric Biederman530b5192003-07-01 10:05:30 +000023199 /* Replate uses of ins with the appropriate piece of the div */
23200 propogate_use(state, ins, LHS(div, index));
23201 release_triple(state, ins);
23202
23203 /* Return the address of the next instruction */
23204 return piece1->next;
23205}
23206
Eric Biederman90089602004-05-28 14:11:54 +000023207static int noop_adecl(struct triple *adecl)
23208{
23209 struct triple_set *use;
23210 /* It's a noop if it doesn't specify stoorage */
23211 if (adecl->lhs == 0) {
23212 return 1;
23213 }
23214 /* Is the adecl used? If not it's a noop */
23215 for(use = adecl->use; use ; use = use->next) {
23216 if ((use->member->op != OP_PIECE) ||
23217 (MISC(use->member, 0) != adecl)) {
23218 return 0;
23219 }
23220 }
23221 return 1;
23222}
23223
23224static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23225{
23226 struct triple *mask, *nmask, *shift;
23227 struct triple *val, *val_mask, *val_shift;
23228 struct triple *targ, *targ_mask;
23229 struct triple *new;
23230 ulong_t the_mask, the_nmask;
23231
23232 targ = RHS(ins, 0);
23233 val = RHS(ins, 1);
23234
23235 /* Get constant for the mask value */
23236 the_mask = 1;
23237 the_mask <<= ins->u.bitfield.size;
23238 the_mask -= 1;
23239 the_mask <<= ins->u.bitfield.offset;
23240 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23241 mask->u.cval = the_mask;
23242
23243 /* Get the inverted mask value */
23244 the_nmask = ~the_mask;
23245 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23246 nmask->u.cval = the_nmask;
23247
23248 /* Get constant for the shift value */
23249 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23250 shift->u.cval = ins->u.bitfield.offset;
23251
23252 /* Shift and mask the source value */
23253 val_shift = val;
23254 if (shift->u.cval != 0) {
23255 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23256 use_triple(val, val_shift);
23257 use_triple(shift, val_shift);
23258 }
23259 val_mask = val_shift;
23260 if (is_signed(val->type)) {
23261 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23262 use_triple(val_shift, val_mask);
23263 use_triple(mask, val_mask);
23264 }
23265
23266 /* Mask the target value */
23267 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23268 use_triple(targ, targ_mask);
23269 use_triple(nmask, targ_mask);
23270
23271 /* Now combined them together */
23272 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23273 use_triple(targ_mask, new);
23274 use_triple(val_mask, new);
23275
23276 /* Move all of the users over to the new expression */
23277 propogate_use(state, ins, new);
23278
23279 /* Delete the original triple */
23280 release_triple(state, ins);
23281
23282 /* Restart the transformation at mask */
23283 return mask;
23284}
23285
23286static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23287{
23288 struct triple *mask, *shift;
23289 struct triple *val, *val_mask, *val_shift;
23290 ulong_t the_mask;
23291
23292 val = RHS(ins, 0);
23293
23294 /* Get constant for the mask value */
23295 the_mask = 1;
23296 the_mask <<= ins->u.bitfield.size;
23297 the_mask -= 1;
23298 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23299 mask->u.cval = the_mask;
23300
23301 /* Get constant for the right shift value */
23302 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23303 shift->u.cval = ins->u.bitfield.offset;
23304
23305 /* Shift arithmetic right, to correct the sign */
23306 val_shift = val;
23307 if (shift->u.cval != 0) {
23308 int op;
23309 if (ins->op == OP_SEXTRACT) {
23310 op = OP_SSR;
23311 } else {
23312 op = OP_USR;
23313 }
23314 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23315 use_triple(val, val_shift);
23316 use_triple(shift, val_shift);
23317 }
23318
23319 /* Finally mask the value */
23320 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23321 use_triple(val_shift, val_mask);
23322 use_triple(mask, val_mask);
23323
23324 /* Move all of the users over to the new expression */
23325 propogate_use(state, ins, val_mask);
23326
23327 /* Release the original instruction */
23328 release_triple(state, ins);
23329
23330 return mask;
23331
23332}
23333
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023334static struct triple *transform_to_arch_instruction(
23335 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023336{
23337 /* Transform from generic 3 address instructions
23338 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023339 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023340 * Copies are inserted to preserve the register flexibility
23341 * of 3 address instructions.
23342 */
Eric Biederman90089602004-05-28 14:11:54 +000023343 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023344 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023345 next = ins->next;
23346 switch(ins->op) {
23347 case OP_INTCONST:
23348 ins->template_id = TEMPLATE_INTCONST32;
23349 if (ins->u.cval < 256) {
23350 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023351 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023352 break;
23353 case OP_ADDRCONST:
23354 ins->template_id = TEMPLATE_INTCONST32;
23355 break;
Eric Biederman90089602004-05-28 14:11:54 +000023356 case OP_UNKNOWNVAL:
23357 ins->template_id = TEMPLATE_UNKNOWNVAL;
23358 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023359 case OP_NOOP:
23360 case OP_SDECL:
23361 case OP_BLOBCONST:
23362 case OP_LABEL:
23363 ins->template_id = TEMPLATE_NOP;
23364 break;
23365 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023366 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023367 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023368 value = RHS(ins, 0);
23369 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023370 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023371 }
Eric Biederman90089602004-05-28 14:11:54 +000023372 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023373 ins->template_id = TEMPLATE_COPY_IMM16;
23374 }
Eric Biederman90089602004-05-28 14:11:54 +000023375 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023376 ins->template_id = TEMPLATE_COPY_IMM32;
23377 }
Eric Biederman90089602004-05-28 14:11:54 +000023378 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023379 internal_error(state, ins, "bad constant passed to copy");
23380 }
Eric Biederman90089602004-05-28 14:11:54 +000023381 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023382 ins->template_id = TEMPLATE_COPY8_REG;
23383 }
Eric Biederman90089602004-05-28 14:11:54 +000023384 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023385 ins->template_id = TEMPLATE_COPY16_REG;
23386 }
Eric Biederman90089602004-05-28 14:11:54 +000023387 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023388 ins->template_id = TEMPLATE_COPY32_REG;
23389 }
23390 else {
23391 internal_error(state, ins, "bad type passed to copy");
23392 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023393 break;
23394 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023395 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023396 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023397 ins->template_id = TEMPLATE_PHI8;
23398 }
Eric Biederman90089602004-05-28 14:11:54 +000023399 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023400 ins->template_id = TEMPLATE_PHI16;
23401 }
Eric Biederman90089602004-05-28 14:11:54 +000023402 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023403 ins->template_id = TEMPLATE_PHI32;
23404 }
23405 else {
23406 internal_error(state, ins, "bad type passed to phi");
23407 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023408 break;
Eric Biederman90089602004-05-28 14:11:54 +000023409 case OP_ADECL:
23410 /* Adecls should always be treated as dead code and
23411 * removed. If we are not optimizing they may linger.
23412 */
23413 if (!noop_adecl(ins)) {
23414 internal_error(state, ins, "adecl remains?");
23415 }
23416 ins->template_id = TEMPLATE_NOP;
23417 next = after_lhs(state, ins);
23418 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023419 case OP_STORE:
23420 switch(ins->type->type & TYPE_MASK) {
23421 case TYPE_CHAR: case TYPE_UCHAR:
23422 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023423 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023424 case TYPE_SHORT: case TYPE_USHORT:
23425 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023426 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023427 case TYPE_INT: case TYPE_UINT:
23428 case TYPE_LONG: case TYPE_ULONG:
23429 case TYPE_POINTER:
23430 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023431 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023432 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023433 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023434 break;
23435 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023436 break;
23437 case OP_LOAD:
23438 switch(ins->type->type & TYPE_MASK) {
23439 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023440 case TYPE_SHORT: case TYPE_USHORT:
23441 case TYPE_INT: case TYPE_UINT:
23442 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023443 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023444 break;
23445 default:
23446 internal_error(state, ins, "unknown type in load");
23447 break;
23448 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023449 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023450 break;
23451 case OP_ADD:
23452 case OP_SUB:
23453 case OP_AND:
23454 case OP_XOR:
23455 case OP_OR:
23456 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023457 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023458 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023459 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023460 }
23461 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023462 case OP_SDIVT:
23463 case OP_UDIVT:
23464 ins->template_id = TEMPLATE_DIV32;
23465 next = after_lhs(state, ins);
23466 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023467 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023468 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023469 break;
23470 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023471 next = mod_div(state, ins, OP_UDIVT, 0);
23472 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023473 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023474 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023475 break;
23476 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023477 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023478 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023479 case OP_SMOD:
23480 next = mod_div(state, ins, OP_SDIVT, 1);
23481 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023482 case OP_SL:
23483 case OP_SSR:
23484 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023485 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023486 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023487 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023488 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23489 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023490 }
23491 break;
23492 case OP_INVERT:
23493 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023494 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023495 break;
23496 case OP_EQ:
23497 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
23498 break;
23499 case OP_NOTEQ:
23500 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23501 break;
23502 case OP_SLESS:
23503 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23504 break;
23505 case OP_ULESS:
23506 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23507 break;
23508 case OP_SMORE:
23509 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23510 break;
23511 case OP_UMORE:
23512 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23513 break;
23514 case OP_SLESSEQ:
23515 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23516 break;
23517 case OP_ULESSEQ:
23518 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23519 break;
23520 case OP_SMOREEQ:
23521 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23522 break;
23523 case OP_UMOREEQ:
23524 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23525 break;
23526 case OP_LTRUE:
23527 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23528 break;
23529 case OP_LFALSE:
23530 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23531 break;
23532 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023533 ins->op = OP_JMP;
23534 ins->template_id = TEMPLATE_NOP;
23535 break;
23536 case OP_CBRANCH:
23537 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
23538 RHS(ins, 0)->type, RHS(ins, 0), 0);
23539 break;
23540 case OP_CALL:
23541 ins->template_id = TEMPLATE_NOP;
23542 break;
23543 case OP_RET:
23544 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023545 break;
23546 case OP_INB:
23547 case OP_INW:
23548 case OP_INL:
23549 switch(ins->op) {
23550 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23551 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23552 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23553 }
23554 if (get_imm8(ins, &RHS(ins, 0))) {
23555 ins->template_id += 1;
23556 }
23557 break;
23558 case OP_OUTB:
23559 case OP_OUTW:
23560 case OP_OUTL:
23561 switch(ins->op) {
23562 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23563 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23564 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23565 }
23566 if (get_imm8(ins, &RHS(ins, 1))) {
23567 ins->template_id += 1;
23568 }
23569 break;
23570 case OP_BSF:
23571 case OP_BSR:
23572 ins->template_id = TEMPLATE_BSF;
23573 break;
23574 case OP_RDMSR:
23575 ins->template_id = TEMPLATE_RDMSR;
23576 next = after_lhs(state, ins);
23577 break;
23578 case OP_WRMSR:
23579 ins->template_id = TEMPLATE_WRMSR;
23580 break;
23581 case OP_HLT:
23582 ins->template_id = TEMPLATE_NOP;
23583 break;
23584 case OP_ASM:
23585 ins->template_id = TEMPLATE_NOP;
23586 next = after_lhs(state, ins);
23587 break;
23588 /* Already transformed instructions */
23589 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023590 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023591 break;
23592 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023593 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023594 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023595 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023596 }
23597 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023598 case OP_JMP:
23599 ins->template_id = TEMPLATE_NOP;
23600 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023601 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23602 case OP_JMP_SLESS: case OP_JMP_ULESS:
23603 case OP_JMP_SMORE: case OP_JMP_UMORE:
23604 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23605 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23606 ins->template_id = TEMPLATE_JMP;
23607 break;
23608 case OP_SET_EQ: case OP_SET_NOTEQ:
23609 case OP_SET_SLESS: case OP_SET_ULESS:
23610 case OP_SET_SMORE: case OP_SET_UMORE:
23611 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23612 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23613 ins->template_id = TEMPLATE_SET;
23614 break;
Eric Biederman90089602004-05-28 14:11:54 +000023615 case OP_DEPOSIT:
23616 next = x86_deposit(state, ins);
23617 break;
23618 case OP_SEXTRACT:
23619 case OP_UEXTRACT:
23620 next = x86_extract(state, ins);
23621 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023622 /* Unhandled instructions */
23623 case OP_PIECE:
23624 default:
Eric Biederman90089602004-05-28 14:11:54 +000023625 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023626 ins->op, tops(ins->op));
23627 break;
23628 }
23629 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023630}
23631
Eric Biederman530b5192003-07-01 10:05:30 +000023632static long next_label(struct compile_state *state)
23633{
Eric Biederman90089602004-05-28 14:11:54 +000023634 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023635 return ++label_counter;
23636}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023637static void generate_local_labels(struct compile_state *state)
23638{
23639 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023640 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023641 label = first;
23642 do {
23643 if ((label->op == OP_LABEL) ||
23644 (label->op == OP_SDECL)) {
23645 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023646 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023647 } else {
23648 label->u.cval = 0;
23649 }
23650
23651 }
23652 label = label->next;
23653 } while(label != first);
23654}
23655
23656static int check_reg(struct compile_state *state,
23657 struct triple *triple, int classes)
23658{
23659 unsigned mask;
23660 int reg;
23661 reg = ID_REG(triple->id);
23662 if (reg == REG_UNSET) {
23663 internal_error(state, triple, "register not set");
23664 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023665 mask = arch_reg_regcm(state, reg);
23666 if (!(classes & mask)) {
23667 internal_error(state, triple, "reg %d in wrong class",
23668 reg);
23669 }
23670 return reg;
23671}
23672
Eric Biederman90089602004-05-28 14:11:54 +000023673
Eric Biederman530b5192003-07-01 10:05:30 +000023674#if REG_XMM7 != 44
23675#error "Registers have renumberd fix arch_reg_str"
23676#endif
Eric Biederman90089602004-05-28 14:11:54 +000023677static const char *arch_regs[] = {
23678 "%unset",
23679 "%unneeded",
23680 "%eflags",
23681 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23682 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23683 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23684 "%edx:%eax",
23685 "%dx:%ax",
23686 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
23687 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
23688 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23689};
23690static const char *arch_reg_str(int reg)
23691{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023692 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23693 reg = 0;
23694 }
Eric Biederman90089602004-05-28 14:11:54 +000023695 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023696}
23697
23698static const char *reg(struct compile_state *state, struct triple *triple,
23699 int classes)
23700{
23701 int reg;
23702 reg = check_reg(state, triple, classes);
23703 return arch_reg_str(reg);
23704}
23705
Eric Biederman90089602004-05-28 14:11:54 +000023706static int arch_reg_size(int reg)
23707{
23708 int size;
23709 size = 0;
23710 if (reg == REG_EFLAGS) {
23711 size = 32;
23712 }
23713 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23714 size = 8;
23715 }
23716 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23717 size = 16;
23718 }
23719 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23720 size = 32;
23721 }
23722 else if (reg == REG_EDXEAX) {
23723 size = 64;
23724 }
23725 else if (reg == REG_DXAX) {
23726 size = 32;
23727 }
23728 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23729 size = 64;
23730 }
23731 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23732 size = 128;
23733 }
23734 return size;
23735}
23736
23737static int reg_size(struct compile_state *state, struct triple *ins)
23738{
23739 int reg;
23740 reg = ID_REG(ins->id);
23741 if (reg == REG_UNSET) {
23742 internal_error(state, ins, "register not set");
23743 }
23744 return arch_reg_size(reg);
23745}
23746
23747
23748
Eric Biedermanb138ac82003-04-22 18:44:01 +000023749const char *type_suffix(struct compile_state *state, struct type *type)
23750{
23751 const char *suffix;
23752 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023753 case SIZEOF_I8: suffix = "b"; break;
23754 case SIZEOF_I16: suffix = "w"; break;
23755 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023756 default:
23757 internal_error(state, 0, "unknown suffix");
23758 suffix = 0;
23759 break;
23760 }
23761 return suffix;
23762}
23763
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023764static void print_const_val(
23765 struct compile_state *state, struct triple *ins, FILE *fp)
23766{
23767 switch(ins->op) {
23768 case OP_INTCONST:
23769 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023770 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023771 break;
23772 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023773 if ((MISC(ins, 0)->op != OP_SDECL) &&
23774 (MISC(ins, 0)->op != OP_LABEL))
23775 {
Eric Biederman830c9882003-07-04 00:27:33 +000023776 internal_error(state, ins, "bad base for addrconst");
23777 }
23778 if (MISC(ins, 0)->u.cval <= 0) {
23779 internal_error(state, ins, "unlabeled constant");
23780 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023781 fprintf(fp, " $L%s%lu+%lu ",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023782 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023783 (unsigned long)(MISC(ins, 0)->u.cval),
23784 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023785 break;
23786 default:
23787 internal_error(state, ins, "unknown constant type");
23788 break;
23789 }
23790}
23791
Eric Biederman530b5192003-07-01 10:05:30 +000023792static void print_const(struct compile_state *state,
23793 struct triple *ins, FILE *fp)
23794{
23795 switch(ins->op) {
23796 case OP_INTCONST:
23797 switch(ins->type->type & TYPE_MASK) {
23798 case TYPE_CHAR:
23799 case TYPE_UCHAR:
Eric Biederman83b991a2003-10-11 06:20:25 +000023800 fprintf(fp, ".byte 0x%02lx\n",
23801 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023802 break;
23803 case TYPE_SHORT:
23804 case TYPE_USHORT:
Eric Biederman83b991a2003-10-11 06:20:25 +000023805 fprintf(fp, ".short 0x%04lx\n",
23806 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023807 break;
23808 case TYPE_INT:
23809 case TYPE_UINT:
23810 case TYPE_LONG:
23811 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023812 case TYPE_POINTER:
Eric Biederman83b991a2003-10-11 06:20:25 +000023813 fprintf(fp, ".int %lu\n",
23814 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023815 break;
23816 default:
Eric Biederman90089602004-05-28 14:11:54 +000023817 fprintf(state->errout, "type: ");
23818 name_of(state->errout, ins->type);
23819 fprintf(state->errout, "\n");
23820 internal_error(state, ins, "Unknown constant type. Val: %lu",
23821 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023822 }
Eric Biederman90089602004-05-28 14:11:54 +000023823
Eric Biederman530b5192003-07-01 10:05:30 +000023824 break;
23825 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023826 if ((MISC(ins, 0)->op != OP_SDECL) &&
23827 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023828 internal_error(state, ins, "bad base for addrconst");
23829 }
23830 if (MISC(ins, 0)->u.cval <= 0) {
23831 internal_error(state, ins, "unlabeled constant");
23832 }
23833 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023834 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023835 (unsigned long)(MISC(ins, 0)->u.cval),
23836 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023837 break;
23838 case OP_BLOBCONST:
23839 {
23840 unsigned char *blob;
23841 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023842 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023843 blob = ins->u.blob;
23844 for(i = 0; i < size; i++) {
23845 fprintf(fp, ".byte 0x%02x\n",
23846 blob[i]);
23847 }
23848 break;
23849 }
23850 default:
23851 internal_error(state, ins, "Unknown constant type");
23852 break;
23853 }
23854}
23855
23856#define TEXT_SECTION ".rom.text"
23857#define DATA_SECTION ".rom.data"
23858
23859static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023860 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023861{
Eric Biederman90089602004-05-28 14:11:54 +000023862 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023863 long ref;
23864 ref = next_label(state);
23865 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023866 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023867 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023868 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023869 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23870 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023871 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023872 }
Eric Biederman530b5192003-07-01 10:05:30 +000023873 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23874 return ref;
23875}
23876
Eric Biederman90089602004-05-28 14:11:54 +000023877static long get_mask_pool_ref(
23878 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23879{
23880 long ref;
23881 if (mask == 0xff) {
23882 ref = 1;
23883 }
23884 else if (mask == 0xffff) {
23885 ref = 2;
23886 }
23887 else {
23888 ref = 0;
23889 internal_error(state, ins, "unhandled mask value");
23890 }
23891 return ref;
23892}
23893
Eric Biedermanb138ac82003-04-22 18:44:01 +000023894static void print_binary_op(struct compile_state *state,
23895 const char *op, struct triple *ins, FILE *fp)
23896{
23897 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023898 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023899 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023900 internal_error(state, ins, "invalid register assignment");
23901 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023902 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023903 fprintf(fp, "\t%s ", op);
23904 print_const_val(state, RHS(ins, 1), fp);
23905 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023906 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023907 }
23908 else {
23909 unsigned lmask, rmask;
23910 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023911 lreg = check_reg(state, RHS(ins, 0), mask);
23912 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023913 lmask = arch_reg_regcm(state, lreg);
23914 rmask = arch_reg_regcm(state, rreg);
23915 mask = lmask & rmask;
23916 fprintf(fp, "\t%s %s, %s\n",
23917 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023918 reg(state, RHS(ins, 1), mask),
23919 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023920 }
23921}
23922static void print_unary_op(struct compile_state *state,
23923 const char *op, struct triple *ins, FILE *fp)
23924{
23925 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023926 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023927 fprintf(fp, "\t%s %s\n",
23928 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023929 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023930}
23931
23932static void print_op_shift(struct compile_state *state,
23933 const char *op, struct triple *ins, FILE *fp)
23934{
23935 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023936 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023937 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023938 internal_error(state, ins, "invalid register assignment");
23939 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023940 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023941 fprintf(fp, "\t%s ", op);
23942 print_const_val(state, RHS(ins, 1), fp);
23943 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023944 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023945 }
23946 else {
23947 fprintf(fp, "\t%s %s, %s\n",
23948 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023949 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023950 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023951 }
23952}
23953
23954static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23955{
23956 const char *op;
23957 int mask;
23958 int dreg;
23959 mask = 0;
23960 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023961 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023962 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23963 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23964 default:
23965 internal_error(state, ins, "not an in operation");
23966 op = 0;
23967 break;
23968 }
23969 dreg = check_reg(state, ins, mask);
23970 if (!reg_is_reg(state, dreg, REG_EAX)) {
23971 internal_error(state, ins, "dst != %%eax");
23972 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023973 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023974 fprintf(fp, "\t%s ", op);
23975 print_const_val(state, RHS(ins, 0), fp);
23976 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023977 reg(state, ins, mask));
23978 }
23979 else {
23980 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023981 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023982 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23983 internal_error(state, ins, "src != %%dx");
23984 }
23985 fprintf(fp, "\t%s %s, %s\n",
23986 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023987 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023988 reg(state, ins, mask));
23989 }
23990}
23991
23992static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23993{
23994 const char *op;
23995 int mask;
23996 int lreg;
23997 mask = 0;
23998 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023999 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024000 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
24001 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
24002 default:
24003 internal_error(state, ins, "not an out operation");
24004 op = 0;
24005 break;
24006 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024007 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024008 if (!reg_is_reg(state, lreg, REG_EAX)) {
24009 internal_error(state, ins, "src != %%eax");
24010 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024011 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024012 fprintf(fp, "\t%s %s,",
24013 op, reg(state, RHS(ins, 0), mask));
24014 print_const_val(state, RHS(ins, 1), fp);
24015 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024016 }
24017 else {
24018 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024019 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024020 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24021 internal_error(state, ins, "dst != %%dx");
24022 }
24023 fprintf(fp, "\t%s %s, %s\n",
24024 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024025 reg(state, RHS(ins, 0), mask),
24026 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024027 }
24028}
24029
24030static void print_op_move(struct compile_state *state,
24031 struct triple *ins, FILE *fp)
24032{
24033 /* op_move is complex because there are many types
24034 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024035 * Because OP_COPY will be introduced in arbitrary locations
24036 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024037 * OP_CONVERT can change the flags and it is the only operation
24038 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024039 */
24040 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24041 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024042 if (state->arch->features & X86_NOOP_COPY) {
24043 omit_copy = 0;
24044 }
24045 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024046 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024047 dst = ins;
24048 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024049 else {
24050 internal_error(state, ins, "unknown move operation");
24051 src = dst = 0;
24052 }
Eric Biederman90089602004-05-28 14:11:54 +000024053 if (reg_size(state, dst) < size_of(state, dst->type)) {
24054 internal_error(state, ins, "Invalid destination register");
24055 }
24056 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24057 fprintf(state->errout, "src type: ");
24058 name_of(state->errout, src->type);
24059 fprintf(state->errout, "\n");
24060 fprintf(state->errout, "dst type: ");
24061 name_of(state->errout, dst->type);
24062 fprintf(state->errout, "\n");
24063 internal_error(state, ins, "Type mismatch for OP_COPY");
24064 }
24065
Eric Biederman0babc1c2003-05-09 02:39:00 +000024066 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024067 int src_reg, dst_reg;
24068 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024069 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024070 dst_reg = ID_REG(dst->id);
24071 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024072 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024073 /* If the class is the same just move the register */
24074 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024075 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024076 if ((src_reg != dst_reg) || !omit_copy) {
24077 fprintf(fp, "\tmov %s, %s\n",
24078 reg(state, src, src_regcm),
24079 reg(state, dst, dst_regcm));
24080 }
24081 }
24082 /* Move 32bit to 16bit */
24083 else if ((src_regcm & REGCM_GPR32) &&
24084 (dst_regcm & REGCM_GPR16)) {
24085 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24086 if ((src_reg != dst_reg) || !omit_copy) {
24087 fprintf(fp, "\tmovw %s, %s\n",
24088 arch_reg_str(src_reg),
24089 arch_reg_str(dst_reg));
24090 }
24091 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024092 /* Move from 32bit gprs to 16bit gprs */
24093 else if ((src_regcm & REGCM_GPR32) &&
24094 (dst_regcm & REGCM_GPR16)) {
24095 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24096 if ((src_reg != dst_reg) || !omit_copy) {
24097 fprintf(fp, "\tmov %s, %s\n",
24098 arch_reg_str(src_reg),
24099 arch_reg_str(dst_reg));
24100 }
24101 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024102 /* Move 32bit to 8bit */
24103 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024104 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024105 {
24106 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24107 if ((src_reg != dst_reg) || !omit_copy) {
24108 fprintf(fp, "\tmovb %s, %s\n",
24109 arch_reg_str(src_reg),
24110 arch_reg_str(dst_reg));
24111 }
24112 }
24113 /* Move 16bit to 8bit */
24114 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024115 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024116 {
24117 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24118 if ((src_reg != dst_reg) || !omit_copy) {
24119 fprintf(fp, "\tmovb %s, %s\n",
24120 arch_reg_str(src_reg),
24121 arch_reg_str(dst_reg));
24122 }
24123 }
24124 /* Move 8/16bit to 16/32bit */
Eric Biederman530b5192003-07-01 10:05:30 +000024125 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024126 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024127 const char *op;
24128 op = is_signed(src->type)? "movsx": "movzx";
24129 fprintf(fp, "\t%s %s, %s\n",
24130 op,
24131 reg(state, src, src_regcm),
24132 reg(state, dst, dst_regcm));
24133 }
24134 /* Move between sse registers */
24135 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24136 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024137 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024138 reg(state, src, src_regcm),
24139 reg(state, dst, dst_regcm));
24140 }
24141 }
Eric Biederman530b5192003-07-01 10:05:30 +000024142 /* Move between mmx registers */
24143 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024144 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024145 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024146 reg(state, src, src_regcm),
24147 reg(state, dst, dst_regcm));
24148 }
24149 }
Eric Biederman530b5192003-07-01 10:05:30 +000024150 /* Move from sse to mmx registers */
24151 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24152 fprintf(fp, "\tmovdq2q %s, %s\n",
24153 reg(state, src, src_regcm),
24154 reg(state, dst, dst_regcm));
24155 }
24156 /* Move from mmx to sse registers */
24157 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24158 fprintf(fp, "\tmovq2dq %s, %s\n",
24159 reg(state, src, src_regcm),
24160 reg(state, dst, dst_regcm));
24161 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024162 /* Move between 32bit gprs & mmx/sse registers */
24163 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24164 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24165 fprintf(fp, "\tmovd %s, %s\n",
24166 reg(state, src, src_regcm),
24167 reg(state, dst, dst_regcm));
24168 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024169 /* Move from 16bit gprs & mmx/sse registers */
24170 else if ((src_regcm & REGCM_GPR16) &&
24171 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24172 const char *op;
24173 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024174 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024175 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24176 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24177 op,
24178 arch_reg_str(src_reg),
24179 arch_reg_str(mid_reg),
24180 arch_reg_str(mid_reg),
24181 arch_reg_str(dst_reg));
24182 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024183 /* Move from mmx/sse registers to 16bit gprs */
24184 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24185 (dst_regcm & REGCM_GPR16)) {
24186 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24187 fprintf(fp, "\tmovd %s, %s\n",
24188 arch_reg_str(src_reg),
24189 arch_reg_str(dst_reg));
24190 }
Eric Biederman530b5192003-07-01 10:05:30 +000024191 /* Move from gpr to 64bit dividend */
24192 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24193 (dst_regcm & REGCM_DIVIDEND64)) {
24194 const char *extend;
24195 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24196 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
24197 arch_reg_str(src_reg),
24198 extend);
24199 }
24200 /* Move from 64bit gpr to gpr */
24201 else if ((src_regcm & REGCM_DIVIDEND64) &&
24202 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24203 if (dst_regcm & REGCM_GPR32) {
24204 src_reg = REG_EAX;
24205 }
24206 else if (dst_regcm & REGCM_GPR16) {
24207 src_reg = REG_AX;
24208 }
24209 else if (dst_regcm & REGCM_GPR8_LO) {
24210 src_reg = REG_AL;
24211 }
24212 fprintf(fp, "\tmov %s, %s\n",
24213 arch_reg_str(src_reg),
24214 arch_reg_str(dst_reg));
24215 }
24216 /* Move from mmx/sse registers to 64bit gpr */
24217 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24218 (dst_regcm & REGCM_DIVIDEND64)) {
24219 const char *extend;
24220 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24221 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24222 arch_reg_str(src_reg),
24223 extend);
24224 }
24225 /* Move from 64bit gpr to mmx/sse register */
24226 else if ((src_regcm & REGCM_DIVIDEND64) &&
24227 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24228 fprintf(fp, "\tmovd %%eax, %s\n",
24229 arch_reg_str(dst_reg));
24230 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024231#if X86_4_8BIT_GPRS
24232 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024233 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024234 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24235 const char *op;
24236 int mid_reg;
24237 op = is_signed(src->type)? "movsx":"movzx";
24238 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24239 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24240 op,
24241 reg(state, src, src_regcm),
24242 arch_reg_str(mid_reg),
24243 arch_reg_str(mid_reg),
24244 reg(state, dst, dst_regcm));
24245 }
24246 /* Move from mmx/sse registers and 8bit gprs */
24247 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024248 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024249 int mid_reg;
24250 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24251 fprintf(fp, "\tmovd %s, %s\n",
24252 reg(state, src, src_regcm),
24253 arch_reg_str(mid_reg));
24254 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024255 /* Move from 32bit gprs to 8bit gprs */
24256 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024257 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024258 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24259 if ((src_reg != dst_reg) || !omit_copy) {
24260 fprintf(fp, "\tmov %s, %s\n",
24261 arch_reg_str(src_reg),
24262 arch_reg_str(dst_reg));
24263 }
24264 }
24265 /* Move from 16bit gprs to 8bit gprs */
24266 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024267 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024268 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24269 if ((src_reg != dst_reg) || !omit_copy) {
24270 fprintf(fp, "\tmov %s, %s\n",
24271 arch_reg_str(src_reg),
24272 arch_reg_str(dst_reg));
24273 }
24274 }
24275#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024276 /* Move from %eax:%edx to %eax:%edx */
24277 else if ((src_regcm & REGCM_DIVIDEND64) &&
24278 (dst_regcm & REGCM_DIVIDEND64) &&
24279 (src_reg == dst_reg)) {
24280 if (!omit_copy) {
24281 fprintf(fp, "\t/*mov %s, %s*/\n",
24282 arch_reg_str(src_reg),
24283 arch_reg_str(dst_reg));
24284 }
24285 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024286 else {
Eric Biederman90089602004-05-28 14:11:54 +000024287 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24288 internal_error(state, ins, "attempt to copy from %%eflags!");
24289 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024290 internal_error(state, ins, "unknown copy type");
24291 }
24292 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024293 else {
Eric Biederman90089602004-05-28 14:11:54 +000024294 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024295 int dst_reg;
24296 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024297 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024298 dst_reg = ID_REG(dst->id);
24299 dst_regcm = arch_reg_regcm(state, dst_reg);
24300 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24301 fprintf(fp, "\tmov ");
24302 print_const_val(state, src, fp);
24303 fprintf(fp, ", %s\n",
24304 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24305 }
24306 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024307 if (dst_size > SIZEOF_I32) {
24308 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024309 }
24310 fprintf(fp, "\tmov $0, %%edx\n");
24311 fprintf(fp, "\tmov ");
24312 print_const_val(state, src, fp);
24313 fprintf(fp, ", %%eax\n");
24314 }
24315 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024316 if (dst_size > SIZEOF_I16) {
24317 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024318 }
24319 fprintf(fp, "\tmov $0, %%dx\n");
24320 fprintf(fp, "\tmov ");
24321 print_const_val(state, src, fp);
24322 fprintf(fp, ", %%ax");
24323 }
24324 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24325 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024326 if (dst_size > SIZEOF_I32) {
24327 internal_error(state, ins, "%d bit constant...", dst_size);
24328 }
24329 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024330 fprintf(fp, "\tmovd L%s%lu, %s\n",
24331 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024332 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24333 }
24334 else {
24335 internal_error(state, ins, "unknown copy immediate type");
24336 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024337 }
Eric Biederman90089602004-05-28 14:11:54 +000024338 /* Leave now if this is not a type conversion */
24339 if (ins->op != OP_CONVERT) {
24340 return;
24341 }
24342 /* Now make certain I have not logically overflowed the destination */
24343 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24344 (size_of(state, dst->type) < reg_size(state, dst)))
24345 {
24346 unsigned long mask;
24347 int dst_reg;
24348 int dst_regcm;
24349 if (size_of(state, dst->type) >= 32) {
24350 fprintf(state->errout, "dst type: ");
24351 name_of(state->errout, dst->type);
24352 fprintf(state->errout, "\n");
24353 internal_error(state, dst, "unhandled dst type size");
24354 }
24355 mask = 1;
24356 mask <<= size_of(state, dst->type);
24357 mask -= 1;
24358
24359 dst_reg = ID_REG(dst->id);
24360 dst_regcm = arch_reg_regcm(state, dst_reg);
24361
24362 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24363 fprintf(fp, "\tand $0x%lx, %s\n",
24364 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24365 }
24366 else if (dst_regcm & REGCM_MMX) {
24367 long ref;
24368 ref = get_mask_pool_ref(state, dst, mask, fp);
24369 fprintf(fp, "\tpand L%s%lu, %s\n",
24370 state->compiler->label_prefix, ref,
24371 reg(state, dst, REGCM_MMX));
24372 }
24373 else if (dst_regcm & REGCM_XMM) {
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_XMM));
24379 }
24380 else {
24381 fprintf(state->errout, "dst type: ");
24382 name_of(state->errout, dst->type);
24383 fprintf(state->errout, "\n");
24384 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24385 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24386 }
24387 }
24388 /* Make certain I am properly sign extended */
24389 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24390 (is_signed(src->type)))
24391 {
24392 int bits, reg_bits, shift_bits;
24393 int dst_reg;
24394 int dst_regcm;
24395
24396 bits = size_of(state, src->type);
24397 reg_bits = reg_size(state, dst);
24398 if (reg_bits > 32) {
24399 reg_bits = 32;
24400 }
24401 shift_bits = reg_bits - size_of(state, src->type);
24402 dst_reg = ID_REG(dst->id);
24403 dst_regcm = arch_reg_regcm(state, dst_reg);
24404
24405 if (shift_bits < 0) {
24406 internal_error(state, dst, "negative shift?");
24407 }
24408
24409 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24410 fprintf(fp, "\tshl $%d, %s\n",
24411 shift_bits,
24412 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24413 fprintf(fp, "\tsar $%d, %s\n",
24414 shift_bits,
24415 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24416 }
24417 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24418 fprintf(fp, "\tpslld $%d, %s\n",
24419 shift_bits,
24420 reg(state, dst, REGCM_MMX | REGCM_XMM));
24421 fprintf(fp, "\tpsrad $%d, %s\n",
24422 shift_bits,
24423 reg(state, dst, REGCM_MMX | REGCM_XMM));
24424 }
24425 else {
24426 fprintf(state->errout, "dst type: ");
24427 name_of(state->errout, dst->type);
24428 fprintf(state->errout, "\n");
24429 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24430 internal_error(state, dst, "failed to signed extend value");
24431 }
24432 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024433}
24434
24435static void print_op_load(struct compile_state *state,
24436 struct triple *ins, FILE *fp)
24437{
24438 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024439 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024440 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024441 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024442 if (is_const(src) || is_const(dst)) {
24443 internal_error(state, ins, "unknown load operation");
24444 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024445 switch(ins->type->type & TYPE_MASK) {
24446 case TYPE_CHAR: op = "movsbl"; break;
24447 case TYPE_UCHAR: op = "movzbl"; break;
24448 case TYPE_SHORT: op = "movswl"; break;
24449 case TYPE_USHORT: op = "movzwl"; break;
24450 case TYPE_INT: case TYPE_UINT:
24451 case TYPE_LONG: case TYPE_ULONG:
24452 case TYPE_POINTER:
24453 op = "movl";
24454 break;
24455 default:
24456 internal_error(state, ins, "unknown type in load");
24457 op = "<invalid opcode>";
24458 break;
24459 }
24460 fprintf(fp, "\t%s (%s), %s\n",
24461 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024462 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024463 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024464}
24465
24466
24467static void print_op_store(struct compile_state *state,
24468 struct triple *ins, FILE *fp)
24469{
24470 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024471 dst = RHS(ins, 0);
24472 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024473 if (is_const(src) && (src->op == OP_INTCONST)) {
24474 long_t value;
24475 value = (long_t)(src->u.cval);
24476 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24477 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024478 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024479 reg(state, dst, REGCM_GPR32));
24480 }
24481 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24482 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24483 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024484 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024485 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024486 }
24487 else {
24488 if (is_const(src) || is_const(dst)) {
24489 internal_error(state, ins, "unknown store operation");
24490 }
24491 fprintf(fp, "\tmov%s %s, (%s)\n",
24492 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024493 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024494 reg(state, dst, REGCM_GPR32));
24495 }
24496
24497
24498}
24499
24500static void print_op_smul(struct compile_state *state,
24501 struct triple *ins, FILE *fp)
24502{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024503 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024504 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024505 reg(state, RHS(ins, 1), REGCM_GPR32),
24506 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024507 }
24508 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024509 fprintf(fp, "\timul ");
24510 print_const_val(state, RHS(ins, 1), fp);
24511 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024512 }
24513}
24514
24515static void print_op_cmp(struct compile_state *state,
24516 struct triple *ins, FILE *fp)
24517{
24518 unsigned mask;
24519 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024520 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024521 dreg = check_reg(state, ins, REGCM_FLAGS);
24522 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24523 internal_error(state, ins, "bad dest register for cmp");
24524 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024525 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024526 fprintf(fp, "\tcmp ");
24527 print_const_val(state, RHS(ins, 1), fp);
24528 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024529 }
24530 else {
24531 unsigned lmask, rmask;
24532 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024533 lreg = check_reg(state, RHS(ins, 0), mask);
24534 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024535 lmask = arch_reg_regcm(state, lreg);
24536 rmask = arch_reg_regcm(state, rreg);
24537 mask = lmask & rmask;
24538 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024539 reg(state, RHS(ins, 1), mask),
24540 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024541 }
24542}
24543
24544static void print_op_test(struct compile_state *state,
24545 struct triple *ins, FILE *fp)
24546{
24547 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024548 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024549 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024550 reg(state, RHS(ins, 0), mask),
24551 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024552}
24553
24554static void print_op_branch(struct compile_state *state,
24555 struct triple *branch, FILE *fp)
24556{
24557 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024558 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024559 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024560 internal_error(state, branch, "jmp with condition?");
24561 }
24562 bop = "jmp";
24563 }
24564 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024565 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024566 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024567 internal_error(state, branch, "jmpcc without condition?");
24568 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024569 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24570 if ((RHS(branch, 0)->op != OP_CMP) &&
24571 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024572 internal_error(state, branch, "bad branch test");
24573 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024574#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024575#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024576#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024577 ptr = RHS(branch, 0);
24578 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24579 if (ptr->op != OP_COPY) {
24580 internal_error(state, branch, "branch does not follow test");
24581 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024582 }
24583 switch(branch->op) {
24584 case OP_JMP_EQ: bop = "jz"; break;
24585 case OP_JMP_NOTEQ: bop = "jnz"; break;
24586 case OP_JMP_SLESS: bop = "jl"; break;
24587 case OP_JMP_ULESS: bop = "jb"; break;
24588 case OP_JMP_SMORE: bop = "jg"; break;
24589 case OP_JMP_UMORE: bop = "ja"; break;
24590 case OP_JMP_SLESSEQ: bop = "jle"; break;
24591 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24592 case OP_JMP_SMOREEQ: bop = "jge"; break;
24593 case OP_JMP_UMOREEQ: bop = "jae"; break;
24594 default:
24595 internal_error(state, branch, "Invalid branch op");
24596 break;
24597 }
24598
24599 }
Eric Biederman90089602004-05-28 14:11:54 +000024600#if 1
24601 if (branch->op == OP_CALL) {
24602 fprintf(fp, "\t/* call */\n");
24603 }
24604#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024605 fprintf(fp, "\t%s L%s%lu\n",
24606 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024607 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024608 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024609}
24610
Eric Biederman5ade04a2003-10-22 04:03:46 +000024611static void print_op_ret(struct compile_state *state,
24612 struct triple *branch, FILE *fp)
24613{
24614 fprintf(fp, "\tjmp *%s\n",
24615 reg(state, RHS(branch, 0), REGCM_GPR32));
24616}
24617
Eric Biedermanb138ac82003-04-22 18:44:01 +000024618static void print_op_set(struct compile_state *state,
24619 struct triple *set, FILE *fp)
24620{
24621 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024622 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024623 internal_error(state, set, "setcc without condition?");
24624 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024625 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24626 if ((RHS(set, 0)->op != OP_CMP) &&
24627 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024628 internal_error(state, set, "bad set test");
24629 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024630 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024631 internal_error(state, set, "set does not follow test");
24632 }
24633 switch(set->op) {
24634 case OP_SET_EQ: sop = "setz"; break;
24635 case OP_SET_NOTEQ: sop = "setnz"; break;
24636 case OP_SET_SLESS: sop = "setl"; break;
24637 case OP_SET_ULESS: sop = "setb"; break;
24638 case OP_SET_SMORE: sop = "setg"; break;
24639 case OP_SET_UMORE: sop = "seta"; break;
24640 case OP_SET_SLESSEQ: sop = "setle"; break;
24641 case OP_SET_ULESSEQ: sop = "setbe"; break;
24642 case OP_SET_SMOREEQ: sop = "setge"; break;
24643 case OP_SET_UMOREEQ: sop = "setae"; break;
24644 default:
24645 internal_error(state, set, "Invalid set op");
24646 break;
24647 }
24648 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024649 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024650}
24651
24652static void print_op_bit_scan(struct compile_state *state,
24653 struct triple *ins, FILE *fp)
24654{
24655 const char *op;
24656 switch(ins->op) {
24657 case OP_BSF: op = "bsf"; break;
24658 case OP_BSR: op = "bsr"; break;
24659 default:
24660 internal_error(state, ins, "unknown bit scan");
24661 op = 0;
24662 break;
24663 }
24664 fprintf(fp,
24665 "\t%s %s, %s\n"
24666 "\tjnz 1f\n"
24667 "\tmovl $-1, %s\n"
24668 "1:\n",
24669 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024670 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024671 reg(state, ins, REGCM_GPR32),
24672 reg(state, ins, REGCM_GPR32));
24673}
24674
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024675
Eric Biedermanb138ac82003-04-22 18:44:01 +000024676static void print_sdecl(struct compile_state *state,
24677 struct triple *ins, FILE *fp)
24678{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024679 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024680 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman83b991a2003-10-11 06:20:25 +000024681 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024682 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024683 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024684 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024685
24686}
24687
24688static void print_instruction(struct compile_state *state,
24689 struct triple *ins, FILE *fp)
24690{
24691 /* Assumption: after I have exted the register allocator
24692 * everything is in a valid register.
24693 */
24694 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024695 case OP_ASM:
24696 print_op_asm(state, ins, fp);
24697 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024698 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24699 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24700 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24701 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24702 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24703 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24704 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24705 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24706 case OP_POS: break;
24707 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24708 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024709 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024710 case OP_INTCONST:
24711 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024712 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024713 /* Don't generate anything here for constants */
24714 case OP_PHI:
24715 /* Don't generate anything for variable declarations. */
24716 break;
Eric Biederman90089602004-05-28 14:11:54 +000024717 case OP_UNKNOWNVAL:
24718 fprintf(fp, " /* unknown %s */\n",
24719 reg(state, ins, REGCM_ALL));
24720 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024721 case OP_SDECL:
24722 print_sdecl(state, ins, fp);
24723 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024724 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024725 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024726 print_op_move(state, ins, fp);
24727 break;
24728 case OP_LOAD:
24729 print_op_load(state, ins, fp);
24730 break;
24731 case OP_STORE:
24732 print_op_store(state, ins, fp);
24733 break;
24734 case OP_SMUL:
24735 print_op_smul(state, ins, fp);
24736 break;
24737 case OP_CMP: print_op_cmp(state, ins, fp); break;
24738 case OP_TEST: print_op_test(state, ins, fp); break;
24739 case OP_JMP:
24740 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24741 case OP_JMP_SLESS: case OP_JMP_ULESS:
24742 case OP_JMP_SMORE: case OP_JMP_UMORE:
24743 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24744 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024745 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024746 print_op_branch(state, ins, fp);
24747 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024748 case OP_RET:
24749 print_op_ret(state, ins, fp);
24750 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024751 case OP_SET_EQ: case OP_SET_NOTEQ:
24752 case OP_SET_SLESS: case OP_SET_ULESS:
24753 case OP_SET_SMORE: case OP_SET_UMORE:
24754 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24755 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24756 print_op_set(state, ins, fp);
24757 break;
24758 case OP_INB: case OP_INW: case OP_INL:
24759 print_op_in(state, ins, fp);
24760 break;
24761 case OP_OUTB: case OP_OUTW: case OP_OUTL:
24762 print_op_out(state, ins, fp);
24763 break;
24764 case OP_BSF:
24765 case OP_BSR:
24766 print_op_bit_scan(state, ins, fp);
24767 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024768 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024769 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024770 fprintf(fp, "\trdmsr\n");
24771 break;
24772 case OP_WRMSR:
24773 fprintf(fp, "\twrmsr\n");
24774 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024775 case OP_HLT:
24776 fprintf(fp, "\thlt\n");
24777 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024778 case OP_SDIVT:
24779 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24780 break;
24781 case OP_UDIVT:
24782 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24783 break;
24784 case OP_UMUL:
24785 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24786 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024787 case OP_LABEL:
24788 if (!ins->use) {
24789 return;
24790 }
Eric Biederman83b991a2003-10-11 06:20:25 +000024791 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024792 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024793 break;
Eric Biederman90089602004-05-28 14:11:54 +000024794 case OP_ADECL:
24795 /* Ignore adecls with no registers error otherwise */
24796 if (!noop_adecl(ins)) {
24797 internal_error(state, ins, "adecl remains?");
24798 }
24799 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024800 /* Ignore OP_PIECE */
24801 case OP_PIECE:
24802 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024803 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024804 case OP_SDIV: case OP_UDIV:
24805 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024806 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24807 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24808 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24809 default:
24810 internal_error(state, ins, "unknown op: %d %s",
24811 ins->op, tops(ins->op));
24812 break;
24813 }
24814}
24815
24816static void print_instructions(struct compile_state *state)
24817{
24818 struct triple *first, *ins;
24819 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024820 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024821 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024822 int max_inline_depth;
24823 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024824 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024825 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024826 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024827 /* Masks for common sizes */
24828 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24829 fprintf(fp, ".balign 16\n");
24830 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24831 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24832 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24833 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024834 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024835 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024836 ins = first;
24837 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024838 if (print_location &&
24839 last_occurance != ins->occurance) {
24840 if (!ins->occurance->parent) {
24841 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024842 ins->occurance->function?ins->occurance->function:"(null)",
24843 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024844 ins->occurance->line,
24845 ins->occurance->col);
24846 }
24847 else {
24848 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024849 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024850 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024851 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024852 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024853 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024854 fprintf(fp, "\t * %s,%s:%d.%d\n",
24855 ptr->function,
24856 ptr->filename,
24857 ptr->line,
24858 ptr->col);
24859 }
24860 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024861 if (inline_depth > max_inline_depth) {
24862 max_inline_depth = inline_depth;
24863 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024864 }
24865 if (last_occurance) {
24866 put_occurance(last_occurance);
24867 }
24868 get_occurance(ins->occurance);
24869 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024870 }
24871
24872 print_instruction(state, ins, fp);
24873 ins = ins->next;
24874 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024875 if (print_location) {
24876 fprintf(fp, "/* max inline depth %d */\n",
24877 max_inline_depth);
24878 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024879}
Eric Biederman530b5192003-07-01 10:05:30 +000024880
Eric Biedermanb138ac82003-04-22 18:44:01 +000024881static void generate_code(struct compile_state *state)
24882{
24883 generate_local_labels(state);
24884 print_instructions(state);
24885
24886}
24887
Eric Biederman90089602004-05-28 14:11:54 +000024888static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024889{
Eric Biederman90089602004-05-28 14:11:54 +000024890 int tok;
24891 FILE *fp;
24892 int line;
24893 const char *filename;
24894 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024895 filename = 0;
24896 line = 0;
24897 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024898 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024899 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024900 const char *token_str;
24901 tok = peek(state);
24902 if (tok == TOK_EOF) {
24903 break;
24904 }
Eric Biederman41203d92004-11-08 09:31:09 +000024905 tk = eat(state, tok);
Eric Biederman90089602004-05-28 14:11:54 +000024906 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024907 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024908 tk->str_len ? tk->val.str :
24909 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024910
24911 file = state->file;
24912 while(file->macro && file->prev) {
24913 file = file->prev;
24914 }
24915 if (!file->macro &&
24916 ((file->line != line) || (file->basename != filename)))
24917 {
Eric Biederman90089602004-05-28 14:11:54 +000024918 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024919 if ((file->basename == filename) &&
24920 (line < file->line)) {
24921 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024922 fprintf(fp, "\n");
24923 line++;
24924 }
24925 }
24926 else {
24927 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024928 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024929 }
Eric Biederman132368b2004-11-09 08:59:23 +000024930 line = file->line;
24931 filename = file->basename;
24932 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024933 for(i = 0; i < col; i++) {
24934 fprintf(fp, " ");
24935 }
24936 }
24937
24938 fprintf(fp, "%s ", token_str);
24939
24940 if (state->compiler->debug & DEBUG_TOKENS) {
24941 loc(state->dbgout, state, 0);
24942 fprintf(state->dbgout, "%s <- `%s'\n",
24943 tokens[tok], token_str);
24944 }
24945 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024946}
24947
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024948static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024949 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024950{
24951 int i;
24952 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024953 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024954 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024955 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024956 state.compiler = compiler;
24957 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024958 state.file = 0;
24959 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24960 memset(&state.token[i], 0, sizeof(state.token[i]));
24961 state.token[i].tok = -1;
24962 }
Eric Biederman90089602004-05-28 14:11:54 +000024963 /* Remember the output descriptors */
24964 state.errout = stderr;
24965 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024966 /* Remember the output filename */
Eric Biederman5ade04a2003-10-22 04:03:46 +000024967 state.output = fopen(state.compiler->ofilename, "w");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024968 if (!state.output) {
24969 error(&state, 0, "Cannot open output file %s\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024970 state.compiler->ofilename);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024971 }
Eric Biederman90089602004-05-28 14:11:54 +000024972 /* Make certain a good cleanup happens */
24973 exit_state = &state;
24974 atexit(exit_cleanup);
24975
Eric Biedermanb138ac82003-04-22 18:44:01 +000024976 /* Prep the preprocessor */
24977 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024978 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024979 /* register the C keywords */
24980 register_keywords(&state);
24981 /* register the keywords the macro preprocessor knows */
24982 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024983 /* generate some builtin macros */
24984 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024985 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024986 state.i_switch = lookup(&state, "switch", 6);
24987 state.i_case = lookup(&state, "case", 4);
24988 state.i_continue = lookup(&state, "continue", 8);
24989 state.i_break = lookup(&state, "break", 5);
24990 state.i_default = lookup(&state, "default", 7);
24991 state.i_return = lookup(&state, "return", 6);
24992 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024993 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24994 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24995 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24996 /* Memorize where predefined identifiers are. */
24997 state.i___func__ = lookup(&state, "__func__", 8);
24998 /* Memorize where some attribute keywords are. */
24999 state.i_noinline = lookup(&state, "noinline", 8);
25000 state.i_always_inline = lookup(&state, "always_inline", 13);
Stefan Reinauerc89c4d42010-02-28 18:37:38 +000025001 state.i_noreturn = lookup(&state, "noreturn", 8);
Eric Biederman90089602004-05-28 14:11:54 +000025002
25003 /* Process the command line macros */
25004 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000025005
25006 /* Allocate beginning bounding labels for the function list */
25007 state.first = label(&state);
25008 state.first->id |= TRIPLE_FLAG_VOLATILE;
25009 use_triple(state.first, state.first);
25010 ptr = label(&state);
25011 ptr->id |= TRIPLE_FLAG_VOLATILE;
25012 use_triple(ptr, ptr);
25013 flatten(&state, state.first, ptr);
25014
Eric Biederman5ade04a2003-10-22 04:03:46 +000025015 /* Allocate a label for the pool of global variables */
25016 state.global_pool = label(&state);
25017 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25018 flatten(&state, state.first, state.global_pool);
25019
Eric Biedermanb138ac82003-04-22 18:44:01 +000025020 /* Enter the globl definition scope */
25021 start_scope(&state);
25022 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025023
Eric Biedermanb138ac82003-04-22 18:44:01 +000025024 compile_file(&state, filename, 1);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025025
25026 while (includes) {
25027 compile_file(&state, includes->filename, 1);
25028 includes=includes->next;
25029 }
Eric Biederman90089602004-05-28 14:11:54 +000025030
25031 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025032 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025033 print_preprocessed_tokens(&state);
25034 return;
25035 }
25036
Eric Biedermanb138ac82003-04-22 18:44:01 +000025037 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025038
Eric Biedermanb138ac82003-04-22 18:44:01 +000025039 /* Exit the global definition scope */
25040 end_scope(&state);
25041
25042 /* Now that basic compilation has happened
25043 * optimize the intermediate code
25044 */
25045 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025046
Eric Biedermanb138ac82003-04-22 18:44:01 +000025047 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025048 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025049 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025050 }
Eric Biederman90089602004-05-28 14:11:54 +000025051 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025052}
25053
Eric Biederman90089602004-05-28 14:11:54 +000025054static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025055{
Eric Biederman90089602004-05-28 14:11:54 +000025056 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025057}
25058
25059static void usage(void)
25060{
Eric Biederman90089602004-05-28 14:11:54 +000025061 FILE *fp = stdout;
25062 version(fp);
25063 fprintf(fp,
25064 "\nUsage: romcc [options] <source>.c\n"
25065 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25066 "Options: \n"
25067 "-o <output file name>\n"
25068 "-f<option> Specify a generic compiler option\n"
25069 "-m<option> Specify a arch dependent option\n"
25070 "-- Specify this is the last option\n"
25071 "\nGeneric compiler options:\n"
25072 );
25073 compiler_usage(fp);
25074 fprintf(fp,
25075 "\nArchitecture compiler options:\n"
25076 );
25077 arch_usage(fp);
25078 fprintf(fp,
25079 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025080 );
25081}
25082
25083static void arg_error(char *fmt, ...)
25084{
25085 va_list args;
25086 va_start(args, fmt);
25087 vfprintf(stderr, fmt, args);
25088 va_end(args);
25089 usage();
25090 exit(1);
25091}
25092
25093int main(int argc, char **argv)
25094{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025095 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025096 struct compiler_state compiler;
25097 struct arch_state arch;
25098 int all_opts;
Eric Biederman90089602004-05-28 14:11:54 +000025099
25100
25101 /* I don't want any surprises */
25102 setlocale(LC_ALL, "C");
25103
Eric Biederman5ade04a2003-10-22 04:03:46 +000025104 init_compiler_state(&compiler);
25105 init_arch_state(&arch);
25106 filename = 0;
25107 all_opts = 0;
25108 while(argc > 1) {
25109 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25110 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025111 argv += 2;
25112 argc -= 2;
25113 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025114 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025115 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025116 result = -1;
25117 if (strcmp(argv[1], "--") == 0) {
25118 result = 0;
25119 all_opts = 1;
25120 }
Eric Biederman90089602004-05-28 14:11:54 +000025121 else if (strncmp(argv[1], "-E", 2) == 0) {
25122 result = compiler_encode_flag(&compiler, argv[1]);
25123 }
25124 else if (strncmp(argv[1], "-O", 2) == 0) {
25125 result = compiler_encode_flag(&compiler, argv[1]);
25126 }
25127 else if (strncmp(argv[1], "-I", 2) == 0) {
25128 result = compiler_encode_flag(&compiler, argv[1]);
25129 }
25130 else if (strncmp(argv[1], "-D", 2) == 0) {
25131 result = compiler_encode_flag(&compiler, argv[1]);
25132 }
25133 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025134 result = compiler_encode_flag(&compiler, argv[1]);
25135 }
25136 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25137 result = compiler_encode_flag(&compiler, argv[1]+2);
25138 }
25139 else if (strncmp(argv[1], "-f", 2) == 0) {
25140 result = compiler_encode_flag(&compiler, argv[1]+2);
25141 }
25142 else if (strncmp(argv[1], "-m", 2) == 0) {
25143 result = arch_encode_flag(&arch, argv[1]+2);
25144 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025145 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025146 struct filelist *old_head = include_filelist;
25147 include_filelist = malloc(sizeof(struct filelist));
25148 if (!include_filelist) {
25149 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025150 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025151 argv++;
25152 argc--;
25153 include_filelist->filename = argv[1];
25154 include_filelist->next = old_head;
25155 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025156 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025157 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025158 arg_error("Invalid option specified: %s\n",
25159 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025160 }
25161 argv++;
25162 argc--;
25163 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025164 else {
25165 if (filename) {
25166 arg_error("Only one filename may be specified\n");
25167 }
25168 filename = argv[1];
25169 argv++;
25170 argc--;
25171 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025172 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025173 if (!filename) {
25174 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025175 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025176 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025177
25178 return 0;
25179}