blob: ff11058da47e0c6058dd12c4177a4d4174521f91 [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;
Eric Biederman90089602004-05-28 14:11:54 +00001086 /* Additional hash entries for predefined macros */
1087 struct hash_entry *i_defined;
1088 struct hash_entry *i___VA_ARGS__;
1089 struct hash_entry *i___FILE__;
1090 struct hash_entry *i___LINE__;
1091 /* Additional hash entries for predefined identifiers */
1092 struct hash_entry *i___func__;
1093 /* Additional hash entries for attributes */
1094 struct hash_entry *i_noinline;
1095 struct hash_entry *i_always_inline;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001096 int scope_depth;
Eric Biedermancb364952004-11-15 10:46:44 +00001097 unsigned char if_bytes[(MAX_PP_IF_DEPTH + CHAR_BIT -1)/CHAR_BIT];
Eric Biederman90089602004-05-28 14:11:54 +00001098 int if_depth;
1099 int eat_depth, eat_targ;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001100 struct file_state *macro_file;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001101 struct triple *functions;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001102 struct triple *main_function;
Eric Biederman83b991a2003-10-11 06:20:25 +00001103 struct triple *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001104 struct triple *global_pool;
Eric Biederman90089602004-05-28 14:11:54 +00001105 struct basic_blocks bb;
1106 int functions_joined;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001107};
1108
Eric Biederman0babc1c2003-05-09 02:39:00 +00001109/* visibility global/local */
1110/* static/auto duration */
1111/* typedef, register, inline */
1112#define STOR_SHIFT 0
Eric Biederman5ade04a2003-10-22 04:03:46 +00001113#define STOR_MASK 0x001f
Eric Biederman0babc1c2003-05-09 02:39:00 +00001114/* Visibility */
1115#define STOR_GLOBAL 0x0001
1116/* Duration */
1117#define STOR_PERM 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001118/* Definition locality */
1119#define STOR_NONLOCAL 0x0004 /* The definition is not in this translation unit */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001120/* Storage specifiers */
1121#define STOR_AUTO 0x0000
1122#define STOR_STATIC 0x0002
Eric Biederman5ade04a2003-10-22 04:03:46 +00001123#define STOR_LOCAL 0x0003
1124#define STOR_EXTERN 0x0007
1125#define STOR_INLINE 0x0008
1126#define STOR_REGISTER 0x0010
1127#define STOR_TYPEDEF 0x0018
Eric Biederman0babc1c2003-05-09 02:39:00 +00001128
Eric Biederman5ade04a2003-10-22 04:03:46 +00001129#define QUAL_SHIFT 5
1130#define QUAL_MASK 0x00e0
Eric Biederman0babc1c2003-05-09 02:39:00 +00001131#define QUAL_NONE 0x0000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001132#define QUAL_CONST 0x0020
1133#define QUAL_VOLATILE 0x0040
1134#define QUAL_RESTRICT 0x0080
Eric Biederman0babc1c2003-05-09 02:39:00 +00001135
1136#define TYPE_SHIFT 8
1137#define TYPE_MASK 0x1f00
Eric Biederman90089602004-05-28 14:11:54 +00001138#define TYPE_INTEGER(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_ULLONG)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
1139#define TYPE_ARITHMETIC(TYPE) ((((TYPE) >= TYPE_CHAR) && ((TYPE) <= TYPE_LDOUBLE)) || ((TYPE) == TYPE_ENUM) || ((TYPE) == TYPE_BITFIELD))
Eric Biederman0babc1c2003-05-09 02:39:00 +00001140#define TYPE_UNSIGNED(TYPE) ((TYPE) & 0x0100)
1141#define TYPE_SIGNED(TYPE) (!TYPE_UNSIGNED(TYPE))
Eric Biederman83b991a2003-10-11 06:20:25 +00001142#define TYPE_MKUNSIGNED(TYPE) (((TYPE) & ~0xF000) | 0x0100)
1143#define TYPE_RANK(TYPE) ((TYPE) & ~0xF1FF)
Eric Biederman0babc1c2003-05-09 02:39:00 +00001144#define TYPE_PTR(TYPE) (((TYPE) & TYPE_MASK) == TYPE_POINTER)
1145#define TYPE_DEFAULT 0x0000
1146#define TYPE_VOID 0x0100
1147#define TYPE_CHAR 0x0200
1148#define TYPE_UCHAR 0x0300
1149#define TYPE_SHORT 0x0400
1150#define TYPE_USHORT 0x0500
1151#define TYPE_INT 0x0600
1152#define TYPE_UINT 0x0700
1153#define TYPE_LONG 0x0800
1154#define TYPE_ULONG 0x0900
1155#define TYPE_LLONG 0x0a00 /* long long */
1156#define TYPE_ULLONG 0x0b00
1157#define TYPE_FLOAT 0x0c00
1158#define TYPE_DOUBLE 0x0d00
1159#define TYPE_LDOUBLE 0x0e00 /* long double */
Eric Biederman83b991a2003-10-11 06:20:25 +00001160
1161/* Note: TYPE_ENUM is chosen very carefully so TYPE_RANK works */
1162#define TYPE_ENUM 0x1600
1163#define TYPE_LIST 0x1700
1164/* TYPE_LIST is a basic building block when defining enumerations
1165 * type->field_ident holds the name of this enumeration entry.
1166 * type->right holds the entry in the list.
1167 */
1168
Eric Biederman0babc1c2003-05-09 02:39:00 +00001169#define TYPE_STRUCT 0x1000
Eric Biederman90089602004-05-28 14:11:54 +00001170/* For TYPE_STRUCT
1171 * type->left holds the link list of TYPE_PRODUCT entries that
1172 * make up the structure.
1173 * type->elements hold the length of the linked list
1174 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001175#define TYPE_UNION 0x1100
Eric Biederman90089602004-05-28 14:11:54 +00001176/* For TYPE_UNION
1177 * type->left holds the link list of TYPE_OVERLAP entries that
1178 * make up the union.
1179 * type->elements hold the length of the linked list
1180 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00001181#define TYPE_POINTER 0x1200
1182/* For TYPE_POINTER:
1183 * type->left holds the type pointed to.
1184 */
1185#define TYPE_FUNCTION 0x1300
1186/* For TYPE_FUNCTION:
1187 * type->left holds the return type.
Eric Biederman90089602004-05-28 14:11:54 +00001188 * type->right holds the type of the arguments
1189 * type->elements holds the count of the arguments
Eric Biederman0babc1c2003-05-09 02:39:00 +00001190 */
1191#define TYPE_PRODUCT 0x1400
1192/* TYPE_PRODUCT is a basic building block when defining structures
1193 * type->left holds the type that appears first in memory.
1194 * type->right holds the type that appears next in memory.
1195 */
1196#define TYPE_OVERLAP 0x1500
1197/* TYPE_OVERLAP is a basic building block when defining unions
1198 * type->left and type->right holds to types that overlap
1199 * each other in memory.
1200 */
Eric Biederman83b991a2003-10-11 06:20:25 +00001201#define TYPE_ARRAY 0x1800
Eric Biederman0babc1c2003-05-09 02:39:00 +00001202/* TYPE_ARRAY is a basic building block when definitng arrays.
1203 * type->left holds the type we are an array of.
Eric Biederman90089602004-05-28 14:11:54 +00001204 * type->elements holds the number of elements.
Eric Biederman0babc1c2003-05-09 02:39:00 +00001205 */
Eric Biederman90089602004-05-28 14:11:54 +00001206#define TYPE_TUPLE 0x1900
1207/* TYPE_TUPLE is a basic building block when defining
1208 * positionally reference type conglomerations. (i.e. closures)
1209 * In essence it is a wrapper for TYPE_PRODUCT, like TYPE_STRUCT
1210 * except it has no field names.
1211 * type->left holds the liked list of TYPE_PRODUCT entries that
1212 * make up the closure type.
1213 * type->elements hold the number of elements in the closure.
1214 */
1215#define TYPE_JOIN 0x1a00
1216/* TYPE_JOIN is a basic building block when defining
1217 * positionally reference type conglomerations. (i.e. closures)
1218 * In essence it is a wrapper for TYPE_OVERLAP, like TYPE_UNION
1219 * except it has no field names.
1220 * type->left holds the liked list of TYPE_OVERLAP entries that
1221 * make up the closure type.
1222 * type->elements hold the number of elements in the closure.
1223 */
1224#define TYPE_BITFIELD 0x1b00
1225/* TYPE_BITFIED is the type of a bitfield.
1226 * type->left holds the type basic type TYPE_BITFIELD is derived from.
1227 * type->elements holds the number of bits in the bitfield.
1228 */
1229#define TYPE_UNKNOWN 0x1c00
1230/* TYPE_UNKNOWN is the type of an unknown value.
1231 * Used on unknown consts and other places where I don't know the type.
1232 */
1233
1234#define ATTRIB_SHIFT 16
1235#define ATTRIB_MASK 0xffff0000
1236#define ATTRIB_NOINLINE 0x00010000
1237#define ATTRIB_ALWAYS_INLINE 0x00020000
Eric Biederman0babc1c2003-05-09 02:39:00 +00001238
Eric Biederman83b991a2003-10-11 06:20:25 +00001239#define ELEMENT_COUNT_UNSPECIFIED ULONG_T_MAX
Eric Biederman0babc1c2003-05-09 02:39:00 +00001240
1241struct type {
1242 unsigned int type;
1243 struct type *left, *right;
1244 ulong_t elements;
1245 struct hash_entry *field_ident;
1246 struct hash_entry *type_ident;
1247};
1248
Eric Biederman530b5192003-07-01 10:05:30 +00001249#define TEMPLATE_BITS 7
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001250#define MAX_TEMPLATES (1<<TEMPLATE_BITS)
Eric Biederman83b991a2003-10-11 06:20:25 +00001251#define MAX_REG_EQUIVS 16
Eric Biederman530b5192003-07-01 10:05:30 +00001252#define MAX_REGC 14
Eric Biederman83b991a2003-10-11 06:20:25 +00001253#define MAX_REGISTERS 75
1254#define REGISTER_BITS 7
1255#define MAX_VIRT_REGISTERS (1<<REGISTER_BITS)
Eric Biederman90089602004-05-28 14:11:54 +00001256#define REG_ERROR 0
1257#define REG_UNSET 1
1258#define REG_UNNEEDED 2
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001259#define REG_VIRT0 (MAX_REGISTERS + 0)
1260#define REG_VIRT1 (MAX_REGISTERS + 1)
1261#define REG_VIRT2 (MAX_REGISTERS + 2)
1262#define REG_VIRT3 (MAX_REGISTERS + 3)
1263#define REG_VIRT4 (MAX_REGISTERS + 4)
1264#define REG_VIRT5 (MAX_REGISTERS + 5)
Eric Biederman83b991a2003-10-11 06:20:25 +00001265#define REG_VIRT6 (MAX_REGISTERS + 6)
1266#define REG_VIRT7 (MAX_REGISTERS + 7)
1267#define REG_VIRT8 (MAX_REGISTERS + 8)
1268#define REG_VIRT9 (MAX_REGISTERS + 9)
1269
1270#if (MAX_REGISTERS + 9) > MAX_VIRT_REGISTERS
1271#error "MAX_VIRT_REGISTERS to small"
1272#endif
Eric Biederman90089602004-05-28 14:11:54 +00001273#if (MAX_REGC + REGISTER_BITS) >= 26
Eric Biederman83b991a2003-10-11 06:20:25 +00001274#error "Too many id bits used"
1275#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00001276
1277/* Provision for 8 register classes */
Eric Biedermanf96a8102003-06-16 16:57:34 +00001278#define REG_SHIFT 0
1279#define REGC_SHIFT REGISTER_BITS
1280#define REGC_MASK (((1 << MAX_REGC) - 1) << REGISTER_BITS)
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001281#define REG_MASK (MAX_VIRT_REGISTERS -1)
1282#define ID_REG(ID) ((ID) & REG_MASK)
1283#define SET_REG(ID, REG) ((ID) = (((ID) & ~REG_MASK) | ((REG) & REG_MASK)))
Eric Biedermanf96a8102003-06-16 16:57:34 +00001284#define ID_REGCM(ID) (((ID) & REGC_MASK) >> REGC_SHIFT)
1285#define SET_REGCM(ID, REGCM) ((ID) = (((ID) & ~REGC_MASK) | (((REGCM) << REGC_SHIFT) & REGC_MASK)))
1286#define SET_INFO(ID, INFO) ((ID) = (((ID) & ~(REG_MASK | REGC_MASK)) | \
1287 (((INFO).reg) & REG_MASK) | ((((INFO).regcm) << REGC_SHIFT) & REGC_MASK)))
Eric Biedermanb138ac82003-04-22 18:44:01 +00001288
Eric Biederman90089602004-05-28 14:11:54 +00001289#define ARCH_INPUT_REGS 4
1290#define ARCH_OUTPUT_REGS 4
1291
1292static const struct reg_info arch_input_regs[ARCH_INPUT_REGS];
1293static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS];
Eric Biedermanb138ac82003-04-22 18:44:01 +00001294static unsigned arch_reg_regcm(struct compile_state *state, int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001295static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermand1ea5392003-06-28 06:49:45 +00001296static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001297static void arch_reg_equivs(
1298 struct compile_state *state, unsigned *equiv, int reg);
1299static int arch_select_free_register(
1300 struct compile_state *state, char *used, int classes);
1301static unsigned arch_regc_size(struct compile_state *state, int class);
1302static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2);
1303static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type);
1304static const char *arch_reg_str(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001305static struct reg_info arch_reg_constraint(
1306 struct compile_state *state, struct type *type, const char *constraint);
1307static struct reg_info arch_reg_clobber(
1308 struct compile_state *state, const char *clobber);
1309static struct reg_info arch_reg_lhs(struct compile_state *state,
1310 struct triple *ins, int index);
1311static struct reg_info arch_reg_rhs(struct compile_state *state,
1312 struct triple *ins, int index);
Eric Biederman90089602004-05-28 14:11:54 +00001313static int arch_reg_size(int reg);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001314static struct triple *transform_to_arch_instruction(
1315 struct compile_state *state, struct triple *ins);
Eric Biederman90089602004-05-28 14:11:54 +00001316static struct triple *flatten(
1317 struct compile_state *state, struct triple *first, struct triple *ptr);
Jason Schildt27b85112005-08-10 14:31:52 +00001318static void print_dominators(struct compile_state *state,
1319 FILE *fp, struct basic_blocks *bb);
1320static void print_dominance_frontiers(struct compile_state *state,
1321 FILE *fp, struct basic_blocks *bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001322
1323
Eric Biedermanb138ac82003-04-22 18:44:01 +00001324
Eric Biederman5ade04a2003-10-22 04:03:46 +00001325#define DEBUG_ABORT_ON_ERROR 0x00000001
1326#define DEBUG_BASIC_BLOCKS 0x00000002
1327#define DEBUG_FDOMINATORS 0x00000004
1328#define DEBUG_RDOMINATORS 0x00000008
1329#define DEBUG_TRIPLES 0x00000010
1330#define DEBUG_INTERFERENCE 0x00000020
1331#define DEBUG_SCC_TRANSFORM 0x00000040
1332#define DEBUG_SCC_TRANSFORM2 0x00000080
1333#define DEBUG_REBUILD_SSA_FORM 0x00000100
1334#define DEBUG_INLINE 0x00000200
1335#define DEBUG_RANGE_CONFLICTS 0x00000400
1336#define DEBUG_RANGE_CONFLICTS2 0x00000800
1337#define DEBUG_COLOR_GRAPH 0x00001000
1338#define DEBUG_COLOR_GRAPH2 0x00002000
1339#define DEBUG_COALESCING 0x00004000
1340#define DEBUG_COALESCING2 0x00008000
Eric Biederman90089602004-05-28 14:11:54 +00001341#define DEBUG_VERIFICATION 0x00010000
1342#define DEBUG_CALLS 0x00020000
1343#define DEBUG_CALLS2 0x00040000
1344#define DEBUG_TOKENS 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001345
1346#define DEBUG_DEFAULT ( \
1347 DEBUG_ABORT_ON_ERROR | \
1348 DEBUG_BASIC_BLOCKS | \
1349 DEBUG_FDOMINATORS | \
1350 DEBUG_RDOMINATORS | \
1351 DEBUG_TRIPLES | \
1352 0 )
1353
Eric Biederman90089602004-05-28 14:11:54 +00001354#define DEBUG_ALL ( \
1355 DEBUG_ABORT_ON_ERROR | \
1356 DEBUG_BASIC_BLOCKS | \
1357 DEBUG_FDOMINATORS | \
1358 DEBUG_RDOMINATORS | \
1359 DEBUG_TRIPLES | \
1360 DEBUG_INTERFERENCE | \
1361 DEBUG_SCC_TRANSFORM | \
1362 DEBUG_SCC_TRANSFORM2 | \
1363 DEBUG_REBUILD_SSA_FORM | \
1364 DEBUG_INLINE | \
1365 DEBUG_RANGE_CONFLICTS | \
1366 DEBUG_RANGE_CONFLICTS2 | \
1367 DEBUG_COLOR_GRAPH | \
1368 DEBUG_COLOR_GRAPH2 | \
1369 DEBUG_COALESCING | \
1370 DEBUG_COALESCING2 | \
1371 DEBUG_VERIFICATION | \
1372 DEBUG_CALLS | \
1373 DEBUG_CALLS2 | \
1374 DEBUG_TOKENS | \
1375 0 )
1376
1377#define COMPILER_INLINE_MASK 0x00000007
1378#define COMPILER_INLINE_ALWAYS 0x00000000
1379#define COMPILER_INLINE_NEVER 0x00000001
1380#define COMPILER_INLINE_DEFAULTON 0x00000002
1381#define COMPILER_INLINE_DEFAULTOFF 0x00000003
1382#define COMPILER_INLINE_NOPENALTY 0x00000004
1383#define COMPILER_ELIMINATE_INEFECTUAL_CODE 0x00000008
1384#define COMPILER_SIMPLIFY 0x00000010
1385#define COMPILER_SCC_TRANSFORM 0x00000020
1386#define COMPILER_SIMPLIFY_OP 0x00000040
1387#define COMPILER_SIMPLIFY_PHI 0x00000080
1388#define COMPILER_SIMPLIFY_LABEL 0x00000100
1389#define COMPILER_SIMPLIFY_BRANCH 0x00000200
1390#define COMPILER_SIMPLIFY_COPY 0x00000400
1391#define COMPILER_SIMPLIFY_ARITH 0x00000800
1392#define COMPILER_SIMPLIFY_SHIFT 0x00001000
1393#define COMPILER_SIMPLIFY_BITWISE 0x00002000
1394#define COMPILER_SIMPLIFY_LOGICAL 0x00004000
1395#define COMPILER_SIMPLIFY_BITFIELD 0x00008000
1396
Eric Biedermancb364952004-11-15 10:46:44 +00001397#define COMPILER_TRIGRAPHS 0x40000000
1398#define COMPILER_PP_ONLY 0x80000000
Eric Biederman5ade04a2003-10-22 04:03:46 +00001399
1400#define COMPILER_DEFAULT_FLAGS ( \
Eric Biedermancb364952004-11-15 10:46:44 +00001401 COMPILER_TRIGRAPHS | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001402 COMPILER_ELIMINATE_INEFECTUAL_CODE | \
Eric Biederman90089602004-05-28 14:11:54 +00001403 COMPILER_INLINE_DEFAULTON | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001404 COMPILER_SIMPLIFY_OP | \
1405 COMPILER_SIMPLIFY_PHI | \
1406 COMPILER_SIMPLIFY_LABEL | \
1407 COMPILER_SIMPLIFY_BRANCH | \
1408 COMPILER_SIMPLIFY_COPY | \
1409 COMPILER_SIMPLIFY_ARITH | \
1410 COMPILER_SIMPLIFY_SHIFT | \
1411 COMPILER_SIMPLIFY_BITWISE | \
1412 COMPILER_SIMPLIFY_LOGICAL | \
Eric Biederman90089602004-05-28 14:11:54 +00001413 COMPILER_SIMPLIFY_BITFIELD | \
Eric Biederman5ade04a2003-10-22 04:03:46 +00001414 0 )
Eric Biedermanb138ac82003-04-22 18:44:01 +00001415
Eric Biederman153ea352003-06-20 14:43:20 +00001416#define GLOBAL_SCOPE_DEPTH 1
1417#define FUNCTION_SCOPE_DEPTH (GLOBAL_SCOPE_DEPTH + 1)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001418
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001419static void compile_file(struct compile_state *old_state, const char *filename, int local);
1420
Eric Biederman5ade04a2003-10-22 04:03:46 +00001421
1422
1423static void init_compiler_state(struct compiler_state *compiler)
1424{
1425 memset(compiler, 0, sizeof(*compiler));
1426 compiler->label_prefix = "";
1427 compiler->ofilename = "auto.inc";
1428 compiler->flags = COMPILER_DEFAULT_FLAGS;
1429 compiler->debug = 0;
1430 compiler->max_allocation_passes = MAX_ALLOCATION_PASSES;
Eric Biederman90089602004-05-28 14:11:54 +00001431 compiler->include_path_count = 1;
1432 compiler->include_paths = xcmalloc(sizeof(char *), "include_paths");
1433 compiler->define_count = 1;
1434 compiler->defines = xcmalloc(sizeof(char *), "defines");
1435 compiler->undef_count = 1;
1436 compiler->undefs = xcmalloc(sizeof(char *), "undefs");
Eric Biederman5ade04a2003-10-22 04:03:46 +00001437}
1438
1439struct compiler_flag {
1440 const char *name;
1441 unsigned long flag;
1442};
Eric Biederman90089602004-05-28 14:11:54 +00001443
1444struct compiler_arg {
1445 const char *name;
1446 unsigned long mask;
1447 struct compiler_flag flags[16];
1448};
1449
Eric Biederman5ade04a2003-10-22 04:03:46 +00001450static int set_flag(
1451 const struct compiler_flag *ptr, unsigned long *flags,
1452 int act, const char *flag)
1453{
1454 int result = -1;
1455 for(; ptr->name; ptr++) {
1456 if (strcmp(ptr->name, flag) == 0) {
1457 break;
1458 }
1459 }
1460 if (ptr->name) {
1461 result = 0;
1462 *flags &= ~(ptr->flag);
1463 if (act) {
1464 *flags |= ptr->flag;
1465 }
1466 }
1467 return result;
1468}
1469
Eric Biederman90089602004-05-28 14:11:54 +00001470static int set_arg(
1471 const struct compiler_arg *ptr, unsigned long *flags, const char *arg)
1472{
1473 const char *val;
1474 int result = -1;
1475 int len;
1476 val = strchr(arg, '=');
1477 if (val) {
1478 len = val - arg;
1479 val++;
1480 for(; ptr->name; ptr++) {
1481 if (strncmp(ptr->name, arg, len) == 0) {
1482 break;
1483 }
1484 }
1485 if (ptr->name) {
1486 *flags &= ~ptr->mask;
1487 result = set_flag(&ptr->flags[0], flags, 1, val);
1488 }
1489 }
1490 return result;
1491}
1492
1493
1494static void flag_usage(FILE *fp, const struct compiler_flag *ptr,
1495 const char *prefix, const char *invert_prefix)
1496{
1497 for(;ptr->name; ptr++) {
1498 fprintf(fp, "%s%s\n", prefix, ptr->name);
1499 if (invert_prefix) {
1500 fprintf(fp, "%s%s\n", invert_prefix, ptr->name);
1501 }
1502 }
1503}
1504
1505static void arg_usage(FILE *fp, const struct compiler_arg *ptr,
1506 const char *prefix)
1507{
1508 for(;ptr->name; ptr++) {
1509 const struct compiler_flag *flag;
1510 for(flag = &ptr->flags[0]; flag->name; flag++) {
1511 fprintf(fp, "%s%s=%s\n",
1512 prefix, ptr->name, flag->name);
1513 }
1514 }
1515}
1516
1517static int append_string(size_t *max, const char ***vec, const char *str,
1518 const char *name)
1519{
1520 size_t count;
1521 count = ++(*max);
1522 *vec = xrealloc(*vec, sizeof(char *)*count, "name");
1523 (*vec)[count -1] = 0;
1524 (*vec)[count -2] = str;
1525 return 0;
1526}
1527
1528static void arg_error(char *fmt, ...);
1529static const char *identifier(const char *str, const char *end);
1530
1531static int append_include_path(struct compiler_state *compiler, const char *str)
1532{
1533 int result;
1534 if (!exists(str, ".")) {
1535 arg_error("Nonexistent include path: `%s'\n",
1536 str);
1537 }
1538 result = append_string(&compiler->include_path_count,
1539 &compiler->include_paths, str, "include_paths");
1540 return result;
1541}
1542
1543static int append_define(struct compiler_state *compiler, const char *str)
1544{
1545 const char *end, *rest;
1546 int result;
1547
1548 end = strchr(str, '=');
1549 if (!end) {
1550 end = str + strlen(str);
1551 }
1552 rest = identifier(str, end);
1553 if (rest != end) {
1554 int len = end - str - 1;
1555 arg_error("Invalid name cannot define macro: `%*.*s'\n",
1556 len, len, str);
1557 }
1558 result = append_string(&compiler->define_count,
1559 &compiler->defines, str, "defines");
1560 return result;
1561}
1562
1563static int append_undef(struct compiler_state *compiler, const char *str)
1564{
1565 const char *end, *rest;
1566 int result;
1567
1568 end = str + strlen(str);
1569 rest = identifier(str, end);
1570 if (rest != end) {
1571 int len = end - str - 1;
1572 arg_error("Invalid name cannot undefine macro: `%*.*s'\n",
1573 len, len, str);
1574 }
1575 result = append_string(&compiler->undef_count,
1576 &compiler->undefs, str, "undefs");
1577 return result;
1578}
1579
1580static const struct compiler_flag romcc_flags[] = {
Eric Biedermancb364952004-11-15 10:46:44 +00001581 { "trigraphs", COMPILER_TRIGRAPHS },
1582 { "pp-only", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001583 { "eliminate-inefectual-code", COMPILER_ELIMINATE_INEFECTUAL_CODE },
1584 { "simplify", COMPILER_SIMPLIFY },
1585 { "scc-transform", COMPILER_SCC_TRANSFORM },
1586 { "simplify-op", COMPILER_SIMPLIFY_OP },
1587 { "simplify-phi", COMPILER_SIMPLIFY_PHI },
1588 { "simplify-label", COMPILER_SIMPLIFY_LABEL },
1589 { "simplify-branch", COMPILER_SIMPLIFY_BRANCH },
1590 { "simplify-copy", COMPILER_SIMPLIFY_COPY },
1591 { "simplify-arith", COMPILER_SIMPLIFY_ARITH },
1592 { "simplify-shift", COMPILER_SIMPLIFY_SHIFT },
1593 { "simplify-bitwise", COMPILER_SIMPLIFY_BITWISE },
1594 { "simplify-logical", COMPILER_SIMPLIFY_LOGICAL },
1595 { "simplify-bitfield", COMPILER_SIMPLIFY_BITFIELD },
1596 { 0, 0 },
1597};
1598static const struct compiler_arg romcc_args[] = {
1599 { "inline-policy", COMPILER_INLINE_MASK,
1600 {
1601 { "always", COMPILER_INLINE_ALWAYS, },
1602 { "never", COMPILER_INLINE_NEVER, },
1603 { "defaulton", COMPILER_INLINE_DEFAULTON, },
1604 { "defaultoff", COMPILER_INLINE_DEFAULTOFF, },
1605 { "nopenalty", COMPILER_INLINE_NOPENALTY, },
1606 { 0, 0 },
1607 },
1608 },
1609 { 0, 0 },
1610};
1611static const struct compiler_flag romcc_opt_flags[] = {
1612 { "-O", COMPILER_SIMPLIFY },
1613 { "-O2", COMPILER_SIMPLIFY | COMPILER_SCC_TRANSFORM },
Eric Biedermancb364952004-11-15 10:46:44 +00001614 { "-E", COMPILER_PP_ONLY },
Eric Biederman90089602004-05-28 14:11:54 +00001615 { 0, 0, },
1616};
1617static const struct compiler_flag romcc_debug_flags[] = {
1618 { "all", DEBUG_ALL },
1619 { "abort-on-error", DEBUG_ABORT_ON_ERROR },
1620 { "basic-blocks", DEBUG_BASIC_BLOCKS },
1621 { "fdominators", DEBUG_FDOMINATORS },
1622 { "rdominators", DEBUG_RDOMINATORS },
1623 { "triples", DEBUG_TRIPLES },
1624 { "interference", DEBUG_INTERFERENCE },
1625 { "scc-transform", DEBUG_SCC_TRANSFORM },
1626 { "scc-transform2", DEBUG_SCC_TRANSFORM2 },
1627 { "rebuild-ssa-form", DEBUG_REBUILD_SSA_FORM },
1628 { "inline", DEBUG_INLINE },
1629 { "live-range-conflicts", DEBUG_RANGE_CONFLICTS },
1630 { "live-range-conflicts2", DEBUG_RANGE_CONFLICTS2 },
1631 { "color-graph", DEBUG_COLOR_GRAPH },
1632 { "color-graph2", DEBUG_COLOR_GRAPH2 },
1633 { "coalescing", DEBUG_COALESCING },
1634 { "coalescing2", DEBUG_COALESCING2 },
1635 { "verification", DEBUG_VERIFICATION },
1636 { "calls", DEBUG_CALLS },
1637 { "calls2", DEBUG_CALLS2 },
1638 { "tokens", DEBUG_TOKENS },
1639 { 0, 0 },
1640};
1641
Eric Biederman5ade04a2003-10-22 04:03:46 +00001642static int compiler_encode_flag(
1643 struct compiler_state *compiler, const char *flag)
1644{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001645 int act;
1646 int result;
1647
1648 act = 1;
1649 result = -1;
1650 if (strncmp(flag, "no-", 3) == 0) {
1651 flag += 3;
1652 act = 0;
1653 }
1654 if (strncmp(flag, "-O", 2) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +00001655 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1656 }
1657 else if (strncmp(flag, "-E", 2) == 0) {
1658 result = set_flag(romcc_opt_flags, &compiler->flags, act, flag);
1659 }
1660 else if (strncmp(flag, "-I", 2) == 0) {
1661 result = append_include_path(compiler, flag + 2);
1662 }
1663 else if (strncmp(flag, "-D", 2) == 0) {
1664 result = append_define(compiler, flag + 2);
1665 }
1666 else if (strncmp(flag, "-U", 2) == 0) {
1667 result = append_undef(compiler, flag + 2);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001668 }
1669 else if (act && strncmp(flag, "label-prefix=", 13) == 0) {
1670 result = 0;
1671 compiler->label_prefix = flag + 13;
1672 }
1673 else if (act && strncmp(flag, "max-allocation-passes=", 22) == 0) {
1674 unsigned long max_passes;
1675 char *end;
1676 max_passes = strtoul(flag + 22, &end, 10);
1677 if (end[0] == '\0') {
1678 result = 0;
1679 compiler->max_allocation_passes = max_passes;
1680 }
1681 }
1682 else if (act && strcmp(flag, "debug") == 0) {
1683 result = 0;
1684 compiler->debug |= DEBUG_DEFAULT;
1685 }
1686 else if (strncmp(flag, "debug-", 6) == 0) {
1687 flag += 6;
Eric Biederman90089602004-05-28 14:11:54 +00001688 result = set_flag(romcc_debug_flags, &compiler->debug, act, flag);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001689 }
1690 else {
Eric Biederman90089602004-05-28 14:11:54 +00001691 result = set_flag(romcc_flags, &compiler->flags, act, flag);
1692 if (result < 0) {
1693 result = set_arg(romcc_args, &compiler->flags, flag);
1694 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00001695 }
1696 return result;
1697}
1698
Eric Biederman90089602004-05-28 14:11:54 +00001699static void compiler_usage(FILE *fp)
1700{
1701 flag_usage(fp, romcc_opt_flags, "", 0);
1702 flag_usage(fp, romcc_flags, "-f", "-fno-");
1703 arg_usage(fp, romcc_args, "-f");
1704 flag_usage(fp, romcc_debug_flags, "-fdebug-", "-fno-debug-");
1705 fprintf(fp, "-flabel-prefix=<prefix for assembly language labels>\n");
1706 fprintf(fp, "--label-prefix=<prefix for assembly language labels>\n");
1707 fprintf(fp, "-I<include path>\n");
1708 fprintf(fp, "-D<macro>[=defn]\n");
1709 fprintf(fp, "-U<macro>\n");
1710}
1711
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001712static void do_cleanup(struct compile_state *state)
1713{
1714 if (state->output) {
1715 fclose(state->output);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001716 unlink(state->compiler->ofilename);
Eric Biederman90089602004-05-28 14:11:54 +00001717 state->output = 0;
1718 }
1719 if (state->dbgout) {
1720 fflush(state->dbgout);
1721 }
1722 if (state->errout) {
1723 fflush(state->errout);
1724 }
1725}
1726
1727static struct compile_state *exit_state;
1728static void exit_cleanup(void)
1729{
1730 if (exit_state) {
1731 do_cleanup(exit_state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001732 }
1733}
Eric Biedermanb138ac82003-04-22 18:44:01 +00001734
1735static int get_col(struct file_state *file)
1736{
1737 int col;
Eric Biederman90089602004-05-28 14:11:54 +00001738 const char *ptr, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001739 ptr = file->line_start;
1740 end = file->pos;
1741 for(col = 0; ptr < end; ptr++) {
1742 if (*ptr != '\t') {
1743 col++;
1744 }
1745 else {
1746 col = (col & ~7) + 8;
1747 }
1748 }
1749 return col;
1750}
1751
1752static void loc(FILE *fp, struct compile_state *state, struct triple *triple)
1753{
1754 int col;
Eric Biederman530b5192003-07-01 10:05:30 +00001755 if (triple && triple->occurance) {
Eric Biederman00443072003-06-24 12:34:45 +00001756 struct occurance *spot;
Eric Biederman5ade04a2003-10-22 04:03:46 +00001757 for(spot = triple->occurance; spot; spot = spot->parent) {
1758 fprintf(fp, "%s:%d.%d: ",
1759 spot->filename, spot->line, spot->col);
Eric Biederman00443072003-06-24 12:34:45 +00001760 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001761 return;
1762 }
1763 if (!state->file) {
1764 return;
1765 }
1766 col = get_col(state->file);
1767 fprintf(fp, "%s:%d.%d: ",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001768 state->file->report_name, state->file->report_line, col);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001769}
1770
Patrick Georgib203c2f2009-08-20 14:48:03 +00001771static void __attribute__ ((noreturn)) internal_error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001772 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001773{
Eric Biederman90089602004-05-28 14:11:54 +00001774 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001775 va_list args;
1776 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001777 loc(fp, state, ptr);
1778 fputc('\n', fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001779 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001780 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001781 }
Eric Biederman90089602004-05-28 14:11:54 +00001782 fprintf(fp, "Internal compiler error: ");
1783 vfprintf(fp, fmt, args);
1784 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001785 va_end(args);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001786 do_cleanup(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001787 abort();
1788}
1789
1790
Eric Biederman5ade04a2003-10-22 04:03:46 +00001791static void internal_warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001792 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001793{
Eric Biederman90089602004-05-28 14:11:54 +00001794 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001795 va_list args;
1796 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001797 loc(fp, state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00001798 if (ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00001799 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman66fe2222003-07-04 15:14:04 +00001800 }
Eric Biederman90089602004-05-28 14:11:54 +00001801 fprintf(fp, "Internal compiler warning: ");
1802 vfprintf(fp, fmt, args);
1803 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001804 va_end(args);
1805}
1806
1807
1808
Patrick Georgib203c2f2009-08-20 14:48:03 +00001809static void __attribute__ ((noreturn)) error(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001810 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001811{
Eric Biederman90089602004-05-28 14:11:54 +00001812 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001813 va_list args;
1814 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001815 loc(fp, state, ptr);
1816 fputc('\n', fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001817 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
Eric Biederman90089602004-05-28 14:11:54 +00001818 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
Eric Biederman83b991a2003-10-11 06:20:25 +00001819 }
Eric Biederman90089602004-05-28 14:11:54 +00001820 vfprintf(fp, fmt, args);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001821 va_end(args);
Eric Biederman90089602004-05-28 14:11:54 +00001822 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001823 do_cleanup(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +00001824 if (state->compiler->debug & DEBUG_ABORT_ON_ERROR) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00001825 abort();
1826 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001827 exit(1);
1828}
1829
Eric Biederman5ade04a2003-10-22 04:03:46 +00001830static void warning(struct compile_state *state, struct triple *ptr,
Eric Biederman90089602004-05-28 14:11:54 +00001831 const char *fmt, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001832{
Eric Biederman90089602004-05-28 14:11:54 +00001833 FILE *fp = state->errout;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001834 va_list args;
1835 va_start(args, fmt);
Eric Biederman90089602004-05-28 14:11:54 +00001836 loc(fp, state, ptr);
1837 fprintf(fp, "warning: ");
1838 if (ptr && (state->compiler->debug & DEBUG_ABORT_ON_ERROR)) {
1839 fprintf(fp, "%p %-10s ", ptr, tops(ptr->op));
1840 }
1841 vfprintf(fp, fmt, args);
1842 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00001843 va_end(args);
1844}
1845
Eric Biedermanb138ac82003-04-22 18:44:01 +00001846#define FINISHME() warning(state, 0, "FINISHME @ %s.%s:%d", __FILE__, __func__, __LINE__)
1847
Eric Biederman0babc1c2003-05-09 02:39:00 +00001848static void valid_op(struct compile_state *state, int op)
Eric Biedermanb138ac82003-04-22 18:44:01 +00001849{
1850 char *fmt = "invalid op: %d";
Eric Biederman0babc1c2003-05-09 02:39:00 +00001851 if (op >= OP_MAX) {
1852 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001853 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00001854 if (op < 0) {
1855 internal_error(state, 0, fmt, op);
Eric Biedermanb138ac82003-04-22 18:44:01 +00001856 }
1857}
1858
Eric Biederman0babc1c2003-05-09 02:39:00 +00001859static void valid_ins(struct compile_state *state, struct triple *ptr)
1860{
1861 valid_op(state, ptr->op);
1862}
1863
Stefan Reinauer50542a82007-10-24 11:14:14 +00001864#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00001865static void valid_param_count(struct compile_state *state, struct triple *ins)
1866{
1867 int lhs, rhs, misc, targ;
1868 valid_ins(state, ins);
1869 lhs = table_ops[ins->op].lhs;
1870 rhs = table_ops[ins->op].rhs;
1871 misc = table_ops[ins->op].misc;
1872 targ = table_ops[ins->op].targ;
1873
1874 if ((lhs >= 0) && (ins->lhs != lhs)) {
1875 internal_error(state, ins, "Bad lhs count");
1876 }
1877 if ((rhs >= 0) && (ins->rhs != rhs)) {
1878 internal_error(state, ins, "Bad rhs count");
1879 }
1880 if ((misc >= 0) && (ins->misc != misc)) {
1881 internal_error(state, ins, "Bad misc count");
1882 }
1883 if ((targ >= 0) && (ins->targ != targ)) {
1884 internal_error(state, ins, "Bad targ count");
1885 }
1886}
Stefan Reinauer50542a82007-10-24 11:14:14 +00001887#endif
Eric Biederman90089602004-05-28 14:11:54 +00001888
Eric Biedermanb138ac82003-04-22 18:44:01 +00001889static struct type void_type;
Eric Biederman90089602004-05-28 14:11:54 +00001890static struct type unknown_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +00001891static void use_triple(struct triple *used, struct triple *user)
1892{
1893 struct triple_set **ptr, *new;
1894 if (!used)
1895 return;
1896 if (!user)
1897 return;
1898 ptr = &used->use;
1899 while(*ptr) {
1900 if ((*ptr)->member == user) {
1901 return;
1902 }
1903 ptr = &(*ptr)->next;
1904 }
1905 /* Append new to the head of the list,
1906 * copy_func and rename_block_variables
1907 * depends on this.
1908 */
1909 new = xcmalloc(sizeof(*new), "triple_set");
1910 new->member = user;
1911 new->next = used->use;
1912 used->use = new;
1913}
1914
1915static void unuse_triple(struct triple *used, struct triple *unuser)
1916{
1917 struct triple_set *use, **ptr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00001918 if (!used) {
1919 return;
1920 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00001921 ptr = &used->use;
1922 while(*ptr) {
1923 use = *ptr;
1924 if (use->member == unuser) {
1925 *ptr = use->next;
1926 xfree(use);
1927 }
1928 else {
1929 ptr = &use->next;
1930 }
1931 }
1932}
1933
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001934static void put_occurance(struct occurance *occurance)
1935{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001936 if (occurance) {
1937 occurance->count -= 1;
1938 if (occurance->count <= 0) {
1939 if (occurance->parent) {
1940 put_occurance(occurance->parent);
1941 }
1942 xfree(occurance);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001943 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001944 }
1945}
1946
1947static void get_occurance(struct occurance *occurance)
1948{
Eric Biederman5ade04a2003-10-22 04:03:46 +00001949 if (occurance) {
1950 occurance->count += 1;
1951 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001952}
1953
1954
1955static struct occurance *new_occurance(struct compile_state *state)
1956{
1957 struct occurance *result, *last;
1958 const char *filename;
1959 const char *function;
1960 int line, col;
1961
1962 function = "";
1963 filename = 0;
1964 line = 0;
1965 col = 0;
1966 if (state->file) {
1967 filename = state->file->report_name;
1968 line = state->file->report_line;
1969 col = get_col(state->file);
1970 }
1971 if (state->function) {
1972 function = state->function;
1973 }
1974 last = state->last_occurance;
1975 if (last &&
1976 (last->col == col) &&
1977 (last->line == line) &&
1978 (last->function == function) &&
Eric Biederman83b991a2003-10-11 06:20:25 +00001979 ((last->filename == filename) ||
1980 (strcmp(last->filename, filename) == 0)))
1981 {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00001982 get_occurance(last);
1983 return last;
1984 }
1985 if (last) {
1986 state->last_occurance = 0;
1987 put_occurance(last);
1988 }
1989 result = xmalloc(sizeof(*result), "occurance");
1990 result->count = 2;
1991 result->filename = filename;
1992 result->function = function;
1993 result->line = line;
1994 result->col = col;
1995 result->parent = 0;
1996 state->last_occurance = result;
1997 return result;
1998}
1999
2000static struct occurance *inline_occurance(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00002001 struct occurance *base, struct occurance *top)
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002002{
2003 struct occurance *result, *last;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002004 if (top->parent) {
2005 internal_error(state, 0, "inlining an already inlined function?");
2006 }
2007 /* If I have a null base treat it that way */
2008 if ((base->parent == 0) &&
2009 (base->col == 0) &&
2010 (base->line == 0) &&
2011 (base->function[0] == '\0') &&
2012 (base->filename[0] == '\0')) {
2013 base = 0;
2014 }
2015 /* See if I can reuse the last occurance I had */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002016 last = state->last_occurance;
2017 if (last &&
Eric Biederman5ade04a2003-10-22 04:03:46 +00002018 (last->parent == base) &&
2019 (last->col == top->col) &&
2020 (last->line == top->line) &&
2021 (last->function == top->function) &&
2022 (last->filename == top->filename)) {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002023 get_occurance(last);
2024 return last;
2025 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002026 /* I can't reuse the last occurance so free it */
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002027 if (last) {
2028 state->last_occurance = 0;
2029 put_occurance(last);
2030 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002031 /* Generate a new occurance structure */
2032 get_occurance(base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002033 result = xmalloc(sizeof(*result), "occurance");
2034 result->count = 2;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002035 result->filename = top->filename;
2036 result->function = top->function;
2037 result->line = top->line;
2038 result->col = top->col;
2039 result->parent = base;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002040 state->last_occurance = result;
2041 return result;
2042}
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002043
2044static struct occurance dummy_occurance = {
2045 .count = 2,
2046 .filename = __FILE__,
2047 .function = "",
2048 .line = __LINE__,
2049 .col = 0,
2050 .parent = 0,
2051};
Eric Biedermanb138ac82003-04-22 18:44:01 +00002052
Eric Biederman90089602004-05-28 14:11:54 +00002053/* The undef triple is used as a place holder when we are removing pointers
Eric Biedermanb138ac82003-04-22 18:44:01 +00002054 * from a triple. Having allows certain sanity checks to pass even
2055 * when the original triple that was pointed to is gone.
2056 */
Eric Biederman90089602004-05-28 14:11:54 +00002057static struct triple unknown_triple = {
2058 .next = &unknown_triple,
2059 .prev = &unknown_triple,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002060 .use = 0,
Eric Biederman90089602004-05-28 14:11:54 +00002061 .op = OP_UNKNOWNVAL,
2062 .lhs = 0,
2063 .rhs = 0,
2064 .misc = 0,
2065 .targ = 0,
2066 .type = &unknown_type,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002067 .id = -1, /* An invalid id */
Eric Biederman830c9882003-07-04 00:27:33 +00002068 .u = { .cval = 0, },
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002069 .occurance = &dummy_occurance,
Eric Biederman830c9882003-07-04 00:27:33 +00002070 .param = { [0] = 0, [1] = 0, },
Eric Biedermanb138ac82003-04-22 18:44:01 +00002071};
2072
Eric Biederman0babc1c2003-05-09 02:39:00 +00002073
Eric Biederman90089602004-05-28 14:11:54 +00002074static size_t registers_of(struct compile_state *state, struct type *type);
2075
2076static struct triple *alloc_triple(struct compile_state *state,
Eric Biederman678d8162003-07-03 03:59:38 +00002077 int op, struct type *type, int lhs_wanted, int rhs_wanted,
2078 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002079{
Eric Biederman90089602004-05-28 14:11:54 +00002080 size_t size, extra_count, min_count;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002081 int lhs, rhs, misc, targ;
Eric Biederman90089602004-05-28 14:11:54 +00002082 struct triple *ret, dummy;
Eric Biederman678d8162003-07-03 03:59:38 +00002083 dummy.op = op;
2084 dummy.occurance = occurance;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002085 valid_op(state, op);
2086 lhs = table_ops[op].lhs;
2087 rhs = table_ops[op].rhs;
2088 misc = table_ops[op].misc;
2089 targ = table_ops[op].targ;
Eric Biederman90089602004-05-28 14:11:54 +00002090
2091 switch(op) {
2092 case OP_FCALL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00002093 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002094 break;
2095 case OP_PHI:
Eric Biederman0babc1c2003-05-09 02:39:00 +00002096 rhs = rhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002097 break;
2098 case OP_ADECL:
2099 lhs = registers_of(state, type);
2100 break;
2101 case OP_TUPLE:
2102 lhs = registers_of(state, type);
2103 break;
2104 case OP_ASM:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002105 rhs = rhs_wanted;
2106 lhs = lhs_wanted;
Eric Biederman90089602004-05-28 14:11:54 +00002107 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002108 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002109 if ((rhs < 0) || (rhs > MAX_RHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002110 internal_error(state, &dummy, "bad rhs count %d", rhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002111 }
2112 if ((lhs < 0) || (lhs > MAX_LHS)) {
Eric Biederman90089602004-05-28 14:11:54 +00002113 internal_error(state, &dummy, "bad lhs count %d", lhs);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002114 }
2115 if ((misc < 0) || (misc > MAX_MISC)) {
Eric Biederman90089602004-05-28 14:11:54 +00002116 internal_error(state, &dummy, "bad misc count %d", misc);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002117 }
2118 if ((targ < 0) || (targ > MAX_TARG)) {
Eric Biederman90089602004-05-28 14:11:54 +00002119 internal_error(state, &dummy, "bad targs count %d", targ);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002120 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002121
2122 min_count = sizeof(ret->param)/sizeof(ret->param[0]);
Eric Biederman90089602004-05-28 14:11:54 +00002123 extra_count = lhs + rhs + misc + targ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002124 extra_count = (extra_count < min_count)? 0 : extra_count - min_count;
2125
2126 size = sizeof(*ret) + sizeof(ret->param[0]) * extra_count;
2127 ret = xcmalloc(size, "tripple");
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002128 ret->op = op;
Eric Biederman90089602004-05-28 14:11:54 +00002129 ret->lhs = lhs;
2130 ret->rhs = rhs;
2131 ret->misc = misc;
2132 ret->targ = targ;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002133 ret->type = type;
2134 ret->next = ret;
2135 ret->prev = ret;
2136 ret->occurance = occurance;
Eric Biederman90089602004-05-28 14:11:54 +00002137 /* A simple sanity check */
2138 if ((ret->op != op) ||
2139 (ret->lhs != lhs) ||
2140 (ret->rhs != rhs) ||
2141 (ret->misc != misc) ||
2142 (ret->targ != targ) ||
2143 (ret->type != type) ||
2144 (ret->next != ret) ||
2145 (ret->prev != ret) ||
2146 (ret->occurance != occurance)) {
2147 internal_error(state, ret, "huh?");
2148 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002149 return ret;
2150}
2151
Eric Biederman0babc1c2003-05-09 02:39:00 +00002152struct triple *dup_triple(struct compile_state *state, struct triple *src)
2153{
2154 struct triple *dup;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002155 int src_lhs, src_rhs, src_size;
Eric Biederman90089602004-05-28 14:11:54 +00002156 src_lhs = src->lhs;
2157 src_rhs = src->rhs;
2158 src_size = TRIPLE_SIZE(src);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002159 get_occurance(src->occurance);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002160 dup = alloc_triple(state, src->op, src->type, src_lhs, src_rhs,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002161 src->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002162 memcpy(dup, src, sizeof(*src));
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002163 memcpy(dup->param, src->param, src_size * sizeof(src->param[0]));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002164 return dup;
2165}
2166
Eric Biederman41203d92004-11-08 09:31:09 +00002167static struct triple *copy_triple(struct compile_state *state, struct triple *src)
2168{
2169 struct triple *copy;
2170 copy = dup_triple(state, src);
2171 copy->use = 0;
2172 copy->next = copy->prev = copy;
2173 return copy;
2174}
2175
Eric Biederman0babc1c2003-05-09 02:39:00 +00002176static struct triple *new_triple(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002177 int op, struct type *type, int lhs, int rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002178{
2179 struct triple *ret;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002180 struct occurance *occurance;
2181 occurance = new_occurance(state);
2182 ret = alloc_triple(state, op, type, lhs, rhs, occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002183 return ret;
2184}
2185
2186static struct triple *build_triple(struct compile_state *state,
2187 int op, struct type *type, struct triple *left, struct triple *right,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002188 struct occurance *occurance)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002189{
2190 struct triple *ret;
2191 size_t count;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002192 ret = alloc_triple(state, op, type, -1, -1, occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002193 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002194 if (count > 0) {
2195 ret->param[0] = left;
2196 }
2197 if (count > 1) {
2198 ret->param[1] = right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002199 }
2200 return ret;
2201}
2202
Eric Biederman0babc1c2003-05-09 02:39:00 +00002203static struct triple *triple(struct compile_state *state,
2204 int op, struct type *type, struct triple *left, struct triple *right)
2205{
2206 struct triple *ret;
2207 size_t count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002208 ret = new_triple(state, op, type, -1, -1);
Eric Biederman90089602004-05-28 14:11:54 +00002209 count = TRIPLE_SIZE(ret);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002210 if (count >= 1) {
2211 ret->param[0] = left;
2212 }
2213 if (count >= 2) {
2214 ret->param[1] = right;
2215 }
2216 return ret;
2217}
2218
2219static struct triple *branch(struct compile_state *state,
2220 struct triple *targ, struct triple *test)
2221{
2222 struct triple *ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002223 if (test) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002224 ret = new_triple(state, OP_CBRANCH, &void_type, -1, 1);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002225 RHS(ret, 0) = test;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002226 } else {
2227 ret = new_triple(state, OP_BRANCH, &void_type, -1, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002228 }
2229 TARG(ret, 0) = targ;
2230 /* record the branch target was used */
2231 if (!targ || (targ->op != OP_LABEL)) {
2232 internal_error(state, 0, "branch not to label");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002233 }
2234 return ret;
2235}
2236
Eric Biederman90089602004-05-28 14:11:54 +00002237static int triple_is_label(struct compile_state *state, struct triple *ins);
2238static int triple_is_call(struct compile_state *state, struct triple *ins);
2239static int triple_is_cbranch(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002240static void insert_triple(struct compile_state *state,
2241 struct triple *first, struct triple *ptr)
2242{
2243 if (ptr) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002244 if ((ptr->id & TRIPLE_FLAG_FLATTENED) || (ptr->next != ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002245 internal_error(state, ptr, "expression already used");
2246 }
2247 ptr->next = first;
2248 ptr->prev = first->prev;
2249 ptr->prev->next = ptr;
2250 ptr->next->prev = ptr;
Eric Biederman90089602004-05-28 14:11:54 +00002251
2252 if (triple_is_cbranch(state, ptr->prev) ||
2253 triple_is_call(state, ptr->prev)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002254 unuse_triple(first, ptr->prev);
2255 use_triple(ptr, ptr->prev);
2256 }
2257 }
2258}
2259
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002260static int triple_stores_block(struct compile_state *state, struct triple *ins)
2261{
2262 /* This function is used to determine if u.block
2263 * is utilized to store the current block number.
2264 */
2265 int stores_block;
2266 valid_ins(state, ins);
2267 stores_block = (table_ops[ins->op].flags & BLOCK) == BLOCK;
2268 return stores_block;
2269}
2270
Eric Biederman90089602004-05-28 14:11:54 +00002271static int triple_is_branch(struct compile_state *state, struct triple *ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002272static struct block *block_of_triple(struct compile_state *state,
2273 struct triple *ins)
2274{
2275 struct triple *first;
Eric Biederman90089602004-05-28 14:11:54 +00002276 if (!ins || ins == &unknown_triple) {
Eric Biederman83b991a2003-10-11 06:20:25 +00002277 return 0;
2278 }
2279 first = state->first;
Eric Biederman90089602004-05-28 14:11:54 +00002280 while(ins != first && !triple_is_branch(state, ins->prev) &&
2281 !triple_stores_block(state, ins))
2282 {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002283 if (ins == ins->prev) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002284 internal_error(state, ins, "ins == ins->prev?");
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002285 }
2286 ins = ins->prev;
2287 }
Eric Biederman90089602004-05-28 14:11:54 +00002288 return triple_stores_block(state, ins)? ins->u.block: 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002289}
2290
Eric Biederman90089602004-05-28 14:11:54 +00002291static void generate_lhs_pieces(struct compile_state *state, struct triple *ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002292static struct triple *pre_triple(struct compile_state *state,
2293 struct triple *base,
2294 int op, struct type *type, struct triple *left, struct triple *right)
2295{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002296 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002297 struct triple *ret;
Eric Biederman90089602004-05-28 14:11:54 +00002298 int i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002299 /* If I am an OP_PIECE jump to the real instruction */
2300 if (base->op == OP_PIECE) {
2301 base = MISC(base, 0);
2302 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002303 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002304 get_occurance(base->occurance);
2305 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002306 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002307 if (triple_stores_block(state, ret)) {
2308 ret->u.block = block;
2309 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002310 insert_triple(state, base, ret);
Eric Biederman90089602004-05-28 14:11:54 +00002311 for(i = 0; i < ret->lhs; i++) {
2312 struct triple *piece;
2313 piece = LHS(ret, i);
2314 insert_triple(state, base, piece);
2315 use_triple(ret, piece);
2316 use_triple(piece, ret);
2317 }
2318 if (block && (block->first == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002319 block->first = ret;
2320 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002321 return ret;
2322}
2323
2324static struct triple *post_triple(struct compile_state *state,
2325 struct triple *base,
2326 int op, struct type *type, struct triple *left, struct triple *right)
2327{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002328 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002329 struct triple *ret, *next;
2330 int zlhs, i;
Eric Biedermand3283ec2003-06-18 11:03:18 +00002331 /* If I am an OP_PIECE jump to the real instruction */
2332 if (base->op == OP_PIECE) {
2333 base = MISC(base, 0);
2334 }
2335 /* If I have a left hand side skip over it */
Eric Biederman90089602004-05-28 14:11:54 +00002336 zlhs = base->lhs;
Eric Biederman530b5192003-07-01 10:05:30 +00002337 if (zlhs) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002338 base = LHS(base, zlhs - 1);
2339 }
2340
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002341 block = block_of_triple(state, base);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002342 get_occurance(base->occurance);
2343 ret = build_triple(state, op, type, left, right, base->occurance);
Eric Biederman90089602004-05-28 14:11:54 +00002344 generate_lhs_pieces(state, ret);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002345 if (triple_stores_block(state, ret)) {
2346 ret->u.block = block;
2347 }
Eric Biederman90089602004-05-28 14:11:54 +00002348 next = base->next;
2349 insert_triple(state, next, ret);
2350 zlhs = ret->lhs;
2351 for(i = 0; i < zlhs; i++) {
2352 struct triple *piece;
2353 piece = LHS(ret, i);
2354 insert_triple(state, next, piece);
2355 use_triple(ret, piece);
2356 use_triple(piece, ret);
2357 }
2358 if (block && (block->last == base)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002359 block->last = ret;
Eric Biederman90089602004-05-28 14:11:54 +00002360 if (zlhs) {
2361 block->last = LHS(ret, zlhs - 1);
2362 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002363 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002364 return ret;
2365}
2366
Eric Biederman90089602004-05-28 14:11:54 +00002367static struct type *reg_type(
2368 struct compile_state *state, struct type *type, int reg);
2369
2370static void generate_lhs_piece(
2371 struct compile_state *state, struct triple *ins, int index)
2372{
2373 struct type *piece_type;
2374 struct triple *piece;
2375 get_occurance(ins->occurance);
2376 piece_type = reg_type(state, ins->type, index * REG_SIZEOF_REG);
2377
2378 if ((piece_type->type & TYPE_MASK) == TYPE_BITFIELD) {
2379 piece_type = piece_type->left;
2380 }
2381#if 0
2382{
2383 static void name_of(FILE *fp, struct type *type);
2384 FILE * fp = state->errout;
2385 fprintf(fp, "piece_type(%d): ", index);
2386 name_of(fp, piece_type);
2387 fprintf(fp, "\n");
2388}
2389#endif
2390 piece = alloc_triple(state, OP_PIECE, piece_type, -1, -1, ins->occurance);
2391 piece->u.cval = index;
2392 LHS(ins, piece->u.cval) = piece;
2393 MISC(piece, 0) = ins;
2394}
2395
2396static void generate_lhs_pieces(struct compile_state *state, struct triple *ins)
2397{
2398 int i, zlhs;
2399 zlhs = ins->lhs;
2400 for(i = 0; i < zlhs; i++) {
2401 generate_lhs_piece(state, ins, i);
2402 }
2403}
2404
Eric Biedermanb138ac82003-04-22 18:44:01 +00002405static struct triple *label(struct compile_state *state)
2406{
2407 /* Labels don't get a type */
2408 struct triple *result;
2409 result = triple(state, OP_LABEL, &void_type, 0, 0);
2410 return result;
2411}
2412
Eric Biederman90089602004-05-28 14:11:54 +00002413static struct triple *mkprog(struct compile_state *state, ...)
2414{
2415 struct triple *prog, *head, *arg;
2416 va_list args;
2417 int i;
2418
2419 head = label(state);
2420 prog = new_triple(state, OP_PROG, &void_type, -1, -1);
2421 RHS(prog, 0) = head;
2422 va_start(args, state);
2423 i = 0;
2424 while((arg = va_arg(args, struct triple *)) != 0) {
2425 if (++i >= 100) {
2426 internal_error(state, 0, "too many arguments to mkprog");
2427 }
2428 flatten(state, head, arg);
2429 }
2430 va_end(args);
2431 prog->type = head->prev->type;
2432 return prog;
2433}
2434static void name_of(FILE *fp, struct type *type);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002435static void display_triple(FILE *fp, struct triple *ins)
2436{
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002437 struct occurance *ptr;
2438 const char *reg;
Eric Biederman90089602004-05-28 14:11:54 +00002439 char pre, post, vol;
2440 pre = post = vol = ' ';
2441 if (ins) {
2442 if (ins->id & TRIPLE_FLAG_PRE_SPLIT) {
2443 pre = '^';
2444 }
2445 if (ins->id & TRIPLE_FLAG_POST_SPLIT) {
2446 post = ',';
2447 }
2448 if (ins->id & TRIPLE_FLAG_VOLATILE) {
2449 vol = 'v';
2450 }
2451 reg = arch_reg_str(ID_REG(ins->id));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002452 }
Eric Biederman90089602004-05-28 14:11:54 +00002453 if (ins == 0) {
2454 fprintf(fp, "(%p) <nothing> ", ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002455 }
Eric Biederman90089602004-05-28 14:11:54 +00002456 else if (ins->op == OP_INTCONST) {
2457 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s <0x%08lx> ",
2458 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002459 (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002460 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002461 else if (ins->op == OP_ADDRCONST) {
Eric Biederman90089602004-05-28 14:11:54 +00002462 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2463 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2464 MISC(ins, 0), (unsigned long)(ins->u.cval));
2465 }
2466 else if (ins->op == OP_INDEX) {
2467 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2468 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
2469 RHS(ins, 0), (unsigned long)(ins->u.cval));
2470 }
2471 else if (ins->op == OP_PIECE) {
2472 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s %-10p <0x%08lx>",
2473 ins, pre, post, vol, reg, ins->template_id, tops(ins->op),
Eric Biederman83b991a2003-10-11 06:20:25 +00002474 MISC(ins, 0), (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +00002475 }
2476 else {
2477 int i, count;
Eric Biederman90089602004-05-28 14:11:54 +00002478 fprintf(fp, "(%p) %c%c%c %-7s %-2d %-10s",
2479 ins, pre, post, vol, reg, ins->template_id, tops(ins->op));
2480 if (table_ops[ins->op].flags & BITFIELD) {
2481 fprintf(fp, " <%2d-%2d:%2d>",
2482 ins->u.bitfield.offset,
2483 ins->u.bitfield.offset + ins->u.bitfield.size,
2484 ins->u.bitfield.size);
2485 }
2486 count = TRIPLE_SIZE(ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002487 for(i = 0; i < count; i++) {
2488 fprintf(fp, " %-10p", ins->param[i]);
2489 }
2490 for(; i < 2; i++) {
Eric Biedermand3283ec2003-06-18 11:03:18 +00002491 fprintf(fp, " ");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002492 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002493 }
Eric Biederman90089602004-05-28 14:11:54 +00002494 if (ins) {
Eric Biederman530b5192003-07-01 10:05:30 +00002495 struct triple_set *user;
Eric Biederman90089602004-05-28 14:11:54 +00002496#if DEBUG_DISPLAY_TYPES
2497 fprintf(fp, " <");
2498 name_of(fp, ins->type);
2499 fprintf(fp, "> ");
2500#endif
2501#if DEBUG_DISPLAY_USES
2502 fprintf(fp, " [");
2503 for(user = ins->use; user; user = user->next) {
2504 fprintf(fp, " %-10p", user->member);
2505 }
2506 fprintf(fp, " ]");
2507#endif
2508 fprintf(fp, " @");
2509 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
2510 fprintf(fp, " %s,%s:%d.%d",
2511 ptr->function,
2512 ptr->filename,
2513 ptr->line,
2514 ptr->col);
2515 }
2516 if (ins->op == OP_ASM) {
2517 fprintf(fp, "\n\t%s", ins->u.ainfo->str);
Eric Biederman530b5192003-07-01 10:05:30 +00002518 }
2519 }
Eric Biederman90089602004-05-28 14:11:54 +00002520 fprintf(fp, "\n");
Eric Biederman0babc1c2003-05-09 02:39:00 +00002521 fflush(fp);
2522}
2523
Eric Biederman90089602004-05-28 14:11:54 +00002524static int equiv_types(struct type *left, struct type *right);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002525static void display_triple_changes(
2526 FILE *fp, const struct triple *new, const struct triple *orig)
2527{
2528
2529 int new_count, orig_count;
Eric Biederman90089602004-05-28 14:11:54 +00002530 new_count = TRIPLE_SIZE(new);
2531 orig_count = TRIPLE_SIZE(orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002532 if ((new->op != orig->op) ||
2533 (new_count != orig_count) ||
2534 (memcmp(orig->param, new->param,
2535 orig_count * sizeof(orig->param[0])) != 0) ||
2536 (memcmp(&orig->u, &new->u, sizeof(orig->u)) != 0))
2537 {
2538 struct occurance *ptr;
2539 int i, min_count, indent;
Eric Biederman90089602004-05-28 14:11:54 +00002540 fprintf(fp, "(%p %p)", new, orig);
Eric Biederman5ade04a2003-10-22 04:03:46 +00002541 if (orig->op == new->op) {
2542 fprintf(fp, " %-11s", tops(orig->op));
2543 } else {
2544 fprintf(fp, " [%-10s %-10s]",
2545 tops(new->op), tops(orig->op));
2546 }
2547 min_count = new_count;
2548 if (min_count > orig_count) {
2549 min_count = orig_count;
2550 }
2551 for(indent = i = 0; i < min_count; i++) {
2552 if (orig->param[i] == new->param[i]) {
2553 fprintf(fp, " %-11p",
2554 orig->param[i]);
2555 indent += 12;
2556 } else {
2557 fprintf(fp, " [%-10p %-10p]",
2558 new->param[i],
2559 orig->param[i]);
2560 indent += 24;
2561 }
2562 }
2563 for(; i < orig_count; i++) {
2564 fprintf(fp, " [%-9p]", orig->param[i]);
2565 indent += 12;
2566 }
2567 for(; i < new_count; i++) {
2568 fprintf(fp, " [%-9p]", new->param[i]);
2569 indent += 12;
2570 }
2571 if ((new->op == OP_INTCONST)||
2572 (new->op == OP_ADDRCONST)) {
2573 fprintf(fp, " <0x%08lx>",
2574 (unsigned long)(new->u.cval));
2575 indent += 13;
2576 }
2577 for(;indent < 36; indent++) {
2578 putc(' ', fp);
2579 }
Eric Biederman90089602004-05-28 14:11:54 +00002580
2581#if DEBUG_DISPLAY_TYPES
2582 fprintf(fp, " <");
2583 name_of(fp, new->type);
2584 if (!equiv_types(new->type, orig->type)) {
2585 fprintf(fp, " -- ");
2586 name_of(fp, orig->type);
2587 }
2588 fprintf(fp, "> ");
2589#endif
2590
Eric Biederman5ade04a2003-10-22 04:03:46 +00002591 fprintf(fp, " @");
2592 for(ptr = orig->occurance; ptr; ptr = ptr->parent) {
2593 fprintf(fp, " %s,%s:%d.%d",
2594 ptr->function,
2595 ptr->filename,
2596 ptr->line,
2597 ptr->col);
2598
2599 }
2600 fprintf(fp, "\n");
2601 fflush(fp);
2602 }
2603}
2604
Eric Biederman83b991a2003-10-11 06:20:25 +00002605static int triple_is_pure(struct compile_state *state, struct triple *ins, unsigned id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002606{
2607 /* Does the triple have no side effects.
2608 * I.e. Rexecuting the triple with the same arguments
2609 * gives the same value.
2610 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002611 unsigned pure;
2612 valid_ins(state, ins);
2613 pure = PURE_BITS(table_ops[ins->op].flags);
2614 if ((pure != PURE) && (pure != IMPURE)) {
Eric Biederman90089602004-05-28 14:11:54 +00002615 internal_error(state, 0, "Purity of %s not known",
Eric Biedermanb138ac82003-04-22 18:44:01 +00002616 tops(ins->op));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002617 }
Eric Biederman83b991a2003-10-11 06:20:25 +00002618 return (pure == PURE) && !(id & TRIPLE_FLAG_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002619}
2620
Eric Biederman90089602004-05-28 14:11:54 +00002621static int triple_is_branch_type(struct compile_state *state,
2622 struct triple *ins, unsigned type)
2623{
2624 /* Is this one of the passed branch types? */
2625 valid_ins(state, ins);
2626 return (BRANCH_BITS(table_ops[ins->op].flags) == type);
2627}
2628
Eric Biederman0babc1c2003-05-09 02:39:00 +00002629static int triple_is_branch(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002630{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002631 /* Is this triple a branch instruction? */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002632 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002633 return (BRANCH_BITS(table_ops[ins->op].flags) != 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002634}
2635
Eric Biederman90089602004-05-28 14:11:54 +00002636static int triple_is_cbranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002637{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002638 /* Is this triple a conditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002639 return triple_is_branch_type(state, ins, CBRANCH);
Eric Biederman530b5192003-07-01 10:05:30 +00002640}
2641
Eric Biederman90089602004-05-28 14:11:54 +00002642static int triple_is_ubranch(struct compile_state *state, struct triple *ins)
Eric Biederman530b5192003-07-01 10:05:30 +00002643{
Eric Biederman5ade04a2003-10-22 04:03:46 +00002644 /* Is this triple a unconditional branch instruction? */
Eric Biederman90089602004-05-28 14:11:54 +00002645 unsigned type;
Eric Biederman5ade04a2003-10-22 04:03:46 +00002646 valid_ins(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00002647 type = BRANCH_BITS(table_ops[ins->op].flags);
2648 return (type != 0) && (type != CBRANCH);
2649}
2650
2651static int triple_is_call(struct compile_state *state, struct triple *ins)
2652{
2653 /* Is this triple a call instruction? */
2654 return triple_is_branch_type(state, ins, CALLBRANCH);
2655}
2656
2657static int triple_is_ret(struct compile_state *state, struct triple *ins)
2658{
2659 /* Is this triple a return instruction? */
2660 return triple_is_branch_type(state, ins, RETBRANCH);
2661}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002662
2663#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00002664static int triple_is_simple_ubranch(struct compile_state *state, struct triple *ins)
2665{
2666 /* Is this triple an unconditional branch and not a call or a
2667 * return? */
2668 return triple_is_branch_type(state, ins, UBRANCH);
2669}
Stefan Reinauer50542a82007-10-24 11:14:14 +00002670#endif
Eric Biederman90089602004-05-28 14:11:54 +00002671
2672static int triple_is_end(struct compile_state *state, struct triple *ins)
2673{
2674 return triple_is_branch_type(state, ins, ENDBRANCH);
2675}
2676
2677static int triple_is_label(struct compile_state *state, struct triple *ins)
2678{
2679 valid_ins(state, ins);
2680 return (ins->op == OP_LABEL);
2681}
2682
2683static struct triple *triple_to_block_start(
2684 struct compile_state *state, struct triple *start)
2685{
2686 while(!triple_is_branch(state, start->prev) &&
2687 (!triple_is_label(state, start) || !start->use)) {
2688 start = start->prev;
2689 }
2690 return start;
Eric Biederman530b5192003-07-01 10:05:30 +00002691}
2692
Eric Biederman0babc1c2003-05-09 02:39:00 +00002693static int triple_is_def(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002694{
2695 /* This function is used to determine which triples need
2696 * a register.
2697 */
Eric Biederman0babc1c2003-05-09 02:39:00 +00002698 int is_def;
2699 valid_ins(state, ins);
2700 is_def = (table_ops[ins->op].flags & DEF) == DEF;
Eric Biederman90089602004-05-28 14:11:54 +00002701 if (ins->lhs >= 1) {
2702 is_def = 0;
2703 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002704 return is_def;
2705}
2706
Eric Biederman83b991a2003-10-11 06:20:25 +00002707static int triple_is_structural(struct compile_state *state, struct triple *ins)
2708{
2709 int is_structural;
2710 valid_ins(state, ins);
2711 is_structural = (table_ops[ins->op].flags & STRUCTURAL) == STRUCTURAL;
2712 return is_structural;
2713}
2714
Eric Biederman90089602004-05-28 14:11:54 +00002715static int triple_is_part(struct compile_state *state, struct triple *ins)
2716{
2717 int is_part;
2718 valid_ins(state, ins);
2719 is_part = (table_ops[ins->op].flags & PART) == PART;
2720 return is_part;
2721}
2722
2723static int triple_is_auto_var(struct compile_state *state, struct triple *ins)
2724{
2725 return (ins->op == OP_PIECE) && (MISC(ins, 0)->op == OP_ADECL);
2726}
2727
Eric Biederman0babc1c2003-05-09 02:39:00 +00002728static struct triple **triple_iter(struct compile_state *state,
2729 size_t count, struct triple **vector,
2730 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002731{
2732 struct triple **ret;
2733 ret = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002734 if (count) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002735 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00002736 ret = vector;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002737 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00002738 else if ((last >= vector) && (last < (vector + count - 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00002739 ret = last + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002740 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002741 }
2742 return ret;
Eric Biederman0babc1c2003-05-09 02:39:00 +00002743
Eric Biedermanb138ac82003-04-22 18:44:01 +00002744}
2745
2746static struct triple **triple_lhs(struct compile_state *state,
Eric Biederman0babc1c2003-05-09 02:39:00 +00002747 struct triple *ins, struct triple **last)
Eric Biedermanb138ac82003-04-22 18:44:01 +00002748{
Eric Biederman90089602004-05-28 14:11:54 +00002749 return triple_iter(state, ins->lhs, &LHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002750 ins, last);
2751}
2752
2753static struct triple **triple_rhs(struct compile_state *state,
2754 struct triple *ins, struct triple **last)
2755{
Eric Biederman90089602004-05-28 14:11:54 +00002756 return triple_iter(state, ins->rhs, &RHS(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002757 ins, last);
2758}
2759
2760static struct triple **triple_misc(struct compile_state *state,
2761 struct triple *ins, struct triple **last)
2762{
Eric Biederman90089602004-05-28 14:11:54 +00002763 return triple_iter(state, ins->misc, &MISC(ins,0),
Eric Biederman0babc1c2003-05-09 02:39:00 +00002764 ins, last);
2765}
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002766
Eric Biederman90089602004-05-28 14:11:54 +00002767static struct triple **do_triple_targ(struct compile_state *state,
2768 struct triple *ins, struct triple **last, int call_edges, int next_edges)
Eric Biederman0babc1c2003-05-09 02:39:00 +00002769{
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002770 size_t count;
2771 struct triple **ret, **vector;
Eric Biederman90089602004-05-28 14:11:54 +00002772 int next_is_targ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002773 ret = 0;
Eric Biederman90089602004-05-28 14:11:54 +00002774 count = ins->targ;
2775 next_is_targ = 0;
2776 if (triple_is_cbranch(state, ins)) {
2777 next_is_targ = 1;
2778 }
2779 if (!call_edges && triple_is_call(state, ins)) {
2780 count = 0;
2781 }
2782 if (next_edges && triple_is_call(state, ins)) {
2783 next_is_targ = 1;
2784 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002785 vector = &TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +00002786 if (!ret && next_is_targ) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002787 if (!last) {
2788 ret = &ins->next;
2789 } else if (last == &ins->next) {
2790 last = 0;
2791 }
2792 }
2793 if (!ret && count) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002794 if (!last) {
2795 ret = vector;
2796 }
2797 else if ((last >= vector) && (last < (vector + count - 1))) {
2798 ret = last + 1;
2799 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002800 else if (last == vector + count - 1) {
2801 last = 0;
2802 }
2803 }
Eric Biederman90089602004-05-28 14:11:54 +00002804 if (!ret && triple_is_ret(state, ins) && call_edges) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002805 struct triple_set *use;
2806 for(use = ins->use; use; use = use->next) {
Eric Biederman90089602004-05-28 14:11:54 +00002807 if (!triple_is_call(state, use->member)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00002808 continue;
2809 }
2810 if (!last) {
2811 ret = &use->member->next;
2812 break;
2813 }
2814 else if (last == &use->member->next) {
2815 last = 0;
2816 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002817 }
2818 }
2819 return ret;
2820}
2821
Eric Biederman90089602004-05-28 14:11:54 +00002822static struct triple **triple_targ(struct compile_state *state,
2823 struct triple *ins, struct triple **last)
2824{
2825 return do_triple_targ(state, ins, last, 1, 1);
2826}
2827
2828static struct triple **triple_edge_targ(struct compile_state *state,
2829 struct triple *ins, struct triple **last)
2830{
2831 return do_triple_targ(state, ins, last,
2832 state->functions_joined, !state->functions_joined);
2833}
2834
2835static struct triple *after_lhs(struct compile_state *state, struct triple *ins)
2836{
2837 struct triple *next;
2838 int lhs, i;
2839 lhs = ins->lhs;
2840 next = ins->next;
2841 for(i = 0; i < lhs; i++) {
2842 struct triple *piece;
2843 piece = LHS(ins, i);
2844 if (next != piece) {
2845 internal_error(state, ins, "malformed lhs on %s",
2846 tops(ins->op));
2847 }
2848 if (next->op != OP_PIECE) {
2849 internal_error(state, ins, "bad lhs op %s at %d on %s",
2850 tops(next->op), i, tops(ins->op));
2851 }
2852 if (next->u.cval != i) {
2853 internal_error(state, ins, "bad u.cval of %d %d expected",
2854 next->u.cval, i);
2855 }
2856 next = next->next;
2857 }
2858 return next;
2859}
2860
2861/* Function piece accessor functions */
2862static struct triple *do_farg(struct compile_state *state,
2863 struct triple *func, unsigned index)
2864{
2865 struct type *ftype;
2866 struct triple *first, *arg;
2867 unsigned i;
2868
2869 ftype = func->type;
2870 if((index < 0) || (index >= (ftype->elements + 2))) {
2871 internal_error(state, func, "bad argument index: %d", index);
2872 }
2873 first = RHS(func, 0);
2874 arg = first->next;
2875 for(i = 0; i < index; i++, arg = after_lhs(state, arg)) {
2876 /* do nothing */
2877 }
2878 if (arg->op != OP_ADECL) {
2879 internal_error(state, 0, "arg not adecl?");
2880 }
2881 return arg;
2882}
2883static struct triple *fresult(struct compile_state *state, struct triple *func)
2884{
2885 return do_farg(state, func, 0);
2886}
2887static struct triple *fretaddr(struct compile_state *state, struct triple *func)
2888{
2889 return do_farg(state, func, 1);
2890}
2891static struct triple *farg(struct compile_state *state,
2892 struct triple *func, unsigned index)
2893{
2894 return do_farg(state, func, index + 2);
2895}
2896
2897
2898static void display_func(struct compile_state *state, FILE *fp, struct triple *func)
2899{
2900 struct triple *first, *ins;
2901 fprintf(fp, "display_func %s\n", func->type->type_ident->name);
2902 first = ins = RHS(func, 0);
2903 do {
2904 if (triple_is_label(state, ins) && ins->use) {
2905 fprintf(fp, "%p:\n", ins);
2906 }
2907 display_triple(fp, ins);
2908
2909 if (triple_is_branch(state, ins)) {
2910 fprintf(fp, "\n");
2911 }
2912 if (ins->next->prev != ins) {
2913 internal_error(state, ins->next, "bad prev");
2914 }
2915 ins = ins->next;
2916 } while(ins != first);
2917}
2918
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002919static void verify_use(struct compile_state *state,
2920 struct triple *user, struct triple *used)
2921{
2922 int size, i;
Eric Biederman90089602004-05-28 14:11:54 +00002923 size = TRIPLE_SIZE(user);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002924 for(i = 0; i < size; i++) {
2925 if (user->param[i] == used) {
2926 break;
2927 }
2928 }
2929 if (triple_is_branch(state, user)) {
2930 if (user->next == used) {
2931 i = -1;
2932 }
2933 }
2934 if (i == size) {
2935 internal_error(state, user, "%s(%p) does not use %s(%p)",
2936 tops(user->op), user, tops(used->op), used);
2937 }
2938}
2939
2940static int find_rhs_use(struct compile_state *state,
2941 struct triple *user, struct triple *used)
2942{
2943 struct triple **param;
2944 int size, i;
2945 verify_use(state, user, used);
Stefan Reinauer50542a82007-10-24 11:14:14 +00002946
2947#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00002948#warning "AUDIT ME ->rhs"
Stefan Reinauer50542a82007-10-24 11:14:14 +00002949#endif
Eric Biederman90089602004-05-28 14:11:54 +00002950 size = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00002951 param = &RHS(user, 0);
2952 for(i = 0; i < size; i++) {
2953 if (param[i] == used) {
2954 return i;
2955 }
2956 }
2957 return -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00002958}
2959
2960static void free_triple(struct compile_state *state, struct triple *ptr)
2961{
Eric Biederman0babc1c2003-05-09 02:39:00 +00002962 size_t size;
2963 size = sizeof(*ptr) - sizeof(ptr->param) +
Eric Biederman90089602004-05-28 14:11:54 +00002964 (sizeof(ptr->param[0])*TRIPLE_SIZE(ptr));
Eric Biedermanb138ac82003-04-22 18:44:01 +00002965 ptr->prev->next = ptr->next;
2966 ptr->next->prev = ptr->prev;
2967 if (ptr->use) {
2968 internal_error(state, ptr, "ptr->use != 0");
2969 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00002970 put_occurance(ptr->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +00002971 memset(ptr, -1, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00002972 xfree(ptr);
2973}
2974
2975static void release_triple(struct compile_state *state, struct triple *ptr)
2976{
2977 struct triple_set *set, *next;
2978 struct triple **expr;
Eric Biederman66fe2222003-07-04 15:14:04 +00002979 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00002980 if (ptr == &unknown_triple) {
2981 return;
2982 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00002983 valid_ins(state, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +00002984 /* Make certain the we are not the first or last element of a block */
2985 block = block_of_triple(state, ptr);
Eric Biederman83b991a2003-10-11 06:20:25 +00002986 if (block) {
2987 if ((block->last == ptr) && (block->first == ptr)) {
2988 block->last = block->first = 0;
2989 }
2990 else if (block->last == ptr) {
2991 block->last = ptr->prev;
2992 }
2993 else if (block->first == ptr) {
2994 block->first = ptr->next;
2995 }
Eric Biederman66fe2222003-07-04 15:14:04 +00002996 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00002997 /* Remove ptr from use chains where it is the user */
2998 expr = triple_rhs(state, ptr, 0);
2999 for(; expr; expr = triple_rhs(state, ptr, expr)) {
3000 if (*expr) {
3001 unuse_triple(*expr, ptr);
3002 }
3003 }
3004 expr = triple_lhs(state, ptr, 0);
3005 for(; expr; expr = triple_lhs(state, ptr, expr)) {
3006 if (*expr) {
3007 unuse_triple(*expr, ptr);
3008 }
3009 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003010 expr = triple_misc(state, ptr, 0);
3011 for(; expr; expr = triple_misc(state, ptr, expr)) {
3012 if (*expr) {
3013 unuse_triple(*expr, ptr);
3014 }
3015 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003016 expr = triple_targ(state, ptr, 0);
3017 for(; expr; expr = triple_targ(state, ptr, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00003018 if (*expr){
Eric Biedermanb138ac82003-04-22 18:44:01 +00003019 unuse_triple(*expr, ptr);
3020 }
3021 }
3022 /* Reomve ptr from use chains where it is used */
3023 for(set = ptr->use; set; set = next) {
3024 next = set->next;
Eric Biederman5ade04a2003-10-22 04:03:46 +00003025 valid_ins(state, set->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003026 expr = triple_rhs(state, set->member, 0);
3027 for(; expr; expr = triple_rhs(state, set->member, expr)) {
3028 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003029 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003030 }
3031 }
3032 expr = triple_lhs(state, set->member, 0);
3033 for(; expr; expr = triple_lhs(state, set->member, expr)) {
3034 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003035 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003036 }
3037 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003038 expr = triple_misc(state, set->member, 0);
3039 for(; expr; expr = triple_misc(state, set->member, expr)) {
3040 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003041 *expr = &unknown_triple;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003042 }
3043 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003044 expr = triple_targ(state, set->member, 0);
3045 for(; expr; expr = triple_targ(state, set->member, expr)) {
3046 if (*expr == ptr) {
Eric Biederman90089602004-05-28 14:11:54 +00003047 *expr = &unknown_triple;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003048 }
3049 }
3050 unuse_triple(ptr, set->member);
3051 }
3052 free_triple(state, ptr);
3053}
3054
Eric Biederman5ade04a2003-10-22 04:03:46 +00003055static void print_triples(struct compile_state *state);
3056static void print_blocks(struct compile_state *state, const char *func, FILE *fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003057
Jason Schildt27b85112005-08-10 14:31:52 +00003058#define TOK_UNKNOWN 0
3059#define TOK_SPACE 1
3060#define TOK_SEMI 2
3061#define TOK_LBRACE 3
3062#define TOK_RBRACE 4
3063#define TOK_COMMA 5
3064#define TOK_EQ 6
3065#define TOK_COLON 7
3066#define TOK_LBRACKET 8
3067#define TOK_RBRACKET 9
3068#define TOK_LPAREN 10
3069#define TOK_RPAREN 11
3070#define TOK_STAR 12
3071#define TOK_DOTS 13
3072#define TOK_MORE 14
3073#define TOK_LESS 15
3074#define TOK_TIMESEQ 16
3075#define TOK_DIVEQ 17
3076#define TOK_MODEQ 18
3077#define TOK_PLUSEQ 19
3078#define TOK_MINUSEQ 20
3079#define TOK_SLEQ 21
3080#define TOK_SREQ 22
3081#define TOK_ANDEQ 23
3082#define TOK_XOREQ 24
3083#define TOK_OREQ 25
3084#define TOK_EQEQ 26
3085#define TOK_NOTEQ 27
3086#define TOK_QUEST 28
3087#define TOK_LOGOR 29
3088#define TOK_LOGAND 30
3089#define TOK_OR 31
3090#define TOK_AND 32
3091#define TOK_XOR 33
3092#define TOK_LESSEQ 34
3093#define TOK_MOREEQ 35
3094#define TOK_SL 36
3095#define TOK_SR 37
3096#define TOK_PLUS 38
3097#define TOK_MINUS 39
3098#define TOK_DIV 40
3099#define TOK_MOD 41
3100#define TOK_PLUSPLUS 42
3101#define TOK_MINUSMINUS 43
3102#define TOK_BANG 44
3103#define TOK_ARROW 45
3104#define TOK_DOT 46
3105#define TOK_TILDE 47
3106#define TOK_LIT_STRING 48
3107#define TOK_LIT_CHAR 49
3108#define TOK_LIT_INT 50
3109#define TOK_LIT_FLOAT 51
3110#define TOK_MACRO 52
3111#define TOK_CONCATENATE 53
Eric Biedermanb138ac82003-04-22 18:44:01 +00003112
Jason Schildt27b85112005-08-10 14:31:52 +00003113#define TOK_IDENT 54
3114#define TOK_STRUCT_NAME 55
3115#define TOK_ENUM_CONST 56
3116#define TOK_TYPE_NAME 57
Eric Biedermanb138ac82003-04-22 18:44:01 +00003117
Jason Schildt27b85112005-08-10 14:31:52 +00003118#define TOK_AUTO 58
3119#define TOK_BREAK 59
3120#define TOK_CASE 60
3121#define TOK_CHAR 61
3122#define TOK_CONST 62
3123#define TOK_CONTINUE 63
3124#define TOK_DEFAULT 64
3125#define TOK_DO 65
3126#define TOK_DOUBLE 66
3127#define TOK_ELSE 67
3128#define TOK_ENUM 68
3129#define TOK_EXTERN 69
3130#define TOK_FLOAT 70
3131#define TOK_FOR 71
3132#define TOK_GOTO 72
3133#define TOK_IF 73
3134#define TOK_INLINE 74
3135#define TOK_INT 75
3136#define TOK_LONG 76
3137#define TOK_REGISTER 77
3138#define TOK_RESTRICT 78
3139#define TOK_RETURN 79
3140#define TOK_SHORT 80
3141#define TOK_SIGNED 81
3142#define TOK_SIZEOF 82
3143#define TOK_STATIC 83
3144#define TOK_STRUCT 84
3145#define TOK_SWITCH 85
3146#define TOK_TYPEDEF 86
3147#define TOK_UNION 87
3148#define TOK_UNSIGNED 88
3149#define TOK_VOID 89
3150#define TOK_VOLATILE 90
3151#define TOK_WHILE 91
3152#define TOK_ASM 92
3153#define TOK_ATTRIBUTE 93
3154#define TOK_ALIGNOF 94
Eric Biedermanb138ac82003-04-22 18:44:01 +00003155#define TOK_FIRST_KEYWORD TOK_AUTO
3156#define TOK_LAST_KEYWORD TOK_ALIGNOF
3157
Eric Biedermancb364952004-11-15 10:46:44 +00003158#define TOK_MDEFINE 100
3159#define TOK_MDEFINED 101
3160#define TOK_MUNDEF 102
3161#define TOK_MINCLUDE 103
3162#define TOK_MLINE 104
3163#define TOK_MERROR 105
3164#define TOK_MWARNING 106
3165#define TOK_MPRAGMA 107
3166#define TOK_MIFDEF 108
3167#define TOK_MIFNDEF 109
3168#define TOK_MELIF 110
3169#define TOK_MENDIF 111
Eric Biedermanb138ac82003-04-22 18:44:01 +00003170
Eric Biedermancb364952004-11-15 10:46:44 +00003171#define TOK_FIRST_MACRO TOK_MDEFINE
3172#define TOK_LAST_MACRO TOK_MENDIF
Eric Biedermanb138ac82003-04-22 18:44:01 +00003173
Eric Biedermancb364952004-11-15 10:46:44 +00003174#define TOK_MIF 112
3175#define TOK_MELSE 113
3176#define TOK_MIDENT 114
Eric Biederman41203d92004-11-08 09:31:09 +00003177
Eric Biedermancb364952004-11-15 10:46:44 +00003178#define TOK_EOL 115
3179#define TOK_EOF 116
Eric Biedermanb138ac82003-04-22 18:44:01 +00003180
3181static const char *tokens[] = {
Eric Biederman41203d92004-11-08 09:31:09 +00003182[TOK_UNKNOWN ] = ":unknown:",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003183[TOK_SPACE ] = ":space:",
3184[TOK_SEMI ] = ";",
3185[TOK_LBRACE ] = "{",
3186[TOK_RBRACE ] = "}",
3187[TOK_COMMA ] = ",",
3188[TOK_EQ ] = "=",
3189[TOK_COLON ] = ":",
3190[TOK_LBRACKET ] = "[",
3191[TOK_RBRACKET ] = "]",
3192[TOK_LPAREN ] = "(",
3193[TOK_RPAREN ] = ")",
3194[TOK_STAR ] = "*",
3195[TOK_DOTS ] = "...",
3196[TOK_MORE ] = ">",
3197[TOK_LESS ] = "<",
3198[TOK_TIMESEQ ] = "*=",
3199[TOK_DIVEQ ] = "/=",
3200[TOK_MODEQ ] = "%=",
3201[TOK_PLUSEQ ] = "+=",
3202[TOK_MINUSEQ ] = "-=",
3203[TOK_SLEQ ] = "<<=",
3204[TOK_SREQ ] = ">>=",
3205[TOK_ANDEQ ] = "&=",
3206[TOK_XOREQ ] = "^=",
3207[TOK_OREQ ] = "|=",
3208[TOK_EQEQ ] = "==",
3209[TOK_NOTEQ ] = "!=",
3210[TOK_QUEST ] = "?",
3211[TOK_LOGOR ] = "||",
3212[TOK_LOGAND ] = "&&",
3213[TOK_OR ] = "|",
3214[TOK_AND ] = "&",
3215[TOK_XOR ] = "^",
3216[TOK_LESSEQ ] = "<=",
3217[TOK_MOREEQ ] = ">=",
3218[TOK_SL ] = "<<",
3219[TOK_SR ] = ">>",
3220[TOK_PLUS ] = "+",
3221[TOK_MINUS ] = "-",
3222[TOK_DIV ] = "/",
3223[TOK_MOD ] = "%",
3224[TOK_PLUSPLUS ] = "++",
3225[TOK_MINUSMINUS ] = "--",
3226[TOK_BANG ] = "!",
3227[TOK_ARROW ] = "->",
3228[TOK_DOT ] = ".",
3229[TOK_TILDE ] = "~",
3230[TOK_LIT_STRING ] = ":string:",
3231[TOK_IDENT ] = ":ident:",
3232[TOK_TYPE_NAME ] = ":typename:",
3233[TOK_LIT_CHAR ] = ":char:",
3234[TOK_LIT_INT ] = ":integer:",
3235[TOK_LIT_FLOAT ] = ":float:",
3236[TOK_MACRO ] = "#",
3237[TOK_CONCATENATE ] = "##",
3238
3239[TOK_AUTO ] = "auto",
3240[TOK_BREAK ] = "break",
3241[TOK_CASE ] = "case",
3242[TOK_CHAR ] = "char",
3243[TOK_CONST ] = "const",
3244[TOK_CONTINUE ] = "continue",
3245[TOK_DEFAULT ] = "default",
3246[TOK_DO ] = "do",
3247[TOK_DOUBLE ] = "double",
3248[TOK_ELSE ] = "else",
3249[TOK_ENUM ] = "enum",
3250[TOK_EXTERN ] = "extern",
3251[TOK_FLOAT ] = "float",
3252[TOK_FOR ] = "for",
3253[TOK_GOTO ] = "goto",
3254[TOK_IF ] = "if",
3255[TOK_INLINE ] = "inline",
3256[TOK_INT ] = "int",
3257[TOK_LONG ] = "long",
3258[TOK_REGISTER ] = "register",
3259[TOK_RESTRICT ] = "restrict",
3260[TOK_RETURN ] = "return",
3261[TOK_SHORT ] = "short",
3262[TOK_SIGNED ] = "signed",
3263[TOK_SIZEOF ] = "sizeof",
3264[TOK_STATIC ] = "static",
3265[TOK_STRUCT ] = "struct",
3266[TOK_SWITCH ] = "switch",
3267[TOK_TYPEDEF ] = "typedef",
3268[TOK_UNION ] = "union",
3269[TOK_UNSIGNED ] = "unsigned",
3270[TOK_VOID ] = "void",
3271[TOK_VOLATILE ] = "volatile",
3272[TOK_WHILE ] = "while",
3273[TOK_ASM ] = "asm",
3274[TOK_ATTRIBUTE ] = "__attribute__",
3275[TOK_ALIGNOF ] = "__alignof__",
3276
Eric Biederman41203d92004-11-08 09:31:09 +00003277[TOK_MDEFINE ] = "#define",
3278[TOK_MDEFINED ] = "#defined",
3279[TOK_MUNDEF ] = "#undef",
3280[TOK_MINCLUDE ] = "#include",
3281[TOK_MLINE ] = "#line",
3282[TOK_MERROR ] = "#error",
3283[TOK_MWARNING ] = "#warning",
3284[TOK_MPRAGMA ] = "#pragma",
3285[TOK_MIFDEF ] = "#ifdef",
3286[TOK_MIFNDEF ] = "#ifndef",
3287[TOK_MELIF ] = "#elif",
3288[TOK_MENDIF ] = "#endif",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003289
Eric Biederman41203d92004-11-08 09:31:09 +00003290[TOK_MIF ] = "#if",
3291[TOK_MELSE ] = "#else",
3292[TOK_MIDENT ] = "#:ident:",
3293[TOK_EOL ] = "EOL",
Eric Biedermanb138ac82003-04-22 18:44:01 +00003294[TOK_EOF ] = "EOF",
3295};
3296
3297static unsigned int hash(const char *str, int str_len)
3298{
3299 unsigned int hash;
3300 const char *end;
3301 end = str + str_len;
3302 hash = 0;
3303 for(; str < end; str++) {
3304 hash = (hash *263) + *str;
3305 }
3306 hash = hash & (HASH_TABLE_SIZE -1);
3307 return hash;
3308}
3309
3310static struct hash_entry *lookup(
3311 struct compile_state *state, const char *name, int name_len)
3312{
3313 struct hash_entry *entry;
3314 unsigned int index;
3315 index = hash(name, name_len);
3316 entry = state->hash_table[index];
3317 while(entry &&
3318 ((entry->name_len != name_len) ||
3319 (memcmp(entry->name, name, name_len) != 0))) {
3320 entry = entry->next;
3321 }
3322 if (!entry) {
3323 char *new_name;
3324 /* Get a private copy of the name */
3325 new_name = xmalloc(name_len + 1, "hash_name");
3326 memcpy(new_name, name, name_len);
3327 new_name[name_len] = '\0';
3328
3329 /* Create a new hash entry */
3330 entry = xcmalloc(sizeof(*entry), "hash_entry");
3331 entry->next = state->hash_table[index];
3332 entry->name = new_name;
3333 entry->name_len = name_len;
3334
3335 /* Place the new entry in the hash table */
3336 state->hash_table[index] = entry;
3337 }
3338 return entry;
3339}
3340
3341static void ident_to_keyword(struct compile_state *state, struct token *tk)
3342{
3343 struct hash_entry *entry;
3344 entry = tk->ident;
3345 if (entry && ((entry->tok == TOK_TYPE_NAME) ||
3346 (entry->tok == TOK_ENUM_CONST) ||
3347 ((entry->tok >= TOK_FIRST_KEYWORD) &&
3348 (entry->tok <= TOK_LAST_KEYWORD)))) {
3349 tk->tok = entry->tok;
3350 }
3351}
3352
3353static void ident_to_macro(struct compile_state *state, struct token *tk)
3354{
3355 struct hash_entry *entry;
3356 entry = tk->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00003357 if (!entry)
3358 return;
3359 if ((entry->tok >= TOK_FIRST_MACRO) && (entry->tok <= TOK_LAST_MACRO)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003360 tk->tok = entry->tok;
3361 }
Eric Biederman41203d92004-11-08 09:31:09 +00003362 else if (entry->tok == TOK_IF) {
3363 tk->tok = TOK_MIF;
3364 }
3365 else if (entry->tok == TOK_ELSE) {
3366 tk->tok = TOK_MELSE;
3367 }
3368 else {
3369 tk->tok = TOK_MIDENT;
3370 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00003371}
3372
3373static void hash_keyword(
3374 struct compile_state *state, const char *keyword, int tok)
3375{
3376 struct hash_entry *entry;
3377 entry = lookup(state, keyword, strlen(keyword));
3378 if (entry && entry->tok != TOK_UNKNOWN) {
3379 die("keyword %s already hashed", keyword);
3380 }
3381 entry->tok = tok;
3382}
3383
Eric Biederman90089602004-05-28 14:11:54 +00003384static void romcc_symbol(
Eric Biedermanb138ac82003-04-22 18:44:01 +00003385 struct compile_state *state, struct hash_entry *ident,
Eric Biederman90089602004-05-28 14:11:54 +00003386 struct symbol **chain, struct triple *def, struct type *type, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003387{
3388 struct symbol *sym;
Eric Biederman90089602004-05-28 14:11:54 +00003389 if (*chain && ((*chain)->scope_depth >= depth)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00003390 error(state, 0, "%s already defined", ident->name);
3391 }
3392 sym = xcmalloc(sizeof(*sym), "symbol");
3393 sym->ident = ident;
3394 sym->def = def;
3395 sym->type = type;
Eric Biederman90089602004-05-28 14:11:54 +00003396 sym->scope_depth = depth;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003397 sym->next = *chain;
3398 *chain = sym;
3399}
3400
Eric Biederman90089602004-05-28 14:11:54 +00003401static void symbol(
3402 struct compile_state *state, struct hash_entry *ident,
3403 struct symbol **chain, struct triple *def, struct type *type)
Eric Biederman153ea352003-06-20 14:43:20 +00003404{
Eric Biederman90089602004-05-28 14:11:54 +00003405 romcc_symbol(state, ident, chain, def, type, state->scope_depth);
3406}
3407
3408static void var_symbol(struct compile_state *state,
3409 struct hash_entry *ident, struct triple *def)
3410{
3411 if ((def->type->type & TYPE_MASK) == TYPE_PRODUCT) {
3412 internal_error(state, 0, "bad var type");
Eric Biederman153ea352003-06-20 14:43:20 +00003413 }
Eric Biederman90089602004-05-28 14:11:54 +00003414 symbol(state, ident, &ident->sym_ident, def, def->type);
3415}
3416
3417static void label_symbol(struct compile_state *state,
3418 struct hash_entry *ident, struct triple *label, int depth)
3419{
3420 romcc_symbol(state, ident, &ident->sym_label, label, &void_type, depth);
Eric Biederman153ea352003-06-20 14:43:20 +00003421}
3422
Eric Biedermanb138ac82003-04-22 18:44:01 +00003423static void start_scope(struct compile_state *state)
3424{
3425 state->scope_depth++;
3426}
3427
Eric Biederman90089602004-05-28 14:11:54 +00003428static void end_scope_syms(struct compile_state *state,
3429 struct symbol **chain, int depth)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003430{
3431 struct symbol *sym, *next;
3432 sym = *chain;
3433 while(sym && (sym->scope_depth == depth)) {
3434 next = sym->next;
3435 xfree(sym);
3436 sym = next;
3437 }
3438 *chain = sym;
3439}
3440
3441static void end_scope(struct compile_state *state)
3442{
3443 int i;
3444 int depth;
3445 /* Walk through the hash table and remove all symbols
3446 * in the current scope.
3447 */
3448 depth = state->scope_depth;
3449 for(i = 0; i < HASH_TABLE_SIZE; i++) {
3450 struct hash_entry *entry;
3451 entry = state->hash_table[i];
3452 while(entry) {
Eric Biederman90089602004-05-28 14:11:54 +00003453 end_scope_syms(state, &entry->sym_label, depth);
3454 end_scope_syms(state, &entry->sym_tag, depth);
3455 end_scope_syms(state, &entry->sym_ident, depth);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003456 entry = entry->next;
3457 }
3458 }
3459 state->scope_depth = depth - 1;
3460}
3461
3462static void register_keywords(struct compile_state *state)
3463{
3464 hash_keyword(state, "auto", TOK_AUTO);
3465 hash_keyword(state, "break", TOK_BREAK);
3466 hash_keyword(state, "case", TOK_CASE);
3467 hash_keyword(state, "char", TOK_CHAR);
3468 hash_keyword(state, "const", TOK_CONST);
3469 hash_keyword(state, "continue", TOK_CONTINUE);
3470 hash_keyword(state, "default", TOK_DEFAULT);
3471 hash_keyword(state, "do", TOK_DO);
3472 hash_keyword(state, "double", TOK_DOUBLE);
3473 hash_keyword(state, "else", TOK_ELSE);
3474 hash_keyword(state, "enum", TOK_ENUM);
3475 hash_keyword(state, "extern", TOK_EXTERN);
3476 hash_keyword(state, "float", TOK_FLOAT);
3477 hash_keyword(state, "for", TOK_FOR);
3478 hash_keyword(state, "goto", TOK_GOTO);
3479 hash_keyword(state, "if", TOK_IF);
3480 hash_keyword(state, "inline", TOK_INLINE);
3481 hash_keyword(state, "int", TOK_INT);
3482 hash_keyword(state, "long", TOK_LONG);
3483 hash_keyword(state, "register", TOK_REGISTER);
3484 hash_keyword(state, "restrict", TOK_RESTRICT);
3485 hash_keyword(state, "return", TOK_RETURN);
3486 hash_keyword(state, "short", TOK_SHORT);
3487 hash_keyword(state, "signed", TOK_SIGNED);
3488 hash_keyword(state, "sizeof", TOK_SIZEOF);
3489 hash_keyword(state, "static", TOK_STATIC);
3490 hash_keyword(state, "struct", TOK_STRUCT);
3491 hash_keyword(state, "switch", TOK_SWITCH);
3492 hash_keyword(state, "typedef", TOK_TYPEDEF);
3493 hash_keyword(state, "union", TOK_UNION);
3494 hash_keyword(state, "unsigned", TOK_UNSIGNED);
3495 hash_keyword(state, "void", TOK_VOID);
3496 hash_keyword(state, "volatile", TOK_VOLATILE);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003497 hash_keyword(state, "__volatile__", TOK_VOLATILE);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003498 hash_keyword(state, "while", TOK_WHILE);
3499 hash_keyword(state, "asm", TOK_ASM);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00003500 hash_keyword(state, "__asm__", TOK_ASM);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003501 hash_keyword(state, "__attribute__", TOK_ATTRIBUTE);
3502 hash_keyword(state, "__alignof__", TOK_ALIGNOF);
3503}
3504
3505static void register_macro_keywords(struct compile_state *state)
3506{
Eric Biederman41203d92004-11-08 09:31:09 +00003507 hash_keyword(state, "define", TOK_MDEFINE);
3508 hash_keyword(state, "defined", TOK_MDEFINED);
3509 hash_keyword(state, "undef", TOK_MUNDEF);
3510 hash_keyword(state, "include", TOK_MINCLUDE);
3511 hash_keyword(state, "line", TOK_MLINE);
3512 hash_keyword(state, "error", TOK_MERROR);
3513 hash_keyword(state, "warning", TOK_MWARNING);
3514 hash_keyword(state, "pragma", TOK_MPRAGMA);
3515 hash_keyword(state, "ifdef", TOK_MIFDEF);
3516 hash_keyword(state, "ifndef", TOK_MIFNDEF);
3517 hash_keyword(state, "elif", TOK_MELIF);
3518 hash_keyword(state, "endif", TOK_MENDIF);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003519}
3520
Eric Biederman90089602004-05-28 14:11:54 +00003521
3522static void undef_macro(struct compile_state *state, struct hash_entry *ident)
3523{
3524 if (ident->sym_define != 0) {
3525 struct macro *macro;
3526 struct macro_arg *arg, *anext;
3527 macro = ident->sym_define;
3528 ident->sym_define = 0;
3529
3530 /* Free the macro arguments... */
3531 anext = macro->args;
3532 while(anext) {
3533 arg = anext;
3534 anext = arg->next;
3535 xfree(arg);
3536 }
3537
3538 /* Free the macro buffer */
3539 xfree(macro->buf);
3540
3541 /* Now free the macro itself */
3542 xfree(macro);
3543 }
3544}
3545
Eric Biedermancb364952004-11-15 10:46:44 +00003546static void do_define_macro(struct compile_state *state,
3547 struct hash_entry *ident, const char *body,
3548 int argc, struct macro_arg *args)
Eric Biederman90089602004-05-28 14:11:54 +00003549{
3550 struct macro *macro;
3551 struct macro_arg *arg;
Eric Biedermancb364952004-11-15 10:46:44 +00003552 size_t body_len;
3553
3554 /* Find the length of the body */
3555 body_len = strlen(body);
Eric Biederman90089602004-05-28 14:11:54 +00003556 macro = ident->sym_define;
3557 if (macro != 0) {
Eric Biedermancb364952004-11-15 10:46:44 +00003558 int identical_bodies, identical_args;
3559 struct macro_arg *oarg;
3560 /* Explicitly allow identical redfinitions of the same macro */
3561 identical_bodies =
3562 (macro->buf_len == body_len) &&
3563 (memcmp(macro->buf, body, body_len) == 0);
3564 identical_args = macro->argc == argc;
3565 oarg = macro->args;
3566 arg = args;
3567 while(identical_args && arg) {
3568 identical_args = oarg->ident == arg->ident;
3569 arg = arg->next;
3570 oarg = oarg->next;
3571 }
3572 if (identical_bodies && identical_args) {
3573 xfree(body);
Eric Biederman90089602004-05-28 14:11:54 +00003574 return;
3575 }
3576 error(state, 0, "macro %s already defined\n", ident->name);
3577 }
3578#if 0
Eric Biedermancb364952004-11-15 10:46:44 +00003579 fprintf(state->errout, "#define %s: `%*.*s'\n",
3580 ident->name, body_len, body_len, body);
Eric Biederman90089602004-05-28 14:11:54 +00003581#endif
3582 macro = xmalloc(sizeof(*macro), "macro");
Eric Biedermancb364952004-11-15 10:46:44 +00003583 macro->ident = ident;
3584 macro->buf = body;
3585 macro->buf_len = body_len;
Eric Biederman90089602004-05-28 14:11:54 +00003586 macro->args = args;
Eric Biedermancb364952004-11-15 10:46:44 +00003587 macro->argc = argc;
Eric Biederman90089602004-05-28 14:11:54 +00003588
3589 ident->sym_define = macro;
3590}
Eric Biedermancb364952004-11-15 10:46:44 +00003591
3592static void define_macro(
3593 struct compile_state *state,
3594 struct hash_entry *ident,
3595 const char *body, int body_len,
3596 int argc, struct macro_arg *args)
3597{
3598 char *buf;
3599 buf = xmalloc(body_len + 1, "macro buf");
3600 memcpy(buf, body, body_len);
3601 buf[body_len] = '\0';
3602 do_define_macro(state, ident, buf, argc, args);
3603}
Eric Biederman90089602004-05-28 14:11:54 +00003604
3605static void register_builtin_macro(struct compile_state *state,
3606 const char *name, const char *value)
3607{
3608 struct hash_entry *ident;
3609
3610 if (value[0] == '(') {
3611 internal_error(state, 0, "Builtin macros with arguments not supported");
3612 }
3613 ident = lookup(state, name, strlen(name));
Eric Biedermancb364952004-11-15 10:46:44 +00003614 define_macro(state, ident, value, strlen(value), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003615}
3616
3617static void register_builtin_macros(struct compile_state *state)
3618{
3619 char buf[30];
3620 char scratch[30];
3621 time_t now;
3622 struct tm *tm;
3623 now = time(NULL);
3624 tm = localtime(&now);
3625
3626 register_builtin_macro(state, "__ROMCC__", VERSION_MAJOR);
3627 register_builtin_macro(state, "__ROMCC_MINOR__", VERSION_MINOR);
3628 register_builtin_macro(state, "__FILE__", "\"This should be the filename\"");
3629 register_builtin_macro(state, "__LINE__", "54321");
3630
3631 strftime(scratch, sizeof(scratch), "%b %e %Y", tm);
3632 sprintf(buf, "\"%s\"", scratch);
3633 register_builtin_macro(state, "__DATE__", buf);
3634
3635 strftime(scratch, sizeof(scratch), "%H:%M:%S", tm);
3636 sprintf(buf, "\"%s\"", scratch);
3637 register_builtin_macro(state, "__TIME__", buf);
3638
3639 /* I can't be a conforming implementation of C :( */
3640 register_builtin_macro(state, "__STDC__", "0");
3641 /* In particular I don't conform to C99 */
3642 register_builtin_macro(state, "__STDC_VERSION__", "199901L");
3643
3644}
3645
3646static void process_cmdline_macros(struct compile_state *state)
3647{
3648 const char **macro, *name;
3649 struct hash_entry *ident;
3650 for(macro = state->compiler->defines; (name = *macro); macro++) {
3651 const char *body;
3652 size_t name_len;
3653
3654 name_len = strlen(name);
3655 body = strchr(name, '=');
3656 if (!body) {
3657 body = "\0";
3658 } else {
3659 name_len = body - name;
3660 body++;
3661 }
3662 ident = lookup(state, name, name_len);
Eric Biedermancb364952004-11-15 10:46:44 +00003663 define_macro(state, ident, body, strlen(body), -1, 0);
Eric Biederman90089602004-05-28 14:11:54 +00003664 }
3665 for(macro = state->compiler->undefs; (name = *macro); macro++) {
3666 ident = lookup(state, name, strlen(name));
3667 undef_macro(state, ident);
3668 }
3669}
3670
Eric Biedermanb138ac82003-04-22 18:44:01 +00003671static int spacep(int c)
3672{
3673 int ret = 0;
3674 switch(c) {
3675 case ' ':
3676 case '\t':
3677 case '\f':
3678 case '\v':
3679 case '\r':
Eric Biederman41203d92004-11-08 09:31:09 +00003680 ret = 1;
3681 break;
3682 }
3683 return ret;
3684}
3685
Eric Biedermanb138ac82003-04-22 18:44:01 +00003686static int digitp(int c)
3687{
3688 int ret = 0;
3689 switch(c) {
3690 case '0': case '1': case '2': case '3': case '4':
3691 case '5': case '6': case '7': case '8': case '9':
3692 ret = 1;
3693 break;
3694 }
3695 return ret;
3696}
Eric Biederman8d9c1232003-06-17 08:42:17 +00003697static int digval(int c)
3698{
3699 int val = -1;
3700 if ((c >= '0') && (c <= '9')) {
3701 val = c - '0';
3702 }
3703 return val;
3704}
Eric Biedermanb138ac82003-04-22 18:44:01 +00003705
3706static int hexdigitp(int c)
3707{
3708 int ret = 0;
3709 switch(c) {
3710 case '0': case '1': case '2': case '3': case '4':
3711 case '5': case '6': case '7': case '8': case '9':
3712 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3713 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3714 ret = 1;
3715 break;
3716 }
3717 return ret;
3718}
3719static int hexdigval(int c)
3720{
3721 int val = -1;
3722 if ((c >= '0') && (c <= '9')) {
3723 val = c - '0';
3724 }
3725 else if ((c >= 'A') && (c <= 'F')) {
3726 val = 10 + (c - 'A');
3727 }
3728 else if ((c >= 'a') && (c <= 'f')) {
3729 val = 10 + (c - 'a');
3730 }
3731 return val;
3732}
3733
3734static int octdigitp(int c)
3735{
3736 int ret = 0;
3737 switch(c) {
3738 case '0': case '1': case '2': case '3':
3739 case '4': case '5': case '6': case '7':
3740 ret = 1;
3741 break;
3742 }
3743 return ret;
3744}
3745static int octdigval(int c)
3746{
3747 int val = -1;
3748 if ((c >= '0') && (c <= '7')) {
3749 val = c - '0';
3750 }
3751 return val;
3752}
3753
3754static int letterp(int c)
3755{
3756 int ret = 0;
3757 switch(c) {
3758 case 'a': case 'b': case 'c': case 'd': case 'e':
3759 case 'f': case 'g': case 'h': case 'i': case 'j':
3760 case 'k': case 'l': case 'm': case 'n': case 'o':
3761 case 'p': case 'q': case 'r': case 's': case 't':
3762 case 'u': case 'v': case 'w': case 'x': case 'y':
3763 case 'z':
3764 case 'A': case 'B': case 'C': case 'D': case 'E':
3765 case 'F': case 'G': case 'H': case 'I': case 'J':
3766 case 'K': case 'L': case 'M': case 'N': case 'O':
3767 case 'P': case 'Q': case 'R': case 'S': case 'T':
3768 case 'U': case 'V': case 'W': case 'X': case 'Y':
3769 case 'Z':
3770 case '_':
3771 ret = 1;
3772 break;
3773 }
3774 return ret;
3775}
3776
Eric Biederman90089602004-05-28 14:11:54 +00003777static const char *identifier(const char *str, const char *end)
3778{
3779 if (letterp(*str)) {
3780 for(; str < end; str++) {
3781 int c;
3782 c = *str;
3783 if (!letterp(c) && !digitp(c)) {
3784 break;
3785 }
3786 }
3787 }
3788 return str;
3789}
3790
Eric Biedermanb138ac82003-04-22 18:44:01 +00003791static int char_value(struct compile_state *state,
3792 const signed char **strp, const signed char *end)
3793{
3794 const signed char *str;
3795 int c;
3796 str = *strp;
3797 c = *str++;
3798 if ((c == '\\') && (str < end)) {
3799 switch(*str) {
3800 case 'n': c = '\n'; str++; break;
3801 case 't': c = '\t'; str++; break;
3802 case 'v': c = '\v'; str++; break;
3803 case 'b': c = '\b'; str++; break;
3804 case 'r': c = '\r'; str++; break;
3805 case 'f': c = '\f'; str++; break;
3806 case 'a': c = '\a'; str++; break;
3807 case '\\': c = '\\'; str++; break;
3808 case '?': c = '?'; str++; break;
3809 case '\'': c = '\''; str++; break;
Eric Biederman90089602004-05-28 14:11:54 +00003810 case '"': c = '"'; str++; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00003811 case 'x':
3812 c = 0;
3813 str++;
3814 while((str < end) && hexdigitp(*str)) {
3815 c <<= 4;
3816 c += hexdigval(*str);
3817 str++;
3818 }
3819 break;
3820 case '0': case '1': case '2': case '3':
3821 case '4': case '5': case '6': case '7':
3822 c = 0;
3823 while((str < end) && octdigitp(*str)) {
3824 c <<= 3;
3825 c += octdigval(*str);
3826 str++;
3827 }
3828 break;
3829 default:
3830 error(state, 0, "Invalid character constant");
3831 break;
3832 }
3833 }
3834 *strp = str;
3835 return c;
3836}
3837
Eric Biedermancb364952004-11-15 10:46:44 +00003838static const char *next_char(struct file_state *file, const char *pos, int index)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003839{
Eric Biedermancb364952004-11-15 10:46:44 +00003840 const char *end = file->buf + file->size;
3841 while(pos < end) {
3842 /* Lookup the character */
3843 int size = 1;
3844 int c = *pos;
3845 /* Is this a trigraph? */
3846 if (file->trigraphs &&
3847 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3848 {
3849 switch(pos[2]) {
3850 case '=': c = '#'; break;
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 }
3860 if (c != '?') {
3861 size = 3;
3862 }
3863 }
3864 /* Is this an escaped newline? */
3865 if (file->join_lines &&
Patrick Georgi26774f22009-11-21 19:54:02 +00003866 (c == '\\') && (pos + size < end) && ((pos[1] == '\n') || ((pos[1] == '\r') && (pos[2] == '\n'))))
Eric Biedermancb364952004-11-15 10:46:44 +00003867 {
Patrick Georgi26774f22009-11-21 19:54:02 +00003868 int cr_offset = ((pos[1] == '\r') && (pos[2] == '\n'))?1:0;
Eric Biedermancb364952004-11-15 10:46:44 +00003869 /* At the start of a line just eat it */
3870 if (pos == file->pos) {
3871 file->line++;
3872 file->report_line++;
Patrick Georgi26774f22009-11-21 19:54:02 +00003873 file->line_start = pos + size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003874 }
Patrick Georgi26774f22009-11-21 19:54:02 +00003875 pos += size + 1 + cr_offset;
Eric Biedermancb364952004-11-15 10:46:44 +00003876 }
3877 /* Do I need to ga any farther? */
3878 else if (index == 0) {
3879 break;
3880 }
3881 /* Process a normal character */
3882 else {
3883 pos += size;
3884 index -= 1;
3885 }
3886 }
3887 return pos;
3888}
3889
3890static int get_char(struct file_state *file, const char *pos)
3891{
3892 const char *end = file->buf + file->size;
3893 int c;
3894 c = -1;
3895 pos = next_char(file, pos, 0);
3896 if (pos < end) {
3897 /* Lookup the character */
3898 c = *pos;
3899 /* If it is a trigraph get the trigraph value */
3900 if (file->trigraphs &&
3901 (c == '?') && ((end - pos) >= 3) && (pos[1] == '?'))
3902 {
3903 switch(pos[2]) {
3904 case '=': c = '#'; break;
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 }
3914 }
3915 }
3916 return c;
3917}
3918
3919static void eat_chars(struct file_state *file, const char *targ)
3920{
3921 const char *pos = file->pos;
3922 while(pos < targ) {
3923 /* Do we have a newline? */
3924 if (pos[0] == '\n') {
3925 file->line++;
3926 file->report_line++;
3927 file->line_start = pos + 1;
3928 }
3929 pos++;
3930 }
3931 file->pos = pos;
3932}
3933
3934
3935static size_t char_strlen(struct file_state *file, const char *src, const char *end)
3936{
3937 size_t len;
3938 len = 0;
3939 while(src < end) {
3940 src = next_char(file, src, 1);
3941 len++;
3942 }
3943 return len;
3944}
3945
3946static void char_strcpy(char *dest,
3947 struct file_state *file, const char *src, const char *end)
3948{
3949 while(src < end) {
3950 int c;
3951 c = get_char(file, src);
3952 src = next_char(file, src, 1);
3953 *dest++ = c;
3954 }
3955}
3956
3957static char *char_strdup(struct file_state *file,
3958 const char *start, const char *end, const char *id)
3959{
3960 char *str;
3961 size_t str_len;
3962 str_len = char_strlen(file, start, end);
3963 str = xcmalloc(str_len + 1, id);
3964 char_strcpy(str, file, start, end);
3965 str[str_len] = '\0';
3966 return str;
3967}
3968
3969static const char *after_digits(struct file_state *file, const char *ptr)
3970{
3971 while(digitp(get_char(file, ptr))) {
3972 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003973 }
3974 return ptr;
3975}
3976
Eric Biedermancb364952004-11-15 10:46:44 +00003977static const char *after_octdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003978{
Eric Biedermancb364952004-11-15 10:46:44 +00003979 while(octdigitp(get_char(file, ptr))) {
3980 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003981 }
3982 return ptr;
3983}
3984
Eric Biedermancb364952004-11-15 10:46:44 +00003985static const char *after_hexdigits(struct file_state *file, const char *ptr)
Eric Biedermanb138ac82003-04-22 18:44:01 +00003986{
Eric Biedermancb364952004-11-15 10:46:44 +00003987 while(hexdigitp(get_char(file, ptr))) {
3988 ptr = next_char(file, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00003989 }
3990 return ptr;
3991}
3992
Eric Biedermancb364952004-11-15 10:46:44 +00003993static const char *after_alnums(struct file_state *file, const char *ptr)
3994{
3995 int c;
3996 c = get_char(file, ptr);
3997 while(letterp(c) || digitp(c)) {
3998 ptr = next_char(file, ptr, 1);
3999 c = get_char(file, ptr);
4000 }
4001 return ptr;
4002}
4003
4004static void save_string(struct file_state *file,
Eric Biederman90089602004-05-28 14:11:54 +00004005 struct token *tk, const char *start, const char *end, const char *id)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004006{
4007 char *str;
Eric Biedermancb364952004-11-15 10:46:44 +00004008
Eric Biedermanb138ac82003-04-22 18:44:01 +00004009 /* Create a private copy of the string */
Eric Biedermancb364952004-11-15 10:46:44 +00004010 str = char_strdup(file, start, end, id);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004011
4012 /* Store the copy in the token */
4013 tk->val.str = str;
Eric Biedermancb364952004-11-15 10:46:44 +00004014 tk->str_len = strlen(str);
Eric Biederman90089602004-05-28 14:11:54 +00004015}
4016
4017static void raw_next_token(struct compile_state *state,
4018 struct file_state *file, struct token *tk)
4019{
4020 const char *token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004021 int c, c1, c2, c3;
Eric Biedermancb364952004-11-15 10:46:44 +00004022 const char *tokp;
4023 int eat;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004024 int tok;
Eric Biederman90089602004-05-28 14:11:54 +00004025
Eric Biedermanb138ac82003-04-22 18:44:01 +00004026 tk->str_len = 0;
4027 tk->ident = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004028 token = tokp = next_char(file, file->pos, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004029 tok = TOK_UNKNOWN;
Eric Biedermancb364952004-11-15 10:46:44 +00004030 c = get_char(file, tokp);
4031 tokp = next_char(file, tokp, 1);
4032 eat = 0;
4033 c1 = get_char(file, tokp);
4034 c2 = get_char(file, next_char(file, tokp, 1));
4035 c3 = get_char(file, next_char(file, tokp, 2));
4036
4037 /* The end of the file */
4038 if (c == -1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004039 tok = TOK_EOF;
Eric Biederman41203d92004-11-08 09:31:09 +00004040 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004041 /* Whitespace */
4042 else if (spacep(c)) {
4043 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004044 while (spacep(get_char(file, tokp))) {
4045 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004046 }
4047 }
4048 /* EOL Comments */
4049 else if ((c == '/') && (c1 == '/')) {
4050 tok = TOK_SPACE;
Eric Biedermancb364952004-11-15 10:46:44 +00004051 tokp = next_char(file, tokp, 1);
4052 while((c = get_char(file, tokp)) != -1) {
Eric Biederman57183382006-12-02 16:48:48 +00004053 /* Advance to the next character only after we verify
4054 * the current character is not a newline.
4055 * EOL is special to the preprocessor so we don't
4056 * want to loose any.
4057 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004058 if (c == '\n') {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004059 break;
4060 }
Eric Biederman57183382006-12-02 16:48:48 +00004061 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004062 }
4063 }
4064 /* Comments */
4065 else if ((c == '/') && (c1 == '*')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004066 tokp = next_char(file, tokp, 2);
4067 c = c2;
4068 while((c1 = get_char(file, tokp)) != -1) {
4069 tokp = next_char(file, tokp, 1);
4070 if ((c == '*') && (c1 == '/')) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004071 tok = TOK_SPACE;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004072 break;
4073 }
Eric Biedermancb364952004-11-15 10:46:44 +00004074 c = c1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004075 }
4076 if (tok == TOK_UNKNOWN) {
4077 error(state, 0, "unterminated comment");
4078 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004079 }
4080 /* string constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004081 else if ((c == '"') || ((c == 'L') && (c1 == '"'))) {
4082 int wchar, multiline;
4083
Eric Biedermanb138ac82003-04-22 18:44:01 +00004084 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004085 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004086 if (c == 'L') {
4087 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004088 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004089 }
Eric Biedermancb364952004-11-15 10:46:44 +00004090 while((c = get_char(file, tokp)) != -1) {
4091 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004092 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004093 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004094 }
Eric Biedermancb364952004-11-15 10:46:44 +00004095 else if (c == '\\') {
4096 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004097 }
4098 else if (c == '"') {
4099 tok = TOK_LIT_STRING;
4100 break;
4101 }
4102 }
4103 if (tok == TOK_UNKNOWN) {
4104 error(state, 0, "unterminated string constant");
4105 }
Eric Biedermancb364952004-11-15 10:46:44 +00004106 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004107 warning(state, 0, "multiline string constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004108 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004109
4110 /* Save the string value */
Eric Biedermancb364952004-11-15 10:46:44 +00004111 save_string(file, tk, token, tokp, "literal string");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004112 }
4113 /* character constants */
Eric Biedermancb364952004-11-15 10:46:44 +00004114 else if ((c == '\'') || ((c == 'L') && (c1 == '\''))) {
4115 int wchar, multiline;
4116
Eric Biedermanb138ac82003-04-22 18:44:01 +00004117 wchar = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004118 multiline = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004119 if (c == 'L') {
4120 wchar = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004121 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004122 }
Eric Biedermancb364952004-11-15 10:46:44 +00004123 while((c = get_char(file, tokp)) != -1) {
4124 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004125 if (c == '\n') {
Eric Biedermancb364952004-11-15 10:46:44 +00004126 multiline = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004127 }
Eric Biedermancb364952004-11-15 10:46:44 +00004128 else if (c == '\\') {
4129 tokp = next_char(file, tokp, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004130 }
4131 else if (c == '\'') {
4132 tok = TOK_LIT_CHAR;
4133 break;
4134 }
4135 }
4136 if (tok == TOK_UNKNOWN) {
4137 error(state, 0, "unterminated character constant");
4138 }
Eric Biedermancb364952004-11-15 10:46:44 +00004139 if (multiline) {
Eric Biedermana649a412004-11-09 00:35:39 +00004140 warning(state, 0, "multiline character constant");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004141 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004142
4143 /* Save the character value */
Eric Biedermancb364952004-11-15 10:46:44 +00004144 save_string(file, tk, token, tokp, "literal character");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004145 }
4146 /* integer and floating constants
4147 * Integer Constants
4148 * {digits}
4149 * 0[Xx]{hexdigits}
4150 * 0{octdigit}+
4151 *
4152 * Floating constants
4153 * {digits}.{digits}[Ee][+-]?{digits}
4154 * {digits}.{digits}
4155 * {digits}[Ee][+-]?{digits}
4156 * .{digits}[Ee][+-]?{digits}
4157 * .{digits}
4158 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004159 else if (digitp(c) || ((c == '.') && (digitp(c1)))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004160 const char *next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004161 int is_float;
Eric Biedermancb364952004-11-15 10:46:44 +00004162 int cn;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004163 is_float = 0;
4164 if (c != '.') {
Eric Biedermancb364952004-11-15 10:46:44 +00004165 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004166 }
4167 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004168 next = token;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004169 }
Eric Biedermancb364952004-11-15 10:46:44 +00004170 cn = get_char(file, next);
4171 if (cn == '.') {
4172 next = next_char(file, next, 1);
4173 next = after_digits(file, next);
4174 is_float = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004175 }
Eric Biedermancb364952004-11-15 10:46:44 +00004176 cn = get_char(file, next);
4177 if ((cn == 'e') || (cn == 'E')) {
4178 const char *new;
4179 next = next_char(file, next, 1);
4180 cn = get_char(file, next);
4181 if ((cn == '+') || (cn == '-')) {
4182 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004183 }
Eric Biedermancb364952004-11-15 10:46:44 +00004184 new = after_digits(file, next);
4185 is_float |= (new != next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004186 next = new;
4187 }
4188 if (is_float) {
4189 tok = TOK_LIT_FLOAT;
Eric Biedermancb364952004-11-15 10:46:44 +00004190 cn = get_char(file, next);
4191 if ((cn == 'f') || (cn == 'F') || (cn == 'l') || (cn == 'L')) {
4192 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004193 }
4194 }
4195 if (!is_float && digitp(c)) {
4196 tok = TOK_LIT_INT;
4197 if ((c == '0') && ((c1 == 'x') || (c1 == 'X'))) {
Eric Biedermancb364952004-11-15 10:46:44 +00004198 next = next_char(file, tokp, 1);
4199 next = after_hexdigits(file, next);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004200 }
4201 else if (c == '0') {
Eric Biedermancb364952004-11-15 10:46:44 +00004202 next = after_octdigits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004203 }
4204 else {
Eric Biedermancb364952004-11-15 10:46:44 +00004205 next = after_digits(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004206 }
4207 /* crazy integer suffixes */
Eric Biedermancb364952004-11-15 10:46:44 +00004208 cn = get_char(file, next);
4209 if ((cn == 'u') || (cn == 'U')) {
4210 next = next_char(file, next, 1);
4211 cn = get_char(file, next);
4212 if ((cn == 'l') || (cn == 'L')) {
4213 next = next_char(file, next, 1);
4214 cn = get_char(file, next);
4215 }
4216 if ((cn == 'l') || (cn == 'L')) {
4217 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004218 }
4219 }
Eric Biedermancb364952004-11-15 10:46:44 +00004220 else if ((cn == 'l') || (cn == 'L')) {
4221 next = next_char(file, next, 1);
4222 cn = get_char(file, next);
4223 if ((cn == 'l') || (cn == 'L')) {
4224 next = next_char(file, next, 1);
4225 cn = get_char(file, next);
4226 }
4227 if ((cn == 'u') || (cn == 'U')) {
4228 next = next_char(file, next, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004229 }
4230 }
4231 }
Eric Biedermancb364952004-11-15 10:46:44 +00004232 tokp = next;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004233
4234 /* Save the integer/floating point value */
Eric Biedermancb364952004-11-15 10:46:44 +00004235 save_string(file, tk, token, tokp, "literal number");
Eric Biedermanb138ac82003-04-22 18:44:01 +00004236 }
4237 /* identifiers */
4238 else if (letterp(c)) {
4239 tok = TOK_IDENT;
Eric Biedermancb364952004-11-15 10:46:44 +00004240
4241 /* Find and save the identifier string */
4242 tokp = after_alnums(file, tokp);
4243 save_string(file, tk, token, tokp, "identifier");
4244
4245 /* Look up to see which identifier it is */
4246 tk->ident = lookup(state, tk->val.str, tk->str_len);
4247
4248 /* Free the identifier string */
4249 tk->str_len = 0;
4250 xfree(tk->val.str);
4251
Eric Biederman90089602004-05-28 14:11:54 +00004252 /* See if this identifier can be macro expanded */
4253 tk->val.notmacro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004254 c = get_char(file, tokp);
4255 if (c == '$') {
4256 tokp = next_char(file, tokp, 1);
Eric Biederman90089602004-05-28 14:11:54 +00004257 tk->val.notmacro = 1;
4258 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004259 }
4260 /* C99 alternate macro characters */
4261 else if ((c == '%') && (c1 == ':') && (c2 == '%') && (c3 == ':')) {
Eric Biedermancb364952004-11-15 10:46:44 +00004262 eat += 3;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004263 tok = TOK_CONCATENATE;
4264 }
Eric Biedermancb364952004-11-15 10:46:44 +00004265 else if ((c == '.') && (c1 == '.') && (c2 == '.')) { eat += 2; tok = TOK_DOTS; }
4266 else if ((c == '<') && (c1 == '<') && (c2 == '=')) { eat += 2; tok = TOK_SLEQ; }
4267 else if ((c == '>') && (c1 == '>') && (c2 == '=')) { eat += 2; tok = TOK_SREQ; }
4268 else if ((c == '*') && (c1 == '=')) { eat += 1; tok = TOK_TIMESEQ; }
4269 else if ((c == '/') && (c1 == '=')) { eat += 1; tok = TOK_DIVEQ; }
4270 else if ((c == '%') && (c1 == '=')) { eat += 1; tok = TOK_MODEQ; }
4271 else if ((c == '+') && (c1 == '=')) { eat += 1; tok = TOK_PLUSEQ; }
4272 else if ((c == '-') && (c1 == '=')) { eat += 1; tok = TOK_MINUSEQ; }
4273 else if ((c == '&') && (c1 == '=')) { eat += 1; tok = TOK_ANDEQ; }
4274 else if ((c == '^') && (c1 == '=')) { eat += 1; tok = TOK_XOREQ; }
4275 else if ((c == '|') && (c1 == '=')) { eat += 1; tok = TOK_OREQ; }
4276 else if ((c == '=') && (c1 == '=')) { eat += 1; tok = TOK_EQEQ; }
4277 else if ((c == '!') && (c1 == '=')) { eat += 1; tok = TOK_NOTEQ; }
4278 else if ((c == '|') && (c1 == '|')) { eat += 1; tok = TOK_LOGOR; }
4279 else if ((c == '&') && (c1 == '&')) { eat += 1; tok = TOK_LOGAND; }
4280 else if ((c == '<') && (c1 == '=')) { eat += 1; tok = TOK_LESSEQ; }
4281 else if ((c == '>') && (c1 == '=')) { eat += 1; tok = TOK_MOREEQ; }
4282 else if ((c == '<') && (c1 == '<')) { eat += 1; tok = TOK_SL; }
4283 else if ((c == '>') && (c1 == '>')) { eat += 1; tok = TOK_SR; }
4284 else if ((c == '+') && (c1 == '+')) { eat += 1; tok = TOK_PLUSPLUS; }
4285 else if ((c == '-') && (c1 == '-')) { eat += 1; tok = TOK_MINUSMINUS; }
4286 else if ((c == '-') && (c1 == '>')) { eat += 1; tok = TOK_ARROW; }
4287 else if ((c == '<') && (c1 == ':')) { eat += 1; tok = TOK_LBRACKET; }
4288 else if ((c == ':') && (c1 == '>')) { eat += 1; tok = TOK_RBRACKET; }
4289 else if ((c == '<') && (c1 == '%')) { eat += 1; tok = TOK_LBRACE; }
4290 else if ((c == '%') && (c1 == '>')) { eat += 1; tok = TOK_RBRACE; }
4291 else if ((c == '%') && (c1 == ':')) { eat += 1; tok = TOK_MACRO; }
4292 else if ((c == '#') && (c1 == '#')) { eat += 1; tok = TOK_CONCATENATE; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004293 else if (c == ';') { tok = TOK_SEMI; }
4294 else if (c == '{') { tok = TOK_LBRACE; }
4295 else if (c == '}') { tok = TOK_RBRACE; }
4296 else if (c == ',') { tok = TOK_COMMA; }
4297 else if (c == '=') { tok = TOK_EQ; }
4298 else if (c == ':') { tok = TOK_COLON; }
4299 else if (c == '[') { tok = TOK_LBRACKET; }
4300 else if (c == ']') { tok = TOK_RBRACKET; }
4301 else if (c == '(') { tok = TOK_LPAREN; }
4302 else if (c == ')') { tok = TOK_RPAREN; }
4303 else if (c == '*') { tok = TOK_STAR; }
4304 else if (c == '>') { tok = TOK_MORE; }
4305 else if (c == '<') { tok = TOK_LESS; }
4306 else if (c == '?') { tok = TOK_QUEST; }
4307 else if (c == '|') { tok = TOK_OR; }
4308 else if (c == '&') { tok = TOK_AND; }
4309 else if (c == '^') { tok = TOK_XOR; }
4310 else if (c == '+') { tok = TOK_PLUS; }
4311 else if (c == '-') { tok = TOK_MINUS; }
4312 else if (c == '/') { tok = TOK_DIV; }
4313 else if (c == '%') { tok = TOK_MOD; }
4314 else if (c == '!') { tok = TOK_BANG; }
4315 else if (c == '.') { tok = TOK_DOT; }
4316 else if (c == '~') { tok = TOK_TILDE; }
4317 else if (c == '#') { tok = TOK_MACRO; }
Eric Biedermancb364952004-11-15 10:46:44 +00004318 else if (c == '\n') { tok = TOK_EOL; }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004319
Eric Biedermancb364952004-11-15 10:46:44 +00004320 tokp = next_char(file, tokp, eat);
4321 eat_chars(file, tokp);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004322 tk->tok = tok;
Eric Biedermancb364952004-11-15 10:46:44 +00004323 tk->pos = token;
Eric Biederman90089602004-05-28 14:11:54 +00004324}
4325
4326static void check_tok(struct compile_state *state, struct token *tk, int tok)
4327{
4328 if (tk->tok != tok) {
4329 const char *name1, *name2;
4330 name1 = tokens[tk->tok];
4331 name2 = "";
Eric Biederman41203d92004-11-08 09:31:09 +00004332 if ((tk->tok == TOK_IDENT) || (tk->tok == TOK_MIDENT)) {
Eric Biederman90089602004-05-28 14:11:54 +00004333 name2 = tk->ident->name;
4334 }
4335 error(state, 0, "\tfound %s %s expected %s",
4336 name1, name2, tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004337 }
4338}
4339
Eric Biederman90089602004-05-28 14:11:54 +00004340struct macro_arg_value {
4341 struct hash_entry *ident;
Stefan Reinauer50542a82007-10-24 11:14:14 +00004342 char *value;
Eric Biederman90089602004-05-28 14:11:54 +00004343 size_t len;
4344};
4345static struct macro_arg_value *read_macro_args(
4346 struct compile_state *state, struct macro *macro,
4347 struct file_state *file, struct token *tk)
4348{
4349 struct macro_arg_value *argv;
4350 struct macro_arg *arg;
4351 int paren_depth;
4352 int i;
4353
4354 if (macro->argc == 0) {
4355 do {
4356 raw_next_token(state, file, tk);
4357 } while(tk->tok == TOK_SPACE);
Eric Biedermancb364952004-11-15 10:46:44 +00004358 return NULL;
Eric Biederman90089602004-05-28 14:11:54 +00004359 }
4360 argv = xcmalloc(sizeof(*argv) * macro->argc, "macro args");
4361 for(i = 0, arg = macro->args; arg; arg = arg->next, i++) {
4362 argv[i].value = 0;
4363 argv[i].len = 0;
4364 argv[i].ident = arg->ident;
4365 }
4366 paren_depth = 0;
4367 i = 0;
4368
4369 for(;;) {
4370 const char *start;
4371 size_t len;
4372 start = file->pos;
4373 raw_next_token(state, file, tk);
4374
4375 if (!paren_depth && (tk->tok == TOK_COMMA) &&
4376 (argv[i].ident != state->i___VA_ARGS__))
4377 {
4378 i++;
4379 if (i >= macro->argc) {
4380 error(state, 0, "too many args to %s\n",
4381 macro->ident->name);
4382 }
4383 continue;
4384 }
4385
4386 if (tk->tok == TOK_LPAREN) {
4387 paren_depth++;
4388 }
4389
4390 if (tk->tok == TOK_RPAREN) {
4391 if (paren_depth == 0) {
4392 break;
4393 }
4394 paren_depth--;
4395 }
4396 if (tk->tok == TOK_EOF) {
4397 error(state, 0, "End of file encountered while parsing macro arguments");
4398 }
Eric Biedermancb364952004-11-15 10:46:44 +00004399
4400 len = char_strlen(file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004401 argv[i].value = xrealloc(
4402 argv[i].value, argv[i].len + len, "macro args");
Stefan Reinauer50542a82007-10-24 11:14:14 +00004403 char_strcpy((char *)argv[i].value + argv[i].len, file, start, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004404 argv[i].len += len;
4405 }
4406 if (i != macro->argc -1) {
4407 error(state, 0, "missing %s arg %d\n",
4408 macro->ident->name, i +2);
4409 }
4410 return argv;
4411}
4412
4413
4414static void free_macro_args(struct macro *macro, struct macro_arg_value *argv)
4415{
4416 int i;
4417 for(i = 0; i < macro->argc; i++) {
4418 xfree(argv[i].value);
4419 }
4420 xfree(argv);
4421}
4422
4423struct macro_buf {
4424 char *str;
4425 size_t len, pos;
4426};
4427
Eric Biedermancb364952004-11-15 10:46:44 +00004428static void grow_macro_buf(struct compile_state *state,
4429 const char *id, struct macro_buf *buf,
4430 size_t grow)
4431{
4432 if ((buf->pos + grow) >= buf->len) {
4433 buf->str = xrealloc(buf->str, buf->len + grow, id);
4434 buf->len += grow;
4435 }
4436}
4437
Eric Biederman90089602004-05-28 14:11:54 +00004438static void append_macro_text(struct compile_state *state,
Eric Biedermancb364952004-11-15 10:46:44 +00004439 const char *id, struct macro_buf *buf,
Eric Biederman90089602004-05-28 14:11:54 +00004440 const char *fstart, size_t flen)
4441{
Eric Biedermancb364952004-11-15 10:46:44 +00004442 grow_macro_buf(state, id, buf, flen);
4443 memcpy(buf->str + buf->pos, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004444#if 0
4445 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4446 buf->pos, buf->pos, buf->str,
Eric Biedermancb364952004-11-15 10:46:44 +00004447 flen, flen, buf->str + buf->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004448#endif
Eric Biedermancb364952004-11-15 10:46:44 +00004449 buf->pos += flen;
4450}
4451
4452
4453static void append_macro_chars(struct compile_state *state,
4454 const char *id, struct macro_buf *buf,
4455 struct file_state *file, const char *start, const char *end)
4456{
4457 size_t flen;
4458 flen = char_strlen(file, start, end);
4459 grow_macro_buf(state, id, buf, flen);
4460 char_strcpy(buf->str + buf->pos, file, start, end);
4461#if 0
4462 fprintf(state->errout, "append: `%*.*s' `%*.*s'\n",
4463 buf->pos, buf->pos, buf->str,
4464 flen, flen, buf->str + buf->pos);
4465#endif
Eric Biederman90089602004-05-28 14:11:54 +00004466 buf->pos += flen;
4467}
4468
4469static int compile_macro(struct compile_state *state,
4470 struct file_state **filep, struct token *tk);
4471
4472static void macro_expand_args(struct compile_state *state,
4473 struct macro *macro, struct macro_arg_value *argv, struct token *tk)
4474{
Eric Biedermancb364952004-11-15 10:46:44 +00004475 int i;
Eric Biederman90089602004-05-28 14:11:54 +00004476
4477 for(i = 0; i < macro->argc; i++) {
4478 struct file_state fmacro, *file;
4479 struct macro_buf buf;
Eric Biederman90089602004-05-28 14:11:54 +00004480
Eric Biedermancb364952004-11-15 10:46:44 +00004481 fmacro.prev = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004482 fmacro.basename = argv[i].ident->name;
4483 fmacro.dirname = "";
Stefan Reinauer50542a82007-10-24 11:14:14 +00004484 fmacro.buf = (char *)argv[i].value;
Eric Biedermancb364952004-11-15 10:46:44 +00004485 fmacro.size = argv[i].len;
Eric Biederman90089602004-05-28 14:11:54 +00004486 fmacro.pos = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004487 fmacro.line = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004488 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004489 fmacro.report_line = 1;
4490 fmacro.report_name = fmacro.basename;
4491 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004492 fmacro.macro = 1;
4493 fmacro.trigraphs = 0;
4494 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004495
4496 buf.len = argv[i].len;
4497 buf.str = xmalloc(buf.len, argv[i].ident->name);
4498 buf.pos = 0;
4499
4500 file = &fmacro;
4501 for(;;) {
Eric Biederman90089602004-05-28 14:11:54 +00004502 raw_next_token(state, file, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004503
Eric Biedermancb364952004-11-15 10:46:44 +00004504 /* If we have recursed into another macro body
4505 * get out of it.
4506 */
Eric Biederman90089602004-05-28 14:11:54 +00004507 if (tk->tok == TOK_EOF) {
4508 struct file_state *old;
4509 old = file;
4510 file = file->prev;
4511 if (!file) {
4512 break;
4513 }
4514 /* old->basename is used keep it */
4515 xfree(old->dirname);
4516 xfree(old->buf);
4517 xfree(old);
4518 continue;
4519 }
4520 else if (tk->ident && tk->ident->sym_define) {
4521 if (compile_macro(state, &file, tk)) {
4522 continue;
4523 }
4524 }
4525
Eric Biedermancb364952004-11-15 10:46:44 +00004526 append_macro_chars(state, macro->ident->name, &buf,
4527 file, tk->pos, file->pos);
Eric Biederman90089602004-05-28 14:11:54 +00004528 }
4529
4530 xfree(argv[i].value);
4531 argv[i].value = buf.str;
4532 argv[i].len = buf.pos;
4533 }
4534 return;
4535}
4536
4537static void expand_macro(struct compile_state *state,
4538 struct macro *macro, struct macro_buf *buf,
4539 struct macro_arg_value *argv, struct token *tk)
4540{
4541 struct file_state fmacro;
4542 const char space[] = " ";
4543 const char *fstart;
4544 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004545 int i, j;
4546
4547 /* Place the macro body in a dummy file */
4548 fmacro.prev = 0;
4549 fmacro.basename = macro->ident->name;
4550 fmacro.dirname = "";
4551 fmacro.buf = macro->buf;
4552 fmacro.size = macro->buf_len;
4553 fmacro.pos = fmacro.buf;
4554 fmacro.line = 1;
4555 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004556 fmacro.report_line = 1;
4557 fmacro.report_name = fmacro.basename;
4558 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004559 fmacro.macro = 1;
4560 fmacro.trigraphs = 0;
4561 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004562
Eric Biedermancb364952004-11-15 10:46:44 +00004563 /* Allocate a buffer to hold the macro expansion */
Eric Biederman90089602004-05-28 14:11:54 +00004564 buf->len = macro->buf_len + 3;
4565 buf->str = xmalloc(buf->len, macro->ident->name);
4566 buf->pos = 0;
4567
4568 fstart = fmacro.pos;
4569 raw_next_token(state, &fmacro, tk);
4570 while(tk->tok != TOK_EOF) {
4571 flen = fmacro.pos - fstart;
4572 switch(tk->tok) {
4573 case TOK_IDENT:
4574 for(i = 0; i < macro->argc; i++) {
4575 if (argv[i].ident == tk->ident) {
4576 break;
4577 }
4578 }
4579 if (i >= macro->argc) {
4580 break;
4581 }
4582 /* Substitute macro parameter */
4583 fstart = argv[i].value;
4584 flen = argv[i].len;
4585 break;
4586 case TOK_MACRO:
Eric Biedermancb364952004-11-15 10:46:44 +00004587 if (macro->argc < 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004588 break;
4589 }
4590 do {
4591 raw_next_token(state, &fmacro, tk);
4592 } while(tk->tok == TOK_SPACE);
4593 check_tok(state, tk, TOK_IDENT);
4594 for(i = 0; i < macro->argc; i++) {
4595 if (argv[i].ident == tk->ident) {
4596 break;
4597 }
4598 }
4599 if (i >= macro->argc) {
4600 error(state, 0, "parameter `%s' not found",
4601 tk->ident->name);
4602 }
4603 /* Stringize token */
Eric Biedermancb364952004-11-15 10:46:44 +00004604 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004605 for(j = 0; j < argv[i].len; j++) {
4606 char *str = argv[i].value + j;
4607 size_t len = 1;
4608 if (*str == '\\') {
4609 str = "\\";
4610 len = 2;
4611 }
4612 else if (*str == '"') {
4613 str = "\\\"";
4614 len = 2;
4615 }
Eric Biedermancb364952004-11-15 10:46:44 +00004616 append_macro_text(state, macro->ident->name, buf, str, len);
Eric Biederman90089602004-05-28 14:11:54 +00004617 }
Eric Biedermancb364952004-11-15 10:46:44 +00004618 append_macro_text(state, macro->ident->name, buf, "\"", 1);
Eric Biederman90089602004-05-28 14:11:54 +00004619 fstart = 0;
4620 flen = 0;
4621 break;
4622 case TOK_CONCATENATE:
4623 /* Concatenate tokens */
4624 /* Delete the previous whitespace token */
4625 if (buf->str[buf->pos - 1] == ' ') {
4626 buf->pos -= 1;
4627 }
4628 /* Skip the next sequence of whitspace tokens */
4629 do {
4630 fstart = fmacro.pos;
4631 raw_next_token(state, &fmacro, tk);
4632 } while(tk->tok == TOK_SPACE);
4633 /* Restart at the top of the loop.
4634 * I need to process the non white space token.
4635 */
4636 continue;
4637 break;
4638 case TOK_SPACE:
4639 /* Collapse multiple spaces into one */
4640 if (buf->str[buf->pos - 1] != ' ') {
4641 fstart = space;
4642 flen = 1;
4643 } else {
4644 fstart = 0;
4645 flen = 0;
4646 }
4647 break;
4648 default:
4649 break;
4650 }
4651
Eric Biedermancb364952004-11-15 10:46:44 +00004652 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004653
4654 fstart = fmacro.pos;
4655 raw_next_token(state, &fmacro, tk);
4656 }
4657}
4658
4659static void tag_macro_name(struct compile_state *state,
4660 struct macro *macro, struct macro_buf *buf,
4661 struct token *tk)
4662{
4663 /* Guard all instances of the macro name in the replacement
4664 * text from further macro expansion.
4665 */
4666 struct file_state fmacro;
4667 const char *fstart;
4668 size_t flen;
Eric Biedermancb364952004-11-15 10:46:44 +00004669
4670 /* Put the old macro expansion buffer in a file */
4671 fmacro.prev = 0;
4672 fmacro.basename = macro->ident->name;
4673 fmacro.dirname = "";
4674 fmacro.buf = buf->str;
4675 fmacro.size = buf->pos;
4676 fmacro.pos = fmacro.buf;
4677 fmacro.line = 1;
4678 fmacro.line_start = fmacro.buf;
Eric Biederman90089602004-05-28 14:11:54 +00004679 fmacro.report_line = 1;
4680 fmacro.report_name = fmacro.basename;
4681 fmacro.report_dir = fmacro.dirname;
Eric Biedermancb364952004-11-15 10:46:44 +00004682 fmacro.macro = 1;
4683 fmacro.trigraphs = 0;
4684 fmacro.join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004685
Eric Biedermancb364952004-11-15 10:46:44 +00004686 /* Allocate a new macro expansion buffer */
Eric Biederman90089602004-05-28 14:11:54 +00004687 buf->len = macro->buf_len + 3;
4688 buf->str = xmalloc(buf->len, macro->ident->name);
4689 buf->pos = 0;
4690
4691 fstart = fmacro.pos;
4692 raw_next_token(state, &fmacro, tk);
4693 while(tk->tok != TOK_EOF) {
4694 flen = fmacro.pos - fstart;
4695 if ((tk->tok == TOK_IDENT) &&
4696 (tk->ident == macro->ident) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004697 (tk->val.notmacro == 0))
4698 {
4699 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004700 fstart = "$";
4701 flen = 1;
4702 }
4703
Eric Biedermancb364952004-11-15 10:46:44 +00004704 append_macro_text(state, macro->ident->name, buf, fstart, flen);
Eric Biederman90089602004-05-28 14:11:54 +00004705
4706 fstart = fmacro.pos;
4707 raw_next_token(state, &fmacro, tk);
4708 }
4709 xfree(fmacro.buf);
4710}
Eric Biedermancb364952004-11-15 10:46:44 +00004711
Eric Biederman90089602004-05-28 14:11:54 +00004712static int compile_macro(struct compile_state *state,
4713 struct file_state **filep, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004714{
4715 struct file_state *file;
4716 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +00004717 struct macro *macro;
4718 struct macro_arg_value *argv;
4719 struct macro_buf buf;
4720
4721#if 0
4722 fprintf(state->errout, "macro: %s\n", tk->ident->name);
4723#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00004724 ident = tk->ident;
Eric Biederman90089602004-05-28 14:11:54 +00004725 macro = ident->sym_define;
4726
4727 /* If this token comes from a macro expansion ignore it */
4728 if (tk->val.notmacro) {
4729 return 0;
4730 }
4731 /* If I am a function like macro and the identifier is not followed
4732 * by a left parenthesis, do nothing.
4733 */
Eric Biedermancb364952004-11-15 10:46:44 +00004734 if ((macro->argc >= 0) && (get_char(*filep, (*filep)->pos) != '(')) {
Eric Biederman90089602004-05-28 14:11:54 +00004735 return 0;
4736 }
4737
4738 /* Read in the macro arguments */
4739 argv = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00004740 if (macro->argc >= 0) {
Eric Biederman90089602004-05-28 14:11:54 +00004741 raw_next_token(state, *filep, tk);
4742 check_tok(state, tk, TOK_LPAREN);
4743
4744 argv = read_macro_args(state, macro, *filep, tk);
4745
4746 check_tok(state, tk, TOK_RPAREN);
4747 }
4748 /* Macro expand the macro arguments */
4749 macro_expand_args(state, macro, argv, tk);
4750
4751 buf.str = 0;
4752 buf.len = 0;
4753 buf.pos = 0;
4754 if (ident == state->i___FILE__) {
4755 buf.len = strlen(state->file->basename) + 1 + 2 + 3;
4756 buf.str = xmalloc(buf.len, ident->name);
4757 sprintf(buf.str, "\"%s\"", state->file->basename);
4758 buf.pos = strlen(buf.str);
4759 }
4760 else if (ident == state->i___LINE__) {
4761 buf.len = 30;
4762 buf.str = xmalloc(buf.len, ident->name);
4763 sprintf(buf.str, "%d", state->file->line);
4764 buf.pos = strlen(buf.str);
4765 }
4766 else {
4767 expand_macro(state, macro, &buf, argv, tk);
4768 }
4769 /* Tag the macro name with a $ so it will no longer
4770 * be regonized as a canidate for macro expansion.
4771 */
4772 tag_macro_name(state, macro, &buf, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004773
4774#if 0
4775 fprintf(state->errout, "%s: %d -> `%*.*s'\n",
4776 ident->name, buf.pos, buf.pos, (int)(buf.pos), buf.str);
4777#endif
4778
4779 free_macro_args(macro, argv);
4780
Eric Biedermanb138ac82003-04-22 18:44:01 +00004781 file = xmalloc(sizeof(*file), "file_state");
Eric Biedermancb364952004-11-15 10:46:44 +00004782 file->prev = *filep;
4783 file->basename = xstrdup(ident->name);
4784 file->dirname = xstrdup("");
4785 file->buf = buf.str;
4786 file->size = buf.pos;
4787 file->pos = file->buf;
4788 file->line = 1;
4789 file->line_start = file->pos;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00004790 file->report_line = 1;
4791 file->report_name = file->basename;
4792 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00004793 file->macro = 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004794 file->trigraphs = 0;
4795 file->join_lines = 0;
Eric Biederman90089602004-05-28 14:11:54 +00004796 *filep = file;
4797 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004798}
4799
Eric Biederman90089602004-05-28 14:11:54 +00004800static void eat_tokens(struct compile_state *state, int targ_tok)
4801{
4802 if (state->eat_depth > 0) {
4803 internal_error(state, 0, "Already eating...");
4804 }
4805 state->eat_depth = state->if_depth;
4806 state->eat_targ = targ_tok;
4807}
4808static int if_eat(struct compile_state *state)
4809{
4810 return state->eat_depth > 0;
4811}
4812static int if_value(struct compile_state *state)
4813{
4814 int index, offset;
4815 index = state->if_depth / CHAR_BIT;
4816 offset = state->if_depth % CHAR_BIT;
4817 return !!(state->if_bytes[index] & (1 << (offset)));
4818}
4819static void set_if_value(struct compile_state *state, int value)
4820{
4821 int index, offset;
4822 index = state->if_depth / CHAR_BIT;
4823 offset = state->if_depth % CHAR_BIT;
4824
4825 state->if_bytes[index] &= ~(1 << offset);
4826 if (value) {
4827 state->if_bytes[index] |= (1 << offset);
4828 }
4829}
4830static void in_if(struct compile_state *state, const char *name)
4831{
4832 if (state->if_depth <= 0) {
4833 error(state, 0, "%s without #if", name);
4834 }
4835}
4836static void enter_if(struct compile_state *state)
4837{
4838 state->if_depth += 1;
Eric Biedermancb364952004-11-15 10:46:44 +00004839 if (state->if_depth > MAX_PP_IF_DEPTH) {
Eric Biederman90089602004-05-28 14:11:54 +00004840 error(state, 0, "#if depth too great");
4841 }
4842}
4843static void reenter_if(struct compile_state *state, const char *name)
4844{
4845 in_if(state, name);
4846 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004847 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004848 state->eat_depth = 0;
4849 state->eat_targ = 0;
4850 }
4851}
4852static void enter_else(struct compile_state *state, const char *name)
4853{
4854 in_if(state, name);
4855 if ((state->eat_depth == state->if_depth) &&
Eric Biederman41203d92004-11-08 09:31:09 +00004856 (state->eat_targ == TOK_MELSE)) {
Eric Biederman90089602004-05-28 14:11:54 +00004857 state->eat_depth = 0;
4858 state->eat_targ = 0;
4859 }
4860}
4861static void exit_if(struct compile_state *state, const char *name)
4862{
4863 in_if(state, name);
4864 if (state->eat_depth == state->if_depth) {
4865 state->eat_depth = 0;
4866 state->eat_targ = 0;
4867 }
4868 state->if_depth -= 1;
4869}
4870
Eric Biedermancb364952004-11-15 10:46:44 +00004871static void raw_token(struct compile_state *state, struct token *tk)
Eric Biedermanb138ac82003-04-22 18:44:01 +00004872{
4873 struct file_state *file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00004874 int rescan;
4875
Eric Biedermancb364952004-11-15 10:46:44 +00004876 file = state->file;
4877 raw_next_token(state, file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004878 do {
4879 rescan = 0;
4880 file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00004881 /* Exit out of an include directive or macro call */
4882 if ((tk->tok == TOK_EOF) &&
Eric Biedermancb364952004-11-15 10:46:44 +00004883 (file != state->macro_file) && file->prev)
Eric Biederman41203d92004-11-08 09:31:09 +00004884 {
Eric Biedermanb138ac82003-04-22 18:44:01 +00004885 state->file = file->prev;
4886 /* file->basename is used keep it */
4887 xfree(file->dirname);
4888 xfree(file->buf);
4889 xfree(file);
Eric Biedermancb364952004-11-15 10:46:44 +00004890 file = 0;
4891 raw_next_token(state, state->file, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004892 rescan = 1;
4893 }
Eric Biederman41203d92004-11-08 09:31:09 +00004894 } while(rescan);
4895}
4896
Eric Biedermancb364952004-11-15 10:46:44 +00004897static void pp_token(struct compile_state *state, struct token *tk)
4898{
4899 struct file_state *file;
4900 int rescan;
4901
4902 raw_token(state, tk);
4903 do {
4904 rescan = 0;
4905 file = state->file;
4906 if (tk->tok == TOK_SPACE) {
4907 raw_token(state, tk);
4908 rescan = 1;
4909 }
4910 else if (tk->tok == TOK_IDENT) {
4911 if (state->token_base == 0) {
4912 ident_to_keyword(state, tk);
4913 } else {
4914 ident_to_macro(state, tk);
4915 }
4916 }
4917 } while(rescan);
4918}
4919
Eric Biederman41203d92004-11-08 09:31:09 +00004920static void preprocess(struct compile_state *state, struct token *tk);
4921
4922static void token(struct compile_state *state, struct token *tk)
4923{
4924 int rescan;
Eric Biedermancb364952004-11-15 10:46:44 +00004925 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004926 do {
4927 rescan = 0;
4928 /* Process a macro directive */
4929 if (tk->tok == TOK_MACRO) {
Eric Biedermancb364952004-11-15 10:46:44 +00004930 /* Only match preprocessor directives at the start of a line */
4931 const char *ptr;
4932 ptr = state->file->line_start;
4933 while((ptr < tk->pos)
4934 && spacep(get_char(state->file, ptr)))
4935 {
4936 ptr = next_char(state->file, ptr, 1);
4937 }
4938 if (ptr == tk->pos) {
4939 preprocess(state, tk);
4940 rescan = 1;
4941 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004942 }
Eric Biederman41203d92004-11-08 09:31:09 +00004943 /* Expand a macro call */
Eric Biedermanb138ac82003-04-22 18:44:01 +00004944 else if (tk->ident && tk->ident->sym_define) {
Eric Biederman90089602004-05-28 14:11:54 +00004945 rescan = compile_macro(state, &state->file, tk);
4946 if (rescan) {
Eric Biedermancb364952004-11-15 10:46:44 +00004947 pp_token(state, tk);
Eric Biederman90089602004-05-28 14:11:54 +00004948 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004949 }
Eric Biedermancb364952004-11-15 10:46:44 +00004950 /* Eat tokens disabled by the preprocessor
4951 * (Unless we are parsing a preprocessor directive
4952 */
Eric Biedermana649a412004-11-09 00:35:39 +00004953 else if (if_eat(state) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004954 pp_token(state, tk);
Eric Biederman41203d92004-11-08 09:31:09 +00004955 rescan = 1;
4956 }
Eric Biedermana649a412004-11-09 00:35:39 +00004957 /* Make certain EOL only shows up in preprocessor directives */
Eric Biederman41203d92004-11-08 09:31:09 +00004958 else if ((tk->tok == TOK_EOL) && (state->token_base == 0)) {
Eric Biedermancb364952004-11-15 10:46:44 +00004959 pp_token(state, tk);
Eric Biedermanb138ac82003-04-22 18:44:01 +00004960 rescan = 1;
4961 }
Eric Biedermancb364952004-11-15 10:46:44 +00004962 /* Error on unknown tokens */
4963 else if (tk->tok == TOK_UNKNOWN) {
4964 error(state, 0, "unknown token");
4965 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00004966 } while(rescan);
4967}
4968
Eric Biederman41203d92004-11-08 09:31:09 +00004969
4970static inline struct token *get_token(struct compile_state *state, int offset)
4971{
4972 int index;
4973 index = state->token_base + offset;
4974 if (index >= sizeof(state->token)/sizeof(state->token[0])) {
4975 internal_error(state, 0, "token array to small");
4976 }
4977 return &state->token[index];
4978}
4979
4980static struct token *do_eat_token(struct compile_state *state, int tok)
4981{
4982 struct token *tk;
4983 int i;
4984 check_tok(state, get_token(state, 1), tok);
4985
4986 /* Free the old token value */
4987 tk = get_token(state, 0);
4988 if (tk->str_len) {
4989 memset((void *)tk->val.str, -1, tk->str_len);
4990 xfree(tk->val.str);
4991 }
4992 /* Overwrite the old token with newer tokens */
4993 for(i = state->token_base; i < sizeof(state->token)/sizeof(state->token[0]) - 1; i++) {
4994 state->token[i] = state->token[i + 1];
4995 }
4996 /* Clear the last token */
4997 memset(&state->token[i], 0, sizeof(state->token[i]));
4998 state->token[i].tok = -1;
4999
5000 /* Return the token */
5001 return tk;
5002}
5003
Eric Biedermancb364952004-11-15 10:46:44 +00005004static int raw_peek(struct compile_state *state)
Eric Biederman41203d92004-11-08 09:31:09 +00005005{
5006 struct token *tk1;
5007 tk1 = get_token(state, 1);
5008 if (tk1->tok == -1) {
Eric Biedermancb364952004-11-15 10:46:44 +00005009 raw_token(state, tk1);
Eric Biederman41203d92004-11-08 09:31:09 +00005010 }
5011 return tk1->tok;
5012}
5013
Eric Biedermancb364952004-11-15 10:46:44 +00005014static struct token *raw_eat(struct compile_state *state, int tok)
Eric Biederman41203d92004-11-08 09:31:09 +00005015{
Eric Biedermancb364952004-11-15 10:46:44 +00005016 raw_peek(state);
5017 return do_eat_token(state, tok);
5018}
5019
5020static int pp_peek(struct compile_state *state)
5021{
5022 struct token *tk1;
5023 tk1 = get_token(state, 1);
5024 if (tk1->tok == -1) {
5025 pp_token(state, tk1);
5026 }
5027 return tk1->tok;
5028}
5029
5030static struct token *pp_eat(struct compile_state *state, int tok)
5031{
5032 pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005033 return do_eat_token(state, tok);
5034}
5035
Eric Biedermanb138ac82003-04-22 18:44:01 +00005036static int peek(struct compile_state *state)
5037{
Eric Biederman41203d92004-11-08 09:31:09 +00005038 struct token *tk1;
5039 tk1 = get_token(state, 1);
5040 if (tk1->tok == -1) {
5041 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005042 }
Eric Biederman41203d92004-11-08 09:31:09 +00005043 return tk1->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005044}
5045
5046static int peek2(struct compile_state *state)
5047{
Eric Biederman41203d92004-11-08 09:31:09 +00005048 struct token *tk1, *tk2;
5049 tk1 = get_token(state, 1);
5050 tk2 = get_token(state, 2);
5051 if (tk1->tok == -1) {
5052 token(state, tk1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005053 }
Eric Biederman41203d92004-11-08 09:31:09 +00005054 if (tk2->tok == -1) {
5055 token(state, tk2);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005056 }
Eric Biederman41203d92004-11-08 09:31:09 +00005057 return tk2->tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005058}
5059
Eric Biederman41203d92004-11-08 09:31:09 +00005060static struct token *eat(struct compile_state *state, int tok)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005061{
Eric Biederman90089602004-05-28 14:11:54 +00005062 peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005063 return do_eat_token(state, tok);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005064}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005065
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005066static void compile_file(struct compile_state *state, const char *filename, int local)
Eric Biedermanb138ac82003-04-22 18:44:01 +00005067{
Eric Biederman90089602004-05-28 14:11:54 +00005068 char cwd[MAX_CWD_SIZE];
Eric Biederman6aa31cc2003-06-10 21:22:07 +00005069 const char *subdir, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005070 int subdir_len;
5071 struct file_state *file;
5072 char *basename;
5073 file = xmalloc(sizeof(*file), "file_state");
5074
5075 base = strrchr(filename, '/');
5076 subdir = filename;
5077 if (base != 0) {
5078 subdir_len = base - filename;
5079 base++;
5080 }
5081 else {
5082 base = filename;
5083 subdir_len = 0;
5084 }
5085 basename = xmalloc(strlen(base) +1, "basename");
5086 strcpy(basename, base);
5087 file->basename = basename;
5088
5089 if (getcwd(cwd, sizeof(cwd)) == 0) {
5090 die("cwd buffer to small");
5091 }
Patrick Georgi26774f22009-11-21 19:54:02 +00005092 if ((subdir[0] == '/') || ((subdir[1] == ':') && ((subdir[2] == '/') || (subdir[2] == '\\')))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005093 file->dirname = xmalloc(subdir_len + 1, "dirname");
5094 memcpy(file->dirname, subdir, subdir_len);
5095 file->dirname[subdir_len] = '\0';
5096 }
5097 else {
Eric Biederman90089602004-05-28 14:11:54 +00005098 const char *dir;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005099 int dirlen;
Eric Biederman90089602004-05-28 14:11:54 +00005100 const char **path;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005101 /* Find the appropriate directory... */
5102 dir = 0;
5103 if (!state->file && exists(cwd, filename)) {
5104 dir = cwd;
5105 }
5106 if (local && state->file && exists(state->file->dirname, filename)) {
5107 dir = state->file->dirname;
5108 }
Eric Biederman90089602004-05-28 14:11:54 +00005109 for(path = state->compiler->include_paths; !dir && *path; path++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005110 if (exists(*path, filename)) {
5111 dir = *path;
5112 }
5113 }
5114 if (!dir) {
Eric Biedermancb364952004-11-15 10:46:44 +00005115 error(state, 0, "Cannot open `%s'\n", filename);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005116 }
5117 dirlen = strlen(dir);
5118 file->dirname = xmalloc(dirlen + 1 + subdir_len + 1, "dirname");
5119 memcpy(file->dirname, dir, dirlen);
5120 file->dirname[dirlen] = '/';
5121 memcpy(file->dirname + dirlen + 1, subdir, subdir_len);
5122 file->dirname[dirlen + 1 + subdir_len] = '\0';
5123 }
5124 file->buf = slurp_file(file->dirname, file->basename, &file->size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005125
5126 file->pos = file->buf;
5127 file->line_start = file->pos;
5128 file->line = 1;
5129
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005130 file->report_line = 1;
5131 file->report_name = file->basename;
5132 file->report_dir = file->dirname;
Eric Biederman132368b2004-11-09 08:59:23 +00005133 file->macro = 0;
Eric Biedermancb364952004-11-15 10:46:44 +00005134 file->trigraphs = (state->compiler->flags & COMPILER_TRIGRAPHS)? 1: 0;
5135 file->join_lines = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +00005136
Eric Biedermanb138ac82003-04-22 18:44:01 +00005137 file->prev = state->file;
5138 state->file = file;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005139}
5140
Eric Biederman41203d92004-11-08 09:31:09 +00005141static struct triple *constant_expr(struct compile_state *state);
5142static void integral(struct compile_state *state, struct triple *def);
5143
5144static int mcexpr(struct compile_state *state)
5145{
5146 struct triple *cvalue;
5147 cvalue = constant_expr(state);
5148 integral(state, cvalue);
5149 if (cvalue->op != OP_INTCONST) {
5150 error(state, 0, "integer constant expected");
5151 }
5152 return cvalue->u.cval != 0;
5153}
5154
5155static void preprocess(struct compile_state *state, struct token *current_token)
5156{
5157 /* Doing much more with the preprocessor would require
5158 * a parser and a major restructuring.
5159 * Postpone that for later.
5160 */
Eric Biederman41203d92004-11-08 09:31:09 +00005161 int old_token_base;
Eric Biederman41203d92004-11-08 09:31:09 +00005162 int tok;
5163
Eric Biedermancb364952004-11-15 10:46:44 +00005164 state->macro_file = state->file;
Eric Biederman41203d92004-11-08 09:31:09 +00005165
5166 old_token_base = state->token_base;
5167 state->token_base = current_token - state->token;
5168
Eric Biedermancb364952004-11-15 10:46:44 +00005169 tok = pp_peek(state);
5170 switch(tok) {
Eric Biederman41203d92004-11-08 09:31:09 +00005171 case TOK_LIT_INT:
5172 {
5173 struct token *tk;
5174 int override_line;
Eric Biedermancb364952004-11-15 10:46:44 +00005175 tk = pp_eat(state, TOK_LIT_INT);
Eric Biederman41203d92004-11-08 09:31:09 +00005176 override_line = strtoul(tk->val.str, 0, 10);
Eric Biedermancb364952004-11-15 10:46:44 +00005177 /* I have a preprocessor line marker parse it */
5178 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005179 const char *token, *base;
5180 char *name, *dir;
5181 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005182 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005183 name = xmalloc(tk->str_len, "report_name");
5184 token = tk->val.str + 1;
5185 base = strrchr(token, '/');
5186 name_len = tk->str_len -2;
5187 if (base != 0) {
5188 dir_len = base - token;
5189 base++;
5190 name_len -= base - token;
5191 } else {
5192 dir_len = 0;
5193 base = token;
5194 }
5195 memcpy(name, base, name_len);
5196 name[name_len] = '\0';
5197 dir = xmalloc(dir_len + 1, "report_dir");
5198 memcpy(dir, token, dir_len);
5199 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005200 state->file->report_line = override_line - 1;
5201 state->file->report_name = name;
5202 state->file->report_dir = dir;
5203 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005204 }
5205 break;
5206 }
5207 case TOK_MLINE:
5208 {
5209 struct token *tk;
Eric Biedermancb364952004-11-15 10:46:44 +00005210 pp_eat(state, TOK_MLINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005211 tk = eat(state, TOK_LIT_INT);
Eric Biedermancb364952004-11-15 10:46:44 +00005212 state->file->report_line = strtoul(tk->val.str, 0, 10) -1;
5213 if (pp_peek(state) == TOK_LIT_STRING) {
Eric Biederman41203d92004-11-08 09:31:09 +00005214 const char *token, *base;
5215 char *name, *dir;
5216 int name_len, dir_len;
Eric Biedermancb364952004-11-15 10:46:44 +00005217 tk = pp_eat(state, TOK_LIT_STRING);
Eric Biederman41203d92004-11-08 09:31:09 +00005218 name = xmalloc(tk->str_len, "report_name");
5219 token = tk->val.str + 1;
5220 base = strrchr(token, '/');
5221 name_len = tk->str_len - 2;
5222 if (base != 0) {
5223 dir_len = base - token;
5224 base++;
5225 name_len -= base - token;
5226 } else {
5227 dir_len = 0;
5228 base = token;
5229 }
5230 memcpy(name, base, name_len);
5231 name[name_len] = '\0';
5232 dir = xmalloc(dir_len + 1, "report_dir");
5233 memcpy(dir, token, dir_len);
5234 dir[dir_len] = '\0';
Eric Biedermancb364952004-11-15 10:46:44 +00005235 state->file->report_name = name;
5236 state->file->report_dir = dir;
5237 state->file->macro = 0;
Eric Biederman41203d92004-11-08 09:31:09 +00005238 }
5239 break;
5240 }
5241 case TOK_MUNDEF:
5242 {
5243 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005244 pp_eat(state, TOK_MUNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005245 if (if_eat(state)) /* quit early when #if'd out */
5246 break;
5247
Eric Biedermancb364952004-11-15 10:46:44 +00005248 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005249
5250 undef_macro(state, ident);
5251 break;
5252 }
5253 case TOK_MPRAGMA:
Eric Biedermancb364952004-11-15 10:46:44 +00005254 pp_eat(state, TOK_MPRAGMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005255 if (if_eat(state)) /* quit early when #if'd out */
5256 break;
5257 warning(state, 0, "Ignoring pragma");
5258 break;
5259 case TOK_MELIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005260 pp_eat(state, TOK_MELIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005261 reenter_if(state, "#elif");
5262 if (if_eat(state)) /* quit early when #if'd out */
5263 break;
5264 /* If the #if was taken the #elif just disables the following code */
5265 if (if_value(state)) {
5266 eat_tokens(state, TOK_MENDIF);
5267 }
5268 /* If the previous #if was not taken see if the #elif enables the
5269 * trailing code.
5270 */
5271 else {
5272 set_if_value(state, mcexpr(state));
5273 if (!if_value(state)) {
5274 eat_tokens(state, TOK_MELSE);
5275 }
5276 }
5277 break;
5278 case TOK_MIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005279 pp_eat(state, TOK_MIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005280 enter_if(state);
5281 if (if_eat(state)) /* quit early when #if'd out */
5282 break;
5283 set_if_value(state, mcexpr(state));
5284 if (!if_value(state)) {
5285 eat_tokens(state, TOK_MELSE);
5286 }
5287 break;
5288 case TOK_MIFNDEF:
5289 {
5290 struct hash_entry *ident;
5291
Eric Biedermancb364952004-11-15 10:46:44 +00005292 pp_eat(state, TOK_MIFNDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005293 enter_if(state);
5294 if (if_eat(state)) /* quit early when #if'd out */
5295 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005296 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005297 set_if_value(state, ident->sym_define == 0);
5298 if (!if_value(state)) {
5299 eat_tokens(state, TOK_MELSE);
5300 }
5301 break;
5302 }
5303 case TOK_MIFDEF:
5304 {
5305 struct hash_entry *ident;
Eric Biedermancb364952004-11-15 10:46:44 +00005306 pp_eat(state, TOK_MIFDEF);
Eric Biederman41203d92004-11-08 09:31:09 +00005307 enter_if(state);
5308 if (if_eat(state)) /* quit early when #if'd out */
5309 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005310 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005311 set_if_value(state, ident->sym_define != 0);
5312 if (!if_value(state)) {
5313 eat_tokens(state, TOK_MELSE);
5314 }
5315 break;
5316 }
5317 case TOK_MELSE:
Eric Biedermancb364952004-11-15 10:46:44 +00005318 pp_eat(state, TOK_MELSE);
Eric Biederman41203d92004-11-08 09:31:09 +00005319 enter_else(state, "#else");
5320 if (!if_eat(state) && if_value(state)) {
5321 eat_tokens(state, TOK_MENDIF);
5322 }
5323 break;
5324 case TOK_MENDIF:
Eric Biedermancb364952004-11-15 10:46:44 +00005325 pp_eat(state, TOK_MENDIF);
Eric Biederman41203d92004-11-08 09:31:09 +00005326 exit_if(state, "#endif");
5327 break;
5328 case TOK_MDEFINE:
5329 {
5330 struct hash_entry *ident;
5331 struct macro_arg *args, **larg;
Eric Biedermancb364952004-11-15 10:46:44 +00005332 const char *mstart, *mend;
5333 int argc;
Eric Biederman41203d92004-11-08 09:31:09 +00005334
Eric Biedermancb364952004-11-15 10:46:44 +00005335 pp_eat(state, TOK_MDEFINE);
Eric Biederman41203d92004-11-08 09:31:09 +00005336 if (if_eat(state)) /* quit early when #if'd out */
5337 break;
Eric Biedermancb364952004-11-15 10:46:44 +00005338 ident = pp_eat(state, TOK_MIDENT)->ident;
5339 argc = -1;
Eric Biederman41203d92004-11-08 09:31:09 +00005340 args = 0;
5341 larg = &args;
5342
Eric Biederman41203d92004-11-08 09:31:09 +00005343 /* Parse macro parameters */
Eric Biedermancb364952004-11-15 10:46:44 +00005344 if (raw_peek(state) == TOK_LPAREN) {
5345 raw_eat(state, TOK_LPAREN);
5346 argc += 1;
5347
Eric Biederman41203d92004-11-08 09:31:09 +00005348 for(;;) {
5349 struct macro_arg *narg, *arg;
5350 struct hash_entry *aident;
5351 int tok;
5352
Eric Biedermancb364952004-11-15 10:46:44 +00005353 tok = pp_peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005354 if (!args && (tok == TOK_RPAREN)) {
5355 break;
5356 }
5357 else if (tok == TOK_DOTS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005358 pp_eat(state, TOK_DOTS);
Eric Biederman41203d92004-11-08 09:31:09 +00005359 aident = state->i___VA_ARGS__;
5360 }
5361 else {
Eric Biedermancb364952004-11-15 10:46:44 +00005362 aident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +00005363 }
5364
5365 narg = xcmalloc(sizeof(*arg), "macro arg");
5366 narg->ident = aident;
5367
5368 /* Verify I don't have a duplicate identifier */
5369 for(arg = args; arg; arg = arg->next) {
5370 if (arg->ident == narg->ident) {
5371 error(state, 0, "Duplicate macro arg `%s'",
5372 narg->ident->name);
5373 }
5374 }
5375 /* Add the new argument to the end of the list */
5376 *larg = narg;
5377 larg = &narg->next;
Eric Biedermancb364952004-11-15 10:46:44 +00005378 argc += 1;
Eric Biederman41203d92004-11-08 09:31:09 +00005379
5380 if ((aident == state->i___VA_ARGS__) ||
Eric Biedermancb364952004-11-15 10:46:44 +00005381 (pp_peek(state) != TOK_COMMA)) {
Eric Biederman41203d92004-11-08 09:31:09 +00005382 break;
5383 }
Eric Biedermancb364952004-11-15 10:46:44 +00005384 pp_eat(state, TOK_COMMA);
Eric Biederman41203d92004-11-08 09:31:09 +00005385 }
Eric Biedermancb364952004-11-15 10:46:44 +00005386 pp_eat(state, TOK_RPAREN);
5387 }
5388 /* Remove leading whitespace */
5389 while(raw_peek(state) == TOK_SPACE) {
5390 raw_eat(state, TOK_SPACE);
5391 }
Eric Biederman41203d92004-11-08 09:31:09 +00005392
Eric Biedermancb364952004-11-15 10:46:44 +00005393 /* Remember the start of the macro body */
5394 tok = raw_peek(state);
5395 mend = mstart = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005396
Eric Biedermancb364952004-11-15 10:46:44 +00005397 /* Find the end of the macro */
5398 for(tok = raw_peek(state); tok != TOK_EOL; tok = raw_peek(state)) {
5399 raw_eat(state, tok);
5400 /* Remember the end of the last non space token */
5401 raw_peek(state);
5402 if (tok != TOK_SPACE) {
5403 mend = get_token(state, 1)->pos;
Eric Biederman41203d92004-11-08 09:31:09 +00005404 }
5405 }
Eric Biedermancb364952004-11-15 10:46:44 +00005406
5407 /* Now that I have found the body defined the token */
5408 do_define_macro(state, ident,
5409 char_strdup(state->file, mstart, mend, "macro buf"),
5410 argc, args);
Eric Biederman41203d92004-11-08 09:31:09 +00005411 break;
5412 }
5413 case TOK_MERROR:
5414 {
Eric Biedermancb364952004-11-15 10:46:44 +00005415 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005416 int len;
5417
Eric Biedermancb364952004-11-15 10:46:44 +00005418 pp_eat(state, TOK_MERROR);
5419 /* Find the start of the line */
5420 raw_peek(state);
5421 start = get_token(state, 1)->pos;
5422
Eric Biederman41203d92004-11-08 09:31:09 +00005423 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005424 while((tok = raw_peek(state)) != TOK_EOL) {
5425 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005426 }
Eric Biedermancb364952004-11-15 10:46:44 +00005427 end = get_token(state, 1)->pos;
5428 len = end - start;
5429 if (!if_eat(state)) {
5430 error(state, 0, "%*.*s", len, len, start);
5431 }
Eric Biederman41203d92004-11-08 09:31:09 +00005432 break;
5433 }
5434 case TOK_MWARNING:
5435 {
Eric Biedermancb364952004-11-15 10:46:44 +00005436 const char *start, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00005437 int len;
5438
Eric Biedermancb364952004-11-15 10:46:44 +00005439 pp_eat(state, TOK_MWARNING);
5440
5441 /* Find the start of the line */
5442 raw_peek(state);
5443 start = get_token(state, 1)->pos;
5444
Eric Biederman41203d92004-11-08 09:31:09 +00005445 /* Find the end of the line */
Eric Biedermancb364952004-11-15 10:46:44 +00005446 while((tok = raw_peek(state)) != TOK_EOL) {
5447 raw_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005448 }
Eric Biedermancb364952004-11-15 10:46:44 +00005449 end = get_token(state, 1)->pos;
5450 len = end - start;
5451 if (!if_eat(state)) {
5452 warning(state, 0, "%*.*s", len, len, start);
5453 }
Eric Biederman41203d92004-11-08 09:31:09 +00005454 break;
5455 }
5456 case TOK_MINCLUDE:
5457 {
5458 char *name;
5459 int local;
5460 local = 0;
5461 name = 0;
5462
Eric Biedermancb364952004-11-15 10:46:44 +00005463 pp_eat(state, TOK_MINCLUDE);
Patrick Georgi1bb68282009-12-31 12:56:53 +00005464 if (if_eat(state)) {
5465 /* Find the end of the line */
5466 while((tok = raw_peek(state)) != TOK_EOL) {
5467 raw_eat(state, tok);
5468 }
5469 break;
5470 }
Eric Biederman41203d92004-11-08 09:31:09 +00005471 tok = peek(state);
5472 if (tok == TOK_LIT_STRING) {
5473 struct token *tk;
5474 const char *token;
5475 int name_len;
5476 tk = eat(state, TOK_LIT_STRING);
5477 name = xmalloc(tk->str_len, "include");
5478 token = tk->val.str +1;
5479 name_len = tk->str_len -2;
5480 if (*token == '"') {
5481 token++;
5482 name_len--;
5483 }
5484 memcpy(name, token, name_len);
5485 name[name_len] = '\0';
5486 local = 1;
5487 }
5488 else if (tok == TOK_LESS) {
Eric Biedermancb364952004-11-15 10:46:44 +00005489 struct macro_buf buf;
Eric Biederman41203d92004-11-08 09:31:09 +00005490 eat(state, TOK_LESS);
Eric Biedermancb364952004-11-15 10:46:44 +00005491
5492 buf.len = 40;
5493 buf.str = xmalloc(buf.len, "include");
5494 buf.pos = 0;
5495
5496 tok = peek(state);
5497 while((tok != TOK_MORE) &&
5498 (tok != TOK_EOL) && (tok != TOK_EOF))
5499 {
5500 struct token *tk;
5501 tk = eat(state, tok);
5502 append_macro_chars(state, "include", &buf,
5503 state->file, tk->pos, state->file->pos);
5504 tok = peek(state);
Eric Biederman41203d92004-11-08 09:31:09 +00005505 }
Eric Biedermancb364952004-11-15 10:46:44 +00005506 append_macro_text(state, "include", &buf, "\0", 1);
5507 if (peek(state) != TOK_MORE) {
Eric Biederman41203d92004-11-08 09:31:09 +00005508 error(state, 0, "Unterminated include directive");
5509 }
Eric Biederman41203d92004-11-08 09:31:09 +00005510 eat(state, TOK_MORE);
Eric Biedermancb364952004-11-15 10:46:44 +00005511 local = 0;
5512 name = buf.str;
Eric Biederman41203d92004-11-08 09:31:09 +00005513 }
5514 else {
5515 error(state, 0, "Invalid include directive");
5516 }
5517 /* Error if there are any tokens after the include */
Eric Biedermancb364952004-11-15 10:46:44 +00005518 if (pp_peek(state) != TOK_EOL) {
Eric Biederman41203d92004-11-08 09:31:09 +00005519 error(state, 0, "garbage after include directive");
5520 }
5521 if (!if_eat(state)) {
5522 compile_file(state, name, local);
5523 }
5524 xfree(name);
5525 break;
5526 }
5527 case TOK_EOL:
5528 /* Ignore # without a follwing ident */
5529 break;
5530 default:
5531 {
5532 const char *name1, *name2;
5533 name1 = tokens[tok];
5534 name2 = "";
5535 if (tok == TOK_MIDENT) {
5536 name2 = get_token(state, 1)->ident->name;
5537 }
5538 error(state, 0, "Invalid preprocessor directive: %s %s",
5539 name1, name2);
5540 break;
5541 }
5542 }
5543 /* Consume the rest of the macro line */
5544 do {
Eric Biedermancb364952004-11-15 10:46:44 +00005545 tok = pp_peek(state);
5546 pp_eat(state, tok);
Eric Biederman41203d92004-11-08 09:31:09 +00005547 } while((tok != TOK_EOF) && (tok != TOK_EOL));
5548 state->token_base = old_token_base;
Eric Biedermancb364952004-11-15 10:46:44 +00005549 state->macro_file = NULL;
Eric Biederman41203d92004-11-08 09:31:09 +00005550 return;
5551}
5552
Eric Biederman0babc1c2003-05-09 02:39:00 +00005553/* Type helper functions */
Eric Biedermanb138ac82003-04-22 18:44:01 +00005554
5555static struct type *new_type(
5556 unsigned int type, struct type *left, struct type *right)
5557{
5558 struct type *result;
5559 result = xmalloc(sizeof(*result), "type");
5560 result->type = type;
5561 result->left = left;
5562 result->right = right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005563 result->field_ident = 0;
5564 result->type_ident = 0;
Eric Biederman90089602004-05-28 14:11:54 +00005565 result->elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005566 return result;
5567}
5568
5569static struct type *clone_type(unsigned int specifiers, struct type *old)
5570{
5571 struct type *result;
5572 result = xmalloc(sizeof(*result), "type");
5573 memcpy(result, old, sizeof(*result));
5574 result->type &= TYPE_MASK;
5575 result->type |= specifiers;
5576 return result;
5577}
5578
Eric Biederman90089602004-05-28 14:11:54 +00005579static struct type *dup_type(struct compile_state *state, struct type *orig)
5580{
5581 struct type *new;
5582 new = xcmalloc(sizeof(*new), "type");
5583 new->type = orig->type;
5584 new->field_ident = orig->field_ident;
5585 new->type_ident = orig->type_ident;
5586 new->elements = orig->elements;
5587 if (orig->left) {
5588 new->left = dup_type(state, orig->left);
5589 }
5590 if (orig->right) {
5591 new->right = dup_type(state, orig->right);
5592 }
5593 return new;
5594}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005595
Eric Biederman90089602004-05-28 14:11:54 +00005596
5597static struct type *invalid_type(struct compile_state *state, struct type *type)
5598{
5599 struct type *invalid, *member;
5600 invalid = 0;
5601 if (!type) {
5602 internal_error(state, 0, "type missing?");
5603 }
5604 switch(type->type & TYPE_MASK) {
5605 case TYPE_VOID:
5606 case TYPE_CHAR: case TYPE_UCHAR:
5607 case TYPE_SHORT: case TYPE_USHORT:
5608 case TYPE_INT: case TYPE_UINT:
5609 case TYPE_LONG: case TYPE_ULONG:
5610 case TYPE_LLONG: case TYPE_ULLONG:
5611 case TYPE_POINTER:
5612 case TYPE_ENUM:
5613 break;
5614 case TYPE_BITFIELD:
5615 invalid = invalid_type(state, type->left);
5616 break;
5617 case TYPE_ARRAY:
5618 invalid = invalid_type(state, type->left);
5619 break;
5620 case TYPE_STRUCT:
5621 case TYPE_TUPLE:
5622 member = type->left;
5623 while(member && (invalid == 0) &&
5624 ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
5625 invalid = invalid_type(state, member->left);
5626 member = member->right;
5627 }
5628 if (!invalid) {
5629 invalid = invalid_type(state, member);
5630 }
5631 break;
5632 case TYPE_UNION:
5633 case TYPE_JOIN:
5634 member = type->left;
5635 while(member && (invalid == 0) &&
5636 ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
5637 invalid = invalid_type(state, member->left);
5638 member = member->right;
5639 }
5640 if (!invalid) {
5641 invalid = invalid_type(state, member);
5642 }
5643 break;
5644 default:
5645 invalid = type;
5646 break;
5647 }
5648 return invalid;
5649
5650}
Eric Biedermanb138ac82003-04-22 18:44:01 +00005651
5652#define MASK_UCHAR(X) ((X) & ((ulong_t)0xff))
Eric Biederman90089602004-05-28 14:11:54 +00005653#define MASK_USHORT(X) ((X) & (((ulong_t)1 << (SIZEOF_SHORT)) - 1))
Eric Biedermanb138ac82003-04-22 18:44:01 +00005654static inline ulong_t mask_uint(ulong_t x)
5655{
5656 if (SIZEOF_INT < SIZEOF_LONG) {
Stefan Reinauer50542a82007-10-24 11:14:14 +00005657 ulong_t mask = (1ULL << ((ulong_t)(SIZEOF_INT))) -1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005658 x &= mask;
5659 }
5660 return x;
5661}
5662#define MASK_UINT(X) (mask_uint(X))
5663#define MASK_ULONG(X) (X)
5664
Eric Biederman90089602004-05-28 14:11:54 +00005665static struct type void_type = { .type = TYPE_VOID };
5666static struct type char_type = { .type = TYPE_CHAR };
5667static struct type uchar_type = { .type = TYPE_UCHAR };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005668#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00005669static struct type short_type = { .type = TYPE_SHORT };
Stefan Reinauer50542a82007-10-24 11:14:14 +00005670#endif
Eric Biederman90089602004-05-28 14:11:54 +00005671static struct type ushort_type = { .type = TYPE_USHORT };
5672static struct type int_type = { .type = TYPE_INT };
5673static struct type uint_type = { .type = TYPE_UINT };
5674static struct type long_type = { .type = TYPE_LONG };
5675static struct type ulong_type = { .type = TYPE_ULONG };
5676static struct type unknown_type = { .type = TYPE_UNKNOWN };
Eric Biedermanb138ac82003-04-22 18:44:01 +00005677
Eric Biederman5ade04a2003-10-22 04:03:46 +00005678static struct type void_ptr_type = {
5679 .type = TYPE_POINTER,
5680 .left = &void_type,
5681};
5682
Stefan Reinauer50542a82007-10-24 11:14:14 +00005683#if DEBUG_ROMCC_WARNING
Eric Biederman5ade04a2003-10-22 04:03:46 +00005684static struct type void_func_type = {
Eric Biederman83b991a2003-10-11 06:20:25 +00005685 .type = TYPE_FUNCTION,
5686 .left = &void_type,
5687 .right = &void_type,
5688};
Stefan Reinauer50542a82007-10-24 11:14:14 +00005689#endif
Eric Biederman83b991a2003-10-11 06:20:25 +00005690
Eric Biederman90089602004-05-28 14:11:54 +00005691static size_t bits_to_bytes(size_t size)
5692{
5693 return (size + SIZEOF_CHAR - 1)/SIZEOF_CHAR;
5694}
5695
Eric Biedermanb138ac82003-04-22 18:44:01 +00005696static struct triple *variable(struct compile_state *state, struct type *type)
5697{
5698 struct triple *result;
5699 if ((type->type & STOR_MASK) != STOR_PERM) {
Eric Biederman90089602004-05-28 14:11:54 +00005700 result = triple(state, OP_ADECL, type, 0, 0);
5701 generate_lhs_pieces(state, result);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005702 }
5703 else {
5704 result = triple(state, OP_SDECL, type, 0, 0);
5705 }
5706 return result;
5707}
5708
5709static void stor_of(FILE *fp, struct type *type)
5710{
5711 switch(type->type & STOR_MASK) {
5712 case STOR_AUTO:
5713 fprintf(fp, "auto ");
5714 break;
5715 case STOR_STATIC:
5716 fprintf(fp, "static ");
5717 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005718 case STOR_LOCAL:
5719 fprintf(fp, "local ");
5720 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005721 case STOR_EXTERN:
5722 fprintf(fp, "extern ");
5723 break;
5724 case STOR_REGISTER:
5725 fprintf(fp, "register ");
5726 break;
5727 case STOR_TYPEDEF:
5728 fprintf(fp, "typedef ");
5729 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005730 case STOR_INLINE | STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005731 fprintf(fp, "inline ");
5732 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00005733 case STOR_INLINE | STOR_STATIC:
5734 fprintf(fp, "static inline");
5735 break;
5736 case STOR_INLINE | STOR_EXTERN:
5737 fprintf(fp, "extern inline");
5738 break;
5739 default:
5740 fprintf(fp, "stor:%x", type->type & STOR_MASK);
5741 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005742 }
5743}
5744static void qual_of(FILE *fp, struct type *type)
5745{
5746 if (type->type & QUAL_CONST) {
5747 fprintf(fp, " const");
5748 }
5749 if (type->type & QUAL_VOLATILE) {
5750 fprintf(fp, " volatile");
5751 }
5752 if (type->type & QUAL_RESTRICT) {
5753 fprintf(fp, " restrict");
5754 }
5755}
Eric Biederman0babc1c2003-05-09 02:39:00 +00005756
Eric Biedermanb138ac82003-04-22 18:44:01 +00005757static void name_of(FILE *fp, struct type *type)
5758{
Eric Biederman90089602004-05-28 14:11:54 +00005759 unsigned int base_type;
5760 base_type = type->type & TYPE_MASK;
5761 if ((base_type != TYPE_PRODUCT) && (base_type != TYPE_OVERLAP)) {
5762 stor_of(fp, type);
5763 }
5764 switch(base_type) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00005765 case TYPE_VOID:
5766 fprintf(fp, "void");
5767 qual_of(fp, type);
5768 break;
5769 case TYPE_CHAR:
5770 fprintf(fp, "signed char");
5771 qual_of(fp, type);
5772 break;
5773 case TYPE_UCHAR:
5774 fprintf(fp, "unsigned char");
5775 qual_of(fp, type);
5776 break;
5777 case TYPE_SHORT:
5778 fprintf(fp, "signed short");
5779 qual_of(fp, type);
5780 break;
5781 case TYPE_USHORT:
5782 fprintf(fp, "unsigned short");
5783 qual_of(fp, type);
5784 break;
5785 case TYPE_INT:
5786 fprintf(fp, "signed int");
5787 qual_of(fp, type);
5788 break;
5789 case TYPE_UINT:
5790 fprintf(fp, "unsigned int");
5791 qual_of(fp, type);
5792 break;
5793 case TYPE_LONG:
5794 fprintf(fp, "signed long");
5795 qual_of(fp, type);
5796 break;
5797 case TYPE_ULONG:
5798 fprintf(fp, "unsigned long");
5799 qual_of(fp, type);
5800 break;
5801 case TYPE_POINTER:
5802 name_of(fp, type->left);
5803 fprintf(fp, " * ");
5804 qual_of(fp, type);
5805 break;
5806 case TYPE_PRODUCT:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005807 name_of(fp, type->left);
5808 fprintf(fp, ", ");
5809 name_of(fp, type->right);
5810 break;
Eric Biederman90089602004-05-28 14:11:54 +00005811 case TYPE_OVERLAP:
5812 name_of(fp, type->left);
5813 fprintf(fp, ",| ");
5814 name_of(fp, type->right);
5815 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005816 case TYPE_ENUM:
Eric Biederman90089602004-05-28 14:11:54 +00005817 fprintf(fp, "enum %s",
5818 (type->type_ident)? type->type_ident->name : "");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005819 qual_of(fp, type);
5820 break;
5821 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005822 fprintf(fp, "struct %s { ",
5823 (type->type_ident)? type->type_ident->name : "");
5824 name_of(fp, type->left);
5825 fprintf(fp, " } ");
5826 qual_of(fp, type);
5827 break;
5828 case TYPE_UNION:
5829 fprintf(fp, "union %s { ",
5830 (type->type_ident)? type->type_ident->name : "");
5831 name_of(fp, type->left);
5832 fprintf(fp, " } ");
Eric Biedermanb138ac82003-04-22 18:44:01 +00005833 qual_of(fp, type);
5834 break;
5835 case TYPE_FUNCTION:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005836 name_of(fp, type->left);
5837 fprintf(fp, " (*)(");
5838 name_of(fp, type->right);
5839 fprintf(fp, ")");
5840 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005841 case TYPE_ARRAY:
5842 name_of(fp, type->left);
Eric Biederman83b991a2003-10-11 06:20:25 +00005843 fprintf(fp, " [%ld]", (long)(type->elements));
Eric Biedermanb138ac82003-04-22 18:44:01 +00005844 break;
Eric Biederman90089602004-05-28 14:11:54 +00005845 case TYPE_TUPLE:
5846 fprintf(fp, "tuple { ");
5847 name_of(fp, type->left);
5848 fprintf(fp, " } ");
5849 qual_of(fp, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005850 break;
Eric Biederman90089602004-05-28 14:11:54 +00005851 case TYPE_JOIN:
5852 fprintf(fp, "join { ");
5853 name_of(fp, type->left);
5854 fprintf(fp, " } ");
5855 qual_of(fp, type);
5856 break;
5857 case TYPE_BITFIELD:
5858 name_of(fp, type->left);
5859 fprintf(fp, " : %d ", type->elements);
5860 qual_of(fp, type);
5861 break;
5862 case TYPE_UNKNOWN:
5863 fprintf(fp, "unknown_t");
5864 break;
5865 default:
5866 fprintf(fp, "????: %x", base_type);
5867 break;
5868 }
5869 if (type->field_ident && type->field_ident->name) {
5870 fprintf(fp, " .%s", type->field_ident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +00005871 }
5872}
5873
5874static size_t align_of(struct compile_state *state, struct type *type)
5875{
5876 size_t align;
5877 align = 0;
5878 switch(type->type & TYPE_MASK) {
5879 case TYPE_VOID:
5880 align = 1;
5881 break;
Eric Biederman90089602004-05-28 14:11:54 +00005882 case TYPE_BITFIELD:
5883 align = 1;
5884 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005885 case TYPE_CHAR:
5886 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00005887 align = ALIGNOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005888 break;
5889 case TYPE_SHORT:
5890 case TYPE_USHORT:
5891 align = ALIGNOF_SHORT;
5892 break;
5893 case TYPE_INT:
5894 case TYPE_UINT:
5895 case TYPE_ENUM:
5896 align = ALIGNOF_INT;
5897 break;
5898 case TYPE_LONG:
5899 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00005900 align = ALIGNOF_LONG;
5901 break;
Eric Biederman90089602004-05-28 14:11:54 +00005902 case TYPE_POINTER:
5903 align = ALIGNOF_POINTER;
5904 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005905 case TYPE_PRODUCT:
5906 case TYPE_OVERLAP:
5907 {
5908 size_t left_align, right_align;
5909 left_align = align_of(state, type->left);
5910 right_align = align_of(state, type->right);
5911 align = (left_align >= right_align) ? left_align : right_align;
5912 break;
5913 }
5914 case TYPE_ARRAY:
5915 align = align_of(state, type->left);
5916 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00005917 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00005918 case TYPE_TUPLE:
5919 case TYPE_UNION:
5920 case TYPE_JOIN:
Eric Biederman0babc1c2003-05-09 02:39:00 +00005921 align = align_of(state, type->left);
5922 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00005923 default:
5924 error(state, 0, "alignof not yet defined for type\n");
5925 break;
5926 }
5927 return align;
5928}
5929
Eric Biederman90089602004-05-28 14:11:54 +00005930static size_t reg_align_of(struct compile_state *state, struct type *type)
Eric Biederman03b59862003-06-24 14:27:37 +00005931{
Eric Biederman90089602004-05-28 14:11:54 +00005932 size_t align;
5933 align = 0;
5934 switch(type->type & TYPE_MASK) {
5935 case TYPE_VOID:
5936 align = 1;
5937 break;
5938 case TYPE_BITFIELD:
5939 align = 1;
5940 break;
5941 case TYPE_CHAR:
5942 case TYPE_UCHAR:
5943 align = REG_ALIGNOF_CHAR;
5944 break;
5945 case TYPE_SHORT:
5946 case TYPE_USHORT:
5947 align = REG_ALIGNOF_SHORT;
5948 break;
5949 case TYPE_INT:
5950 case TYPE_UINT:
5951 case TYPE_ENUM:
5952 align = REG_ALIGNOF_INT;
5953 break;
5954 case TYPE_LONG:
5955 case TYPE_ULONG:
5956 align = REG_ALIGNOF_LONG;
5957 break;
5958 case TYPE_POINTER:
5959 align = REG_ALIGNOF_POINTER;
5960 break;
5961 case TYPE_PRODUCT:
5962 case TYPE_OVERLAP:
5963 {
5964 size_t left_align, right_align;
5965 left_align = reg_align_of(state, type->left);
5966 right_align = reg_align_of(state, type->right);
5967 align = (left_align >= right_align) ? left_align : right_align;
5968 break;
5969 }
5970 case TYPE_ARRAY:
5971 align = reg_align_of(state, type->left);
5972 break;
5973 case TYPE_STRUCT:
5974 case TYPE_UNION:
5975 case TYPE_TUPLE:
5976 case TYPE_JOIN:
5977 align = reg_align_of(state, type->left);
5978 break;
5979 default:
5980 error(state, 0, "alignof not yet defined for type\n");
5981 break;
5982 }
5983 return align;
5984}
5985
5986static size_t align_of_in_bytes(struct compile_state *state, struct type *type)
5987{
5988 return bits_to_bytes(align_of(state, type));
5989}
5990static size_t size_of(struct compile_state *state, struct type *type);
5991static size_t reg_size_of(struct compile_state *state, struct type *type);
5992
5993static size_t needed_padding(struct compile_state *state,
5994 struct type *type, size_t offset)
5995{
5996 size_t padding, align;
5997 align = align_of(state, type);
5998 /* Align to the next machine word if the bitfield does completely
5999 * fit into the current word.
6000 */
6001 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
6002 size_t size;
6003 size = size_of(state, type);
6004 if ((offset + type->elements)/size != offset/size) {
6005 align = size;
6006 }
6007 }
Eric Biederman03b59862003-06-24 14:27:37 +00006008 padding = 0;
6009 if (offset % align) {
6010 padding = align - (offset % align);
6011 }
6012 return padding;
6013}
Eric Biederman90089602004-05-28 14:11:54 +00006014
6015static size_t reg_needed_padding(struct compile_state *state,
6016 struct type *type, size_t offset)
6017{
6018 size_t padding, align;
6019 align = reg_align_of(state, type);
6020 /* Align to the next register word if the bitfield does completely
6021 * fit into the current register.
6022 */
6023 if (((type->type & TYPE_MASK) == TYPE_BITFIELD) &&
6024 (((offset + type->elements)/REG_SIZEOF_REG) != (offset/REG_SIZEOF_REG)))
6025 {
6026 align = REG_SIZEOF_REG;
6027 }
6028 padding = 0;
6029 if (offset % align) {
6030 padding = align - (offset % align);
6031 }
6032 return padding;
6033}
6034
Eric Biedermanb138ac82003-04-22 18:44:01 +00006035static size_t size_of(struct compile_state *state, struct type *type)
6036{
6037 size_t size;
6038 size = 0;
6039 switch(type->type & TYPE_MASK) {
6040 case TYPE_VOID:
6041 size = 0;
6042 break;
Eric Biederman90089602004-05-28 14:11:54 +00006043 case TYPE_BITFIELD:
6044 size = type->elements;
6045 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006046 case TYPE_CHAR:
6047 case TYPE_UCHAR:
Eric Biederman90089602004-05-28 14:11:54 +00006048 size = SIZEOF_CHAR;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006049 break;
6050 case TYPE_SHORT:
6051 case TYPE_USHORT:
6052 size = SIZEOF_SHORT;
6053 break;
6054 case TYPE_INT:
6055 case TYPE_UINT:
6056 case TYPE_ENUM:
6057 size = SIZEOF_INT;
6058 break;
6059 case TYPE_LONG:
6060 case TYPE_ULONG:
Eric Biedermanb138ac82003-04-22 18:44:01 +00006061 size = SIZEOF_LONG;
6062 break;
Eric Biederman90089602004-05-28 14:11:54 +00006063 case TYPE_POINTER:
6064 size = SIZEOF_POINTER;
6065 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006066 case TYPE_PRODUCT:
6067 {
Eric Biederman90089602004-05-28 14:11:54 +00006068 size_t pad;
Eric Biederman03b59862003-06-24 14:27:37 +00006069 size = 0;
6070 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006071 pad = needed_padding(state, type->left, size);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006072 size = size + pad + size_of(state, type->left);
Eric Biederman03b59862003-06-24 14:27:37 +00006073 type = type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006074 }
Eric Biederman90089602004-05-28 14:11:54 +00006075 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006076 size = size + pad + size_of(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +00006077 break;
6078 }
6079 case TYPE_OVERLAP:
6080 {
6081 size_t size_left, size_right;
6082 size_left = size_of(state, type->left);
6083 size_right = size_of(state, type->right);
6084 size = (size_left >= size_right)? size_left : size_right;
6085 break;
6086 }
6087 case TYPE_ARRAY:
6088 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6089 internal_error(state, 0, "Invalid array type");
6090 } else {
6091 size = size_of(state, type->left) * type->elements;
6092 }
6093 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006094 case TYPE_STRUCT:
Eric Biederman90089602004-05-28 14:11:54 +00006095 case TYPE_TUPLE:
Eric Biedermane058a1e2003-07-12 01:21:31 +00006096 {
Eric Biederman90089602004-05-28 14:11:54 +00006097 size_t pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006098 size = size_of(state, type->left);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006099 /* Pad structures so their size is a multiples of their alignment */
Eric Biederman90089602004-05-28 14:11:54 +00006100 pad = needed_padding(state, type, size);
6101 size = size + pad;
6102 break;
6103 }
6104 case TYPE_UNION:
6105 case TYPE_JOIN:
6106 {
6107 size_t pad;
6108 size = size_of(state, type->left);
6109 /* Pad unions so their size is a multiple of their alignment */
6110 pad = needed_padding(state, type, size);
Eric Biedermane058a1e2003-07-12 01:21:31 +00006111 size = size + pad;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006112 break;
Eric Biedermane058a1e2003-07-12 01:21:31 +00006113 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006114 default:
Eric Biederman90089602004-05-28 14:11:54 +00006115 internal_error(state, 0, "sizeof not yet defined for type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00006116 break;
6117 }
6118 return size;
6119}
6120
Eric Biederman90089602004-05-28 14:11:54 +00006121static size_t reg_size_of(struct compile_state *state, struct type *type)
6122{
6123 size_t size;
6124 size = 0;
6125 switch(type->type & TYPE_MASK) {
6126 case TYPE_VOID:
6127 size = 0;
6128 break;
6129 case TYPE_BITFIELD:
6130 size = type->elements;
6131 break;
6132 case TYPE_CHAR:
6133 case TYPE_UCHAR:
6134 size = REG_SIZEOF_CHAR;
6135 break;
6136 case TYPE_SHORT:
6137 case TYPE_USHORT:
6138 size = REG_SIZEOF_SHORT;
6139 break;
6140 case TYPE_INT:
6141 case TYPE_UINT:
6142 case TYPE_ENUM:
6143 size = REG_SIZEOF_INT;
6144 break;
6145 case TYPE_LONG:
6146 case TYPE_ULONG:
6147 size = REG_SIZEOF_LONG;
6148 break;
6149 case TYPE_POINTER:
6150 size = REG_SIZEOF_POINTER;
6151 break;
6152 case TYPE_PRODUCT:
6153 {
6154 size_t pad;
6155 size = 0;
6156 while((type->type & TYPE_MASK) == TYPE_PRODUCT) {
6157 pad = reg_needed_padding(state, type->left, size);
6158 size = size + pad + reg_size_of(state, type->left);
6159 type = type->right;
6160 }
6161 pad = reg_needed_padding(state, type, size);
6162 size = size + pad + reg_size_of(state, type);
6163 break;
6164 }
6165 case TYPE_OVERLAP:
6166 {
6167 size_t size_left, size_right;
6168 size_left = reg_size_of(state, type->left);
6169 size_right = reg_size_of(state, type->right);
6170 size = (size_left >= size_right)? size_left : size_right;
6171 break;
6172 }
6173 case TYPE_ARRAY:
6174 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6175 internal_error(state, 0, "Invalid array type");
6176 } else {
6177 size = reg_size_of(state, type->left) * type->elements;
6178 }
6179 break;
6180 case TYPE_STRUCT:
6181 case TYPE_TUPLE:
6182 {
6183 size_t pad;
6184 size = reg_size_of(state, type->left);
6185 /* Pad structures so their size is a multiples of their alignment */
6186 pad = reg_needed_padding(state, type, size);
6187 size = size + pad;
6188 break;
6189 }
6190 case TYPE_UNION:
6191 case TYPE_JOIN:
6192 {
6193 size_t pad;
6194 size = reg_size_of(state, type->left);
6195 /* Pad unions so their size is a multiple of their alignment */
6196 pad = reg_needed_padding(state, type, size);
6197 size = size + pad;
6198 break;
6199 }
6200 default:
6201 internal_error(state, 0, "sizeof not yet defined for type");
6202 break;
6203 }
6204 return size;
6205}
6206
6207static size_t registers_of(struct compile_state *state, struct type *type)
6208{
6209 size_t registers;
6210 registers = reg_size_of(state, type);
6211 registers += REG_SIZEOF_REG - 1;
6212 registers /= REG_SIZEOF_REG;
6213 return registers;
6214}
6215
6216static size_t size_of_in_bytes(struct compile_state *state, struct type *type)
6217{
6218 return bits_to_bytes(size_of(state, type));
6219}
6220
Eric Biederman0babc1c2003-05-09 02:39:00 +00006221static size_t field_offset(struct compile_state *state,
6222 struct type *type, struct hash_entry *field)
6223{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006224 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006225 size_t size;
6226
Eric Biederman0babc1c2003-05-09 02:39:00 +00006227 size = 0;
Eric Biederman90089602004-05-28 14:11:54 +00006228 member = 0;
6229 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6230 member = type->left;
6231 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6232 size += needed_padding(state, member->left, size);
6233 if (member->left->field_ident == field) {
6234 member = member->left;
6235 break;
6236 }
6237 size += size_of(state, member->left);
6238 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006239 }
Eric Biederman90089602004-05-28 14:11:54 +00006240 size += needed_padding(state, member, size);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006241 }
Eric Biederman90089602004-05-28 14:11:54 +00006242 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6243 member = type->left;
6244 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6245 if (member->left->field_ident == field) {
6246 member = member->left;
6247 break;
6248 }
6249 member = member->right;
6250 }
6251 }
6252 else {
6253 internal_error(state, 0, "field_offset only works on structures and unions");
6254 }
6255
6256 if (!member || (member->field_ident != field)) {
6257 error(state, 0, "member %s not present", field->name);
6258 }
6259 return size;
6260}
6261
6262static size_t field_reg_offset(struct compile_state *state,
6263 struct type *type, struct hash_entry *field)
6264{
6265 struct type *member;
6266 size_t size;
6267
6268 size = 0;
6269 member = 0;
6270 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6271 member = type->left;
6272 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6273 size += reg_needed_padding(state, member->left, size);
6274 if (member->left->field_ident == field) {
6275 member = member->left;
6276 break;
6277 }
6278 size += reg_size_of(state, member->left);
6279 member = member->right;
6280 }
6281 }
6282 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6283 member = type->left;
6284 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6285 if (member->left->field_ident == field) {
6286 member = member->left;
6287 break;
6288 }
6289 member = member->right;
6290 }
6291 }
6292 else {
6293 internal_error(state, 0, "field_reg_offset only works on structures and unions");
6294 }
6295
6296 size += reg_needed_padding(state, member, size);
6297 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006298 error(state, 0, "member %s not present", field->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006299 }
6300 return size;
6301}
6302
6303static struct type *field_type(struct compile_state *state,
6304 struct type *type, struct hash_entry *field)
6305{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006306 struct type *member;
Eric Biederman90089602004-05-28 14:11:54 +00006307
6308 member = 0;
6309 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
6310 member = type->left;
6311 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6312 if (member->left->field_ident == field) {
6313 member = member->left;
6314 break;
6315 }
6316 member = member->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006317 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006318 }
Eric Biederman90089602004-05-28 14:11:54 +00006319 else if ((type->type & TYPE_MASK) == TYPE_UNION) {
6320 member = type->left;
6321 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6322 if (member->left->field_ident == field) {
6323 member = member->left;
6324 break;
6325 }
6326 member = member->right;
6327 }
6328 }
6329 else {
6330 internal_error(state, 0, "field_type only works on structures and unions");
6331 }
6332
6333 if (!member || (member->field_ident != field)) {
Eric Biederman03b59862003-06-24 14:27:37 +00006334 error(state, 0, "member %s not present", field->name);
6335 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006336 return member;
Eric Biederman03b59862003-06-24 14:27:37 +00006337}
6338
Eric Biederman90089602004-05-28 14:11:54 +00006339static size_t index_offset(struct compile_state *state,
6340 struct type *type, ulong_t index)
6341{
6342 struct type *member;
6343 size_t size;
6344 size = 0;
6345 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6346 size = size_of(state, type->left) * index;
6347 }
6348 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6349 ulong_t i;
6350 member = type->left;
6351 i = 0;
6352 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6353 size += needed_padding(state, member->left, size);
6354 if (i == index) {
6355 member = member->left;
6356 break;
6357 }
6358 size += size_of(state, member->left);
6359 i++;
6360 member = member->right;
6361 }
6362 size += needed_padding(state, member, size);
6363 if (i != index) {
6364 internal_error(state, 0, "Missing member index: %u", index);
6365 }
6366 }
6367 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6368 ulong_t i;
6369 size = 0;
6370 member = type->left;
6371 i = 0;
6372 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6373 if (i == index) {
6374 member = member->left;
6375 break;
6376 }
6377 i++;
6378 member = member->right;
6379 }
6380 if (i != index) {
6381 internal_error(state, 0, "Missing member index: %u", index);
6382 }
6383 }
6384 else {
6385 internal_error(state, 0,
6386 "request for index %u in something not an array, tuple or join",
6387 index);
6388 }
6389 return size;
6390}
6391
6392static size_t index_reg_offset(struct compile_state *state,
6393 struct type *type, ulong_t index)
6394{
6395 struct type *member;
6396 size_t size;
6397 size = 0;
6398 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6399 size = reg_size_of(state, type->left) * index;
6400 }
6401 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6402 ulong_t i;
6403 member = type->left;
6404 i = 0;
6405 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6406 size += reg_needed_padding(state, member->left, size);
6407 if (i == index) {
6408 member = member->left;
6409 break;
6410 }
6411 size += reg_size_of(state, member->left);
6412 i++;
6413 member = member->right;
6414 }
6415 size += reg_needed_padding(state, member, size);
6416 if (i != index) {
6417 internal_error(state, 0, "Missing member index: %u", index);
6418 }
6419
6420 }
6421 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6422 ulong_t i;
6423 size = 0;
6424 member = type->left;
6425 i = 0;
6426 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6427 if (i == index) {
6428 member = member->left;
6429 break;
6430 }
6431 i++;
6432 member = member->right;
6433 }
6434 if (i != index) {
6435 internal_error(state, 0, "Missing member index: %u", index);
6436 }
6437 }
6438 else {
6439 internal_error(state, 0,
6440 "request for index %u in something not an array, tuple or join",
6441 index);
6442 }
6443 return size;
6444}
6445
6446static struct type *index_type(struct compile_state *state,
6447 struct type *type, ulong_t index)
6448{
6449 struct type *member;
6450 if (index >= type->elements) {
6451 internal_error(state, 0, "Invalid element %u requested", index);
6452 }
6453 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6454 member = type->left;
6455 }
6456 else if ((type->type & TYPE_MASK) == TYPE_TUPLE) {
6457 ulong_t i;
6458 member = type->left;
6459 i = 0;
6460 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6461 if (i == index) {
6462 member = member->left;
6463 break;
6464 }
6465 i++;
6466 member = member->right;
6467 }
6468 if (i != index) {
6469 internal_error(state, 0, "Missing member index: %u", index);
6470 }
6471 }
6472 else if ((type->type & TYPE_MASK) == TYPE_JOIN) {
6473 ulong_t i;
6474 member = type->left;
6475 i = 0;
6476 while(member && ((member->type & TYPE_MASK) == TYPE_OVERLAP)) {
6477 if (i == index) {
6478 member = member->left;
6479 break;
6480 }
6481 i++;
6482 member = member->right;
6483 }
6484 if (i != index) {
6485 internal_error(state, 0, "Missing member index: %u", index);
6486 }
6487 }
6488 else {
6489 member = 0;
6490 internal_error(state, 0,
6491 "request for index %u in something not an array, tuple or join",
6492 index);
6493 }
6494 return member;
6495}
6496
6497static struct type *unpack_type(struct compile_state *state, struct type *type)
6498{
6499 /* If I have a single register compound type not a bit-field
6500 * find the real type.
6501 */
6502 struct type *start_type;
6503 size_t size;
6504 /* Get out early if I need multiple registers for this type */
6505 size = reg_size_of(state, type);
6506 if (size > REG_SIZEOF_REG) {
6507 return type;
6508 }
6509 /* Get out early if I don't need any registers for this type */
6510 if (size == 0) {
6511 return &void_type;
6512 }
6513 /* Loop until I have no more layers I can remove */
6514 do {
6515 start_type = type;
6516 switch(type->type & TYPE_MASK) {
6517 case TYPE_ARRAY:
6518 /* If I have a single element the unpacked type
6519 * is that element.
6520 */
6521 if (type->elements == 1) {
6522 type = type->left;
6523 }
6524 break;
6525 case TYPE_STRUCT:
6526 case TYPE_TUPLE:
6527 /* If I have a single element the unpacked type
6528 * is that element.
6529 */
6530 if (type->elements == 1) {
6531 type = type->left;
6532 }
6533 /* If I have multiple elements the unpacked
6534 * type is the non-void element.
6535 */
6536 else {
6537 struct type *next, *member;
6538 struct type *sub_type;
6539 sub_type = 0;
6540 next = type->left;
6541 while(next) {
6542 member = next;
6543 next = 0;
6544 if ((member->type & TYPE_MASK) == TYPE_PRODUCT) {
6545 next = member->right;
6546 member = member->left;
6547 }
6548 if (reg_size_of(state, member) > 0) {
6549 if (sub_type) {
6550 internal_error(state, 0, "true compound type in a register");
6551 }
6552 sub_type = member;
6553 }
6554 }
6555 if (sub_type) {
6556 type = sub_type;
6557 }
6558 }
6559 break;
6560
6561 case TYPE_UNION:
6562 case TYPE_JOIN:
6563 /* If I have a single element the unpacked type
6564 * is that element.
6565 */
6566 if (type->elements == 1) {
6567 type = type->left;
6568 }
6569 /* I can't in general unpack union types */
6570 break;
6571 default:
6572 /* If I'm not a compound type I can't unpack it */
6573 break;
6574 }
6575 } while(start_type != type);
6576 switch(type->type & TYPE_MASK) {
6577 case TYPE_STRUCT:
6578 case TYPE_ARRAY:
6579 case TYPE_TUPLE:
6580 internal_error(state, 0, "irredicible type?");
6581 break;
6582 }
6583 return type;
6584}
6585
6586static int equiv_types(struct type *left, struct type *right);
6587static int is_compound_type(struct type *type);
6588
6589static struct type *reg_type(
6590 struct compile_state *state, struct type *type, int reg_offset)
6591{
6592 struct type *member;
6593 size_t size;
6594#if 1
6595 struct type *invalid;
6596 invalid = invalid_type(state, type);
6597 if (invalid) {
6598 fprintf(state->errout, "type: ");
6599 name_of(state->errout, type);
6600 fprintf(state->errout, "\n");
6601 fprintf(state->errout, "invalid: ");
6602 name_of(state->errout, invalid);
6603 fprintf(state->errout, "\n");
6604 internal_error(state, 0, "bad input type?");
6605 }
6606#endif
6607
6608 size = reg_size_of(state, type);
6609 if (reg_offset > size) {
6610 member = 0;
6611 fprintf(state->errout, "type: ");
6612 name_of(state->errout, type);
6613 fprintf(state->errout, "\n");
6614 internal_error(state, 0, "offset outside of type");
6615 }
6616 else {
6617 switch(type->type & TYPE_MASK) {
6618 /* Don't do anything with the basic types */
6619 case TYPE_VOID:
6620 case TYPE_CHAR: case TYPE_UCHAR:
6621 case TYPE_SHORT: case TYPE_USHORT:
6622 case TYPE_INT: case TYPE_UINT:
6623 case TYPE_LONG: case TYPE_ULONG:
6624 case TYPE_LLONG: case TYPE_ULLONG:
6625 case TYPE_FLOAT: case TYPE_DOUBLE:
6626 case TYPE_LDOUBLE:
6627 case TYPE_POINTER:
6628 case TYPE_ENUM:
6629 case TYPE_BITFIELD:
6630 member = type;
6631 break;
6632 case TYPE_ARRAY:
6633 member = type->left;
6634 size = reg_size_of(state, member);
6635 if (size > REG_SIZEOF_REG) {
6636 member = reg_type(state, member, reg_offset % size);
6637 }
6638 break;
6639 case TYPE_STRUCT:
6640 case TYPE_TUPLE:
6641 {
6642 size_t offset;
6643 offset = 0;
6644 member = type->left;
6645 while(member && ((member->type & TYPE_MASK) == TYPE_PRODUCT)) {
6646 size = reg_size_of(state, member->left);
6647 offset += reg_needed_padding(state, member->left, offset);
6648 if ((offset + size) > reg_offset) {
6649 member = member->left;
6650 break;
6651 }
6652 offset += size;
6653 member = member->right;
6654 }
6655 offset += reg_needed_padding(state, member, offset);
6656 member = reg_type(state, member, reg_offset - offset);
6657 break;
6658 }
6659 case TYPE_UNION:
6660 case TYPE_JOIN:
6661 {
6662 struct type *join, **jnext, *mnext;
6663 join = new_type(TYPE_JOIN, 0, 0);
6664 jnext = &join->left;
6665 mnext = type->left;
6666 while(mnext) {
6667 size_t size;
6668 member = mnext;
6669 mnext = 0;
6670 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
6671 mnext = member->right;
6672 member = member->left;
6673 }
6674 size = reg_size_of(state, member);
6675 if (size > reg_offset) {
6676 struct type *part, *hunt;
6677 part = reg_type(state, member, reg_offset);
6678 /* See if this type is already in the union */
6679 hunt = join->left;
6680 while(hunt) {
6681 struct type *test = hunt;
6682 hunt = 0;
6683 if ((test->type & TYPE_MASK) == TYPE_OVERLAP) {
6684 hunt = test->right;
6685 test = test->left;
6686 }
6687 if (equiv_types(part, test)) {
6688 goto next;
6689 }
6690 }
6691 /* Nope add it */
6692 if (!*jnext) {
6693 *jnext = part;
6694 } else {
6695 *jnext = new_type(TYPE_OVERLAP, *jnext, part);
6696 jnext = &(*jnext)->right;
6697 }
6698 join->elements++;
6699 }
6700 next:
6701 ;
6702 }
6703 if (join->elements == 0) {
6704 internal_error(state, 0, "No elements?");
6705 }
6706 member = join;
6707 break;
6708 }
6709 default:
6710 member = 0;
6711 fprintf(state->errout, "type: ");
6712 name_of(state->errout, type);
6713 fprintf(state->errout, "\n");
6714 internal_error(state, 0, "reg_type not yet defined for type");
6715
6716 }
6717 }
6718 /* If I have a single register compound type not a bit-field
6719 * find the real type.
6720 */
6721 member = unpack_type(state, member);
6722 ;
6723 size = reg_size_of(state, member);
6724 if (size > REG_SIZEOF_REG) {
6725 internal_error(state, 0, "Cannot find type of single register");
6726 }
6727#if 1
6728 invalid = invalid_type(state, member);
6729 if (invalid) {
6730 fprintf(state->errout, "type: ");
6731 name_of(state->errout, member);
6732 fprintf(state->errout, "\n");
6733 fprintf(state->errout, "invalid: ");
6734 name_of(state->errout, invalid);
6735 fprintf(state->errout, "\n");
6736 internal_error(state, 0, "returning bad type?");
6737 }
6738#endif
6739 return member;
6740}
6741
Eric Biederman03b59862003-06-24 14:27:37 +00006742static struct type *next_field(struct compile_state *state,
6743 struct type *type, struct type *prev_member)
6744{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006745 struct type *member;
Eric Biederman03b59862003-06-24 14:27:37 +00006746 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
6747 internal_error(state, 0, "next_field only works on structures");
6748 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006749 member = type->left;
6750 while((member->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman03b59862003-06-24 14:27:37 +00006751 if (!prev_member) {
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006752 member = member->left;
Eric Biederman03b59862003-06-24 14:27:37 +00006753 break;
6754 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006755 if (member->left == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006756 prev_member = 0;
6757 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006758 member = member->right;
Eric Biederman03b59862003-06-24 14:27:37 +00006759 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006760 if (member == prev_member) {
Eric Biederman03b59862003-06-24 14:27:37 +00006761 prev_member = 0;
6762 }
6763 if (prev_member) {
6764 internal_error(state, 0, "prev_member %s not present",
6765 prev_member->field_ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +00006766 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00006767 return member;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006768}
6769
Eric Biederman90089602004-05-28 14:11:54 +00006770typedef void (*walk_type_fields_cb_t)(struct compile_state *state, struct type *type,
6771 size_t ret_offset, size_t mem_offset, void *arg);
6772
6773static void walk_type_fields(struct compile_state *state,
6774 struct type *type, size_t reg_offset, size_t mem_offset,
6775 walk_type_fields_cb_t cb, void *arg);
6776
6777static void walk_struct_fields(struct compile_state *state,
6778 struct type *type, size_t reg_offset, size_t mem_offset,
6779 walk_type_fields_cb_t cb, void *arg)
Eric Biederman0babc1c2003-05-09 02:39:00 +00006780{
Eric Biederman90089602004-05-28 14:11:54 +00006781 struct type *tptr;
6782 ulong_t i;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006783 if ((type->type & TYPE_MASK) != TYPE_STRUCT) {
Eric Biederman90089602004-05-28 14:11:54 +00006784 internal_error(state, 0, "walk_struct_fields only works on structures");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006785 }
Eric Biederman90089602004-05-28 14:11:54 +00006786 tptr = type->left;
6787 for(i = 0; i < type->elements; i++) {
6788 struct type *mtype;
6789 mtype = tptr;
6790 if ((mtype->type & TYPE_MASK) == TYPE_PRODUCT) {
6791 mtype = mtype->left;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006792 }
Eric Biederman90089602004-05-28 14:11:54 +00006793 walk_type_fields(state, mtype,
6794 reg_offset +
6795 field_reg_offset(state, type, mtype->field_ident),
6796 mem_offset +
6797 field_offset(state, type, mtype->field_ident),
6798 cb, arg);
6799 tptr = tptr->right;
Eric Biederman0babc1c2003-05-09 02:39:00 +00006800 }
Eric Biederman90089602004-05-28 14:11:54 +00006801
6802}
6803
6804static void walk_type_fields(struct compile_state *state,
6805 struct type *type, size_t reg_offset, size_t mem_offset,
6806 walk_type_fields_cb_t cb, void *arg)
6807{
6808 switch(type->type & TYPE_MASK) {
6809 case TYPE_STRUCT:
6810 walk_struct_fields(state, type, reg_offset, mem_offset, cb, arg);
6811 break;
6812 case TYPE_CHAR:
6813 case TYPE_UCHAR:
6814 case TYPE_SHORT:
6815 case TYPE_USHORT:
6816 case TYPE_INT:
6817 case TYPE_UINT:
6818 case TYPE_LONG:
6819 case TYPE_ULONG:
6820 cb(state, type, reg_offset, mem_offset, arg);
6821 break;
6822 case TYPE_VOID:
6823 break;
6824 default:
6825 internal_error(state, 0, "walk_type_fields not yet implemented for type");
Eric Biederman0babc1c2003-05-09 02:39:00 +00006826 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00006827}
6828
Eric Biedermanb138ac82003-04-22 18:44:01 +00006829static void arrays_complete(struct compile_state *state, struct type *type)
6830{
6831 if ((type->type & TYPE_MASK) == TYPE_ARRAY) {
6832 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
6833 error(state, 0, "array size not specified");
6834 }
6835 arrays_complete(state, type->left);
6836 }
6837}
6838
Eric Biederman90089602004-05-28 14:11:54 +00006839static unsigned int get_basic_type(struct type *type)
6840{
6841 unsigned int basic;
6842 basic = type->type & TYPE_MASK;
6843 /* Convert enums to ints */
6844 if (basic == TYPE_ENUM) {
6845 basic = TYPE_INT;
6846 }
6847 /* Convert bitfields to standard types */
6848 else if (basic == TYPE_BITFIELD) {
6849 if (type->elements <= SIZEOF_CHAR) {
6850 basic = TYPE_CHAR;
6851 }
6852 else if (type->elements <= SIZEOF_SHORT) {
6853 basic = TYPE_SHORT;
6854 }
6855 else if (type->elements <= SIZEOF_INT) {
6856 basic = TYPE_INT;
6857 }
6858 else if (type->elements <= SIZEOF_LONG) {
6859 basic = TYPE_LONG;
6860 }
6861 if (!TYPE_SIGNED(type->left->type)) {
6862 basic += 1;
6863 }
6864 }
6865 return basic;
6866}
6867
Eric Biedermanb138ac82003-04-22 18:44:01 +00006868static unsigned int do_integral_promotion(unsigned int type)
6869{
Eric Biederman83b991a2003-10-11 06:20:25 +00006870 if (TYPE_INTEGER(type) && (TYPE_RANK(type) < TYPE_RANK(TYPE_INT))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006871 type = TYPE_INT;
6872 }
6873 return type;
6874}
6875
6876static unsigned int do_arithmetic_conversion(
6877 unsigned int left, unsigned int right)
6878{
Eric Biedermanb138ac82003-04-22 18:44:01 +00006879 if ((left == TYPE_LDOUBLE) || (right == TYPE_LDOUBLE)) {
6880 return TYPE_LDOUBLE;
6881 }
6882 else if ((left == TYPE_DOUBLE) || (right == TYPE_DOUBLE)) {
6883 return TYPE_DOUBLE;
6884 }
6885 else if ((left == TYPE_FLOAT) || (right == TYPE_FLOAT)) {
6886 return TYPE_FLOAT;
6887 }
6888 left = do_integral_promotion(left);
6889 right = do_integral_promotion(right);
6890 /* If both operands have the same size done */
6891 if (left == right) {
6892 return left;
6893 }
6894 /* If both operands have the same signedness pick the larger */
6895 else if (!!TYPE_UNSIGNED(left) == !!TYPE_UNSIGNED(right)) {
6896 return (TYPE_RANK(left) >= TYPE_RANK(right)) ? left : right;
6897 }
6898 /* If the signed type can hold everything use it */
6899 else if (TYPE_SIGNED(left) && (TYPE_RANK(left) > TYPE_RANK(right))) {
6900 return left;
6901 }
6902 else if (TYPE_SIGNED(right) && (TYPE_RANK(right) > TYPE_RANK(left))) {
6903 return right;
6904 }
6905 /* Convert to the unsigned type with the same rank as the signed type */
6906 else if (TYPE_SIGNED(left)) {
6907 return TYPE_MKUNSIGNED(left);
6908 }
6909 else {
6910 return TYPE_MKUNSIGNED(right);
6911 }
6912}
6913
6914/* see if two types are the same except for qualifiers */
6915static int equiv_types(struct type *left, struct type *right)
6916{
6917 unsigned int type;
6918 /* Error if the basic types do not match */
6919 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6920 return 0;
6921 }
6922 type = left->type & TYPE_MASK;
Eric Biederman530b5192003-07-01 10:05:30 +00006923 /* If the basic types match and it is a void type we are done */
6924 if (type == TYPE_VOID) {
6925 return 1;
6926 }
Eric Biederman90089602004-05-28 14:11:54 +00006927 /* For bitfields we need to compare the sizes */
6928 else if (type == TYPE_BITFIELD) {
6929 return (left->elements == right->elements) &&
6930 (TYPE_SIGNED(left->left->type) == TYPE_SIGNED(right->left->type));
6931 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006932 /* if the basic types match and it is an arithmetic type we are done */
Eric Biederman90089602004-05-28 14:11:54 +00006933 else if (TYPE_ARITHMETIC(type)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006934 return 1;
6935 }
6936 /* If it is a pointer type recurse and keep testing */
Eric Biederman90089602004-05-28 14:11:54 +00006937 else if (type == TYPE_POINTER) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00006938 return equiv_types(left->left, right->left);
6939 }
6940 else if (type == TYPE_ARRAY) {
6941 return (left->elements == right->elements) &&
6942 equiv_types(left->left, right->left);
6943 }
Eric Biederman90089602004-05-28 14:11:54 +00006944 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006945 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00006946 return left->type_ident == right->type_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +00006947 }
Eric Biederman90089602004-05-28 14:11:54 +00006948 /* test for union equality */
6949 else if (type == TYPE_UNION) {
6950 return left->type_ident == right->type_ident;
6951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006952 /* Test for equivalent functions */
6953 else if (type == TYPE_FUNCTION) {
6954 return equiv_types(left->left, right->left) &&
6955 equiv_types(left->right, right->right);
6956 }
6957 /* We only see TYPE_PRODUCT as part of function equivalence matching */
Eric Biederman90089602004-05-28 14:11:54 +00006958 /* We also see TYPE_PRODUCT as part of of tuple equivalence matchin */
Eric Biedermanb138ac82003-04-22 18:44:01 +00006959 else if (type == TYPE_PRODUCT) {
6960 return equiv_types(left->left, right->left) &&
6961 equiv_types(left->right, right->right);
6962 }
Eric Biederman90089602004-05-28 14:11:54 +00006963 /* We should see TYPE_OVERLAP when comparing joins */
6964 else if (type == TYPE_OVERLAP) {
6965 return equiv_types(left->left, right->left) &&
6966 equiv_types(left->right, right->right);
6967 }
6968 /* Test for equivalence of tuples */
6969 else if (type == TYPE_TUPLE) {
6970 return (left->elements == right->elements) &&
6971 equiv_types(left->left, right->left);
6972 }
6973 /* Test for equivalence of joins */
6974 else if (type == TYPE_JOIN) {
6975 return (left->elements == right->elements) &&
6976 equiv_types(left->left, right->left);
6977 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00006978 else {
6979 return 0;
6980 }
6981}
6982
6983static int equiv_ptrs(struct type *left, struct type *right)
6984{
6985 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
6986 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
6987 return 0;
6988 }
6989 return equiv_types(left->left, right->left);
6990}
6991
6992static struct type *compatible_types(struct type *left, struct type *right)
6993{
6994 struct type *result;
6995 unsigned int type, qual_type;
6996 /* Error if the basic types do not match */
6997 if ((left->type & TYPE_MASK) != (right->type & TYPE_MASK)) {
6998 return 0;
6999 }
7000 type = left->type & TYPE_MASK;
7001 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7002 result = 0;
7003 /* if the basic types match and it is an arithmetic type we are done */
7004 if (TYPE_ARITHMETIC(type)) {
7005 result = new_type(qual_type, 0, 0);
7006 }
7007 /* If it is a pointer type recurse and keep testing */
7008 else if (type == TYPE_POINTER) {
7009 result = compatible_types(left->left, right->left);
7010 if (result) {
7011 result = new_type(qual_type, result, 0);
7012 }
7013 }
Eric Biederman90089602004-05-28 14:11:54 +00007014 /* test for struct equality */
Eric Biedermanb138ac82003-04-22 18:44:01 +00007015 else if (type == TYPE_STRUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007016 if (left->type_ident == right->type_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007017 result = left;
7018 }
7019 }
Eric Biederman90089602004-05-28 14:11:54 +00007020 /* test for union equality */
7021 else if (type == TYPE_UNION) {
7022 if (left->type_ident == right->type_ident) {
7023 result = left;
7024 }
7025 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007026 /* Test for equivalent functions */
7027 else if (type == TYPE_FUNCTION) {
7028 struct type *lf, *rf;
7029 lf = compatible_types(left->left, right->left);
7030 rf = compatible_types(left->right, right->right);
7031 if (lf && rf) {
7032 result = new_type(qual_type, lf, rf);
7033 }
7034 }
7035 /* We only see TYPE_PRODUCT as part of function equivalence matching */
7036 else if (type == TYPE_PRODUCT) {
7037 struct type *lf, *rf;
7038 lf = compatible_types(left->left, right->left);
7039 rf = compatible_types(left->right, right->right);
7040 if (lf && rf) {
7041 result = new_type(qual_type, lf, rf);
7042 }
7043 }
7044 else {
7045 /* Nothing else is compatible */
7046 }
7047 return result;
7048}
7049
Eric Biederman90089602004-05-28 14:11:54 +00007050/* See if left is a equivalent to right or right is a union member of left */
7051static int is_subset_type(struct type *left, struct type *right)
7052{
7053 if (equiv_types(left, right)) {
7054 return 1;
7055 }
7056 if ((left->type & TYPE_MASK) == TYPE_JOIN) {
7057 struct type *member, *mnext;
7058 mnext = left->left;
7059 while(mnext) {
7060 member = mnext;
7061 mnext = 0;
7062 if ((member->type & TYPE_MASK) == TYPE_OVERLAP) {
7063 mnext = member->right;
7064 member = member->left;
7065 }
7066 if (is_subset_type( member, right)) {
7067 return 1;
7068 }
7069 }
7070 }
7071 return 0;
7072}
7073
Eric Biedermanb138ac82003-04-22 18:44:01 +00007074static struct type *compatible_ptrs(struct type *left, struct type *right)
7075{
7076 struct type *result;
7077 if (((left->type & TYPE_MASK) != TYPE_POINTER) ||
7078 ((right->type & TYPE_MASK) != TYPE_POINTER)) {
7079 return 0;
7080 }
7081 result = compatible_types(left->left, right->left);
7082 if (result) {
7083 unsigned int qual_type;
7084 qual_type = (left->type & ~STOR_MASK) | (right->type & ~STOR_MASK);
7085 result = new_type(qual_type, result, 0);
7086 }
7087 return result;
7088
7089}
7090static struct triple *integral_promotion(
7091 struct compile_state *state, struct triple *def)
7092{
7093 struct type *type;
7094 type = def->type;
7095 /* As all operations are carried out in registers
7096 * the values are converted on load I just convert
7097 * logical type of the operand.
7098 */
7099 if (TYPE_INTEGER(type->type)) {
7100 unsigned int int_type;
7101 int_type = type->type & ~TYPE_MASK;
Eric Biederman90089602004-05-28 14:11:54 +00007102 int_type |= do_integral_promotion(get_basic_type(type));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007103 if (int_type != type->type) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00007104 if (def->op != OP_LOAD) {
7105 def->type = new_type(int_type, 0, 0);
7106 }
7107 else {
Eric Biederman90089602004-05-28 14:11:54 +00007108 def = triple(state, OP_CONVERT,
Eric Biederman5ade04a2003-10-22 04:03:46 +00007109 new_type(int_type, 0, 0), def, 0);
7110 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007111 }
7112 }
7113 return def;
7114}
7115
7116
7117static void arithmetic(struct compile_state *state, struct triple *def)
7118{
7119 if (!TYPE_ARITHMETIC(def->type->type)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007120 error(state, 0, "arithmetic type expexted");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007121 }
7122}
7123
7124static void ptr_arithmetic(struct compile_state *state, struct triple *def)
7125{
7126 if (!TYPE_PTR(def->type->type) && !TYPE_ARITHMETIC(def->type->type)) {
7127 error(state, def, "pointer or arithmetic type expected");
7128 }
7129}
7130
7131static int is_integral(struct triple *ins)
7132{
7133 return TYPE_INTEGER(ins->type->type);
7134}
7135
7136static void integral(struct compile_state *state, struct triple *def)
7137{
7138 if (!is_integral(def)) {
7139 error(state, 0, "integral type expected");
7140 }
7141}
7142
7143
7144static void bool(struct compile_state *state, struct triple *def)
7145{
7146 if (!TYPE_ARITHMETIC(def->type->type) &&
7147 ((def->type->type & TYPE_MASK) != TYPE_POINTER)) {
7148 error(state, 0, "arithmetic or pointer type expected");
7149 }
7150}
7151
7152static int is_signed(struct type *type)
7153{
Eric Biederman90089602004-05-28 14:11:54 +00007154 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
7155 type = type->left;
7156 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007157 return !!TYPE_SIGNED(type->type);
7158}
Eric Biederman90089602004-05-28 14:11:54 +00007159static int is_compound_type(struct type *type)
7160{
7161 int is_compound;
7162 switch((type->type & TYPE_MASK)) {
7163 case TYPE_ARRAY:
7164 case TYPE_STRUCT:
7165 case TYPE_TUPLE:
7166 case TYPE_UNION:
7167 case TYPE_JOIN:
7168 is_compound = 1;
7169 break;
7170 default:
7171 is_compound = 0;
7172 break;
7173 }
7174 return is_compound;
7175}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007176
Eric Biederman0babc1c2003-05-09 02:39:00 +00007177/* Is this value located in a register otherwise it must be in memory */
7178static int is_in_reg(struct compile_state *state, struct triple *def)
7179{
7180 int in_reg;
7181 if (def->op == OP_ADECL) {
7182 in_reg = 1;
7183 }
7184 else if ((def->op == OP_SDECL) || (def->op == OP_DEREF)) {
7185 in_reg = 0;
7186 }
Eric Biederman90089602004-05-28 14:11:54 +00007187 else if (triple_is_part(state, def)) {
7188 in_reg = is_in_reg(state, MISC(def, 0));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007189 }
7190 else {
Eric Biederman90089602004-05-28 14:11:54 +00007191 internal_error(state, def, "unknown expr storage location");
Eric Biederman0babc1c2003-05-09 02:39:00 +00007192 in_reg = -1;
7193 }
7194 return in_reg;
7195}
7196
Eric Biederman90089602004-05-28 14:11:54 +00007197/* Is this an auto or static variable location? Something that can
7198 * be assigned to. Otherwise it must must be a pure value, a temporary.
7199 */
7200static int is_lvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007201{
7202 int ret;
7203 ret = 0;
7204 if (!def) {
7205 return 0;
7206 }
7207 if ((def->op == OP_ADECL) ||
7208 (def->op == OP_SDECL) ||
7209 (def->op == OP_DEREF) ||
Eric Biederman5cd81732004-03-11 15:01:31 +00007210 (def->op == OP_BLOBCONST) ||
7211 (def->op == OP_LIST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007212 ret = 1;
7213 }
Eric Biederman90089602004-05-28 14:11:54 +00007214 else if (triple_is_part(state, def)) {
7215 ret = is_lvalue(state, MISC(def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00007216 }
7217 return ret;
7218}
7219
Eric Biederman00443072003-06-24 12:34:45 +00007220static void clvalue(struct compile_state *state, struct triple *def)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007221{
7222 if (!def) {
7223 internal_error(state, def, "nothing where lvalue expected?");
7224 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007225 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007226 error(state, def, "lvalue expected");
7227 }
7228}
Eric Biederman00443072003-06-24 12:34:45 +00007229static void lvalue(struct compile_state *state, struct triple *def)
7230{
7231 clvalue(state, def);
7232 if (def->type->type & QUAL_CONST) {
7233 error(state, def, "modifable lvalue expected");
7234 }
7235}
Eric Biedermanb138ac82003-04-22 18:44:01 +00007236
7237static int is_pointer(struct triple *def)
7238{
7239 return (def->type->type & TYPE_MASK) == TYPE_POINTER;
7240}
7241
7242static void pointer(struct compile_state *state, struct triple *def)
7243{
7244 if (!is_pointer(def)) {
7245 error(state, def, "pointer expected");
7246 }
7247}
7248
7249static struct triple *int_const(
7250 struct compile_state *state, struct type *type, ulong_t value)
7251{
7252 struct triple *result;
7253 switch(type->type & TYPE_MASK) {
7254 case TYPE_CHAR:
7255 case TYPE_INT: case TYPE_UINT:
7256 case TYPE_LONG: case TYPE_ULONG:
7257 break;
7258 default:
Eric Biederman90089602004-05-28 14:11:54 +00007259 internal_error(state, 0, "constant for unknown type");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007260 }
7261 result = triple(state, OP_INTCONST, type, 0, 0);
7262 result->u.cval = value;
7263 return result;
7264}
7265
7266
Eric Biederman83b991a2003-10-11 06:20:25 +00007267static struct triple *read_expr(struct compile_state *state, struct triple *def);
7268
Eric Biederman0babc1c2003-05-09 02:39:00 +00007269static struct triple *do_mk_addr_expr(struct compile_state *state,
7270 struct triple *expr, struct type *type, ulong_t offset)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007271{
7272 struct triple *result;
Eric Biederman90089602004-05-28 14:11:54 +00007273 struct type *ptr_type;
Eric Biederman00443072003-06-24 12:34:45 +00007274 clvalue(state, expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007275
Eric Biederman90089602004-05-28 14:11:54 +00007276 ptr_type = new_type(TYPE_POINTER | (type->type & QUAL_MASK), type, 0);
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007277
Eric Biederman90089602004-05-28 14:11:54 +00007278
Eric Biedermanb138ac82003-04-22 18:44:01 +00007279 result = 0;
7280 if (expr->op == OP_ADECL) {
7281 error(state, expr, "address of auto variables not supported");
7282 }
7283 else if (expr->op == OP_SDECL) {
Eric Biederman90089602004-05-28 14:11:54 +00007284 result = triple(state, OP_ADDRCONST, ptr_type, 0, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007285 MISC(result, 0) = expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007286 result->u.cval = offset;
7287 }
7288 else if (expr->op == OP_DEREF) {
Eric Biederman90089602004-05-28 14:11:54 +00007289 result = triple(state, OP_ADD, ptr_type,
Eric Biederman0babc1c2003-05-09 02:39:00 +00007290 RHS(expr, 0),
Eric Biedermanb138ac82003-04-22 18:44:01 +00007291 int_const(state, &ulong_type, offset));
7292 }
Eric Biederman90089602004-05-28 14:11:54 +00007293 else if (expr->op == OP_BLOBCONST) {
7294 FINISHME();
7295 internal_error(state, expr, "not yet implemented");
7296 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007297 else if (expr->op == OP_LIST) {
7298 error(state, 0, "Function addresses not supported");
7299 }
Eric Biederman90089602004-05-28 14:11:54 +00007300 else if (triple_is_part(state, expr)) {
7301 struct triple *part;
7302 part = expr;
7303 expr = MISC(expr, 0);
7304 if (part->op == OP_DOT) {
7305 offset += bits_to_bytes(
7306 field_offset(state, expr->type, part->u.field));
7307 }
7308 else if (part->op == OP_INDEX) {
7309 offset += bits_to_bytes(
7310 index_offset(state, expr->type, part->u.cval));
7311 }
7312 else {
7313 internal_error(state, part, "unhandled part type");
7314 }
7315 result = do_mk_addr_expr(state, expr, type, offset);
7316 }
Eric Biederman83b991a2003-10-11 06:20:25 +00007317 if (!result) {
7318 internal_error(state, expr, "cannot take address of expression");
7319 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007320 return result;
7321}
7322
Eric Biederman0babc1c2003-05-09 02:39:00 +00007323static struct triple *mk_addr_expr(
7324 struct compile_state *state, struct triple *expr, ulong_t offset)
7325{
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007326 return do_mk_addr_expr(state, expr, expr->type, offset);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007327}
7328
Eric Biedermanb138ac82003-04-22 18:44:01 +00007329static struct triple *mk_deref_expr(
7330 struct compile_state *state, struct triple *expr)
7331{
7332 struct type *base_type;
7333 pointer(state, expr);
7334 base_type = expr->type->left;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007335 return triple(state, OP_DEREF, base_type, expr, 0);
7336}
7337
Eric Biederman90089602004-05-28 14:11:54 +00007338/* lvalue conversions always apply except when certain operators
7339 * are applied. So I apply apply it when I know no more
7340 * operators will be applied.
7341 */
Eric Biederman5cd81732004-03-11 15:01:31 +00007342static struct triple *lvalue_conversion(struct compile_state *state, struct triple *def)
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007343{
Eric Biederman5cd81732004-03-11 15:01:31 +00007344 /* Tranform an array to a pointer to the first element */
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007345 if ((def->type->type & TYPE_MASK) == TYPE_ARRAY) {
7346 struct type *type;
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007347 type = new_type(
7348 TYPE_POINTER | (def->type->type & QUAL_MASK),
7349 def->type->left, 0);
Eric Biederman66fe2222003-07-04 15:14:04 +00007350 if ((def->op == OP_SDECL) || IS_CONST_OP(def->op)) {
Eric Biederman830c9882003-07-04 00:27:33 +00007351 struct triple *addrconst;
7352 if ((def->op != OP_SDECL) && (def->op != OP_BLOBCONST)) {
7353 internal_error(state, def, "bad array constant");
7354 }
7355 addrconst = triple(state, OP_ADDRCONST, type, 0, 0);
7356 MISC(addrconst, 0) = def;
7357 def = addrconst;
7358 }
7359 else {
Eric Biederman90089602004-05-28 14:11:54 +00007360 def = triple(state, OP_CONVERT, type, def, 0);
Eric Biederman830c9882003-07-04 00:27:33 +00007361 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007362 }
Eric Biederman5cd81732004-03-11 15:01:31 +00007363 /* Transform a function to a pointer to it */
7364 else if ((def->type->type & TYPE_MASK) == TYPE_FUNCTION) {
7365 def = mk_addr_expr(state, def, 0);
7366 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +00007367 return def;
7368}
7369
Eric Biederman0babc1c2003-05-09 02:39:00 +00007370static struct triple *deref_field(
7371 struct compile_state *state, struct triple *expr, struct hash_entry *field)
7372{
7373 struct triple *result;
7374 struct type *type, *member;
Eric Biederman90089602004-05-28 14:11:54 +00007375 ulong_t offset;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007376 if (!field) {
7377 internal_error(state, 0, "No field passed to deref_field");
7378 }
7379 result = 0;
7380 type = expr->type;
Eric Biederman90089602004-05-28 14:11:54 +00007381 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
7382 ((type->type & TYPE_MASK) != TYPE_UNION)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007383 error(state, 0, "request for member %s in something not a struct or union",
7384 field->name);
7385 }
Eric Biederman03b59862003-06-24 14:27:37 +00007386 member = field_type(state, type, field);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007387 if ((type->type & STOR_MASK) == STOR_PERM) {
7388 /* Do the pointer arithmetic to get a deref the field */
Eric Biederman90089602004-05-28 14:11:54 +00007389 offset = bits_to_bytes(field_offset(state, type, field));
Eric Biederman0babc1c2003-05-09 02:39:00 +00007390 result = do_mk_addr_expr(state, expr, member, offset);
7391 result = mk_deref_expr(state, result);
7392 }
7393 else {
7394 /* Find the variable for the field I want. */
Eric Biederman03b59862003-06-24 14:27:37 +00007395 result = triple(state, OP_DOT, member, expr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007396 result->u.field = field;
7397 }
7398 return result;
7399}
7400
Eric Biederman90089602004-05-28 14:11:54 +00007401static struct triple *deref_index(
7402 struct compile_state *state, struct triple *expr, size_t index)
7403{
7404 struct triple *result;
7405 struct type *type, *member;
7406 ulong_t offset;
7407
7408 result = 0;
7409 type = expr->type;
7410 member = index_type(state, type, index);
7411
7412 if ((type->type & STOR_MASK) == STOR_PERM) {
7413 offset = bits_to_bytes(index_offset(state, type, index));
7414 result = do_mk_addr_expr(state, expr, member, offset);
7415 result = mk_deref_expr(state, result);
7416 }
7417 else {
7418 result = triple(state, OP_INDEX, member, expr, 0);
7419 result->u.cval = index;
7420 }
7421 return result;
7422}
7423
Eric Biedermanb138ac82003-04-22 18:44:01 +00007424static struct triple *read_expr(struct compile_state *state, struct triple *def)
7425{
7426 int op;
7427 if (!def) {
7428 return 0;
7429 }
Stefan Reinauer50542a82007-10-24 11:14:14 +00007430#if DEBUG_ROMCC_WARNINGS
Eric Biederman5cd81732004-03-11 15:01:31 +00007431#warning "CHECK_ME is this the only place I need to do lvalue conversions?"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007432#endif
Eric Biederman5cd81732004-03-11 15:01:31 +00007433 /* Transform lvalues into something we can read */
7434 def = lvalue_conversion(state, def);
Eric Biederman90089602004-05-28 14:11:54 +00007435 if (!is_lvalue(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007436 return def;
7437 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007438 if (is_in_reg(state, def)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007439 op = OP_READ;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007440 } else {
Eric Biederman83b991a2003-10-11 06:20:25 +00007441 if (def->op == OP_SDECL) {
7442 def = mk_addr_expr(state, def, 0);
7443 def = mk_deref_expr(state, def);
7444 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007445 op = OP_LOAD;
7446 }
Eric Biederman90089602004-05-28 14:11:54 +00007447 def = triple(state, op, def->type, def, 0);
7448 if (def->type->type & QUAL_VOLATILE) {
7449 def->id |= TRIPLE_FLAG_VOLATILE;
7450 }
7451 return def;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007452}
7453
Eric Biedermane058a1e2003-07-12 01:21:31 +00007454int is_write_compatible(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +00007455 struct type *dest, struct type *rval)
7456{
7457 int compatible = 0;
7458 /* Both operands have arithmetic type */
7459 if (TYPE_ARITHMETIC(dest->type) && TYPE_ARITHMETIC(rval->type)) {
7460 compatible = 1;
7461 }
7462 /* One operand is a pointer and the other is a pointer to void */
7463 else if (((dest->type & TYPE_MASK) == TYPE_POINTER) &&
7464 ((rval->type & TYPE_MASK) == TYPE_POINTER) &&
7465 (((dest->left->type & TYPE_MASK) == TYPE_VOID) ||
7466 ((rval->left->type & TYPE_MASK) == TYPE_VOID))) {
7467 compatible = 1;
7468 }
7469 /* If both types are the same without qualifiers we are good */
7470 else if (equiv_ptrs(dest, rval)) {
7471 compatible = 1;
7472 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007473 /* test for struct/union equality */
Eric Biederman90089602004-05-28 14:11:54 +00007474 else if (equiv_types(dest, rval)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007475 compatible = 1;
7476 }
Eric Biedermane058a1e2003-07-12 01:21:31 +00007477 return compatible;
7478}
7479
Eric Biedermane058a1e2003-07-12 01:21:31 +00007480static void write_compatible(struct compile_state *state,
7481 struct type *dest, struct type *rval)
7482{
7483 if (!is_write_compatible(state, dest, rval)) {
Eric Biederman90089602004-05-28 14:11:54 +00007484 FILE *fp = state->errout;
7485 fprintf(fp, "dest: ");
7486 name_of(fp, dest);
7487 fprintf(fp,"\nrval: ");
7488 name_of(fp, rval);
7489 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +00007490 error(state, 0, "Incompatible types in assignment");
7491 }
7492}
7493
Eric Biedermane058a1e2003-07-12 01:21:31 +00007494static int is_init_compatible(struct compile_state *state,
7495 struct type *dest, struct type *rval)
7496{
7497 int compatible = 0;
7498 if (is_write_compatible(state, dest, rval)) {
7499 compatible = 1;
7500 }
7501 else if (equiv_types(dest, rval)) {
7502 compatible = 1;
7503 }
7504 return compatible;
7505}
7506
Eric Biedermanb138ac82003-04-22 18:44:01 +00007507static struct triple *write_expr(
7508 struct compile_state *state, struct triple *dest, struct triple *rval)
7509{
7510 struct triple *def;
7511 int op;
7512
7513 def = 0;
7514 if (!rval) {
7515 internal_error(state, 0, "missing rval");
7516 }
7517
7518 if (rval->op == OP_LIST) {
7519 internal_error(state, 0, "expression of type OP_LIST?");
7520 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007521 if (!is_lvalue(state, dest)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007522 internal_error(state, 0, "writing to a non lvalue?");
7523 }
Eric Biederman00443072003-06-24 12:34:45 +00007524 if (dest->type->type & QUAL_CONST) {
7525 internal_error(state, 0, "modifable lvalue expexted");
7526 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007527
7528 write_compatible(state, dest->type, rval->type);
Eric Biederman90089602004-05-28 14:11:54 +00007529 if (!equiv_types(dest->type, rval->type)) {
7530 rval = triple(state, OP_CONVERT, dest->type, rval, 0);
7531 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007532
7533 /* Now figure out which assignment operator to use */
7534 op = -1;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007535 if (is_in_reg(state, dest)) {
Eric Biederman90089602004-05-28 14:11:54 +00007536 def = triple(state, OP_WRITE, dest->type, rval, dest);
7537 if (MISC(def, 0) != dest) {
7538 internal_error(state, def, "huh?");
7539 }
7540 if (RHS(def, 0) != rval) {
7541 internal_error(state, def, "huh?");
7542 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007543 } else {
Eric Biederman90089602004-05-28 14:11:54 +00007544 def = triple(state, OP_STORE, dest->type, dest, rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007545 }
Eric Biederman90089602004-05-28 14:11:54 +00007546 if (def->type->type & QUAL_VOLATILE) {
7547 def->id |= TRIPLE_FLAG_VOLATILE;
7548 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007549 return def;
7550}
7551
7552static struct triple *init_expr(
7553 struct compile_state *state, struct triple *dest, struct triple *rval)
7554{
7555 struct triple *def;
7556
7557 def = 0;
7558 if (!rval) {
7559 internal_error(state, 0, "missing rval");
7560 }
7561 if ((dest->type->type & STOR_MASK) != STOR_PERM) {
7562 rval = read_expr(state, rval);
7563 def = write_expr(state, dest, rval);
7564 }
7565 else {
7566 /* Fill in the array size if necessary */
7567 if (((dest->type->type & TYPE_MASK) == TYPE_ARRAY) &&
7568 ((rval->type->type & TYPE_MASK) == TYPE_ARRAY)) {
7569 if (dest->type->elements == ELEMENT_COUNT_UNSPECIFIED) {
7570 dest->type->elements = rval->type->elements;
7571 }
7572 }
7573 if (!equiv_types(dest->type, rval->type)) {
7574 error(state, 0, "Incompatible types in inializer");
7575 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007576 MISC(dest, 0) = rval;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007577 insert_triple(state, dest, rval);
7578 rval->id |= TRIPLE_FLAG_FLATTENED;
7579 use_triple(MISC(dest, 0), dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007580 }
7581 return def;
7582}
7583
7584struct type *arithmetic_result(
7585 struct compile_state *state, struct triple *left, struct triple *right)
7586{
7587 struct type *type;
7588 /* Sanity checks to ensure I am working with arithmetic types */
7589 arithmetic(state, left);
7590 arithmetic(state, right);
7591 type = new_type(
7592 do_arithmetic_conversion(
Eric Biederman90089602004-05-28 14:11:54 +00007593 get_basic_type(left->type),
7594 get_basic_type(right->type)),
7595 0, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007596 return type;
7597}
7598
7599struct type *ptr_arithmetic_result(
7600 struct compile_state *state, struct triple *left, struct triple *right)
7601{
7602 struct type *type;
7603 /* Sanity checks to ensure I am working with the proper types */
7604 ptr_arithmetic(state, left);
7605 arithmetic(state, right);
7606 if (TYPE_ARITHMETIC(left->type->type) &&
7607 TYPE_ARITHMETIC(right->type->type)) {
7608 type = arithmetic_result(state, left, right);
7609 }
7610 else if (TYPE_PTR(left->type->type)) {
7611 type = left->type;
7612 }
7613 else {
7614 internal_error(state, 0, "huh?");
7615 type = 0;
7616 }
7617 return type;
7618}
7619
Eric Biedermanb138ac82003-04-22 18:44:01 +00007620/* boolean helper function */
7621
7622static struct triple *ltrue_expr(struct compile_state *state,
7623 struct triple *expr)
7624{
7625 switch(expr->op) {
7626 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
7627 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
7628 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
7629 /* If the expression is already boolean do nothing */
7630 break;
7631 default:
7632 expr = triple(state, OP_LTRUE, &int_type, expr, 0);
7633 break;
7634 }
7635 return expr;
7636}
7637
7638static struct triple *lfalse_expr(struct compile_state *state,
7639 struct triple *expr)
7640{
7641 return triple(state, OP_LFALSE, &int_type, expr, 0);
7642}
7643
Eric Biederman90089602004-05-28 14:11:54 +00007644static struct triple *mkland_expr(
7645 struct compile_state *state,
7646 struct triple *left, struct triple *right)
7647{
7648 struct triple *def, *val, *var, *jmp, *mid, *end;
Eric Biederman41203d92004-11-08 09:31:09 +00007649 struct triple *lstore, *rstore;
Eric Biederman90089602004-05-28 14:11:54 +00007650
7651 /* Generate some intermediate triples */
7652 end = label(state);
7653 var = variable(state, &int_type);
7654
7655 /* Store the left hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007656 lstore = write_expr(state, var, left);
Eric Biederman90089602004-05-28 14:11:54 +00007657
7658 /* Jump if the value is false */
7659 jmp = branch(state, end,
7660 lfalse_expr(state, read_expr(state, var)));
7661 mid = label(state);
7662
7663 /* Store the right hand side value */
Eric Biederman41203d92004-11-08 09:31:09 +00007664 rstore = write_expr(state, var, right);
Eric Biederman90089602004-05-28 14:11:54 +00007665
7666 /* An expression for the computed value */
7667 val = read_expr(state, var);
7668
7669 /* Generate the prog for a logical and */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007670 def = mkprog(state, var, lstore, jmp, mid, rstore, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007671
7672 return def;
7673}
7674
7675static struct triple *mklor_expr(
7676 struct compile_state *state,
7677 struct triple *left, struct triple *right)
7678{
7679 struct triple *def, *val, *var, *jmp, *mid, *end;
7680
7681 /* Generate some intermediate triples */
7682 end = label(state);
7683 var = variable(state, &int_type);
7684
7685 /* Store the left hand side value */
7686 left = write_expr(state, var, left);
7687
7688 /* Jump if the value is true */
7689 jmp = branch(state, end, read_expr(state, var));
7690 mid = label(state);
7691
7692 /* Store the right hand side value */
7693 right = write_expr(state, var, right);
7694
7695 /* An expression for the computed value*/
7696 val = read_expr(state, var);
7697
7698 /* Generate the prog for a logical or */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007699 def = mkprog(state, var, left, jmp, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007700
7701 return def;
7702}
7703
7704static struct triple *mkcond_expr(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007705 struct compile_state *state,
7706 struct triple *test, struct triple *left, struct triple *right)
7707{
Eric Biederman90089602004-05-28 14:11:54 +00007708 struct triple *def, *val, *var, *jmp1, *jmp2, *top, *mid, *end;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007709 struct type *result_type;
7710 unsigned int left_type, right_type;
7711 bool(state, test);
7712 left_type = left->type->type;
7713 right_type = right->type->type;
7714 result_type = 0;
7715 /* Both operands have arithmetic type */
7716 if (TYPE_ARITHMETIC(left_type) && TYPE_ARITHMETIC(right_type)) {
7717 result_type = arithmetic_result(state, left, right);
7718 }
7719 /* Both operands have void type */
7720 else if (((left_type & TYPE_MASK) == TYPE_VOID) &&
7721 ((right_type & TYPE_MASK) == TYPE_VOID)) {
7722 result_type = &void_type;
7723 }
7724 /* pointers to the same type... */
7725 else if ((result_type = compatible_ptrs(left->type, right->type))) {
7726 ;
7727 }
7728 /* Both operands are pointers and left is a pointer to void */
7729 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7730 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7731 ((left->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7732 result_type = right->type;
7733 }
7734 /* Both operands are pointers and right is a pointer to void */
7735 else if (((left_type & TYPE_MASK) == TYPE_POINTER) &&
7736 ((right_type & TYPE_MASK) == TYPE_POINTER) &&
7737 ((right->type->left->type & TYPE_MASK) == TYPE_VOID)) {
7738 result_type = left->type;
7739 }
7740 if (!result_type) {
7741 error(state, 0, "Incompatible types in conditional expression");
7742 }
Eric Biederman90089602004-05-28 14:11:54 +00007743 /* Generate some intermediate triples */
7744 mid = label(state);
7745 end = label(state);
7746 var = variable(state, result_type);
7747
7748 /* Branch if the test is false */
7749 jmp1 = branch(state, mid, lfalse_expr(state, read_expr(state, test)));
7750 top = label(state);
7751
7752 /* Store the left hand side value */
7753 left = write_expr(state, var, left);
7754
7755 /* Branch to the end */
7756 jmp2 = branch(state, end, 0);
7757
7758 /* Store the right hand side value */
7759 right = write_expr(state, var, right);
7760
7761 /* An expression for the computed value */
7762 val = read_expr(state, var);
7763
7764 /* Generate the prog for a conditional expression */
Stefan Reinauer7db27ee2006-02-19 14:43:48 +00007765 def = mkprog(state, var, jmp1, top, left, jmp2, mid, right, end, val, 0UL);
Eric Biederman90089602004-05-28 14:11:54 +00007766
Eric Biedermanb138ac82003-04-22 18:44:01 +00007767 return def;
7768}
7769
7770
Eric Biederman0babc1c2003-05-09 02:39:00 +00007771static int expr_depth(struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007772{
Stefan Reinauer50542a82007-10-24 11:14:14 +00007773#if DEBUG_ROMCC_WARNINGS
Eric Biederman90089602004-05-28 14:11:54 +00007774#warning "FIXME move optimal ordering of subexpressions into the optimizer"
Stefan Reinauer50542a82007-10-24 11:14:14 +00007775#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00007776 int count;
7777 count = 0;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007778 if (!ins || (ins->id & TRIPLE_FLAG_FLATTENED)) {
7779 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007780 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007781 else if (ins->op == OP_DEREF) {
7782 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007783 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007784 else if (ins->op == OP_VAL) {
7785 count = expr_depth(state, RHS(ins, 0)) - 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007786 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00007787 else if (ins->op == OP_FCALL) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007788 /* Don't figure the depth of a call just guess it is huge */
7789 count = 1000;
7790 }
7791 else {
7792 struct triple **expr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007793 expr = triple_rhs(state, ins, 0);
7794 for(;expr; expr = triple_rhs(state, ins, expr)) {
7795 if (*expr) {
7796 int depth;
7797 depth = expr_depth(state, *expr);
7798 if (depth > count) {
7799 count = depth;
7800 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007801 }
7802 }
7803 }
7804 return count + 1;
7805}
7806
Eric Biederman0babc1c2003-05-09 02:39:00 +00007807static struct triple *flatten_generic(
Eric Biederman5ade04a2003-10-22 04:03:46 +00007808 struct compile_state *state, struct triple *first, struct triple *ptr,
7809 int ignored)
Eric Biedermanb138ac82003-04-22 18:44:01 +00007810{
Eric Biederman0babc1c2003-05-09 02:39:00 +00007811 struct rhs_vector {
7812 int depth;
7813 struct triple **ins;
7814 } vector[MAX_RHS];
7815 int i, rhs, lhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007816 /* Only operations with just a rhs and a lhs should come here */
Eric Biederman90089602004-05-28 14:11:54 +00007817 rhs = ptr->rhs;
7818 lhs = ptr->lhs;
7819 if (TRIPLE_SIZE(ptr) != lhs + rhs + ignored) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00007820 internal_error(state, ptr, "unexpected args for: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +00007821 ptr->op, tops(ptr->op));
7822 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007823 /* Find the depth of the rhs elements */
7824 for(i = 0; i < rhs; i++) {
7825 vector[i].ins = &RHS(ptr, i);
7826 vector[i].depth = expr_depth(state, *vector[i].ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007827 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007828 /* Selection sort the rhs */
7829 for(i = 0; i < rhs; i++) {
7830 int j, max = i;
7831 for(j = i + 1; j < rhs; j++ ) {
7832 if (vector[j].depth > vector[max].depth) {
7833 max = j;
7834 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007835 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007836 if (max != i) {
7837 struct rhs_vector tmp;
7838 tmp = vector[i];
7839 vector[i] = vector[max];
7840 vector[max] = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007841 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007842 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00007843 /* Now flatten the rhs elements */
7844 for(i = 0; i < rhs; i++) {
7845 *vector[i].ins = flatten(state, first, *vector[i].ins);
7846 use_triple(*vector[i].ins, ptr);
7847 }
Eric Biederman90089602004-05-28 14:11:54 +00007848 if (lhs) {
7849 insert_triple(state, first, ptr);
7850 ptr->id |= TRIPLE_FLAG_FLATTENED;
7851 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7852
7853 /* Now flatten the lhs elements */
7854 for(i = 0; i < lhs; i++) {
7855 struct triple **ins = &LHS(ptr, i);
7856 *ins = flatten(state, first, *ins);
7857 use_triple(*ins, ptr);
7858 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007859 }
7860 return ptr;
7861}
7862
Eric Biederman90089602004-05-28 14:11:54 +00007863static struct triple *flatten_prog(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007864 struct compile_state *state, struct triple *first, struct triple *ptr)
7865{
Eric Biederman90089602004-05-28 14:11:54 +00007866 struct triple *head, *body, *val;
7867 head = RHS(ptr, 0);
7868 RHS(ptr, 0) = 0;
7869 val = head->prev;
7870 body = head->next;
7871 release_triple(state, head);
7872 release_triple(state, ptr);
7873 val->next = first;
7874 body->prev = first->prev;
7875 body->prev->next = body;
7876 val->next->prev = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007877
Eric Biederman90089602004-05-28 14:11:54 +00007878 if (triple_is_cbranch(state, body->prev) ||
7879 triple_is_call(state, body->prev)) {
7880 unuse_triple(first, body->prev);
7881 use_triple(body, body->prev);
7882 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007883
Eric Biederman90089602004-05-28 14:11:54 +00007884 if (!(val->id & TRIPLE_FLAG_FLATTENED)) {
7885 internal_error(state, val, "val not flattened?");
7886 }
7887
7888 return val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007889}
7890
Eric Biederman90089602004-05-28 14:11:54 +00007891
7892static struct triple *flatten_part(
Eric Biedermanb138ac82003-04-22 18:44:01 +00007893 struct compile_state *state, struct triple *first, struct triple *ptr)
7894{
Eric Biederman90089602004-05-28 14:11:54 +00007895 if (!triple_is_part(state, ptr)) {
7896 internal_error(state, ptr, "not a part");
7897 }
7898 if (ptr->rhs || ptr->lhs || ptr->targ || (ptr->misc != 1)) {
7899 internal_error(state, ptr, "unexpected args for: %d %s",
7900 ptr->op, tops(ptr->op));
7901 }
7902 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7903 use_triple(MISC(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007904 return flatten_generic(state, first, ptr, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007905}
7906
7907static struct triple *flatten(
7908 struct compile_state *state, struct triple *first, struct triple *ptr)
7909{
7910 struct triple *orig_ptr;
7911 if (!ptr)
7912 return 0;
7913 do {
7914 orig_ptr = ptr;
Eric Biederman0babc1c2003-05-09 02:39:00 +00007915 /* Only flatten triples once */
7916 if (ptr->id & TRIPLE_FLAG_FLATTENED) {
7917 return ptr;
7918 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007919 switch(ptr->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00007920 case OP_VAL:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007921 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7922 return MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007923 break;
Eric Biederman90089602004-05-28 14:11:54 +00007924 case OP_PROG:
7925 ptr = flatten_prog(state, first, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007926 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007927 case OP_FCALL:
Eric Biederman90089602004-05-28 14:11:54 +00007928 ptr = flatten_generic(state, first, ptr, 1);
7929 insert_triple(state, first, ptr);
7930 ptr->id |= TRIPLE_FLAG_FLATTENED;
7931 ptr->id &= ~TRIPLE_FLAG_LOCAL;
7932 if (ptr->next != ptr) {
7933 use_triple(ptr->next, ptr);
7934 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00007935 break;
7936 case OP_READ:
7937 case OP_LOAD:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007938 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7939 use_triple(RHS(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007940 break;
Eric Biederman90089602004-05-28 14:11:54 +00007941 case OP_WRITE:
7942 ptr = flatten_generic(state, first, ptr, 1);
7943 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7944 use_triple(MISC(ptr, 0), ptr);
7945 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007946 case OP_BRANCH:
Eric Biederman0babc1c2003-05-09 02:39:00 +00007947 use_triple(TARG(ptr, 0), ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +00007948 break;
7949 case OP_CBRANCH:
7950 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7951 use_triple(RHS(ptr, 0), ptr);
7952 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007953 insert_triple(state, first, ptr);
7954 ptr->id |= TRIPLE_FLAG_FLATTENED;
7955 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007956 if (ptr->next != ptr) {
7957 use_triple(ptr->next, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007958 }
7959 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007960 case OP_CALL:
7961 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
7962 use_triple(MISC(ptr, 0), ptr);
7963 use_triple(TARG(ptr, 0), ptr);
Eric Biederman90089602004-05-28 14:11:54 +00007964 insert_triple(state, first, ptr);
7965 ptr->id |= TRIPLE_FLAG_FLATTENED;
7966 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00007967 if (ptr->next != ptr) {
7968 use_triple(ptr->next, ptr);
7969 }
7970 break;
7971 case OP_RET:
7972 RHS(ptr, 0) = flatten(state, first, RHS(ptr, 0));
7973 use_triple(RHS(ptr, 0), ptr);
7974 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007975 case OP_BLOBCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +00007976 insert_triple(state, state->global_pool, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00007977 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00007978 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007979 ptr = triple(state, OP_SDECL, ptr->type, ptr, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00007980 use_triple(MISC(ptr, 0), ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +00007981 break;
7982 case OP_DEREF:
7983 /* Since OP_DEREF is just a marker delete it when I flatten it */
Eric Biederman0babc1c2003-05-09 02:39:00 +00007984 ptr = RHS(ptr, 0);
7985 RHS(orig_ptr, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007986 free_triple(state, orig_ptr);
7987 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00007988 case OP_DOT:
Eric Biederman90089602004-05-28 14:11:54 +00007989 if (RHS(ptr, 0)->op == OP_DEREF) {
7990 struct triple *base, *left;
Eric Biederman00443072003-06-24 12:34:45 +00007991 ulong_t offset;
Eric Biederman90089602004-05-28 14:11:54 +00007992 base = MISC(ptr, 0);
7993 offset = bits_to_bytes(field_offset(state, base->type, ptr->u.field));
Eric Biederman03b59862003-06-24 14:27:37 +00007994 left = RHS(base, 0);
7995 ptr = triple(state, OP_ADD, left->type,
7996 read_expr(state, left),
Eric Biederman00443072003-06-24 12:34:45 +00007997 int_const(state, &ulong_type, offset));
7998 free_triple(state, base);
7999 }
Eric Biederman90089602004-05-28 14:11:54 +00008000 else {
8001 ptr = flatten_part(state, first, ptr);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008002 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008003 break;
Eric Biederman90089602004-05-28 14:11:54 +00008004 case OP_INDEX:
8005 if (RHS(ptr, 0)->op == OP_DEREF) {
8006 struct triple *base, *left;
8007 ulong_t offset;
8008 base = MISC(ptr, 0);
8009 offset = bits_to_bytes(index_offset(state, base->type, ptr->u.cval));
8010 left = RHS(base, 0);
8011 ptr = triple(state, OP_ADD, left->type,
8012 read_expr(state, left),
8013 int_const(state, &long_type, offset));
8014 free_triple(state, base);
8015 }
8016 else {
8017 ptr = flatten_part(state, first, ptr);
8018 }
8019 break;
Eric Biederman8d9c1232003-06-17 08:42:17 +00008020 case OP_PIECE:
Eric Biederman90089602004-05-28 14:11:54 +00008021 ptr = flatten_part(state, first, ptr);
Eric Biederman8d9c1232003-06-17 08:42:17 +00008022 use_triple(ptr, MISC(ptr, 0));
8023 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008024 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008025 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8026 use_triple(MISC(ptr, 0), ptr);
8027 break;
Eric Biederman83b991a2003-10-11 06:20:25 +00008028 case OP_SDECL:
Eric Biederman5ade04a2003-10-22 04:03:46 +00008029 first = state->global_pool;
Eric Biederman83b991a2003-10-11 06:20:25 +00008030 MISC(ptr, 0) = flatten(state, first, MISC(ptr, 0));
8031 use_triple(MISC(ptr, 0), ptr);
8032 insert_triple(state, first, ptr);
8033 ptr->id |= TRIPLE_FLAG_FLATTENED;
8034 ptr->id &= ~TRIPLE_FLAG_LOCAL;
8035 return ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008036 case OP_ADECL:
Eric Biederman90089602004-05-28 14:11:54 +00008037 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008038 break;
8039 default:
8040 /* Flatten the easy cases we don't override */
Eric Biederman5ade04a2003-10-22 04:03:46 +00008041 ptr = flatten_generic(state, first, ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008042 break;
8043 }
8044 } while(ptr && (ptr != orig_ptr));
Eric Biederman90089602004-05-28 14:11:54 +00008045 if (ptr && !(ptr->id & TRIPLE_FLAG_FLATTENED)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008046 insert_triple(state, first, ptr);
8047 ptr->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman83b991a2003-10-11 06:20:25 +00008048 ptr->id &= ~TRIPLE_FLAG_LOCAL;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008049 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008050 return ptr;
8051}
8052
8053static void release_expr(struct compile_state *state, struct triple *expr)
8054{
8055 struct triple *head;
8056 head = label(state);
8057 flatten(state, head, expr);
8058 while(head->next != head) {
8059 release_triple(state, head->next);
8060 }
8061 free_triple(state, head);
8062}
8063
8064static int replace_rhs_use(struct compile_state *state,
8065 struct triple *orig, struct triple *new, struct triple *use)
8066{
8067 struct triple **expr;
8068 int found;
8069 found = 0;
8070 expr = triple_rhs(state, use, 0);
8071 for(;expr; expr = triple_rhs(state, use, expr)) {
8072 if (*expr == orig) {
8073 *expr = new;
8074 found = 1;
8075 }
8076 }
8077 if (found) {
8078 unuse_triple(orig, use);
8079 use_triple(new, use);
8080 }
8081 return found;
8082}
8083
8084static int replace_lhs_use(struct compile_state *state,
8085 struct triple *orig, struct triple *new, struct triple *use)
8086{
8087 struct triple **expr;
8088 int found;
8089 found = 0;
8090 expr = triple_lhs(state, use, 0);
8091 for(;expr; expr = triple_lhs(state, use, expr)) {
8092 if (*expr == orig) {
8093 *expr = new;
8094 found = 1;
8095 }
8096 }
8097 if (found) {
8098 unuse_triple(orig, use);
8099 use_triple(new, use);
8100 }
8101 return found;
8102}
8103
Eric Biederman90089602004-05-28 14:11:54 +00008104static int replace_misc_use(struct compile_state *state,
8105 struct triple *orig, struct triple *new, struct triple *use)
8106{
8107 struct triple **expr;
8108 int found;
8109 found = 0;
8110 expr = triple_misc(state, use, 0);
8111 for(;expr; expr = triple_misc(state, use, expr)) {
8112 if (*expr == orig) {
8113 *expr = new;
8114 found = 1;
8115 }
8116 }
8117 if (found) {
8118 unuse_triple(orig, use);
8119 use_triple(new, use);
8120 }
8121 return found;
8122}
8123
8124static int replace_targ_use(struct compile_state *state,
8125 struct triple *orig, struct triple *new, struct triple *use)
8126{
8127 struct triple **expr;
8128 int found;
8129 found = 0;
8130 expr = triple_targ(state, use, 0);
8131 for(;expr; expr = triple_targ(state, use, expr)) {
8132 if (*expr == orig) {
8133 *expr = new;
8134 found = 1;
8135 }
8136 }
8137 if (found) {
8138 unuse_triple(orig, use);
8139 use_triple(new, use);
8140 }
8141 return found;
8142}
8143
8144static void replace_use(struct compile_state *state,
8145 struct triple *orig, struct triple *new, struct triple *use)
8146{
8147 int found;
8148 found = 0;
8149 found |= replace_rhs_use(state, orig, new, use);
8150 found |= replace_lhs_use(state, orig, new, use);
8151 found |= replace_misc_use(state, orig, new, use);
8152 found |= replace_targ_use(state, orig, new, use);
8153 if (!found) {
8154 internal_error(state, use, "use without use");
8155 }
8156}
8157
Eric Biedermanb138ac82003-04-22 18:44:01 +00008158static void propogate_use(struct compile_state *state,
8159 struct triple *orig, struct triple *new)
8160{
8161 struct triple_set *user, *next;
8162 for(user = orig->use; user; user = next) {
Eric Biederman90089602004-05-28 14:11:54 +00008163 /* Careful replace_use modifies the use chain and
8164 * removes use. So we must get a copy of the next
8165 * entry early.
8166 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008167 next = user->next;
Eric Biederman90089602004-05-28 14:11:54 +00008168 replace_use(state, orig, new, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008169 }
8170 if (orig->use) {
8171 internal_error(state, orig, "used after propogate_use");
8172 }
8173}
8174
8175/*
8176 * Code generators
8177 * ===========================
8178 */
8179
Eric Biederman90089602004-05-28 14:11:54 +00008180static struct triple *mk_cast_expr(
8181 struct compile_state *state, struct type *type, struct triple *expr)
8182{
8183 struct triple *def;
8184 def = read_expr(state, expr);
8185 def = triple(state, OP_CONVERT, type, def, 0);
8186 return def;
8187}
8188
Eric Biedermanb138ac82003-04-22 18:44:01 +00008189static struct triple *mk_add_expr(
8190 struct compile_state *state, struct triple *left, struct triple *right)
8191{
8192 struct type *result_type;
8193 /* Put pointer operands on the left */
8194 if (is_pointer(right)) {
8195 struct triple *tmp;
8196 tmp = left;
8197 left = right;
8198 right = tmp;
8199 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008200 left = read_expr(state, left);
8201 right = read_expr(state, right);
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008202 result_type = ptr_arithmetic_result(state, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008203 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008204 struct type *ptr_math;
8205 int op;
8206 if (is_signed(right->type)) {
8207 ptr_math = &long_type;
8208 op = OP_SMUL;
8209 } else {
8210 ptr_math = &ulong_type;
8211 op = OP_UMUL;
8212 }
8213 if (!equiv_types(right->type, ptr_math)) {
8214 right = mk_cast_expr(state, ptr_math, right);
8215 }
8216 right = triple(state, op, ptr_math, right,
8217 int_const(state, ptr_math,
8218 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008219 }
8220 return triple(state, OP_ADD, result_type, left, right);
8221}
8222
8223static struct triple *mk_sub_expr(
8224 struct compile_state *state, struct triple *left, struct triple *right)
8225{
8226 struct type *result_type;
8227 result_type = ptr_arithmetic_result(state, left, right);
8228 left = read_expr(state, left);
8229 right = read_expr(state, right);
8230 if (is_pointer(left)) {
Eric Biederman90089602004-05-28 14:11:54 +00008231 struct type *ptr_math;
8232 int op;
8233 if (is_signed(right->type)) {
8234 ptr_math = &long_type;
8235 op = OP_SMUL;
8236 } else {
8237 ptr_math = &ulong_type;
8238 op = OP_UMUL;
8239 }
8240 if (!equiv_types(right->type, ptr_math)) {
8241 right = mk_cast_expr(state, ptr_math, right);
8242 }
8243 right = triple(state, op, ptr_math, right,
8244 int_const(state, ptr_math,
8245 size_of_in_bytes(state, left->type->left)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00008246 }
8247 return triple(state, OP_SUB, result_type, left, right);
8248}
8249
8250static struct triple *mk_pre_inc_expr(
8251 struct compile_state *state, struct triple *def)
8252{
8253 struct triple *val;
8254 lvalue(state, def);
8255 val = mk_add_expr(state, def, int_const(state, &int_type, 1));
8256 return triple(state, OP_VAL, def->type,
8257 write_expr(state, def, val),
8258 val);
8259}
8260
8261static struct triple *mk_pre_dec_expr(
8262 struct compile_state *state, struct triple *def)
8263{
8264 struct triple *val;
8265 lvalue(state, def);
8266 val = mk_sub_expr(state, def, int_const(state, &int_type, 1));
8267 return triple(state, OP_VAL, def->type,
8268 write_expr(state, def, val),
8269 val);
8270}
8271
8272static struct triple *mk_post_inc_expr(
8273 struct compile_state *state, struct triple *def)
8274{
8275 struct triple *val;
8276 lvalue(state, def);
8277 val = read_expr(state, def);
8278 return triple(state, OP_VAL, def->type,
8279 write_expr(state, def,
8280 mk_add_expr(state, val, int_const(state, &int_type, 1)))
8281 , val);
8282}
8283
8284static struct triple *mk_post_dec_expr(
8285 struct compile_state *state, struct triple *def)
8286{
8287 struct triple *val;
8288 lvalue(state, def);
8289 val = read_expr(state, def);
8290 return triple(state, OP_VAL, def->type,
8291 write_expr(state, def,
8292 mk_sub_expr(state, val, int_const(state, &int_type, 1)))
8293 , val);
8294}
8295
8296static struct triple *mk_subscript_expr(
8297 struct compile_state *state, struct triple *left, struct triple *right)
8298{
8299 left = read_expr(state, left);
8300 right = read_expr(state, right);
8301 if (!is_pointer(left) && !is_pointer(right)) {
8302 error(state, left, "subscripted value is not a pointer");
8303 }
8304 return mk_deref_expr(state, mk_add_expr(state, left, right));
8305}
8306
Eric Biedermane058a1e2003-07-12 01:21:31 +00008307
Eric Biedermanb138ac82003-04-22 18:44:01 +00008308/*
8309 * Compile time evaluation
8310 * ===========================
8311 */
8312static int is_const(struct triple *ins)
8313{
8314 return IS_CONST_OP(ins->op);
8315}
8316
Eric Biederman83b991a2003-10-11 06:20:25 +00008317static int is_simple_const(struct triple *ins)
8318{
Eric Biederman90089602004-05-28 14:11:54 +00008319 /* Is this a constant that u.cval has the value.
8320 * Or equivalently is this a constant that read_const
8321 * works on.
8322 * So far only OP_INTCONST qualifies.
8323 */
8324 return (ins->op == OP_INTCONST);
Eric Biederman83b991a2003-10-11 06:20:25 +00008325}
8326
Eric Biedermanb138ac82003-04-22 18:44:01 +00008327static int constants_equal(struct compile_state *state,
8328 struct triple *left, struct triple *right)
8329{
8330 int equal;
Eric Biederman90089602004-05-28 14:11:54 +00008331 if ((left->op == OP_UNKNOWNVAL) || (right->op == OP_UNKNOWNVAL)) {
8332 equal = 0;
8333 }
8334 else if (!is_const(left) || !is_const(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008335 equal = 0;
8336 }
8337 else if (left->op != right->op) {
8338 equal = 0;
8339 }
8340 else if (!equiv_types(left->type, right->type)) {
8341 equal = 0;
8342 }
8343 else {
8344 equal = 0;
8345 switch(left->op) {
8346 case OP_INTCONST:
8347 if (left->u.cval == right->u.cval) {
8348 equal = 1;
8349 }
8350 break;
8351 case OP_BLOBCONST:
8352 {
Eric Biederman90089602004-05-28 14:11:54 +00008353 size_t lsize, rsize, bytes;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008354 lsize = size_of(state, left->type);
8355 rsize = size_of(state, right->type);
8356 if (lsize != rsize) {
8357 break;
8358 }
Eric Biederman90089602004-05-28 14:11:54 +00008359 bytes = bits_to_bytes(lsize);
8360 if (memcmp(left->u.blob, right->u.blob, bytes) == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008361 equal = 1;
8362 }
8363 break;
8364 }
8365 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008366 if ((MISC(left, 0) == MISC(right, 0)) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +00008367 (left->u.cval == right->u.cval)) {
8368 equal = 1;
8369 }
8370 break;
8371 default:
8372 internal_error(state, left, "uknown constant type");
8373 break;
8374 }
8375 }
8376 return equal;
8377}
8378
8379static int is_zero(struct triple *ins)
8380{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008381 return is_simple_const(ins) && (ins->u.cval == 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008382}
8383
8384static int is_one(struct triple *ins)
8385{
Eric Biederman5ade04a2003-10-22 04:03:46 +00008386 return is_simple_const(ins) && (ins->u.cval == 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008387}
8388
Stefan Reinauer50542a82007-10-24 11:14:14 +00008389#if DEBUG_ROMCC_WARNING
Eric Biederman530b5192003-07-01 10:05:30 +00008390static long_t bit_count(ulong_t value)
8391{
8392 int count;
8393 int i;
8394 count = 0;
8395 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8396 ulong_t mask;
8397 mask = 1;
8398 mask <<= i;
8399 if (value & mask) {
8400 count++;
8401 }
8402 }
8403 return count;
8404
8405}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008406#endif
8407
Eric Biedermanb138ac82003-04-22 18:44:01 +00008408static long_t bsr(ulong_t value)
8409{
8410 int i;
8411 for(i = (sizeof(ulong_t)*8) -1; i >= 0; i--) {
8412 ulong_t mask;
8413 mask = 1;
8414 mask <<= i;
8415 if (value & mask) {
8416 return i;
8417 }
8418 }
8419 return -1;
8420}
8421
8422static long_t bsf(ulong_t value)
8423{
8424 int i;
8425 for(i = 0; i < (sizeof(ulong_t)*8); i++) {
8426 ulong_t mask;
8427 mask = 1;
8428 mask <<= 1;
8429 if (value & mask) {
8430 return i;
8431 }
8432 }
8433 return -1;
8434}
8435
Eric Biedermancb364952004-11-15 10:46:44 +00008436static long_t ilog2(ulong_t value)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008437{
8438 return bsr(value);
8439}
8440
8441static long_t tlog2(struct triple *ins)
8442{
Eric Biedermancb364952004-11-15 10:46:44 +00008443 return ilog2(ins->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008444}
8445
8446static int is_pow2(struct triple *ins)
8447{
8448 ulong_t value, mask;
8449 long_t log;
8450 if (!is_const(ins)) {
8451 return 0;
8452 }
8453 value = ins->u.cval;
Eric Biedermancb364952004-11-15 10:46:44 +00008454 log = ilog2(value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008455 if (log == -1) {
8456 return 0;
8457 }
8458 mask = 1;
8459 mask <<= log;
8460 return ((value & mask) == value);
8461}
8462
8463static ulong_t read_const(struct compile_state *state,
Eric Biederman5ade04a2003-10-22 04:03:46 +00008464 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008465{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008466 switch(rhs->type->type &TYPE_MASK) {
8467 case TYPE_CHAR:
8468 case TYPE_SHORT:
8469 case TYPE_INT:
8470 case TYPE_LONG:
8471 case TYPE_UCHAR:
8472 case TYPE_USHORT:
8473 case TYPE_UINT:
8474 case TYPE_ULONG:
8475 case TYPE_POINTER:
Eric Biederman90089602004-05-28 14:11:54 +00008476 case TYPE_BITFIELD:
Eric Biedermanb138ac82003-04-22 18:44:01 +00008477 break;
8478 default:
Eric Biederman90089602004-05-28 14:11:54 +00008479 fprintf(state->errout, "type: ");
8480 name_of(state->errout, rhs->type);
8481 fprintf(state->errout, "\n");
8482 internal_warning(state, rhs, "bad type to read_const");
Eric Biedermanb138ac82003-04-22 18:44:01 +00008483 break;
8484 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008485 if (!is_simple_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008486 internal_error(state, rhs, "bad op to read_const");
Eric Biederman83b991a2003-10-11 06:20:25 +00008487 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008488 return rhs->u.cval;
8489}
8490
Eric Biederman5ade04a2003-10-22 04:03:46 +00008491static long_t read_sconst(struct compile_state *state,
8492 struct triple *ins, struct triple *rhs)
Eric Biedermanb138ac82003-04-22 18:44:01 +00008493{
Eric Biedermanb138ac82003-04-22 18:44:01 +00008494 return (long_t)(rhs->u.cval);
8495}
8496
Eric Biederman5ade04a2003-10-22 04:03:46 +00008497int const_ltrue(struct compile_state *state, struct triple *ins, struct triple *rhs)
8498{
8499 if (!is_const(rhs)) {
Eric Biederman90089602004-05-28 14:11:54 +00008500 internal_error(state, 0, "non const passed to const_true");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008501 }
8502 return !is_zero(rhs);
8503}
8504
8505int const_eq(struct compile_state *state, struct triple *ins,
8506 struct triple *left, struct triple *right)
8507{
8508 int result;
8509 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008510 internal_warning(state, ins, "non const passed to const_eq");
8511 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008512 }
8513 else if (left == right) {
8514 result = 1;
8515 }
8516 else if (is_simple_const(left) && is_simple_const(right)) {
8517 ulong_t lval, rval;
8518 lval = read_const(state, ins, left);
8519 rval = read_const(state, ins, right);
8520 result = (lval == rval);
8521 }
8522 else if ((left->op == OP_ADDRCONST) &&
8523 (right->op == OP_ADDRCONST)) {
8524 result = (MISC(left, 0) == MISC(right, 0)) &&
8525 (left->u.cval == right->u.cval);
8526 }
8527 else {
Eric Biederman90089602004-05-28 14:11:54 +00008528 internal_warning(state, ins, "incomparable constants passed to const_eq");
8529 result = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +00008530 }
8531 return result;
8532
8533}
8534
8535int const_ucmp(struct compile_state *state, struct triple *ins,
8536 struct triple *left, struct triple *right)
8537{
8538 int result;
8539 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008540 internal_warning(state, ins, "non const past to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008541 result = -2;
8542 }
8543 else if (left == right) {
8544 result = 0;
8545 }
8546 else if (is_simple_const(left) && is_simple_const(right)) {
8547 ulong_t lval, rval;
8548 lval = read_const(state, ins, left);
8549 rval = read_const(state, ins, right);
8550 result = 0;
8551 if (lval > rval) {
8552 result = 1;
8553 } else if (rval > lval) {
8554 result = -1;
8555 }
8556 }
8557 else if ((left->op == OP_ADDRCONST) &&
8558 (right->op == OP_ADDRCONST) &&
8559 (MISC(left, 0) == MISC(right, 0))) {
8560 result = 0;
8561 if (left->u.cval > right->u.cval) {
8562 result = 1;
8563 } else if (left->u.cval < right->u.cval) {
8564 result = -1;
8565 }
8566 }
8567 else {
Eric Biederman90089602004-05-28 14:11:54 +00008568 internal_warning(state, ins, "incomparable constants passed to const_ucmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008569 result = -2;
8570 }
8571 return result;
8572}
8573
8574int const_scmp(struct compile_state *state, struct triple *ins,
8575 struct triple *left, struct triple *right)
8576{
8577 int result;
8578 if (!is_const(left) || !is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00008579 internal_warning(state, ins, "non const past to ucmp_const");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008580 result = -2;
8581 }
8582 else if (left == right) {
8583 result = 0;
8584 }
8585 else if (is_simple_const(left) && is_simple_const(right)) {
8586 long_t lval, rval;
8587 lval = read_sconst(state, ins, left);
8588 rval = read_sconst(state, ins, right);
8589 result = 0;
8590 if (lval > rval) {
8591 result = 1;
8592 } else if (rval > lval) {
8593 result = -1;
8594 }
8595 }
8596 else {
Eric Biederman90089602004-05-28 14:11:54 +00008597 internal_warning(state, ins, "incomparable constants passed to const_scmp");
Eric Biederman5ade04a2003-10-22 04:03:46 +00008598 result = -2;
8599 }
8600 return result;
8601}
8602
Eric Biedermanb138ac82003-04-22 18:44:01 +00008603static void unuse_rhs(struct compile_state *state, struct triple *ins)
8604{
8605 struct triple **expr;
8606 expr = triple_rhs(state, ins, 0);
8607 for(;expr;expr = triple_rhs(state, ins, expr)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +00008608 if (*expr) {
8609 unuse_triple(*expr, ins);
8610 *expr = 0;
8611 }
8612 }
8613}
8614
8615static void unuse_lhs(struct compile_state *state, struct triple *ins)
8616{
8617 struct triple **expr;
8618 expr = triple_lhs(state, ins, 0);
8619 for(;expr;expr = triple_lhs(state, ins, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00008620 unuse_triple(*expr, ins);
8621 *expr = 0;
8622 }
8623}
Eric Biederman0babc1c2003-05-09 02:39:00 +00008624
Stefan Reinauer50542a82007-10-24 11:14:14 +00008625#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008626static void unuse_misc(struct compile_state *state, struct triple *ins)
8627{
8628 struct triple **expr;
8629 expr = triple_misc(state, ins, 0);
8630 for(;expr;expr = triple_misc(state, ins, expr)) {
8631 unuse_triple(*expr, ins);
8632 *expr = 0;
8633 }
8634}
8635
8636static void unuse_targ(struct compile_state *state, struct triple *ins)
8637{
8638 int i;
8639 struct triple **slot;
8640 slot = &TARG(ins, 0);
8641 for(i = 0; i < ins->targ; i++) {
8642 unuse_triple(slot[i], ins);
8643 slot[i] = 0;
8644 }
8645}
8646
Eric Biedermanb138ac82003-04-22 18:44:01 +00008647static void check_lhs(struct compile_state *state, struct triple *ins)
8648{
8649 struct triple **expr;
8650 expr = triple_lhs(state, ins, 0);
8651 for(;expr;expr = triple_lhs(state, ins, expr)) {
8652 internal_error(state, ins, "unexpected lhs");
8653 }
8654
8655}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008656#endif
Eric Biederman90089602004-05-28 14:11:54 +00008657
8658static void check_misc(struct compile_state *state, struct triple *ins)
8659{
8660 struct triple **expr;
8661 expr = triple_misc(state, ins, 0);
8662 for(;expr;expr = triple_misc(state, ins, expr)) {
8663 if (*expr) {
8664 internal_error(state, ins, "unexpected misc");
8665 }
8666 }
8667}
8668
Eric Biedermanb138ac82003-04-22 18:44:01 +00008669static void check_targ(struct compile_state *state, struct triple *ins)
8670{
8671 struct triple **expr;
8672 expr = triple_targ(state, ins, 0);
8673 for(;expr;expr = triple_targ(state, ins, expr)) {
8674 internal_error(state, ins, "unexpected targ");
8675 }
8676}
8677
8678static void wipe_ins(struct compile_state *state, struct triple *ins)
8679{
Eric Biederman0babc1c2003-05-09 02:39:00 +00008680 /* Becareful which instructions you replace the wiped
8681 * instruction with, as there are not enough slots
8682 * in all instructions to hold all others.
8683 */
Eric Biedermanb138ac82003-04-22 18:44:01 +00008684 check_targ(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008685 check_misc(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008686 unuse_rhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00008687 unuse_lhs(state, ins);
Eric Biederman90089602004-05-28 14:11:54 +00008688 ins->lhs = 0;
8689 ins->rhs = 0;
8690 ins->misc = 0;
8691 ins->targ = 0;
8692}
8693
Stefan Reinauer50542a82007-10-24 11:14:14 +00008694#if DEBUG_ROMCC_WARNING
Eric Biederman90089602004-05-28 14:11:54 +00008695static void wipe_branch(struct compile_state *state, struct triple *ins)
8696{
8697 /* Becareful which instructions you replace the wiped
8698 * instruction with, as there are not enough slots
8699 * in all instructions to hold all others.
8700 */
8701 unuse_rhs(state, ins);
8702 unuse_lhs(state, ins);
8703 unuse_misc(state, ins);
8704 unuse_targ(state, ins);
8705 ins->lhs = 0;
8706 ins->rhs = 0;
8707 ins->misc = 0;
8708 ins->targ = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008709}
Stefan Reinauer50542a82007-10-24 11:14:14 +00008710#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00008711
8712static void mkcopy(struct compile_state *state,
8713 struct triple *ins, struct triple *rhs)
8714{
Eric Biederman83b991a2003-10-11 06:20:25 +00008715 struct block *block;
Eric Biederman90089602004-05-28 14:11:54 +00008716 if (!equiv_types(ins->type, rhs->type)) {
8717 FILE *fp = state->errout;
8718 fprintf(fp, "src type: ");
8719 name_of(fp, rhs->type);
8720 fprintf(fp, "\ndst type: ");
8721 name_of(fp, ins->type);
8722 fprintf(fp, "\n");
8723 internal_error(state, ins, "mkcopy type mismatch");
8724 }
Eric Biederman83b991a2003-10-11 06:20:25 +00008725 block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008726 wipe_ins(state, ins);
8727 ins->op = OP_COPY;
Eric Biederman90089602004-05-28 14:11:54 +00008728 ins->rhs = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +00008729 ins->u.block = block;
Eric Biederman0babc1c2003-05-09 02:39:00 +00008730 RHS(ins, 0) = rhs;
8731 use_triple(RHS(ins, 0), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008732}
8733
8734static void mkconst(struct compile_state *state,
8735 struct triple *ins, ulong_t value)
8736{
8737 if (!is_integral(ins) && !is_pointer(ins)) {
Eric Biederman90089602004-05-28 14:11:54 +00008738 fprintf(state->errout, "type: ");
8739 name_of(state->errout, ins->type);
8740 fprintf(state->errout, "\n");
8741 internal_error(state, ins, "unknown type to make constant value: %ld",
8742 value);
Eric Biedermanb138ac82003-04-22 18:44:01 +00008743 }
8744 wipe_ins(state, ins);
8745 ins->op = OP_INTCONST;
8746 ins->u.cval = value;
8747}
8748
8749static void mkaddr_const(struct compile_state *state,
8750 struct triple *ins, struct triple *sdecl, ulong_t value)
8751{
Eric Biederman90089602004-05-28 14:11:54 +00008752 if ((sdecl->op != OP_SDECL) && (sdecl->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +00008753 internal_error(state, ins, "bad base for addrconst");
8754 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00008755 wipe_ins(state, ins);
8756 ins->op = OP_ADDRCONST;
Eric Biederman90089602004-05-28 14:11:54 +00008757 ins->misc = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00008758 MISC(ins, 0) = sdecl;
Eric Biedermanb138ac82003-04-22 18:44:01 +00008759 ins->u.cval = value;
8760 use_triple(sdecl, ins);
8761}
8762
Eric Biederman90089602004-05-28 14:11:54 +00008763#if DEBUG_DECOMPOSE_PRINT_TUPLES
8764static void print_tuple(struct compile_state *state,
8765 struct triple *ins, struct triple *tuple)
Eric Biederman0babc1c2003-05-09 02:39:00 +00008766{
Eric Biederman90089602004-05-28 14:11:54 +00008767 FILE *fp = state->dbgout;
8768 fprintf(fp, "%5s %p tuple: %p ", tops(ins->op), ins, tuple);
8769 name_of(fp, tuple->type);
8770 if (tuple->lhs > 0) {
8771 fprintf(fp, " lhs: ");
8772 name_of(fp, LHS(tuple, 0)->type);
8773 }
8774 fprintf(fp, "\n");
8775
8776}
8777#endif
8778
8779static struct triple *decompose_with_tuple(struct compile_state *state,
8780 struct triple *ins, struct triple *tuple)
8781{
8782 struct triple *next;
8783 next = ins->next;
8784 flatten(state, next, tuple);
8785#if DEBUG_DECOMPOSE_PRINT_TUPLES
8786 print_tuple(state, ins, tuple);
8787#endif
8788
8789 if (!is_compound_type(tuple->type) && (tuple->lhs > 0)) {
8790 struct triple *tmp;
8791 if (tuple->lhs != 1) {
8792 internal_error(state, tuple, "plain type in multiple registers?");
8793 }
8794 tmp = LHS(tuple, 0);
8795 release_triple(state, tuple);
8796 tuple = tmp;
8797 }
8798
8799 propogate_use(state, ins, tuple);
8800 release_triple(state, ins);
8801
8802 return next;
8803}
8804
8805static struct triple *decompose_unknownval(struct compile_state *state,
8806 struct triple *ins)
8807{
8808 struct triple *tuple;
8809 ulong_t i;
8810
8811#if DEBUG_DECOMPOSE_HIRES
8812 FILE *fp = state->dbgout;
8813 fprintf(fp, "unknown type: ");
8814 name_of(fp, ins->type);
8815 fprintf(fp, "\n");
8816#endif
8817
8818 get_occurance(ins->occurance);
8819 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8820 ins->occurance);
8821
8822 for(i = 0; i < tuple->lhs; i++) {
8823 struct type *piece_type;
8824 struct triple *unknown;
8825
8826 piece_type = reg_type(state, ins->type, i * REG_SIZEOF_REG);
8827 get_occurance(tuple->occurance);
8828 unknown = alloc_triple(state, OP_UNKNOWNVAL, piece_type, 0, 0,
8829 tuple->occurance);
8830 LHS(tuple, i) = unknown;
8831 }
8832 return decompose_with_tuple(state, ins, tuple);
8833}
8834
8835
8836static struct triple *decompose_read(struct compile_state *state,
8837 struct triple *ins)
8838{
8839 struct triple *tuple, *lval;
8840 ulong_t i;
8841
8842 lval = RHS(ins, 0);
8843
8844 if (lval->op == OP_PIECE) {
8845 return ins->next;
8846 }
8847 get_occurance(ins->occurance);
8848 tuple = alloc_triple(state, OP_TUPLE, lval->type, -1, -1,
8849 ins->occurance);
8850
8851 if ((tuple->lhs != lval->lhs) &&
8852 (!triple_is_def(state, lval) || (tuple->lhs != 1)))
8853 {
8854 internal_error(state, ins, "lhs size inconsistency?");
8855 }
8856 for(i = 0; i < tuple->lhs; i++) {
8857 struct triple *piece, *read, *bitref;
8858 if ((i != 0) || !triple_is_def(state, lval)) {
8859 piece = LHS(lval, i);
8860 } else {
8861 piece = lval;
8862 }
8863
8864 /* See if the piece is really a bitref */
8865 bitref = 0;
8866 if (piece->op == OP_BITREF) {
8867 bitref = piece;
8868 piece = RHS(bitref, 0);
8869 }
8870
8871 get_occurance(tuple->occurance);
8872 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8873 tuple->occurance);
8874 RHS(read, 0) = piece;
8875
8876 if (bitref) {
8877 struct triple *extract;
8878 int op;
8879 if (is_signed(bitref->type->left)) {
8880 op = OP_SEXTRACT;
8881 } else {
8882 op = OP_UEXTRACT;
8883 }
8884 get_occurance(tuple->occurance);
8885 extract = alloc_triple(state, op, bitref->type, -1, -1,
8886 tuple->occurance);
8887 RHS(extract, 0) = read;
8888 extract->u.bitfield.size = bitref->u.bitfield.size;
8889 extract->u.bitfield.offset = bitref->u.bitfield.offset;
8890
8891 read = extract;
8892 }
8893
8894 LHS(tuple, i) = read;
8895 }
8896 return decompose_with_tuple(state, ins, tuple);
8897}
8898
8899static struct triple *decompose_write(struct compile_state *state,
8900 struct triple *ins)
8901{
8902 struct triple *tuple, *lval, *val;
8903 ulong_t i;
8904
8905 lval = MISC(ins, 0);
8906 val = RHS(ins, 0);
8907 get_occurance(ins->occurance);
8908 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
8909 ins->occurance);
8910
8911 if ((tuple->lhs != lval->lhs) &&
8912 (!triple_is_def(state, lval) || tuple->lhs != 1))
8913 {
8914 internal_error(state, ins, "lhs size inconsistency?");
8915 }
8916 for(i = 0; i < tuple->lhs; i++) {
8917 struct triple *piece, *write, *pval, *bitref;
8918 if ((i != 0) || !triple_is_def(state, lval)) {
8919 piece = LHS(lval, i);
8920 } else {
8921 piece = lval;
8922 }
8923 if ((i == 0) && (tuple->lhs == 1) && (val->lhs == 0)) {
8924 pval = val;
8925 }
8926 else {
8927 if (i > val->lhs) {
8928 internal_error(state, ins, "lhs size inconsistency?");
8929 }
8930 pval = LHS(val, i);
8931 }
8932
8933 /* See if the piece is really a bitref */
8934 bitref = 0;
8935 if (piece->op == OP_BITREF) {
8936 struct triple *read, *deposit;
8937 bitref = piece;
8938 piece = RHS(bitref, 0);
8939
8940 /* Read the destination register */
8941 get_occurance(tuple->occurance);
8942 read = alloc_triple(state, OP_READ, piece->type, -1, -1,
8943 tuple->occurance);
8944 RHS(read, 0) = piece;
8945
8946 /* Deposit the new bitfield value */
8947 get_occurance(tuple->occurance);
8948 deposit = alloc_triple(state, OP_DEPOSIT, piece->type, -1, -1,
8949 tuple->occurance);
8950 RHS(deposit, 0) = read;
8951 RHS(deposit, 1) = pval;
8952 deposit->u.bitfield.size = bitref->u.bitfield.size;
8953 deposit->u.bitfield.offset = bitref->u.bitfield.offset;
8954
8955 /* Now write the newly generated value */
8956 pval = deposit;
8957 }
8958
8959 get_occurance(tuple->occurance);
8960 write = alloc_triple(state, OP_WRITE, piece->type, -1, -1,
8961 tuple->occurance);
8962 MISC(write, 0) = piece;
8963 RHS(write, 0) = pval;
8964 LHS(tuple, i) = write;
8965 }
8966 return decompose_with_tuple(state, ins, tuple);
8967}
8968
8969struct decompose_load_info {
8970 struct occurance *occurance;
8971 struct triple *lval;
8972 struct triple *tuple;
8973};
8974static void decompose_load_cb(struct compile_state *state,
8975 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
8976{
8977 struct decompose_load_info *info = arg;
8978 struct triple *load;
8979
8980 if (reg_offset > info->tuple->lhs) {
8981 internal_error(state, info->tuple, "lhs to small?");
8982 }
8983 get_occurance(info->occurance);
8984 load = alloc_triple(state, OP_LOAD, type, -1, -1, info->occurance);
8985 RHS(load, 0) = mk_addr_expr(state, info->lval, mem_offset);
8986 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = load;
8987}
8988
8989static struct triple *decompose_load(struct compile_state *state,
8990 struct triple *ins)
8991{
8992 struct triple *tuple;
8993 struct decompose_load_info info;
8994
8995 if (!is_compound_type(ins->type)) {
8996 return ins->next;
8997 }
8998 get_occurance(ins->occurance);
8999 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9000 ins->occurance);
9001
9002 info.occurance = ins->occurance;
9003 info.lval = RHS(ins, 0);
9004 info.tuple = tuple;
9005 walk_type_fields(state, ins->type, 0, 0, decompose_load_cb, &info);
9006
9007 return decompose_with_tuple(state, ins, tuple);
9008}
9009
9010
9011struct decompose_store_info {
9012 struct occurance *occurance;
9013 struct triple *lval;
9014 struct triple *val;
9015 struct triple *tuple;
9016};
9017static void decompose_store_cb(struct compile_state *state,
9018 struct type *type, size_t reg_offset, size_t mem_offset, void *arg)
9019{
9020 struct decompose_store_info *info = arg;
9021 struct triple *store;
9022
9023 if (reg_offset > info->tuple->lhs) {
9024 internal_error(state, info->tuple, "lhs to small?");
9025 }
9026 get_occurance(info->occurance);
9027 store = alloc_triple(state, OP_STORE, type, -1, -1, info->occurance);
9028 RHS(store, 0) = mk_addr_expr(state, info->lval, mem_offset);
9029 RHS(store, 1) = LHS(info->val, reg_offset);
9030 LHS(info->tuple, reg_offset/REG_SIZEOF_REG) = store;
9031}
9032
9033static struct triple *decompose_store(struct compile_state *state,
9034 struct triple *ins)
9035{
9036 struct triple *tuple;
9037 struct decompose_store_info info;
9038
9039 if (!is_compound_type(ins->type)) {
9040 return ins->next;
9041 }
9042 get_occurance(ins->occurance);
9043 tuple = alloc_triple(state, OP_TUPLE, ins->type, -1, -1,
9044 ins->occurance);
9045
9046 info.occurance = ins->occurance;
9047 info.lval = RHS(ins, 0);
9048 info.val = RHS(ins, 1);
9049 info.tuple = tuple;
9050 walk_type_fields(state, ins->type, 0, 0, decompose_store_cb, &info);
9051
9052 return decompose_with_tuple(state, ins, tuple);
9053}
9054
9055static struct triple *decompose_dot(struct compile_state *state,
9056 struct triple *ins)
9057{
9058 struct triple *tuple, *lval;
9059 struct type *type;
9060 size_t reg_offset;
9061 int i, idx;
9062
9063 lval = MISC(ins, 0);
9064 reg_offset = field_reg_offset(state, lval->type, ins->u.field);
9065 idx = reg_offset/REG_SIZEOF_REG;
9066 type = field_type(state, lval->type, ins->u.field);
9067#if DEBUG_DECOMPOSE_HIRES
9068 {
9069 FILE *fp = state->dbgout;
9070 fprintf(fp, "field type: ");
9071 name_of(fp, type);
9072 fprintf(fp, "\n");
9073 }
9074#endif
9075
9076 get_occurance(ins->occurance);
9077 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9078 ins->occurance);
9079
9080 if (((ins->type->type & TYPE_MASK) == TYPE_BITFIELD) &&
9081 (tuple->lhs != 1))
9082 {
9083 internal_error(state, ins, "multi register bitfield?");
9084 }
9085
9086 for(i = 0; i < tuple->lhs; i++, idx++) {
9087 struct triple *piece;
9088 if (!triple_is_def(state, lval)) {
9089 if (idx > lval->lhs) {
9090 internal_error(state, ins, "inconsistent lhs count");
9091 }
9092 piece = LHS(lval, idx);
9093 } else {
9094 if (idx != 0) {
9095 internal_error(state, ins, "bad reg_offset into def");
9096 }
9097 if (i != 0) {
9098 internal_error(state, ins, "bad reg count from def");
9099 }
9100 piece = lval;
9101 }
9102
9103 /* Remember the offset of the bitfield */
9104 if ((type->type & TYPE_MASK) == TYPE_BITFIELD) {
9105 get_occurance(ins->occurance);
9106 piece = build_triple(state, OP_BITREF, type, piece, 0,
9107 ins->occurance);
9108 piece->u.bitfield.size = size_of(state, type);
9109 piece->u.bitfield.offset = reg_offset % REG_SIZEOF_REG;
9110 }
9111 else if ((reg_offset % REG_SIZEOF_REG) != 0) {
9112 internal_error(state, ins,
9113 "request for a nonbitfield sub register?");
9114 }
9115
9116 LHS(tuple, i) = piece;
9117 }
9118
9119 return decompose_with_tuple(state, ins, tuple);
9120}
9121
9122static struct triple *decompose_index(struct compile_state *state,
9123 struct triple *ins)
9124{
9125 struct triple *tuple, *lval;
9126 struct type *type;
9127 int i, idx;
9128
9129 lval = MISC(ins, 0);
9130 idx = index_reg_offset(state, lval->type, ins->u.cval)/REG_SIZEOF_REG;
9131 type = index_type(state, lval->type, ins->u.cval);
9132#if DEBUG_DECOMPOSE_HIRES
9133{
9134 FILE *fp = state->dbgout;
9135 fprintf(fp, "index type: ");
9136 name_of(fp, type);
9137 fprintf(fp, "\n");
9138}
9139#endif
9140
9141 get_occurance(ins->occurance);
9142 tuple = alloc_triple(state, OP_TUPLE, type, -1, -1,
9143 ins->occurance);
9144
9145 for(i = 0; i < tuple->lhs; i++, idx++) {
9146 struct triple *piece;
9147 if (!triple_is_def(state, lval)) {
9148 if (idx > lval->lhs) {
9149 internal_error(state, ins, "inconsistent lhs count");
9150 }
9151 piece = LHS(lval, idx);
9152 } else {
9153 if (idx != 0) {
9154 internal_error(state, ins, "bad reg_offset into def");
9155 }
9156 if (i != 0) {
9157 internal_error(state, ins, "bad reg count from def");
9158 }
9159 piece = lval;
9160 }
9161 LHS(tuple, i) = piece;
9162 }
9163
9164 return decompose_with_tuple(state, ins, tuple);
9165}
9166
9167static void decompose_compound_types(struct compile_state *state)
9168{
9169 struct triple *ins, *next, *first;
9170 FILE *fp;
9171 fp = state->dbgout;
Eric Biederman83b991a2003-10-11 06:20:25 +00009172 first = state->first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009173 ins = first;
Eric Biederman90089602004-05-28 14:11:54 +00009174
9175 /* Pass one expand compound values into pseudo registers.
9176 */
9177 next = first;
9178 do {
9179 ins = next;
9180 next = ins->next;
9181 switch(ins->op) {
9182 case OP_UNKNOWNVAL:
9183 next = decompose_unknownval(state, ins);
9184 break;
9185
9186 case OP_READ:
9187 next = decompose_read(state, ins);
9188 break;
9189
9190 case OP_WRITE:
9191 next = decompose_write(state, ins);
9192 break;
9193
9194
9195 /* Be very careful with the load/store logic. These
9196 * operations must convert from the in register layout
9197 * to the in memory layout, which is nontrivial.
9198 */
9199 case OP_LOAD:
9200 next = decompose_load(state, ins);
9201 break;
9202 case OP_STORE:
9203 next = decompose_store(state, ins);
9204 break;
9205
9206 case OP_DOT:
9207 next = decompose_dot(state, ins);
9208 break;
9209 case OP_INDEX:
9210 next = decompose_index(state, ins);
9211 break;
9212
9213 }
9214#if DEBUG_DECOMPOSE_HIRES
9215 fprintf(fp, "decompose next: %p \n", next);
9216 fflush(fp);
9217 fprintf(fp, "next->op: %d %s\n",
9218 next->op, tops(next->op));
9219 /* High resolution debugging mode */
9220 print_triples(state);
9221#endif
9222 } while (next != first);
9223
9224 /* Pass two remove the tuples.
Eric Biederman0babc1c2003-05-09 02:39:00 +00009225 */
9226 ins = first;
9227 do {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009228 next = ins->next;
Eric Biederman90089602004-05-28 14:11:54 +00009229 if (ins->op == OP_TUPLE) {
9230 if (ins->use) {
9231 internal_error(state, ins, "tuple used");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009232 }
Eric Biederman90089602004-05-28 14:11:54 +00009233 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009234 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009235 }
Eric Biederman90089602004-05-28 14:11:54 +00009236 }
9237 ins = next;
9238 } while(ins != first);
9239 ins = first;
9240 do {
9241 next = ins->next;
9242 if (ins->op == OP_BITREF) {
9243 if (ins->use) {
9244 internal_error(state, ins, "bitref used");
9245 }
9246 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009247 release_triple(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009248 }
9249 }
9250 ins = next;
9251 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +00009252
Eric Biederman0babc1c2003-05-09 02:39:00 +00009253 /* Pass three verify the state and set ->id to 0.
9254 */
Eric Biederman90089602004-05-28 14:11:54 +00009255 next = first;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009256 do {
Eric Biederman90089602004-05-28 14:11:54 +00009257 ins = next;
9258 next = ins->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009259 ins->id &= ~TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +00009260 if (triple_stores_block(state, ins)) {
9261 ins->u.block = 0;
9262 }
9263 if (triple_is_def(state, ins)) {
9264 if (reg_size_of(state, ins->type) > REG_SIZEOF_REG) {
9265 internal_error(state, ins, "multi register value remains?");
9266 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009267 }
9268 if (ins->op == OP_DOT) {
Eric Biederman00443072003-06-24 12:34:45 +00009269 internal_error(state, ins, "OP_DOT remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009270 }
Eric Biederman90089602004-05-28 14:11:54 +00009271 if (ins->op == OP_INDEX) {
9272 internal_error(state, ins, "OP_INDEX remains?");
Eric Biederman0babc1c2003-05-09 02:39:00 +00009273 }
Eric Biederman90089602004-05-28 14:11:54 +00009274 if (ins->op == OP_BITREF) {
9275 internal_error(state, ins, "OP_BITREF remains?");
9276 }
9277 if (ins->op == OP_TUPLE) {
9278 internal_error(state, ins, "OP_TUPLE remains?");
9279 }
9280 } while(next != first);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009281}
9282
Eric Biedermanb138ac82003-04-22 18:44:01 +00009283/* For those operations that cannot be simplified */
9284static void simplify_noop(struct compile_state *state, struct triple *ins)
9285{
9286 return;
9287}
9288
9289static void simplify_smul(struct compile_state *state, struct triple *ins)
9290{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009291 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009292 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009293 tmp = RHS(ins, 0);
9294 RHS(ins, 0) = RHS(ins, 1);
9295 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009296 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009297 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009298 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009299 left = read_sconst(state, ins, RHS(ins, 0));
9300 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009301 mkconst(state, ins, left * right);
9302 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009303 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009304 mkconst(state, ins, 0);
9305 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009306 else if (is_one(RHS(ins, 1))) {
9307 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009308 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009309 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009310 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009311 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009312 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009313 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009314 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009315 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009316 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009317 }
9318}
9319
9320static void simplify_umul(struct compile_state *state, struct triple *ins)
9321{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009322 if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009323 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009324 tmp = RHS(ins, 0);
9325 RHS(ins, 0) = RHS(ins, 1);
9326 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009327 }
Eric Biederman90089602004-05-28 14:11:54 +00009328 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009329 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009330 left = read_const(state, ins, RHS(ins, 0));
9331 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009332 mkconst(state, ins, left * right);
9333 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009334 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009335 mkconst(state, ins, 0);
9336 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009337 else if (is_one(RHS(ins, 1))) {
9338 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009339 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009340 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009341 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009342 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009343 ins->op = OP_SL;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009344 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009345 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009346 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009347 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009348 }
9349}
9350
9351static void simplify_sdiv(struct compile_state *state, struct triple *ins)
9352{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009353 if (is_const(RHS(ins, 0)) && is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009354 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009355 left = read_sconst(state, ins, RHS(ins, 0));
9356 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009357 mkconst(state, ins, left / right);
9358 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009359 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009360 mkconst(state, ins, 0);
9361 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009362 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009363 error(state, ins, "division by zero");
9364 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009365 else if (is_one(RHS(ins, 1))) {
9366 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009367 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009368 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009369 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009370 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009371 ins->op = OP_SSR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009372 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009373 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009374 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009375 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009376 }
9377}
9378
9379static void simplify_udiv(struct compile_state *state, struct triple *ins)
9380{
Eric Biederman90089602004-05-28 14:11:54 +00009381 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009382 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009383 left = read_const(state, ins, RHS(ins, 0));
9384 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009385 mkconst(state, ins, left / right);
9386 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009387 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009388 mkconst(state, ins, 0);
9389 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009390 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009391 error(state, ins, "division by zero");
9392 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009393 else if (is_one(RHS(ins, 1))) {
9394 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009395 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009396 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009397 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009398 val = int_const(state, ins->type, tlog2(RHS(ins, 1)));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009399 ins->op = OP_USR;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009400 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009401 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009402 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009403 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009404 }
9405}
9406
9407static void simplify_smod(struct compile_state *state, struct triple *ins)
9408{
Eric Biederman90089602004-05-28 14:11:54 +00009409 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009410 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009411 left = read_const(state, ins, RHS(ins, 0));
9412 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009413 mkconst(state, ins, left % right);
9414 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009415 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009416 mkconst(state, ins, 0);
9417 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009418 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009419 error(state, ins, "division by zero");
9420 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009421 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009422 mkconst(state, ins, 0);
9423 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009424 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009425 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009426 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009427 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009428 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009429 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009430 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009431 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009432 }
9433}
Eric Biederman83b991a2003-10-11 06:20:25 +00009434
Eric Biedermanb138ac82003-04-22 18:44:01 +00009435static void simplify_umod(struct compile_state *state, struct triple *ins)
9436{
Eric Biederman90089602004-05-28 14:11:54 +00009437 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009438 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009439 left = read_const(state, ins, RHS(ins, 0));
9440 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009441 mkconst(state, ins, left % right);
9442 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009443 else if (is_zero(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009444 mkconst(state, ins, 0);
9445 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009446 else if (is_zero(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009447 error(state, ins, "division by zero");
9448 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009449 else if (is_one(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009450 mkconst(state, ins, 0);
9451 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009452 else if (is_pow2(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009453 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009454 val = int_const(state, ins->type, RHS(ins, 1)->u.cval - 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009455 ins->op = OP_AND;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009456 insert_triple(state, state->global_pool, val);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009457 unuse_triple(RHS(ins, 1), ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009458 use_triple(val, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009459 RHS(ins, 1) = val;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009460 }
9461}
9462
9463static void simplify_add(struct compile_state *state, struct triple *ins)
9464{
9465 /* start with the pointer on the left */
Eric Biederman0babc1c2003-05-09 02:39:00 +00009466 if (is_pointer(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009467 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009468 tmp = RHS(ins, 0);
9469 RHS(ins, 0) = RHS(ins, 1);
9470 RHS(ins, 1) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009471 }
Eric Biederman90089602004-05-28 14:11:54 +00009472 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009473 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009474 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009475 left = read_const(state, ins, RHS(ins, 0));
9476 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009477 mkconst(state, ins, left + right);
9478 }
Eric Biederman530b5192003-07-01 10:05:30 +00009479 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009480 struct triple *sdecl;
9481 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009482 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009483 left = RHS(ins, 0)->u.cval;
9484 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009485 mkaddr_const(state, ins, sdecl, left + right);
9486 }
Eric Biederman530b5192003-07-01 10:05:30 +00009487 else {
9488 internal_warning(state, ins, "Optimize me!");
9489 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009490 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009491 else if (is_const(RHS(ins, 0)) && !is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009492 struct triple *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +00009493 tmp = RHS(ins, 1);
9494 RHS(ins, 1) = RHS(ins, 0);
9495 RHS(ins, 0) = tmp;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009496 }
9497}
9498
9499static void simplify_sub(struct compile_state *state, struct triple *ins)
9500{
Eric Biederman90089602004-05-28 14:11:54 +00009501 if (is_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +00009502 if (RHS(ins, 0)->op == OP_INTCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009503 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009504 left = read_const(state, ins, RHS(ins, 0));
9505 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009506 mkconst(state, ins, left - right);
9507 }
Eric Biederman530b5192003-07-01 10:05:30 +00009508 else if (RHS(ins, 0)->op == OP_ADDRCONST) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009509 struct triple *sdecl;
9510 ulong_t left, right;
Eric Biederman6aa31cc2003-06-10 21:22:07 +00009511 sdecl = MISC(RHS(ins, 0), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +00009512 left = RHS(ins, 0)->u.cval;
9513 right = RHS(ins, 1)->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009514 mkaddr_const(state, ins, sdecl, left - right);
9515 }
Eric Biederman530b5192003-07-01 10:05:30 +00009516 else {
9517 internal_warning(state, ins, "Optimize me!");
9518 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009519 }
9520}
9521
9522static void simplify_sl(struct compile_state *state, struct triple *ins)
9523{
Eric Biederman90089602004-05-28 14:11:54 +00009524 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009525 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009526 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009527 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009528 warning(state, ins, "left shift count >= width of type");
9529 }
9530 }
Eric Biederman90089602004-05-28 14:11:54 +00009531 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009532 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009533 left = read_const(state, ins, RHS(ins, 0));
9534 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009535 mkconst(state, ins, left << right);
9536 }
9537}
9538
9539static void simplify_usr(struct compile_state *state, struct triple *ins)
9540{
Eric Biederman90089602004-05-28 14:11:54 +00009541 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009542 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009543 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009544 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009545 warning(state, ins, "right shift count >= width of type");
9546 }
9547 }
Eric Biederman90089602004-05-28 14:11:54 +00009548 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009549 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009550 left = read_const(state, ins, RHS(ins, 0));
9551 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009552 mkconst(state, ins, left >> right);
9553 }
9554}
9555
9556static void simplify_ssr(struct compile_state *state, struct triple *ins)
9557{
Eric Biederman90089602004-05-28 14:11:54 +00009558 if (is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009559 ulong_t right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009560 right = read_const(state, ins, RHS(ins, 1));
Eric Biederman90089602004-05-28 14:11:54 +00009561 if (right >= (size_of(state, ins->type))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009562 warning(state, ins, "right shift count >= width of type");
9563 }
9564 }
Eric Biederman90089602004-05-28 14:11:54 +00009565 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009566 long_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009567 left = read_sconst(state, ins, RHS(ins, 0));
9568 right = read_sconst(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009569 mkconst(state, ins, left >> right);
9570 }
9571}
9572
9573static void simplify_and(struct compile_state *state, struct triple *ins)
9574{
Eric Biederman90089602004-05-28 14:11:54 +00009575 struct triple *left, *right;
9576 left = RHS(ins, 0);
9577 right = RHS(ins, 1);
9578
9579 if (is_simple_const(left) && is_simple_const(right)) {
9580 ulong_t lval, rval;
9581 lval = read_const(state, ins, left);
9582 rval = read_const(state, ins, right);
9583 mkconst(state, ins, lval & rval);
9584 }
9585 else if (is_zero(right) || is_zero(left)) {
9586 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009587 }
9588}
9589
9590static void simplify_or(struct compile_state *state, struct triple *ins)
9591{
Eric Biederman90089602004-05-28 14:11:54 +00009592 struct triple *left, *right;
9593 left = RHS(ins, 0);
9594 right = RHS(ins, 1);
9595
9596 if (is_simple_const(left) && is_simple_const(right)) {
9597 ulong_t lval, rval;
9598 lval = read_const(state, ins, left);
9599 rval = read_const(state, ins, right);
9600 mkconst(state, ins, lval | rval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009601 }
Eric Biederman90089602004-05-28 14:11:54 +00009602#if 0 /* I need to handle type mismatches here... */
9603 else if (is_zero(right)) {
9604 mkcopy(state, ins, left);
9605 }
9606 else if (is_zero(left)) {
9607 mkcopy(state, ins, right);
9608 }
9609#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +00009610}
9611
9612static void simplify_xor(struct compile_state *state, struct triple *ins)
9613{
Eric Biederman90089602004-05-28 14:11:54 +00009614 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009615 ulong_t left, right;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009616 left = read_const(state, ins, RHS(ins, 0));
9617 right = read_const(state, ins, RHS(ins, 1));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009618 mkconst(state, ins, left ^ right);
9619 }
9620}
9621
9622static void simplify_pos(struct compile_state *state, struct triple *ins)
9623{
Eric Biederman0babc1c2003-05-09 02:39:00 +00009624 if (is_const(RHS(ins, 0))) {
9625 mkconst(state, ins, RHS(ins, 0)->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009626 }
9627 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +00009628 mkcopy(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009629 }
9630}
9631
9632static void simplify_neg(struct compile_state *state, struct triple *ins)
9633{
Eric Biederman90089602004-05-28 14:11:54 +00009634 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009635 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009636 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009637 mkconst(state, ins, -left);
9638 }
Eric Biederman0babc1c2003-05-09 02:39:00 +00009639 else if (RHS(ins, 0)->op == OP_NEG) {
9640 mkcopy(state, ins, RHS(RHS(ins, 0), 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009641 }
9642}
9643
9644static void simplify_invert(struct compile_state *state, struct triple *ins)
9645{
Eric Biederman90089602004-05-28 14:11:54 +00009646 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009647 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +00009648 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009649 mkconst(state, ins, ~left);
9650 }
9651}
9652
9653static void simplify_eq(struct compile_state *state, struct triple *ins)
9654{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009655 struct triple *left, *right;
9656 left = RHS(ins, 0);
9657 right = RHS(ins, 1);
9658
9659 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009660 int val;
9661 val = const_eq(state, ins, left, right);
9662 if (val >= 0) {
9663 mkconst(state, ins, val == 1);
9664 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009665 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009666 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009667 mkconst(state, ins, 1);
9668 }
9669}
9670
9671static void simplify_noteq(struct compile_state *state, struct triple *ins)
9672{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009673 struct triple *left, *right;
9674 left = RHS(ins, 0);
9675 right = RHS(ins, 1);
9676
9677 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009678 int val;
9679 val = const_eq(state, ins, left, right);
9680 if (val >= 0) {
9681 mkconst(state, ins, val != 1);
9682 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009683 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009684 if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009685 mkconst(state, ins, 0);
9686 }
9687}
9688
9689static void simplify_sless(struct compile_state *state, struct triple *ins)
9690{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009691 struct triple *left, *right;
9692 left = RHS(ins, 0);
9693 right = RHS(ins, 1);
9694
9695 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009696 int val;
9697 val = const_scmp(state, ins, left, right);
9698 if ((val >= -1) && (val <= 1)) {
9699 mkconst(state, ins, val < 0);
9700 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009701 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009702 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009703 mkconst(state, ins, 0);
9704 }
9705}
9706
9707static void simplify_uless(struct compile_state *state, struct triple *ins)
9708{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009709 struct triple *left, *right;
9710 left = RHS(ins, 0);
9711 right = RHS(ins, 1);
9712
9713 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009714 int val;
9715 val = const_ucmp(state, ins, left, right);
9716 if ((val >= -1) && (val <= 1)) {
9717 mkconst(state, ins, val < 0);
9718 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009719 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009720 else if (is_zero(right)) {
9721 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009722 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009723 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009724 mkconst(state, ins, 0);
9725 }
9726}
9727
9728static void simplify_smore(struct compile_state *state, struct triple *ins)
9729{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009730 struct triple *left, *right;
9731 left = RHS(ins, 0);
9732 right = RHS(ins, 1);
9733
9734 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009735 int val;
9736 val = const_scmp(state, ins, left, right);
9737 if ((val >= -1) && (val <= 1)) {
9738 mkconst(state, ins, val > 0);
9739 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009740 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009741 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009742 mkconst(state, ins, 0);
9743 }
9744}
9745
9746static void simplify_umore(struct compile_state *state, struct triple *ins)
9747{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009748 struct triple *left, *right;
9749 left = RHS(ins, 0);
9750 right = RHS(ins, 1);
9751
9752 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009753 int val;
9754 val = const_ucmp(state, ins, left, right);
9755 if ((val >= -1) && (val <= 1)) {
9756 mkconst(state, ins, val > 0);
9757 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009758 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009759 else if (is_zero(left)) {
9760 mkconst(state, ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009761 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009762 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009763 mkconst(state, ins, 0);
9764 }
9765}
9766
9767
9768static void simplify_slesseq(struct compile_state *state, struct triple *ins)
9769{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009770 struct triple *left, *right;
9771 left = RHS(ins, 0);
9772 right = RHS(ins, 1);
9773
9774 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009775 int val;
9776 val = const_scmp(state, ins, left, right);
9777 if ((val >= -1) && (val <= 1)) {
9778 mkconst(state, ins, val <= 0);
9779 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009780 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009781 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009782 mkconst(state, ins, 1);
9783 }
9784}
9785
9786static void simplify_ulesseq(struct compile_state *state, struct triple *ins)
9787{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009788 struct triple *left, *right;
9789 left = RHS(ins, 0);
9790 right = RHS(ins, 1);
9791
9792 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009793 int val;
9794 val = const_ucmp(state, ins, left, right);
9795 if ((val >= -1) && (val <= 1)) {
9796 mkconst(state, ins, val <= 0);
9797 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009798 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009799 else if (is_zero(left)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009800 mkconst(state, ins, 1);
9801 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009802 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009803 mkconst(state, ins, 1);
9804 }
9805}
9806
9807static void simplify_smoreeq(struct compile_state *state, struct triple *ins)
9808{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009809 struct triple *left, *right;
9810 left = RHS(ins, 0);
9811 right = RHS(ins, 1);
9812
9813 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009814 int val;
9815 val = const_scmp(state, ins, left, right);
9816 if ((val >= -1) && (val <= 1)) {
9817 mkconst(state, ins, val >= 0);
9818 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009819 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009820 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009821 mkconst(state, ins, 1);
9822 }
9823}
9824
9825static void simplify_umoreeq(struct compile_state *state, struct triple *ins)
9826{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009827 struct triple *left, *right;
9828 left = RHS(ins, 0);
9829 right = RHS(ins, 1);
9830
9831 if (is_const(left) && is_const(right)) {
Eric Biederman90089602004-05-28 14:11:54 +00009832 int val;
9833 val = const_ucmp(state, ins, left, right);
9834 if ((val >= -1) && (val <= 1)) {
9835 mkconst(state, ins, val >= 0);
9836 }
Eric Biedermanb138ac82003-04-22 18:44:01 +00009837 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009838 else if (is_zero(right)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009839 mkconst(state, ins, 1);
9840 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009841 else if (left == right) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009842 mkconst(state, ins, 1);
9843 }
9844}
9845
9846static void simplify_lfalse(struct compile_state *state, struct triple *ins)
9847{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009848 struct triple *rhs;
9849 rhs = RHS(ins, 0);
9850
9851 if (is_const(rhs)) {
9852 mkconst(state, ins, !const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009853 }
9854 /* Otherwise if I am the only user... */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009855 else if ((rhs->use) &&
9856 (rhs->use->member == ins) && (rhs->use->next == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009857 int need_copy = 1;
9858 /* Invert a boolean operation */
Eric Biederman5ade04a2003-10-22 04:03:46 +00009859 switch(rhs->op) {
9860 case OP_LTRUE: rhs->op = OP_LFALSE; break;
9861 case OP_LFALSE: rhs->op = OP_LTRUE; break;
9862 case OP_EQ: rhs->op = OP_NOTEQ; break;
9863 case OP_NOTEQ: rhs->op = OP_EQ; break;
9864 case OP_SLESS: rhs->op = OP_SMOREEQ; break;
9865 case OP_ULESS: rhs->op = OP_UMOREEQ; break;
9866 case OP_SMORE: rhs->op = OP_SLESSEQ; break;
9867 case OP_UMORE: rhs->op = OP_ULESSEQ; break;
9868 case OP_SLESSEQ: rhs->op = OP_SMORE; break;
9869 case OP_ULESSEQ: rhs->op = OP_UMORE; break;
9870 case OP_SMOREEQ: rhs->op = OP_SLESS; break;
9871 case OP_UMOREEQ: rhs->op = OP_ULESS; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +00009872 default:
9873 need_copy = 0;
9874 break;
9875 }
9876 if (need_copy) {
Eric Biederman5ade04a2003-10-22 04:03:46 +00009877 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009878 }
9879 }
9880}
9881
9882static void simplify_ltrue (struct compile_state *state, struct triple *ins)
9883{
Eric Biederman5ade04a2003-10-22 04:03:46 +00009884 struct triple *rhs;
9885 rhs = RHS(ins, 0);
9886
9887 if (is_const(rhs)) {
9888 mkconst(state, ins, const_ltrue(state, ins, rhs));
Eric Biedermanb138ac82003-04-22 18:44:01 +00009889 }
Eric Biederman5ade04a2003-10-22 04:03:46 +00009890 else switch(rhs->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +00009891 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
9892 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
9893 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +00009894 mkcopy(state, ins, rhs);
Eric Biedermanb138ac82003-04-22 18:44:01 +00009895 }
9896
9897}
9898
Eric Biederman90089602004-05-28 14:11:54 +00009899static void simplify_load(struct compile_state *state, struct triple *ins)
9900{
9901 struct triple *addr, *sdecl, *blob;
9902
9903 /* If I am doing a load with a constant pointer from a constant
9904 * table get the value.
9905 */
9906 addr = RHS(ins, 0);
9907 if ((addr->op == OP_ADDRCONST) && (sdecl = MISC(addr, 0)) &&
9908 (sdecl->op == OP_SDECL) && (blob = MISC(sdecl, 0)) &&
9909 (blob->op == OP_BLOBCONST)) {
9910 unsigned char buffer[SIZEOF_WORD];
9911 size_t reg_size, mem_size;
Eric Biederman7dea9552004-06-29 05:38:37 +00009912 const char *src, *end;
Eric Biederman90089602004-05-28 14:11:54 +00009913 ulong_t val;
9914 reg_size = reg_size_of(state, ins->type);
9915 if (reg_size > REG_SIZEOF_REG) {
9916 internal_error(state, ins, "load size greater than register");
9917 }
9918 mem_size = size_of(state, ins->type);
Eric Biederman7dea9552004-06-29 05:38:37 +00009919 end = blob->u.blob;
9920 end += bits_to_bytes(size_of(state, sdecl->type));
Eric Biederman90089602004-05-28 14:11:54 +00009921 src = blob->u.blob;
9922 src += addr->u.cval;
9923
Eric Biederman7dea9552004-06-29 05:38:37 +00009924 if (src > end) {
Jason Schildt27b85112005-08-10 14:31:52 +00009925 error(state, ins, "Load address out of bounds");
Eric Biederman7dea9552004-06-29 05:38:37 +00009926 }
9927
Eric Biederman90089602004-05-28 14:11:54 +00009928 memset(buffer, 0, sizeof(buffer));
9929 memcpy(buffer, src, bits_to_bytes(mem_size));
9930
9931 switch(mem_size) {
9932 case SIZEOF_I8: val = *((uint8_t *) buffer); break;
9933 case SIZEOF_I16: val = *((uint16_t *)buffer); break;
9934 case SIZEOF_I32: val = *((uint32_t *)buffer); break;
9935 case SIZEOF_I64: val = *((uint64_t *)buffer); break;
9936 default:
9937 internal_error(state, ins, "mem_size: %d not handled",
9938 mem_size);
9939 val = 0;
9940 break;
9941 }
9942 mkconst(state, ins, val);
9943 }
9944}
9945
9946static void simplify_uextract(struct compile_state *state, struct triple *ins)
9947{
9948 if (is_simple_const(RHS(ins, 0))) {
9949 ulong_t val;
9950 ulong_t mask;
9951 val = read_const(state, ins, RHS(ins, 0));
9952 mask = 1;
9953 mask <<= ins->u.bitfield.size;
9954 mask -= 1;
9955 val >>= ins->u.bitfield.offset;
9956 val &= mask;
9957 mkconst(state, ins, val);
9958 }
9959}
9960
9961static void simplify_sextract(struct compile_state *state, struct triple *ins)
9962{
9963 if (is_simple_const(RHS(ins, 0))) {
9964 ulong_t val;
9965 ulong_t mask;
9966 long_t sval;
9967 val = read_const(state, ins, RHS(ins, 0));
9968 mask = 1;
9969 mask <<= ins->u.bitfield.size;
9970 mask -= 1;
9971 val >>= ins->u.bitfield.offset;
9972 val &= mask;
9973 val <<= (SIZEOF_LONG - ins->u.bitfield.size);
9974 sval = val;
9975 sval >>= (SIZEOF_LONG - ins->u.bitfield.size);
9976 mkconst(state, ins, sval);
9977 }
9978}
9979
9980static void simplify_deposit(struct compile_state *state, struct triple *ins)
9981{
9982 if (is_simple_const(RHS(ins, 0)) && is_simple_const(RHS(ins, 1))) {
9983 ulong_t targ, val;
9984 ulong_t mask;
9985 targ = read_const(state, ins, RHS(ins, 0));
9986 val = read_const(state, ins, RHS(ins, 1));
9987 mask = 1;
9988 mask <<= ins->u.bitfield.size;
9989 mask -= 1;
9990 mask <<= ins->u.bitfield.offset;
9991 targ &= ~mask;
9992 val <<= ins->u.bitfield.offset;
9993 val &= mask;
9994 targ |= val;
9995 mkconst(state, ins, targ);
9996 }
9997}
9998
Eric Biedermanb138ac82003-04-22 18:44:01 +00009999static void simplify_copy(struct compile_state *state, struct triple *ins)
10000{
Eric Biederman90089602004-05-28 14:11:54 +000010001 struct triple *right;
10002 right = RHS(ins, 0);
10003 if (is_subset_type(ins->type, right->type)) {
10004 ins->type = right->type;
10005 }
10006 if (equiv_types(ins->type, right->type)) {
10007 ins->op = OP_COPY;/* I don't need to convert if the types match */
10008 } else {
10009 if (ins->op == OP_COPY) {
10010 internal_error(state, ins, "type mismatch on copy");
10011 }
10012 }
10013 if (is_const(right) && (right->op == OP_ADDRCONST) && is_pointer(ins)) {
10014 struct triple *sdecl;
10015 ulong_t offset;
10016 sdecl = MISC(right, 0);
10017 offset = right->u.cval;
10018 mkaddr_const(state, ins, sdecl, offset);
10019 }
10020 else if (is_const(right) && is_write_compatible(state, ins->type, right->type)) {
10021 switch(right->op) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010022 case OP_INTCONST:
10023 {
10024 ulong_t left;
Eric Biederman90089602004-05-28 14:11:54 +000010025 left = read_const(state, ins, right);
10026 /* Ensure I have not overflowed the destination. */
10027 if (size_of(state, right->type) > size_of(state, ins->type)) {
10028 ulong_t mask;
10029 mask = 1;
10030 mask <<= size_of(state, ins->type);
10031 mask -= 1;
10032 left &= mask;
10033 }
10034 /* Ensure I am properly sign extended */
10035 if (size_of(state, right->type) < size_of(state, ins->type) &&
10036 is_signed(right->type)) {
10037 long_t val;
10038 int shift;
10039 shift = SIZEOF_LONG - size_of(state, right->type);
10040 val = left;
10041 val <<= shift;
10042 val >>= shift;
10043 left = val;
10044 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010045 mkconst(state, ins, left);
10046 break;
10047 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010048 default:
10049 internal_error(state, ins, "uknown constant");
10050 break;
10051 }
10052 }
10053}
10054
Eric Biederman83b991a2003-10-11 06:20:25 +000010055static int phi_present(struct block *block)
Eric Biederman530b5192003-07-01 10:05:30 +000010056{
10057 struct triple *ptr;
10058 if (!block) {
10059 return 0;
10060 }
10061 ptr = block->first;
10062 do {
10063 if (ptr->op == OP_PHI) {
10064 return 1;
10065 }
10066 ptr = ptr->next;
10067 } while(ptr != block->last);
10068 return 0;
10069}
10070
Eric Biederman83b991a2003-10-11 06:20:25 +000010071static int phi_dependency(struct block *block)
10072{
10073 /* A block has a phi dependency if a phi function
10074 * depends on that block to exist, and makes a block
10075 * that is otherwise useless unsafe to remove.
10076 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010077 if (block) {
10078 struct block_set *edge;
10079 for(edge = block->edges; edge; edge = edge->next) {
10080 if (phi_present(edge->member)) {
10081 return 1;
10082 }
10083 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010084 }
10085 return 0;
10086}
10087
10088static struct triple *branch_target(struct compile_state *state, struct triple *ins)
10089{
10090 struct triple *targ;
10091 targ = TARG(ins, 0);
10092 /* During scc_transform temporary triples are allocated that
10093 * loop back onto themselves. If I see one don't advance the
10094 * target.
10095 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010096 while(triple_is_structural(state, targ) &&
10097 (targ->next != targ) && (targ->next != state->first)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010098 targ = targ->next;
10099 }
10100 return targ;
10101}
10102
10103
10104static void simplify_branch(struct compile_state *state, struct triple *ins)
10105{
Eric Biederman90089602004-05-28 14:11:54 +000010106 int simplified, loops;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010107 if ((ins->op != OP_BRANCH) && (ins->op != OP_CBRANCH)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010108 internal_error(state, ins, "not branch");
10109 }
10110 if (ins->use != 0) {
10111 internal_error(state, ins, "branch use");
10112 }
10113 /* The challenge here with simplify branch is that I need to
10114 * make modifications to the control flow graph as well
10115 * as to the branch instruction itself. That is handled
10116 * by rebuilding the basic blocks after simplify all is called.
10117 */
10118
10119 /* If we have a branch to an unconditional branch update
10120 * our target. But watch out for dependencies from phi
Eric Biederman90089602004-05-28 14:11:54 +000010121 * functions.
10122 * Also only do this a limited number of times so
10123 * we don't get into an infinite loop.
Eric Biederman83b991a2003-10-11 06:20:25 +000010124 */
Eric Biederman90089602004-05-28 14:11:54 +000010125 loops = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010126 do {
10127 struct triple *targ;
10128 simplified = 0;
10129 targ = branch_target(state, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010130 if ((targ != ins) && (targ->op == OP_BRANCH) &&
10131 !phi_dependency(targ->u.block))
10132 {
10133 unuse_triple(TARG(ins, 0), ins);
10134 TARG(ins, 0) = TARG(targ, 0);
10135 use_triple(TARG(ins, 0), ins);
10136 simplified = 1;
Eric Biederman83b991a2003-10-11 06:20:25 +000010137 }
Eric Biederman90089602004-05-28 14:11:54 +000010138 } while(simplified && (++loops < 20));
Eric Biederman83b991a2003-10-11 06:20:25 +000010139
10140 /* If we have a conditional branch with a constant condition
10141 * make it an unconditional branch.
10142 */
Eric Biederman90089602004-05-28 14:11:54 +000010143 if ((ins->op == OP_CBRANCH) && is_simple_const(RHS(ins, 0))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010144 struct triple *targ;
10145 ulong_t value;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010146 value = read_const(state, ins, RHS(ins, 0));
Eric Biederman83b991a2003-10-11 06:20:25 +000010147 unuse_triple(RHS(ins, 0), ins);
10148 targ = TARG(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010149 ins->rhs = 0;
10150 ins->targ = 1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010151 ins->op = OP_BRANCH;
Eric Biederman83b991a2003-10-11 06:20:25 +000010152 if (value) {
10153 unuse_triple(ins->next, ins);
10154 TARG(ins, 0) = targ;
10155 }
10156 else {
10157 unuse_triple(targ, ins);
10158 TARG(ins, 0) = ins->next;
10159 }
10160 }
Eric Biederman90089602004-05-28 14:11:54 +000010161
10162 /* If we have a branch to the next instruction,
Eric Biederman83b991a2003-10-11 06:20:25 +000010163 * make it a noop.
10164 */
10165 if (TARG(ins, 0) == ins->next) {
Eric Biederman90089602004-05-28 14:11:54 +000010166 unuse_triple(TARG(ins, 0), ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010167 if (ins->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010168 unuse_triple(RHS(ins, 0), ins);
10169 unuse_triple(ins->next, ins);
10170 }
Eric Biederman90089602004-05-28 14:11:54 +000010171 ins->lhs = 0;
10172 ins->rhs = 0;
10173 ins->misc = 0;
10174 ins->targ = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000010175 ins->op = OP_NOOP;
10176 if (ins->use) {
10177 internal_error(state, ins, "noop use != 0");
10178 }
10179 }
10180}
10181
Eric Biederman530b5192003-07-01 10:05:30 +000010182static void simplify_label(struct compile_state *state, struct triple *ins)
10183{
Eric Biederman83b991a2003-10-11 06:20:25 +000010184 /* Ignore volatile labels */
10185 if (!triple_is_pure(state, ins, ins->id)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010186 return;
10187 }
10188 if (ins->use == 0) {
10189 ins->op = OP_NOOP;
10190 }
10191 else if (ins->prev->op == OP_LABEL) {
Eric Biederman530b5192003-07-01 10:05:30 +000010192 /* In general it is not safe to merge one label that
10193 * imediately follows another. The problem is that the empty
10194 * looking block may have phi functions that depend on it.
10195 */
Eric Biederman83b991a2003-10-11 06:20:25 +000010196 if (!phi_dependency(ins->prev->u.block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000010197 struct triple_set *user, *next;
10198 ins->op = OP_NOOP;
10199 for(user = ins->use; user; user = next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010200 struct triple *use, **expr;
Eric Biederman530b5192003-07-01 10:05:30 +000010201 next = user->next;
10202 use = user->member;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010203 expr = triple_targ(state, use, 0);
10204 for(;expr; expr = triple_targ(state, use, expr)) {
10205 if (*expr == ins) {
10206 *expr = ins->prev;
10207 unuse_triple(ins, use);
10208 use_triple(ins->prev, use);
10209 }
10210
Eric Biederman530b5192003-07-01 10:05:30 +000010211 }
10212 }
10213 if (ins->use) {
10214 internal_error(state, ins, "noop use != 0");
10215 }
10216 }
10217 }
10218}
10219
Eric Biedermanb138ac82003-04-22 18:44:01 +000010220static void simplify_phi(struct compile_state *state, struct triple *ins)
10221{
Eric Biederman83b991a2003-10-11 06:20:25 +000010222 struct triple **slot;
10223 struct triple *value;
10224 int zrhs, i;
10225 ulong_t cvalue;
10226 slot = &RHS(ins, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010227 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000010228 if (zrhs == 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010229 return;
10230 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010231 /* See if all of the rhs members of a phi have the same value */
10232 if (slot[0] && is_simple_const(slot[0])) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010233 cvalue = read_const(state, ins, slot[0]);
Eric Biederman83b991a2003-10-11 06:20:25 +000010234 for(i = 1; i < zrhs; i++) {
10235 if ( !slot[i] ||
10236 !is_simple_const(slot[i]) ||
Eric Biederman90089602004-05-28 14:11:54 +000010237 !equiv_types(slot[0]->type, slot[i]->type) ||
Eric Biederman5ade04a2003-10-22 04:03:46 +000010238 (cvalue != read_const(state, ins, slot[i]))) {
Eric Biederman83b991a2003-10-11 06:20:25 +000010239 break;
10240 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010241 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010242 if (i == zrhs) {
10243 mkconst(state, ins, cvalue);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010244 return;
10245 }
10246 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010247
10248 /* See if all of rhs members of a phi are the same */
10249 value = slot[0];
10250 for(i = 1; i < zrhs; i++) {
10251 if (slot[i] != value) {
10252 break;
10253 }
10254 }
10255 if (i == zrhs) {
10256 /* If the phi has a single value just copy it */
Eric Biederman90089602004-05-28 14:11:54 +000010257 if (!is_subset_type(ins->type, value->type)) {
10258 internal_error(state, ins, "bad input type to phi");
10259 }
10260 /* Make the types match */
10261 if (!equiv_types(ins->type, value->type)) {
10262 ins->type = value->type;
10263 }
10264 /* Now make the actual copy */
Eric Biederman83b991a2003-10-11 06:20:25 +000010265 mkcopy(state, ins, value);
10266 return;
10267 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010268}
10269
10270
10271static void simplify_bsf(struct compile_state *state, struct triple *ins)
10272{
Eric Biederman90089602004-05-28 14:11:54 +000010273 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010274 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010275 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010276 mkconst(state, ins, bsf(left));
10277 }
10278}
10279
10280static void simplify_bsr(struct compile_state *state, struct triple *ins)
10281{
Eric Biederman90089602004-05-28 14:11:54 +000010282 if (is_simple_const(RHS(ins, 0))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010283 ulong_t left;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010284 left = read_const(state, ins, RHS(ins, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010285 mkconst(state, ins, bsr(left));
10286 }
10287}
10288
10289
10290typedef void (*simplify_t)(struct compile_state *state, struct triple *ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010291static const struct simplify_table {
10292 simplify_t func;
10293 unsigned long flag;
10294} table_simplify[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000010295#define simplify_sdivt simplify_noop
10296#define simplify_udivt simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010297#define simplify_piece simplify_noop
Eric Biederman83b991a2003-10-11 06:20:25 +000010298
Eric Biederman5ade04a2003-10-22 04:03:46 +000010299[OP_SDIVT ] = { simplify_sdivt, COMPILER_SIMPLIFY_ARITH },
10300[OP_UDIVT ] = { simplify_udivt, COMPILER_SIMPLIFY_ARITH },
10301[OP_SMUL ] = { simplify_smul, COMPILER_SIMPLIFY_ARITH },
10302[OP_UMUL ] = { simplify_umul, COMPILER_SIMPLIFY_ARITH },
10303[OP_SDIV ] = { simplify_sdiv, COMPILER_SIMPLIFY_ARITH },
10304[OP_UDIV ] = { simplify_udiv, COMPILER_SIMPLIFY_ARITH },
10305[OP_SMOD ] = { simplify_smod, COMPILER_SIMPLIFY_ARITH },
10306[OP_UMOD ] = { simplify_umod, COMPILER_SIMPLIFY_ARITH },
10307[OP_ADD ] = { simplify_add, COMPILER_SIMPLIFY_ARITH },
10308[OP_SUB ] = { simplify_sub, COMPILER_SIMPLIFY_ARITH },
10309[OP_SL ] = { simplify_sl, COMPILER_SIMPLIFY_SHIFT },
10310[OP_USR ] = { simplify_usr, COMPILER_SIMPLIFY_SHIFT },
10311[OP_SSR ] = { simplify_ssr, COMPILER_SIMPLIFY_SHIFT },
10312[OP_AND ] = { simplify_and, COMPILER_SIMPLIFY_BITWISE },
10313[OP_XOR ] = { simplify_xor, COMPILER_SIMPLIFY_BITWISE },
10314[OP_OR ] = { simplify_or, COMPILER_SIMPLIFY_BITWISE },
10315[OP_POS ] = { simplify_pos, COMPILER_SIMPLIFY_ARITH },
10316[OP_NEG ] = { simplify_neg, COMPILER_SIMPLIFY_ARITH },
10317[OP_INVERT ] = { simplify_invert, COMPILER_SIMPLIFY_BITWISE },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010318
Eric Biederman5ade04a2003-10-22 04:03:46 +000010319[OP_EQ ] = { simplify_eq, COMPILER_SIMPLIFY_LOGICAL },
10320[OP_NOTEQ ] = { simplify_noteq, COMPILER_SIMPLIFY_LOGICAL },
10321[OP_SLESS ] = { simplify_sless, COMPILER_SIMPLIFY_LOGICAL },
10322[OP_ULESS ] = { simplify_uless, COMPILER_SIMPLIFY_LOGICAL },
10323[OP_SMORE ] = { simplify_smore, COMPILER_SIMPLIFY_LOGICAL },
10324[OP_UMORE ] = { simplify_umore, COMPILER_SIMPLIFY_LOGICAL },
10325[OP_SLESSEQ ] = { simplify_slesseq, COMPILER_SIMPLIFY_LOGICAL },
10326[OP_ULESSEQ ] = { simplify_ulesseq, COMPILER_SIMPLIFY_LOGICAL },
10327[OP_SMOREEQ ] = { simplify_smoreeq, COMPILER_SIMPLIFY_LOGICAL },
10328[OP_UMOREEQ ] = { simplify_umoreeq, COMPILER_SIMPLIFY_LOGICAL },
10329[OP_LFALSE ] = { simplify_lfalse, COMPILER_SIMPLIFY_LOGICAL },
10330[OP_LTRUE ] = { simplify_ltrue, COMPILER_SIMPLIFY_LOGICAL },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010331
Eric Biederman90089602004-05-28 14:11:54 +000010332[OP_LOAD ] = { simplify_load, COMPILER_SIMPLIFY_OP },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010333[OP_STORE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010334
Eric Biederman90089602004-05-28 14:11:54 +000010335[OP_UEXTRACT ] = { simplify_uextract, COMPILER_SIMPLIFY_BITFIELD },
10336[OP_SEXTRACT ] = { simplify_sextract, COMPILER_SIMPLIFY_BITFIELD },
10337[OP_DEPOSIT ] = { simplify_deposit, COMPILER_SIMPLIFY_BITFIELD },
10338
Eric Biederman5ade04a2003-10-22 04:03:46 +000010339[OP_NOOP ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010340
Eric Biederman5ade04a2003-10-22 04:03:46 +000010341[OP_INTCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10342[OP_BLOBCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10343[OP_ADDRCONST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010344[OP_UNKNOWNVAL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010345
Eric Biederman5ade04a2003-10-22 04:03:46 +000010346[OP_WRITE ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10347[OP_READ ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10348[OP_COPY ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman90089602004-05-28 14:11:54 +000010349[OP_CONVERT ] = { simplify_copy, COMPILER_SIMPLIFY_COPY },
Eric Biederman5ade04a2003-10-22 04:03:46 +000010350[OP_PIECE ] = { simplify_piece, COMPILER_SIMPLIFY_OP },
10351[OP_ASM ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman0babc1c2003-05-09 02:39:00 +000010352
Eric Biederman5ade04a2003-10-22 04:03:46 +000010353[OP_DOT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biederman90089602004-05-28 14:11:54 +000010354[OP_INDEX ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010355
Eric Biederman5ade04a2003-10-22 04:03:46 +000010356[OP_LIST ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10357[OP_BRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10358[OP_CBRANCH ] = { simplify_branch, COMPILER_SIMPLIFY_BRANCH },
10359[OP_CALL ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10360[OP_RET ] = { simplify_noop, COMPILER_SIMPLIFY_BRANCH },
10361[OP_LABEL ] = { simplify_label, COMPILER_SIMPLIFY_LABEL },
10362[OP_ADECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10363[OP_SDECL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10364[OP_PHI ] = { simplify_phi, COMPILER_SIMPLIFY_PHI },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010365
Eric Biederman5ade04a2003-10-22 04:03:46 +000010366[OP_INB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10367[OP_INW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10368[OP_INL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10369[OP_OUTB ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10370[OP_OUTW ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10371[OP_OUTL ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10372[OP_BSF ] = { simplify_bsf, COMPILER_SIMPLIFY_OP },
10373[OP_BSR ] = { simplify_bsr, COMPILER_SIMPLIFY_OP },
10374[OP_RDMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10375[OP_WRMSR ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
10376[OP_HLT ] = { simplify_noop, COMPILER_SIMPLIFY_OP },
Eric Biedermanb138ac82003-04-22 18:44:01 +000010377};
10378
Eric Biederman90089602004-05-28 14:11:54 +000010379static inline void debug_simplify(struct compile_state *state,
10380 simplify_t do_simplify, struct triple *ins)
10381{
10382#if DEBUG_SIMPLIFY_HIRES
10383 if (state->functions_joined && (do_simplify != simplify_noop)) {
10384 /* High resolution debugging mode */
10385 fprintf(state->dbgout, "simplifing: ");
10386 display_triple(state->dbgout, ins);
10387 }
10388#endif
10389 do_simplify(state, ins);
10390#if DEBUG_SIMPLIFY_HIRES
10391 if (state->functions_joined && (do_simplify != simplify_noop)) {
10392 /* High resolution debugging mode */
10393 fprintf(state->dbgout, "simplified: ");
10394 display_triple(state->dbgout, ins);
10395 }
10396#endif
10397}
Eric Biedermanb138ac82003-04-22 18:44:01 +000010398static void simplify(struct compile_state *state, struct triple *ins)
10399{
10400 int op;
10401 simplify_t do_simplify;
Eric Biederman90089602004-05-28 14:11:54 +000010402 if (ins == &unknown_triple) {
10403 internal_error(state, ins, "simplifying the unknown triple?");
10404 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010405 do {
10406 op = ins->op;
10407 do_simplify = 0;
10408 if ((op < 0) || (op > sizeof(table_simplify)/sizeof(table_simplify[0]))) {
10409 do_simplify = 0;
10410 }
Eric Biederman90089602004-05-28 14:11:54 +000010411 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010412 do_simplify = table_simplify[op].func;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010413 }
Eric Biederman90089602004-05-28 14:11:54 +000010414 if (do_simplify &&
10415 !(state->compiler->flags & table_simplify[op].flag)) {
10416 do_simplify = simplify_noop;
10417 }
10418 if (do_simplify && (ins->id & TRIPLE_FLAG_VOLATILE)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000010419 do_simplify = simplify_noop;
10420 }
10421
Eric Biedermanb138ac82003-04-22 18:44:01 +000010422 if (!do_simplify) {
Eric Biederman90089602004-05-28 14:11:54 +000010423 internal_error(state, ins, "cannot simplify op: %d %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000010424 op, tops(op));
10425 return;
10426 }
Eric Biederman90089602004-05-28 14:11:54 +000010427 debug_simplify(state, do_simplify, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010428 } while(ins->op != op);
10429}
10430
Eric Biederman5ade04a2003-10-22 04:03:46 +000010431static void rebuild_ssa_form(struct compile_state *state);
10432
Eric Biedermanb138ac82003-04-22 18:44:01 +000010433static void simplify_all(struct compile_state *state)
10434{
10435 struct triple *ins, *first;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010436 if (!(state->compiler->flags & COMPILER_SIMPLIFY)) {
10437 return;
10438 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010439 first = state->first;
10440 ins = first->prev;
10441 do {
10442 simplify(state, ins);
10443 ins = ins->prev;
10444 } while(ins != first->prev);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010445 ins = first;
10446 do {
10447 simplify(state, ins);
10448 ins = ins->next;
Eric Biederman530b5192003-07-01 10:05:30 +000010449 }while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010450 rebuild_ssa_form(state);
10451
Eric Biederman90089602004-05-28 14:11:54 +000010452 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010453}
10454
10455/*
10456 * Builtins....
10457 * ============================
10458 */
10459
Eric Biederman0babc1c2003-05-09 02:39:00 +000010460static void register_builtin_function(struct compile_state *state,
10461 const char *name, int op, struct type *rtype, ...)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010462{
Eric Biederman90089602004-05-28 14:11:54 +000010463 struct type *ftype, *atype, *ctype, *crtype, *param, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000010464 struct triple *def, *arg, *result, *work, *last, *first, *retvar, *ret;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010465 struct hash_entry *ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010466 struct file_state file;
10467 int parameters;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010468 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010469 va_list args;
10470 int i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010471
10472 /* Dummy file state to get debug handling right */
Eric Biedermanb138ac82003-04-22 18:44:01 +000010473 memset(&file, 0, sizeof(file));
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010474 file.basename = "<built-in>";
Eric Biedermanb138ac82003-04-22 18:44:01 +000010475 file.line = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010476 file.report_line = 1;
10477 file.report_name = file.basename;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010478 file.prev = state->file;
10479 state->file = &file;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010480 state->function = name;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010481
10482 /* Find the Parameter count */
10483 valid_op(state, op);
10484 parameters = table_ops[op].rhs;
10485 if (parameters < 0 ) {
10486 internal_error(state, 0, "Invalid builtin parameter count");
10487 }
10488
10489 /* Find the function type */
Eric Biederman5ade04a2003-10-22 04:03:46 +000010490 ftype = new_type(TYPE_FUNCTION | STOR_INLINE | STOR_STATIC, rtype, 0);
Eric Biederman90089602004-05-28 14:11:54 +000010491 ftype->elements = parameters;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010492 next = &ftype->right;
10493 va_start(args, rtype);
10494 for(i = 0; i < parameters; i++) {
10495 atype = va_arg(args, struct type *);
10496 if (!*next) {
10497 *next = atype;
10498 } else {
10499 *next = new_type(TYPE_PRODUCT, *next, atype);
10500 next = &((*next)->right);
10501 }
10502 }
10503 if (!*next) {
10504 *next = &void_type;
10505 }
10506 va_end(args);
10507
Eric Biederman90089602004-05-28 14:11:54 +000010508 /* Get the initial closure type */
10509 ctype = new_type(TYPE_JOIN, &void_type, 0);
10510 ctype->elements = 1;
10511
10512 /* Get the return type */
10513 crtype = new_type(TYPE_TUPLE, new_type(TYPE_PRODUCT, ctype, rtype), 0);
10514 crtype->elements = 2;
10515
Eric Biedermanb138ac82003-04-22 18:44:01 +000010516 /* Generate the needed triples */
10517 def = triple(state, OP_LIST, ftype, 0, 0);
10518 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010519 RHS(def, 0) = first;
Eric Biederman90089602004-05-28 14:11:54 +000010520 result = flatten(state, first, variable(state, crtype));
10521 retvar = flatten(state, first, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010522 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010523
10524 /* Now string them together */
10525 param = ftype->right;
10526 for(i = 0; i < parameters; i++) {
10527 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10528 atype = param->left;
10529 } else {
10530 atype = param;
10531 }
10532 arg = flatten(state, first, variable(state, atype));
10533 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010534 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000010535 work = new_triple(state, op, rtype, -1, parameters);
Eric Biederman90089602004-05-28 14:11:54 +000010536 generate_lhs_pieces(state, work);
10537 for(i = 0; i < parameters; i++) {
10538 RHS(work, i) = read_expr(state, farg(state, def, i));
Eric Biederman0babc1c2003-05-09 02:39:00 +000010539 }
Eric Biederman90089602004-05-28 14:11:54 +000010540 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
10541 work = write_expr(state, deref_index(state, result, 1), work);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010542 }
10543 work = flatten(state, first, work);
10544 last = flatten(state, first, label(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000010545 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010546 name_len = strlen(name);
10547 ident = lookup(state, name, name_len);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010548 ftype->type_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010549 symbol(state, ident, &ident->sym_ident, def, ftype);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010550
Eric Biedermanb138ac82003-04-22 18:44:01 +000010551 state->file = file.prev;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000010552 state->function = 0;
Eric Biederman90089602004-05-28 14:11:54 +000010553 state->main_function = 0;
10554
Eric Biederman5ade04a2003-10-22 04:03:46 +000010555 if (!state->functions) {
10556 state->functions = def;
10557 } else {
10558 insert_triple(state, state->functions, def);
10559 }
10560 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000010561 FILE *fp = state->dbgout;
10562 fprintf(fp, "\n");
10563 loc(fp, state, 0);
10564 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
10565 display_func(state, fp, def);
10566 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010567 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010568}
10569
Eric Biederman0babc1c2003-05-09 02:39:00 +000010570static struct type *partial_struct(struct compile_state *state,
10571 const char *field_name, struct type *type, struct type *rest)
Eric Biedermanb138ac82003-04-22 18:44:01 +000010572{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010573 struct hash_entry *field_ident;
10574 struct type *result;
10575 int field_name_len;
10576
10577 field_name_len = strlen(field_name);
10578 field_ident = lookup(state, field_name, field_name_len);
10579
10580 result = clone_type(0, type);
10581 result->field_ident = field_ident;
10582
10583 if (rest) {
10584 result = new_type(TYPE_PRODUCT, result, rest);
10585 }
10586 return result;
10587}
10588
10589static struct type *register_builtin_type(struct compile_state *state,
10590 const char *name, struct type *type)
10591{
Eric Biedermanb138ac82003-04-22 18:44:01 +000010592 struct hash_entry *ident;
10593 int name_len;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010594
Eric Biedermanb138ac82003-04-22 18:44:01 +000010595 name_len = strlen(name);
10596 ident = lookup(state, name, name_len);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010597
10598 if ((type->type & TYPE_MASK) == TYPE_PRODUCT) {
10599 ulong_t elements = 0;
10600 struct type *field;
10601 type = new_type(TYPE_STRUCT, type, 0);
10602 field = type->left;
10603 while((field->type & TYPE_MASK) == TYPE_PRODUCT) {
10604 elements++;
10605 field = field->right;
10606 }
10607 elements++;
Eric Biederman83b991a2003-10-11 06:20:25 +000010608 symbol(state, ident, &ident->sym_tag, 0, type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010609 type->type_ident = ident;
10610 type->elements = elements;
10611 }
10612 symbol(state, ident, &ident->sym_ident, 0, type);
10613 ident->tok = TOK_TYPE_NAME;
10614 return type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010615}
10616
Eric Biederman0babc1c2003-05-09 02:39:00 +000010617
Eric Biedermanb138ac82003-04-22 18:44:01 +000010618static void register_builtins(struct compile_state *state)
10619{
Eric Biederman530b5192003-07-01 10:05:30 +000010620 struct type *div_type, *ldiv_type;
10621 struct type *udiv_type, *uldiv_type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010622 struct type *msr_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010623
Eric Biederman530b5192003-07-01 10:05:30 +000010624 div_type = register_builtin_type(state, "__builtin_div_t",
10625 partial_struct(state, "quot", &int_type,
10626 partial_struct(state, "rem", &int_type, 0)));
10627 ldiv_type = register_builtin_type(state, "__builtin_ldiv_t",
10628 partial_struct(state, "quot", &long_type,
10629 partial_struct(state, "rem", &long_type, 0)));
10630 udiv_type = register_builtin_type(state, "__builtin_udiv_t",
10631 partial_struct(state, "quot", &uint_type,
10632 partial_struct(state, "rem", &uint_type, 0)));
10633 uldiv_type = register_builtin_type(state, "__builtin_uldiv_t",
10634 partial_struct(state, "quot", &ulong_type,
10635 partial_struct(state, "rem", &ulong_type, 0)));
10636
10637 register_builtin_function(state, "__builtin_div", OP_SDIVT, div_type,
10638 &int_type, &int_type);
10639 register_builtin_function(state, "__builtin_ldiv", OP_SDIVT, ldiv_type,
10640 &long_type, &long_type);
10641 register_builtin_function(state, "__builtin_udiv", OP_UDIVT, udiv_type,
10642 &uint_type, &uint_type);
10643 register_builtin_function(state, "__builtin_uldiv", OP_UDIVT, uldiv_type,
10644 &ulong_type, &ulong_type);
10645
Eric Biederman0babc1c2003-05-09 02:39:00 +000010646 register_builtin_function(state, "__builtin_inb", OP_INB, &uchar_type,
10647 &ushort_type);
10648 register_builtin_function(state, "__builtin_inw", OP_INW, &ushort_type,
10649 &ushort_type);
10650 register_builtin_function(state, "__builtin_inl", OP_INL, &uint_type,
10651 &ushort_type);
10652
10653 register_builtin_function(state, "__builtin_outb", OP_OUTB, &void_type,
10654 &uchar_type, &ushort_type);
10655 register_builtin_function(state, "__builtin_outw", OP_OUTW, &void_type,
10656 &ushort_type, &ushort_type);
10657 register_builtin_function(state, "__builtin_outl", OP_OUTL, &void_type,
10658 &uint_type, &ushort_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010659
Eric Biederman0babc1c2003-05-09 02:39:00 +000010660 register_builtin_function(state, "__builtin_bsf", OP_BSF, &int_type,
10661 &int_type);
10662 register_builtin_function(state, "__builtin_bsr", OP_BSR, &int_type,
10663 &int_type);
10664
10665 msr_type = register_builtin_type(state, "__builtin_msr_t",
10666 partial_struct(state, "lo", &ulong_type,
10667 partial_struct(state, "hi", &ulong_type, 0)));
10668
10669 register_builtin_function(state, "__builtin_rdmsr", OP_RDMSR, msr_type,
10670 &ulong_type);
10671 register_builtin_function(state, "__builtin_wrmsr", OP_WRMSR, &void_type,
10672 &ulong_type, &ulong_type, &ulong_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010673
Eric Biederman0babc1c2003-05-09 02:39:00 +000010674 register_builtin_function(state, "__builtin_hlt", OP_HLT, &void_type,
10675 &void_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010676}
10677
10678static struct type *declarator(
10679 struct compile_state *state, struct type *type,
10680 struct hash_entry **ident, int need_ident);
10681static void decl(struct compile_state *state, struct triple *first);
10682static struct type *specifier_qualifier_list(struct compile_state *state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010683#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000010684static int isdecl_specifier(int tok);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010685#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000010686static struct type *decl_specifiers(struct compile_state *state);
10687static int istype(int tok);
10688static struct triple *expr(struct compile_state *state);
10689static struct triple *assignment_expr(struct compile_state *state);
10690static struct type *type_name(struct compile_state *state);
Eric Biederman90089602004-05-28 14:11:54 +000010691static void statement(struct compile_state *state, struct triple *first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010692
10693static struct triple *call_expr(
10694 struct compile_state *state, struct triple *func)
10695{
Eric Biederman0babc1c2003-05-09 02:39:00 +000010696 struct triple *def;
10697 struct type *param, *type;
10698 ulong_t pvals, index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010699
10700 if ((func->type->type & TYPE_MASK) != TYPE_FUNCTION) {
10701 error(state, 0, "Called object is not a function");
10702 }
10703 if (func->op != OP_LIST) {
10704 internal_error(state, 0, "improper function");
10705 }
10706 eat(state, TOK_LPAREN);
10707 /* Find the return type without any specifiers */
10708 type = clone_type(0, func->type->left);
Eric Biederman5ade04a2003-10-22 04:03:46 +000010709 /* Count the number of rhs entries for OP_FCALL */
10710 param = func->type->right;
10711 pvals = 0;
10712 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10713 pvals++;
10714 param = param->right;
10715 }
10716 if ((param->type & TYPE_MASK) != TYPE_VOID) {
10717 pvals++;
10718 }
10719 def = new_triple(state, OP_FCALL, type, -1, pvals);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010720 MISC(def, 0) = func;
10721
10722 param = func->type->right;
10723 for(index = 0; index < pvals; index++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010724 struct triple *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010725 struct type *arg_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010726 val = read_expr(state, assignment_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000010727 arg_type = param;
10728 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
10729 arg_type = param->left;
10730 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010731 write_compatible(state, arg_type, val->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010732 RHS(def, index) = val;
10733 if (index != (pvals - 1)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010734 eat(state, TOK_COMMA);
Eric Biederman0babc1c2003-05-09 02:39:00 +000010735 param = param->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010736 }
10737 }
10738 eat(state, TOK_RPAREN);
10739 return def;
10740}
10741
10742
10743static struct triple *character_constant(struct compile_state *state)
10744{
10745 struct triple *def;
10746 struct token *tk;
10747 const signed char *str, *end;
10748 int c;
10749 int str_len;
Eric Biederman41203d92004-11-08 09:31:09 +000010750 tk = eat(state, TOK_LIT_CHAR);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010751 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010752 str_len = tk->str_len - 2;
10753 if (str_len <= 0) {
10754 error(state, 0, "empty character constant");
10755 }
10756 end = str + str_len;
10757 c = char_value(state, &str, end);
10758 if (str != end) {
10759 error(state, 0, "multibyte character constant not supported");
10760 }
10761 def = int_const(state, &char_type, (ulong_t)((long_t)c));
10762 return def;
10763}
10764
10765static struct triple *string_constant(struct compile_state *state)
10766{
10767 struct triple *def;
10768 struct token *tk;
10769 struct type *type;
10770 const signed char *str, *end;
10771 signed char *buf, *ptr;
10772 int str_len;
10773
10774 buf = 0;
10775 type = new_type(TYPE_ARRAY, &char_type, 0);
10776 type->elements = 0;
10777 /* The while loop handles string concatenation */
10778 do {
Eric Biederman41203d92004-11-08 09:31:09 +000010779 tk = eat(state, TOK_LIT_STRING);
Stefan Reinauer50542a82007-10-24 11:14:14 +000010780 str = (signed char *)tk->val.str + 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010781 str_len = tk->str_len - 2;
Eric Biedermanab2ea6b2003-04-26 03:20:53 +000010782 if (str_len < 0) {
10783 error(state, 0, "negative string constant length");
Eric Biedermanb138ac82003-04-22 18:44:01 +000010784 }
Patrick Georgi78fbb512010-02-11 11:13:32 +000010785 /* ignore empty string tokens */
10786 if (strcmp("\"", str) == 0)
10787 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010788 end = str + str_len;
10789 ptr = buf;
10790 buf = xmalloc(type->elements + str_len + 1, "string_constant");
10791 memcpy(buf, ptr, type->elements);
10792 ptr = buf + type->elements;
10793 do {
10794 *ptr++ = char_value(state, &str, end);
10795 } while(str < end);
10796 type->elements = ptr - buf;
10797 } while(peek(state) == TOK_LIT_STRING);
10798 *ptr = '\0';
10799 type->elements += 1;
10800 def = triple(state, OP_BLOBCONST, type, 0, 0);
10801 def->u.blob = buf;
Eric Biederman90089602004-05-28 14:11:54 +000010802
Eric Biedermanb138ac82003-04-22 18:44:01 +000010803 return def;
10804}
10805
10806
10807static struct triple *integer_constant(struct compile_state *state)
10808{
10809 struct triple *def;
10810 unsigned long val;
10811 struct token *tk;
10812 char *end;
10813 int u, l, decimal;
10814 struct type *type;
10815
Eric Biederman41203d92004-11-08 09:31:09 +000010816 tk = eat(state, TOK_LIT_INT);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010817 errno = 0;
10818 decimal = (tk->val.str[0] != '0');
10819 val = strtoul(tk->val.str, &end, 0);
Eric Biederman83b991a2003-10-11 06:20:25 +000010820 if ((val > ULONG_T_MAX) || ((val == ULONG_MAX) && (errno == ERANGE))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010821 error(state, 0, "Integer constant to large");
10822 }
10823 u = l = 0;
10824 if ((*end == 'u') || (*end == 'U')) {
10825 u = 1;
10826 end++;
10827 }
10828 if ((*end == 'l') || (*end == 'L')) {
10829 l = 1;
10830 end++;
10831 }
10832 if ((*end == 'u') || (*end == 'U')) {
10833 u = 1;
10834 end++;
10835 }
10836 if (*end) {
10837 error(state, 0, "Junk at end of integer constant");
10838 }
10839 if (u && l) {
10840 type = &ulong_type;
10841 }
10842 else if (l) {
10843 type = &long_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010844 if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010845 type = &ulong_type;
10846 }
10847 }
10848 else if (u) {
10849 type = &uint_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010850 if (val > UINT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010851 type = &ulong_type;
10852 }
10853 }
10854 else {
10855 type = &int_type;
Eric Biederman83b991a2003-10-11 06:20:25 +000010856 if (!decimal && (val > INT_T_MAX) && (val <= UINT_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010857 type = &uint_type;
10858 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010859 else if (!decimal && (val > LONG_T_MAX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010860 type = &ulong_type;
10861 }
Eric Biederman83b991a2003-10-11 06:20:25 +000010862 else if (val > INT_T_MAX) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000010863 type = &long_type;
10864 }
10865 }
10866 def = int_const(state, type, val);
10867 return def;
10868}
10869
10870static struct triple *primary_expr(struct compile_state *state)
10871{
10872 struct triple *def;
10873 int tok;
10874 tok = peek(state);
10875 switch(tok) {
10876 case TOK_IDENT:
10877 {
10878 struct hash_entry *ident;
10879 /* Here ident is either:
10880 * a varable name
10881 * a function name
Eric Biedermanb138ac82003-04-22 18:44:01 +000010882 */
Eric Biederman41203d92004-11-08 09:31:09 +000010883 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010884 if (!ident->sym_ident) {
10885 error(state, 0, "%s undeclared", ident->name);
10886 }
10887 def = ident->sym_ident->def;
10888 break;
10889 }
10890 case TOK_ENUM_CONST:
Eric Biederman83b991a2003-10-11 06:20:25 +000010891 {
10892 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010893 /* Here ident is an enumeration constant */
Eric Biederman41203d92004-11-08 09:31:09 +000010894 ident = eat(state, TOK_ENUM_CONST)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000010895 if (!ident->sym_ident) {
10896 error(state, 0, "%s undeclared", ident->name);
10897 }
10898 def = ident->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010899 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000010900 }
Eric Biederman41203d92004-11-08 09:31:09 +000010901 case TOK_MIDENT:
10902 {
10903 struct hash_entry *ident;
10904 ident = eat(state, TOK_MIDENT)->ident;
10905 warning(state, 0, "Replacing undefined macro: %s with 0",
10906 ident->name);
10907 def = int_const(state, &int_type, 0);
10908 break;
10909 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010910 case TOK_LPAREN:
10911 eat(state, TOK_LPAREN);
10912 def = expr(state);
10913 eat(state, TOK_RPAREN);
10914 break;
10915 case TOK_LIT_INT:
10916 def = integer_constant(state);
10917 break;
10918 case TOK_LIT_FLOAT:
10919 eat(state, TOK_LIT_FLOAT);
10920 error(state, 0, "Floating point constants not supported");
10921 def = 0;
10922 FINISHME();
10923 break;
10924 case TOK_LIT_CHAR:
10925 def = character_constant(state);
10926 break;
10927 case TOK_LIT_STRING:
10928 def = string_constant(state);
10929 break;
10930 default:
10931 def = 0;
10932 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
10933 }
10934 return def;
10935}
10936
10937static struct triple *postfix_expr(struct compile_state *state)
10938{
10939 struct triple *def;
10940 int postfix;
10941 def = primary_expr(state);
10942 do {
10943 struct triple *left;
10944 int tok;
10945 postfix = 1;
10946 left = def;
10947 switch((tok = peek(state))) {
10948 case TOK_LBRACKET:
10949 eat(state, TOK_LBRACKET);
10950 def = mk_subscript_expr(state, left, expr(state));
10951 eat(state, TOK_RBRACKET);
10952 break;
10953 case TOK_LPAREN:
10954 def = call_expr(state, def);
10955 break;
10956 case TOK_DOT:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010957 {
10958 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010959 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000010960 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010961 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010962 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010963 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010964 case TOK_ARROW:
Eric Biederman0babc1c2003-05-09 02:39:00 +000010965 {
10966 struct hash_entry *field;
Eric Biedermanb138ac82003-04-22 18:44:01 +000010967 eat(state, TOK_ARROW);
Eric Biederman41203d92004-11-08 09:31:09 +000010968 field = eat(state, TOK_IDENT)->ident;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010969 def = mk_deref_expr(state, read_expr(state, def));
10970 def = deref_field(state, def, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000010971 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000010972 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000010973 case TOK_PLUSPLUS:
10974 eat(state, TOK_PLUSPLUS);
10975 def = mk_post_inc_expr(state, left);
10976 break;
10977 case TOK_MINUSMINUS:
10978 eat(state, TOK_MINUSMINUS);
10979 def = mk_post_dec_expr(state, left);
10980 break;
10981 default:
10982 postfix = 0;
10983 break;
10984 }
10985 } while(postfix);
10986 return def;
10987}
10988
10989static struct triple *cast_expr(struct compile_state *state);
10990
10991static struct triple *unary_expr(struct compile_state *state)
10992{
10993 struct triple *def, *right;
10994 int tok;
10995 switch((tok = peek(state))) {
10996 case TOK_PLUSPLUS:
10997 eat(state, TOK_PLUSPLUS);
10998 def = mk_pre_inc_expr(state, unary_expr(state));
10999 break;
11000 case TOK_MINUSMINUS:
11001 eat(state, TOK_MINUSMINUS);
11002 def = mk_pre_dec_expr(state, unary_expr(state));
11003 break;
11004 case TOK_AND:
11005 eat(state, TOK_AND);
11006 def = mk_addr_expr(state, cast_expr(state), 0);
11007 break;
11008 case TOK_STAR:
11009 eat(state, TOK_STAR);
11010 def = mk_deref_expr(state, read_expr(state, cast_expr(state)));
11011 break;
11012 case TOK_PLUS:
11013 eat(state, TOK_PLUS);
11014 right = read_expr(state, cast_expr(state));
11015 arithmetic(state, right);
11016 def = integral_promotion(state, right);
11017 break;
11018 case TOK_MINUS:
11019 eat(state, TOK_MINUS);
11020 right = read_expr(state, cast_expr(state));
11021 arithmetic(state, right);
11022 def = integral_promotion(state, right);
11023 def = triple(state, OP_NEG, def->type, def, 0);
11024 break;
11025 case TOK_TILDE:
11026 eat(state, TOK_TILDE);
11027 right = read_expr(state, cast_expr(state));
11028 integral(state, right);
11029 def = integral_promotion(state, right);
11030 def = triple(state, OP_INVERT, def->type, def, 0);
11031 break;
11032 case TOK_BANG:
11033 eat(state, TOK_BANG);
11034 right = read_expr(state, cast_expr(state));
11035 bool(state, right);
11036 def = lfalse_expr(state, right);
11037 break;
11038 case TOK_SIZEOF:
11039 {
11040 struct type *type;
11041 int tok1, tok2;
11042 eat(state, TOK_SIZEOF);
11043 tok1 = peek(state);
11044 tok2 = peek2(state);
11045 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11046 eat(state, TOK_LPAREN);
11047 type = type_name(state);
11048 eat(state, TOK_RPAREN);
11049 }
11050 else {
11051 struct triple *expr;
11052 expr = unary_expr(state);
11053 type = expr->type;
11054 release_expr(state, expr);
11055 }
Eric Biederman90089602004-05-28 14:11:54 +000011056 def = int_const(state, &ulong_type, size_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011057 break;
11058 }
11059 case TOK_ALIGNOF:
11060 {
11061 struct type *type;
11062 int tok1, tok2;
11063 eat(state, TOK_ALIGNOF);
11064 tok1 = peek(state);
11065 tok2 = peek2(state);
11066 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11067 eat(state, TOK_LPAREN);
11068 type = type_name(state);
11069 eat(state, TOK_RPAREN);
11070 }
11071 else {
11072 struct triple *expr;
11073 expr = unary_expr(state);
11074 type = expr->type;
11075 release_expr(state, expr);
11076 }
Eric Biederman90089602004-05-28 14:11:54 +000011077 def = int_const(state, &ulong_type, align_of_in_bytes(state, type));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011078 break;
11079 }
Eric Biederman41203d92004-11-08 09:31:09 +000011080 case TOK_MDEFINED:
11081 {
11082 /* We only come here if we are called from the preprocessor */
11083 struct hash_entry *ident;
11084 int parens;
11085 eat(state, TOK_MDEFINED);
11086 parens = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011087 if (pp_peek(state) == TOK_LPAREN) {
11088 pp_eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000011089 parens = 1;
11090 }
Eric Biedermancb364952004-11-15 10:46:44 +000011091 ident = pp_eat(state, TOK_MIDENT)->ident;
Eric Biederman41203d92004-11-08 09:31:09 +000011092 if (parens) {
11093 eat(state, TOK_RPAREN);
11094 }
11095 def = int_const(state, &int_type, ident->sym_define != 0);
11096 break;
11097 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011098 default:
11099 def = postfix_expr(state);
11100 break;
11101 }
11102 return def;
11103}
11104
11105static struct triple *cast_expr(struct compile_state *state)
11106{
11107 struct triple *def;
11108 int tok1, tok2;
11109 tok1 = peek(state);
11110 tok2 = peek2(state);
11111 if ((tok1 == TOK_LPAREN) && istype(tok2)) {
11112 struct type *type;
11113 eat(state, TOK_LPAREN);
11114 type = type_name(state);
11115 eat(state, TOK_RPAREN);
Eric Biedermane058a1e2003-07-12 01:21:31 +000011116 def = mk_cast_expr(state, type, cast_expr(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011117 }
11118 else {
11119 def = unary_expr(state);
11120 }
11121 return def;
11122}
11123
11124static struct triple *mult_expr(struct compile_state *state)
11125{
11126 struct triple *def;
11127 int done;
11128 def = cast_expr(state);
11129 do {
11130 struct triple *left, *right;
11131 struct type *result_type;
11132 int tok, op, sign;
11133 done = 0;
Eric Biedermancb364952004-11-15 10:46:44 +000011134 tok = peek(state);
11135 switch(tok) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011136 case TOK_STAR:
11137 case TOK_DIV:
11138 case TOK_MOD:
11139 left = read_expr(state, def);
11140 arithmetic(state, left);
11141
11142 eat(state, tok);
11143
11144 right = read_expr(state, cast_expr(state));
11145 arithmetic(state, right);
11146
11147 result_type = arithmetic_result(state, left, right);
11148 sign = is_signed(result_type);
11149 op = -1;
11150 switch(tok) {
11151 case TOK_STAR: op = sign? OP_SMUL : OP_UMUL; break;
11152 case TOK_DIV: op = sign? OP_SDIV : OP_UDIV; break;
11153 case TOK_MOD: op = sign? OP_SMOD : OP_UMOD; break;
11154 }
11155 def = triple(state, op, result_type, left, right);
11156 break;
11157 default:
11158 done = 1;
11159 break;
11160 }
11161 } while(!done);
11162 return def;
11163}
11164
11165static struct triple *add_expr(struct compile_state *state)
11166{
11167 struct triple *def;
11168 int done;
11169 def = mult_expr(state);
11170 do {
11171 done = 0;
11172 switch( peek(state)) {
11173 case TOK_PLUS:
11174 eat(state, TOK_PLUS);
11175 def = mk_add_expr(state, def, mult_expr(state));
11176 break;
11177 case TOK_MINUS:
11178 eat(state, TOK_MINUS);
11179 def = mk_sub_expr(state, def, mult_expr(state));
11180 break;
11181 default:
11182 done = 1;
11183 break;
11184 }
11185 } while(!done);
11186 return def;
11187}
11188
11189static struct triple *shift_expr(struct compile_state *state)
11190{
11191 struct triple *def;
11192 int done;
11193 def = add_expr(state);
11194 do {
11195 struct triple *left, *right;
11196 int tok, op;
11197 done = 0;
11198 switch((tok = peek(state))) {
11199 case TOK_SL:
11200 case TOK_SR:
11201 left = read_expr(state, def);
11202 integral(state, left);
11203 left = integral_promotion(state, left);
11204
11205 eat(state, tok);
11206
11207 right = read_expr(state, add_expr(state));
11208 integral(state, right);
11209 right = integral_promotion(state, right);
11210
11211 op = (tok == TOK_SL)? OP_SL :
11212 is_signed(left->type)? OP_SSR: OP_USR;
11213
11214 def = triple(state, op, left->type, left, right);
11215 break;
11216 default:
11217 done = 1;
11218 break;
11219 }
11220 } while(!done);
11221 return def;
11222}
11223
11224static struct triple *relational_expr(struct compile_state *state)
11225{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011226#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011227#warning "Extend relational exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011228#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011229 struct triple *def;
11230 int done;
11231 def = shift_expr(state);
11232 do {
11233 struct triple *left, *right;
11234 struct type *arg_type;
11235 int tok, op, sign;
11236 done = 0;
11237 switch((tok = peek(state))) {
11238 case TOK_LESS:
11239 case TOK_MORE:
11240 case TOK_LESSEQ:
11241 case TOK_MOREEQ:
11242 left = read_expr(state, def);
11243 arithmetic(state, left);
11244
11245 eat(state, tok);
11246
11247 right = read_expr(state, shift_expr(state));
11248 arithmetic(state, right);
11249
11250 arg_type = arithmetic_result(state, left, right);
11251 sign = is_signed(arg_type);
11252 op = -1;
11253 switch(tok) {
11254 case TOK_LESS: op = sign? OP_SLESS : OP_ULESS; break;
11255 case TOK_MORE: op = sign? OP_SMORE : OP_UMORE; break;
11256 case TOK_LESSEQ: op = sign? OP_SLESSEQ : OP_ULESSEQ; break;
11257 case TOK_MOREEQ: op = sign? OP_SMOREEQ : OP_UMOREEQ; break;
11258 }
11259 def = triple(state, op, &int_type, left, right);
11260 break;
11261 default:
11262 done = 1;
11263 break;
11264 }
11265 } while(!done);
11266 return def;
11267}
11268
11269static struct triple *equality_expr(struct compile_state *state)
11270{
Stefan Reinauer50542a82007-10-24 11:14:14 +000011271#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011272#warning "Extend equality exprs to work on more than arithmetic types"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011273#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011274 struct triple *def;
11275 int done;
11276 def = relational_expr(state);
11277 do {
11278 struct triple *left, *right;
11279 int tok, op;
11280 done = 0;
11281 switch((tok = peek(state))) {
11282 case TOK_EQEQ:
11283 case TOK_NOTEQ:
11284 left = read_expr(state, def);
11285 arithmetic(state, left);
11286 eat(state, tok);
11287 right = read_expr(state, relational_expr(state));
11288 arithmetic(state, right);
11289 op = (tok == TOK_EQEQ) ? OP_EQ: OP_NOTEQ;
11290 def = triple(state, op, &int_type, left, right);
11291 break;
11292 default:
11293 done = 1;
11294 break;
11295 }
11296 } while(!done);
11297 return def;
11298}
11299
11300static struct triple *and_expr(struct compile_state *state)
11301{
11302 struct triple *def;
11303 def = equality_expr(state);
11304 while(peek(state) == TOK_AND) {
11305 struct triple *left, *right;
11306 struct type *result_type;
11307 left = read_expr(state, def);
11308 integral(state, left);
11309 eat(state, TOK_AND);
11310 right = read_expr(state, equality_expr(state));
11311 integral(state, right);
11312 result_type = arithmetic_result(state, left, right);
11313 def = triple(state, OP_AND, result_type, left, right);
11314 }
11315 return def;
11316}
11317
11318static struct triple *xor_expr(struct compile_state *state)
11319{
11320 struct triple *def;
11321 def = and_expr(state);
11322 while(peek(state) == TOK_XOR) {
11323 struct triple *left, *right;
11324 struct type *result_type;
11325 left = read_expr(state, def);
11326 integral(state, left);
11327 eat(state, TOK_XOR);
11328 right = read_expr(state, and_expr(state));
11329 integral(state, right);
11330 result_type = arithmetic_result(state, left, right);
11331 def = triple(state, OP_XOR, result_type, left, right);
11332 }
11333 return def;
11334}
11335
11336static struct triple *or_expr(struct compile_state *state)
11337{
11338 struct triple *def;
11339 def = xor_expr(state);
11340 while(peek(state) == TOK_OR) {
11341 struct triple *left, *right;
11342 struct type *result_type;
11343 left = read_expr(state, def);
11344 integral(state, left);
11345 eat(state, TOK_OR);
11346 right = read_expr(state, xor_expr(state));
11347 integral(state, right);
11348 result_type = arithmetic_result(state, left, right);
11349 def = triple(state, OP_OR, result_type, left, right);
11350 }
11351 return def;
11352}
11353
11354static struct triple *land_expr(struct compile_state *state)
11355{
11356 struct triple *def;
11357 def = or_expr(state);
11358 while(peek(state) == TOK_LOGAND) {
11359 struct triple *left, *right;
11360 left = read_expr(state, def);
11361 bool(state, left);
11362 eat(state, TOK_LOGAND);
11363 right = read_expr(state, or_expr(state));
11364 bool(state, right);
11365
Eric Biederman90089602004-05-28 14:11:54 +000011366 def = mkland_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011367 ltrue_expr(state, left),
11368 ltrue_expr(state, right));
11369 }
11370 return def;
11371}
11372
11373static struct triple *lor_expr(struct compile_state *state)
11374{
11375 struct triple *def;
11376 def = land_expr(state);
11377 while(peek(state) == TOK_LOGOR) {
11378 struct triple *left, *right;
11379 left = read_expr(state, def);
11380 bool(state, left);
11381 eat(state, TOK_LOGOR);
11382 right = read_expr(state, land_expr(state));
11383 bool(state, right);
Eric Biederman90089602004-05-28 14:11:54 +000011384
11385 def = mklor_expr(state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000011386 ltrue_expr(state, left),
11387 ltrue_expr(state, right));
11388 }
11389 return def;
11390}
11391
11392static struct triple *conditional_expr(struct compile_state *state)
11393{
11394 struct triple *def;
11395 def = lor_expr(state);
11396 if (peek(state) == TOK_QUEST) {
11397 struct triple *test, *left, *right;
11398 bool(state, def);
11399 test = ltrue_expr(state, read_expr(state, def));
11400 eat(state, TOK_QUEST);
11401 left = read_expr(state, expr(state));
11402 eat(state, TOK_COLON);
11403 right = read_expr(state, conditional_expr(state));
11404
Eric Biederman90089602004-05-28 14:11:54 +000011405 def = mkcond_expr(state, test, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011406 }
11407 return def;
11408}
11409
Eric Biederman41203d92004-11-08 09:31:09 +000011410struct cv_triple {
11411 struct triple *val;
11412 int id;
11413};
11414
11415static void set_cv(struct compile_state *state, struct cv_triple *cv,
11416 struct triple *dest, struct triple *val)
11417{
11418 if (cv[dest->id].val) {
11419 free_triple(state, cv[dest->id].val);
11420 }
11421 cv[dest->id].val = val;
11422}
11423static struct triple *get_cv(struct compile_state *state, struct cv_triple *cv,
11424 struct triple *src)
11425{
11426 return cv[src->id].val;
11427}
11428
Eric Biedermanb138ac82003-04-22 18:44:01 +000011429static struct triple *eval_const_expr(
11430 struct compile_state *state, struct triple *expr)
11431{
11432 struct triple *def;
Eric Biederman00443072003-06-24 12:34:45 +000011433 if (is_const(expr)) {
11434 def = expr;
Eric Biederman41203d92004-11-08 09:31:09 +000011435 }
Eric Biederman00443072003-06-24 12:34:45 +000011436 else {
11437 /* If we don't start out as a constant simplify into one */
11438 struct triple *head, *ptr;
Eric Biederman41203d92004-11-08 09:31:09 +000011439 struct cv_triple *cv;
11440 int i, count;
Eric Biederman00443072003-06-24 12:34:45 +000011441 head = label(state); /* dummy initial triple */
11442 flatten(state, head, expr);
Eric Biederman41203d92004-11-08 09:31:09 +000011443 count = 1;
Eric Biederman00443072003-06-24 12:34:45 +000011444 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Eric Biederman41203d92004-11-08 09:31:09 +000011445 count++;
Eric Biederman00443072003-06-24 12:34:45 +000011446 }
Eric Biederman41203d92004-11-08 09:31:09 +000011447 cv = xcmalloc(sizeof(struct cv_triple)*count, "const value vector");
11448 i = 1;
11449 for(ptr = head->next; ptr != head; ptr = ptr->next) {
11450 cv[i].val = 0;
11451 cv[i].id = ptr->id;
11452 ptr->id = i;
11453 i++;
Eric Biederman00443072003-06-24 12:34:45 +000011454 }
Eric Biederman41203d92004-11-08 09:31:09 +000011455 ptr = head->next;
11456 do {
11457 valid_ins(state, ptr);
11458 if ((ptr->op == OP_PHI) || (ptr->op == OP_LIST)) {
11459 internal_error(state, ptr,
11460 "unexpected %s in constant expression",
11461 tops(ptr->op));
11462 }
11463 else if (ptr->op == OP_LIST) {
11464 }
11465 else if (triple_is_structural(state, ptr)) {
11466 ptr = ptr->next;
11467 }
11468 else if (triple_is_ubranch(state, ptr)) {
11469 ptr = TARG(ptr, 0);
11470 }
11471 else if (triple_is_cbranch(state, ptr)) {
11472 struct triple *cond_val;
11473 cond_val = get_cv(state, cv, RHS(ptr, 0));
11474 if (!cond_val || !is_const(cond_val) ||
11475 (cond_val->op != OP_INTCONST))
11476 {
11477 internal_error(state, ptr, "bad branch condition");
11478 }
11479 if (cond_val->u.cval == 0) {
11480 ptr = ptr->next;
11481 } else {
11482 ptr = TARG(ptr, 0);
11483 }
11484 }
11485 else if (triple_is_branch(state, ptr)) {
11486 error(state, ptr, "bad branch type in constant expression");
11487 }
11488 else if (ptr->op == OP_WRITE) {
11489 struct triple *val;
11490 val = get_cv(state, cv, RHS(ptr, 0));
11491
11492 set_cv(state, cv, MISC(ptr, 0),
11493 copy_triple(state, val));
11494 set_cv(state, cv, ptr,
11495 copy_triple(state, val));
11496 ptr = ptr->next;
11497 }
11498 else if (ptr->op == OP_READ) {
11499 set_cv(state, cv, ptr,
11500 copy_triple(state,
11501 get_cv(state, cv, RHS(ptr, 0))));
11502 ptr = ptr->next;
11503 }
11504 else if (triple_is_pure(state, ptr, cv[ptr->id].id)) {
11505 struct triple *val, **rhs;
11506 val = copy_triple(state, ptr);
11507 rhs = triple_rhs(state, val, 0);
11508 for(; rhs; rhs = triple_rhs(state, val, rhs)) {
11509 if (!*rhs) {
11510 internal_error(state, ptr, "Missing rhs");
11511 }
11512 *rhs = get_cv(state, cv, *rhs);
11513 }
11514 simplify(state, val);
11515 set_cv(state, cv, ptr, val);
11516 ptr = ptr->next;
11517 }
11518 else {
11519 error(state, ptr, "impure operation in constant expression");
11520 }
11521
11522 } while(ptr != head);
11523
11524 /* Get the result value */
11525 def = get_cv(state, cv, head->prev);
11526 cv[head->prev->id].val = 0;
11527
11528 /* Free the temporary values */
11529 for(i = 0; i < count; i++) {
11530 if (cv[i].val) {
11531 free_triple(state, cv[i].val);
11532 cv[i].val = 0;
11533 }
11534 }
11535 xfree(cv);
Eric Biederman00443072003-06-24 12:34:45 +000011536 /* Free the intermediate expressions */
11537 while(head->next != head) {
11538 release_triple(state, head->next);
11539 }
11540 free_triple(state, head);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011541 }
Eric Biederman41203d92004-11-08 09:31:09 +000011542 if (!is_const(def)) {
11543 error(state, expr, "Not a constant expression");
11544 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011545 return def;
11546}
11547
11548static struct triple *constant_expr(struct compile_state *state)
11549{
11550 return eval_const_expr(state, conditional_expr(state));
11551}
11552
11553static struct triple *assignment_expr(struct compile_state *state)
11554{
11555 struct triple *def, *left, *right;
11556 int tok, op, sign;
11557 /* The C grammer in K&R shows assignment expressions
11558 * only taking unary expressions as input on their
11559 * left hand side. But specifies the precedence of
11560 * assignemnt as the lowest operator except for comma.
11561 *
11562 * Allowing conditional expressions on the left hand side
11563 * of an assignement results in a grammar that accepts
11564 * a larger set of statements than standard C. As long
11565 * as the subset of the grammar that is standard C behaves
11566 * correctly this should cause no problems.
11567 *
11568 * For the extra token strings accepted by the grammar
11569 * none of them should produce a valid lvalue, so they
11570 * should not produce functioning programs.
11571 *
11572 * GCC has this bug as well, so surprises should be minimal.
11573 */
11574 def = conditional_expr(state);
11575 left = def;
11576 switch((tok = peek(state))) {
11577 case TOK_EQ:
11578 lvalue(state, left);
11579 eat(state, TOK_EQ);
11580 def = write_expr(state, left,
11581 read_expr(state, assignment_expr(state)));
11582 break;
11583 case TOK_TIMESEQ:
11584 case TOK_DIVEQ:
11585 case TOK_MODEQ:
Eric Biedermanb138ac82003-04-22 18:44:01 +000011586 lvalue(state, left);
11587 arithmetic(state, left);
11588 eat(state, tok);
11589 right = read_expr(state, assignment_expr(state));
11590 arithmetic(state, right);
11591
11592 sign = is_signed(left->type);
11593 op = -1;
11594 switch(tok) {
11595 case TOK_TIMESEQ: op = sign? OP_SMUL : OP_UMUL; break;
11596 case TOK_DIVEQ: op = sign? OP_SDIV : OP_UDIV; break;
11597 case TOK_MODEQ: op = sign? OP_SMOD : OP_UMOD; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011598 }
11599 def = write_expr(state, left,
11600 triple(state, op, left->type,
11601 read_expr(state, left), right));
11602 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000011603 case TOK_PLUSEQ:
11604 lvalue(state, left);
11605 eat(state, TOK_PLUSEQ);
11606 def = write_expr(state, left,
11607 mk_add_expr(state, left, assignment_expr(state)));
11608 break;
11609 case TOK_MINUSEQ:
11610 lvalue(state, left);
11611 eat(state, TOK_MINUSEQ);
11612 def = write_expr(state, left,
11613 mk_sub_expr(state, left, assignment_expr(state)));
11614 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011615 case TOK_SLEQ:
11616 case TOK_SREQ:
11617 case TOK_ANDEQ:
11618 case TOK_XOREQ:
11619 case TOK_OREQ:
11620 lvalue(state, left);
11621 integral(state, left);
11622 eat(state, tok);
11623 right = read_expr(state, assignment_expr(state));
11624 integral(state, right);
11625 right = integral_promotion(state, right);
11626 sign = is_signed(left->type);
11627 op = -1;
11628 switch(tok) {
11629 case TOK_SLEQ: op = OP_SL; break;
11630 case TOK_SREQ: op = sign? OP_SSR: OP_USR; break;
11631 case TOK_ANDEQ: op = OP_AND; break;
11632 case TOK_XOREQ: op = OP_XOR; break;
11633 case TOK_OREQ: op = OP_OR; break;
11634 }
11635 def = write_expr(state, left,
11636 triple(state, op, left->type,
11637 read_expr(state, left), right));
11638 break;
11639 }
11640 return def;
11641}
11642
11643static struct triple *expr(struct compile_state *state)
11644{
11645 struct triple *def;
11646 def = assignment_expr(state);
11647 while(peek(state) == TOK_COMMA) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000011648 eat(state, TOK_COMMA);
Stefan Reinauer7db27ee2006-02-19 14:43:48 +000011649 def = mkprog(state, def, assignment_expr(state), 0UL);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011650 }
11651 return def;
11652}
11653
11654static void expr_statement(struct compile_state *state, struct triple *first)
11655{
11656 if (peek(state) != TOK_SEMI) {
Eric Biederman90089602004-05-28 14:11:54 +000011657 /* lvalue conversions always apply except when certian operators
11658 * are applied. I apply the lvalue conversions here
11659 * as I know no more operators will be applied.
Eric Biederman5cd81732004-03-11 15:01:31 +000011660 */
11661 flatten(state, first, lvalue_conversion(state, expr(state)));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011662 }
11663 eat(state, TOK_SEMI);
11664}
11665
11666static void if_statement(struct compile_state *state, struct triple *first)
11667{
11668 struct triple *test, *jmp1, *jmp2, *middle, *end;
11669
11670 jmp1 = jmp2 = middle = 0;
11671 eat(state, TOK_IF);
11672 eat(state, TOK_LPAREN);
11673 test = expr(state);
11674 bool(state, test);
11675 /* Cleanup and invert the test */
11676 test = lfalse_expr(state, read_expr(state, test));
11677 eat(state, TOK_RPAREN);
11678 /* Generate the needed pieces */
11679 middle = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011680 jmp1 = branch(state, middle, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011681 /* Thread the pieces together */
11682 flatten(state, first, test);
11683 flatten(state, first, jmp1);
11684 flatten(state, first, label(state));
11685 statement(state, first);
11686 if (peek(state) == TOK_ELSE) {
11687 eat(state, TOK_ELSE);
11688 /* Generate the rest of the pieces */
11689 end = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011690 jmp2 = branch(state, end, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011691 /* Thread them together */
11692 flatten(state, first, jmp2);
11693 flatten(state, first, middle);
11694 statement(state, first);
11695 flatten(state, first, end);
11696 }
11697 else {
11698 flatten(state, first, middle);
11699 }
11700}
11701
11702static void for_statement(struct compile_state *state, struct triple *first)
11703{
11704 struct triple *head, *test, *tail, *jmp1, *jmp2, *end;
11705 struct triple *label1, *label2, *label3;
11706 struct hash_entry *ident;
11707
11708 eat(state, TOK_FOR);
11709 eat(state, TOK_LPAREN);
11710 head = test = tail = jmp1 = jmp2 = 0;
11711 if (peek(state) != TOK_SEMI) {
11712 head = expr(state);
11713 }
11714 eat(state, TOK_SEMI);
11715 if (peek(state) != TOK_SEMI) {
11716 test = expr(state);
11717 bool(state, test);
11718 test = ltrue_expr(state, read_expr(state, test));
11719 }
11720 eat(state, TOK_SEMI);
11721 if (peek(state) != TOK_RPAREN) {
11722 tail = expr(state);
11723 }
11724 eat(state, TOK_RPAREN);
11725 /* Generate the needed pieces */
11726 label1 = label(state);
11727 label2 = label(state);
11728 label3 = label(state);
11729 if (test) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011730 jmp1 = branch(state, label3, 0);
11731 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011732 }
11733 else {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011734 jmp2 = branch(state, label1, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011735 }
11736 end = label(state);
11737 /* Remember where break and continue go */
11738 start_scope(state);
11739 ident = state->i_break;
11740 symbol(state, ident, &ident->sym_ident, end, end->type);
11741 ident = state->i_continue;
11742 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11743 /* Now include the body */
11744 flatten(state, first, head);
11745 flatten(state, first, jmp1);
11746 flatten(state, first, label1);
11747 statement(state, first);
11748 flatten(state, first, label2);
11749 flatten(state, first, tail);
11750 flatten(state, first, label3);
11751 flatten(state, first, test);
11752 flatten(state, first, jmp2);
11753 flatten(state, first, end);
11754 /* Cleanup the break/continue scope */
11755 end_scope(state);
11756}
11757
11758static void while_statement(struct compile_state *state, struct triple *first)
11759{
11760 struct triple *label1, *test, *label2, *jmp1, *jmp2, *end;
11761 struct hash_entry *ident;
11762 eat(state, TOK_WHILE);
11763 eat(state, TOK_LPAREN);
11764 test = expr(state);
11765 bool(state, test);
11766 test = ltrue_expr(state, read_expr(state, test));
11767 eat(state, TOK_RPAREN);
11768 /* Generate the needed pieces */
11769 label1 = label(state);
11770 label2 = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011771 jmp1 = branch(state, label2, 0);
11772 jmp2 = branch(state, label1, test);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011773 end = label(state);
11774 /* Remember where break and continue go */
11775 start_scope(state);
11776 ident = state->i_break;
11777 symbol(state, ident, &ident->sym_ident, end, end->type);
11778 ident = state->i_continue;
11779 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11780 /* Thread them together */
11781 flatten(state, first, jmp1);
11782 flatten(state, first, label1);
11783 statement(state, first);
11784 flatten(state, first, label2);
11785 flatten(state, first, test);
11786 flatten(state, first, jmp2);
11787 flatten(state, first, end);
11788 /* Cleanup the break/continue scope */
11789 end_scope(state);
11790}
11791
11792static void do_statement(struct compile_state *state, struct triple *first)
11793{
11794 struct triple *label1, *label2, *test, *end;
11795 struct hash_entry *ident;
11796 eat(state, TOK_DO);
11797 /* Generate the needed pieces */
11798 label1 = label(state);
11799 label2 = label(state);
11800 end = label(state);
11801 /* Remember where break and continue go */
11802 start_scope(state);
11803 ident = state->i_break;
11804 symbol(state, ident, &ident->sym_ident, end, end->type);
11805 ident = state->i_continue;
11806 symbol(state, ident, &ident->sym_ident, label2, label2->type);
11807 /* Now include the body */
11808 flatten(state, first, label1);
11809 statement(state, first);
11810 /* Cleanup the break/continue scope */
11811 end_scope(state);
11812 /* Eat the rest of the loop */
11813 eat(state, TOK_WHILE);
11814 eat(state, TOK_LPAREN);
11815 test = read_expr(state, expr(state));
11816 bool(state, test);
11817 eat(state, TOK_RPAREN);
11818 eat(state, TOK_SEMI);
11819 /* Thread the pieces together */
11820 test = ltrue_expr(state, test);
11821 flatten(state, first, label2);
11822 flatten(state, first, test);
Eric Biederman0babc1c2003-05-09 02:39:00 +000011823 flatten(state, first, branch(state, label1, test));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011824 flatten(state, first, end);
11825}
11826
11827
11828static void return_statement(struct compile_state *state, struct triple *first)
11829{
11830 struct triple *jmp, *mv, *dest, *var, *val;
11831 int last;
11832 eat(state, TOK_RETURN);
11833
Stefan Reinauer50542a82007-10-24 11:14:14 +000011834#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000011835#warning "FIXME implement a more general excess branch elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000011836#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000011837 val = 0;
11838 /* If we have a return value do some more work */
11839 if (peek(state) != TOK_SEMI) {
11840 val = read_expr(state, expr(state));
11841 }
11842 eat(state, TOK_SEMI);
11843
11844 /* See if this last statement in a function */
11845 last = ((peek(state) == TOK_RBRACE) &&
11846 (state->scope_depth == GLOBAL_SCOPE_DEPTH +2));
11847
11848 /* Find the return variable */
Eric Biederman90089602004-05-28 14:11:54 +000011849 var = fresult(state, state->main_function);
11850
Eric Biedermanb138ac82003-04-22 18:44:01 +000011851 /* Find the return destination */
Eric Biederman5ade04a2003-10-22 04:03:46 +000011852 dest = state->i_return->sym_ident->def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011853 mv = jmp = 0;
11854 /* If needed generate a jump instruction */
11855 if (!last) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000011856 jmp = branch(state, dest, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011857 }
11858 /* If needed generate an assignment instruction */
11859 if (val) {
Eric Biederman90089602004-05-28 14:11:54 +000011860 mv = write_expr(state, deref_index(state, var, 1), val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011861 }
11862 /* Now put the code together */
11863 if (mv) {
11864 flatten(state, first, mv);
11865 flatten(state, first, jmp);
11866 }
11867 else if (jmp) {
11868 flatten(state, first, jmp);
11869 }
11870}
11871
11872static void break_statement(struct compile_state *state, struct triple *first)
11873{
11874 struct triple *dest;
11875 eat(state, TOK_BREAK);
11876 eat(state, TOK_SEMI);
11877 if (!state->i_break->sym_ident) {
11878 error(state, 0, "break statement not within loop or switch");
11879 }
11880 dest = state->i_break->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011881 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011882}
11883
11884static void continue_statement(struct compile_state *state, struct triple *first)
11885{
11886 struct triple *dest;
11887 eat(state, TOK_CONTINUE);
11888 eat(state, TOK_SEMI);
11889 if (!state->i_continue->sym_ident) {
11890 error(state, 0, "continue statement outside of a loop");
11891 }
11892 dest = state->i_continue->sym_ident->def;
Eric Biederman0babc1c2003-05-09 02:39:00 +000011893 flatten(state, first, branch(state, dest, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011894}
11895
11896static void goto_statement(struct compile_state *state, struct triple *first)
11897{
Eric Biederman153ea352003-06-20 14:43:20 +000011898 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000011899 eat(state, TOK_GOTO);
Eric Biederman41203d92004-11-08 09:31:09 +000011900 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011901 if (!ident->sym_label) {
11902 /* If this is a forward branch allocate the label now,
11903 * it will be flattend in the appropriate location later.
11904 */
11905 struct triple *ins;
11906 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011907 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011908 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011909 eat(state, TOK_SEMI);
Eric Biederman153ea352003-06-20 14:43:20 +000011910
11911 flatten(state, first, branch(state, ident->sym_label->def, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000011912}
11913
11914static void labeled_statement(struct compile_state *state, struct triple *first)
11915{
Eric Biederman153ea352003-06-20 14:43:20 +000011916 struct triple *ins;
11917 struct hash_entry *ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011918
Eric Biederman41203d92004-11-08 09:31:09 +000011919 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman153ea352003-06-20 14:43:20 +000011920 if (ident->sym_label && ident->sym_label->def) {
11921 ins = ident->sym_label->def;
11922 put_occurance(ins->occurance);
11923 ins->occurance = new_occurance(state);
11924 }
11925 else {
11926 ins = label(state);
Eric Biederman90089602004-05-28 14:11:54 +000011927 label_symbol(state, ident, ins, FUNCTION_SCOPE_DEPTH);
Eric Biederman153ea352003-06-20 14:43:20 +000011928 }
11929 if (ins->id & TRIPLE_FLAG_FLATTENED) {
11930 error(state, 0, "label %s already defined", ident->name);
11931 }
11932 flatten(state, first, ins);
11933
Eric Biedermanb138ac82003-04-22 18:44:01 +000011934 eat(state, TOK_COLON);
11935 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011936}
11937
11938static void switch_statement(struct compile_state *state, struct triple *first)
11939{
Eric Biederman83b991a2003-10-11 06:20:25 +000011940 struct triple *value, *top, *end, *dbranch;
11941 struct hash_entry *ident;
11942
11943 /* See if we have a valid switch statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011944 eat(state, TOK_SWITCH);
11945 eat(state, TOK_LPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011946 value = expr(state);
11947 integral(state, value);
11948 value = read_expr(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011949 eat(state, TOK_RPAREN);
Eric Biederman83b991a2003-10-11 06:20:25 +000011950 /* Generate the needed pieces */
11951 top = label(state);
11952 end = label(state);
11953 dbranch = branch(state, end, 0);
11954 /* Remember where case branches and break goes */
11955 start_scope(state);
11956 ident = state->i_switch;
11957 symbol(state, ident, &ident->sym_ident, value, value->type);
11958 ident = state->i_case;
11959 symbol(state, ident, &ident->sym_ident, top, top->type);
11960 ident = state->i_break;
11961 symbol(state, ident, &ident->sym_ident, end, end->type);
11962 ident = state->i_default;
11963 symbol(state, ident, &ident->sym_ident, dbranch, dbranch->type);
11964 /* Thread them together */
11965 flatten(state, first, value);
11966 flatten(state, first, top);
11967 flatten(state, first, dbranch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011968 statement(state, first);
Eric Biederman83b991a2003-10-11 06:20:25 +000011969 flatten(state, first, end);
11970 /* Cleanup the switch scope */
11971 end_scope(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000011972}
11973
11974static void case_statement(struct compile_state *state, struct triple *first)
11975{
Eric Biederman83b991a2003-10-11 06:20:25 +000011976 struct triple *cvalue, *dest, *test, *jmp;
11977 struct triple *ptr, *value, *top, *dbranch;
11978
11979 /* See if w have a valid case statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000011980 eat(state, TOK_CASE);
Eric Biederman83b991a2003-10-11 06:20:25 +000011981 cvalue = constant_expr(state);
11982 integral(state, cvalue);
11983 if (cvalue->op != OP_INTCONST) {
11984 error(state, 0, "integer constant expected");
11985 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000011986 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000011987 if (!state->i_case->sym_ident) {
11988 error(state, 0, "case statement not within a switch");
11989 }
11990
11991 /* Lookup the interesting pieces */
11992 top = state->i_case->sym_ident->def;
11993 value = state->i_switch->sym_ident->def;
11994 dbranch = state->i_default->sym_ident->def;
11995
11996 /* See if this case label has already been used */
11997 for(ptr = top; ptr != dbranch; ptr = ptr->next) {
11998 if (ptr->op != OP_EQ) {
11999 continue;
12000 }
12001 if (RHS(ptr, 1)->u.cval == cvalue->u.cval) {
12002 error(state, 0, "duplicate case %d statement",
12003 cvalue->u.cval);
12004 }
12005 }
12006 /* Generate the needed pieces */
12007 dest = label(state);
12008 test = triple(state, OP_EQ, &int_type, value, cvalue);
12009 jmp = branch(state, dest, test);
12010 /* Thread the pieces together */
12011 flatten(state, dbranch, test);
12012 flatten(state, dbranch, jmp);
12013 flatten(state, dbranch, label(state));
12014 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012015 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012016}
12017
12018static void default_statement(struct compile_state *state, struct triple *first)
12019{
Eric Biederman83b991a2003-10-11 06:20:25 +000012020 struct triple *dest;
12021 struct triple *dbranch, *end;
12022
12023 /* See if we have a valid default statement */
Eric Biedermanb138ac82003-04-22 18:44:01 +000012024 eat(state, TOK_DEFAULT);
12025 eat(state, TOK_COLON);
Eric Biederman83b991a2003-10-11 06:20:25 +000012026
12027 if (!state->i_case->sym_ident) {
12028 error(state, 0, "default statement not within a switch");
12029 }
12030
12031 /* Lookup the interesting pieces */
12032 dbranch = state->i_default->sym_ident->def;
12033 end = state->i_break->sym_ident->def;
12034
12035 /* See if a default statement has already happened */
12036 if (TARG(dbranch, 0) != end) {
12037 error(state, 0, "duplicate default statement");
12038 }
12039
12040 /* Generate the needed pieces */
12041 dest = label(state);
12042
Eric Biederman90089602004-05-28 14:11:54 +000012043 /* Blame the branch on the default statement */
12044 put_occurance(dbranch->occurance);
12045 dbranch->occurance = new_occurance(state);
12046
Eric Biederman83b991a2003-10-11 06:20:25 +000012047 /* Thread the pieces together */
12048 TARG(dbranch, 0) = dest;
Eric Biederman90089602004-05-28 14:11:54 +000012049 use_triple(dest, dbranch);
Eric Biederman83b991a2003-10-11 06:20:25 +000012050 flatten(state, first, dest);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012051 statement(state, first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012052}
12053
12054static void asm_statement(struct compile_state *state, struct triple *first)
12055{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012056 struct asm_info *info;
12057 struct {
12058 struct triple *constraint;
12059 struct triple *expr;
12060 } out_param[MAX_LHS], in_param[MAX_RHS], clob_param[MAX_LHS];
12061 struct triple *def, *asm_str;
12062 int out, in, clobbers, more, colons, i;
Eric Biederman90089602004-05-28 14:11:54 +000012063 int flags;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012064
Eric Biederman90089602004-05-28 14:11:54 +000012065 flags = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012066 eat(state, TOK_ASM);
12067 /* For now ignore the qualifiers */
12068 switch(peek(state)) {
12069 case TOK_CONST:
12070 eat(state, TOK_CONST);
12071 break;
12072 case TOK_VOLATILE:
12073 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012074 flags |= TRIPLE_FLAG_VOLATILE;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012075 break;
12076 }
12077 eat(state, TOK_LPAREN);
12078 asm_str = string_constant(state);
12079
12080 colons = 0;
12081 out = in = clobbers = 0;
12082 /* Outputs */
12083 if ((colons == 0) && (peek(state) == TOK_COLON)) {
12084 eat(state, TOK_COLON);
12085 colons++;
12086 more = (peek(state) == TOK_LIT_STRING);
12087 while(more) {
12088 struct triple *var;
12089 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012090 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012091 more = 0;
12092 if (out > MAX_LHS) {
12093 error(state, 0, "Maximum output count exceeded.");
12094 }
12095 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012096 str = constraint->u.blob;
12097 if (str[0] != '=') {
12098 error(state, 0, "Output constraint does not start with =");
12099 }
12100 constraint->u.blob = str + 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012101 eat(state, TOK_LPAREN);
12102 var = conditional_expr(state);
12103 eat(state, TOK_RPAREN);
12104
12105 lvalue(state, var);
12106 out_param[out].constraint = constraint;
12107 out_param[out].expr = var;
12108 if (peek(state) == TOK_COMMA) {
12109 eat(state, TOK_COMMA);
12110 more = 1;
12111 }
12112 out++;
12113 }
12114 }
12115 /* Inputs */
12116 if ((colons == 1) && (peek(state) == TOK_COLON)) {
12117 eat(state, TOK_COLON);
12118 colons++;
12119 more = (peek(state) == TOK_LIT_STRING);
12120 while(more) {
12121 struct triple *val;
12122 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012123 char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012124 more = 0;
12125 if (in > MAX_RHS) {
12126 error(state, 0, "Maximum input count exceeded.");
12127 }
12128 constraint = string_constant(state);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012129 str = constraint->u.blob;
12130 if (digitp(str[0] && str[1] == '\0')) {
12131 int val;
12132 val = digval(str[0]);
12133 if ((val < 0) || (val >= out)) {
12134 error(state, 0, "Invalid input constraint %d", val);
12135 }
12136 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012137 eat(state, TOK_LPAREN);
12138 val = conditional_expr(state);
12139 eat(state, TOK_RPAREN);
12140
12141 in_param[in].constraint = constraint;
12142 in_param[in].expr = val;
12143 if (peek(state) == TOK_COMMA) {
12144 eat(state, TOK_COMMA);
12145 more = 1;
12146 }
12147 in++;
12148 }
12149 }
12150
12151 /* Clobber */
12152 if ((colons == 2) && (peek(state) == TOK_COLON)) {
12153 eat(state, TOK_COLON);
12154 colons++;
12155 more = (peek(state) == TOK_LIT_STRING);
12156 while(more) {
12157 struct triple *clobber;
12158 more = 0;
12159 if ((clobbers + out) > MAX_LHS) {
12160 error(state, 0, "Maximum clobber limit exceeded.");
12161 }
12162 clobber = string_constant(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012163
12164 clob_param[clobbers].constraint = clobber;
12165 if (peek(state) == TOK_COMMA) {
12166 eat(state, TOK_COMMA);
12167 more = 1;
12168 }
12169 clobbers++;
12170 }
12171 }
12172 eat(state, TOK_RPAREN);
12173 eat(state, TOK_SEMI);
12174
12175
12176 info = xcmalloc(sizeof(*info), "asm_info");
12177 info->str = asm_str->u.blob;
12178 free_triple(state, asm_str);
12179
12180 def = new_triple(state, OP_ASM, &void_type, clobbers + out, in);
12181 def->u.ainfo = info;
Eric Biederman90089602004-05-28 14:11:54 +000012182 def->id |= flags;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012183
12184 /* Find the register constraints */
12185 for(i = 0; i < out; i++) {
12186 struct triple *constraint;
12187 constraint = out_param[i].constraint;
12188 info->tmpl.lhs[i] = arch_reg_constraint(state,
12189 out_param[i].expr->type, constraint->u.blob);
12190 free_triple(state, constraint);
12191 }
12192 for(; i - out < clobbers; i++) {
12193 struct triple *constraint;
12194 constraint = clob_param[i - out].constraint;
12195 info->tmpl.lhs[i] = arch_reg_clobber(state, constraint->u.blob);
12196 free_triple(state, constraint);
12197 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012198 for(i = 0; i < in; i++) {
12199 struct triple *constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012200 const char *str;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012201 constraint = in_param[i].constraint;
Eric Biederman8d9c1232003-06-17 08:42:17 +000012202 str = constraint->u.blob;
12203 if (digitp(str[0]) && str[1] == '\0') {
12204 struct reg_info cinfo;
12205 int val;
12206 val = digval(str[0]);
12207 cinfo.reg = info->tmpl.lhs[val].reg;
12208 cinfo.regcm = arch_type_to_regcm(state, in_param[i].expr->type);
12209 cinfo.regcm &= info->tmpl.lhs[val].regcm;
12210 if (cinfo.reg == REG_UNSET) {
12211 cinfo.reg = REG_VIRT0 + val;
12212 }
12213 if (cinfo.regcm == 0) {
12214 error(state, 0, "No registers for %d", val);
12215 }
12216 info->tmpl.lhs[val] = cinfo;
12217 info->tmpl.rhs[i] = cinfo;
12218
12219 } else {
12220 info->tmpl.rhs[i] = arch_reg_constraint(state,
12221 in_param[i].expr->type, str);
12222 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012223 free_triple(state, constraint);
12224 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000012225
12226 /* Now build the helper expressions */
12227 for(i = 0; i < in; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000012228 RHS(def, i) = read_expr(state, in_param[i].expr);
Eric Biederman8d9c1232003-06-17 08:42:17 +000012229 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012230 flatten(state, first, def);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012231 for(i = 0; i < (out + clobbers); i++) {
12232 struct type *type;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012233 struct triple *piece;
Eric Biederman90089602004-05-28 14:11:54 +000012234 if (i < out) {
12235 type = out_param[i].expr->type;
12236 } else {
12237 size_t size = arch_reg_size(info->tmpl.lhs[i].reg);
12238 if (size >= SIZEOF_LONG) {
12239 type = &ulong_type;
12240 }
12241 else if (size >= SIZEOF_INT) {
12242 type = &uint_type;
12243 }
12244 else if (size >= SIZEOF_SHORT) {
12245 type = &ushort_type;
12246 }
12247 else {
12248 type = &uchar_type;
12249 }
12250 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012251 piece = triple(state, OP_PIECE, type, def, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012252 piece->u.cval = i;
12253 LHS(def, i) = piece;
12254 flatten(state, first, piece);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000012255 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000012256 /* And write the helpers to their destinations */
12257 for(i = 0; i < out; i++) {
12258 struct triple *piece;
12259 piece = LHS(def, i);
12260 flatten(state, first,
12261 write_expr(state, out_param[i].expr, piece));
12262 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012263}
12264
12265
12266static int isdecl(int tok)
12267{
12268 switch(tok) {
12269 case TOK_AUTO:
12270 case TOK_REGISTER:
12271 case TOK_STATIC:
12272 case TOK_EXTERN:
12273 case TOK_TYPEDEF:
12274 case TOK_CONST:
12275 case TOK_RESTRICT:
12276 case TOK_VOLATILE:
12277 case TOK_VOID:
12278 case TOK_CHAR:
12279 case TOK_SHORT:
12280 case TOK_INT:
12281 case TOK_LONG:
12282 case TOK_FLOAT:
12283 case TOK_DOUBLE:
12284 case TOK_SIGNED:
12285 case TOK_UNSIGNED:
12286 case TOK_STRUCT:
12287 case TOK_UNION:
12288 case TOK_ENUM:
12289 case TOK_TYPE_NAME: /* typedef name */
12290 return 1;
12291 default:
12292 return 0;
12293 }
12294}
12295
12296static void compound_statement(struct compile_state *state, struct triple *first)
12297{
12298 eat(state, TOK_LBRACE);
12299 start_scope(state);
12300
12301 /* statement-list opt */
12302 while (peek(state) != TOK_RBRACE) {
12303 statement(state, first);
12304 }
12305 end_scope(state);
12306 eat(state, TOK_RBRACE);
12307}
12308
12309static void statement(struct compile_state *state, struct triple *first)
12310{
12311 int tok;
12312 tok = peek(state);
12313 if (tok == TOK_LBRACE) {
12314 compound_statement(state, first);
12315 }
12316 else if (tok == TOK_IF) {
12317 if_statement(state, first);
12318 }
12319 else if (tok == TOK_FOR) {
12320 for_statement(state, first);
12321 }
12322 else if (tok == TOK_WHILE) {
12323 while_statement(state, first);
12324 }
12325 else if (tok == TOK_DO) {
12326 do_statement(state, first);
12327 }
12328 else if (tok == TOK_RETURN) {
12329 return_statement(state, first);
12330 }
12331 else if (tok == TOK_BREAK) {
12332 break_statement(state, first);
12333 }
12334 else if (tok == TOK_CONTINUE) {
12335 continue_statement(state, first);
12336 }
12337 else if (tok == TOK_GOTO) {
12338 goto_statement(state, first);
12339 }
12340 else if (tok == TOK_SWITCH) {
12341 switch_statement(state, first);
12342 }
12343 else if (tok == TOK_ASM) {
12344 asm_statement(state, first);
12345 }
12346 else if ((tok == TOK_IDENT) && (peek2(state) == TOK_COLON)) {
12347 labeled_statement(state, first);
12348 }
12349 else if (tok == TOK_CASE) {
12350 case_statement(state, first);
12351 }
12352 else if (tok == TOK_DEFAULT) {
12353 default_statement(state, first);
12354 }
12355 else if (isdecl(tok)) {
12356 /* This handles C99 intermixing of statements and decls */
12357 decl(state, first);
12358 }
12359 else {
12360 expr_statement(state, first);
12361 }
12362}
12363
12364static struct type *param_decl(struct compile_state *state)
12365{
12366 struct type *type;
12367 struct hash_entry *ident;
12368 /* Cheat so the declarator will know we are not global */
12369 start_scope(state);
12370 ident = 0;
12371 type = decl_specifiers(state);
12372 type = declarator(state, type, &ident, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012373 type->field_ident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012374 end_scope(state);
12375 return type;
12376}
12377
12378static struct type *param_type_list(struct compile_state *state, struct type *type)
12379{
12380 struct type *ftype, **next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000012381 ftype = new_type(TYPE_FUNCTION | (type->type & STOR_MASK), type, param_decl(state));
Eric Biedermanb138ac82003-04-22 18:44:01 +000012382 next = &ftype->right;
Eric Biederman90089602004-05-28 14:11:54 +000012383 ftype->elements = 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012384 while(peek(state) == TOK_COMMA) {
12385 eat(state, TOK_COMMA);
12386 if (peek(state) == TOK_DOTS) {
12387 eat(state, TOK_DOTS);
12388 error(state, 0, "variadic functions not supported");
12389 }
12390 else {
12391 *next = new_type(TYPE_PRODUCT, *next, param_decl(state));
12392 next = &((*next)->right);
Eric Biederman90089602004-05-28 14:11:54 +000012393 ftype->elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012394 }
12395 }
12396 return ftype;
12397}
12398
Eric Biedermanb138ac82003-04-22 18:44:01 +000012399static struct type *type_name(struct compile_state *state)
12400{
12401 struct type *type;
12402 type = specifier_qualifier_list(state);
12403 /* abstract-declarator (may consume no tokens) */
12404 type = declarator(state, type, 0, 0);
12405 return type;
12406}
12407
12408static struct type *direct_declarator(
12409 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012410 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012411{
Eric Biederman41203d92004-11-08 09:31:09 +000012412 struct hash_entry *ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012413 struct type *outer;
12414 int op;
12415 outer = 0;
12416 arrays_complete(state, type);
12417 switch(peek(state)) {
12418 case TOK_IDENT:
Eric Biederman41203d92004-11-08 09:31:09 +000012419 ident = eat(state, TOK_IDENT)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012420 if (!ident) {
12421 error(state, 0, "Unexpected identifier found");
12422 }
12423 /* The name of what we are declaring */
Eric Biederman41203d92004-11-08 09:31:09 +000012424 *pident = ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012425 break;
12426 case TOK_LPAREN:
12427 eat(state, TOK_LPAREN);
Eric Biederman41203d92004-11-08 09:31:09 +000012428 outer = declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012429 eat(state, TOK_RPAREN);
12430 break;
12431 default:
12432 if (need_ident) {
12433 error(state, 0, "Identifier expected");
12434 }
12435 break;
12436 }
12437 do {
12438 op = 1;
12439 arrays_complete(state, type);
12440 switch(peek(state)) {
12441 case TOK_LPAREN:
12442 eat(state, TOK_LPAREN);
12443 type = param_type_list(state, type);
12444 eat(state, TOK_RPAREN);
12445 break;
12446 case TOK_LBRACKET:
12447 {
12448 unsigned int qualifiers;
12449 struct triple *value;
12450 value = 0;
12451 eat(state, TOK_LBRACKET);
12452 if (peek(state) != TOK_RBRACKET) {
12453 value = constant_expr(state);
12454 integral(state, value);
12455 }
12456 eat(state, TOK_RBRACKET);
12457
12458 qualifiers = type->type & (QUAL_MASK | STOR_MASK);
12459 type = new_type(TYPE_ARRAY | qualifiers, type, 0);
12460 if (value) {
12461 type->elements = value->u.cval;
12462 free_triple(state, value);
12463 } else {
12464 type->elements = ELEMENT_COUNT_UNSPECIFIED;
12465 op = 0;
12466 }
12467 }
12468 break;
12469 default:
12470 op = 0;
12471 break;
12472 }
12473 } while(op);
12474 if (outer) {
12475 struct type *inner;
12476 arrays_complete(state, type);
12477 FINISHME();
12478 for(inner = outer; inner->left; inner = inner->left)
12479 ;
12480 inner->left = type;
12481 type = outer;
12482 }
12483 return type;
12484}
12485
12486static struct type *declarator(
12487 struct compile_state *state, struct type *type,
Eric Biederman41203d92004-11-08 09:31:09 +000012488 struct hash_entry **pident, int need_ident)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012489{
12490 while(peek(state) == TOK_STAR) {
12491 eat(state, TOK_STAR);
12492 type = new_type(TYPE_POINTER | (type->type & STOR_MASK), type, 0);
12493 }
Eric Biederman41203d92004-11-08 09:31:09 +000012494 type = direct_declarator(state, type, pident, need_ident);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012495 return type;
12496}
12497
Eric Biedermanb138ac82003-04-22 18:44:01 +000012498static struct type *typedef_name(
12499 struct compile_state *state, unsigned int specifiers)
12500{
12501 struct hash_entry *ident;
12502 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012503 ident = eat(state, TOK_TYPE_NAME)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012504 type = ident->sym_ident->type;
12505 specifiers |= type->type & QUAL_MASK;
12506 if ((specifiers & (STOR_MASK | QUAL_MASK)) !=
12507 (type->type & (STOR_MASK | QUAL_MASK))) {
12508 type = clone_type(specifiers, type);
12509 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012510 return type;
12511}
12512
12513static struct type *enum_specifier(
Eric Biederman83b991a2003-10-11 06:20:25 +000012514 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012515{
Eric Biederman83b991a2003-10-11 06:20:25 +000012516 struct hash_entry *ident;
12517 ulong_t base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012518 int tok;
Eric Biederman83b991a2003-10-11 06:20:25 +000012519 struct type *enum_type;
12520 enum_type = 0;
12521 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012522 eat(state, TOK_ENUM);
12523 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012524 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012525 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012526 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012527 base = 0;
12528 if (!ident || (peek(state) == TOK_LBRACE)) {
12529 struct type **next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012530 eat(state, TOK_LBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012531 enum_type = new_type(TYPE_ENUM | spec, 0, 0);
12532 enum_type->type_ident = ident;
12533 next = &enum_type->right;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012534 do {
Eric Biederman83b991a2003-10-11 06:20:25 +000012535 struct hash_entry *eident;
12536 struct triple *value;
12537 struct type *entry;
Eric Biederman41203d92004-11-08 09:31:09 +000012538 eident = eat(state, TOK_IDENT)->ident;
Eric Biederman83b991a2003-10-11 06:20:25 +000012539 if (eident->sym_ident) {
12540 error(state, 0, "%s already declared",
12541 eident->name);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012542 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012543 eident->tok = TOK_ENUM_CONST;
12544 if (peek(state) == TOK_EQ) {
12545 struct triple *val;
12546 eat(state, TOK_EQ);
12547 val = constant_expr(state);
12548 integral(state, val);
12549 base = val->u.cval;
12550 }
12551 value = int_const(state, &int_type, base);
12552 symbol(state, eident, &eident->sym_ident, value, &int_type);
12553 entry = new_type(TYPE_LIST, 0, 0);
12554 entry->field_ident = eident;
12555 *next = entry;
12556 next = &entry->right;
12557 base += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012558 if (peek(state) == TOK_COMMA) {
12559 eat(state, TOK_COMMA);
12560 }
12561 } while(peek(state) != TOK_RBRACE);
12562 eat(state, TOK_RBRACE);
Eric Biederman83b991a2003-10-11 06:20:25 +000012563 if (ident) {
12564 symbol(state, ident, &ident->sym_tag, 0, enum_type);
12565 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012566 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012567 if (ident && ident->sym_tag &&
12568 ident->sym_tag->type &&
12569 ((ident->sym_tag->type->type & TYPE_MASK) == TYPE_ENUM)) {
12570 enum_type = clone_type(spec, ident->sym_tag->type);
12571 }
12572 else if (ident && !enum_type) {
12573 error(state, 0, "enum %s undeclared", ident->name);
12574 }
12575 return enum_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012576}
12577
Eric Biedermanb138ac82003-04-22 18:44:01 +000012578static struct type *struct_declarator(
12579 struct compile_state *state, struct type *type, struct hash_entry **ident)
12580{
Eric Biederman90089602004-05-28 14:11:54 +000012581 if (peek(state) != TOK_COLON) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012582 type = declarator(state, type, ident, 1);
12583 }
Eric Biederman90089602004-05-28 14:11:54 +000012584 if (peek(state) == TOK_COLON) {
Eric Biederman530b5192003-07-01 10:05:30 +000012585 struct triple *value;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012586 eat(state, TOK_COLON);
Eric Biederman530b5192003-07-01 10:05:30 +000012587 value = constant_expr(state);
Eric Biederman90089602004-05-28 14:11:54 +000012588 if (value->op != OP_INTCONST) {
12589 error(state, 0, "Invalid constant expression");
12590 }
12591 if (value->u.cval > size_of(state, type)) {
12592 error(state, 0, "bitfield larger than base type");
12593 }
12594 if (!TYPE_INTEGER(type->type) || ((type->type & TYPE_MASK) == TYPE_BITFIELD)) {
12595 error(state, 0, "bitfield base not an integer type");
12596 }
12597 type = new_type(TYPE_BITFIELD, type, 0);
12598 type->elements = value->u.cval;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012599 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012600 return type;
12601}
Eric Biedermanb138ac82003-04-22 18:44:01 +000012602
12603static struct type *struct_or_union_specifier(
Eric Biederman00443072003-06-24 12:34:45 +000012604 struct compile_state *state, unsigned int spec)
Eric Biedermanb138ac82003-04-22 18:44:01 +000012605{
Eric Biederman0babc1c2003-05-09 02:39:00 +000012606 struct type *struct_type;
12607 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000012608 unsigned int type_main;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012609 unsigned int type_join;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012610 int tok;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012611 struct_type = 0;
12612 ident = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012613 switch(peek(state)) {
12614 case TOK_STRUCT:
12615 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012616 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012617 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012618 break;
12619 case TOK_UNION:
12620 eat(state, TOK_UNION);
Eric Biederman90089602004-05-28 14:11:54 +000012621 type_main = TYPE_UNION;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012622 type_join = TYPE_OVERLAP;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012623 break;
12624 default:
12625 eat(state, TOK_STRUCT);
Eric Biederman90089602004-05-28 14:11:54 +000012626 type_main = TYPE_STRUCT;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012627 type_join = TYPE_PRODUCT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012628 break;
12629 }
12630 tok = peek(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000012631 if ((tok == TOK_IDENT) || (tok == TOK_ENUM_CONST) || (tok == TOK_TYPE_NAME)) {
Eric Biederman41203d92004-11-08 09:31:09 +000012632 ident = eat(state, tok)->ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012633 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012634 if (!ident || (peek(state) == TOK_LBRACE)) {
12635 ulong_t elements;
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012636 struct type **next;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012637 elements = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012638 eat(state, TOK_LBRACE);
Eric Biederman3a51f3b2003-06-25 10:38:10 +000012639 next = &struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012640 do {
12641 struct type *base_type;
12642 int done;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012643 base_type = specifier_qualifier_list(state);
12644 do {
12645 struct type *type;
Eric Biederman0babc1c2003-05-09 02:39:00 +000012646 struct hash_entry *fident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012647 done = 1;
Eric Biederman530b5192003-07-01 10:05:30 +000012648 type = struct_declarator(state, base_type, &fident);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012649 elements++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012650 if (peek(state) == TOK_COMMA) {
12651 done = 0;
12652 eat(state, TOK_COMMA);
12653 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000012654 type = clone_type(0, type);
12655 type->field_ident = fident;
12656 if (*next) {
12657 *next = new_type(type_join, *next, type);
12658 next = &((*next)->right);
12659 } else {
12660 *next = type;
12661 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012662 } while(!done);
12663 eat(state, TOK_SEMI);
12664 } while(peek(state) != TOK_RBRACE);
12665 eat(state, TOK_RBRACE);
Eric Biederman90089602004-05-28 14:11:54 +000012666 struct_type = new_type(type_main | spec, struct_type, 0);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012667 struct_type->type_ident = ident;
12668 struct_type->elements = elements;
Eric Biedermane058a1e2003-07-12 01:21:31 +000012669 if (ident) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012670 symbol(state, ident, &ident->sym_tag, 0, struct_type);
Eric Biedermane058a1e2003-07-12 01:21:31 +000012671 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000012672 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012673 if (ident && ident->sym_tag &&
12674 ident->sym_tag->type &&
Eric Biederman90089602004-05-28 14:11:54 +000012675 ((ident->sym_tag->type->type & TYPE_MASK) == type_main)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000012676 struct_type = clone_type(spec, ident->sym_tag->type);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012677 }
Eric Biederman83b991a2003-10-11 06:20:25 +000012678 else if (ident && !struct_type) {
Eric Biederman90089602004-05-28 14:11:54 +000012679 error(state, 0, "%s %s undeclared",
12680 (type_main == TYPE_STRUCT)?"struct" : "union",
12681 ident->name);
Eric Biederman0babc1c2003-05-09 02:39:00 +000012682 }
12683 return struct_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012684}
12685
12686static unsigned int storage_class_specifier_opt(struct compile_state *state)
12687{
12688 unsigned int specifiers;
12689 switch(peek(state)) {
12690 case TOK_AUTO:
12691 eat(state, TOK_AUTO);
12692 specifiers = STOR_AUTO;
12693 break;
12694 case TOK_REGISTER:
12695 eat(state, TOK_REGISTER);
12696 specifiers = STOR_REGISTER;
12697 break;
12698 case TOK_STATIC:
12699 eat(state, TOK_STATIC);
12700 specifiers = STOR_STATIC;
12701 break;
12702 case TOK_EXTERN:
12703 eat(state, TOK_EXTERN);
12704 specifiers = STOR_EXTERN;
12705 break;
12706 case TOK_TYPEDEF:
12707 eat(state, TOK_TYPEDEF);
12708 specifiers = STOR_TYPEDEF;
12709 break;
12710 default:
12711 if (state->scope_depth <= GLOBAL_SCOPE_DEPTH) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000012712 specifiers = STOR_LOCAL;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012713 }
12714 else {
12715 specifiers = STOR_AUTO;
12716 }
12717 }
12718 return specifiers;
12719}
12720
12721static unsigned int function_specifier_opt(struct compile_state *state)
12722{
12723 /* Ignore the inline keyword */
12724 unsigned int specifiers;
12725 specifiers = 0;
12726 switch(peek(state)) {
12727 case TOK_INLINE:
12728 eat(state, TOK_INLINE);
12729 specifiers = STOR_INLINE;
12730 }
12731 return specifiers;
12732}
12733
Eric Biederman90089602004-05-28 14:11:54 +000012734static unsigned int attrib(struct compile_state *state, unsigned int attributes)
12735{
12736 int tok = peek(state);
12737 switch(tok) {
12738 case TOK_COMMA:
12739 case TOK_LPAREN:
12740 /* The empty attribute ignore it */
12741 break;
12742 case TOK_IDENT:
12743 case TOK_ENUM_CONST:
12744 case TOK_TYPE_NAME:
12745 {
12746 struct hash_entry *ident;
Eric Biederman41203d92004-11-08 09:31:09 +000012747 ident = eat(state, TOK_IDENT)->ident;
Eric Biederman90089602004-05-28 14:11:54 +000012748
12749 if (ident == state->i_noinline) {
12750 if (attributes & ATTRIB_ALWAYS_INLINE) {
12751 error(state, 0, "both always_inline and noinline attribtes");
12752 }
12753 attributes |= ATTRIB_NOINLINE;
12754 }
12755 else if (ident == state->i_always_inline) {
12756 if (attributes & ATTRIB_NOINLINE) {
12757 error(state, 0, "both noinline and always_inline attribtes");
12758 }
12759 attributes |= ATTRIB_ALWAYS_INLINE;
12760 }
12761 else {
12762 error(state, 0, "Unknown attribute:%s", ident->name);
12763 }
12764 break;
12765 }
12766 default:
12767 error(state, 0, "Unexpected token: %s\n", tokens[tok]);
12768 break;
12769 }
12770 return attributes;
12771}
12772
12773static unsigned int attribute_list(struct compile_state *state, unsigned type)
12774{
12775 type = attrib(state, type);
12776 while(peek(state) == TOK_COMMA) {
12777 eat(state, TOK_COMMA);
12778 type = attrib(state, type);
12779 }
12780 return type;
12781}
12782
12783static unsigned int attributes_opt(struct compile_state *state, unsigned type)
12784{
12785 if (peek(state) == TOK_ATTRIBUTE) {
12786 eat(state, TOK_ATTRIBUTE);
12787 eat(state, TOK_LPAREN);
12788 eat(state, TOK_LPAREN);
12789 type = attribute_list(state, type);
12790 eat(state, TOK_RPAREN);
12791 eat(state, TOK_RPAREN);
12792 }
12793 return type;
12794}
12795
Eric Biedermanb138ac82003-04-22 18:44:01 +000012796static unsigned int type_qualifiers(struct compile_state *state)
12797{
12798 unsigned int specifiers;
12799 int done;
12800 done = 0;
12801 specifiers = QUAL_NONE;
12802 do {
12803 switch(peek(state)) {
12804 case TOK_CONST:
12805 eat(state, TOK_CONST);
Eric Biederman90089602004-05-28 14:11:54 +000012806 specifiers |= QUAL_CONST;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012807 break;
12808 case TOK_VOLATILE:
12809 eat(state, TOK_VOLATILE);
Eric Biederman90089602004-05-28 14:11:54 +000012810 specifiers |= QUAL_VOLATILE;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012811 break;
12812 case TOK_RESTRICT:
12813 eat(state, TOK_RESTRICT);
Eric Biederman90089602004-05-28 14:11:54 +000012814 specifiers |= QUAL_RESTRICT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012815 break;
12816 default:
12817 done = 1;
12818 break;
12819 }
12820 } while(!done);
12821 return specifiers;
12822}
12823
12824static struct type *type_specifier(
12825 struct compile_state *state, unsigned int spec)
12826{
12827 struct type *type;
Eric Biederman41203d92004-11-08 09:31:09 +000012828 int tok;
Eric Biedermanb138ac82003-04-22 18:44:01 +000012829 type = 0;
Eric Biederman41203d92004-11-08 09:31:09 +000012830 switch((tok = peek(state))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000012831 case TOK_VOID:
12832 eat(state, TOK_VOID);
12833 type = new_type(TYPE_VOID | spec, 0, 0);
12834 break;
12835 case TOK_CHAR:
12836 eat(state, TOK_CHAR);
12837 type = new_type(TYPE_CHAR | spec, 0, 0);
12838 break;
12839 case TOK_SHORT:
12840 eat(state, TOK_SHORT);
12841 if (peek(state) == TOK_INT) {
12842 eat(state, TOK_INT);
12843 }
12844 type = new_type(TYPE_SHORT | spec, 0, 0);
12845 break;
12846 case TOK_INT:
12847 eat(state, TOK_INT);
12848 type = new_type(TYPE_INT | spec, 0, 0);
12849 break;
12850 case TOK_LONG:
12851 eat(state, TOK_LONG);
12852 switch(peek(state)) {
12853 case TOK_LONG:
12854 eat(state, TOK_LONG);
12855 error(state, 0, "long long not supported");
12856 break;
12857 case TOK_DOUBLE:
12858 eat(state, TOK_DOUBLE);
12859 error(state, 0, "long double not supported");
12860 break;
12861 case TOK_INT:
12862 eat(state, TOK_INT);
12863 type = new_type(TYPE_LONG | spec, 0, 0);
12864 break;
12865 default:
12866 type = new_type(TYPE_LONG | spec, 0, 0);
12867 break;
12868 }
12869 break;
12870 case TOK_FLOAT:
12871 eat(state, TOK_FLOAT);
12872 error(state, 0, "type float not supported");
12873 break;
12874 case TOK_DOUBLE:
12875 eat(state, TOK_DOUBLE);
12876 error(state, 0, "type double not supported");
12877 break;
12878 case TOK_SIGNED:
12879 eat(state, TOK_SIGNED);
12880 switch(peek(state)) {
12881 case TOK_LONG:
12882 eat(state, TOK_LONG);
12883 switch(peek(state)) {
12884 case TOK_LONG:
12885 eat(state, TOK_LONG);
12886 error(state, 0, "type long long not supported");
12887 break;
12888 case TOK_INT:
12889 eat(state, TOK_INT);
12890 type = new_type(TYPE_LONG | spec, 0, 0);
12891 break;
12892 default:
12893 type = new_type(TYPE_LONG | spec, 0, 0);
12894 break;
12895 }
12896 break;
12897 case TOK_INT:
12898 eat(state, TOK_INT);
12899 type = new_type(TYPE_INT | spec, 0, 0);
12900 break;
12901 case TOK_SHORT:
12902 eat(state, TOK_SHORT);
12903 type = new_type(TYPE_SHORT | spec, 0, 0);
12904 break;
12905 case TOK_CHAR:
12906 eat(state, TOK_CHAR);
12907 type = new_type(TYPE_CHAR | spec, 0, 0);
12908 break;
12909 default:
12910 type = new_type(TYPE_INT | spec, 0, 0);
12911 break;
12912 }
12913 break;
12914 case TOK_UNSIGNED:
12915 eat(state, TOK_UNSIGNED);
12916 switch(peek(state)) {
12917 case TOK_LONG:
12918 eat(state, TOK_LONG);
12919 switch(peek(state)) {
12920 case TOK_LONG:
12921 eat(state, TOK_LONG);
12922 error(state, 0, "unsigned long long not supported");
12923 break;
12924 case TOK_INT:
12925 eat(state, TOK_INT);
12926 type = new_type(TYPE_ULONG | spec, 0, 0);
12927 break;
12928 default:
12929 type = new_type(TYPE_ULONG | spec, 0, 0);
12930 break;
12931 }
12932 break;
12933 case TOK_INT:
12934 eat(state, TOK_INT);
12935 type = new_type(TYPE_UINT | spec, 0, 0);
12936 break;
12937 case TOK_SHORT:
12938 eat(state, TOK_SHORT);
12939 type = new_type(TYPE_USHORT | spec, 0, 0);
12940 break;
12941 case TOK_CHAR:
12942 eat(state, TOK_CHAR);
12943 type = new_type(TYPE_UCHAR | spec, 0, 0);
12944 break;
12945 default:
12946 type = new_type(TYPE_UINT | spec, 0, 0);
12947 break;
12948 }
12949 break;
12950 /* struct or union specifier */
12951 case TOK_STRUCT:
12952 case TOK_UNION:
12953 type = struct_or_union_specifier(state, spec);
12954 break;
12955 /* enum-spefifier */
12956 case TOK_ENUM:
12957 type = enum_specifier(state, spec);
12958 break;
12959 /* typedef name */
12960 case TOK_TYPE_NAME:
12961 type = typedef_name(state, spec);
12962 break;
12963 default:
12964 error(state, 0, "bad type specifier %s",
Eric Biederman41203d92004-11-08 09:31:09 +000012965 tokens[tok]);
Eric Biedermanb138ac82003-04-22 18:44:01 +000012966 break;
12967 }
12968 return type;
12969}
12970
12971static int istype(int tok)
12972{
12973 switch(tok) {
12974 case TOK_CONST:
12975 case TOK_RESTRICT:
12976 case TOK_VOLATILE:
12977 case TOK_VOID:
12978 case TOK_CHAR:
12979 case TOK_SHORT:
12980 case TOK_INT:
12981 case TOK_LONG:
12982 case TOK_FLOAT:
12983 case TOK_DOUBLE:
12984 case TOK_SIGNED:
12985 case TOK_UNSIGNED:
12986 case TOK_STRUCT:
12987 case TOK_UNION:
12988 case TOK_ENUM:
12989 case TOK_TYPE_NAME:
12990 return 1;
12991 default:
12992 return 0;
12993 }
12994}
12995
12996
12997static struct type *specifier_qualifier_list(struct compile_state *state)
12998{
12999 struct type *type;
13000 unsigned int specifiers = 0;
13001
13002 /* type qualifiers */
13003 specifiers |= type_qualifiers(state);
13004
13005 /* type specifier */
13006 type = type_specifier(state, specifiers);
13007
13008 return type;
13009}
13010
Stefan Reinauer50542a82007-10-24 11:14:14 +000013011#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000013012static int isdecl_specifier(int tok)
13013{
13014 switch(tok) {
13015 /* storage class specifier */
13016 case TOK_AUTO:
13017 case TOK_REGISTER:
13018 case TOK_STATIC:
13019 case TOK_EXTERN:
13020 case TOK_TYPEDEF:
13021 /* type qualifier */
13022 case TOK_CONST:
13023 case TOK_RESTRICT:
13024 case TOK_VOLATILE:
13025 /* type specifiers */
13026 case TOK_VOID:
13027 case TOK_CHAR:
13028 case TOK_SHORT:
13029 case TOK_INT:
13030 case TOK_LONG:
13031 case TOK_FLOAT:
13032 case TOK_DOUBLE:
13033 case TOK_SIGNED:
13034 case TOK_UNSIGNED:
13035 /* struct or union specifier */
13036 case TOK_STRUCT:
13037 case TOK_UNION:
13038 /* enum-spefifier */
13039 case TOK_ENUM:
13040 /* typedef name */
13041 case TOK_TYPE_NAME:
13042 /* function specifiers */
13043 case TOK_INLINE:
13044 return 1;
13045 default:
13046 return 0;
13047 }
13048}
Stefan Reinauer50542a82007-10-24 11:14:14 +000013049#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013050
13051static struct type *decl_specifiers(struct compile_state *state)
13052{
13053 struct type *type;
13054 unsigned int specifiers;
13055 /* I am overly restrictive in the arragement of specifiers supported.
13056 * C is overly flexible in this department it makes interpreting
13057 * the parse tree difficult.
13058 */
13059 specifiers = 0;
13060
13061 /* storage class specifier */
13062 specifiers |= storage_class_specifier_opt(state);
13063
13064 /* function-specifier */
13065 specifiers |= function_specifier_opt(state);
13066
Eric Biederman90089602004-05-28 14:11:54 +000013067 /* attributes */
13068 specifiers |= attributes_opt(state, 0);
13069
Eric Biedermanb138ac82003-04-22 18:44:01 +000013070 /* type qualifier */
13071 specifiers |= type_qualifiers(state);
13072
13073 /* type specifier */
13074 type = type_specifier(state, specifiers);
13075 return type;
13076}
13077
Eric Biederman00443072003-06-24 12:34:45 +000013078struct field_info {
13079 struct type *type;
13080 size_t offset;
13081};
13082
13083static struct field_info designator(struct compile_state *state, struct type *type)
Eric Biedermanb138ac82003-04-22 18:44:01 +000013084{
13085 int tok;
Eric Biederman00443072003-06-24 12:34:45 +000013086 struct field_info info;
13087 info.offset = ~0U;
13088 info.type = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013089 do {
13090 switch(peek(state)) {
13091 case TOK_LBRACKET:
13092 {
13093 struct triple *value;
Eric Biederman00443072003-06-24 12:34:45 +000013094 if ((type->type & TYPE_MASK) != TYPE_ARRAY) {
13095 error(state, 0, "Array designator not in array initializer");
13096 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013097 eat(state, TOK_LBRACKET);
13098 value = constant_expr(state);
13099 eat(state, TOK_RBRACKET);
Eric Biederman00443072003-06-24 12:34:45 +000013100
13101 info.type = type->left;
13102 info.offset = value->u.cval * size_of(state, info.type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013103 break;
13104 }
13105 case TOK_DOT:
Eric Biederman00443072003-06-24 12:34:45 +000013106 {
13107 struct hash_entry *field;
Eric Biederman90089602004-05-28 14:11:54 +000013108 if (((type->type & TYPE_MASK) != TYPE_STRUCT) &&
13109 ((type->type & TYPE_MASK) != TYPE_UNION))
13110 {
Eric Biederman00443072003-06-24 12:34:45 +000013111 error(state, 0, "Struct designator not in struct initializer");
13112 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013113 eat(state, TOK_DOT);
Eric Biederman41203d92004-11-08 09:31:09 +000013114 field = eat(state, TOK_IDENT)->ident;
Eric Biederman03b59862003-06-24 14:27:37 +000013115 info.offset = field_offset(state, type, field);
13116 info.type = field_type(state, type, field);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013117 break;
Eric Biederman00443072003-06-24 12:34:45 +000013118 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013119 default:
13120 error(state, 0, "Invalid designator");
13121 }
13122 tok = peek(state);
13123 } while((tok == TOK_LBRACKET) || (tok == TOK_DOT));
13124 eat(state, TOK_EQ);
Eric Biederman00443072003-06-24 12:34:45 +000013125 return info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013126}
13127
13128static struct triple *initializer(
13129 struct compile_state *state, struct type *type)
13130{
13131 struct triple *result;
Stefan Reinauer50542a82007-10-24 11:14:14 +000013132#if DEBUG_ROMCC_WARNINGS
Eric Biedermane058a1e2003-07-12 01:21:31 +000013133#warning "FIXME more consistent initializer handling (where should eval_const_expr go?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000013134#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013135 if (peek(state) != TOK_LBRACE) {
13136 result = assignment_expr(state);
Eric Biedermane058a1e2003-07-12 01:21:31 +000013137 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
13138 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13139 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13140 (result->type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13141 (equiv_types(type->left, result->type->left))) {
13142 type->elements = result->type->elements;
13143 }
Eric Biederman90089602004-05-28 14:11:54 +000013144 if (is_lvalue(state, result) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000013145 ((result->type->type & TYPE_MASK) == TYPE_ARRAY) &&
13146 (type->type & TYPE_MASK) != TYPE_ARRAY)
13147 {
Eric Biederman5cd81732004-03-11 15:01:31 +000013148 result = lvalue_conversion(state, result);
Eric Biederman83b991a2003-10-11 06:20:25 +000013149 }
Eric Biedermane058a1e2003-07-12 01:21:31 +000013150 if (!is_init_compatible(state, type, result->type)) {
13151 error(state, 0, "Incompatible types in initializer");
13152 }
13153 if (!equiv_types(type, result->type)) {
13154 result = mk_cast_expr(state, type, result);
13155 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013156 }
13157 else {
13158 int comma;
Eric Biederman00443072003-06-24 12:34:45 +000013159 size_t max_offset;
13160 struct field_info info;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013161 void *buf;
Eric Biederman00443072003-06-24 12:34:45 +000013162 if (((type->type & TYPE_MASK) != TYPE_ARRAY) &&
13163 ((type->type & TYPE_MASK) != TYPE_STRUCT)) {
13164 internal_error(state, 0, "unknown initializer type");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013165 }
Eric Biederman00443072003-06-24 12:34:45 +000013166 info.offset = 0;
13167 info.type = type->left;
Eric Biederman03b59862003-06-24 14:27:37 +000013168 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13169 info.type = next_field(state, type, 0);
13170 }
Eric Biederman00443072003-06-24 12:34:45 +000013171 if (type->elements == ELEMENT_COUNT_UNSPECIFIED) {
13172 max_offset = 0;
13173 } else {
13174 max_offset = size_of(state, type);
13175 }
Eric Biederman90089602004-05-28 14:11:54 +000013176 buf = xcmalloc(bits_to_bytes(max_offset), "initializer");
Eric Biedermanb138ac82003-04-22 18:44:01 +000013177 eat(state, TOK_LBRACE);
13178 do {
13179 struct triple *value;
13180 struct type *value_type;
13181 size_t value_size;
Eric Biederman00443072003-06-24 12:34:45 +000013182 void *dest;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013183 int tok;
13184 comma = 0;
13185 tok = peek(state);
13186 if ((tok == TOK_LBRACKET) || (tok == TOK_DOT)) {
Eric Biederman00443072003-06-24 12:34:45 +000013187 info = designator(state, type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013188 }
Eric Biederman00443072003-06-24 12:34:45 +000013189 if ((type->elements != ELEMENT_COUNT_UNSPECIFIED) &&
13190 (info.offset >= max_offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013191 error(state, 0, "element beyond bounds");
13192 }
Eric Biederman00443072003-06-24 12:34:45 +000013193 value_type = info.type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013194 value = eval_const_expr(state, initializer(state, value_type));
13195 value_size = size_of(state, value_type);
13196 if (((type->type & TYPE_MASK) == TYPE_ARRAY) &&
Eric Biederman00443072003-06-24 12:34:45 +000013197 (type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13198 (max_offset <= info.offset)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013199 void *old_buf;
13200 size_t old_size;
13201 old_buf = buf;
Eric Biederman00443072003-06-24 12:34:45 +000013202 old_size = max_offset;
13203 max_offset = info.offset + value_size;
Eric Biederman90089602004-05-28 14:11:54 +000013204 buf = xmalloc(bits_to_bytes(max_offset), "initializer");
13205 memcpy(buf, old_buf, bits_to_bytes(old_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013206 xfree(old_buf);
13207 }
Eric Biederman90089602004-05-28 14:11:54 +000013208 dest = ((char *)buf) + bits_to_bytes(info.offset);
13209#if DEBUG_INITIALIZER
13210 fprintf(state->errout, "dest = buf + %d max_offset: %d value_size: %d op: %d\n",
13211 dest - buf,
13212 bits_to_bytes(max_offset),
13213 bits_to_bytes(value_size),
13214 value->op);
13215#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000013216 if (value->op == OP_BLOBCONST) {
Eric Biederman90089602004-05-28 14:11:54 +000013217 memcpy(dest, value->u.blob, bits_to_bytes(value_size));
Eric Biedermanb138ac82003-04-22 18:44:01 +000013218 }
Eric Biederman90089602004-05-28 14:11:54 +000013219 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I8)) {
13220#if DEBUG_INITIALIZER
13221 fprintf(state->errout, "byte: %02x\n", value->u.cval & 0xff);
13222#endif
Eric Biederman00443072003-06-24 12:34:45 +000013223 *((uint8_t *)dest) = value->u.cval & 0xff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013224 }
Eric Biederman90089602004-05-28 14:11:54 +000013225 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I16)) {
Eric Biederman00443072003-06-24 12:34:45 +000013226 *((uint16_t *)dest) = value->u.cval & 0xffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013227 }
Eric Biederman90089602004-05-28 14:11:54 +000013228 else if ((value->op == OP_INTCONST) && (value_size == SIZEOF_I32)) {
Eric Biederman00443072003-06-24 12:34:45 +000013229 *((uint32_t *)dest) = value->u.cval & 0xffffffff;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013230 }
13231 else {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013232 internal_error(state, 0, "unhandled constant initializer");
13233 }
Eric Biederman00443072003-06-24 12:34:45 +000013234 free_triple(state, value);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013235 if (peek(state) == TOK_COMMA) {
13236 eat(state, TOK_COMMA);
13237 comma = 1;
13238 }
Eric Biederman00443072003-06-24 12:34:45 +000013239 info.offset += value_size;
Eric Biederman03b59862003-06-24 14:27:37 +000013240 if ((type->type & TYPE_MASK) == TYPE_STRUCT) {
13241 info.type = next_field(state, type, info.type);
13242 info.offset = field_offset(state, type,
13243 info.type->field_ident);
Eric Biederman00443072003-06-24 12:34:45 +000013244 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013245 } while(comma && (peek(state) != TOK_RBRACE));
Eric Biederman00443072003-06-24 12:34:45 +000013246 if ((type->elements == ELEMENT_COUNT_UNSPECIFIED) &&
13247 ((type->type & TYPE_MASK) == TYPE_ARRAY)) {
13248 type->elements = max_offset / size_of(state, type->left);
13249 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013250 eat(state, TOK_RBRACE);
13251 result = triple(state, OP_BLOBCONST, type, 0, 0);
13252 result->u.blob = buf;
13253 }
13254 return result;
13255}
13256
Eric Biederman90089602004-05-28 14:11:54 +000013257static void resolve_branches(struct compile_state *state, struct triple *first)
Eric Biederman153ea352003-06-20 14:43:20 +000013258{
13259 /* Make a second pass and finish anything outstanding
13260 * with respect to branches. The only outstanding item
13261 * is to see if there are goto to labels that have not
13262 * been defined and to error about them.
13263 */
13264 int i;
Eric Biederman90089602004-05-28 14:11:54 +000013265 struct triple *ins;
13266 /* Also error on branches that do not use their targets */
13267 ins = first;
13268 do {
13269 if (!triple_is_ret(state, ins)) {
13270 struct triple **expr ;
13271 struct triple_set *set;
13272 expr = triple_targ(state, ins, 0);
13273 for(; expr; expr = triple_targ(state, ins, expr)) {
13274 struct triple *targ;
13275 targ = *expr;
13276 for(set = targ?targ->use:0; set; set = set->next) {
13277 if (set->member == ins) {
13278 break;
13279 }
13280 }
13281 if (!set) {
13282 internal_error(state, ins, "targ not used");
13283 }
13284 }
13285 }
13286 ins = ins->next;
13287 } while(ins != first);
13288 /* See if there are goto to labels that have not been defined */
Eric Biederman153ea352003-06-20 14:43:20 +000013289 for(i = 0; i < HASH_TABLE_SIZE; i++) {
13290 struct hash_entry *entry;
13291 for(entry = state->hash_table[i]; entry; entry = entry->next) {
13292 struct triple *ins;
13293 if (!entry->sym_label) {
13294 continue;
13295 }
13296 ins = entry->sym_label->def;
13297 if (!(ins->id & TRIPLE_FLAG_FLATTENED)) {
13298 error(state, ins, "label `%s' used but not defined",
13299 entry->name);
13300 }
13301 }
13302 }
13303}
13304
Eric Biedermanb138ac82003-04-22 18:44:01 +000013305static struct triple *function_definition(
13306 struct compile_state *state, struct type *type)
13307{
Eric Biederman90089602004-05-28 14:11:54 +000013308 struct triple *def, *tmp, *first, *end, *retvar, *result, *ret;
13309 struct triple *fname;
13310 struct type *fname_type;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013311 struct hash_entry *ident;
Eric Biederman90089602004-05-28 14:11:54 +000013312 struct type *param, *crtype, *ctype;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013313 int i;
13314 if ((type->type &TYPE_MASK) != TYPE_FUNCTION) {
13315 error(state, 0, "Invalid function header");
13316 }
13317
13318 /* Verify the function type */
13319 if (((type->right->type & TYPE_MASK) != TYPE_VOID) &&
13320 ((type->right->type & TYPE_MASK) != TYPE_PRODUCT) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000013321 (type->right->field_ident == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013322 error(state, 0, "Invalid function parameters");
13323 }
13324 param = type->right;
13325 i = 0;
13326 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
13327 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013328 if (!param->left->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013329 error(state, 0, "No identifier for parameter %d\n", i);
13330 }
13331 param = param->right;
13332 }
13333 i++;
Eric Biederman0babc1c2003-05-09 02:39:00 +000013334 if (((param->type & TYPE_MASK) != TYPE_VOID) && !param->field_ident) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000013335 error(state, 0, "No identifier for paramter %d\n", i);
13336 }
13337
13338 /* Get a list of statements for this function. */
13339 def = triple(state, OP_LIST, type, 0, 0);
13340
13341 /* Start a new scope for the passed parameters */
13342 start_scope(state);
13343
13344 /* Put a label at the very start of a function */
13345 first = label(state);
Eric Biederman0babc1c2003-05-09 02:39:00 +000013346 RHS(def, 0) = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013347
13348 /* Put a label at the very end of a function */
13349 end = label(state);
13350 flatten(state, first, end);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013351 /* Remember where return goes */
13352 ident = state->i_return;
13353 symbol(state, ident, &ident->sym_ident, end, end->type);
13354
Eric Biederman90089602004-05-28 14:11:54 +000013355 /* Get the initial closure type */
13356 ctype = new_type(TYPE_JOIN, &void_type, 0);
13357 ctype->elements = 1;
13358
13359 /* Add a variable for the return value */
13360 crtype = new_type(TYPE_TUPLE,
13361 /* Remove all type qualifiers from the return type */
13362 new_type(TYPE_PRODUCT, ctype, clone_type(0, type->left)), 0);
13363 crtype->elements = 2;
13364 result = flatten(state, end, variable(state, crtype));
13365
Eric Biederman5ade04a2003-10-22 04:03:46 +000013366 /* Allocate a variable for the return address */
Eric Biederman90089602004-05-28 14:11:54 +000013367 retvar = flatten(state, end, variable(state, &void_ptr_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013368
13369 /* Add in the return instruction */
13370 ret = triple(state, OP_RET, &void_type, read_expr(state, retvar), 0);
13371 ret = flatten(state, first, ret);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013372
13373 /* Walk through the parameters and create symbol table entries
13374 * for them.
13375 */
13376 param = type->right;
13377 while((param->type & TYPE_MASK) == TYPE_PRODUCT) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000013378 ident = param->left->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013379 tmp = variable(state, param->left);
Eric Biederman90089602004-05-28 14:11:54 +000013380 var_symbol(state, ident, tmp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013381 flatten(state, end, tmp);
13382 param = param->right;
13383 }
13384 if ((param->type & TYPE_MASK) != TYPE_VOID) {
13385 /* And don't forget the last parameter */
Eric Biederman0babc1c2003-05-09 02:39:00 +000013386 ident = param->field_ident;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013387 tmp = variable(state, param);
13388 symbol(state, ident, &ident->sym_ident, tmp, tmp->type);
13389 flatten(state, end, tmp);
13390 }
Eric Biederman90089602004-05-28 14:11:54 +000013391
13392 /* Add the declaration static const char __func__ [] = "func-name" */
13393 fname_type = new_type(TYPE_ARRAY,
13394 clone_type(QUAL_CONST | STOR_STATIC, &char_type), 0);
13395 fname_type->type |= QUAL_CONST | STOR_STATIC;
13396 fname_type->elements = strlen(state->function) + 1;
13397
13398 fname = triple(state, OP_BLOBCONST, fname_type, 0, 0);
13399 fname->u.blob = (void *)state->function;
13400 fname = flatten(state, end, fname);
13401
13402 ident = state->i___func__;
13403 symbol(state, ident, &ident->sym_ident, fname, fname_type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013404
13405 /* Remember which function I am compiling.
13406 * Also assume the last defined function is the main function.
13407 */
13408 state->main_function = def;
13409
13410 /* Now get the actual function definition */
13411 compound_statement(state, end);
13412
Eric Biederman153ea352003-06-20 14:43:20 +000013413 /* Finish anything unfinished with branches */
Eric Biederman90089602004-05-28 14:11:54 +000013414 resolve_branches(state, first);
Eric Biederman153ea352003-06-20 14:43:20 +000013415
Eric Biedermanb138ac82003-04-22 18:44:01 +000013416 /* Remove the parameter scope */
13417 end_scope(state);
Eric Biederman153ea352003-06-20 14:43:20 +000013418
Eric Biederman5ade04a2003-10-22 04:03:46 +000013419
13420 /* Remember I have defined a function */
13421 if (!state->functions) {
13422 state->functions = def;
13423 } else {
13424 insert_triple(state, state->functions, def);
13425 }
13426 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013427 FILE *fp = state->dbgout;
13428 fprintf(fp, "\n");
13429 loc(fp, state, 0);
13430 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13431 display_func(state, fp, def);
13432 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013433 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000013434
13435 return def;
13436}
13437
13438static struct triple *do_decl(struct compile_state *state,
13439 struct type *type, struct hash_entry *ident)
13440{
13441 struct triple *def;
13442 def = 0;
13443 /* Clean up the storage types used */
13444 switch (type->type & STOR_MASK) {
13445 case STOR_AUTO:
13446 case STOR_STATIC:
13447 /* These are the good types I am aiming for */
13448 break;
13449 case STOR_REGISTER:
13450 type->type &= ~STOR_MASK;
13451 type->type |= STOR_AUTO;
13452 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013453 case STOR_LOCAL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000013454 case STOR_EXTERN:
13455 type->type &= ~STOR_MASK;
13456 type->type |= STOR_STATIC;
13457 break;
13458 case STOR_TYPEDEF:
Eric Biederman0babc1c2003-05-09 02:39:00 +000013459 if (!ident) {
13460 error(state, 0, "typedef without name");
13461 }
13462 symbol(state, ident, &ident->sym_ident, 0, type);
13463 ident->tok = TOK_TYPE_NAME;
13464 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013465 break;
13466 default:
13467 internal_error(state, 0, "Undefined storage class");
13468 }
Eric Biederman3a51f3b2003-06-25 10:38:10 +000013469 if ((type->type & TYPE_MASK) == TYPE_FUNCTION) {
13470 error(state, 0, "Function prototypes not supported");
13471 }
Eric Biederman00443072003-06-24 12:34:45 +000013472 if (ident &&
13473 ((type->type & STOR_MASK) == STOR_STATIC) &&
Eric Biedermanb138ac82003-04-22 18:44:01 +000013474 ((type->type & QUAL_CONST) == 0)) {
13475 error(state, 0, "non const static variables not supported");
13476 }
13477 if (ident) {
13478 def = variable(state, type);
Eric Biederman90089602004-05-28 14:11:54 +000013479 var_symbol(state, ident, def);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013480 }
13481 return def;
13482}
13483
13484static void decl(struct compile_state *state, struct triple *first)
13485{
13486 struct type *base_type, *type;
13487 struct hash_entry *ident;
13488 struct triple *def;
13489 int global;
13490 global = (state->scope_depth <= GLOBAL_SCOPE_DEPTH);
13491 base_type = decl_specifiers(state);
13492 ident = 0;
13493 type = declarator(state, base_type, &ident, 0);
Eric Biederman90089602004-05-28 14:11:54 +000013494 type->type = attributes_opt(state, type->type);
Eric Biedermanb138ac82003-04-22 18:44:01 +000013495 if (global && ident && (peek(state) == TOK_LBRACE)) {
13496 /* function */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013497 type->type_ident = ident;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013498 state->function = ident->name;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013499 def = function_definition(state, type);
13500 symbol(state, ident, &ident->sym_ident, def, type);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000013501 state->function = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000013502 }
13503 else {
13504 int done;
13505 flatten(state, first, do_decl(state, type, ident));
13506 /* type or variable definition */
13507 do {
13508 done = 1;
13509 if (peek(state) == TOK_EQ) {
13510 if (!ident) {
13511 error(state, 0, "cannot assign to a type");
13512 }
13513 eat(state, TOK_EQ);
13514 flatten(state, first,
13515 init_expr(state,
13516 ident->sym_ident->def,
13517 initializer(state, type)));
13518 }
13519 arrays_complete(state, type);
13520 if (peek(state) == TOK_COMMA) {
13521 eat(state, TOK_COMMA);
13522 ident = 0;
13523 type = declarator(state, base_type, &ident, 0);
13524 flatten(state, first, do_decl(state, type, ident));
13525 done = 0;
13526 }
13527 } while(!done);
13528 eat(state, TOK_SEMI);
13529 }
13530}
13531
13532static void decls(struct compile_state *state)
13533{
13534 struct triple *list;
13535 int tok;
13536 list = label(state);
13537 while(1) {
13538 tok = peek(state);
13539 if (tok == TOK_EOF) {
13540 return;
13541 }
13542 if (tok == TOK_SPACE) {
13543 eat(state, TOK_SPACE);
13544 }
13545 decl(state, list);
13546 if (list->next != list) {
13547 error(state, 0, "global variables not supported");
13548 }
13549 }
13550}
13551
Eric Biederman5ade04a2003-10-22 04:03:46 +000013552/*
13553 * Function inlining
13554 */
Eric Biederman90089602004-05-28 14:11:54 +000013555struct triple_reg_set {
13556 struct triple_reg_set *next;
13557 struct triple *member;
13558 struct triple *new;
13559};
13560struct reg_block {
13561 struct block *block;
13562 struct triple_reg_set *in;
13563 struct triple_reg_set *out;
13564 int vertex;
13565};
13566static void setup_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13567static void analyze_basic_blocks(struct compile_state *state, struct basic_blocks *bb);
13568static void free_basic_blocks(struct compile_state *, struct basic_blocks *bb);
13569static int tdominates(struct compile_state *state, struct triple *dom, struct triple *sub);
13570static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
13571 void (*cb)(struct compile_state *state, struct block *block, void *arg),
13572 void *arg);
13573static void print_block(
13574 struct compile_state *state, struct block *block, void *arg);
13575static int do_triple_set(struct triple_reg_set **head,
13576 struct triple *member, struct triple *new_member);
13577static void do_triple_unset(struct triple_reg_set **head, struct triple *member);
13578static struct reg_block *compute_variable_lifetimes(
13579 struct compile_state *state, struct basic_blocks *bb);
13580static void free_variable_lifetimes(struct compile_state *state,
13581 struct basic_blocks *bb, struct reg_block *blocks);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013582#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000013583static void print_live_variables(struct compile_state *state,
13584 struct basic_blocks *bb, struct reg_block *rb, FILE *fp);
Stefan Reinauer50542a82007-10-24 11:14:14 +000013585#endif
Eric Biederman90089602004-05-28 14:11:54 +000013586
Eric Biederman5ade04a2003-10-22 04:03:46 +000013587
13588static struct triple *call(struct compile_state *state,
13589 struct triple *retvar, struct triple *ret_addr,
13590 struct triple *targ, struct triple *ret)
13591{
13592 struct triple *call;
13593
13594 if (!retvar || !is_lvalue(state, retvar)) {
13595 internal_error(state, 0, "writing to a non lvalue?");
13596 }
13597 write_compatible(state, retvar->type, &void_ptr_type);
13598
13599 call = new_triple(state, OP_CALL, &void_type, 1, 0);
13600 TARG(call, 0) = targ;
13601 MISC(call, 0) = ret;
13602 if (!targ || (targ->op != OP_LABEL)) {
13603 internal_error(state, 0, "call not to a label");
13604 }
13605 if (!ret || (ret->op != OP_RET)) {
13606 internal_error(state, 0, "call not matched with return");
13607 }
13608 return call;
13609}
13610
Eric Biederman5ade04a2003-10-22 04:03:46 +000013611static void walk_functions(struct compile_state *state,
13612 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13613 void *arg)
13614{
13615 struct triple *func, *first;
13616 func = first = state->functions;
13617 do {
13618 cb(state, func, arg);
13619 func = func->next;
13620 } while(func != first);
13621}
13622
Eric Biederman90089602004-05-28 14:11:54 +000013623static void reverse_walk_functions(struct compile_state *state,
13624 void (*cb)(struct compile_state *state, struct triple *func, void *arg),
13625 void *arg)
13626{
13627 struct triple *func, *first;
13628 func = first = state->functions;
13629 do {
13630 func = func->prev;
13631 cb(state, func, arg);
13632 } while(func != first);
13633}
13634
13635
13636static void mark_live(struct compile_state *state, struct triple *func, void *arg)
13637{
13638 struct triple *ptr, *first;
13639 if (func->u.cval == 0) {
13640 return;
13641 }
13642 ptr = first = RHS(func, 0);
13643 do {
13644 if (ptr->op == OP_FCALL) {
13645 struct triple *called_func;
13646 called_func = MISC(ptr, 0);
13647 /* Mark the called function as used */
13648 if (!(func->id & TRIPLE_FLAG_FLATTENED)) {
13649 called_func->u.cval++;
13650 }
13651 /* Remove the called function from the list */
13652 called_func->prev->next = called_func->next;
13653 called_func->next->prev = called_func->prev;
13654
13655 /* Place the called function before me on the list */
13656 called_func->next = func;
13657 called_func->prev = func->prev;
13658 called_func->prev->next = called_func;
13659 called_func->next->prev = called_func;
13660 }
13661 ptr = ptr->next;
13662 } while(ptr != first);
13663 func->id |= TRIPLE_FLAG_FLATTENED;
13664}
13665
13666static void mark_live_functions(struct compile_state *state)
13667{
13668 /* Ensure state->main_function is the last function in
13669 * the list of functions.
13670 */
13671 if ((state->main_function->next != state->functions) ||
13672 (state->functions->prev != state->main_function)) {
13673 internal_error(state, 0,
13674 "state->main_function is not at the end of the function list ");
13675 }
13676 state->main_function->u.cval = 1;
13677 reverse_walk_functions(state, mark_live, 0);
13678}
Eric Biederman5ade04a2003-10-22 04:03:46 +000013679
13680static int local_triple(struct compile_state *state,
13681 struct triple *func, struct triple *ins)
13682{
13683 int local = (ins->id & TRIPLE_FLAG_LOCAL);
13684#if 0
13685 if (!local) {
Eric Biederman90089602004-05-28 14:11:54 +000013686 FILE *fp = state->errout;
13687 fprintf(fp, "global: ");
13688 display_triple(fp, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013689 }
13690#endif
13691 return local;
13692}
13693
13694struct triple *copy_func(struct compile_state *state, struct triple *ofunc,
13695 struct occurance *base_occurance)
13696{
13697 struct triple *nfunc;
13698 struct triple *nfirst, *ofirst;
13699 struct triple *new, *old;
13700
13701 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013702 FILE *fp = state->dbgout;
13703 fprintf(fp, "\n");
13704 loc(fp, state, 0);
13705 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13706 display_func(state, fp, ofunc);
13707 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013708 }
13709
13710 /* Make a new copy of the old function */
13711 nfunc = triple(state, OP_LIST, ofunc->type, 0, 0);
13712 nfirst = 0;
13713 ofirst = old = RHS(ofunc, 0);
13714 do {
13715 struct triple *new;
13716 struct occurance *occurance;
13717 int old_lhs, old_rhs;
Eric Biederman90089602004-05-28 14:11:54 +000013718 old_lhs = old->lhs;
13719 old_rhs = old->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013720 occurance = inline_occurance(state, base_occurance, old->occurance);
13721 if (ofunc->u.cval && (old->op == OP_FCALL)) {
13722 MISC(old, 0)->u.cval += 1;
13723 }
13724 new = alloc_triple(state, old->op, old->type, old_lhs, old_rhs,
13725 occurance);
13726 if (!triple_stores_block(state, new)) {
13727 memcpy(&new->u, &old->u, sizeof(new->u));
13728 }
13729 if (!nfirst) {
13730 RHS(nfunc, 0) = nfirst = new;
13731 }
13732 else {
13733 insert_triple(state, nfirst, new);
13734 }
13735 new->id |= TRIPLE_FLAG_FLATTENED;
Eric Biederman90089602004-05-28 14:11:54 +000013736 new->id |= old->id & TRIPLE_FLAG_COPY;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013737
13738 /* During the copy remember new as user of old */
13739 use_triple(old, new);
13740
Eric Biederman5ade04a2003-10-22 04:03:46 +000013741 /* Remember which instructions are local */
13742 old->id |= TRIPLE_FLAG_LOCAL;
13743 old = old->next;
13744 } while(old != ofirst);
13745
13746 /* Make a second pass to fix up any unresolved references */
13747 old = ofirst;
13748 new = nfirst;
13749 do {
13750 struct triple **oexpr, **nexpr;
13751 int count, i;
13752 /* Lookup where the copy is, to join pointers */
Eric Biederman90089602004-05-28 14:11:54 +000013753 count = TRIPLE_SIZE(old);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013754 for(i = 0; i < count; i++) {
13755 oexpr = &old->param[i];
13756 nexpr = &new->param[i];
13757 if (*oexpr && !*nexpr) {
13758 if (!local_triple(state, ofunc, *oexpr)) {
13759 *nexpr = *oexpr;
13760 }
13761 else if ((*oexpr)->use) {
13762 *nexpr = (*oexpr)->use->member;
13763 }
13764 if (*nexpr == old) {
13765 internal_error(state, 0, "new == old?");
13766 }
13767 use_triple(*nexpr, new);
13768 }
13769 if (!*nexpr && *oexpr) {
Eric Biederman90089602004-05-28 14:11:54 +000013770 internal_error(state, 0, "Could not copy %d", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013771 }
13772 }
13773 old = old->next;
13774 new = new->next;
13775 } while((old != ofirst) && (new != nfirst));
13776
13777 /* Make a third pass to cleanup the extra useses */
13778 old = ofirst;
13779 new = nfirst;
13780 do {
13781 unuse_triple(old, new);
13782 /* Forget which instructions are local */
13783 old->id &= ~TRIPLE_FLAG_LOCAL;
13784 old = old->next;
13785 new = new->next;
13786 } while ((old != ofirst) && (new != nfirst));
13787 return nfunc;
13788}
13789
Eric Biederman90089602004-05-28 14:11:54 +000013790static void expand_inline_call(
13791 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000013792{
13793 /* Inline the function call */
13794 struct type *ptype;
Eric Biederman90089602004-05-28 14:11:54 +000013795 struct triple *ofunc, *nfunc, *nfirst, *result, *retvar, *ins;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013796 struct triple *end, *nend;
13797 int pvals, i;
13798
13799 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000013800 ofunc = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013801 if (ofunc->op != OP_LIST) {
13802 internal_error(state, 0, "improper function");
13803 }
Eric Biederman90089602004-05-28 14:11:54 +000013804 nfunc = copy_func(state, ofunc, fcall->occurance);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013805 /* Prepend the parameter reading into the new function list */
13806 ptype = nfunc->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000013807 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013808 for(i = 0; i < pvals; i++) {
13809 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000013810 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013811 atype = ptype;
13812 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
13813 atype = ptype->left;
13814 }
Eric Biederman90089602004-05-28 14:11:54 +000013815 param = farg(state, nfunc, i);
13816 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
13817 internal_error(state, fcall, "param %d type mismatch", i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013818 }
Eric Biederman90089602004-05-28 14:11:54 +000013819 arg = RHS(fcall, i);
13820 flatten(state, fcall, write_expr(state, param, arg));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013821 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013822 }
13823 result = 0;
13824 if ((nfunc->type->left->type & TYPE_MASK) != TYPE_VOID) {
Eric Biederman90089602004-05-28 14:11:54 +000013825 result = read_expr(state,
13826 deref_index(state, fresult(state, nfunc), 1));
Eric Biederman5ade04a2003-10-22 04:03:46 +000013827 }
13828 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000013829 FILE *fp = state->dbgout;
13830 fprintf(fp, "\n");
13831 loc(fp, state, 0);
13832 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
13833 display_func(state, fp, nfunc);
13834 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
Eric Biederman5ade04a2003-10-22 04:03:46 +000013835 }
13836
Eric Biederman90089602004-05-28 14:11:54 +000013837 /*
13838 * Get rid of the extra triples
13839 */
13840 /* Remove the read of the return address */
13841 ins = RHS(nfunc, 0)->prev->prev;
13842 if ((ins->op != OP_READ) || (RHS(ins, 0) != fretaddr(state, nfunc))) {
13843 internal_error(state, ins, "Not return addres read?");
13844 }
13845 release_triple(state, ins);
13846 /* Remove the return instruction */
13847 ins = RHS(nfunc, 0)->prev;
13848 if (ins->op != OP_RET) {
13849 internal_error(state, ins, "Not return?");
13850 }
13851 release_triple(state, ins);
13852 /* Remove the retaddres variable */
13853 retvar = fretaddr(state, nfunc);
13854 if ((retvar->lhs != 1) ||
13855 (retvar->op != OP_ADECL) ||
13856 (retvar->next->op != OP_PIECE) ||
13857 (MISC(retvar->next, 0) != retvar)) {
13858 internal_error(state, retvar, "Not the return address?");
13859 }
13860 release_triple(state, retvar->next);
13861 release_triple(state, retvar);
13862
13863 /* Remove the label at the start of the function */
13864 ins = RHS(nfunc, 0);
13865 if (ins->op != OP_LABEL) {
13866 internal_error(state, ins, "Not label?");
13867 }
13868 nfirst = ins->next;
13869 free_triple(state, ins);
13870 /* Release the new function header */
Eric Biederman5ade04a2003-10-22 04:03:46 +000013871 RHS(nfunc, 0) = 0;
13872 free_triple(state, nfunc);
13873
13874 /* Append the new function list onto the return list */
Eric Biederman90089602004-05-28 14:11:54 +000013875 end = fcall->prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013876 nend = nfirst->prev;
13877 end->next = nfirst;
13878 nfirst->prev = end;
Eric Biederman90089602004-05-28 14:11:54 +000013879 nend->next = fcall;
13880 fcall->prev = nend;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013881
Eric Biederman90089602004-05-28 14:11:54 +000013882 /* Now the result reading code */
13883 if (result) {
13884 result = flatten(state, fcall, result);
13885 propogate_use(state, fcall, result);
13886 }
13887
13888 /* Release the original fcall instruction */
13889 release_triple(state, fcall);
13890
13891 return;
Eric Biederman5ade04a2003-10-22 04:03:46 +000013892}
13893
Eric Biederman90089602004-05-28 14:11:54 +000013894/*
13895 *
13896 * Type of the result variable.
13897 *
13898 * result
13899 * |
13900 * +----------+------------+
13901 * | |
13902 * union of closures result_type
13903 * |
13904 * +------------------+---------------+
13905 * | |
13906 * closure1 ... closuerN
13907 * | |
13908 * +----+--+-+--------+-----+ +----+----+---+-----+
13909 * | | | | | | | | |
13910 * var1 var2 var3 ... varN result var1 var2 ... varN result
13911 * |
13912 * +--------+---------+
13913 * | |
13914 * union of closures result_type
13915 * |
13916 * +-----+-------------------+
13917 * | |
13918 * closure1 ... closureN
13919 * | |
13920 * +-----+---+----+----+ +----+---+----+-----+
13921 * | | | | | | | |
13922 * var1 var2 ... varN result var1 var2 ... varN result
13923 */
13924
13925static int add_closure_type(struct compile_state *state,
13926 struct triple *func, struct type *closure_type)
13927{
13928 struct type *type, *ctype, **next;
13929 struct triple *var, *new_var;
13930 int i;
13931
13932#if 0
13933 FILE *fp = state->errout;
13934 fprintf(fp, "original_type: ");
13935 name_of(fp, fresult(state, func)->type);
13936 fprintf(fp, "\n");
13937#endif
13938 /* find the original type */
13939 var = fresult(state, func);
13940 type = var->type;
13941 if (type->elements != 2) {
13942 internal_error(state, var, "bad return type");
13943 }
13944
13945 /* Find the complete closure type and update it */
13946 ctype = type->left->left;
13947 next = &ctype->left;
13948 while(((*next)->type & TYPE_MASK) == TYPE_OVERLAP) {
13949 next = &(*next)->right;
13950 }
13951 *next = new_type(TYPE_OVERLAP, *next, dup_type(state, closure_type));
13952 ctype->elements += 1;
13953
13954#if 0
13955 fprintf(fp, "new_type: ");
13956 name_of(fp, type);
13957 fprintf(fp, "\n");
13958 fprintf(fp, "ctype: %p %d bits: %d ",
13959 ctype, ctype->elements, reg_size_of(state, ctype));
13960 name_of(fp, ctype);
13961 fprintf(fp, "\n");
13962#endif
13963
13964 /* Regenerate the variable with the new type definition */
13965 new_var = pre_triple(state, var, OP_ADECL, type, 0, 0);
13966 new_var->id |= TRIPLE_FLAG_FLATTENED;
13967 for(i = 0; i < new_var->lhs; i++) {
13968 LHS(new_var, i)->id |= TRIPLE_FLAG_FLATTENED;
13969 }
13970
13971 /* Point everyone at the new variable */
13972 propogate_use(state, var, new_var);
13973
13974 /* Release the original variable */
13975 for(i = 0; i < var->lhs; i++) {
13976 release_triple(state, LHS(var, i));
13977 }
13978 release_triple(state, var);
13979
13980 /* Return the index of the added closure type */
13981 return ctype->elements - 1;
13982}
13983
13984static struct triple *closure_expr(struct compile_state *state,
13985 struct triple *func, int closure_idx, int var_idx)
13986{
13987 return deref_index(state,
13988 deref_index(state,
13989 deref_index(state, fresult(state, func), 0),
13990 closure_idx),
13991 var_idx);
13992}
13993
13994
13995static void insert_triple_set(
13996 struct triple_reg_set **head, struct triple *member)
13997{
13998 struct triple_reg_set *new;
13999 new = xcmalloc(sizeof(*new), "triple_set");
14000 new->member = member;
14001 new->new = 0;
14002 new->next = *head;
14003 *head = new;
14004}
14005
14006static int ordered_triple_set(
14007 struct triple_reg_set **head, struct triple *member)
14008{
14009 struct triple_reg_set **ptr;
14010 if (!member)
14011 return 0;
14012 ptr = head;
14013 while(*ptr) {
14014 if (member == (*ptr)->member) {
14015 return 0;
14016 }
14017 /* keep the list ordered */
14018 if (member->id < (*ptr)->member->id) {
14019 break;
14020 }
14021 ptr = &(*ptr)->next;
14022 }
14023 insert_triple_set(ptr, member);
14024 return 1;
14025}
14026
14027
14028static void free_closure_variables(struct compile_state *state,
14029 struct triple_reg_set **enclose)
14030{
14031 struct triple_reg_set *entry, *next;
14032 for(entry = *enclose; entry; entry = next) {
14033 next = entry->next;
14034 do_triple_unset(enclose, entry->member);
14035 }
14036}
14037
14038static int lookup_closure_index(struct compile_state *state,
14039 struct triple *me, struct triple *val)
14040{
14041 struct triple *first, *ins, *next;
14042 first = RHS(me, 0);
14043 ins = next = first;
14044 do {
14045 struct triple *result;
14046 struct triple *index0, *index1, *index2, *read, *write;
14047 ins = next;
14048 next = ins->next;
14049 if (ins->op != OP_CALL) {
14050 continue;
14051 }
14052 /* I am at a previous call point examine it closely */
14053 if (ins->next->op != OP_LABEL) {
14054 internal_error(state, ins, "call not followed by label");
14055 }
14056 /* Does this call does not enclose any variables? */
14057 if ((ins->next->next->op != OP_INDEX) ||
14058 (ins->next->next->u.cval != 0) ||
14059 (result = MISC(ins->next->next, 0)) ||
14060 (result->id & TRIPLE_FLAG_LOCAL)) {
14061 continue;
14062 }
14063 index0 = ins->next->next;
14064 /* The pattern is:
14065 * 0 index result < 0 >
14066 * 1 index 0 < ? >
14067 * 2 index 1 < ? >
14068 * 3 read 2
14069 * 4 write 3 var
14070 */
14071 for(index0 = ins->next->next;
14072 (index0->op == OP_INDEX) &&
14073 (MISC(index0, 0) == result) &&
14074 (index0->u.cval == 0) ;
14075 index0 = write->next)
14076 {
14077 index1 = index0->next;
14078 index2 = index1->next;
14079 read = index2->next;
14080 write = read->next;
14081 if ((index0->op != OP_INDEX) ||
14082 (index1->op != OP_INDEX) ||
14083 (index2->op != OP_INDEX) ||
14084 (read->op != OP_READ) ||
14085 (write->op != OP_WRITE) ||
14086 (MISC(index1, 0) != index0) ||
14087 (MISC(index2, 0) != index1) ||
14088 (RHS(read, 0) != index2) ||
14089 (RHS(write, 0) != read)) {
14090 internal_error(state, index0, "bad var read");
14091 }
14092 if (MISC(write, 0) == val) {
14093 return index2->u.cval;
14094 }
14095 }
14096 } while(next != first);
14097 return -1;
14098}
14099
14100static inline int enclose_triple(struct triple *ins)
14101{
14102 return (ins && ((ins->type->type & TYPE_MASK) != TYPE_VOID));
14103}
14104
14105static void compute_closure_variables(struct compile_state *state,
14106 struct triple *me, struct triple *fcall, struct triple_reg_set **enclose)
14107{
14108 struct triple_reg_set *set, *vars, **last_var;
14109 struct basic_blocks bb;
14110 struct reg_block *rb;
14111 struct block *block;
14112 struct triple *old_result, *first, *ins;
14113 size_t count, idx;
14114 unsigned long used_indicies;
14115 int i, max_index;
14116#define MAX_INDICIES (sizeof(used_indicies)*CHAR_BIT)
14117#define ID_BITS(X) ((X) & (TRIPLE_FLAG_LOCAL -1))
14118 struct {
14119 unsigned id;
14120 int index;
14121 } *info;
14122
14123
14124 /* Find the basic blocks of this function */
14125 bb.func = me;
14126 bb.first = RHS(me, 0);
14127 old_result = 0;
14128 if (!triple_is_ret(state, bb.first->prev)) {
14129 bb.func = 0;
14130 } else {
14131 old_result = fresult(state, me);
14132 }
14133 analyze_basic_blocks(state, &bb);
14134
14135 /* Find which variables are currently alive in a given block */
14136 rb = compute_variable_lifetimes(state, &bb);
14137
14138 /* Find the variables that are currently alive */
14139 block = block_of_triple(state, fcall);
14140 if (!block || (block->vertex <= 0) || (block->vertex > bb.last_vertex)) {
14141 internal_error(state, fcall, "No reg block? block: %p", block);
14142 }
14143
14144#if DEBUG_EXPLICIT_CLOSURES
14145 print_live_variables(state, &bb, rb, state->dbgout);
14146 fflush(state->dbgout);
14147#endif
14148
14149 /* Count the number of triples in the function */
14150 first = RHS(me, 0);
14151 ins = first;
14152 count = 0;
14153 do {
14154 count++;
14155 ins = ins->next;
14156 } while(ins != first);
14157
14158 /* Allocate some memory to temorary hold the id info */
14159 info = xcmalloc(sizeof(*info) * (count +1), "info");
14160
14161 /* Mark the local function */
14162 first = RHS(me, 0);
14163 ins = first;
14164 idx = 1;
14165 do {
14166 info[idx].id = ins->id;
14167 ins->id = TRIPLE_FLAG_LOCAL | idx;
14168 idx++;
14169 ins = ins->next;
14170 } while(ins != first);
14171
14172 /*
14173 * Build the list of variables to enclose.
14174 *
14175 * A target it to put the same variable in the
14176 * same slot for ever call of a given function.
14177 * After coloring this removes all of the variable
14178 * manipulation code.
14179 *
14180 * The list of variables to enclose is built ordered
14181 * program order because except in corner cases this
14182 * gives me the stability of assignment I need.
14183 *
14184 * To gurantee that stability I lookup the variables
14185 * to see where they have been used before and
14186 * I build my final list with the assigned indicies.
14187 */
14188 vars = 0;
14189 if (enclose_triple(old_result)) {
14190 ordered_triple_set(&vars, old_result);
14191 }
14192 for(set = rb[block->vertex].out; set; set = set->next) {
14193 if (!enclose_triple(set->member)) {
14194 continue;
14195 }
14196 if ((set->member == fcall) || (set->member == old_result)) {
14197 continue;
14198 }
14199 if (!local_triple(state, me, set->member)) {
14200 internal_error(state, set->member, "not local?");
14201 }
14202 ordered_triple_set(&vars, set->member);
14203 }
14204
14205 /* Lookup the current indicies of the live varialbe */
14206 used_indicies = 0;
14207 max_index = -1;
14208 for(set = vars; set ; set = set->next) {
14209 struct triple *ins;
14210 int index;
14211 ins = set->member;
14212 index = lookup_closure_index(state, me, ins);
14213 info[ID_BITS(ins->id)].index = index;
14214 if (index < 0) {
14215 continue;
14216 }
14217 if (index >= MAX_INDICIES) {
14218 internal_error(state, ins, "index unexpectedly large");
14219 }
14220 if (used_indicies & (1 << index)) {
14221 internal_error(state, ins, "index previously used?");
14222 }
14223 /* Remember which indicies have been used */
14224 used_indicies |= (1 << index);
14225 if (index > max_index) {
14226 max_index = index;
14227 }
14228 }
14229
14230 /* Walk through the live variables and make certain
14231 * everything is assigned an index.
14232 */
14233 for(set = vars; set; set = set->next) {
14234 struct triple *ins;
14235 int index;
14236 ins = set->member;
14237 index = info[ID_BITS(ins->id)].index;
14238 if (index >= 0) {
14239 continue;
14240 }
14241 /* Find the lowest unused index value */
14242 for(index = 0; index < MAX_INDICIES; index++) {
14243 if (!(used_indicies & (1 << index))) {
14244 break;
14245 }
14246 }
14247 if (index == MAX_INDICIES) {
14248 internal_error(state, ins, "no free indicies?");
14249 }
14250 info[ID_BITS(ins->id)].index = index;
14251 /* Remember which indicies have been used */
14252 used_indicies |= (1 << index);
14253 if (index > max_index) {
14254 max_index = index;
14255 }
14256 }
14257
14258 /* Build the return list of variables with positions matching
14259 * their indicies.
14260 */
14261 *enclose = 0;
14262 last_var = enclose;
14263 for(i = 0; i <= max_index; i++) {
14264 struct triple *var;
14265 var = 0;
14266 if (used_indicies & (1 << i)) {
14267 for(set = vars; set; set = set->next) {
14268 int index;
14269 index = info[ID_BITS(set->member->id)].index;
14270 if (index == i) {
14271 var = set->member;
14272 break;
14273 }
14274 }
14275 if (!var) {
14276 internal_error(state, me, "missing variable");
14277 }
14278 }
14279 insert_triple_set(last_var, var);
14280 last_var = &(*last_var)->next;
14281 }
14282
14283#if DEBUG_EXPLICIT_CLOSURES
14284 /* Print out the variables to be enclosed */
14285 loc(state->dbgout, state, fcall);
14286 fprintf(state->dbgout, "Alive: \n");
14287 for(set = *enclose; set; set = set->next) {
14288 display_triple(state->dbgout, set->member);
14289 }
14290 fflush(state->dbgout);
14291#endif
14292
14293 /* Clear the marks */
14294 ins = first;
14295 do {
14296 ins->id = info[ID_BITS(ins->id)].id;
14297 ins = ins->next;
14298 } while(ins != first);
14299
14300 /* Release the ordered list of live variables */
14301 free_closure_variables(state, &vars);
14302
14303 /* Release the storage of the old ids */
14304 xfree(info);
14305
14306 /* Release the variable lifetime information */
14307 free_variable_lifetimes(state, &bb, rb);
14308
14309 /* Release the basic blocks of this function */
14310 free_basic_blocks(state, &bb);
14311}
14312
14313static void expand_function_call(
14314 struct compile_state *state, struct triple *me, struct triple *fcall)
Eric Biederman5ade04a2003-10-22 04:03:46 +000014315{
14316 /* Generate an ordinary function call */
Eric Biederman90089602004-05-28 14:11:54 +000014317 struct type *closure_type, **closure_next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014318 struct triple *func, *func_first, *func_last, *retvar;
Eric Biederman90089602004-05-28 14:11:54 +000014319 struct triple *first;
14320 struct type *ptype, *rtype;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014321 struct triple *jmp;
14322 struct triple *ret_addr, *ret_loc, *ret_set;
Eric Biederman90089602004-05-28 14:11:54 +000014323 struct triple_reg_set *enclose, *set;
14324 int closure_idx, pvals, i;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014325
Eric Biederman90089602004-05-28 14:11:54 +000014326#if DEBUG_EXPLICIT_CLOSURES
14327 FILE *fp = state->dbgout;
14328 fprintf(fp, "\ndisplay_func(me) ptr: %p\n", fcall);
14329 display_func(state, fp, MISC(fcall, 0));
14330 display_func(state, fp, me);
14331 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14332#endif
14333
Eric Biederman5ade04a2003-10-22 04:03:46 +000014334 /* Find the triples */
Eric Biederman90089602004-05-28 14:11:54 +000014335 func = MISC(fcall, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014336 func_first = RHS(func, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014337 retvar = fretaddr(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014338 func_last = func_first->prev;
Eric Biederman90089602004-05-28 14:11:54 +000014339 first = fcall->next;
14340
14341 /* Find what I need to enclose */
14342 compute_closure_variables(state, me, fcall, &enclose);
14343
14344 /* Compute the closure type */
14345 closure_type = new_type(TYPE_TUPLE, 0, 0);
14346 closure_type->elements = 0;
14347 closure_next = &closure_type->left;
14348 for(set = enclose; set ; set = set->next) {
14349 struct type *type;
14350 type = &void_type;
14351 if (set->member) {
14352 type = set->member->type;
14353 }
14354 if (!*closure_next) {
14355 *closure_next = type;
14356 } else {
14357 *closure_next = new_type(TYPE_PRODUCT, *closure_next,
14358 type);
14359 closure_next = &(*closure_next)->right;
14360 }
14361 closure_type->elements += 1;
14362 }
14363 if (closure_type->elements == 0) {
14364 closure_type->type = TYPE_VOID;
14365 }
14366
14367
14368#if DEBUG_EXPLICIT_CLOSURES
14369 fprintf(state->dbgout, "closure type: ");
14370 name_of(state->dbgout, closure_type);
14371 fprintf(state->dbgout, "\n");
14372#endif
14373
14374 /* Update the called functions closure variable */
14375 closure_idx = add_closure_type(state, func, closure_type);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014376
14377 /* Generate some needed triples */
14378 ret_loc = label(state);
14379 ret_addr = triple(state, OP_ADDRCONST, &void_ptr_type, ret_loc, 0);
14380
14381 /* Pass the parameters to the new function */
14382 ptype = func->type->right;
Eric Biederman90089602004-05-28 14:11:54 +000014383 pvals = fcall->rhs;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014384 for(i = 0; i < pvals; i++) {
14385 struct type *atype;
Eric Biederman90089602004-05-28 14:11:54 +000014386 struct triple *arg, *param;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014387 atype = ptype;
14388 if ((ptype->type & TYPE_MASK) == TYPE_PRODUCT) {
14389 atype = ptype->left;
14390 }
Eric Biederman90089602004-05-28 14:11:54 +000014391 param = farg(state, func, i);
14392 if ((param->type->type & TYPE_MASK) != (atype->type & TYPE_MASK)) {
14393 internal_error(state, fcall, "param type mismatch");
Eric Biederman5ade04a2003-10-22 04:03:46 +000014394 }
Eric Biederman90089602004-05-28 14:11:54 +000014395 arg = RHS(fcall, i);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014396 flatten(state, first, write_expr(state, param, arg));
14397 ptype = ptype->right;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014398 }
Eric Biederman90089602004-05-28 14:11:54 +000014399 rtype = func->type->left;
14400
Eric Biederman5ade04a2003-10-22 04:03:46 +000014401 /* Thread the triples together */
14402 ret_loc = flatten(state, first, ret_loc);
Eric Biederman90089602004-05-28 14:11:54 +000014403
14404 /* Save the active variables in the result variable */
14405 for(i = 0, set = enclose; set ; set = set->next, i++) {
14406 if (!set->member) {
14407 continue;
14408 }
14409 flatten(state, ret_loc,
14410 write_expr(state,
14411 closure_expr(state, func, closure_idx, i),
14412 read_expr(state, set->member)));
14413 }
14414
14415 /* Initialize the return value */
14416 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14417 flatten(state, ret_loc,
14418 write_expr(state,
14419 deref_index(state, fresult(state, func), 1),
14420 new_triple(state, OP_UNKNOWNVAL, rtype, 0, 0)));
14421 }
14422
Eric Biederman5ade04a2003-10-22 04:03:46 +000014423 ret_addr = flatten(state, ret_loc, ret_addr);
14424 ret_set = flatten(state, ret_loc, write_expr(state, retvar, ret_addr));
14425 jmp = flatten(state, ret_loc,
14426 call(state, retvar, ret_addr, func_first, func_last));
14427
Eric Biederman7dea9552004-06-29 05:38:37 +000014428 /* Find the result */
14429 if ((rtype->type & TYPE_MASK) != TYPE_VOID) {
14430 struct triple * result;
14431 result = flatten(state, first,
14432 read_expr(state,
14433 deref_index(state, fresult(state, func), 1)));
14434
14435 propogate_use(state, fcall, result);
14436 }
14437
14438 /* Release the original fcall instruction */
14439 release_triple(state, fcall);
14440
Eric Biederman90089602004-05-28 14:11:54 +000014441 /* Restore the active variables from the result variable */
14442 for(i = 0, set = enclose; set ; set = set->next, i++) {
14443 struct triple_set *use, *next;
14444 struct triple *new;
Eric Biederman7dea9552004-06-29 05:38:37 +000014445 struct basic_blocks bb;
Eric Biederman90089602004-05-28 14:11:54 +000014446 if (!set->member || (set->member == fcall)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000014447 continue;
14448 }
Eric Biederman90089602004-05-28 14:11:54 +000014449 /* Generate an expression for the value */
14450 new = flatten(state, first,
14451 read_expr(state,
14452 closure_expr(state, func, closure_idx, i)));
14453
14454
14455 /* If the original is an lvalue restore the preserved value */
14456 if (is_lvalue(state, set->member)) {
14457 flatten(state, first,
14458 write_expr(state, set->member, new));
14459 continue;
14460 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014461 /*
14462 * If the original is a value update the dominated uses.
14463 */
14464
14465 /* Analyze the basic blocks so I can see who dominates whom */
14466 bb.func = me;
14467 bb.first = RHS(me, 0);
14468 if (!triple_is_ret(state, bb.first->prev)) {
14469 bb.func = 0;
14470 }
14471 analyze_basic_blocks(state, &bb);
14472
Eric Biederman90089602004-05-28 14:11:54 +000014473
14474#if DEBUG_EXPLICIT_CLOSURES
14475 fprintf(state->errout, "Updating domindated uses: %p -> %p\n",
14476 set->member, new);
14477#endif
14478 /* If fcall dominates the use update the expression */
14479 for(use = set->member->use; use; use = next) {
14480 /* Replace use modifies the use chain and
14481 * removes use, so I must take a copy of the
14482 * next entry early.
14483 */
14484 next = use->next;
14485 if (!tdominates(state, fcall, use->member)) {
14486 continue;
14487 }
14488 replace_use(state, set->member, new, use->member);
14489 }
Eric Biederman7dea9552004-06-29 05:38:37 +000014490
14491 /* Release the basic blocks, the instructions will be
14492 * different next time, and flatten/insert_triple does
14493 * not update the block values so I can't cache the analysis.
14494 */
14495 free_basic_blocks(state, &bb);
Eric Biederman90089602004-05-28 14:11:54 +000014496 }
14497
Eric Biederman90089602004-05-28 14:11:54 +000014498 /* Release the closure variable list */
14499 free_closure_variables(state, &enclose);
14500
14501 if (state->compiler->debug & DEBUG_INLINE) {
14502 FILE *fp = state->dbgout;
14503 fprintf(fp, "\n");
14504 loc(fp, state, 0);
14505 fprintf(fp, "\n__________ %s _________\n", __FUNCTION__);
14506 display_func(state, fp, func);
14507 display_func(state, fp, me);
14508 fprintf(fp, "__________ %s _________ done\n\n", __FUNCTION__);
14509 }
14510
14511 return;
14512}
14513
14514static int do_inline(struct compile_state *state, struct triple *func)
14515{
14516 int do_inline;
14517 int policy;
14518
14519 policy = state->compiler->flags & COMPILER_INLINE_MASK;
14520 switch(policy) {
14521 case COMPILER_INLINE_ALWAYS:
14522 do_inline = 1;
14523 if (func->type->type & ATTRIB_NOINLINE) {
14524 error(state, func, "noinline with always_inline compiler option");
14525 }
14526 break;
14527 case COMPILER_INLINE_NEVER:
14528 do_inline = 0;
14529 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14530 error(state, func, "always_inline with noinline compiler option");
14531 }
14532 break;
14533 case COMPILER_INLINE_DEFAULTON:
14534 switch(func->type->type & STOR_MASK) {
14535 case STOR_STATIC | STOR_INLINE:
14536 case STOR_LOCAL | STOR_INLINE:
14537 case STOR_EXTERN | STOR_INLINE:
14538 do_inline = 1;
14539 break;
14540 default:
14541 do_inline = 1;
14542 break;
14543 }
14544 break;
14545 case COMPILER_INLINE_DEFAULTOFF:
14546 switch(func->type->type & STOR_MASK) {
14547 case STOR_STATIC | STOR_INLINE:
14548 case STOR_LOCAL | STOR_INLINE:
14549 case STOR_EXTERN | STOR_INLINE:
14550 do_inline = 1;
14551 break;
14552 default:
14553 do_inline = 0;
14554 break;
14555 }
14556 break;
14557 case COMPILER_INLINE_NOPENALTY:
Eric Biederman5ade04a2003-10-22 04:03:46 +000014558 switch(func->type->type & STOR_MASK) {
14559 case STOR_STATIC | STOR_INLINE:
14560 case STOR_LOCAL | STOR_INLINE:
14561 case STOR_EXTERN | STOR_INLINE:
14562 do_inline = 1;
14563 break;
14564 default:
14565 do_inline = (func->u.cval == 1);
14566 break;
14567 }
Eric Biederman90089602004-05-28 14:11:54 +000014568 break;
14569 default:
14570 do_inline = 0;
14571 internal_error(state, 0, "Unimplemented inline policy");
14572 break;
14573 }
14574 /* Force inlining */
14575 if (func->type->type & ATTRIB_NOINLINE) {
14576 do_inline = 0;
14577 }
14578 if (func->type->type & ATTRIB_ALWAYS_INLINE) {
14579 do_inline = 1;
14580 }
14581 return do_inline;
14582}
Eric Biederman5ade04a2003-10-22 04:03:46 +000014583
Eric Biederman90089602004-05-28 14:11:54 +000014584static void inline_function(struct compile_state *state, struct triple *me, void *arg)
14585{
14586 struct triple *first, *ptr, *next;
14587 /* If the function is not used don't bother */
14588 if (me->u.cval <= 0) {
14589 return;
14590 }
14591 if (state->compiler->debug & DEBUG_CALLS2) {
14592 FILE *fp = state->dbgout;
14593 fprintf(fp, "in: %s\n",
14594 me->type->type_ident->name);
14595 }
14596
14597 first = RHS(me, 0);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014598 ptr = next = first;
14599 do {
Eric Biederman90089602004-05-28 14:11:54 +000014600 struct triple *func, *prev;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014601 ptr = next;
14602 prev = ptr->prev;
14603 next = ptr->next;
14604 if (ptr->op != OP_FCALL) {
14605 continue;
14606 }
14607 func = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000014608 /* See if the function should be inlined */
14609 if (!do_inline(state, func)) {
14610 /* Put a label after the fcall */
14611 post_triple(state, ptr, OP_LABEL, &void_type, 0, 0);
14612 continue;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014613 }
Eric Biederman90089602004-05-28 14:11:54 +000014614 if (state->compiler->debug & DEBUG_CALLS) {
14615 FILE *fp = state->dbgout;
14616 if (state->compiler->debug & DEBUG_CALLS2) {
14617 loc(fp, state, ptr);
14618 }
14619 fprintf(fp, "inlining %s\n",
14620 func->type->type_ident->name);
14621 fflush(fp);
14622 }
14623
14624 /* Update the function use counts */
14625 func->u.cval -= 1;
14626
14627 /* Replace the fcall with the called function */
14628 expand_inline_call(state, me, ptr);
14629
14630 next = prev->next;
14631 } while (next != first);
14632
14633 ptr = next = first;
14634 do {
14635 struct triple *prev, *func;
14636 ptr = next;
14637 prev = ptr->prev;
14638 next = ptr->next;
14639 if (ptr->op != OP_FCALL) {
14640 continue;
14641 }
14642 func = MISC(ptr, 0);
14643 if (state->compiler->debug & DEBUG_CALLS) {
14644 FILE *fp = state->dbgout;
14645 if (state->compiler->debug & DEBUG_CALLS2) {
14646 loc(fp, state, ptr);
14647 }
14648 fprintf(fp, "calling %s\n",
14649 func->type->type_ident->name);
14650 fflush(fp);
14651 }
14652 /* Replace the fcall with the instruction sequence
14653 * needed to make the call.
14654 */
14655 expand_function_call(state, me, ptr);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014656 next = prev->next;
14657 } while(next != first);
14658}
Eric Biederman90089602004-05-28 14:11:54 +000014659
14660static void inline_functions(struct compile_state *state, struct triple *func)
14661{
14662 inline_function(state, func, 0);
14663 reverse_walk_functions(state, inline_function, 0);
14664}
14665
Eric Biederman5ade04a2003-10-22 04:03:46 +000014666static void insert_function(struct compile_state *state,
14667 struct triple *func, void *arg)
14668{
14669 struct triple *first, *end, *ffirst, *fend;
14670
14671 if (state->compiler->debug & DEBUG_INLINE) {
Eric Biederman90089602004-05-28 14:11:54 +000014672 FILE *fp = state->errout;
14673 fprintf(fp, "%s func count: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000014674 func->type->type_ident->name, func->u.cval);
14675 }
14676 if (func->u.cval == 0) {
14677 return;
14678 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000014679
14680 /* Find the end points of the lists */
14681 first = arg;
14682 end = first->prev;
14683 ffirst = RHS(func, 0);
14684 fend = ffirst->prev;
14685
14686 /* splice the lists together */
14687 end->next = ffirst;
14688 ffirst->prev = end;
14689 fend->next = first;
14690 first->prev = fend;
14691}
14692
Eric Biederman90089602004-05-28 14:11:54 +000014693struct triple *input_asm(struct compile_state *state)
14694{
14695 struct asm_info *info;
14696 struct triple *def;
14697 int i, out;
14698
14699 info = xcmalloc(sizeof(*info), "asm_info");
14700 info->str = "";
14701
14702 out = sizeof(arch_input_regs)/sizeof(arch_input_regs[0]);
14703 memcpy(&info->tmpl.lhs, arch_input_regs, sizeof(arch_input_regs));
14704
14705 def = new_triple(state, OP_ASM, &void_type, out, 0);
14706 def->u.ainfo = info;
14707 def->id |= TRIPLE_FLAG_VOLATILE;
14708
14709 for(i = 0; i < out; i++) {
14710 struct triple *piece;
14711 piece = triple(state, OP_PIECE, &int_type, def, 0);
14712 piece->u.cval = i;
14713 LHS(def, i) = piece;
14714 }
14715
14716 return def;
14717}
14718
14719struct triple *output_asm(struct compile_state *state)
14720{
14721 struct asm_info *info;
14722 struct triple *def;
14723 int in;
14724
14725 info = xcmalloc(sizeof(*info), "asm_info");
14726 info->str = "";
14727
14728 in = sizeof(arch_output_regs)/sizeof(arch_output_regs[0]);
14729 memcpy(&info->tmpl.rhs, arch_output_regs, sizeof(arch_output_regs));
14730
14731 def = new_triple(state, OP_ASM, &void_type, 0, in);
14732 def->u.ainfo = info;
14733 def->id |= TRIPLE_FLAG_VOLATILE;
14734
14735 return def;
14736}
14737
Eric Biederman5ade04a2003-10-22 04:03:46 +000014738static void join_functions(struct compile_state *state)
14739{
Eric Biederman90089602004-05-28 14:11:54 +000014740 struct triple *jmp, *start, *end, *call, *in, *out, *func;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014741 struct file_state file;
Eric Biederman90089602004-05-28 14:11:54 +000014742 struct type *pnext, *param;
14743 struct type *result_type, *args_type;
14744 int idx;
14745
14746 /* Be clear the functions have not been joined yet */
14747 state->functions_joined = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000014748
14749 /* Dummy file state to get debug handing right */
14750 memset(&file, 0, sizeof(file));
14751 file.basename = "";
14752 file.line = 0;
14753 file.report_line = 0;
14754 file.report_name = file.basename;
14755 file.prev = state->file;
14756 state->file = &file;
14757 state->function = "";
Eric Biederman90089602004-05-28 14:11:54 +000014758
Eric Biederman41203d92004-11-08 09:31:09 +000014759 if (!state->main_function) {
14760 error(state, 0, "No functions to compile\n");
14761 }
14762
Eric Biederman90089602004-05-28 14:11:54 +000014763 /* The type of arguments */
14764 args_type = state->main_function->type->right;
14765 /* The return type without any specifiers */
14766 result_type = clone_type(0, state->main_function->type->left);
14767
14768
14769 /* Verify the external arguments */
14770 if (registers_of(state, args_type) > ARCH_INPUT_REGS) {
14771 error(state, state->main_function,
14772 "Too many external input arguments");
14773 }
14774 if (registers_of(state, result_type) > ARCH_OUTPUT_REGS) {
14775 error(state, state->main_function,
14776 "Too many external output arguments");
14777 }
14778
Eric Biederman5ade04a2003-10-22 04:03:46 +000014779 /* Lay down the basic program structure */
Eric Biederman90089602004-05-28 14:11:54 +000014780 end = label(state);
14781 start = label(state);
14782 start = flatten(state, state->first, start);
14783 end = flatten(state, state->first, end);
14784 in = input_asm(state);
14785 out = output_asm(state);
14786 call = new_triple(state, OP_FCALL, result_type, -1, registers_of(state, args_type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000014787 MISC(call, 0) = state->main_function;
Eric Biederman90089602004-05-28 14:11:54 +000014788 in = flatten(state, state->first, in);
14789 call = flatten(state, state->first, call);
14790 out = flatten(state, state->first, out);
14791
14792
14793 /* Read the external input arguments */
14794 pnext = args_type;
14795 idx = 0;
14796 while(pnext && ((pnext->type & TYPE_MASK) != TYPE_VOID)) {
14797 struct triple *expr;
14798 param = pnext;
14799 pnext = 0;
14800 if ((param->type & TYPE_MASK) == TYPE_PRODUCT) {
14801 pnext = param->right;
14802 param = param->left;
14803 }
14804 if (registers_of(state, param) != 1) {
14805 error(state, state->main_function,
14806 "Arg: %d %s requires multiple registers",
14807 idx + 1, param->field_ident->name);
14808 }
14809 expr = read_expr(state, LHS(in, idx));
14810 RHS(call, idx) = expr;
14811 expr = flatten(state, call, expr);
14812 use_triple(expr, call);
14813
14814 idx++;
14815 }
14816
14817
14818 /* Write the external output arguments */
14819 pnext = result_type;
14820 if ((pnext->type & TYPE_MASK) == TYPE_STRUCT) {
14821 pnext = result_type->left;
14822 }
14823 for(idx = 0; idx < out->rhs; idx++) {
14824 struct triple *expr;
14825 param = pnext;
14826 pnext = 0;
14827 if (param && ((param->type & TYPE_MASK) == TYPE_PRODUCT)) {
14828 pnext = param->right;
14829 param = param->left;
14830 }
14831 if (param && ((param->type & TYPE_MASK) == TYPE_VOID)) {
14832 param = 0;
14833 }
14834 if (param) {
14835 if (registers_of(state, param) != 1) {
14836 error(state, state->main_function,
14837 "Result: %d %s requires multiple registers",
14838 idx, param->field_ident->name);
14839 }
14840 expr = read_expr(state, call);
14841 if ((result_type->type & TYPE_MASK) == TYPE_STRUCT) {
14842 expr = deref_field(state, expr, param->field_ident);
14843 }
14844 } else {
14845 expr = triple(state, OP_UNKNOWNVAL, &int_type, 0, 0);
14846 }
14847 flatten(state, out, expr);
14848 RHS(out, idx) = expr;
14849 use_triple(expr, out);
14850 }
14851
14852 /* Allocate a dummy containing function */
14853 func = triple(state, OP_LIST,
14854 new_type(TYPE_FUNCTION, &void_type, &void_type), 0, 0);
14855 func->type->type_ident = lookup(state, "", 0);
14856 RHS(func, 0) = state->first;
14857 func->u.cval = 1;
14858
Eric Biederman5ade04a2003-10-22 04:03:46 +000014859 /* See which functions are called, and how often */
Eric Biederman90089602004-05-28 14:11:54 +000014860 mark_live_functions(state);
14861 inline_functions(state, func);
Eric Biederman5ade04a2003-10-22 04:03:46 +000014862 walk_functions(state, insert_function, end);
14863
14864 if (start->next != end) {
14865 jmp = flatten(state, start, branch(state, end, 0));
14866 }
14867
Eric Biederman90089602004-05-28 14:11:54 +000014868 /* OK now the functions have been joined. */
14869 state->functions_joined = 1;
14870
Eric Biederman5ade04a2003-10-22 04:03:46 +000014871 /* Done now cleanup */
14872 state->file = file.prev;
14873 state->function = 0;
14874}
14875
Eric Biedermanb138ac82003-04-22 18:44:01 +000014876/*
14877 * Data structurs for optimation.
14878 */
14879
Eric Biederman5ade04a2003-10-22 04:03:46 +000014880
Eric Biederman83b991a2003-10-11 06:20:25 +000014881static int do_use_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014882 struct block *used, struct block_set **head, struct block *user,
14883 int front)
14884{
14885 struct block_set **ptr, *new;
14886 if (!used)
Eric Biederman83b991a2003-10-11 06:20:25 +000014887 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014888 if (!user)
Eric Biederman83b991a2003-10-11 06:20:25 +000014889 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014890 ptr = head;
14891 while(*ptr) {
14892 if ((*ptr)->member == user) {
Eric Biederman83b991a2003-10-11 06:20:25 +000014893 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014894 }
14895 ptr = &(*ptr)->next;
14896 }
14897 new = xcmalloc(sizeof(*new), "block_set");
14898 new->member = user;
14899 if (front) {
14900 new->next = *head;
14901 *head = new;
14902 }
14903 else {
14904 new->next = 0;
14905 *ptr = new;
14906 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014907 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014908}
Eric Biederman83b991a2003-10-11 06:20:25 +000014909static int do_unuse_block(
Eric Biedermanb138ac82003-04-22 18:44:01 +000014910 struct block *used, struct block_set **head, struct block *unuser)
14911{
14912 struct block_set *use, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000014913 int count;
14914 count = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014915 ptr = head;
14916 while(*ptr) {
14917 use = *ptr;
14918 if (use->member == unuser) {
14919 *ptr = use->next;
14920 memset(use, -1, sizeof(*use));
14921 xfree(use);
Eric Biederman83b991a2003-10-11 06:20:25 +000014922 count += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014923 }
14924 else {
14925 ptr = &use->next;
14926 }
14927 }
Eric Biederman83b991a2003-10-11 06:20:25 +000014928 return count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014929}
14930
14931static void use_block(struct block *used, struct block *user)
14932{
Eric Biederman83b991a2003-10-11 06:20:25 +000014933 int count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014934 /* Append new to the head of the list, print_block
14935 * depends on this.
14936 */
Eric Biederman83b991a2003-10-11 06:20:25 +000014937 count = do_use_block(used, &used->use, user, 1);
14938 used->users += count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014939}
14940static void unuse_block(struct block *used, struct block *unuser)
14941{
Eric Biederman83b991a2003-10-11 06:20:25 +000014942 int count;
14943 count = do_unuse_block(used, &used->use, unuser);
14944 used->users -= count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000014945}
14946
Eric Biederman5ade04a2003-10-22 04:03:46 +000014947static void add_block_edge(struct block *block, struct block *edge, int front)
14948{
14949 int count;
14950 count = do_use_block(block, &block->edges, edge, front);
14951 block->edge_count += count;
14952}
14953
14954static void remove_block_edge(struct block *block, struct block *edge)
14955{
14956 int count;
14957 count = do_unuse_block(block, &block->edges, edge);
14958 block->edge_count -= count;
14959}
14960
Eric Biedermanb138ac82003-04-22 18:44:01 +000014961static void idom_block(struct block *idom, struct block *user)
14962{
14963 do_use_block(idom, &idom->idominates, user, 0);
14964}
14965
14966static void unidom_block(struct block *idom, struct block *unuser)
14967{
14968 do_unuse_block(idom, &idom->idominates, unuser);
14969}
14970
14971static void domf_block(struct block *block, struct block *domf)
14972{
14973 do_use_block(block, &block->domfrontier, domf, 0);
14974}
14975
14976static void undomf_block(struct block *block, struct block *undomf)
14977{
14978 do_unuse_block(block, &block->domfrontier, undomf);
14979}
14980
14981static void ipdom_block(struct block *ipdom, struct block *user)
14982{
14983 do_use_block(ipdom, &ipdom->ipdominates, user, 0);
14984}
14985
14986static void unipdom_block(struct block *ipdom, struct block *unuser)
14987{
14988 do_unuse_block(ipdom, &ipdom->ipdominates, unuser);
14989}
14990
14991static void ipdomf_block(struct block *block, struct block *ipdomf)
14992{
14993 do_use_block(block, &block->ipdomfrontier, ipdomf, 0);
14994}
14995
14996static void unipdomf_block(struct block *block, struct block *unipdomf)
14997{
14998 do_unuse_block(block, &block->ipdomfrontier, unipdomf);
14999}
15000
Eric Biederman83b991a2003-10-11 06:20:25 +000015001static int walk_triples(
15002 struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000015003 int (*cb)(struct compile_state *state, struct triple *ptr, void *arg),
15004 void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015005{
Eric Biederman83b991a2003-10-11 06:20:25 +000015006 struct triple *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015007 int result;
Eric Biederman83b991a2003-10-11 06:20:25 +000015008 ptr = state->first;
15009 do {
Eric Biederman90089602004-05-28 14:11:54 +000015010 result = cb(state, ptr, arg);
Eric Biederman83b991a2003-10-11 06:20:25 +000015011 if (ptr->next->prev != ptr) {
15012 internal_error(state, ptr->next, "bad prev");
15013 }
15014 ptr = ptr->next;
15015 } while((result == 0) && (ptr != state->first));
Eric Biedermanb138ac82003-04-22 18:44:01 +000015016 return result;
15017}
15018
Eric Biedermanb138ac82003-04-22 18:44:01 +000015019#define PRINT_LIST 1
Eric Biederman90089602004-05-28 14:11:54 +000015020static int do_print_triple(struct compile_state *state, struct triple *ins, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015021{
Eric Biederman90089602004-05-28 14:11:54 +000015022 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015023 int op;
15024 op = ins->op;
15025 if (op == OP_LIST) {
15026#if !PRINT_LIST
15027 return 0;
15028#endif
15029 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015030 if ((op == OP_LABEL) && (ins->use)) {
Eric Biederman90089602004-05-28 14:11:54 +000015031 fprintf(fp, "\n%p:\n", ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015032 }
Eric Biederman90089602004-05-28 14:11:54 +000015033 display_triple(fp, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000015034
Eric Biederman90089602004-05-28 14:11:54 +000015035 if (triple_is_branch(state, ins) && ins->use &&
15036 (ins->op != OP_RET) && (ins->op != OP_FCALL)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015037 internal_error(state, ins, "branch used?");
15038 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000015039 if (triple_is_branch(state, ins)) {
Eric Biederman90089602004-05-28 14:11:54 +000015040 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015041 }
15042 return 0;
15043}
15044
Eric Biedermanb138ac82003-04-22 18:44:01 +000015045static void print_triples(struct compile_state *state)
15046{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015047 if (state->compiler->debug & DEBUG_TRIPLES) {
Eric Biederman90089602004-05-28 14:11:54 +000015048 FILE *fp = state->dbgout;
15049 fprintf(fp, "--------------- triples ---------------\n");
15050 walk_triples(state, do_print_triple, fp);
15051 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015052 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015053}
15054
15055struct cf_block {
15056 struct block *block;
15057};
15058static void find_cf_blocks(struct cf_block *cf, struct block *block)
15059{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015060 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015061 if (!block || (cf[block->vertex].block == block)) {
15062 return;
15063 }
15064 cf[block->vertex].block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015065 for(edge = block->edges; edge; edge = edge->next) {
15066 find_cf_blocks(cf, edge->member);
15067 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015068}
15069
Eric Biederman90089602004-05-28 14:11:54 +000015070static void print_control_flow(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015071 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015072{
15073 struct cf_block *cf;
15074 int i;
Eric Biederman7dea9552004-06-29 05:38:37 +000015075 fprintf(fp, "\ncontrol flow\n");
Eric Biederman90089602004-05-28 14:11:54 +000015076 cf = xcmalloc(sizeof(*cf) * (bb->last_vertex + 1), "cf_block");
15077 find_cf_blocks(cf, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015078
Eric Biederman90089602004-05-28 14:11:54 +000015079 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015080 struct block *block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015081 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015082 block = cf[i].block;
15083 if (!block)
15084 continue;
Eric Biederman7dea9552004-06-29 05:38:37 +000015085 fprintf(fp, "(%p) %d:", block, block->vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015086 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015087 fprintf(fp, " %d", edge->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015088 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015089 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015090 }
15091
15092 xfree(cf);
15093}
15094
Eric Biedermanb138ac82003-04-22 18:44:01 +000015095static void free_basic_block(struct compile_state *state, struct block *block)
15096{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015097 struct block_set *edge, *entry;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015098 struct block *child;
15099 if (!block) {
15100 return;
15101 }
15102 if (block->vertex == -1) {
15103 return;
15104 }
15105 block->vertex = -1;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015106 for(edge = block->edges; edge; edge = edge->next) {
15107 if (edge->member) {
15108 unuse_block(edge->member, block);
15109 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015110 }
15111 if (block->idom) {
15112 unidom_block(block->idom, block);
15113 }
15114 block->idom = 0;
15115 if (block->ipdom) {
15116 unipdom_block(block->ipdom, block);
15117 }
15118 block->ipdom = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015119 while((entry = block->use)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015120 child = entry->member;
15121 unuse_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015122 if (child && (child->vertex != -1)) {
15123 for(edge = child->edges; edge; edge = edge->next) {
15124 edge->member = 0;
15125 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015126 }
15127 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015128 while((entry = block->idominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015129 child = entry->member;
15130 unidom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015131 if (child && (child->vertex != -1)) {
15132 child->idom = 0;
15133 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015134 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015135 while((entry = block->domfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015136 child = entry->member;
15137 undomf_block(block, child);
15138 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015139 while((entry = block->ipdominates)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015140 child = entry->member;
15141 unipdom_block(block, child);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015142 if (child && (child->vertex != -1)) {
15143 child->ipdom = 0;
15144 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015145 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015146 while((entry = block->ipdomfrontier)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015147 child = entry->member;
15148 unipdomf_block(block, child);
15149 }
15150 if (block->users != 0) {
15151 internal_error(state, 0, "block still has users");
15152 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015153 while((edge = block->edges)) {
15154 child = edge->member;
15155 remove_block_edge(block, child);
15156
15157 if (child && (child->vertex != -1)) {
15158 free_basic_block(state, child);
15159 }
15160 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015161 memset(block, -1, sizeof(*block));
Patrick Georgi26774f22009-11-21 19:54:02 +000015162#ifndef WIN32
Eric Biedermanb138ac82003-04-22 18:44:01 +000015163 xfree(block);
Patrick Georgi26774f22009-11-21 19:54:02 +000015164#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000015165}
15166
Eric Biederman90089602004-05-28 14:11:54 +000015167static void free_basic_blocks(struct compile_state *state,
15168 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015169{
15170 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000015171 free_basic_block(state, bb->first_block);
15172 bb->last_vertex = 0;
15173 bb->first_block = bb->last_block = 0;
15174 first = bb->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015175 ins = first;
15176 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015177 if (triple_stores_block(state, ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015178 ins->u.block = 0;
15179 }
15180 ins = ins->next;
15181 } while(ins != first);
15182
15183}
15184
Eric Biederman90089602004-05-28 14:11:54 +000015185static struct block *basic_block(struct compile_state *state,
15186 struct basic_blocks *bb, struct triple *first)
15187{
15188 struct block *block;
15189 struct triple *ptr;
15190 if (!triple_is_label(state, first)) {
15191 internal_error(state, first, "block does not start with a label");
15192 }
15193 /* See if this basic block has already been setup */
15194 if (first->u.block != 0) {
15195 return first->u.block;
15196 }
15197 /* Allocate another basic block structure */
15198 bb->last_vertex += 1;
15199 block = xcmalloc(sizeof(*block), "block");
15200 block->first = block->last = first;
15201 block->vertex = bb->last_vertex;
15202 ptr = first;
15203 do {
15204 if ((ptr != first) && triple_is_label(state, ptr) && (ptr->use)) {
15205 break;
15206 }
15207 block->last = ptr;
15208 /* If ptr->u is not used remember where the baic block is */
15209 if (triple_stores_block(state, ptr)) {
15210 ptr->u.block = block;
15211 }
15212 if (triple_is_branch(state, ptr)) {
15213 break;
15214 }
15215 ptr = ptr->next;
15216 } while (ptr != bb->first);
15217 if ((ptr == bb->first) ||
15218 ((ptr->next == bb->first) && (
15219 triple_is_end(state, ptr) ||
15220 triple_is_ret(state, ptr))))
15221 {
15222 /* The block has no outflowing edges */
15223 }
15224 else if (triple_is_label(state, ptr)) {
15225 struct block *next;
15226 next = basic_block(state, bb, ptr);
15227 add_block_edge(block, next, 0);
15228 use_block(next, block);
15229 }
15230 else if (triple_is_branch(state, ptr)) {
15231 struct triple **expr, *first;
15232 struct block *child;
15233 /* Find the branch targets.
15234 * I special case the first branch as that magically
15235 * avoids some difficult cases for the register allocator.
15236 */
15237 expr = triple_edge_targ(state, ptr, 0);
15238 if (!expr) {
15239 internal_error(state, ptr, "branch without targets");
15240 }
15241 first = *expr;
15242 expr = triple_edge_targ(state, ptr, expr);
15243 for(; expr; expr = triple_edge_targ(state, ptr, expr)) {
15244 if (!*expr) continue;
15245 child = basic_block(state, bb, *expr);
15246 use_block(child, block);
15247 add_block_edge(block, child, 0);
15248 }
15249 if (first) {
15250 child = basic_block(state, bb, first);
15251 use_block(child, block);
15252 add_block_edge(block, child, 1);
15253
15254 /* Be certain the return block of a call is
15255 * in a basic block. When it is not find
15256 * start of the block, insert a label if
15257 * necessary and build the basic block.
15258 * Then add a fake edge from the start block
15259 * to the return block of the function.
15260 */
15261 if (state->functions_joined && triple_is_call(state, ptr)
15262 && !block_of_triple(state, MISC(ptr, 0))) {
15263 struct block *tail;
15264 struct triple *start;
15265 start = triple_to_block_start(state, MISC(ptr, 0));
15266 if (!triple_is_label(state, start)) {
15267 start = pre_triple(state,
15268 start, OP_LABEL, &void_type, 0, 0);
15269 }
15270 tail = basic_block(state, bb, start);
15271 add_block_edge(child, tail, 0);
15272 use_block(tail, child);
15273 }
15274 }
15275 }
15276 else {
15277 internal_error(state, 0, "Bad basic block split");
15278 }
15279#if 0
15280{
15281 struct block_set *edge;
15282 FILE *fp = state->errout;
15283 fprintf(fp, "basic_block: %10p [%2d] ( %10p - %10p )",
15284 block, block->vertex,
15285 block->first, block->last);
15286 for(edge = block->edges; edge; edge = edge->next) {
15287 fprintf(fp, " %10p [%2d]",
15288 edge->member ? edge->member->first : 0,
15289 edge->member ? edge->member->vertex : -1);
15290 }
15291 fprintf(fp, "\n");
15292}
15293#endif
15294 return block;
15295}
15296
15297
15298static void walk_blocks(struct compile_state *state, struct basic_blocks *bb,
15299 void (*cb)(struct compile_state *state, struct block *block, void *arg),
15300 void *arg)
15301{
15302 struct triple *ptr, *first;
15303 struct block *last_block;
15304 last_block = 0;
15305 first = bb->first;
15306 ptr = first;
15307 do {
15308 if (triple_stores_block(state, ptr)) {
15309 struct block *block;
15310 block = ptr->u.block;
15311 if (block && (block != last_block)) {
15312 cb(state, block, arg);
15313 }
15314 last_block = block;
15315 }
15316 ptr = ptr->next;
15317 } while(ptr != first);
15318}
15319
15320static void print_block(
15321 struct compile_state *state, struct block *block, void *arg)
15322{
15323 struct block_set *user, *edge;
15324 struct triple *ptr;
15325 FILE *fp = arg;
15326
15327 fprintf(fp, "\nblock: %p (%d) ",
15328 block,
15329 block->vertex);
15330
15331 for(edge = block->edges; edge; edge = edge->next) {
15332 fprintf(fp, " %p<-%p",
15333 edge->member,
15334 (edge->member && edge->member->use)?
15335 edge->member->use->member : 0);
15336 }
15337 fprintf(fp, "\n");
15338 if (block->first->op == OP_LABEL) {
15339 fprintf(fp, "%p:\n", block->first);
15340 }
15341 for(ptr = block->first; ; ) {
15342 display_triple(fp, ptr);
15343 if (ptr == block->last)
15344 break;
15345 ptr = ptr->next;
15346 if (ptr == block->first) {
15347 internal_error(state, 0, "missing block last?");
15348 }
15349 }
15350 fprintf(fp, "users %d: ", block->users);
15351 for(user = block->use; user; user = user->next) {
15352 fprintf(fp, "%p (%d) ",
15353 user->member,
15354 user->member->vertex);
15355 }
15356 fprintf(fp,"\n\n");
15357}
15358
15359
15360static void romcc_print_blocks(struct compile_state *state, FILE *fp)
15361{
15362 fprintf(fp, "--------------- blocks ---------------\n");
15363 walk_blocks(state, &state->bb, print_block, fp);
15364}
15365static void print_blocks(struct compile_state *state, const char *func, FILE *fp)
15366{
15367 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15368 fprintf(fp, "After %s\n", func);
15369 romcc_print_blocks(state, fp);
Eric Biederman7dea9552004-06-29 05:38:37 +000015370 if (state->compiler->debug & DEBUG_FDOMINATORS) {
15371 print_dominators(state, fp, &state->bb);
15372 print_dominance_frontiers(state, fp, &state->bb);
15373 }
15374 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000015375 }
15376}
15377
15378static void prune_nonblock_triples(struct compile_state *state,
15379 struct basic_blocks *bb)
15380{
15381 struct block *block;
15382 struct triple *first, *ins, *next;
15383 /* Delete the triples not in a basic block */
15384 block = 0;
15385 first = bb->first;
15386 ins = first;
15387 do {
15388 next = ins->next;
15389 if (ins->op == OP_LABEL) {
15390 block = ins->u.block;
15391 }
15392 if (!block) {
15393 struct triple_set *use;
15394 for(use = ins->use; use; use = use->next) {
15395 struct block *block;
15396 block = block_of_triple(state, use->member);
15397 if (block != 0) {
15398 internal_error(state, ins, "pruning used ins?");
15399 }
15400 }
15401 release_triple(state, ins);
15402 }
15403 if (block && block->last == ins) {
15404 block = 0;
15405 }
15406 ins = next;
15407 } while(ins != first);
15408}
15409
15410static void setup_basic_blocks(struct compile_state *state,
15411 struct basic_blocks *bb)
15412{
15413 if (!triple_stores_block(state, bb->first)) {
15414 internal_error(state, 0, "ins will not store block?");
15415 }
15416 /* Initialize the state */
15417 bb->first_block = bb->last_block = 0;
15418 bb->last_vertex = 0;
15419 free_basic_blocks(state, bb);
15420
15421 /* Find the basic blocks */
15422 bb->first_block = basic_block(state, bb, bb->first);
15423
15424 /* Be certain the last instruction of a function, or the
15425 * entire program is in a basic block. When it is not find
15426 * the start of the block, insert a label if necessary and build
15427 * basic block. Then add a fake edge from the start block
15428 * to the final block.
15429 */
15430 if (!block_of_triple(state, bb->first->prev)) {
15431 struct triple *start;
15432 struct block *tail;
15433 start = triple_to_block_start(state, bb->first->prev);
15434 if (!triple_is_label(state, start)) {
15435 start = pre_triple(state,
15436 start, OP_LABEL, &void_type, 0, 0);
15437 }
15438 tail = basic_block(state, bb, start);
15439 add_block_edge(bb->first_block, tail, 0);
15440 use_block(tail, bb->first_block);
15441 }
15442
15443 /* Find the last basic block.
15444 */
15445 bb->last_block = block_of_triple(state, bb->first->prev);
15446
15447 /* Delete the triples not in a basic block */
15448 prune_nonblock_triples(state, bb);
15449
15450#if 0
15451 /* If we are debugging print what I have just done */
15452 if (state->compiler->debug & DEBUG_BASIC_BLOCKS) {
15453 print_blocks(state, state->dbgout);
15454 print_control_flow(state, bb);
15455 }
15456#endif
15457}
15458
15459
Eric Biedermanb138ac82003-04-22 18:44:01 +000015460struct sdom_block {
15461 struct block *block;
15462 struct sdom_block *sdominates;
15463 struct sdom_block *sdom_next;
15464 struct sdom_block *sdom;
15465 struct sdom_block *label;
15466 struct sdom_block *parent;
15467 struct sdom_block *ancestor;
15468 int vertex;
15469};
15470
15471
15472static void unsdom_block(struct sdom_block *block)
15473{
15474 struct sdom_block **ptr;
15475 if (!block->sdom_next) {
15476 return;
15477 }
15478 ptr = &block->sdom->sdominates;
15479 while(*ptr) {
15480 if ((*ptr) == block) {
15481 *ptr = block->sdom_next;
15482 return;
15483 }
15484 ptr = &(*ptr)->sdom_next;
15485 }
15486}
15487
15488static void sdom_block(struct sdom_block *sdom, struct sdom_block *block)
15489{
15490 unsdom_block(block);
15491 block->sdom = sdom;
15492 block->sdom_next = sdom->sdominates;
15493 sdom->sdominates = block;
15494}
15495
15496
15497
15498static int initialize_sdblock(struct sdom_block *sd,
15499 struct block *parent, struct block *block, int vertex)
15500{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015501 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015502 if (!block || (sd[block->vertex].block == block)) {
15503 return vertex;
15504 }
15505 vertex += 1;
15506 /* Renumber the blocks in a convinient fashion */
15507 block->vertex = vertex;
15508 sd[vertex].block = block;
15509 sd[vertex].sdom = &sd[vertex];
15510 sd[vertex].label = &sd[vertex];
15511 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15512 sd[vertex].ancestor = 0;
15513 sd[vertex].vertex = vertex;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015514 for(edge = block->edges; edge; edge = edge->next) {
15515 vertex = initialize_sdblock(sd, block, edge->member, vertex);
15516 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015517 return vertex;
15518}
15519
Eric Biederman83b991a2003-10-11 06:20:25 +000015520static int initialize_spdblock(
Eric Biederman530b5192003-07-01 10:05:30 +000015521 struct compile_state *state, struct sdom_block *sd,
Eric Biedermanb138ac82003-04-22 18:44:01 +000015522 struct block *parent, struct block *block, int vertex)
15523{
15524 struct block_set *user;
15525 if (!block || (sd[block->vertex].block == block)) {
15526 return vertex;
15527 }
15528 vertex += 1;
15529 /* Renumber the blocks in a convinient fashion */
15530 block->vertex = vertex;
15531 sd[vertex].block = block;
15532 sd[vertex].sdom = &sd[vertex];
15533 sd[vertex].label = &sd[vertex];
15534 sd[vertex].parent = parent? &sd[parent->vertex] : 0;
15535 sd[vertex].ancestor = 0;
15536 sd[vertex].vertex = vertex;
15537 for(user = block->use; user; user = user->next) {
Eric Biederman83b991a2003-10-11 06:20:25 +000015538 vertex = initialize_spdblock(state, sd, block, user->member, vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015539 }
15540 return vertex;
15541}
15542
Eric Biederman90089602004-05-28 14:11:54 +000015543static int setup_spdblocks(struct compile_state *state,
15544 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biederman530b5192003-07-01 10:05:30 +000015545{
15546 struct block *block;
15547 int vertex;
15548 /* Setup as many sdpblocks as possible without using fake edges */
Eric Biederman90089602004-05-28 14:11:54 +000015549 vertex = initialize_spdblock(state, sd, 0, bb->last_block, 0);
Eric Biederman530b5192003-07-01 10:05:30 +000015550
Eric Biederman5ade04a2003-10-22 04:03:46 +000015551 /* Walk through the graph and find unconnected blocks. Add a
15552 * fake edge from the unconnected blocks to the end of the
15553 * graph.
Eric Biederman530b5192003-07-01 10:05:30 +000015554 */
Eric Biederman90089602004-05-28 14:11:54 +000015555 block = bb->first_block->last->next->u.block;
15556 for(; block && block != bb->first_block; block = block->last->next->u.block) {
Eric Biederman530b5192003-07-01 10:05:30 +000015557 if (sd[block->vertex].block == block) {
15558 continue;
15559 }
Eric Biederman530b5192003-07-01 10:05:30 +000015560#if DEBUG_SDP_BLOCKS
Eric Biederman90089602004-05-28 14:11:54 +000015561 {
15562 FILE *fp = state->errout;
15563 fprintf(fp, "Adding %d\n", vertex +1);
15564 }
Eric Biederman530b5192003-07-01 10:05:30 +000015565#endif
Eric Biederman90089602004-05-28 14:11:54 +000015566 add_block_edge(block, bb->last_block, 0);
15567 use_block(bb->last_block, block);
Eric Biederman530b5192003-07-01 10:05:30 +000015568
Eric Biederman90089602004-05-28 14:11:54 +000015569 vertex = initialize_spdblock(state, sd, bb->last_block, block, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015570 }
15571 return vertex;
15572}
15573
15574static void compress_ancestors(struct sdom_block *v)
15575{
15576 /* This procedure assumes ancestor(v) != 0 */
15577 /* if (ancestor(ancestor(v)) != 0) {
15578 * compress(ancestor(ancestor(v)));
15579 * if (semi(label(ancestor(v))) < semi(label(v))) {
15580 * label(v) = label(ancestor(v));
15581 * }
15582 * ancestor(v) = ancestor(ancestor(v));
15583 * }
15584 */
15585 if (!v->ancestor) {
15586 return;
15587 }
15588 if (v->ancestor->ancestor) {
15589 compress_ancestors(v->ancestor->ancestor);
15590 if (v->ancestor->label->sdom->vertex < v->label->sdom->vertex) {
15591 v->label = v->ancestor->label;
15592 }
15593 v->ancestor = v->ancestor->ancestor;
15594 }
15595}
15596
Eric Biederman90089602004-05-28 14:11:54 +000015597static void compute_sdom(struct compile_state *state,
15598 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015599{
15600 int i;
15601 /* // step 2
15602 * for each v <= pred(w) {
15603 * u = EVAL(v);
15604 * if (semi[u] < semi[w] {
15605 * semi[w] = semi[u];
15606 * }
15607 * }
15608 * add w to bucket(vertex(semi[w]));
15609 * LINK(parent(w), w);
15610 *
15611 * // step 3
15612 * for each v <= bucket(parent(w)) {
15613 * delete v from bucket(parent(w));
15614 * u = EVAL(v);
15615 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15616 * }
15617 */
Eric Biederman90089602004-05-28 14:11:54 +000015618 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015619 struct sdom_block *v, *parent, *next;
15620 struct block_set *user;
15621 struct block *block;
15622 block = sd[i].block;
15623 parent = sd[i].parent;
15624 /* Step 2 */
15625 for(user = block->use; user; user = user->next) {
15626 struct sdom_block *v, *u;
15627 v = &sd[user->member->vertex];
15628 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15629 if (u->sdom->vertex < sd[i].sdom->vertex) {
15630 sd[i].sdom = u->sdom;
15631 }
15632 }
15633 sdom_block(sd[i].sdom, &sd[i]);
15634 sd[i].ancestor = parent;
15635 /* Step 3 */
15636 for(v = parent->sdominates; v; v = next) {
15637 struct sdom_block *u;
15638 next = v->sdom_next;
15639 unsdom_block(v);
15640 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15641 v->block->idom = (u->sdom->vertex < v->sdom->vertex)?
15642 u->block : parent->block;
15643 }
15644 }
15645}
15646
Eric Biederman90089602004-05-28 14:11:54 +000015647static void compute_spdom(struct compile_state *state,
15648 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015649{
15650 int i;
15651 /* // step 2
15652 * for each v <= pred(w) {
15653 * u = EVAL(v);
15654 * if (semi[u] < semi[w] {
15655 * semi[w] = semi[u];
15656 * }
15657 * }
15658 * add w to bucket(vertex(semi[w]));
15659 * LINK(parent(w), w);
15660 *
15661 * // step 3
15662 * for each v <= bucket(parent(w)) {
15663 * delete v from bucket(parent(w));
15664 * u = EVAL(v);
15665 * dom(v) = (semi[u] < semi[v]) ? u : parent(w);
15666 * }
15667 */
Eric Biederman90089602004-05-28 14:11:54 +000015668 for(i = bb->last_vertex; i >= 2; i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015669 struct sdom_block *u, *v, *parent, *next;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015670 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015671 struct block *block;
15672 block = sd[i].block;
15673 parent = sd[i].parent;
15674 /* Step 2 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015675 for(edge = block->edges; edge; edge = edge->next) {
15676 v = &sd[edge->member->vertex];
Eric Biedermanb138ac82003-04-22 18:44:01 +000015677 u = !(v->ancestor)? v : (compress_ancestors(v), v->label);
15678 if (u->sdom->vertex < sd[i].sdom->vertex) {
15679 sd[i].sdom = u->sdom;
15680 }
15681 }
15682 sdom_block(sd[i].sdom, &sd[i]);
15683 sd[i].ancestor = parent;
15684 /* Step 3 */
15685 for(v = parent->sdominates; v; v = next) {
15686 struct sdom_block *u;
15687 next = v->sdom_next;
15688 unsdom_block(v);
15689 u = (!v->ancestor) ? v : (compress_ancestors(v), v->label);
15690 v->block->ipdom = (u->sdom->vertex < v->sdom->vertex)?
15691 u->block : parent->block;
15692 }
15693 }
15694}
15695
Eric Biederman90089602004-05-28 14:11:54 +000015696static void compute_idom(struct compile_state *state,
15697 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015698{
15699 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015700 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015701 struct block *block;
15702 block = sd[i].block;
15703 if (block->idom->vertex != sd[i].sdom->vertex) {
15704 block->idom = block->idom->idom;
15705 }
15706 idom_block(block->idom, block);
15707 }
15708 sd[1].block->idom = 0;
15709}
15710
Eric Biederman90089602004-05-28 14:11:54 +000015711static void compute_ipdom(struct compile_state *state,
15712 struct basic_blocks *bb, struct sdom_block *sd)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015713{
15714 int i;
Eric Biederman90089602004-05-28 14:11:54 +000015715 for(i = 2; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015716 struct block *block;
15717 block = sd[i].block;
15718 if (block->ipdom->vertex != sd[i].sdom->vertex) {
15719 block->ipdom = block->ipdom->ipdom;
15720 }
15721 ipdom_block(block->ipdom, block);
15722 }
15723 sd[1].block->ipdom = 0;
15724}
15725
15726 /* Theorem 1:
15727 * Every vertex of a flowgraph G = (V, E, r) except r has
15728 * a unique immediate dominator.
15729 * The edges {(idom(w), w) |w <= V - {r}} form a directed tree
15730 * rooted at r, called the dominator tree of G, such that
15731 * v dominates w if and only if v is a proper ancestor of w in
15732 * the dominator tree.
15733 */
15734 /* Lemma 1:
15735 * If v and w are vertices of G such that v <= w,
15736 * than any path from v to w must contain a common ancestor
15737 * of v and w in T.
15738 */
15739 /* Lemma 2: For any vertex w != r, idom(w) -> w */
15740 /* Lemma 3: For any vertex w != r, sdom(w) -> w */
15741 /* Lemma 4: For any vertex w != r, idom(w) -> sdom(w) */
15742 /* Theorem 2:
15743 * Let w != r. Suppose every u for which sdom(w) -> u -> w satisfies
15744 * sdom(u) >= sdom(w). Then idom(w) = sdom(w).
15745 */
15746 /* Theorem 3:
15747 * Let w != r and let u be a vertex for which sdom(u) is
15748 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15749 * Then sdom(u) <= sdom(w) and idom(u) = idom(w).
15750 */
15751 /* Lemma 5: Let vertices v,w satisfy v -> w.
15752 * Then v -> idom(w) or idom(w) -> idom(v)
15753 */
15754
Eric Biederman90089602004-05-28 14:11:54 +000015755static void find_immediate_dominators(struct compile_state *state,
15756 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015757{
15758 struct sdom_block *sd;
15759 /* w->sdom = min{v| there is a path v = v0,v1,...,vk = w such that:
15760 * vi > w for (1 <= i <= k - 1}
15761 */
15762 /* Theorem 4:
15763 * For any vertex w != r.
15764 * sdom(w) = min(
15765 * {v|(v,w) <= E and v < w } U
15766 * {sdom(u) | u > w and there is an edge (v, w) such that u -> v})
15767 */
15768 /* Corollary 1:
15769 * Let w != r and let u be a vertex for which sdom(u) is
15770 * minimum amoung vertices u satisfying sdom(w) -> u -> w.
15771 * Then:
15772 * { sdom(w) if sdom(w) = sdom(u),
15773 * idom(w) = {
15774 * { idom(u) otherwise
15775 */
15776 /* The algorithm consists of the following 4 steps.
15777 * Step 1. Carry out a depth-first search of the problem graph.
15778 * Number the vertices from 1 to N as they are reached during
15779 * the search. Initialize the variables used in succeeding steps.
15780 * Step 2. Compute the semidominators of all vertices by applying
15781 * theorem 4. Carry out the computation vertex by vertex in
15782 * decreasing order by number.
15783 * Step 3. Implicitly define the immediate dominator of each vertex
15784 * by applying Corollary 1.
15785 * Step 4. Explicitly define the immediate dominator of each vertex,
15786 * carrying out the computation vertex by vertex in increasing order
15787 * by number.
15788 */
15789 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015790 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
15791 initialize_sdblock(sd, 0, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015792#if 0
15793 sd[1].size = 0;
15794 sd[1].label = 0;
15795 sd[1].sdom = 0;
15796#endif
15797 /* Step 2 compute the semidominators */
15798 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015799 compute_sdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015800 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015801 compute_idom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015802 xfree(sd);
15803}
15804
Eric Biederman90089602004-05-28 14:11:54 +000015805static void find_post_dominators(struct compile_state *state,
15806 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015807{
15808 struct sdom_block *sd;
Eric Biederman530b5192003-07-01 10:05:30 +000015809 int vertex;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015810 /* Step 1 initialize the basic block information */
Eric Biederman90089602004-05-28 14:11:54 +000015811 sd = xcmalloc(sizeof(*sd) * (bb->last_vertex + 1), "sdom_state");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015812
Eric Biederman90089602004-05-28 14:11:54 +000015813 vertex = setup_spdblocks(state, bb, sd);
15814 if (vertex != bb->last_vertex) {
15815 internal_error(state, 0, "missing %d blocks",
15816 bb->last_vertex - vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000015817 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015818
15819 /* Step 2 compute the semidominators */
15820 /* Step 3 implicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015821 compute_spdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015822 /* Step 4 explicitly define the immediate dominator of each vertex */
Eric Biederman90089602004-05-28 14:11:54 +000015823 compute_ipdom(state, bb, sd);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015824 xfree(sd);
15825}
15826
15827
15828
15829static void find_block_domf(struct compile_state *state, struct block *block)
15830{
15831 struct block *child;
Eric Biederman5ade04a2003-10-22 04:03:46 +000015832 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015833 if (block->domfrontier != 0) {
15834 internal_error(state, block->first, "domfrontier present?");
15835 }
15836 for(user = block->idominates; user; user = user->next) {
15837 child = user->member;
15838 if (child->idom != block) {
15839 internal_error(state, block->first, "bad idom");
15840 }
15841 find_block_domf(state, child);
15842 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000015843 for(edge = block->edges; edge; edge = edge->next) {
15844 if (edge->member->idom != block) {
15845 domf_block(block, edge->member);
15846 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015847 }
15848 for(user = block->idominates; user; user = user->next) {
15849 struct block_set *frontier;
15850 child = user->member;
15851 for(frontier = child->domfrontier; frontier; frontier = frontier->next) {
15852 if (frontier->member->idom != block) {
15853 domf_block(block, frontier->member);
15854 }
15855 }
15856 }
15857}
15858
15859static void find_block_ipdomf(struct compile_state *state, struct block *block)
15860{
15861 struct block *child;
15862 struct block_set *user;
15863 if (block->ipdomfrontier != 0) {
15864 internal_error(state, block->first, "ipdomfrontier present?");
15865 }
15866 for(user = block->ipdominates; user; user = user->next) {
15867 child = user->member;
15868 if (child->ipdom != block) {
15869 internal_error(state, block->first, "bad ipdom");
15870 }
15871 find_block_ipdomf(state, child);
15872 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015873 for(user = block->use; user; user = user->next) {
15874 if (user->member->ipdom != block) {
15875 ipdomf_block(block, user->member);
15876 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015877 }
Eric Biederman83b991a2003-10-11 06:20:25 +000015878 for(user = block->ipdominates; user; user = user->next) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000015879 struct block_set *frontier;
15880 child = user->member;
15881 for(frontier = child->ipdomfrontier; frontier; frontier = frontier->next) {
15882 if (frontier->member->ipdom != block) {
15883 ipdomf_block(block, frontier->member);
15884 }
15885 }
15886 }
15887}
15888
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015889static void print_dominated(
15890 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015891{
15892 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015893 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015894
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015895 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015896 for(user = block->idominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015897 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015898 if (user->member->idom != block) {
15899 internal_error(state, user->member->first, "bad idom");
15900 }
15901 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015902 fprintf(fp,"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000015903}
15904
Eric Biederman5ade04a2003-10-22 04:03:46 +000015905static void print_dominated2(
15906 struct compile_state *state, FILE *fp, int depth, struct block *block)
15907{
15908 struct block_set *user;
15909 struct triple *ins;
15910 struct occurance *ptr, *ptr2;
15911 const char *filename1, *filename2;
15912 int equal_filenames;
15913 int i;
15914 for(i = 0; i < depth; i++) {
15915 fprintf(fp, " ");
15916 }
15917 fprintf(fp, "%3d: %p (%p - %p) @",
15918 block->vertex, block, block->first, block->last);
15919 ins = block->first;
15920 while(ins != block->last && (ins->occurance->line == 0)) {
15921 ins = ins->next;
15922 }
15923 ptr = ins->occurance;
15924 ptr2 = block->last->occurance;
15925 filename1 = ptr->filename? ptr->filename : "";
15926 filename2 = ptr2->filename? ptr2->filename : "";
15927 equal_filenames = (strcmp(filename1, filename2) == 0);
15928 if ((ptr == ptr2) || (equal_filenames && ptr->line == ptr2->line)) {
15929 fprintf(fp, " %s:%d", ptr->filename, ptr->line);
15930 } else if (equal_filenames) {
15931 fprintf(fp, " %s:(%d - %d)",
15932 ptr->filename, ptr->line, ptr2->line);
15933 } else {
15934 fprintf(fp, " (%s:%d - %s:%d)",
15935 ptr->filename, ptr->line,
15936 ptr2->filename, ptr2->line);
15937 }
15938 fprintf(fp, "\n");
15939 for(user = block->idominates; user; user = user->next) {
15940 print_dominated2(state, fp, depth + 1, user->member);
15941 }
15942}
15943
Eric Biederman90089602004-05-28 14:11:54 +000015944static void print_dominators(struct compile_state *state, FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015945{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015946 fprintf(fp, "\ndominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015947 walk_blocks(state, bb, print_dominated, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015948 fprintf(fp, "dominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000015949 print_dominated2(state, fp, 0, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015950}
15951
15952
15953static int print_frontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000015954 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015955{
Eric Biederman5ade04a2003-10-22 04:03:46 +000015956 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000015957
15958 if (!block || (block->vertex != vertex + 1)) {
15959 return vertex;
15960 }
15961 vertex += 1;
15962
Eric Biederman7dea9552004-06-29 05:38:37 +000015963 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015964 for(user = block->domfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015965 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015966 }
Eric Biederman7dea9552004-06-29 05:38:37 +000015967 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000015968
15969 for(edge = block->edges; edge; edge = edge->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000015970 vertex = print_frontiers(state, fp, edge->member, vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000015971 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000015972 return vertex;
15973}
Eric Biederman90089602004-05-28 14:11:54 +000015974static void print_dominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000015975 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015976{
Eric Biederman7dea9552004-06-29 05:38:37 +000015977 fprintf(fp, "\ndominance frontiers\n");
15978 print_frontiers(state, fp, bb->first_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015979
15980}
15981
Eric Biederman90089602004-05-28 14:11:54 +000015982static void analyze_idominators(struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015983{
15984 /* Find the immediate dominators */
Eric Biederman90089602004-05-28 14:11:54 +000015985 find_immediate_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015986 /* Find the dominance frontiers */
Eric Biederman90089602004-05-28 14:11:54 +000015987 find_block_domf(state, bb->first_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015988 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000015989 if (state->compiler->debug & DEBUG_FDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000015990 print_dominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000015991 print_dominance_frontiers(state, state->dbgout, bb);
15992 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000015993 }
15994}
15995
15996
Eric Biederman6aa31cc2003-06-10 21:22:07 +000015997static void print_ipdominated(
15998 struct compile_state *state, struct block *block, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000015999{
16000 struct block_set *user;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016001 FILE *fp = arg;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016002
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016003 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016004 for(user = block->ipdominates; user; user = user->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016005 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016006 if (user->member->ipdom != block) {
16007 internal_error(state, user->member->first, "bad ipdom");
16008 }
16009 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016010 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016011}
16012
Eric Biederman90089602004-05-28 14:11:54 +000016013static void print_ipdominators(struct compile_state *state, FILE *fp,
16014 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016015{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016016 fprintf(fp, "\nipdominates\n");
Eric Biederman90089602004-05-28 14:11:54 +000016017 walk_blocks(state, bb, print_ipdominated, fp);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016018}
16019
16020static int print_pfrontiers(
Eric Biederman7dea9552004-06-29 05:38:37 +000016021 struct compile_state *state, FILE *fp, struct block *block, int vertex)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016022{
16023 struct block_set *user;
16024
16025 if (!block || (block->vertex != vertex + 1)) {
16026 return vertex;
16027 }
16028 vertex += 1;
16029
Eric Biederman7dea9552004-06-29 05:38:37 +000016030 fprintf(fp, "%d:", block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016031 for(user = block->ipdomfrontier; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016032 fprintf(fp, " %d", user->member->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016033 }
Eric Biederman7dea9552004-06-29 05:38:37 +000016034 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016035 for(user = block->use; user; user = user->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000016036 vertex = print_pfrontiers(state, fp, user->member, vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016037 }
16038 return vertex;
16039}
Eric Biederman90089602004-05-28 14:11:54 +000016040static void print_ipdominance_frontiers(struct compile_state *state,
Eric Biederman7dea9552004-06-29 05:38:37 +000016041 FILE *fp, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016042{
Eric Biederman7dea9552004-06-29 05:38:37 +000016043 fprintf(fp, "\nipdominance frontiers\n");
16044 print_pfrontiers(state, fp, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016045
16046}
16047
Eric Biederman90089602004-05-28 14:11:54 +000016048static void analyze_ipdominators(struct compile_state *state,
16049 struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016050{
16051 /* Find the post dominators */
Eric Biederman90089602004-05-28 14:11:54 +000016052 find_post_dominators(state, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016053 /* Find the control dependencies (post dominance frontiers) */
Eric Biederman90089602004-05-28 14:11:54 +000016054 find_block_ipdomf(state, bb->last_block);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016055 /* If debuging print the print what I have just found */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016056 if (state->compiler->debug & DEBUG_RDOMINATORS) {
Eric Biederman90089602004-05-28 14:11:54 +000016057 print_ipdominators(state, state->dbgout, bb);
Eric Biederman7dea9552004-06-29 05:38:37 +000016058 print_ipdominance_frontiers(state, state->dbgout, bb);
16059 print_control_flow(state, state->dbgout, bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016060 }
16061}
16062
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016063static int bdominates(struct compile_state *state,
16064 struct block *dom, struct block *sub)
16065{
16066 while(sub && (sub != dom)) {
16067 sub = sub->idom;
16068 }
16069 return sub == dom;
16070}
16071
16072static int tdominates(struct compile_state *state,
16073 struct triple *dom, struct triple *sub)
16074{
16075 struct block *bdom, *bsub;
16076 int result;
16077 bdom = block_of_triple(state, dom);
16078 bsub = block_of_triple(state, sub);
16079 if (bdom != bsub) {
16080 result = bdominates(state, bdom, bsub);
16081 }
16082 else {
16083 struct triple *ins;
Eric Biederman7dea9552004-06-29 05:38:37 +000016084 if (!bdom || !bsub) {
16085 internal_error(state, dom, "huh?");
16086 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016087 ins = sub;
16088 while((ins != bsub->first) && (ins != dom)) {
16089 ins = ins->prev;
16090 }
16091 result = (ins == dom);
16092 }
16093 return result;
16094}
16095
Eric Biederman90089602004-05-28 14:11:54 +000016096static void analyze_basic_blocks(
16097 struct compile_state *state, struct basic_blocks *bb)
Eric Biederman83b991a2003-10-11 06:20:25 +000016098{
Eric Biederman90089602004-05-28 14:11:54 +000016099 setup_basic_blocks(state, bb);
16100 analyze_idominators(state, bb);
16101 analyze_ipdominators(state, bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016102}
16103
Eric Biedermanb138ac82003-04-22 18:44:01 +000016104static void insert_phi_operations(struct compile_state *state)
16105{
16106 size_t size;
16107 struct triple *first;
16108 int *has_already, *work;
16109 struct block *work_list, **work_list_tail;
16110 int iter;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016111 struct triple *var, *vnext;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016112
Eric Biederman90089602004-05-28 14:11:54 +000016113 size = sizeof(int) * (state->bb.last_vertex + 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016114 has_already = xcmalloc(size, "has_already");
16115 work = xcmalloc(size, "work");
16116 iter = 0;
16117
Eric Biederman83b991a2003-10-11 06:20:25 +000016118 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016119 for(var = first->next; var != first ; var = vnext) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016120 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016121 struct triple_set *user, *unext;
16122 vnext = var->next;
Eric Biederman90089602004-05-28 14:11:54 +000016123
16124 if (!triple_is_auto_var(state, var) || !var->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016125 continue;
16126 }
Eric Biederman90089602004-05-28 14:11:54 +000016127
Eric Biedermanb138ac82003-04-22 18:44:01 +000016128 iter += 1;
16129 work_list = 0;
16130 work_list_tail = &work_list;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016131 for(user = var->use; user; user = unext) {
16132 unext = user->next;
Eric Biederman90089602004-05-28 14:11:54 +000016133 if (MISC(var, 0) == user->member) {
16134 continue;
16135 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016136 if (user->member->op == OP_READ) {
16137 continue;
16138 }
16139 if (user->member->op != OP_WRITE) {
16140 internal_error(state, user->member,
16141 "bad variable access");
16142 }
16143 block = user->member->u.block;
16144 if (!block) {
16145 warning(state, user->member, "dead code");
Eric Biedermand1ea5392003-06-28 06:49:45 +000016146 release_triple(state, user->member);
16147 continue;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016148 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000016149 if (work[block->vertex] >= iter) {
16150 continue;
16151 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016152 work[block->vertex] = iter;
16153 *work_list_tail = block;
16154 block->work_next = 0;
16155 work_list_tail = &block->work_next;
16156 }
16157 for(block = work_list; block; block = block->work_next) {
16158 struct block_set *df;
16159 for(df = block->domfrontier; df; df = df->next) {
16160 struct triple *phi;
16161 struct block *front;
16162 int in_edges;
16163 front = df->member;
16164
16165 if (has_already[front->vertex] >= iter) {
16166 continue;
16167 }
16168 /* Count how many edges flow into this block */
16169 in_edges = front->users;
16170 /* Insert a phi function for this variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016171 get_occurance(var->occurance);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016172 phi = alloc_triple(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016173 state, OP_PHI, var->type, -1, in_edges,
Eric Biederman66fe2222003-07-04 15:14:04 +000016174 var->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016175 phi->u.block = front;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016176 MISC(phi, 0) = var;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016177 use_triple(var, phi);
Eric Biederman90089602004-05-28 14:11:54 +000016178#if 1
16179 if (phi->rhs != in_edges) {
16180 internal_error(state, phi, "phi->rhs: %d != in_edges: %d",
16181 phi->rhs, in_edges);
16182 }
16183#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000016184 /* Insert the phi functions immediately after the label */
16185 insert_triple(state, front->first->next, phi);
16186 if (front->first == front->last) {
16187 front->last = front->first->next;
16188 }
16189 has_already[front->vertex] = iter;
Eric Biederman83b991a2003-10-11 06:20:25 +000016190 transform_to_arch_instruction(state, phi);
Eric Biederman05f26fc2003-06-11 21:55:00 +000016191
Eric Biedermanb138ac82003-04-22 18:44:01 +000016192 /* If necessary plan to visit the basic block */
16193 if (work[front->vertex] >= iter) {
16194 continue;
16195 }
16196 work[front->vertex] = iter;
16197 *work_list_tail = front;
16198 front->work_next = 0;
16199 work_list_tail = &front->work_next;
16200 }
16201 }
16202 }
16203 xfree(has_already);
16204 xfree(work);
16205}
16206
Eric Biederman66fe2222003-07-04 15:14:04 +000016207
Eric Biederman83b991a2003-10-11 06:20:25 +000016208struct stack {
16209 struct triple_set *top;
16210 unsigned orig_id;
16211};
16212
Eric Biederman90089602004-05-28 14:11:54 +000016213static int count_auto_vars(struct compile_state *state)
Eric Biederman66fe2222003-07-04 15:14:04 +000016214{
16215 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016216 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016217 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016218 ins = first;
16219 do {
Eric Biederman90089602004-05-28 14:11:54 +000016220 if (triple_is_auto_var(state, ins)) {
16221 auto_vars += 1;
Eric Biederman66fe2222003-07-04 15:14:04 +000016222 }
16223 ins = ins->next;
16224 } while(ins != first);
Eric Biederman90089602004-05-28 14:11:54 +000016225 return auto_vars;
Eric Biederman66fe2222003-07-04 15:14:04 +000016226}
16227
Eric Biederman90089602004-05-28 14:11:54 +000016228static void number_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016229{
16230 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000016231 int auto_vars = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016232 first = state->first;
16233 ins = first;
16234 do {
Eric Biederman90089602004-05-28 14:11:54 +000016235 if (triple_is_auto_var(state, ins)) {
16236 auto_vars += 1;
16237 stacks[auto_vars].orig_id = ins->id;
16238 ins->id = auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016239 }
16240 ins = ins->next;
16241 } while(ins != first);
16242}
16243
Eric Biederman90089602004-05-28 14:11:54 +000016244static void restore_auto_vars(struct compile_state *state, struct stack *stacks)
Eric Biederman83b991a2003-10-11 06:20:25 +000016245{
16246 struct triple *first, *ins;
16247 first = state->first;
16248 ins = first;
16249 do {
Eric Biederman90089602004-05-28 14:11:54 +000016250 if (triple_is_auto_var(state, ins)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016251 ins->id = stacks[ins->id].orig_id;
16252 }
16253 ins = ins->next;
16254 } while(ins != first);
16255}
16256
16257static struct triple *peek_triple(struct stack *stacks, struct triple *var)
Eric Biederman66fe2222003-07-04 15:14:04 +000016258{
16259 struct triple_set *head;
16260 struct triple *top_val;
16261 top_val = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016262 head = stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016263 if (head) {
16264 top_val = head->member;
16265 }
16266 return top_val;
16267}
16268
Eric Biederman83b991a2003-10-11 06:20:25 +000016269static void push_triple(struct stack *stacks, struct triple *var, struct triple *val)
Eric Biederman66fe2222003-07-04 15:14:04 +000016270{
16271 struct triple_set *new;
16272 /* Append new to the head of the list,
16273 * it's the only sensible behavoir for a stack.
16274 */
16275 new = xcmalloc(sizeof(*new), "triple_set");
16276 new->member = val;
Eric Biederman83b991a2003-10-11 06:20:25 +000016277 new->next = stacks[var->id].top;
16278 stacks[var->id].top = new;
Eric Biederman66fe2222003-07-04 15:14:04 +000016279}
16280
Eric Biederman83b991a2003-10-11 06:20:25 +000016281static void pop_triple(struct stack *stacks, struct triple *var, struct triple *oldval)
Eric Biederman66fe2222003-07-04 15:14:04 +000016282{
16283 struct triple_set *set, **ptr;
Eric Biederman83b991a2003-10-11 06:20:25 +000016284 ptr = &stacks[var->id].top;
Eric Biederman66fe2222003-07-04 15:14:04 +000016285 while(*ptr) {
16286 set = *ptr;
16287 if (set->member == oldval) {
16288 *ptr = set->next;
16289 xfree(set);
16290 /* Only free one occurance from the stack */
16291 return;
16292 }
16293 else {
16294 ptr = &set->next;
16295 }
16296 }
16297}
16298
Eric Biedermanb138ac82003-04-22 18:44:01 +000016299/*
16300 * C(V)
16301 * S(V)
16302 */
16303static void fixup_block_phi_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016304 struct compile_state *state, struct stack *stacks, struct block *parent, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016305{
16306 struct block_set *set;
16307 struct triple *ptr;
16308 int edge;
16309 if (!parent || !block)
16310 return;
16311 /* Find the edge I am coming in on */
16312 edge = 0;
16313 for(set = block->use; set; set = set->next, edge++) {
16314 if (set->member == parent) {
16315 break;
16316 }
16317 }
16318 if (!set) {
16319 internal_error(state, 0, "phi input is not on a control predecessor");
16320 }
16321 for(ptr = block->first; ; ptr = ptr->next) {
16322 if (ptr->op == OP_PHI) {
16323 struct triple *var, *val, **slot;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016324 var = MISC(ptr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016325 if (!var) {
16326 internal_error(state, ptr, "no var???");
16327 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016328 /* Find the current value of the variable */
Eric Biederman66fe2222003-07-04 15:14:04 +000016329 val = peek_triple(stacks, var);
16330 if (val && ((val->op == OP_WRITE) || (val->op == OP_READ))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016331 internal_error(state, val, "bad value in phi");
16332 }
Eric Biederman90089602004-05-28 14:11:54 +000016333 if (edge >= ptr->rhs) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000016334 internal_error(state, ptr, "edges > phi rhs");
16335 }
16336 slot = &RHS(ptr, edge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016337 if ((*slot != 0) && (*slot != val)) {
16338 internal_error(state, ptr, "phi already bound on this edge");
16339 }
16340 *slot = val;
16341 use_triple(val, ptr);
16342 }
16343 if (ptr == block->last) {
16344 break;
16345 }
16346 }
16347}
16348
16349
16350static void rename_block_variables(
Eric Biederman83b991a2003-10-11 06:20:25 +000016351 struct compile_state *state, struct stack *stacks, struct block *block)
Eric Biedermanb138ac82003-04-22 18:44:01 +000016352{
Eric Biederman5ade04a2003-10-22 04:03:46 +000016353 struct block_set *user, *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016354 struct triple *ptr, *next, *last;
16355 int done;
16356 if (!block)
16357 return;
16358 last = block->first;
16359 done = 0;
16360 for(ptr = block->first; !done; ptr = next) {
16361 next = ptr->next;
16362 if (ptr == block->last) {
16363 done = 1;
16364 }
16365 /* RHS(A) */
16366 if (ptr->op == OP_READ) {
16367 struct triple *var, *val;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016368 var = RHS(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016369 if (!triple_is_auto_var(state, var)) {
16370 internal_error(state, ptr, "read of non auto var!");
16371 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016372 unuse_triple(var, ptr);
Eric Biederman66fe2222003-07-04 15:14:04 +000016373 /* Find the current value of the variable */
16374 val = peek_triple(stacks, var);
16375 if (!val) {
Eric Biederman90089602004-05-28 14:11:54 +000016376 /* Let the optimizer at variables that are not initially
16377 * set. But give it a bogus value so things seem to
16378 * work by accident. This is useful for bitfields because
16379 * setting them always involves a read-modify-write.
16380 */
16381 if (TYPE_ARITHMETIC(ptr->type->type)) {
16382 val = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16383 val->u.cval = 0xdeadbeaf;
16384 } else {
16385 val = pre_triple(state, ptr, OP_UNKNOWNVAL, ptr->type, 0, 0);
16386 }
16387 }
16388 if (!val) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016389 error(state, ptr, "variable used without being set");
16390 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016391 if ((val->op == OP_WRITE) || (val->op == OP_READ)) {
16392 internal_error(state, val, "bad value in read");
16393 }
16394 propogate_use(state, ptr, val);
16395 release_triple(state, ptr);
16396 continue;
16397 }
16398 /* LHS(A) */
16399 if (ptr->op == OP_WRITE) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016400 struct triple *var, *val, *tval;
Eric Biederman90089602004-05-28 14:11:54 +000016401 var = MISC(ptr, 0);
16402 if (!triple_is_auto_var(state, var)) {
16403 internal_error(state, ptr, "write to non auto var!");
16404 }
16405 tval = val = RHS(ptr, 0);
16406 if ((val->op == OP_WRITE) || (val->op == OP_READ) ||
16407 triple_is_auto_var(state, val)) {
Eric Biederman678d8162003-07-03 03:59:38 +000016408 internal_error(state, ptr, "bad value in write");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016409 }
Eric Biederman90089602004-05-28 14:11:54 +000016410 /* Insert a cast if the types differ */
16411 if (!is_subset_type(ptr->type, val->type)) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000016412 if (val->op == OP_INTCONST) {
16413 tval = pre_triple(state, ptr, OP_INTCONST, ptr->type, 0, 0);
16414 tval->u.cval = val->u.cval;
16415 }
16416 else {
Eric Biederman90089602004-05-28 14:11:54 +000016417 tval = pre_triple(state, ptr, OP_CONVERT, ptr->type, val, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016418 use_triple(val, tval);
16419 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016420 transform_to_arch_instruction(state, tval);
Eric Biedermand1ea5392003-06-28 06:49:45 +000016421 unuse_triple(val, ptr);
Eric Biederman90089602004-05-28 14:11:54 +000016422 RHS(ptr, 0) = tval;
Eric Biedermand1ea5392003-06-28 06:49:45 +000016423 use_triple(tval, ptr);
16424 }
16425 propogate_use(state, ptr, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016426 unuse_triple(var, ptr);
16427 /* Push OP_WRITE ptr->right onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016428 push_triple(stacks, var, tval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016429 }
16430 if (ptr->op == OP_PHI) {
16431 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016432 var = MISC(ptr, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016433 if (!triple_is_auto_var(state, var)) {
16434 internal_error(state, ptr, "phi references non auto var!");
16435 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016436 /* Push OP_PHI onto a stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016437 push_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016438 }
16439 last = ptr;
16440 }
16441 block->last = last;
16442
16443 /* Fixup PHI functions in the cf successors */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016444 for(edge = block->edges; edge; edge = edge->next) {
16445 fixup_block_phi_variables(state, stacks, block, edge->member);
16446 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016447 /* rename variables in the dominated nodes */
16448 for(user = block->idominates; user; user = user->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000016449 rename_block_variables(state, stacks, user->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016450 }
16451 /* pop the renamed variable stack */
16452 last = block->first;
16453 done = 0;
16454 for(ptr = block->first; !done ; ptr = next) {
16455 next = ptr->next;
16456 if (ptr == block->last) {
16457 done = 1;
16458 }
16459 if (ptr->op == OP_WRITE) {
16460 struct triple *var;
Eric Biederman90089602004-05-28 14:11:54 +000016461 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016462 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman90089602004-05-28 14:11:54 +000016463 pop_triple(stacks, var, RHS(ptr, 0));
Eric Biedermanb138ac82003-04-22 18:44:01 +000016464 release_triple(state, ptr);
16465 continue;
16466 }
16467 if (ptr->op == OP_PHI) {
16468 struct triple *var;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016469 var = MISC(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016470 /* Pop OP_WRITE ptr->right from the stack of variable uses */
Eric Biederman66fe2222003-07-04 15:14:04 +000016471 pop_triple(stacks, var, ptr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016472 }
16473 last = ptr;
16474 }
16475 block->last = last;
16476}
16477
Eric Biederman83b991a2003-10-11 06:20:25 +000016478static void rename_variables(struct compile_state *state)
16479{
16480 struct stack *stacks;
Eric Biederman90089602004-05-28 14:11:54 +000016481 int auto_vars;
Eric Biederman83b991a2003-10-11 06:20:25 +000016482
16483 /* Allocate stacks for the Variables */
Eric Biederman90089602004-05-28 14:11:54 +000016484 auto_vars = count_auto_vars(state);
16485 stacks = xcmalloc(sizeof(stacks[0])*(auto_vars + 1), "auto var stacks");
Eric Biederman83b991a2003-10-11 06:20:25 +000016486
Eric Biederman90089602004-05-28 14:11:54 +000016487 /* Give each auto_var a stack */
16488 number_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016489
16490 /* Rename the variables */
Eric Biederman90089602004-05-28 14:11:54 +000016491 rename_block_variables(state, stacks, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016492
Eric Biederman90089602004-05-28 14:11:54 +000016493 /* Remove the stacks from the auto_vars */
16494 restore_auto_vars(state, stacks);
Eric Biederman83b991a2003-10-11 06:20:25 +000016495 xfree(stacks);
16496}
16497
Eric Biedermanb138ac82003-04-22 18:44:01 +000016498static void prune_block_variables(struct compile_state *state,
16499 struct block *block)
16500{
16501 struct block_set *user;
Eric Biederman90089602004-05-28 14:11:54 +000016502 struct triple *next, *ptr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016503 int done;
Eric Biederman90089602004-05-28 14:11:54 +000016504
Eric Biedermanb138ac82003-04-22 18:44:01 +000016505 done = 0;
16506 for(ptr = block->first; !done; ptr = next) {
Eric Biederman90089602004-05-28 14:11:54 +000016507 /* Be extremely careful I am deleting the list
16508 * as I walk trhough it.
16509 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016510 next = ptr->next;
16511 if (ptr == block->last) {
16512 done = 1;
16513 }
Eric Biederman90089602004-05-28 14:11:54 +000016514 if (triple_is_auto_var(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016515 struct triple_set *user, *next;
16516 for(user = ptr->use; user; user = next) {
16517 struct triple *use;
16518 next = user->next;
16519 use = user->member;
Eric Biederman90089602004-05-28 14:11:54 +000016520 if (MISC(ptr, 0) == user->member) {
16521 continue;
16522 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016523 if (use->op != OP_PHI) {
16524 internal_error(state, use, "decl still used");
16525 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000016526 if (MISC(use, 0) != ptr) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000016527 internal_error(state, use, "bad phi use of decl");
16528 }
16529 unuse_triple(ptr, use);
Eric Biederman0babc1c2003-05-09 02:39:00 +000016530 MISC(use, 0) = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016531 }
Eric Biederman90089602004-05-28 14:11:54 +000016532 if ((ptr->u.cval == 0) && (MISC(ptr, 0)->lhs == 1)) {
16533 /* Delete the adecl */
16534 release_triple(state, MISC(ptr, 0));
16535 /* And the piece */
16536 release_triple(state, ptr);
16537 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016538 continue;
16539 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016540 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016541 for(user = block->idominates; user; user = user->next) {
16542 prune_block_variables(state, user->member);
16543 }
16544}
16545
Eric Biederman66fe2222003-07-04 15:14:04 +000016546struct phi_triple {
16547 struct triple *phi;
16548 unsigned orig_id;
16549 int alive;
16550};
16551
16552static void keep_phi(struct compile_state *state, struct phi_triple *live, struct triple *phi)
16553{
16554 struct triple **slot;
16555 int zrhs, i;
16556 if (live[phi->id].alive) {
16557 return;
16558 }
16559 live[phi->id].alive = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016560 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016561 slot = &RHS(phi, 0);
16562 for(i = 0; i < zrhs; i++) {
16563 struct triple *used;
16564 used = slot[i];
16565 if (used && (used->op == OP_PHI)) {
16566 keep_phi(state, live, used);
16567 }
16568 }
16569}
16570
16571static void prune_unused_phis(struct compile_state *state)
16572{
16573 struct triple *first, *phi;
16574 struct phi_triple *live;
16575 int phis, i;
16576
Eric Biederman66fe2222003-07-04 15:14:04 +000016577 /* Find the first instruction */
Eric Biederman83b991a2003-10-11 06:20:25 +000016578 first = state->first;
Eric Biederman66fe2222003-07-04 15:14:04 +000016579
16580 /* Count how many phi functions I need to process */
16581 phis = 0;
16582 for(phi = first->next; phi != first; phi = phi->next) {
16583 if (phi->op == OP_PHI) {
16584 phis += 1;
16585 }
16586 }
16587
16588 /* Mark them all dead */
16589 live = xcmalloc(sizeof(*live) * (phis + 1), "phi_triple");
16590 phis = 0;
16591 for(phi = first->next; phi != first; phi = phi->next) {
16592 if (phi->op != OP_PHI) {
16593 continue;
16594 }
16595 live[phis].alive = 0;
16596 live[phis].orig_id = phi->id;
16597 live[phis].phi = phi;
16598 phi->id = phis;
16599 phis += 1;
16600 }
16601
16602 /* Mark phis alive that are used by non phis */
16603 for(i = 0; i < phis; i++) {
16604 struct triple_set *set;
16605 for(set = live[i].phi->use; !live[i].alive && set; set = set->next) {
16606 if (set->member->op != OP_PHI) {
16607 keep_phi(state, live, live[i].phi);
16608 break;
16609 }
16610 }
16611 }
16612
16613 /* Delete the extraneous phis */
16614 for(i = 0; i < phis; i++) {
16615 struct triple **slot;
16616 int zrhs, j;
16617 if (!live[i].alive) {
16618 release_triple(state, live[i].phi);
16619 continue;
16620 }
16621 phi = live[i].phi;
16622 slot = &RHS(phi, 0);
Eric Biederman90089602004-05-28 14:11:54 +000016623 zrhs = phi->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000016624 for(j = 0; j < zrhs; j++) {
16625 if(!slot[j]) {
Eric Biederman90089602004-05-28 14:11:54 +000016626 struct triple *unknown;
16627 get_occurance(phi->occurance);
16628 unknown = flatten(state, state->global_pool,
16629 alloc_triple(state, OP_UNKNOWNVAL,
16630 phi->type, 0, 0, phi->occurance));
16631 slot[j] = unknown;
16632 use_triple(unknown, phi);
16633 transform_to_arch_instruction(state, unknown);
16634#if 0
16635 warning(state, phi, "variable not set at index %d on all paths to use", j);
16636#endif
Eric Biederman66fe2222003-07-04 15:14:04 +000016637 }
16638 }
16639 }
16640 xfree(live);
16641}
16642
Eric Biedermanb138ac82003-04-22 18:44:01 +000016643static void transform_to_ssa_form(struct compile_state *state)
16644{
16645 insert_phi_operations(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000016646 rename_variables(state);
Eric Biederman66fe2222003-07-04 15:14:04 +000016647
Eric Biederman90089602004-05-28 14:11:54 +000016648 prune_block_variables(state, state->bb.first_block);
Eric Biederman66fe2222003-07-04 15:14:04 +000016649 prune_unused_phis(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000016650
Eric Biederman90089602004-05-28 14:11:54 +000016651 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016652}
16653
16654
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016655static void clear_vertex(
16656 struct compile_state *state, struct block *block, void *arg)
16657{
Eric Biederman83b991a2003-10-11 06:20:25 +000016658 /* Clear the current blocks vertex and the vertex of all
16659 * of the current blocks neighbors in case there are malformed
16660 * blocks with now instructions at this point.
16661 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000016662 struct block_set *user, *edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016663 block->vertex = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000016664 for(edge = block->edges; edge; edge = edge->next) {
16665 edge->member->vertex = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000016666 }
16667 for(user = block->use; user; user = user->next) {
16668 user->member->vertex = 0;
16669 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016670}
16671
16672static void mark_live_block(
16673 struct compile_state *state, struct block *block, int *next_vertex)
16674{
16675 /* See if this is a block that has not been marked */
16676 if (block->vertex != 0) {
16677 return;
16678 }
16679 block->vertex = *next_vertex;
16680 *next_vertex += 1;
16681 if (triple_is_branch(state, block->last)) {
16682 struct triple **targ;
Eric Biederman90089602004-05-28 14:11:54 +000016683 targ = triple_edge_targ(state, block->last, 0);
16684 for(; targ; targ = triple_edge_targ(state, block->last, targ)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016685 if (!*targ) {
16686 continue;
16687 }
16688 if (!triple_stores_block(state, *targ)) {
16689 internal_error(state, 0, "bad targ");
16690 }
16691 mark_live_block(state, (*targ)->u.block, next_vertex);
16692 }
Eric Biederman90089602004-05-28 14:11:54 +000016693 /* Ensure the last block of a function remains alive */
16694 if (triple_is_call(state, block->last)) {
16695 mark_live_block(state, MISC(block->last, 0)->u.block, next_vertex);
16696 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016697 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016698 else if (block->last->next != state->first) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016699 struct triple *ins;
16700 ins = block->last->next;
16701 if (!triple_stores_block(state, ins)) {
16702 internal_error(state, 0, "bad block start");
16703 }
16704 mark_live_block(state, ins->u.block, next_vertex);
16705 }
16706}
16707
Eric Biedermanb138ac82003-04-22 18:44:01 +000016708static void transform_from_ssa_form(struct compile_state *state)
16709{
16710 /* To get out of ssa form we insert moves on the incoming
16711 * edges to blocks containting phi functions.
16712 */
16713 struct triple *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000016714 struct triple *phi, *var, *next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016715 int next_vertex;
16716
16717 /* Walk the control flow to see which blocks remain alive */
Eric Biederman90089602004-05-28 14:11:54 +000016718 walk_blocks(state, &state->bb, clear_vertex, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016719 next_vertex = 1;
Eric Biederman90089602004-05-28 14:11:54 +000016720 mark_live_block(state, state->bb.first_block, &next_vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016721
16722 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000016723 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016724 for(phi = first->next; phi != first ; phi = next) {
16725 struct block_set *set;
16726 struct block *block;
16727 struct triple **slot;
Eric Biederman83b991a2003-10-11 06:20:25 +000016728 struct triple *var;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016729 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016730 int edge, writers, readers;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016731 next = phi->next;
16732 if (phi->op != OP_PHI) {
16733 continue;
16734 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016735
Eric Biedermanb138ac82003-04-22 18:44:01 +000016736 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000016737 slot = &RHS(phi, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016738
Eric Biederman83b991a2003-10-11 06:20:25 +000016739 /* If this phi is in a dead block just forget it */
16740 if (block->vertex == 0) {
16741 release_triple(state, phi);
16742 continue;
16743 }
16744
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016745 /* Forget uses from code in dead blocks */
16746 for(use = phi->use; use; use = use_next) {
16747 struct block *ublock;
16748 struct triple **expr;
16749 use_next = use->next;
16750 ublock = block_of_triple(state, use->member);
16751 if ((use->member == phi) || (ublock->vertex != 0)) {
16752 continue;
16753 }
16754 expr = triple_rhs(state, use->member, 0);
16755 for(; expr; expr = triple_rhs(state, use->member, expr)) {
16756 if (*expr == phi) {
16757 *expr = 0;
16758 }
16759 }
16760 unuse_triple(phi, use->member);
16761 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016762 /* A variable to replace the phi function */
Eric Biederman90089602004-05-28 14:11:54 +000016763 if (registers_of(state, phi->type) != 1) {
16764 internal_error(state, phi, "phi->type does not fit in a single register!");
16765 }
16766 var = post_triple(state, phi, OP_ADECL, phi->type, 0, 0);
16767 var = var->next; /* point at the var */
16768
Eric Biederman83b991a2003-10-11 06:20:25 +000016769 /* Replaces use of phi with var */
16770 propogate_use(state, phi, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016771
Eric Biederman90089602004-05-28 14:11:54 +000016772 /* Count the readers */
16773 readers = 0;
16774 for(use = var->use; use; use = use->next) {
16775 if (use->member != MISC(var, 0)) {
16776 readers++;
16777 }
16778 }
16779
Eric Biedermanb138ac82003-04-22 18:44:01 +000016780 /* Walk all of the incoming edges/blocks and insert moves.
16781 */
Eric Biederman90089602004-05-28 14:11:54 +000016782 writers = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016783 for(edge = 0, set = block->use; set; set = set->next, edge++) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016784 struct block *eblock, *vblock;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016785 struct triple *move;
Eric Biederman530b5192003-07-01 10:05:30 +000016786 struct triple *val, *base;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016787 eblock = set->member;
16788 val = slot[edge];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016789 slot[edge] = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000016790 unuse_triple(val, phi);
Eric Biederman83b991a2003-10-11 06:20:25 +000016791 vblock = block_of_triple(state, val);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016792
Eric Biederman83b991a2003-10-11 06:20:25 +000016793 /* If we don't have a value that belongs in an OP_WRITE
16794 * continue on.
16795 */
Eric Biederman90089602004-05-28 14:11:54 +000016796 if (!val || (val == &unknown_triple) || (val == phi)
16797 || (vblock && (vblock->vertex == 0))) {
16798 continue;
16799 }
16800 /* If the value should never occur error */
16801 if (!vblock) {
16802 internal_error(state, val, "no vblock?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000016803 continue;
16804 }
Eric Biederman83b991a2003-10-11 06:20:25 +000016805
16806 /* If the value occurs in a dead block see if a replacement
16807 * block can be found.
16808 */
16809 while(eblock && (eblock->vertex == 0)) {
16810 eblock = eblock->idom;
16811 }
16812 /* If not continue on with the next value. */
16813 if (!eblock || (eblock->vertex == 0)) {
16814 continue;
16815 }
16816
16817 /* If we have an empty incoming block ignore it. */
16818 if (!eblock->first) {
16819 internal_error(state, 0, "empty block?");
16820 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016821
Eric Biederman530b5192003-07-01 10:05:30 +000016822 /* Make certain the write is placed in the edge block... */
Eric Biederman90089602004-05-28 14:11:54 +000016823 /* Walk through the edge block backwards to find an
16824 * appropriate location for the OP_WRITE.
16825 */
16826 for(base = eblock->last; base != eblock->first; base = base->prev) {
16827 struct triple **expr;
16828 if (base->op == OP_PIECE) {
16829 base = MISC(base, 0);
16830 }
16831 if ((base == var) || (base == val)) {
16832 goto out;
16833 }
16834 expr = triple_lhs(state, base, 0);
16835 for(; expr; expr = triple_lhs(state, base, expr)) {
16836 if ((*expr) == val) {
16837 goto out;
16838 }
16839 }
16840 expr = triple_rhs(state, base, 0);
16841 for(; expr; expr = triple_rhs(state, base, expr)) {
16842 if ((*expr) == var) {
16843 goto out;
16844 }
16845 }
Eric Biederman530b5192003-07-01 10:05:30 +000016846 }
Eric Biederman90089602004-05-28 14:11:54 +000016847 out:
16848 if (triple_is_branch(state, base)) {
16849 internal_error(state, base,
16850 "Could not insert write to phi");
16851 }
16852 move = post_triple(state, base, OP_WRITE, var->type, val, var);
Eric Biedermanb138ac82003-04-22 18:44:01 +000016853 use_triple(val, move);
16854 use_triple(var, move);
Eric Biederman90089602004-05-28 14:11:54 +000016855 writers++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016856 }
Eric Biederman90089602004-05-28 14:11:54 +000016857 if (!writers && readers) {
16858 internal_error(state, var, "no value written to in use phi?");
16859 }
16860 /* If var is not used free it */
16861 if (!writers) {
16862 release_triple(state, MISC(var, 0));
16863 release_triple(state, var);
16864 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016865 /* Release the phi function */
Eric Biedermanb138ac82003-04-22 18:44:01 +000016866 release_triple(state, phi);
16867 }
16868
Eric Biederman83b991a2003-10-11 06:20:25 +000016869 /* Walk all of the operations to find the adecls */
16870 for(var = first->next; var != first ; var = var->next) {
16871 struct triple_set *use, *use_next;
Eric Biederman90089602004-05-28 14:11:54 +000016872 if (!triple_is_auto_var(state, var)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016873 continue;
16874 }
16875
16876 /* Walk through all of the rhs uses of var and
16877 * replace them with read of var.
16878 */
16879 for(use = var->use; use; use = use_next) {
16880 struct triple *read, *user;
16881 struct triple **slot;
16882 int zrhs, i, used;
16883 use_next = use->next;
16884 user = use->member;
16885
16886 /* Generate a read of var */
16887 read = pre_triple(state, user, OP_READ, var->type, var, 0);
16888 use_triple(var, read);
16889
16890 /* Find the rhs uses and see if they need to be replaced */
16891 used = 0;
Eric Biederman90089602004-05-28 14:11:54 +000016892 zrhs = user->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000016893 slot = &RHS(user, 0);
16894 for(i = 0; i < zrhs; i++) {
Eric Biederman90089602004-05-28 14:11:54 +000016895 if (slot[i] == var) {
Eric Biederman83b991a2003-10-11 06:20:25 +000016896 slot[i] = read;
16897 used = 1;
16898 }
16899 }
16900 /* If we did use it cleanup the uses */
16901 if (used) {
16902 unuse_triple(var, user);
16903 use_triple(read, user);
16904 }
16905 /* If we didn't use it release the extra triple */
16906 else {
16907 release_triple(state, read);
16908 }
16909 }
16910 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000016911}
16912
Eric Biederman5ade04a2003-10-22 04:03:46 +000016913#define HI() if (state->compiler->debug & DEBUG_REBUILD_SSA_FORM) { \
Eric Biederman90089602004-05-28 14:11:54 +000016914 FILE *fp = state->dbgout; \
16915 fprintf(fp, "@ %s:%d\n", __FILE__, __LINE__); romcc_print_blocks(state, fp); \
Eric Biederman5ade04a2003-10-22 04:03:46 +000016916 }
16917
Eric Biederman83b991a2003-10-11 06:20:25 +000016918static void rebuild_ssa_form(struct compile_state *state)
16919{
16920HI();
16921 transform_from_ssa_form(state);
16922HI();
Eric Biederman90089602004-05-28 14:11:54 +000016923 state->bb.first = state->first;
16924 free_basic_blocks(state, &state->bb);
16925 analyze_basic_blocks(state, &state->bb);
Eric Biederman83b991a2003-10-11 06:20:25 +000016926HI();
16927 insert_phi_operations(state);
16928HI();
16929 rename_variables(state);
16930HI();
16931
Eric Biederman90089602004-05-28 14:11:54 +000016932 prune_block_variables(state, state->bb.first_block);
Eric Biederman83b991a2003-10-11 06:20:25 +000016933HI();
16934 prune_unused_phis(state);
16935HI();
16936}
16937#undef HI
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016938
16939/*
16940 * Register conflict resolution
16941 * =========================================================
16942 */
16943
16944static struct reg_info find_def_color(
16945 struct compile_state *state, struct triple *def)
16946{
16947 struct triple_set *set;
16948 struct reg_info info;
16949 info.reg = REG_UNSET;
16950 info.regcm = 0;
16951 if (!triple_is_def(state, def)) {
16952 return info;
16953 }
16954 info = arch_reg_lhs(state, def, 0);
16955 if (info.reg >= MAX_REGISTERS) {
16956 info.reg = REG_UNSET;
16957 }
16958 for(set = def->use; set; set = set->next) {
16959 struct reg_info tinfo;
16960 int i;
16961 i = find_rhs_use(state, set->member, def);
16962 if (i < 0) {
16963 continue;
16964 }
16965 tinfo = arch_reg_rhs(state, set->member, i);
16966 if (tinfo.reg >= MAX_REGISTERS) {
16967 tinfo.reg = REG_UNSET;
16968 }
16969 if ((tinfo.reg != REG_UNSET) &&
16970 (info.reg != REG_UNSET) &&
16971 (tinfo.reg != info.reg)) {
16972 internal_error(state, def, "register conflict");
16973 }
16974 if ((info.regcm & tinfo.regcm) == 0) {
16975 internal_error(state, def, "regcm conflict %x & %x == 0",
16976 info.regcm, tinfo.regcm);
16977 }
16978 if (info.reg == REG_UNSET) {
16979 info.reg = tinfo.reg;
16980 }
16981 info.regcm &= tinfo.regcm;
16982 }
16983 if (info.reg >= MAX_REGISTERS) {
16984 internal_error(state, def, "register out of range");
16985 }
16986 return info;
16987}
16988
16989static struct reg_info find_lhs_pre_color(
16990 struct compile_state *state, struct triple *ins, int index)
16991{
16992 struct reg_info info;
16993 int zlhs, zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000016994 zrhs = ins->rhs;
16995 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000016996 if (!zlhs && triple_is_def(state, ins)) {
16997 zlhs = 1;
16998 }
16999 if (index >= zlhs) {
17000 internal_error(state, ins, "Bad lhs %d", index);
17001 }
17002 info = arch_reg_lhs(state, ins, index);
17003 for(i = 0; i < zrhs; i++) {
17004 struct reg_info rinfo;
17005 rinfo = arch_reg_rhs(state, ins, i);
17006 if ((info.reg == rinfo.reg) &&
17007 (rinfo.reg >= MAX_REGISTERS)) {
17008 struct reg_info tinfo;
17009 tinfo = find_lhs_pre_color(state, RHS(ins, index), 0);
17010 info.reg = tinfo.reg;
17011 info.regcm &= tinfo.regcm;
17012 break;
17013 }
17014 }
17015 if (info.reg >= MAX_REGISTERS) {
17016 info.reg = REG_UNSET;
17017 }
17018 return info;
17019}
17020
17021static struct reg_info find_rhs_post_color(
17022 struct compile_state *state, struct triple *ins, int index);
17023
17024static struct reg_info find_lhs_post_color(
17025 struct compile_state *state, struct triple *ins, int index)
17026{
17027 struct triple_set *set;
17028 struct reg_info info;
17029 struct triple *lhs;
Eric Biederman530b5192003-07-01 10:05:30 +000017030#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017031 fprintf(state->errout, "find_lhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017032 ins, index);
17033#endif
17034 if ((index == 0) && triple_is_def(state, ins)) {
17035 lhs = ins;
17036 }
Eric Biederman90089602004-05-28 14:11:54 +000017037 else if (index < ins->lhs) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017038 lhs = LHS(ins, index);
17039 }
17040 else {
17041 internal_error(state, ins, "Bad lhs %d", index);
17042 lhs = 0;
17043 }
17044 info = arch_reg_lhs(state, ins, index);
17045 if (info.reg >= MAX_REGISTERS) {
17046 info.reg = REG_UNSET;
17047 }
17048 for(set = lhs->use; set; set = set->next) {
17049 struct reg_info rinfo;
17050 struct triple *user;
17051 int zrhs, i;
17052 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000017053 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017054 for(i = 0; i < zrhs; i++) {
17055 if (RHS(user, i) != lhs) {
17056 continue;
17057 }
17058 rinfo = find_rhs_post_color(state, user, i);
17059 if ((info.reg != REG_UNSET) &&
17060 (rinfo.reg != REG_UNSET) &&
17061 (info.reg != rinfo.reg)) {
17062 internal_error(state, ins, "register conflict");
17063 }
17064 if ((info.regcm & rinfo.regcm) == 0) {
17065 internal_error(state, ins, "regcm conflict %x & %x == 0",
17066 info.regcm, rinfo.regcm);
17067 }
17068 if (info.reg == REG_UNSET) {
17069 info.reg = rinfo.reg;
17070 }
17071 info.regcm &= rinfo.regcm;
17072 }
17073 }
Eric Biederman530b5192003-07-01 10:05:30 +000017074#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017075 fprintf(state->errout, "find_lhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017076 ins, index, info.reg, info.regcm);
17077#endif
17078 return info;
17079}
17080
17081static struct reg_info find_rhs_post_color(
17082 struct compile_state *state, struct triple *ins, int index)
17083{
17084 struct reg_info info, rinfo;
17085 int zlhs, i;
Eric Biederman530b5192003-07-01 10:05:30 +000017086#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017087 fprintf(state->errout, "find_rhs_post_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017088 ins, index);
17089#endif
17090 rinfo = arch_reg_rhs(state, ins, index);
Eric Biederman90089602004-05-28 14:11:54 +000017091 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017092 if (!zlhs && triple_is_def(state, ins)) {
17093 zlhs = 1;
17094 }
17095 info = rinfo;
17096 if (info.reg >= MAX_REGISTERS) {
17097 info.reg = REG_UNSET;
17098 }
17099 for(i = 0; i < zlhs; i++) {
17100 struct reg_info linfo;
17101 linfo = arch_reg_lhs(state, ins, i);
17102 if ((linfo.reg == rinfo.reg) &&
17103 (linfo.reg >= MAX_REGISTERS)) {
17104 struct reg_info tinfo;
17105 tinfo = find_lhs_post_color(state, ins, i);
17106 if (tinfo.reg >= MAX_REGISTERS) {
17107 tinfo.reg = REG_UNSET;
17108 }
Eric Biederman530b5192003-07-01 10:05:30 +000017109 info.regcm &= linfo.regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017110 info.regcm &= tinfo.regcm;
17111 if (info.reg != REG_UNSET) {
17112 internal_error(state, ins, "register conflict");
17113 }
17114 if (info.regcm == 0) {
17115 internal_error(state, ins, "regcm conflict");
17116 }
17117 info.reg = tinfo.reg;
17118 }
17119 }
Eric Biederman530b5192003-07-01 10:05:30 +000017120#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017121 fprintf(state->errout, "find_rhs_post_color(%p, %d) -> ( %d, %x)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017122 ins, index, info.reg, info.regcm);
17123#endif
17124 return info;
17125}
17126
17127static struct reg_info find_lhs_color(
17128 struct compile_state *state, struct triple *ins, int index)
17129{
17130 struct reg_info pre, post, info;
Eric Biederman530b5192003-07-01 10:05:30 +000017131#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017132 fprintf(state->errout, "find_lhs_color(%p, %d)\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017133 ins, index);
17134#endif
17135 pre = find_lhs_pre_color(state, ins, index);
17136 post = find_lhs_post_color(state, ins, index);
17137 if ((pre.reg != post.reg) &&
17138 (pre.reg != REG_UNSET) &&
17139 (post.reg != REG_UNSET)) {
17140 internal_error(state, ins, "register conflict");
17141 }
17142 info.regcm = pre.regcm & post.regcm;
17143 info.reg = pre.reg;
17144 if (info.reg == REG_UNSET) {
17145 info.reg = post.reg;
17146 }
Eric Biederman530b5192003-07-01 10:05:30 +000017147#if DEBUG_TRIPLE_COLOR
Eric Biederman90089602004-05-28 14:11:54 +000017148 fprintf(state->errout, "find_lhs_color(%p, %d) -> ( %d, %x) ... (%d, %x) (%d, %x)\n",
Eric Biederman530b5192003-07-01 10:05:30 +000017149 ins, index, info.reg, info.regcm,
17150 pre.reg, pre.regcm, post.reg, post.regcm);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017151#endif
17152 return info;
17153}
17154
17155static struct triple *post_copy(struct compile_state *state, struct triple *ins)
17156{
17157 struct triple_set *entry, *next;
17158 struct triple *out;
17159 struct reg_info info, rinfo;
17160
17161 info = arch_reg_lhs(state, ins, 0);
17162 out = post_triple(state, ins, OP_COPY, ins->type, ins, 0);
17163 use_triple(RHS(out, 0), out);
17164 /* Get the users of ins to use out instead */
17165 for(entry = ins->use; entry; entry = next) {
17166 int i;
17167 next = entry->next;
17168 if (entry->member == out) {
17169 continue;
17170 }
17171 i = find_rhs_use(state, entry->member, ins);
17172 if (i < 0) {
17173 continue;
17174 }
17175 rinfo = arch_reg_rhs(state, entry->member, i);
17176 if ((info.reg == REG_UNNEEDED) && (rinfo.reg == REG_UNNEEDED)) {
17177 continue;
17178 }
17179 replace_rhs_use(state, ins, out, entry->member);
17180 }
17181 transform_to_arch_instruction(state, out);
17182 return out;
17183}
17184
Eric Biedermand1ea5392003-06-28 06:49:45 +000017185static struct triple *typed_pre_copy(
17186 struct compile_state *state, struct type *type, struct triple *ins, int index)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017187{
17188 /* Carefully insert enough operations so that I can
17189 * enter any operation with a GPR32.
17190 */
17191 struct triple *in;
17192 struct triple **expr;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017193 unsigned classes;
17194 struct reg_info info;
Eric Biederman90089602004-05-28 14:11:54 +000017195 int op;
Eric Biederman153ea352003-06-20 14:43:20 +000017196 if (ins->op == OP_PHI) {
17197 internal_error(state, ins, "pre_copy on a phi?");
17198 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000017199 classes = arch_type_to_regcm(state, type);
17200 info = arch_reg_rhs(state, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017201 expr = &RHS(ins, index);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017202 if ((info.regcm & classes) == 0) {
Eric Biederman90089602004-05-28 14:11:54 +000017203 FILE *fp = state->errout;
17204 fprintf(fp, "src_type: ");
17205 name_of(fp, ins->type);
17206 fprintf(fp, "\ndst_type: ");
17207 name_of(fp, type);
17208 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000017209 internal_error(state, ins, "pre_copy with no register classes");
17210 }
Eric Biederman90089602004-05-28 14:11:54 +000017211 op = OP_COPY;
17212 if (!equiv_types(type, (*expr)->type)) {
17213 op = OP_CONVERT;
17214 }
17215 in = pre_triple(state, ins, op, type, *expr, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017216 unuse_triple(*expr, ins);
17217 *expr = in;
17218 use_triple(RHS(in, 0), in);
17219 use_triple(in, ins);
17220 transform_to_arch_instruction(state, in);
17221 return in;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017222
17223}
17224static struct triple *pre_copy(
17225 struct compile_state *state, struct triple *ins, int index)
17226{
17227 return typed_pre_copy(state, RHS(ins, index)->type, ins, index);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017228}
17229
17230
Eric Biedermanb138ac82003-04-22 18:44:01 +000017231static void insert_copies_to_phi(struct compile_state *state)
17232{
17233 /* To get out of ssa form we insert moves on the incoming
17234 * edges to blocks containting phi functions.
17235 */
17236 struct triple *first;
17237 struct triple *phi;
17238
17239 /* Walk all of the operations to find the phi functions */
Eric Biederman83b991a2003-10-11 06:20:25 +000017240 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017241 for(phi = first->next; phi != first ; phi = phi->next) {
17242 struct block_set *set;
17243 struct block *block;
Eric Biedermand1ea5392003-06-28 06:49:45 +000017244 struct triple **slot, *copy;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017245 int edge;
17246 if (phi->op != OP_PHI) {
17247 continue;
17248 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017249 phi->id |= TRIPLE_FLAG_POST_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017250 block = phi->u.block;
Eric Biederman0babc1c2003-05-09 02:39:00 +000017251 slot = &RHS(phi, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000017252 /* Phi's that feed into mandatory live range joins
17253 * cause nasty complications. Insert a copy of
17254 * the phi value so I never have to deal with
17255 * that in the rest of the code.
17256 */
17257 copy = post_copy(state, phi);
17258 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017259 /* Walk all of the incoming edges/blocks and insert moves.
17260 */
17261 for(edge = 0, set = block->use; set; set = set->next, edge++) {
17262 struct block *eblock;
17263 struct triple *move;
17264 struct triple *val;
17265 struct triple *ptr;
17266 eblock = set->member;
17267 val = slot[edge];
17268
17269 if (val == phi) {
17270 continue;
17271 }
17272
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017273 get_occurance(val->occurance);
Eric Biederman90089602004-05-28 14:11:54 +000017274 move = build_triple(state, OP_COPY, val->type, val, 0,
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000017275 val->occurance);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017276 move->u.block = eblock;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017277 move->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017278 use_triple(val, move);
17279
17280 slot[edge] = move;
17281 unuse_triple(val, phi);
17282 use_triple(move, phi);
17283
Eric Biederman66fe2222003-07-04 15:14:04 +000017284 /* Walk up the dominator tree until I have found the appropriate block */
17285 while(eblock && !tdominates(state, val, eblock->last)) {
17286 eblock = eblock->idom;
17287 }
17288 if (!eblock) {
17289 internal_error(state, phi, "Cannot find block dominated by %p",
17290 val);
17291 }
17292
Eric Biedermanb138ac82003-04-22 18:44:01 +000017293 /* Walk through the block backwards to find
17294 * an appropriate location for the OP_COPY.
17295 */
17296 for(ptr = eblock->last; ptr != eblock->first; ptr = ptr->prev) {
17297 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000017298 if (ptr->op == OP_PIECE) {
17299 ptr = MISC(ptr, 0);
17300 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017301 if ((ptr == phi) || (ptr == val)) {
17302 goto out;
17303 }
Eric Biederman90089602004-05-28 14:11:54 +000017304 expr = triple_lhs(state, ptr, 0);
17305 for(;expr; expr = triple_lhs(state, ptr, expr)) {
17306 if ((*expr) == val) {
17307 goto out;
17308 }
17309 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017310 expr = triple_rhs(state, ptr, 0);
17311 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17312 if ((*expr) == phi) {
17313 goto out;
17314 }
17315 }
17316 }
17317 out:
Eric Biederman0babc1c2003-05-09 02:39:00 +000017318 if (triple_is_branch(state, ptr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017319 internal_error(state, ptr,
17320 "Could not insert write to phi");
17321 }
Eric Biederman90089602004-05-28 14:11:54 +000017322 insert_triple(state, after_lhs(state, ptr), move);
17323 if (eblock->last == after_lhs(state, ptr)->prev) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017324 eblock->last = move;
17325 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017326 transform_to_arch_instruction(state, move);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017327 }
17328 }
Eric Biederman90089602004-05-28 14:11:54 +000017329 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017330}
17331
Eric Biederman90089602004-05-28 14:11:54 +000017332struct triple_reg_set;
17333struct reg_block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017334
Eric Biedermanb138ac82003-04-22 18:44:01 +000017335
17336static int do_triple_set(struct triple_reg_set **head,
17337 struct triple *member, struct triple *new_member)
17338{
17339 struct triple_reg_set **ptr, *new;
17340 if (!member)
17341 return 0;
17342 ptr = head;
17343 while(*ptr) {
17344 if ((*ptr)->member == member) {
17345 return 0;
17346 }
17347 ptr = &(*ptr)->next;
17348 }
17349 new = xcmalloc(sizeof(*new), "triple_set");
17350 new->member = member;
17351 new->new = new_member;
17352 new->next = *head;
17353 *head = new;
17354 return 1;
17355}
17356
17357static void do_triple_unset(struct triple_reg_set **head, struct triple *member)
17358{
17359 struct triple_reg_set *entry, **ptr;
17360 ptr = head;
17361 while(*ptr) {
17362 entry = *ptr;
17363 if (entry->member == member) {
17364 *ptr = entry->next;
17365 xfree(entry);
17366 return;
17367 }
17368 else {
17369 ptr = &entry->next;
17370 }
17371 }
17372}
17373
17374static int in_triple(struct reg_block *rb, struct triple *in)
17375{
17376 return do_triple_set(&rb->in, in, 0);
17377}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017378
17379#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017380static void unin_triple(struct reg_block *rb, struct triple *unin)
17381{
17382 do_triple_unset(&rb->in, unin);
17383}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017384#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017385
17386static int out_triple(struct reg_block *rb, struct triple *out)
17387{
17388 return do_triple_set(&rb->out, out, 0);
17389}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017390#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000017391static void unout_triple(struct reg_block *rb, struct triple *unout)
17392{
17393 do_triple_unset(&rb->out, unout);
17394}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017395#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017396
17397static int initialize_regblock(struct reg_block *blocks,
17398 struct block *block, int vertex)
17399{
17400 struct block_set *user;
17401 if (!block || (blocks[block->vertex].block == block)) {
17402 return vertex;
17403 }
17404 vertex += 1;
17405 /* Renumber the blocks in a convinient fashion */
17406 block->vertex = vertex;
17407 blocks[vertex].block = block;
17408 blocks[vertex].vertex = vertex;
17409 for(user = block->use; user; user = user->next) {
17410 vertex = initialize_regblock(blocks, user->member, vertex);
17411 }
17412 return vertex;
17413}
17414
Eric Biederman90089602004-05-28 14:11:54 +000017415static struct triple *part_to_piece(struct compile_state *state, struct triple *ins)
17416{
17417/* Part to piece is a best attempt and it cannot be correct all by
17418 * itself. If various values are read as different sizes in different
17419 * parts of the code this function cannot work. Or rather it cannot
17420 * work in conjunction with compute_variable_liftimes. As the
17421 * analysis will get confused.
17422 */
17423 struct triple *base;
17424 unsigned reg;
17425 if (!is_lvalue(state, ins)) {
17426 return ins;
17427 }
17428 base = 0;
17429 reg = 0;
17430 while(ins && triple_is_part(state, ins) && (ins->op != OP_PIECE)) {
17431 base = MISC(ins, 0);
17432 switch(ins->op) {
17433 case OP_INDEX:
17434 reg += index_reg_offset(state, base->type, ins->u.cval)/REG_SIZEOF_REG;
17435 break;
17436 case OP_DOT:
17437 reg += field_reg_offset(state, base->type, ins->u.field)/REG_SIZEOF_REG;
17438 break;
17439 default:
17440 internal_error(state, ins, "unhandled part");
17441 break;
17442 }
17443 ins = base;
17444 }
17445 if (base) {
17446 if (reg > base->lhs) {
17447 internal_error(state, base, "part out of range?");
17448 }
17449 ins = LHS(base, reg);
17450 }
17451 return ins;
17452}
17453
17454static int this_def(struct compile_state *state,
17455 struct triple *ins, struct triple *other)
17456{
17457 if (ins == other) {
17458 return 1;
17459 }
17460 if (ins->op == OP_WRITE) {
17461 ins = part_to_piece(state, MISC(ins, 0));
17462 }
17463 return ins == other;
17464}
17465
Eric Biedermanb138ac82003-04-22 18:44:01 +000017466static int phi_in(struct compile_state *state, struct reg_block *blocks,
17467 struct reg_block *rb, struct block *suc)
17468{
17469 /* Read the conditional input set of a successor block
17470 * (i.e. the input to the phi nodes) and place it in the
17471 * current blocks output set.
17472 */
17473 struct block_set *set;
17474 struct triple *ptr;
17475 int edge;
17476 int done, change;
17477 change = 0;
17478 /* Find the edge I am coming in on */
17479 for(edge = 0, set = suc->use; set; set = set->next, edge++) {
17480 if (set->member == rb->block) {
17481 break;
17482 }
17483 }
17484 if (!set) {
17485 internal_error(state, 0, "Not coming on a control edge?");
17486 }
17487 for(done = 0, ptr = suc->first; !done; ptr = ptr->next) {
17488 struct triple **slot, *expr, *ptr2;
17489 int out_change, done2;
17490 done = (ptr == suc->last);
17491 if (ptr->op != OP_PHI) {
17492 continue;
17493 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000017494 slot = &RHS(ptr, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017495 expr = slot[edge];
17496 out_change = out_triple(rb, expr);
17497 if (!out_change) {
17498 continue;
17499 }
17500 /* If we don't define the variable also plast it
17501 * in the current blocks input set.
17502 */
17503 ptr2 = rb->block->first;
17504 for(done2 = 0; !done2; ptr2 = ptr2->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017505 if (this_def(state, ptr2, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017506 break;
17507 }
17508 done2 = (ptr2 == rb->block->last);
17509 }
17510 if (!done2) {
17511 continue;
17512 }
17513 change |= in_triple(rb, expr);
17514 }
17515 return change;
17516}
17517
17518static int reg_in(struct compile_state *state, struct reg_block *blocks,
17519 struct reg_block *rb, struct block *suc)
17520{
17521 struct triple_reg_set *in_set;
17522 int change;
17523 change = 0;
17524 /* Read the input set of a successor block
17525 * and place it in the current blocks output set.
17526 */
17527 in_set = blocks[suc->vertex].in;
17528 for(; in_set; in_set = in_set->next) {
17529 int out_change, done;
17530 struct triple *first, *last, *ptr;
17531 out_change = out_triple(rb, in_set->member);
17532 if (!out_change) {
17533 continue;
17534 }
17535 /* If we don't define the variable also place it
17536 * in the current blocks input set.
17537 */
17538 first = rb->block->first;
17539 last = rb->block->last;
17540 done = 0;
17541 for(ptr = first; !done; ptr = ptr->next) {
Eric Biederman90089602004-05-28 14:11:54 +000017542 if (this_def(state, ptr, in_set->member)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017543 break;
17544 }
17545 done = (ptr == last);
17546 }
17547 if (!done) {
17548 continue;
17549 }
17550 change |= in_triple(rb, in_set->member);
17551 }
17552 change |= phi_in(state, blocks, rb, suc);
17553 return change;
17554}
17555
Eric Biedermanb138ac82003-04-22 18:44:01 +000017556static int use_in(struct compile_state *state, struct reg_block *rb)
17557{
17558 /* Find the variables we use but don't define and add
17559 * it to the current blocks input set.
17560 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000017561#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000017562#warning "FIXME is this O(N^2) algorithm bad?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000017563#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000017564 struct block *block;
17565 struct triple *ptr;
17566 int done;
17567 int change;
17568 block = rb->block;
17569 change = 0;
17570 for(done = 0, ptr = block->last; !done; ptr = ptr->prev) {
17571 struct triple **expr;
17572 done = (ptr == block->first);
17573 /* The variable a phi function uses depends on the
17574 * control flow, and is handled in phi_in, not
17575 * here.
17576 */
17577 if (ptr->op == OP_PHI) {
17578 continue;
17579 }
17580 expr = triple_rhs(state, ptr, 0);
17581 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17582 struct triple *rhs, *test;
17583 int tdone;
Eric Biederman90089602004-05-28 14:11:54 +000017584 rhs = part_to_piece(state, *expr);
Eric Biederman0babc1c2003-05-09 02:39:00 +000017585 if (!rhs) {
17586 continue;
17587 }
Eric Biederman90089602004-05-28 14:11:54 +000017588
17589 /* See if rhs is defined in this block.
17590 * A write counts as a definition.
17591 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000017592 for(tdone = 0, test = ptr; !tdone; test = test->prev) {
17593 tdone = (test == block->first);
Eric Biederman90089602004-05-28 14:11:54 +000017594 if (this_def(state, test, rhs)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017595 rhs = 0;
17596 break;
17597 }
17598 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017599 /* If I still have a valid rhs add it to in */
17600 change |= in_triple(rb, rhs);
17601 }
17602 }
17603 return change;
17604}
17605
17606static struct reg_block *compute_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000017607 struct compile_state *state, struct basic_blocks *bb)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017608{
17609 struct reg_block *blocks;
17610 int change;
17611 blocks = xcmalloc(
Eric Biederman90089602004-05-28 14:11:54 +000017612 sizeof(*blocks)*(bb->last_vertex + 1), "reg_block");
17613 initialize_regblock(blocks, bb->last_block, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017614 do {
17615 int i;
17616 change = 0;
Eric Biederman90089602004-05-28 14:11:54 +000017617 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017618 struct block_set *edge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017619 struct reg_block *rb;
17620 rb = &blocks[i];
Eric Biederman5ade04a2003-10-22 04:03:46 +000017621 /* Add the all successor's input set to in */
17622 for(edge = rb->block->edges; edge; edge = edge->next) {
17623 change |= reg_in(state, blocks, rb, edge->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017624 }
17625 /* Add use to in... */
17626 change |= use_in(state, rb);
17627 }
17628 } while(change);
17629 return blocks;
17630}
17631
Eric Biederman90089602004-05-28 14:11:54 +000017632static void free_variable_lifetimes(struct compile_state *state,
17633 struct basic_blocks *bb, struct reg_block *blocks)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017634{
17635 int i;
17636 /* free in_set && out_set on each block */
Eric Biederman90089602004-05-28 14:11:54 +000017637 for(i = 1; i <= bb->last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017638 struct triple_reg_set *entry, *next;
17639 struct reg_block *rb;
17640 rb = &blocks[i];
17641 for(entry = rb->in; entry ; entry = next) {
17642 next = entry->next;
17643 do_triple_unset(&rb->in, entry->member);
17644 }
17645 for(entry = rb->out; entry; entry = next) {
17646 next = entry->next;
17647 do_triple_unset(&rb->out, entry->member);
17648 }
17649 }
17650 xfree(blocks);
17651
17652}
17653
Eric Biedermanf96a8102003-06-16 16:57:34 +000017654typedef void (*wvl_cb_t)(
Eric Biedermanb138ac82003-04-22 18:44:01 +000017655 struct compile_state *state,
17656 struct reg_block *blocks, struct triple_reg_set *live,
17657 struct reg_block *rb, struct triple *ins, void *arg);
17658
17659static void walk_variable_lifetimes(struct compile_state *state,
Eric Biederman90089602004-05-28 14:11:54 +000017660 struct basic_blocks *bb, struct reg_block *blocks,
17661 wvl_cb_t cb, void *arg)
Eric Biedermanb138ac82003-04-22 18:44:01 +000017662{
17663 int i;
17664
Eric Biederman90089602004-05-28 14:11:54 +000017665 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017666 struct triple_reg_set *live;
17667 struct triple_reg_set *entry, *next;
17668 struct triple *ptr, *prev;
17669 struct reg_block *rb;
17670 struct block *block;
17671 int done;
17672
17673 /* Get the blocks */
17674 rb = &blocks[i];
17675 block = rb->block;
17676
17677 /* Copy out into live */
17678 live = 0;
17679 for(entry = rb->out; entry; entry = next) {
17680 next = entry->next;
17681 do_triple_set(&live, entry->member, entry->new);
17682 }
17683 /* Walk through the basic block calculating live */
17684 for(done = 0, ptr = block->last; !done; ptr = prev) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000017685 struct triple **expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017686
17687 prev = ptr->prev;
17688 done = (ptr == block->first);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017689
17690 /* Ensure the current definition is in live */
17691 if (triple_is_def(state, ptr)) {
17692 do_triple_set(&live, ptr, 0);
17693 }
17694
17695 /* Inform the callback function of what is
17696 * going on.
17697 */
Eric Biedermanf96a8102003-06-16 16:57:34 +000017698 cb(state, blocks, live, rb, ptr, arg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017699
17700 /* Remove the current definition from live */
17701 do_triple_unset(&live, ptr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017702
Eric Biedermanb138ac82003-04-22 18:44:01 +000017703 /* Add the current uses to live.
17704 *
17705 * It is safe to skip phi functions because they do
17706 * not have any block local uses, and the block
17707 * output sets already properly account for what
17708 * control flow depedent uses phi functions do have.
17709 */
17710 if (ptr->op == OP_PHI) {
17711 continue;
17712 }
17713 expr = triple_rhs(state, ptr, 0);
17714 for(;expr; expr = triple_rhs(state, ptr, expr)) {
17715 /* If the triple is not a definition skip it. */
Eric Biederman0babc1c2003-05-09 02:39:00 +000017716 if (!*expr || !triple_is_def(state, *expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017717 continue;
17718 }
17719 do_triple_set(&live, *expr, 0);
17720 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000017721 }
17722 /* Free live */
17723 for(entry = live; entry; entry = next) {
17724 next = entry->next;
17725 do_triple_unset(&live, entry->member);
17726 }
17727 }
17728}
17729
Eric Biederman90089602004-05-28 14:11:54 +000017730struct print_live_variable_info {
17731 struct reg_block *rb;
17732 FILE *fp;
17733};
Stefan Reinauer50542a82007-10-24 11:14:14 +000017734#if DEBUG_EXPLICIT_CLOSURES
Eric Biederman90089602004-05-28 14:11:54 +000017735static void print_live_variables_block(
17736 struct compile_state *state, struct block *block, void *arg)
17737
17738{
17739 struct print_live_variable_info *info = arg;
17740 struct block_set *edge;
17741 FILE *fp = info->fp;
17742 struct reg_block *rb;
17743 struct triple *ptr;
17744 int phi_present;
17745 int done;
17746 rb = &info->rb[block->vertex];
17747
17748 fprintf(fp, "\nblock: %p (%d),",
17749 block, block->vertex);
17750 for(edge = block->edges; edge; edge = edge->next) {
17751 fprintf(fp, " %p<-%p",
17752 edge->member,
17753 edge->member && edge->member->use?edge->member->use->member : 0);
17754 }
17755 fprintf(fp, "\n");
17756 if (rb->in) {
17757 struct triple_reg_set *in_set;
17758 fprintf(fp, " in:");
17759 for(in_set = rb->in; in_set; in_set = in_set->next) {
17760 fprintf(fp, " %-10p", in_set->member);
17761 }
17762 fprintf(fp, "\n");
17763 }
17764 phi_present = 0;
17765 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17766 done = (ptr == block->last);
17767 if (ptr->op == OP_PHI) {
17768 phi_present = 1;
17769 break;
17770 }
17771 }
17772 if (phi_present) {
17773 int edge;
17774 for(edge = 0; edge < block->users; edge++) {
17775 fprintf(fp, " in(%d):", edge);
17776 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17777 struct triple **slot;
17778 done = (ptr == block->last);
17779 if (ptr->op != OP_PHI) {
17780 continue;
17781 }
17782 slot = &RHS(ptr, 0);
17783 fprintf(fp, " %-10p", slot[edge]);
17784 }
17785 fprintf(fp, "\n");
17786 }
17787 }
17788 if (block->first->op == OP_LABEL) {
17789 fprintf(fp, "%p:\n", block->first);
17790 }
17791 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
17792 done = (ptr == block->last);
17793 display_triple(fp, ptr);
17794 }
17795 if (rb->out) {
17796 struct triple_reg_set *out_set;
17797 fprintf(fp, " out:");
17798 for(out_set = rb->out; out_set; out_set = out_set->next) {
17799 fprintf(fp, " %-10p", out_set->member);
17800 }
17801 fprintf(fp, "\n");
17802 }
17803 fprintf(fp, "\n");
17804}
17805
17806static void print_live_variables(struct compile_state *state,
17807 struct basic_blocks *bb, struct reg_block *rb, FILE *fp)
17808{
17809 struct print_live_variable_info info;
17810 info.rb = rb;
17811 info.fp = fp;
17812 fprintf(fp, "\nlive variables by block\n");
17813 walk_blocks(state, bb, print_live_variables_block, &info);
17814
17815}
Stefan Reinauer50542a82007-10-24 11:14:14 +000017816#endif
Eric Biederman90089602004-05-28 14:11:54 +000017817
Eric Biedermanb138ac82003-04-22 18:44:01 +000017818static int count_triples(struct compile_state *state)
17819{
17820 struct triple *first, *ins;
17821 int triples = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017822 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017823 ins = first;
17824 do {
17825 triples++;
17826 ins = ins->next;
17827 } while (ins != first);
17828 return triples;
17829}
Eric Biederman66fe2222003-07-04 15:14:04 +000017830
17831
Eric Biedermanb138ac82003-04-22 18:44:01 +000017832struct dead_triple {
17833 struct triple *triple;
17834 struct dead_triple *work_next;
17835 struct block *block;
Eric Biederman83b991a2003-10-11 06:20:25 +000017836 int old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017837 int flags;
17838#define TRIPLE_FLAG_ALIVE 1
Eric Biederman90089602004-05-28 14:11:54 +000017839#define TRIPLE_FLAG_FREE 1
Eric Biedermanb138ac82003-04-22 18:44:01 +000017840};
17841
Eric Biederman90089602004-05-28 14:11:54 +000017842static void print_dead_triples(struct compile_state *state,
17843 struct dead_triple *dtriple)
17844{
17845 struct triple *first, *ins;
17846 struct dead_triple *dt;
17847 FILE *fp;
17848 if (!(state->compiler->debug & DEBUG_TRIPLES)) {
17849 return;
17850 }
17851 fp = state->dbgout;
17852 fprintf(fp, "--------------- dtriples ---------------\n");
17853 first = state->first;
17854 ins = first;
17855 do {
17856 dt = &dtriple[ins->id];
17857 if ((ins->op == OP_LABEL) && (ins->use)) {
17858 fprintf(fp, "\n%p:\n", ins);
17859 }
17860 fprintf(fp, "%c",
17861 (dt->flags & TRIPLE_FLAG_ALIVE)?' ': '-');
17862 display_triple(fp, ins);
17863 if (triple_is_branch(state, ins)) {
17864 fprintf(fp, "\n");
17865 }
17866 ins = ins->next;
17867 } while(ins != first);
17868 fprintf(fp, "\n");
17869}
17870
Eric Biedermanb138ac82003-04-22 18:44:01 +000017871
17872static void awaken(
17873 struct compile_state *state,
17874 struct dead_triple *dtriple, struct triple **expr,
17875 struct dead_triple ***work_list_tail)
17876{
17877 struct triple *triple;
17878 struct dead_triple *dt;
17879 if (!expr) {
17880 return;
17881 }
17882 triple = *expr;
17883 if (!triple) {
17884 return;
17885 }
17886 if (triple->id <= 0) {
17887 internal_error(state, triple, "bad triple id: %d",
17888 triple->id);
17889 }
17890 if (triple->op == OP_NOOP) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017891 internal_error(state, triple, "awakening noop?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000017892 return;
17893 }
17894 dt = &dtriple[triple->id];
17895 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
17896 dt->flags |= TRIPLE_FLAG_ALIVE;
17897 if (!dt->work_next) {
17898 **work_list_tail = dt;
17899 *work_list_tail = &dt->work_next;
17900 }
17901 }
17902}
17903
17904static void eliminate_inefectual_code(struct compile_state *state)
17905{
17906 struct block *block;
17907 struct dead_triple *dtriple, *work_list, **work_list_tail, *dt;
17908 int triples, i;
Eric Biederman83b991a2003-10-11 06:20:25 +000017909 struct triple *first, *final, *ins;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017910
Eric Biederman5ade04a2003-10-22 04:03:46 +000017911 if (!(state->compiler->flags & COMPILER_ELIMINATE_INEFECTUAL_CODE)) {
17912 return;
17913 }
17914
Eric Biedermanb138ac82003-04-22 18:44:01 +000017915 /* Setup the work list */
17916 work_list = 0;
17917 work_list_tail = &work_list;
17918
Eric Biederman83b991a2003-10-11 06:20:25 +000017919 first = state->first;
17920 final = state->first->prev;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017921
17922 /* Count how many triples I have */
17923 triples = count_triples(state);
17924
17925 /* Now put then in an array and mark all of the triples dead */
17926 dtriple = xcmalloc(sizeof(*dtriple) * (triples + 1), "dtriples");
17927
17928 ins = first;
17929 i = 1;
17930 block = 0;
17931 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017932 dtriple[i].triple = ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000017933 dtriple[i].block = block_of_triple(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017934 dtriple[i].flags = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000017935 dtriple[i].old_id = ins->id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017936 ins->id = i;
17937 /* See if it is an operation we always keep */
Eric Biederman83b991a2003-10-11 06:20:25 +000017938 if (!triple_is_pure(state, ins, dtriple[i].old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000017939 awaken(state, dtriple, &ins, &work_list_tail);
17940 }
17941 i++;
17942 ins = ins->next;
17943 } while(ins != first);
17944 while(work_list) {
Eric Biederman83b991a2003-10-11 06:20:25 +000017945 struct block *block;
Eric Biedermanb138ac82003-04-22 18:44:01 +000017946 struct dead_triple *dt;
17947 struct block_set *user;
17948 struct triple **expr;
17949 dt = work_list;
17950 work_list = dt->work_next;
17951 if (!work_list) {
17952 work_list_tail = &work_list;
17953 }
Eric Biederman83b991a2003-10-11 06:20:25 +000017954 /* Make certain the block the current instruction is in lives */
17955 block = block_of_triple(state, dt->triple);
17956 awaken(state, dtriple, &block->first, &work_list_tail);
17957 if (triple_is_branch(state, block->last)) {
17958 awaken(state, dtriple, &block->last, &work_list_tail);
Eric Biederman90089602004-05-28 14:11:54 +000017959 } else {
17960 awaken(state, dtriple, &block->last->next, &work_list_tail);
Eric Biederman83b991a2003-10-11 06:20:25 +000017961 }
17962
Eric Biedermanb138ac82003-04-22 18:44:01 +000017963 /* Wake up the data depencencies of this triple */
17964 expr = 0;
17965 do {
17966 expr = triple_rhs(state, dt->triple, expr);
17967 awaken(state, dtriple, expr, &work_list_tail);
17968 } while(expr);
17969 do {
17970 expr = triple_lhs(state, dt->triple, expr);
17971 awaken(state, dtriple, expr, &work_list_tail);
17972 } while(expr);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000017973 do {
17974 expr = triple_misc(state, dt->triple, expr);
17975 awaken(state, dtriple, expr, &work_list_tail);
17976 } while(expr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017977 /* Wake up the forward control dependencies */
17978 do {
17979 expr = triple_targ(state, dt->triple, expr);
17980 awaken(state, dtriple, expr, &work_list_tail);
17981 } while(expr);
17982 /* Wake up the reverse control dependencies of this triple */
17983 for(user = dt->block->ipdomfrontier; user; user = user->next) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000017984 struct triple *last;
17985 last = user->member->last;
17986 while((last->op == OP_NOOP) && (last != user->member->first)) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000017987#if DEBUG_ROMCC_WARNINGS
17988#warning "Should we bring the awakening noops back?"
17989#endif
17990 // internal_warning(state, last, "awakening noop?");
Eric Biederman5ade04a2003-10-22 04:03:46 +000017991 last = last->prev;
Eric Biederman83b991a2003-10-11 06:20:25 +000017992 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000017993 awaken(state, dtriple, &last, &work_list_tail);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017994 }
17995 }
Eric Biederman90089602004-05-28 14:11:54 +000017996 print_dead_triples(state, dtriple);
Eric Biedermanb138ac82003-04-22 18:44:01 +000017997 for(dt = &dtriple[1]; dt <= &dtriple[triples]; dt++) {
17998 if ((dt->triple->op == OP_NOOP) &&
17999 (dt->flags & TRIPLE_FLAG_ALIVE)) {
18000 internal_error(state, dt->triple, "noop effective?");
18001 }
Eric Biederman83b991a2003-10-11 06:20:25 +000018002 dt->triple->id = dt->old_id; /* Restore the color */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018003 if (!(dt->flags & TRIPLE_FLAG_ALIVE)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018004 release_triple(state, dt->triple);
18005 }
18006 }
18007 xfree(dtriple);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018008
18009 rebuild_ssa_form(state);
18010
Eric Biederman90089602004-05-28 14:11:54 +000018011 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018012}
18013
18014
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018015static void insert_mandatory_copies(struct compile_state *state)
18016{
18017 struct triple *ins, *first;
18018
18019 /* The object is with a minimum of inserted copies,
18020 * to resolve in fundamental register conflicts between
18021 * register value producers and consumers.
18022 * Theoretically we may be greater than minimal when we
18023 * are inserting copies before instructions but that
18024 * case should be rare.
18025 */
Eric Biederman83b991a2003-10-11 06:20:25 +000018026 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018027 ins = first;
18028 do {
18029 struct triple_set *entry, *next;
18030 struct triple *tmp;
18031 struct reg_info info;
18032 unsigned reg, regcm;
18033 int do_post_copy, do_pre_copy;
18034 tmp = 0;
18035 if (!triple_is_def(state, ins)) {
18036 goto next;
18037 }
18038 /* Find the architecture specific color information */
Eric Biederman90089602004-05-28 14:11:54 +000018039 info = find_lhs_pre_color(state, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018040 if (info.reg >= MAX_REGISTERS) {
18041 info.reg = REG_UNSET;
18042 }
Eric Biederman90089602004-05-28 14:11:54 +000018043
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018044 reg = REG_UNSET;
18045 regcm = arch_type_to_regcm(state, ins->type);
18046 do_post_copy = do_pre_copy = 0;
18047
18048 /* Walk through the uses of ins and check for conflicts */
18049 for(entry = ins->use; entry; entry = next) {
18050 struct reg_info rinfo;
18051 int i;
18052 next = entry->next;
18053 i = find_rhs_use(state, entry->member, ins);
18054 if (i < 0) {
18055 continue;
18056 }
18057
18058 /* Find the users color requirements */
18059 rinfo = arch_reg_rhs(state, entry->member, i);
18060 if (rinfo.reg >= MAX_REGISTERS) {
18061 rinfo.reg = REG_UNSET;
18062 }
18063
18064 /* See if I need a pre_copy */
18065 if (rinfo.reg != REG_UNSET) {
18066 if ((reg != REG_UNSET) && (reg != rinfo.reg)) {
18067 do_pre_copy = 1;
18068 }
18069 reg = rinfo.reg;
18070 }
18071 regcm &= rinfo.regcm;
18072 regcm = arch_regcm_normalize(state, regcm);
18073 if (regcm == 0) {
18074 do_pre_copy = 1;
18075 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018076 /* Always use pre_copies for constants.
18077 * They do not take up any registers until a
18078 * copy places them in one.
18079 */
18080 if ((info.reg == REG_UNNEEDED) &&
18081 (rinfo.reg != REG_UNNEEDED)) {
18082 do_pre_copy = 1;
18083 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018084 }
18085 do_post_copy =
18086 !do_pre_copy &&
18087 (((info.reg != REG_UNSET) &&
18088 (reg != REG_UNSET) &&
18089 (info.reg != reg)) ||
18090 ((info.regcm & regcm) == 0));
18091
18092 reg = info.reg;
18093 regcm = info.regcm;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018094 /* 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 +000018095 for(entry = ins->use; entry; entry = next) {
18096 struct reg_info rinfo;
18097 int i;
18098 next = entry->next;
18099 i = find_rhs_use(state, entry->member, ins);
18100 if (i < 0) {
18101 continue;
18102 }
18103
18104 /* Find the users color requirements */
18105 rinfo = arch_reg_rhs(state, entry->member, i);
18106 if (rinfo.reg >= MAX_REGISTERS) {
18107 rinfo.reg = REG_UNSET;
18108 }
18109
18110 /* Now see if it is time to do the pre_copy */
18111 if (rinfo.reg != REG_UNSET) {
18112 if (((reg != REG_UNSET) && (reg != rinfo.reg)) ||
18113 ((regcm & rinfo.regcm) == 0) ||
18114 /* Don't let a mandatory coalesce sneak
18115 * into a operation that is marked to prevent
18116 * coalescing.
18117 */
18118 ((reg != REG_UNNEEDED) &&
18119 ((ins->id & TRIPLE_FLAG_POST_SPLIT) ||
18120 (entry->member->id & TRIPLE_FLAG_PRE_SPLIT)))
18121 ) {
18122 if (do_pre_copy) {
18123 struct triple *user;
18124 user = entry->member;
18125 if (RHS(user, i) != ins) {
18126 internal_error(state, user, "bad rhs");
18127 }
18128 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018129 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018130 continue;
18131 } else {
18132 do_post_copy = 1;
18133 }
18134 }
18135 reg = rinfo.reg;
18136 }
18137 if ((regcm & rinfo.regcm) == 0) {
18138 if (do_pre_copy) {
18139 struct triple *user;
18140 user = entry->member;
18141 if (RHS(user, i) != ins) {
18142 internal_error(state, user, "bad rhs");
18143 }
18144 tmp = pre_copy(state, user, i);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018145 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018146 continue;
18147 } else {
18148 do_post_copy = 1;
18149 }
18150 }
18151 regcm &= rinfo.regcm;
18152
18153 }
18154 if (do_post_copy) {
18155 struct reg_info pre, post;
18156 tmp = post_copy(state, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018157 tmp->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018158 pre = arch_reg_lhs(state, ins, 0);
18159 post = arch_reg_lhs(state, tmp, 0);
18160 if ((pre.reg == post.reg) && (pre.regcm == post.regcm)) {
18161 internal_error(state, tmp, "useless copy");
18162 }
18163 }
18164 next:
18165 ins = ins->next;
18166 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018167
Eric Biederman90089602004-05-28 14:11:54 +000018168 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018169}
18170
18171
Eric Biedermanb138ac82003-04-22 18:44:01 +000018172struct live_range_edge;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018173struct live_range_def;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018174struct live_range {
18175 struct live_range_edge *edges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018176 struct live_range_def *defs;
18177/* Note. The list pointed to by defs is kept in order.
18178 * That is baring splits in the flow control
18179 * defs dominates defs->next wich dominates defs->next->next
18180 * etc.
18181 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000018182 unsigned color;
18183 unsigned classes;
18184 unsigned degree;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018185 unsigned length;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018186 struct live_range *group_next, **group_prev;
18187};
18188
18189struct live_range_edge {
18190 struct live_range_edge *next;
18191 struct live_range *node;
18192};
18193
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018194struct live_range_def {
18195 struct live_range_def *next;
18196 struct live_range_def *prev;
18197 struct live_range *lr;
18198 struct triple *def;
18199 unsigned orig_id;
18200};
18201
Eric Biedermanb138ac82003-04-22 18:44:01 +000018202#define LRE_HASH_SIZE 2048
18203struct lre_hash {
18204 struct lre_hash *next;
18205 struct live_range *left;
18206 struct live_range *right;
18207};
18208
18209
18210struct reg_state {
18211 struct lre_hash *hash[LRE_HASH_SIZE];
18212 struct reg_block *blocks;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018213 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018214 struct live_range *lr;
18215 struct live_range *low, **low_tail;
18216 struct live_range *high, **high_tail;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018217 unsigned defs;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018218 unsigned ranges;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018219 int passes, max_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018220};
18221
18222
Eric Biedermand1ea5392003-06-28 06:49:45 +000018223struct print_interference_block_info {
18224 struct reg_state *rstate;
18225 FILE *fp;
18226 int need_edges;
18227};
18228static void print_interference_block(
18229 struct compile_state *state, struct block *block, void *arg)
18230
18231{
18232 struct print_interference_block_info *info = arg;
18233 struct reg_state *rstate = info->rstate;
Eric Biederman5ade04a2003-10-22 04:03:46 +000018234 struct block_set *edge;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018235 FILE *fp = info->fp;
18236 struct reg_block *rb;
18237 struct triple *ptr;
18238 int phi_present;
18239 int done;
18240 rb = &rstate->blocks[block->vertex];
18241
Eric Biederman5ade04a2003-10-22 04:03:46 +000018242 fprintf(fp, "\nblock: %p (%d),",
18243 block, block->vertex);
18244 for(edge = block->edges; edge; edge = edge->next) {
18245 fprintf(fp, " %p<-%p",
18246 edge->member,
18247 edge->member && edge->member->use?edge->member->use->member : 0);
18248 }
18249 fprintf(fp, "\n");
Eric Biedermand1ea5392003-06-28 06:49:45 +000018250 if (rb->in) {
18251 struct triple_reg_set *in_set;
18252 fprintf(fp, " in:");
18253 for(in_set = rb->in; in_set; in_set = in_set->next) {
18254 fprintf(fp, " %-10p", in_set->member);
18255 }
18256 fprintf(fp, "\n");
18257 }
18258 phi_present = 0;
18259 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18260 done = (ptr == block->last);
18261 if (ptr->op == OP_PHI) {
18262 phi_present = 1;
18263 break;
18264 }
18265 }
18266 if (phi_present) {
18267 int edge;
18268 for(edge = 0; edge < block->users; edge++) {
18269 fprintf(fp, " in(%d):", edge);
18270 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
18271 struct triple **slot;
18272 done = (ptr == block->last);
18273 if (ptr->op != OP_PHI) {
18274 continue;
18275 }
18276 slot = &RHS(ptr, 0);
18277 fprintf(fp, " %-10p", slot[edge]);
18278 }
18279 fprintf(fp, "\n");
18280 }
18281 }
18282 if (block->first->op == OP_LABEL) {
18283 fprintf(fp, "%p:\n", block->first);
18284 }
18285 for(done = 0, ptr = block->first; !done; ptr = ptr->next) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000018286 struct live_range *lr;
18287 unsigned id;
18288 int op;
18289 op = ptr->op;
18290 done = (ptr == block->last);
18291 lr = rstate->lrd[ptr->id].lr;
18292
Eric Biedermand1ea5392003-06-28 06:49:45 +000018293 id = ptr->id;
18294 ptr->id = rstate->lrd[id].orig_id;
18295 SET_REG(ptr->id, lr->color);
18296 display_triple(fp, ptr);
18297 ptr->id = id;
18298
18299 if (triple_is_def(state, ptr) && (lr->defs == 0)) {
18300 internal_error(state, ptr, "lr has no defs!");
18301 }
18302 if (info->need_edges) {
18303 if (lr->defs) {
18304 struct live_range_def *lrd;
18305 fprintf(fp, " range:");
18306 lrd = lr->defs;
18307 do {
18308 fprintf(fp, " %-10p", lrd->def);
18309 lrd = lrd->next;
18310 } while(lrd != lr->defs);
18311 fprintf(fp, "\n");
18312 }
18313 if (lr->edges > 0) {
18314 struct live_range_edge *edge;
18315 fprintf(fp, " edges:");
18316 for(edge = lr->edges; edge; edge = edge->next) {
18317 struct live_range_def *lrd;
18318 lrd = edge->node->defs;
18319 do {
18320 fprintf(fp, " %-10p", lrd->def);
18321 lrd = lrd->next;
18322 } while(lrd != edge->node->defs);
18323 fprintf(fp, "|");
18324 }
18325 fprintf(fp, "\n");
18326 }
18327 }
18328 /* Do a bunch of sanity checks */
18329 valid_ins(state, ptr);
18330 if ((ptr->id < 0) || (ptr->id > rstate->defs)) {
18331 internal_error(state, ptr, "Invalid triple id: %d",
18332 ptr->id);
18333 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018334 }
18335 if (rb->out) {
18336 struct triple_reg_set *out_set;
18337 fprintf(fp, " out:");
18338 for(out_set = rb->out; out_set; out_set = out_set->next) {
18339 fprintf(fp, " %-10p", out_set->member);
18340 }
18341 fprintf(fp, "\n");
18342 }
18343 fprintf(fp, "\n");
18344}
18345
18346static void print_interference_blocks(
18347 struct compile_state *state, struct reg_state *rstate, FILE *fp, int need_edges)
18348{
18349 struct print_interference_block_info info;
18350 info.rstate = rstate;
18351 info.fp = fp;
18352 info.need_edges = need_edges;
18353 fprintf(fp, "\nlive variables by block\n");
Eric Biederman90089602004-05-28 14:11:54 +000018354 walk_blocks(state, &state->bb, print_interference_block, &info);
Eric Biedermand1ea5392003-06-28 06:49:45 +000018355
18356}
18357
Eric Biedermanb138ac82003-04-22 18:44:01 +000018358static unsigned regc_max_size(struct compile_state *state, int classes)
18359{
18360 unsigned max_size;
18361 int i;
18362 max_size = 0;
18363 for(i = 0; i < MAX_REGC; i++) {
18364 if (classes & (1 << i)) {
18365 unsigned size;
18366 size = arch_regc_size(state, i);
18367 if (size > max_size) {
18368 max_size = size;
18369 }
18370 }
18371 }
18372 return max_size;
18373}
18374
18375static int reg_is_reg(struct compile_state *state, int reg1, int reg2)
18376{
18377 unsigned equivs[MAX_REG_EQUIVS];
18378 int i;
18379 if ((reg1 < 0) || (reg1 >= MAX_REGISTERS)) {
18380 internal_error(state, 0, "invalid register");
18381 }
18382 if ((reg2 < 0) || (reg2 >= MAX_REGISTERS)) {
18383 internal_error(state, 0, "invalid register");
18384 }
18385 arch_reg_equivs(state, equivs, reg1);
18386 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18387 if (equivs[i] == reg2) {
18388 return 1;
18389 }
18390 }
18391 return 0;
18392}
18393
18394static void reg_fill_used(struct compile_state *state, char *used, int reg)
18395{
18396 unsigned equivs[MAX_REG_EQUIVS];
18397 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018398 if (reg == REG_UNNEEDED) {
18399 return;
18400 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018401 arch_reg_equivs(state, equivs, reg);
18402 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18403 used[equivs[i]] = 1;
18404 }
18405 return;
18406}
18407
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018408static void reg_inc_used(struct compile_state *state, char *used, int reg)
18409{
18410 unsigned equivs[MAX_REG_EQUIVS];
18411 int i;
18412 if (reg == REG_UNNEEDED) {
18413 return;
18414 }
18415 arch_reg_equivs(state, equivs, reg);
18416 for(i = 0; (i < MAX_REG_EQUIVS) && equivs[i] != REG_UNSET; i++) {
18417 used[equivs[i]] += 1;
18418 }
18419 return;
18420}
18421
Eric Biedermanb138ac82003-04-22 18:44:01 +000018422static unsigned int hash_live_edge(
18423 struct live_range *left, struct live_range *right)
18424{
18425 unsigned int hash, val;
18426 unsigned long lval, rval;
18427 lval = ((unsigned long)left)/sizeof(struct live_range);
18428 rval = ((unsigned long)right)/sizeof(struct live_range);
18429 hash = 0;
18430 while(lval) {
18431 val = lval & 0xff;
18432 lval >>= 8;
18433 hash = (hash *263) + val;
18434 }
18435 while(rval) {
18436 val = rval & 0xff;
18437 rval >>= 8;
18438 hash = (hash *263) + val;
18439 }
18440 hash = hash & (LRE_HASH_SIZE - 1);
18441 return hash;
18442}
18443
18444static struct lre_hash **lre_probe(struct reg_state *rstate,
18445 struct live_range *left, struct live_range *right)
18446{
18447 struct lre_hash **ptr;
18448 unsigned int index;
18449 /* Ensure left <= right */
18450 if (left > right) {
18451 struct live_range *tmp;
18452 tmp = left;
18453 left = right;
18454 right = tmp;
18455 }
18456 index = hash_live_edge(left, right);
18457
18458 ptr = &rstate->hash[index];
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018459 while(*ptr) {
18460 if (((*ptr)->left == left) && ((*ptr)->right == right)) {
18461 break;
18462 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018463 ptr = &(*ptr)->next;
18464 }
18465 return ptr;
18466}
18467
18468static int interfere(struct reg_state *rstate,
18469 struct live_range *left, struct live_range *right)
18470{
18471 struct lre_hash **ptr;
18472 ptr = lre_probe(rstate, left, right);
18473 return ptr && *ptr;
18474}
18475
18476static void add_live_edge(struct reg_state *rstate,
18477 struct live_range *left, struct live_range *right)
18478{
18479 /* FIXME the memory allocation overhead is noticeable here... */
18480 struct lre_hash **ptr, *new_hash;
18481 struct live_range_edge *edge;
18482
18483 if (left == right) {
18484 return;
18485 }
18486 if ((left == &rstate->lr[0]) || (right == &rstate->lr[0])) {
18487 return;
18488 }
18489 /* Ensure left <= right */
18490 if (left > right) {
18491 struct live_range *tmp;
18492 tmp = left;
18493 left = right;
18494 right = tmp;
18495 }
18496 ptr = lre_probe(rstate, left, right);
18497 if (*ptr) {
18498 return;
18499 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018500#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018501 fprintf(state->errout, "new_live_edge(%p, %p)\n",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018502 left, right);
18503#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000018504 new_hash = xmalloc(sizeof(*new_hash), "lre_hash");
18505 new_hash->next = *ptr;
18506 new_hash->left = left;
18507 new_hash->right = right;
18508 *ptr = new_hash;
18509
18510 edge = xmalloc(sizeof(*edge), "live_range_edge");
18511 edge->next = left->edges;
18512 edge->node = right;
18513 left->edges = edge;
18514 left->degree += 1;
18515
18516 edge = xmalloc(sizeof(*edge), "live_range_edge");
18517 edge->next = right->edges;
18518 edge->node = left;
18519 right->edges = edge;
18520 right->degree += 1;
18521}
18522
18523static void remove_live_edge(struct reg_state *rstate,
18524 struct live_range *left, struct live_range *right)
18525{
18526 struct live_range_edge *edge, **ptr;
18527 struct lre_hash **hptr, *entry;
18528 hptr = lre_probe(rstate, left, right);
18529 if (!hptr || !*hptr) {
18530 return;
18531 }
18532 entry = *hptr;
18533 *hptr = entry->next;
18534 xfree(entry);
18535
18536 for(ptr = &left->edges; *ptr; ptr = &(*ptr)->next) {
18537 edge = *ptr;
18538 if (edge->node == right) {
18539 *ptr = edge->next;
18540 memset(edge, 0, sizeof(*edge));
18541 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018542 right->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018543 break;
18544 }
18545 }
18546 for(ptr = &right->edges; *ptr; ptr = &(*ptr)->next) {
18547 edge = *ptr;
18548 if (edge->node == left) {
18549 *ptr = edge->next;
18550 memset(edge, 0, sizeof(*edge));
18551 xfree(edge);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018552 left->degree--;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018553 break;
18554 }
18555 }
18556}
18557
18558static void remove_live_edges(struct reg_state *rstate, struct live_range *range)
18559{
18560 struct live_range_edge *edge, *next;
18561 for(edge = range->edges; edge; edge = next) {
18562 next = edge->next;
18563 remove_live_edge(rstate, range, edge->node);
18564 }
18565}
18566
Eric Biederman153ea352003-06-20 14:43:20 +000018567static void transfer_live_edges(struct reg_state *rstate,
18568 struct live_range *dest, struct live_range *src)
18569{
18570 struct live_range_edge *edge, *next;
18571 for(edge = src->edges; edge; edge = next) {
18572 struct live_range *other;
18573 next = edge->next;
18574 other = edge->node;
18575 remove_live_edge(rstate, src, other);
18576 add_live_edge(rstate, dest, other);
18577 }
18578}
18579
Eric Biedermanb138ac82003-04-22 18:44:01 +000018580
18581/* Interference graph...
18582 *
18583 * new(n) --- Return a graph with n nodes but no edges.
18584 * add(g,x,y) --- Return a graph including g with an between x and y
18585 * interfere(g, x, y) --- Return true if there exists an edge between the nodes
18586 * x and y in the graph g
18587 * degree(g, x) --- Return the degree of the node x in the graph g
18588 * neighbors(g, x, f) --- Apply function f to each neighbor of node x in the graph g
18589 *
18590 * Implement with a hash table && a set of adjcency vectors.
18591 * The hash table supports constant time implementations of add and interfere.
18592 * The adjacency vectors support an efficient implementation of neighbors.
18593 */
18594
18595/*
18596 * +---------------------------------------------------+
18597 * | +--------------+ |
18598 * v v | |
18599 * renumber -> build graph -> colalesce -> spill_costs -> simplify -> select
18600 *
18601 * -- In simplify implment optimistic coloring... (No backtracking)
18602 * -- Implement Rematerialization it is the only form of spilling we can perform
18603 * Essentially this means dropping a constant from a register because
18604 * we can regenerate it later.
18605 *
18606 * --- Very conservative colalescing (don't colalesce just mark the opportunities)
18607 * coalesce at phi points...
18608 * --- Bias coloring if at all possible do the coalesing a compile time.
18609 *
18610 *
18611 */
18612
Stefan Reinauer50542a82007-10-24 11:14:14 +000018613#if DEBUG_ROMCC_WARNING
Eric Biedermanb138ac82003-04-22 18:44:01 +000018614static void different_colored(
18615 struct compile_state *state, struct reg_state *rstate,
18616 struct triple *parent, struct triple *ins)
18617{
18618 struct live_range *lr;
18619 struct triple **expr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018620 lr = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018621 expr = triple_rhs(state, ins, 0);
18622 for(;expr; expr = triple_rhs(state, ins, expr)) {
18623 struct live_range *lr2;
Eric Biederman0babc1c2003-05-09 02:39:00 +000018624 if (!*expr || (*expr == parent) || (*expr == ins)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018625 continue;
18626 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018627 lr2 = rstate->lrd[(*expr)->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018628 if (lr->color == lr2->color) {
18629 internal_error(state, ins, "live range too big");
18630 }
18631 }
18632}
Stefan Reinauer50542a82007-10-24 11:14:14 +000018633#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018634
18635static struct live_range *coalesce_ranges(
18636 struct compile_state *state, struct reg_state *rstate,
18637 struct live_range *lr1, struct live_range *lr2)
18638{
18639 struct live_range_def *head, *mid1, *mid2, *end, *lrd;
18640 unsigned color;
18641 unsigned classes;
18642 if (lr1 == lr2) {
18643 return lr1;
18644 }
18645 if (!lr1->defs || !lr2->defs) {
18646 internal_error(state, 0,
18647 "cannot coalese dead live ranges");
18648 }
18649 if ((lr1->color == REG_UNNEEDED) ||
18650 (lr2->color == REG_UNNEEDED)) {
18651 internal_error(state, 0,
18652 "cannot coalesce live ranges without a possible color");
18653 }
18654 if ((lr1->color != lr2->color) &&
18655 (lr1->color != REG_UNSET) &&
18656 (lr2->color != REG_UNSET)) {
18657 internal_error(state, lr1->defs->def,
18658 "cannot coalesce live ranges of different colors");
18659 }
18660 color = lr1->color;
18661 if (color == REG_UNSET) {
18662 color = lr2->color;
18663 }
18664 classes = lr1->classes & lr2->classes;
18665 if (!classes) {
18666 internal_error(state, lr1->defs->def,
18667 "cannot coalesce live ranges with dissimilar register classes");
18668 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018669 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000018670 FILE *fp = state->errout;
18671 fprintf(fp, "coalescing:");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018672 lrd = lr1->defs;
18673 do {
Eric Biederman90089602004-05-28 14:11:54 +000018674 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018675 lrd = lrd->next;
18676 } while(lrd != lr1->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018677 fprintf(fp, " |");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018678 lrd = lr2->defs;
18679 do {
Eric Biederman90089602004-05-28 14:11:54 +000018680 fprintf(fp, " %p", lrd->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000018681 lrd = lrd->next;
18682 } while(lrd != lr2->defs);
Eric Biederman90089602004-05-28 14:11:54 +000018683 fprintf(fp, "\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000018684 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018685 /* If there is a clear dominate live range put it in lr1,
18686 * For purposes of this test phi functions are
18687 * considered dominated by the definitions that feed into
18688 * them.
18689 */
18690 if ((lr1->defs->prev->def->op == OP_PHI) ||
18691 ((lr2->defs->prev->def->op != OP_PHI) &&
18692 tdominates(state, lr2->defs->def, lr1->defs->def))) {
18693 struct live_range *tmp;
18694 tmp = lr1;
18695 lr1 = lr2;
18696 lr2 = tmp;
18697 }
18698#if 0
18699 if (lr1->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018700 fprintf(state->errout, "lr1 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018701 }
18702 if (lr1->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018703 fprintf(state->errout, "lr1 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018704 }
18705 if (lr2->defs->orig_id & TRIPLE_FLAG_POST_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018706 fprintf(state->errout, "lr2 post\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018707 }
18708 if (lr2->defs->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
Eric Biederman90089602004-05-28 14:11:54 +000018709 fprintf(state->errout, "lr2 pre\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018710 }
18711#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018712#if 0
Eric Biederman90089602004-05-28 14:11:54 +000018713 fprintf(state->errout, "coalesce color1(%p): %3d color2(%p) %3d\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018714 lr1->defs->def,
18715 lr1->color,
18716 lr2->defs->def,
18717 lr2->color);
18718#endif
18719
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018720 /* Append lr2 onto lr1 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000018721#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018722#warning "FIXME should this be a merge instead of a splice?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000018723#endif
Eric Biederman153ea352003-06-20 14:43:20 +000018724 /* This FIXME item applies to the correctness of live_range_end
18725 * and to the necessity of making multiple passes of coalesce_live_ranges.
18726 * A failure to find some coalesce opportunities in coaleace_live_ranges
18727 * does not impact the correct of the compiler just the efficiency with
18728 * which registers are allocated.
18729 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018730 head = lr1->defs;
18731 mid1 = lr1->defs->prev;
18732 mid2 = lr2->defs;
18733 end = lr2->defs->prev;
18734
18735 head->prev = end;
18736 end->next = head;
18737
18738 mid1->next = mid2;
18739 mid2->prev = mid1;
18740
18741 /* Fixup the live range in the added live range defs */
18742 lrd = head;
18743 do {
18744 lrd->lr = lr1;
18745 lrd = lrd->next;
18746 } while(lrd != head);
18747
18748 /* Mark lr2 as free. */
18749 lr2->defs = 0;
18750 lr2->color = REG_UNNEEDED;
18751 lr2->classes = 0;
18752
18753 if (!lr1->defs) {
18754 internal_error(state, 0, "lr1->defs == 0 ?");
18755 }
18756
18757 lr1->color = color;
18758 lr1->classes = classes;
18759
Eric Biederman153ea352003-06-20 14:43:20 +000018760 /* Keep the graph in sync by transfering the edges from lr2 to lr1 */
18761 transfer_live_edges(rstate, lr1, lr2);
18762
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018763 return lr1;
18764}
18765
18766static struct live_range_def *live_range_head(
18767 struct compile_state *state, struct live_range *lr,
18768 struct live_range_def *last)
18769{
18770 struct live_range_def *result;
18771 result = 0;
18772 if (last == 0) {
18773 result = lr->defs;
18774 }
18775 else if (!tdominates(state, lr->defs->def, last->next->def)) {
18776 result = last->next;
18777 }
18778 return result;
18779}
18780
18781static struct live_range_def *live_range_end(
18782 struct compile_state *state, struct live_range *lr,
18783 struct live_range_def *last)
18784{
18785 struct live_range_def *result;
18786 result = 0;
18787 if (last == 0) {
18788 result = lr->defs->prev;
18789 }
18790 else if (!tdominates(state, last->prev->def, lr->defs->prev->def)) {
18791 result = last->prev;
18792 }
18793 return result;
18794}
18795
18796
Eric Biedermanb138ac82003-04-22 18:44:01 +000018797static void initialize_live_ranges(
18798 struct compile_state *state, struct reg_state *rstate)
18799{
18800 struct triple *ins, *first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018801 size_t count, size;
18802 int i, j;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018803
Eric Biederman83b991a2003-10-11 06:20:25 +000018804 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018805 /* First count how many instructions I have.
Eric Biedermanb138ac82003-04-22 18:44:01 +000018806 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018807 count = count_triples(state);
18808 /* Potentially I need one live range definitions for each
Eric Biedermand1ea5392003-06-28 06:49:45 +000018809 * instruction.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018810 */
Eric Biedermand1ea5392003-06-28 06:49:45 +000018811 rstate->defs = count;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018812 /* Potentially I need one live range for each instruction
18813 * plus an extra for the dummy live range.
18814 */
18815 rstate->ranges = count + 1;
18816 size = sizeof(rstate->lrd[0]) * rstate->defs;
18817 rstate->lrd = xcmalloc(size, "live_range_def");
18818 size = sizeof(rstate->lr[0]) * rstate->ranges;
18819 rstate->lr = xcmalloc(size, "live_range");
18820
Eric Biedermanb138ac82003-04-22 18:44:01 +000018821 /* Setup the dummy live range */
18822 rstate->lr[0].classes = 0;
18823 rstate->lr[0].color = REG_UNSET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018824 rstate->lr[0].defs = 0;
18825 i = j = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018826 ins = first;
18827 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018828 /* If the triple is a variable give it a live range */
Eric Biederman0babc1c2003-05-09 02:39:00 +000018829 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018830 struct reg_info info;
18831 /* Find the architecture specific color information */
18832 info = find_def_color(state, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018833 i++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018834 rstate->lr[i].defs = &rstate->lrd[j];
18835 rstate->lr[i].color = info.reg;
18836 rstate->lr[i].classes = info.regcm;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018837 rstate->lr[i].degree = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018838 rstate->lrd[j].lr = &rstate->lr[i];
18839 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018840 /* Otherwise give the triple the dummy live range. */
18841 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018842 rstate->lrd[j].lr = &rstate->lr[0];
Eric Biedermanb138ac82003-04-22 18:44:01 +000018843 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018844
18845 /* Initalize the live_range_def */
18846 rstate->lrd[j].next = &rstate->lrd[j];
18847 rstate->lrd[j].prev = &rstate->lrd[j];
18848 rstate->lrd[j].def = ins;
18849 rstate->lrd[j].orig_id = ins->id;
18850 ins->id = j;
18851
18852 j++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018853 ins = ins->next;
18854 } while(ins != first);
18855 rstate->ranges = i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018856
Eric Biedermanb138ac82003-04-22 18:44:01 +000018857 /* Make a second pass to handle achitecture specific register
18858 * constraints.
18859 */
18860 ins = first;
18861 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018862 int zlhs, zrhs, i, j;
18863 if (ins->id > rstate->defs) {
18864 internal_error(state, ins, "bad id");
18865 }
18866
18867 /* Walk through the template of ins and coalesce live ranges */
Eric Biederman90089602004-05-28 14:11:54 +000018868 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018869 if ((zlhs == 0) && triple_is_def(state, ins)) {
18870 zlhs = 1;
18871 }
Eric Biederman90089602004-05-28 14:11:54 +000018872 zrhs = ins->rhs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000018873
Eric Biederman5ade04a2003-10-22 04:03:46 +000018874 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018875 fprintf(state->errout, "mandatory coalesce: %p %d %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018876 ins, zlhs, zrhs);
18877 }
18878
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018879 for(i = 0; i < zlhs; i++) {
18880 struct reg_info linfo;
18881 struct live_range_def *lhs;
18882 linfo = arch_reg_lhs(state, ins, i);
18883 if (linfo.reg < MAX_REGISTERS) {
18884 continue;
18885 }
18886 if (triple_is_def(state, ins)) {
18887 lhs = &rstate->lrd[ins->id];
18888 } else {
18889 lhs = &rstate->lrd[LHS(ins, i)->id];
18890 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000018891
18892 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018893 fprintf(state->errout, "coalesce lhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018894 i, lhs, linfo.reg);
18895 }
18896
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018897 for(j = 0; j < zrhs; j++) {
18898 struct reg_info rinfo;
18899 struct live_range_def *rhs;
18900 rinfo = arch_reg_rhs(state, ins, j);
18901 if (rinfo.reg < MAX_REGISTERS) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000018902 continue;
18903 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000018904 rhs = &rstate->lrd[RHS(ins, j)->id];
Eric Biederman5ade04a2003-10-22 04:03:46 +000018905
18906 if (state->compiler->debug & DEBUG_COALESCING2) {
Eric Biederman90089602004-05-28 14:11:54 +000018907 fprintf(state->errout, "coalesce rhs(%d): %p %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000018908 j, rhs, rinfo.reg);
18909 }
18910
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018911 if (rinfo.reg == linfo.reg) {
18912 coalesce_ranges(state, rstate,
18913 lhs->lr, rhs->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018914 }
18915 }
18916 }
18917 ins = ins->next;
18918 } while(ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000018919}
18920
Eric Biedermanf96a8102003-06-16 16:57:34 +000018921static void graph_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000018922 struct compile_state *state,
18923 struct reg_block *blocks, struct triple_reg_set *live,
18924 struct reg_block *rb, struct triple *ins, void *arg)
18925{
18926 struct reg_state *rstate = arg;
18927 struct live_range *def;
18928 struct triple_reg_set *entry;
18929
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018930 /* If the triple is not a definition
Eric Biedermanb138ac82003-04-22 18:44:01 +000018931 * we do not have a definition to add to
18932 * the interference graph.
18933 */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018934 if (!triple_is_def(state, ins)) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000018935 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018936 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018937 def = rstate->lrd[ins->id].lr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018938
18939 /* Create an edge between ins and everything that is
18940 * alive, unless the live_range cannot share
18941 * a physical register with ins.
18942 */
18943 for(entry = live; entry; entry = entry->next) {
18944 struct live_range *lr;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000018945 if ((entry->member->id < 0) || (entry->member->id > rstate->defs)) {
18946 internal_error(state, 0, "bad entry?");
18947 }
18948 lr = rstate->lrd[entry->member->id].lr;
18949 if (def == lr) {
18950 continue;
18951 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000018952 if (!arch_regcm_intersect(def->classes, lr->classes)) {
18953 continue;
18954 }
18955 add_live_edge(rstate, def, lr);
18956 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000018957 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000018958}
18959
Stefan Reinauer50542a82007-10-24 11:14:14 +000018960#if DEBUG_CONSISTENCY > 1
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000018961static struct live_range *get_verify_live_range(
18962 struct compile_state *state, struct reg_state *rstate, struct triple *ins)
18963{
18964 struct live_range *lr;
18965 struct live_range_def *lrd;
18966 int ins_found;
18967 if ((ins->id < 0) || (ins->id > rstate->defs)) {
18968 internal_error(state, ins, "bad ins?");
18969 }
18970 lr = rstate->lrd[ins->id].lr;
18971 ins_found = 0;
18972 lrd = lr->defs;
18973 do {
18974 if (lrd->def == ins) {
18975 ins_found = 1;
18976 }
18977 lrd = lrd->next;
18978 } while(lrd != lr->defs);
18979 if (!ins_found) {
18980 internal_error(state, ins, "ins not in live range");
18981 }
18982 return lr;
18983}
18984
18985static void verify_graph_ins(
18986 struct compile_state *state,
18987 struct reg_block *blocks, struct triple_reg_set *live,
18988 struct reg_block *rb, struct triple *ins, void *arg)
18989{
18990 struct reg_state *rstate = arg;
18991 struct triple_reg_set *entry1, *entry2;
18992
18993
18994 /* Compare live against edges and make certain the code is working */
18995 for(entry1 = live; entry1; entry1 = entry1->next) {
18996 struct live_range *lr1;
18997 lr1 = get_verify_live_range(state, rstate, entry1->member);
18998 for(entry2 = live; entry2; entry2 = entry2->next) {
18999 struct live_range *lr2;
19000 struct live_range_edge *edge2;
19001 int lr1_found;
19002 int lr2_degree;
19003 if (entry2 == entry1) {
19004 continue;
19005 }
19006 lr2 = get_verify_live_range(state, rstate, entry2->member);
19007 if (lr1 == lr2) {
19008 internal_error(state, entry2->member,
19009 "live range with 2 values simultaneously alive");
19010 }
19011 if (!arch_regcm_intersect(lr1->classes, lr2->classes)) {
19012 continue;
19013 }
19014 if (!interfere(rstate, lr1, lr2)) {
19015 internal_error(state, entry2->member,
19016 "edges don't interfere?");
19017 }
19018
19019 lr1_found = 0;
19020 lr2_degree = 0;
19021 for(edge2 = lr2->edges; edge2; edge2 = edge2->next) {
19022 lr2_degree++;
19023 if (edge2->node == lr1) {
19024 lr1_found = 1;
19025 }
19026 }
19027 if (lr2_degree != lr2->degree) {
19028 internal_error(state, entry2->member,
19029 "computed degree: %d does not match reported degree: %d\n",
19030 lr2_degree, lr2->degree);
19031 }
19032 if (!lr1_found) {
19033 internal_error(state, entry2->member, "missing edge");
19034 }
19035 }
19036 }
19037 return;
19038}
Stefan Reinauer50542a82007-10-24 11:14:14 +000019039#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000019040
Eric Biedermanf96a8102003-06-16 16:57:34 +000019041static void print_interference_ins(
Eric Biedermanb138ac82003-04-22 18:44:01 +000019042 struct compile_state *state,
19043 struct reg_block *blocks, struct triple_reg_set *live,
19044 struct reg_block *rb, struct triple *ins, void *arg)
19045{
19046 struct reg_state *rstate = arg;
19047 struct live_range *lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019048 unsigned id;
Eric Biederman7dea9552004-06-29 05:38:37 +000019049 FILE *fp = state->dbgout;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019050
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019051 lr = rstate->lrd[ins->id].lr;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019052 id = ins->id;
19053 ins->id = rstate->lrd[id].orig_id;
19054 SET_REG(ins->id, lr->color);
Eric Biederman90089602004-05-28 14:11:54 +000019055 display_triple(state->dbgout, ins);
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019056 ins->id = id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019057
19058 if (lr->defs) {
19059 struct live_range_def *lrd;
Eric Biederman7dea9552004-06-29 05:38:37 +000019060 fprintf(fp, " range:");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019061 lrd = lr->defs;
19062 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019063 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019064 lrd = lrd->next;
19065 } while(lrd != lr->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019066 fprintf(fp, "\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019067 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019068 if (live) {
19069 struct triple_reg_set *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019070 fprintf(fp, " live:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019071 for(entry = live; entry; entry = entry->next) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019072 fprintf(fp, " %-10p", entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019073 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019074 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019075 }
19076 if (lr->edges) {
19077 struct live_range_edge *entry;
Eric Biederman7dea9552004-06-29 05:38:37 +000019078 fprintf(fp, " edges:");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019079 for(entry = lr->edges; entry; entry = entry->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019080 struct live_range_def *lrd;
19081 lrd = entry->node->defs;
19082 do {
Eric Biederman7dea9552004-06-29 05:38:37 +000019083 fprintf(fp, " %-10p", lrd->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019084 lrd = lrd->next;
19085 } while(lrd != entry->node->defs);
Eric Biederman7dea9552004-06-29 05:38:37 +000019086 fprintf(fp, "|");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019087 }
Eric Biederman7dea9552004-06-29 05:38:37 +000019088 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019089 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000019090 if (triple_is_branch(state, ins)) {
Eric Biederman7dea9552004-06-29 05:38:37 +000019091 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019092 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019093 return;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019094}
19095
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019096static int coalesce_live_ranges(
19097 struct compile_state *state, struct reg_state *rstate)
19098{
19099 /* At the point where a value is moved from one
19100 * register to another that value requires two
19101 * registers, thus increasing register pressure.
19102 * Live range coaleescing reduces the register
19103 * pressure by keeping a value in one register
19104 * longer.
19105 *
19106 * In the case of a phi function all paths leading
19107 * into it must be allocated to the same register
19108 * otherwise the phi function may not be removed.
19109 *
19110 * Forcing a value to stay in a single register
19111 * for an extended period of time does have
19112 * limitations when applied to non homogenous
19113 * register pool.
19114 *
19115 * The two cases I have identified are:
19116 * 1) Two forced register assignments may
19117 * collide.
19118 * 2) Registers may go unused because they
19119 * are only good for storing the value
19120 * and not manipulating it.
19121 *
19122 * Because of this I need to split live ranges,
19123 * even outside of the context of coalesced live
19124 * ranges. The need to split live ranges does
19125 * impose some constraints on live range coalescing.
19126 *
19127 * - Live ranges may not be coalesced across phi
19128 * functions. This creates a 2 headed live
19129 * range that cannot be sanely split.
19130 *
19131 * - phi functions (coalesced in initialize_live_ranges)
19132 * are handled as pre split live ranges so we will
19133 * never attempt to split them.
19134 */
19135 int coalesced;
19136 int i;
19137
19138 coalesced = 0;
19139 for(i = 0; i <= rstate->ranges; i++) {
19140 struct live_range *lr1;
19141 struct live_range_def *lrd1;
19142 lr1 = &rstate->lr[i];
19143 if (!lr1->defs) {
19144 continue;
19145 }
19146 lrd1 = live_range_end(state, lr1, 0);
19147 for(; lrd1; lrd1 = live_range_end(state, lr1, lrd1)) {
19148 struct triple_set *set;
19149 if (lrd1->def->op != OP_COPY) {
19150 continue;
19151 }
19152 /* Skip copies that are the result of a live range split. */
19153 if (lrd1->orig_id & TRIPLE_FLAG_POST_SPLIT) {
19154 continue;
19155 }
19156 for(set = lrd1->def->use; set; set = set->next) {
19157 struct live_range_def *lrd2;
19158 struct live_range *lr2, *res;
19159
19160 lrd2 = &rstate->lrd[set->member->id];
19161
19162 /* Don't coalesce with instructions
19163 * that are the result of a live range
19164 * split.
19165 */
19166 if (lrd2->orig_id & TRIPLE_FLAG_PRE_SPLIT) {
19167 continue;
19168 }
19169 lr2 = rstate->lrd[set->member->id].lr;
19170 if (lr1 == lr2) {
19171 continue;
19172 }
19173 if ((lr1->color != lr2->color) &&
19174 (lr1->color != REG_UNSET) &&
19175 (lr2->color != REG_UNSET)) {
19176 continue;
19177 }
19178 if ((lr1->classes & lr2->classes) == 0) {
19179 continue;
19180 }
19181
19182 if (interfere(rstate, lr1, lr2)) {
19183 continue;
19184 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000019185
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019186 res = coalesce_ranges(state, rstate, lr1, lr2);
19187 coalesced += 1;
19188 if (res != lr1) {
19189 goto next;
19190 }
19191 }
19192 }
19193 next:
Eric Biederman05f26fc2003-06-11 21:55:00 +000019194 ;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019195 }
19196 return coalesced;
19197}
19198
19199
Eric Biedermanf96a8102003-06-16 16:57:34 +000019200static void fix_coalesce_conflicts(struct compile_state *state,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019201 struct reg_block *blocks, struct triple_reg_set *live,
19202 struct reg_block *rb, struct triple *ins, void *arg)
19203{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019204 int *conflicts = arg;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019205 int zlhs, zrhs, i, j;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019206
19207 /* See if we have a mandatory coalesce operation between
19208 * a lhs and a rhs value. If so and the rhs value is also
19209 * alive then this triple needs to be pre copied. Otherwise
19210 * we would have two definitions in the same live range simultaneously
19211 * alive.
19212 */
Eric Biederman90089602004-05-28 14:11:54 +000019213 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019214 if ((zlhs == 0) && triple_is_def(state, ins)) {
19215 zlhs = 1;
19216 }
Eric Biederman90089602004-05-28 14:11:54 +000019217 zrhs = ins->rhs;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019218 for(i = 0; i < zlhs; i++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019219 struct reg_info linfo;
19220 linfo = arch_reg_lhs(state, ins, i);
19221 if (linfo.reg < MAX_REGISTERS) {
19222 continue;
19223 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019224 for(j = 0; j < zrhs; j++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019225 struct reg_info rinfo;
19226 struct triple *rhs;
19227 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019228 int found;
19229 found = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019230 rinfo = arch_reg_rhs(state, ins, j);
19231 if (rinfo.reg != linfo.reg) {
19232 continue;
19233 }
19234 rhs = RHS(ins, j);
Eric Biedermanf96a8102003-06-16 16:57:34 +000019235 for(set = live; set && !found; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019236 if (set->member == rhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019237 found = 1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019238 }
19239 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019240 if (found) {
19241 struct triple *copy;
19242 copy = pre_copy(state, ins, j);
19243 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019244 (*conflicts)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019245 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019246 }
19247 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019248 return;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019249}
19250
Eric Biedermand1ea5392003-06-28 06:49:45 +000019251static int correct_coalesce_conflicts(
19252 struct compile_state *state, struct reg_block *blocks)
19253{
19254 int conflicts;
19255 conflicts = 0;
Eric Biederman90089602004-05-28 14:11:54 +000019256 walk_variable_lifetimes(state, &state->bb, blocks,
19257 fix_coalesce_conflicts, &conflicts);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019258 return conflicts;
19259}
19260
Eric Biedermanf96a8102003-06-16 16:57:34 +000019261static void replace_set_use(struct compile_state *state,
19262 struct triple_reg_set *head, struct triple *orig, struct triple *new)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019263{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019264 struct triple_reg_set *set;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019265 for(set = head; set; set = set->next) {
19266 if (set->member == orig) {
19267 set->member = new;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019268 }
19269 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019270}
19271
Eric Biedermanf96a8102003-06-16 16:57:34 +000019272static void replace_block_use(struct compile_state *state,
19273 struct reg_block *blocks, struct triple *orig, struct triple *new)
19274{
19275 int i;
Stefan Reinauer50542a82007-10-24 11:14:14 +000019276#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019277#warning "WISHLIST visit just those blocks that need it *"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019278#endif
Eric Biederman90089602004-05-28 14:11:54 +000019279 for(i = 1; i <= state->bb.last_vertex; i++) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019280 struct reg_block *rb;
19281 rb = &blocks[i];
19282 replace_set_use(state, rb->in, orig, new);
19283 replace_set_use(state, rb->out, orig, new);
19284 }
19285}
19286
19287static void color_instructions(struct compile_state *state)
19288{
19289 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000019290 first = state->first;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019291 ins = first;
19292 do {
19293 if (triple_is_def(state, ins)) {
19294 struct reg_info info;
19295 info = find_lhs_color(state, ins, 0);
19296 if (info.reg >= MAX_REGISTERS) {
19297 info.reg = REG_UNSET;
19298 }
19299 SET_INFO(ins->id, info);
19300 }
19301 ins = ins->next;
19302 } while(ins != first);
19303}
19304
19305static struct reg_info read_lhs_color(
19306 struct compile_state *state, struct triple *ins, int index)
19307{
19308 struct reg_info info;
19309 if ((index == 0) && triple_is_def(state, ins)) {
19310 info.reg = ID_REG(ins->id);
19311 info.regcm = ID_REGCM(ins->id);
19312 }
Eric Biederman90089602004-05-28 14:11:54 +000019313 else if (index < ins->lhs) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019314 info = read_lhs_color(state, LHS(ins, index), 0);
19315 }
19316 else {
19317 internal_error(state, ins, "Bad lhs %d", index);
19318 info.reg = REG_UNSET;
19319 info.regcm = 0;
19320 }
19321 return info;
19322}
19323
19324static struct triple *resolve_tangle(
19325 struct compile_state *state, struct triple *tangle)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019326{
19327 struct reg_info info, uinfo;
19328 struct triple_set *set, *next;
19329 struct triple *copy;
19330
Stefan Reinauer50542a82007-10-24 11:14:14 +000019331#if DEBUG_ROMCC_WARNINGS
Eric Biedermanf96a8102003-06-16 16:57:34 +000019332#warning "WISHLIST recalculate all affected instructions colors"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019333#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019334 info = find_lhs_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019335 for(set = tangle->use; set; set = next) {
19336 struct triple *user;
19337 int i, zrhs;
19338 next = set->next;
19339 user = set->member;
Eric Biederman90089602004-05-28 14:11:54 +000019340 zrhs = user->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019341 for(i = 0; i < zrhs; i++) {
19342 if (RHS(user, i) != tangle) {
19343 continue;
19344 }
19345 uinfo = find_rhs_post_color(state, user, i);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019346 if (uinfo.reg == info.reg) {
19347 copy = pre_copy(state, user, i);
19348 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019349 SET_INFO(copy->id, uinfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019350 }
19351 }
19352 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019353 copy = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019354 uinfo = find_lhs_pre_color(state, tangle, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019355 if (uinfo.reg == info.reg) {
Eric Biedermanf96a8102003-06-16 16:57:34 +000019356 struct reg_info linfo;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019357 copy = post_copy(state, tangle);
19358 copy->id |= TRIPLE_FLAG_PRE_SPLIT;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019359 linfo = find_lhs_color(state, copy, 0);
19360 SET_INFO(copy->id, linfo);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019361 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019362 info = find_lhs_color(state, tangle, 0);
19363 SET_INFO(tangle->id, info);
19364
19365 return copy;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019366}
19367
19368
Eric Biedermanf96a8102003-06-16 16:57:34 +000019369static void fix_tangles(struct compile_state *state,
19370 struct reg_block *blocks, struct triple_reg_set *live,
19371 struct reg_block *rb, struct triple *ins, void *arg)
19372{
Eric Biederman153ea352003-06-20 14:43:20 +000019373 int *tangles = arg;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019374 struct triple *tangle;
19375 do {
19376 char used[MAX_REGISTERS];
19377 struct triple_reg_set *set;
19378 tangle = 0;
19379
19380 /* Find out which registers have multiple uses at this point */
19381 memset(used, 0, sizeof(used));
19382 for(set = live; set; set = set->next) {
19383 struct reg_info info;
19384 info = read_lhs_color(state, set->member, 0);
19385 if (info.reg == REG_UNSET) {
19386 continue;
19387 }
19388 reg_inc_used(state, used, info.reg);
19389 }
19390
19391 /* Now find the least dominated definition of a register in
19392 * conflict I have seen so far.
19393 */
19394 for(set = live; set; set = set->next) {
19395 struct reg_info info;
19396 info = read_lhs_color(state, set->member, 0);
19397 if (used[info.reg] < 2) {
19398 continue;
19399 }
Eric Biederman153ea352003-06-20 14:43:20 +000019400 /* Changing copies that feed into phi functions
19401 * is incorrect.
19402 */
19403 if (set->member->use &&
19404 (set->member->use->member->op == OP_PHI)) {
19405 continue;
19406 }
Eric Biedermanf96a8102003-06-16 16:57:34 +000019407 if (!tangle || tdominates(state, set->member, tangle)) {
19408 tangle = set->member;
19409 }
19410 }
19411 /* If I have found a tangle resolve it */
19412 if (tangle) {
19413 struct triple *post_copy;
Eric Biederman153ea352003-06-20 14:43:20 +000019414 (*tangles)++;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019415 post_copy = resolve_tangle(state, tangle);
19416 if (post_copy) {
19417 replace_block_use(state, blocks, tangle, post_copy);
19418 }
19419 if (post_copy && (tangle != ins)) {
19420 replace_set_use(state, live, tangle, post_copy);
19421 }
19422 }
19423 } while(tangle);
19424 return;
19425}
19426
Eric Biederman153ea352003-06-20 14:43:20 +000019427static int correct_tangles(
Eric Biedermanf96a8102003-06-16 16:57:34 +000019428 struct compile_state *state, struct reg_block *blocks)
19429{
Eric Biederman153ea352003-06-20 14:43:20 +000019430 int tangles;
19431 tangles = 0;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019432 color_instructions(state);
Eric Biederman90089602004-05-28 14:11:54 +000019433 walk_variable_lifetimes(state, &state->bb, blocks,
19434 fix_tangles, &tangles);
Eric Biederman153ea352003-06-20 14:43:20 +000019435 return tangles;
Eric Biedermanf96a8102003-06-16 16:57:34 +000019436}
19437
Eric Biedermand1ea5392003-06-28 06:49:45 +000019438
19439static void ids_from_rstate(struct compile_state *state, struct reg_state *rstate);
19440static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate);
19441
19442struct triple *find_constrained_def(
19443 struct compile_state *state, struct live_range *range, struct triple *constrained)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019444{
Eric Biederman5ade04a2003-10-22 04:03:46 +000019445 struct live_range_def *lrd, *lrd_next;
19446 lrd_next = range->defs;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019447 do {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019448 struct reg_info info;
Eric Biedermand1ea5392003-06-28 06:49:45 +000019449 unsigned regcm;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019450
19451 lrd = lrd_next;
19452 lrd_next = lrd->next;
19453
Eric Biedermand1ea5392003-06-28 06:49:45 +000019454 regcm = arch_type_to_regcm(state, lrd->def->type);
19455 info = find_lhs_color(state, lrd->def, 0);
19456 regcm = arch_regcm_reg_normalize(state, regcm);
19457 info.regcm = arch_regcm_reg_normalize(state, info.regcm);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019458 /* If the 2 register class masks are equal then
19459 * the current register class is not constrained.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019460 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019461 if (regcm == info.regcm) {
19462 continue;
19463 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019464
Eric Biederman5ade04a2003-10-22 04:03:46 +000019465 /* If there is just one use.
19466 * That use cannot accept a larger register class.
19467 * There are no intervening definitions except
19468 * definitions that feed into that use.
19469 * Then a triple is not constrained.
19470 * FIXME handle this case!
19471 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019472#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019473#warning "FIXME ignore cases that cannot be fixed (a definition followed by a use)"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019474#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019475
19476
Eric Biedermand1ea5392003-06-28 06:49:45 +000019477 /* Of the constrained live ranges deal with the
19478 * least dominated one first.
Eric Biedermand3283ec2003-06-18 11:03:18 +000019479 */
Eric Biederman5ade04a2003-10-22 04:03:46 +000019480 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019481 fprintf(state->errout, "canidate: %p %-8s regcm: %x %x\n",
Eric Biederman530b5192003-07-01 10:05:30 +000019482 lrd->def, tops(lrd->def->op), regcm, info.regcm);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019483 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019484 if (!constrained ||
19485 tdominates(state, lrd->def, constrained))
19486 {
19487 constrained = lrd->def;
19488 }
19489 } while(lrd_next != range->defs);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019490 return constrained;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019491}
19492
Eric Biedermand1ea5392003-06-28 06:49:45 +000019493static int split_constrained_ranges(
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019494 struct compile_state *state, struct reg_state *rstate,
Eric Biedermand1ea5392003-06-28 06:49:45 +000019495 struct live_range *range)
19496{
19497 /* Walk through the edges in conflict and our current live
19498 * range, and find definitions that are more severly constrained
19499 * than they type of data they contain require.
19500 *
19501 * Then pick one of those ranges and relax the constraints.
19502 */
19503 struct live_range_edge *edge;
19504 struct triple *constrained;
19505
19506 constrained = 0;
19507 for(edge = range->edges; edge; edge = edge->next) {
19508 constrained = find_constrained_def(state, edge->node, constrained);
19509 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000019510#if DEBUG_ROMCC_WARNINGS
Eric Biederman5ade04a2003-10-22 04:03:46 +000019511#warning "FIXME should I call find_constrained_def here only if no previous constrained def was found?"
Stefan Reinauer50542a82007-10-24 11:14:14 +000019512#endif
Eric Biedermand1ea5392003-06-28 06:49:45 +000019513 if (!constrained) {
19514 constrained = find_constrained_def(state, range, constrained);
19515 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019516
19517 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019518 fprintf(state->errout, "constrained: ");
19519 display_triple(state->errout, constrained);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019520 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019521 if (constrained) {
19522 ids_from_rstate(state, rstate);
19523 cleanup_rstate(state, rstate);
19524 resolve_tangle(state, constrained);
19525 }
19526 return !!constrained;
19527}
19528
19529static int split_ranges(
19530 struct compile_state *state, struct reg_state *rstate,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019531 char *used, struct live_range *range)
19532{
Eric Biedermand1ea5392003-06-28 06:49:45 +000019533 int split;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019534 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000019535 fprintf(state->errout, "split_ranges %d %s %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000019536 rstate->passes, tops(range->defs->def->op), range->defs->def);
19537 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019538 if ((range->color == REG_UNNEEDED) ||
19539 (rstate->passes >= rstate->max_passes)) {
19540 return 0;
19541 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019542 split = split_constrained_ranges(state, rstate, range);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019543
Eric Biedermand1ea5392003-06-28 06:49:45 +000019544 /* Ideally I would split the live range that will not be used
19545 * for the longest period of time in hopes that this will
19546 * (a) allow me to spill a register or
19547 * (b) allow me to place a value in another register.
19548 *
19549 * So far I don't have a test case for this, the resolving
19550 * of mandatory constraints has solved all of my
19551 * know issues. So I have choosen not to write any
19552 * code until I cat get a better feel for cases where
19553 * it would be useful to have.
19554 *
19555 */
Stefan Reinauer50542a82007-10-24 11:14:14 +000019556#if DEBUG_ROMCC_WARNINGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019557#warning "WISHLIST implement live range splitting..."
Stefan Reinauer50542a82007-10-24 11:14:14 +000019558#endif
Eric Biederman5ade04a2003-10-22 04:03:46 +000019559
19560 if (!split && (state->compiler->debug & DEBUG_RANGE_CONFLICTS2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019561 FILE *fp = state->errout;
19562 print_interference_blocks(state, rstate, fp, 0);
19563 print_dominators(state, fp, &state->bb);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019564 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000019565 return split;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019566}
19567
Eric Biederman5ade04a2003-10-22 04:03:46 +000019568static FILE *cgdebug_fp(struct compile_state *state)
19569{
19570 FILE *fp;
19571 fp = 0;
19572 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH2)) {
Eric Biederman90089602004-05-28 14:11:54 +000019573 fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019574 }
19575 if (!fp && (state->compiler->debug & DEBUG_COLOR_GRAPH)) {
Eric Biederman90089602004-05-28 14:11:54 +000019576 fp = state->dbgout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000019577 }
19578 return fp;
19579}
Eric Biedermanb138ac82003-04-22 18:44:01 +000019580
Eric Biederman5ade04a2003-10-22 04:03:46 +000019581static void cgdebug_printf(struct compile_state *state, const char *fmt, ...)
19582{
19583 FILE *fp;
19584 fp = cgdebug_fp(state);
19585 if (fp) {
19586 va_list args;
19587 va_start(args, fmt);
19588 vfprintf(fp, fmt, args);
19589 va_end(args);
19590 }
19591}
19592
19593static void cgdebug_flush(struct compile_state *state)
19594{
19595 FILE *fp;
19596 fp = cgdebug_fp(state);
19597 if (fp) {
19598 fflush(fp);
19599 }
19600}
19601
19602static void cgdebug_loc(struct compile_state *state, struct triple *ins)
19603{
19604 FILE *fp;
19605 fp = cgdebug_fp(state);
19606 if (fp) {
19607 loc(fp, state, ins);
19608 }
19609}
19610
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019611static int select_free_color(struct compile_state *state,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019612 struct reg_state *rstate, struct live_range *range)
19613{
19614 struct triple_set *entry;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019615 struct live_range_def *lrd;
19616 struct live_range_def *phi;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019617 struct live_range_edge *edge;
19618 char used[MAX_REGISTERS];
19619 struct triple **expr;
19620
Eric Biedermanb138ac82003-04-22 18:44:01 +000019621 /* Instead of doing just the trivial color select here I try
19622 * a few extra things because a good color selection will help reduce
19623 * copies.
19624 */
19625
19626 /* Find the registers currently in use */
19627 memset(used, 0, sizeof(used));
19628 for(edge = range->edges; edge; edge = edge->next) {
19629 if (edge->node->color == REG_UNSET) {
19630 continue;
19631 }
19632 reg_fill_used(state, used, edge->node->color);
19633 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019634
19635 if (state->compiler->debug & DEBUG_COLOR_GRAPH2) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019636 int i;
19637 i = 0;
19638 for(edge = range->edges; edge; edge = edge->next) {
19639 i++;
19640 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019641 cgdebug_printf(state, "\n%s edges: %d",
19642 tops(range->defs->def->op), i);
19643 cgdebug_loc(state, range->defs->def);
19644 cgdebug_printf(state, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019645 for(i = 0; i < MAX_REGISTERS; i++) {
19646 if (used[i]) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019647 cgdebug_printf(state, "used: %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019648 arch_reg_str(i));
19649 }
19650 }
19651 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019652
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019653 /* If a color is already assigned see if it will work */
19654 if (range->color != REG_UNSET) {
19655 struct live_range_def *lrd;
19656 if (!used[range->color]) {
19657 return 1;
19658 }
19659 for(edge = range->edges; edge; edge = edge->next) {
19660 if (edge->node->color != range->color) {
19661 continue;
19662 }
19663 warning(state, edge->node->defs->def, "edge: ");
19664 lrd = edge->node->defs;
19665 do {
19666 warning(state, lrd->def, " %p %s",
19667 lrd->def, tops(lrd->def->op));
19668 lrd = lrd->next;
19669 } while(lrd != edge->node->defs);
19670 }
19671 lrd = range->defs;
19672 warning(state, range->defs->def, "def: ");
19673 do {
19674 warning(state, lrd->def, " %p %s",
19675 lrd->def, tops(lrd->def->op));
19676 lrd = lrd->next;
19677 } while(lrd != range->defs);
19678 internal_error(state, range->defs->def,
19679 "live range with already used color %s",
19680 arch_reg_str(range->color));
19681 }
19682
Eric Biedermanb138ac82003-04-22 18:44:01 +000019683 /* If I feed into an expression reuse it's color.
19684 * This should help remove copies in the case of 2 register instructions
19685 * and phi functions.
19686 */
19687 phi = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019688 lrd = live_range_end(state, range, 0);
19689 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_end(state, range, lrd)) {
19690 entry = lrd->def->use;
19691 for(;(range->color == REG_UNSET) && entry; entry = entry->next) {
19692 struct live_range_def *insd;
Eric Biederman530b5192003-07-01 10:05:30 +000019693 unsigned regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019694 insd = &rstate->lrd[entry->member->id];
19695 if (insd->lr->defs == 0) {
19696 continue;
19697 }
19698 if (!phi && (insd->def->op == OP_PHI) &&
19699 !interfere(rstate, range, insd->lr)) {
19700 phi = insd;
19701 }
Eric Biederman530b5192003-07-01 10:05:30 +000019702 if (insd->lr->color == REG_UNSET) {
19703 continue;
19704 }
19705 regcm = insd->lr->classes;
19706 if (((regcm & range->classes) == 0) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019707 (used[insd->lr->color])) {
19708 continue;
19709 }
19710 if (interfere(rstate, range, insd->lr)) {
19711 continue;
19712 }
19713 range->color = insd->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019714 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019715 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019716 /* If I feed into a phi function reuse it's color or the color
Eric Biedermanb138ac82003-04-22 18:44:01 +000019717 * of something else that feeds into the phi function.
19718 */
19719 if (phi) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019720 if (phi->lr->color != REG_UNSET) {
19721 if (used[phi->lr->color]) {
19722 range->color = phi->lr->color;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019723 }
19724 }
19725 else {
19726 expr = triple_rhs(state, phi->def, 0);
19727 for(; expr; expr = triple_rhs(state, phi->def, expr)) {
19728 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019729 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019730 if (!*expr) {
19731 continue;
19732 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019733 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019734 if (lr->color == REG_UNSET) {
19735 continue;
19736 }
19737 regcm = lr->classes;
19738 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019739 (used[lr->color])) {
19740 continue;
19741 }
19742 if (interfere(rstate, range, lr)) {
19743 continue;
19744 }
19745 range->color = lr->color;
19746 }
19747 }
19748 }
19749 /* If I don't interfere with a rhs node reuse it's color */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019750 lrd = live_range_head(state, range, 0);
19751 for(; (range->color == REG_UNSET) && lrd ; lrd = live_range_head(state, range, lrd)) {
19752 expr = triple_rhs(state, lrd->def, 0);
19753 for(; expr; expr = triple_rhs(state, lrd->def, expr)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019754 struct live_range *lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019755 unsigned regcm;
Eric Biederman0babc1c2003-05-09 02:39:00 +000019756 if (!*expr) {
19757 continue;
19758 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019759 lr = rstate->lrd[(*expr)->id].lr;
Eric Biederman530b5192003-07-01 10:05:30 +000019760 if (lr->color == REG_UNSET) {
19761 continue;
19762 }
19763 regcm = lr->classes;
19764 if (((regcm & range->classes) == 0) ||
Eric Biedermanb138ac82003-04-22 18:44:01 +000019765 (used[lr->color])) {
19766 continue;
19767 }
19768 if (interfere(rstate, range, lr)) {
19769 continue;
19770 }
19771 range->color = lr->color;
19772 break;
19773 }
19774 }
19775 /* If I have not opportunitically picked a useful color
19776 * pick the first color that is free.
19777 */
19778 if (range->color == REG_UNSET) {
19779 range->color =
19780 arch_select_free_register(state, used, range->classes);
19781 }
19782 if (range->color == REG_UNSET) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019783 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019784 int i;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019785 if (split_ranges(state, rstate, used, range)) {
19786 return 0;
19787 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019788 for(edge = range->edges; edge; edge = edge->next) {
Eric Biedermand3283ec2003-06-18 11:03:18 +000019789 warning(state, edge->node->defs->def, "edge reg %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019790 arch_reg_str(edge->node->color));
Eric Biedermand3283ec2003-06-18 11:03:18 +000019791 lrd = edge->node->defs;
19792 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019793 warning(state, lrd->def, " %s %p",
19794 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019795 lrd = lrd->next;
19796 } while(lrd != edge->node->defs);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019797 }
Eric Biedermand3283ec2003-06-18 11:03:18 +000019798 warning(state, range->defs->def, "range: ");
19799 lrd = range->defs;
19800 do {
Eric Biedermand1ea5392003-06-28 06:49:45 +000019801 warning(state, lrd->def, " %s %p",
19802 tops(lrd->def->op), lrd->def);
Eric Biedermand3283ec2003-06-18 11:03:18 +000019803 lrd = lrd->next;
19804 } while(lrd != range->defs);
19805
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019806 warning(state, range->defs->def, "classes: %x",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019807 range->classes);
19808 for(i = 0; i < MAX_REGISTERS; i++) {
19809 if (used[i]) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019810 warning(state, range->defs->def, "used: %s",
Eric Biedermanb138ac82003-04-22 18:44:01 +000019811 arch_reg_str(i));
19812 }
19813 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019814 error(state, range->defs->def, "too few registers");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019815 }
Eric Biederman530b5192003-07-01 10:05:30 +000019816 range->classes &= arch_reg_regcm(state, range->color);
19817 if ((range->color == REG_UNSET) || (range->classes == 0)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019818 internal_error(state, range->defs->def, "select_free_color did not?");
19819 }
19820 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019821}
19822
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019823static int color_graph(struct compile_state *state, struct reg_state *rstate)
Eric Biedermanb138ac82003-04-22 18:44:01 +000019824{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019825 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019826 struct live_range_edge *edge;
19827 struct live_range *range;
19828 if (rstate->low) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019829 cgdebug_printf(state, "Lo: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019830 range = rstate->low;
19831 if (*range->group_prev != range) {
19832 internal_error(state, 0, "lo: *prev != range?");
19833 }
19834 *range->group_prev = range->group_next;
19835 if (range->group_next) {
19836 range->group_next->group_prev = range->group_prev;
19837 }
19838 if (&range->group_next == rstate->low_tail) {
19839 rstate->low_tail = range->group_prev;
19840 }
19841 if (rstate->low == range) {
19842 internal_error(state, 0, "low: next != prev?");
19843 }
19844 }
19845 else if (rstate->high) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019846 cgdebug_printf(state, "Hi: ");
Eric Biedermanb138ac82003-04-22 18:44:01 +000019847 range = rstate->high;
19848 if (*range->group_prev != range) {
19849 internal_error(state, 0, "hi: *prev != range?");
19850 }
19851 *range->group_prev = range->group_next;
19852 if (range->group_next) {
19853 range->group_next->group_prev = range->group_prev;
19854 }
19855 if (&range->group_next == rstate->high_tail) {
19856 rstate->high_tail = range->group_prev;
19857 }
19858 if (rstate->high == range) {
19859 internal_error(state, 0, "high: next != prev?");
19860 }
19861 }
19862 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019863 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019864 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019865 cgdebug_printf(state, " %d\n", range - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019866 range->group_prev = 0;
19867 for(edge = range->edges; edge; edge = edge->next) {
19868 struct live_range *node;
19869 node = edge->node;
19870 /* Move nodes from the high to the low list */
19871 if (node->group_prev && (node->color == REG_UNSET) &&
19872 (node->degree == regc_max_size(state, node->classes))) {
19873 if (*node->group_prev != node) {
19874 internal_error(state, 0, "move: *prev != node?");
19875 }
19876 *node->group_prev = node->group_next;
19877 if (node->group_next) {
19878 node->group_next->group_prev = node->group_prev;
19879 }
19880 if (&node->group_next == rstate->high_tail) {
19881 rstate->high_tail = node->group_prev;
19882 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000019883 cgdebug_printf(state, "Moving...%d to low\n", node - rstate->lr);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019884 node->group_prev = rstate->low_tail;
19885 node->group_next = 0;
19886 *rstate->low_tail = node;
19887 rstate->low_tail = &node->group_next;
19888 if (*node->group_prev != node) {
19889 internal_error(state, 0, "move2: *prev != node?");
19890 }
19891 }
19892 node->degree -= 1;
19893 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019894 colored = color_graph(state, rstate);
19895 if (colored) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000019896 cgdebug_printf(state, "Coloring %d @", range - rstate->lr);
Eric Biedermand1ea5392003-06-28 06:49:45 +000019897 cgdebug_loc(state, range->defs->def);
Eric Biederman5ade04a2003-10-22 04:03:46 +000019898 cgdebug_flush(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019899 colored = select_free_color(state, rstate, range);
Eric Biederman90089602004-05-28 14:11:54 +000019900 if (colored) {
19901 cgdebug_printf(state, " %s\n", arch_reg_str(range->color));
19902 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000019903 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019904 return colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019905}
19906
Eric Biedermana96d6a92003-05-13 20:45:19 +000019907static void verify_colors(struct compile_state *state, struct reg_state *rstate)
19908{
19909 struct live_range *lr;
19910 struct live_range_edge *edge;
19911 struct triple *ins, *first;
19912 char used[MAX_REGISTERS];
Eric Biederman83b991a2003-10-11 06:20:25 +000019913 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019914 ins = first;
19915 do {
19916 if (triple_is_def(state, ins)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019917 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019918 internal_error(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019919 "triple without a live range def");
Eric Biedermana96d6a92003-05-13 20:45:19 +000019920 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019921 lr = rstate->lrd[ins->id].lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019922 if (lr->color == REG_UNSET) {
19923 internal_error(state, ins,
19924 "triple without a color");
19925 }
19926 /* Find the registers used by the edges */
19927 memset(used, 0, sizeof(used));
19928 for(edge = lr->edges; edge; edge = edge->next) {
19929 if (edge->node->color == REG_UNSET) {
19930 internal_error(state, 0,
19931 "live range without a color");
19932 }
19933 reg_fill_used(state, used, edge->node->color);
19934 }
19935 if (used[lr->color]) {
19936 internal_error(state, ins,
19937 "triple with already used color");
19938 }
19939 }
19940 ins = ins->next;
19941 } while(ins != first);
19942}
19943
Eric Biedermanb138ac82003-04-22 18:44:01 +000019944static void color_triples(struct compile_state *state, struct reg_state *rstate)
19945{
Eric Biederman90089602004-05-28 14:11:54 +000019946 struct live_range_def *lrd;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019947 struct live_range *lr;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019948 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000019949 first = state->first;
Eric Biedermana96d6a92003-05-13 20:45:19 +000019950 ins = first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000019951 do {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019952 if ((ins->id < 0) || (ins->id > rstate->defs)) {
Eric Biedermana96d6a92003-05-13 20:45:19 +000019953 internal_error(state, ins,
Eric Biedermanb138ac82003-04-22 18:44:01 +000019954 "triple without a live range");
19955 }
Eric Biederman90089602004-05-28 14:11:54 +000019956 lrd = &rstate->lrd[ins->id];
19957 lr = lrd->lr;
19958 ins->id = lrd->orig_id;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019959 SET_REG(ins->id, lr->color);
Eric Biedermana96d6a92003-05-13 20:45:19 +000019960 ins = ins->next;
19961 } while (ins != first);
Eric Biedermanb138ac82003-04-22 18:44:01 +000019962}
19963
Eric Biedermanb138ac82003-04-22 18:44:01 +000019964static struct live_range *merge_sort_lr(
19965 struct live_range *first, struct live_range *last)
19966{
19967 struct live_range *mid, *join, **join_tail, *pick;
19968 size_t size;
19969 size = (last - first) + 1;
19970 if (size >= 2) {
19971 mid = first + size/2;
19972 first = merge_sort_lr(first, mid -1);
19973 mid = merge_sort_lr(mid, last);
19974
19975 join = 0;
19976 join_tail = &join;
19977 /* merge the two lists */
19978 while(first && mid) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000019979 if ((first->degree < mid->degree) ||
19980 ((first->degree == mid->degree) &&
19981 (first->length < mid->length))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000019982 pick = first;
19983 first = first->group_next;
19984 if (first) {
19985 first->group_prev = 0;
19986 }
19987 }
19988 else {
19989 pick = mid;
19990 mid = mid->group_next;
19991 if (mid) {
19992 mid->group_prev = 0;
19993 }
19994 }
19995 pick->group_next = 0;
19996 pick->group_prev = join_tail;
19997 *join_tail = pick;
19998 join_tail = &pick->group_next;
19999 }
20000 /* Splice the remaining list */
20001 pick = (first)? first : mid;
20002 *join_tail = pick;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020003 if (pick) {
20004 pick->group_prev = join_tail;
20005 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020006 }
20007 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020008 if (!first->defs) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020009 first = 0;
20010 }
20011 join = first;
20012 }
20013 return join;
20014}
20015
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020016static void ids_from_rstate(struct compile_state *state,
20017 struct reg_state *rstate)
20018{
20019 struct triple *ins, *first;
20020 if (!rstate->defs) {
20021 return;
20022 }
20023 /* Display the graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020024 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020025 FILE *fp = state->dbgout;
20026 print_interference_blocks(state, rstate, fp, 0);
Eric Biederman7dea9552004-06-29 05:38:37 +000020027 print_control_flow(state, fp, &state->bb);
Eric Biederman90089602004-05-28 14:11:54 +000020028 fflush(fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020029 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020030 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020031 ins = first;
20032 do {
20033 if (ins->id) {
20034 struct live_range_def *lrd;
20035 lrd = &rstate->lrd[ins->id];
20036 ins->id = lrd->orig_id;
20037 }
20038 ins = ins->next;
20039 } while(ins != first);
20040}
20041
20042static void cleanup_live_edges(struct reg_state *rstate)
20043{
20044 int i;
20045 /* Free the edges on each node */
20046 for(i = 1; i <= rstate->ranges; i++) {
20047 remove_live_edges(rstate, &rstate->lr[i]);
20048 }
20049}
20050
20051static void cleanup_rstate(struct compile_state *state, struct reg_state *rstate)
20052{
20053 cleanup_live_edges(rstate);
20054 xfree(rstate->lrd);
20055 xfree(rstate->lr);
20056
20057 /* Free the variable lifetime information */
20058 if (rstate->blocks) {
Eric Biederman90089602004-05-28 14:11:54 +000020059 free_variable_lifetimes(state, &state->bb, rstate->blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020060 }
20061 rstate->defs = 0;
20062 rstate->ranges = 0;
20063 rstate->lrd = 0;
20064 rstate->lr = 0;
20065 rstate->blocks = 0;
20066}
20067
Eric Biederman153ea352003-06-20 14:43:20 +000020068static void verify_consistency(struct compile_state *state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020069static void allocate_registers(struct compile_state *state)
20070{
20071 struct reg_state rstate;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020072 int colored;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020073
20074 /* Clear out the reg_state */
20075 memset(&rstate, 0, sizeof(rstate));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020076 rstate.max_passes = state->compiler->max_allocation_passes;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020077
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020078 do {
20079 struct live_range **point, **next;
Eric Biedermand1ea5392003-06-28 06:49:45 +000020080 int conflicts;
Eric Biederman153ea352003-06-20 14:43:20 +000020081 int tangles;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020082 int coalesced;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020083
Eric Biederman5ade04a2003-10-22 04:03:46 +000020084 if (state->compiler->debug & DEBUG_RANGE_CONFLICTS) {
Eric Biederman90089602004-05-28 14:11:54 +000020085 FILE *fp = state->errout;
20086 fprintf(fp, "pass: %d\n", rstate.passes);
20087 fflush(fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020088 }
Eric Biederman153ea352003-06-20 14:43:20 +000020089
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020090 /* Restore ids */
20091 ids_from_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020092
Eric Biedermanf96a8102003-06-16 16:57:34 +000020093 /* Cleanup the temporary data structures */
20094 cleanup_rstate(state, &rstate);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020095
Eric Biedermanf96a8102003-06-16 16:57:34 +000020096 /* Compute the variable lifetimes */
Eric Biederman90089602004-05-28 14:11:54 +000020097 rstate.blocks = compute_variable_lifetimes(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020098
Eric Biedermanf96a8102003-06-16 16:57:34 +000020099 /* Fix invalid mandatory live range coalesce conflicts */
Eric Biedermand1ea5392003-06-28 06:49:45 +000020100 conflicts = correct_coalesce_conflicts(state, rstate.blocks);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020101
Eric Biederman153ea352003-06-20 14:43:20 +000020102 /* Fix two simultaneous uses of the same register.
20103 * In a few pathlogical cases a partial untangle moves
20104 * the tangle to a part of the graph we won't revisit.
20105 * So we keep looping until we have no more tangle fixes
20106 * to apply.
20107 */
20108 do {
20109 tangles = correct_tangles(state, rstate.blocks);
20110 } while(tangles);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020111
Eric Biederman5ade04a2003-10-22 04:03:46 +000020112
Eric Biederman90089602004-05-28 14:11:54 +000020113 print_blocks(state, "resolve_tangles", state->dbgout);
Eric Biederman153ea352003-06-20 14:43:20 +000020114 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020115
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020116 /* Allocate and initialize the live ranges */
20117 initialize_live_ranges(state, &rstate);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020118
Eric Biederman90089602004-05-28 14:11:54 +000020119 /* Note currently doing coalescing in a loop appears to
Eric Biederman153ea352003-06-20 14:43:20 +000020120 * buys me nothing. The code is left this way in case
20121 * there is some value in it. Or if a future bugfix
Eric Biederman90089602004-05-28 14:11:54 +000020122 * yields some benefit.
Eric Biederman153ea352003-06-20 14:43:20 +000020123 */
20124 do {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020125 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020126 fprintf(state->errout, "coalescing\n");
Eric Biederman5ade04a2003-10-22 04:03:46 +000020127 }
20128
Eric Biederman153ea352003-06-20 14:43:20 +000020129 /* Remove any previous live edge calculations */
20130 cleanup_live_edges(&rstate);
20131
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020132 /* Compute the interference graph */
20133 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020134 state, &state->bb, rstate.blocks,
20135 graph_ins, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020136
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020137 /* Display the interference graph if desired */
Eric Biederman5ade04a2003-10-22 04:03:46 +000020138 if (state->compiler->debug & DEBUG_INTERFERENCE) {
Eric Biederman90089602004-05-28 14:11:54 +000020139 print_interference_blocks(state, &rstate, state->dbgout, 1);
Eric Biederman7dea9552004-06-29 05:38:37 +000020140 fprintf(state->dbgout, "\nlive variables by instruction\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020141 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020142 state, &state->bb, rstate.blocks,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020143 print_interference_ins, &rstate);
20144 }
20145
20146 coalesced = coalesce_live_ranges(state, &rstate);
Eric Biederman153ea352003-06-20 14:43:20 +000020147
Eric Biederman5ade04a2003-10-22 04:03:46 +000020148 if (state->compiler->debug & DEBUG_COALESCING) {
Eric Biederman90089602004-05-28 14:11:54 +000020149 fprintf(state->errout, "coalesced: %d\n", coalesced);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020150 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020151 } while(coalesced);
Eric Biederman153ea352003-06-20 14:43:20 +000020152
Eric Biederman83b991a2003-10-11 06:20:25 +000020153#if DEBUG_CONSISTENCY > 1
20154# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020155 fprintf(state->errout, "verify_graph_ins...\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020156# endif
Eric Biederman153ea352003-06-20 14:43:20 +000020157 /* Verify the interference graph */
Eric Biederman83b991a2003-10-11 06:20:25 +000020158 walk_variable_lifetimes(
Eric Biederman90089602004-05-28 14:11:54 +000020159 state, &state->bb, rstate.blocks,
20160 verify_graph_ins, &rstate);
Eric Biederman83b991a2003-10-11 06:20:25 +000020161# if 0
Eric Biederman90089602004-05-28 14:11:54 +000020162 fprintf(state->errout, "verify_graph_ins done\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000020163#endif
20164#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020165
20166 /* Build the groups low and high. But with the nodes
20167 * first sorted by degree order.
20168 */
20169 rstate.low_tail = &rstate.low;
20170 rstate.high_tail = &rstate.high;
20171 rstate.high = merge_sort_lr(&rstate.lr[1], &rstate.lr[rstate.ranges]);
20172 if (rstate.high) {
20173 rstate.high->group_prev = &rstate.high;
20174 }
20175 for(point = &rstate.high; *point; point = &(*point)->group_next)
20176 ;
20177 rstate.high_tail = point;
20178 /* Walk through the high list and move everything that needs
20179 * to be onto low.
20180 */
20181 for(point = &rstate.high; *point; point = next) {
20182 struct live_range *range;
20183 next = &(*point)->group_next;
20184 range = *point;
20185
20186 /* If it has a low degree or it already has a color
20187 * place the node in low.
20188 */
20189 if ((range->degree < regc_max_size(state, range->classes)) ||
20190 (range->color != REG_UNSET)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020191 cgdebug_printf(state, "Lo: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020192 range - rstate.lr, range->degree,
20193 (range->color != REG_UNSET) ? " (colored)": "");
20194 *range->group_prev = range->group_next;
20195 if (range->group_next) {
20196 range->group_next->group_prev = range->group_prev;
20197 }
20198 if (&range->group_next == rstate.high_tail) {
20199 rstate.high_tail = range->group_prev;
20200 }
20201 range->group_prev = rstate.low_tail;
20202 range->group_next = 0;
20203 *rstate.low_tail = range;
20204 rstate.low_tail = &range->group_next;
20205 next = point;
20206 }
20207 else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020208 cgdebug_printf(state, "hi: %5d degree %5d%s\n",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020209 range - rstate.lr, range->degree,
20210 (range->color != REG_UNSET) ? " (colored)": "");
20211 }
20212 }
20213 /* Color the live_ranges */
20214 colored = color_graph(state, &rstate);
20215 rstate.passes++;
20216 } while (!colored);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020217
Eric Biedermana96d6a92003-05-13 20:45:19 +000020218 /* Verify the graph was properly colored */
20219 verify_colors(state, &rstate);
20220
Eric Biedermanb138ac82003-04-22 18:44:01 +000020221 /* Move the colors from the graph to the triples */
20222 color_triples(state, &rstate);
20223
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020224 /* Cleanup the temporary data structures */
20225 cleanup_rstate(state, &rstate);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020226
20227 /* Display the new graph */
Eric Biederman90089602004-05-28 14:11:54 +000020228 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020229}
20230
20231/* Sparce Conditional Constant Propogation
20232 * =========================================
20233 */
20234struct ssa_edge;
20235struct flow_block;
20236struct lattice_node {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020237 unsigned old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020238 struct triple *def;
20239 struct ssa_edge *out;
20240 struct flow_block *fblock;
20241 struct triple *val;
Eric Biederman90089602004-05-28 14:11:54 +000020242 /* lattice high val == def
Eric Biedermanb138ac82003-04-22 18:44:01 +000020243 * lattice const is_const(val)
Eric Biederman90089602004-05-28 14:11:54 +000020244 * lattice low other
Eric Biedermanb138ac82003-04-22 18:44:01 +000020245 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020246};
20247struct ssa_edge {
20248 struct lattice_node *src;
20249 struct lattice_node *dst;
20250 struct ssa_edge *work_next;
20251 struct ssa_edge *work_prev;
20252 struct ssa_edge *out_next;
20253};
20254struct flow_edge {
20255 struct flow_block *src;
20256 struct flow_block *dst;
20257 struct flow_edge *work_next;
20258 struct flow_edge *work_prev;
20259 struct flow_edge *in_next;
20260 struct flow_edge *out_next;
20261 int executable;
20262};
Eric Biederman5ade04a2003-10-22 04:03:46 +000020263#define MAX_FLOW_BLOCK_EDGES 3
Eric Biedermanb138ac82003-04-22 18:44:01 +000020264struct flow_block {
20265 struct block *block;
20266 struct flow_edge *in;
20267 struct flow_edge *out;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020268 struct flow_edge *edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020269};
20270
20271struct scc_state {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020272 int ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020273 struct lattice_node *lattice;
20274 struct ssa_edge *ssa_edges;
20275 struct flow_block *flow_blocks;
20276 struct flow_edge *flow_work_list;
20277 struct ssa_edge *ssa_work_list;
20278};
20279
20280
Eric Biederman90089602004-05-28 14:11:54 +000020281static int is_scc_const(struct compile_state *state, struct triple *ins)
20282{
20283 return ins && (triple_is_ubranch(state, ins) || is_const(ins));
20284}
20285
20286static int is_lattice_hi(struct compile_state *state, struct lattice_node *lnode)
20287{
20288 return !is_scc_const(state, lnode->val) && (lnode->val == lnode->def);
20289}
20290
20291static int is_lattice_const(struct compile_state *state, struct lattice_node *lnode)
20292{
20293 return is_scc_const(state, lnode->val);
20294}
20295
20296static int is_lattice_lo(struct compile_state *state, struct lattice_node *lnode)
20297{
20298 return (lnode->val != lnode->def) && !is_scc_const(state, lnode->val);
20299}
20300
Eric Biedermanb138ac82003-04-22 18:44:01 +000020301static void scc_add_fedge(struct compile_state *state, struct scc_state *scc,
20302 struct flow_edge *fedge)
20303{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020304 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020305 fprintf(state->errout, "adding fedge: %p (%4d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020306 fedge,
20307 fedge->src->block?fedge->src->block->last->id: 0,
20308 fedge->dst->block?fedge->dst->block->first->id: 0);
20309 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020310 if ((fedge == scc->flow_work_list) ||
20311 (fedge->work_next != fedge) ||
20312 (fedge->work_prev != fedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020313
20314 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Eric Biederman90089602004-05-28 14:11:54 +000020315 fprintf(state->errout, "dupped fedge: %p\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020316 fedge);
20317 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020318 return;
20319 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020320 if (!scc->flow_work_list) {
20321 scc->flow_work_list = fedge;
20322 fedge->work_next = fedge->work_prev = fedge;
20323 }
20324 else {
20325 struct flow_edge *ftail;
20326 ftail = scc->flow_work_list->work_prev;
20327 fedge->work_next = ftail->work_next;
20328 fedge->work_prev = ftail;
20329 fedge->work_next->work_prev = fedge;
20330 fedge->work_prev->work_next = fedge;
20331 }
20332}
20333
20334static struct flow_edge *scc_next_fedge(
20335 struct compile_state *state, struct scc_state *scc)
20336{
20337 struct flow_edge *fedge;
20338 fedge = scc->flow_work_list;
20339 if (fedge) {
20340 fedge->work_next->work_prev = fedge->work_prev;
20341 fedge->work_prev->work_next = fedge->work_next;
20342 if (fedge->work_next != fedge) {
20343 scc->flow_work_list = fedge->work_next;
20344 } else {
20345 scc->flow_work_list = 0;
20346 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020347 fedge->work_next = fedge->work_prev = fedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020348 }
20349 return fedge;
20350}
20351
20352static void scc_add_sedge(struct compile_state *state, struct scc_state *scc,
20353 struct ssa_edge *sedge)
20354{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020355 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020356 fprintf(state->errout, "adding sedge: %5ld (%4d -> %5d)\n",
20357 (long)(sedge - scc->ssa_edges),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020358 sedge->src->def->id,
20359 sedge->dst->def->id);
20360 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020361 if ((sedge == scc->ssa_work_list) ||
20362 (sedge->work_next != sedge) ||
20363 (sedge->work_prev != sedge)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020364
20365 if (state->compiler->debug & DEBUG_SCC_TRANSFORM2) {
Stefan Reinauerbe84b012009-04-04 13:20:33 +000020366 fprintf(state->errout, "dupped sedge: %5ld\n",
20367 (long)(sedge - scc->ssa_edges));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020368 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020369 return;
20370 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020371 if (!scc->ssa_work_list) {
20372 scc->ssa_work_list = sedge;
20373 sedge->work_next = sedge->work_prev = sedge;
20374 }
20375 else {
20376 struct ssa_edge *stail;
20377 stail = scc->ssa_work_list->work_prev;
20378 sedge->work_next = stail->work_next;
20379 sedge->work_prev = stail;
20380 sedge->work_next->work_prev = sedge;
20381 sedge->work_prev->work_next = sedge;
20382 }
20383}
20384
20385static struct ssa_edge *scc_next_sedge(
20386 struct compile_state *state, struct scc_state *scc)
20387{
20388 struct ssa_edge *sedge;
20389 sedge = scc->ssa_work_list;
20390 if (sedge) {
20391 sedge->work_next->work_prev = sedge->work_prev;
20392 sedge->work_prev->work_next = sedge->work_next;
20393 if (sedge->work_next != sedge) {
20394 scc->ssa_work_list = sedge->work_next;
20395 } else {
20396 scc->ssa_work_list = 0;
20397 }
Eric Biederman83b991a2003-10-11 06:20:25 +000020398 sedge->work_next = sedge->work_prev = sedge;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020399 }
20400 return sedge;
20401}
20402
20403static void initialize_scc_state(
20404 struct compile_state *state, struct scc_state *scc)
20405{
20406 int ins_count, ssa_edge_count;
20407 int ins_index, ssa_edge_index, fblock_index;
20408 struct triple *first, *ins;
20409 struct block *block;
20410 struct flow_block *fblock;
20411
20412 memset(scc, 0, sizeof(*scc));
20413
20414 /* Inialize pass zero find out how much memory we need */
Eric Biederman83b991a2003-10-11 06:20:25 +000020415 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020416 ins = first;
20417 ins_count = ssa_edge_count = 0;
20418 do {
20419 struct triple_set *edge;
20420 ins_count += 1;
20421 for(edge = ins->use; edge; edge = edge->next) {
20422 ssa_edge_count++;
20423 }
20424 ins = ins->next;
20425 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020426 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020427 fprintf(state->errout, "ins_count: %d ssa_edge_count: %d vertex_count: %d\n",
20428 ins_count, ssa_edge_count, state->bb.last_vertex);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020429 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020430 scc->ins_count = ins_count;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020431 scc->lattice =
20432 xcmalloc(sizeof(*scc->lattice)*(ins_count + 1), "lattice");
20433 scc->ssa_edges =
20434 xcmalloc(sizeof(*scc->ssa_edges)*(ssa_edge_count + 1), "ssa_edges");
20435 scc->flow_blocks =
Eric Biederman90089602004-05-28 14:11:54 +000020436 xcmalloc(sizeof(*scc->flow_blocks)*(state->bb.last_vertex + 1),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020437 "flow_blocks");
20438
20439 /* Initialize pass one collect up the nodes */
20440 fblock = 0;
20441 block = 0;
20442 ins_index = ssa_edge_index = fblock_index = 0;
20443 ins = first;
20444 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020445 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20446 block = ins->u.block;
20447 if (!block) {
20448 internal_error(state, ins, "label without block");
20449 }
20450 fblock_index += 1;
20451 block->vertex = fblock_index;
20452 fblock = &scc->flow_blocks[fblock_index];
20453 fblock->block = block;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020454 fblock->edges = xcmalloc(sizeof(*fblock->edges)*block->edge_count,
20455 "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020456 }
20457 {
20458 struct lattice_node *lnode;
20459 ins_index += 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020460 lnode = &scc->lattice[ins_index];
20461 lnode->def = ins;
20462 lnode->out = 0;
20463 lnode->fblock = fblock;
20464 lnode->val = ins; /* LATTICE HIGH */
Eric Biederman90089602004-05-28 14:11:54 +000020465 if (lnode->val->op == OP_UNKNOWNVAL) {
20466 lnode->val = 0; /* LATTICE LOW by definition */
20467 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020468 lnode->old_id = ins->id;
20469 ins->id = ins_index;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020470 }
20471 ins = ins->next;
20472 } while(ins != first);
20473 /* Initialize pass two collect up the edges */
20474 block = 0;
20475 fblock = 0;
20476 ins = first;
20477 do {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020478 {
20479 struct triple_set *edge;
20480 struct ssa_edge **stail;
20481 struct lattice_node *lnode;
20482 lnode = &scc->lattice[ins->id];
20483 lnode->out = 0;
20484 stail = &lnode->out;
20485 for(edge = ins->use; edge; edge = edge->next) {
20486 struct ssa_edge *sedge;
20487 ssa_edge_index += 1;
20488 sedge = &scc->ssa_edges[ssa_edge_index];
20489 *stail = sedge;
20490 stail = &sedge->out_next;
20491 sedge->src = lnode;
20492 sedge->dst = &scc->lattice[edge->member->id];
20493 sedge->work_next = sedge->work_prev = sedge;
20494 sedge->out_next = 0;
20495 }
20496 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020497 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20498 struct flow_edge *fedge, **ftail;
20499 struct block_set *bedge;
20500 block = ins->u.block;
20501 fblock = &scc->flow_blocks[block->vertex];
20502 fblock->in = 0;
20503 fblock->out = 0;
20504 ftail = &fblock->out;
20505
20506 fedge = fblock->edges;
20507 bedge = block->edges;
20508 for(; bedge; bedge = bedge->next, fedge++) {
20509 fedge->dst = &scc->flow_blocks[bedge->member->vertex];
20510 if (fedge->dst->block != bedge->member) {
20511 internal_error(state, 0, "block mismatch");
20512 }
20513 *ftail = fedge;
20514 ftail = &fedge->out_next;
20515 fedge->out_next = 0;
20516 }
20517 for(fedge = fblock->out; fedge; fedge = fedge->out_next) {
20518 fedge->src = fblock;
20519 fedge->work_next = fedge->work_prev = fedge;
20520 fedge->executable = 0;
20521 }
20522 }
20523 ins = ins->next;
20524 } while (ins != first);
20525 block = 0;
20526 fblock = 0;
20527 ins = first;
20528 do {
20529 if ((ins->op == OP_LABEL) && (block != ins->u.block)) {
20530 struct flow_edge **ftail;
20531 struct block_set *bedge;
20532 block = ins->u.block;
20533 fblock = &scc->flow_blocks[block->vertex];
20534 ftail = &fblock->in;
20535 for(bedge = block->use; bedge; bedge = bedge->next) {
20536 struct block *src_block;
20537 struct flow_block *sfblock;
20538 struct flow_edge *sfedge;
20539 src_block = bedge->member;
20540 sfblock = &scc->flow_blocks[src_block->vertex];
20541 for(sfedge = sfblock->out; sfedge; sfedge = sfedge->out_next) {
20542 if (sfedge->dst == fblock) {
20543 break;
20544 }
20545 }
20546 if (!sfedge) {
20547 internal_error(state, 0, "edge mismatch");
20548 }
20549 *ftail = sfedge;
20550 ftail = &sfedge->in_next;
20551 sfedge->in_next = 0;
20552 }
20553 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020554 ins = ins->next;
20555 } while(ins != first);
20556 /* Setup a dummy block 0 as a node above the start node */
20557 {
20558 struct flow_block *fblock, *dst;
20559 struct flow_edge *fedge;
20560 fblock = &scc->flow_blocks[0];
20561 fblock->block = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020562 fblock->edges = xcmalloc(sizeof(*fblock->edges)*1, "flow_edges");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020563 fblock->in = 0;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020564 fblock->out = fblock->edges;
Eric Biederman90089602004-05-28 14:11:54 +000020565 dst = &scc->flow_blocks[state->bb.first_block->vertex];
Eric Biederman5ade04a2003-10-22 04:03:46 +000020566 fedge = fblock->edges;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020567 fedge->src = fblock;
20568 fedge->dst = dst;
20569 fedge->work_next = fedge;
20570 fedge->work_prev = fedge;
20571 fedge->in_next = fedge->dst->in;
20572 fedge->out_next = 0;
20573 fedge->executable = 0;
20574 fedge->dst->in = fedge;
20575
20576 /* Initialize the work lists */
20577 scc->flow_work_list = 0;
20578 scc->ssa_work_list = 0;
20579 scc_add_fedge(state, scc, fedge);
20580 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020581 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020582 fprintf(state->errout, "ins_index: %d ssa_edge_index: %d fblock_index: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020583 ins_index, ssa_edge_index, fblock_index);
20584 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020585}
20586
20587
20588static void free_scc_state(
20589 struct compile_state *state, struct scc_state *scc)
20590{
Eric Biederman5ade04a2003-10-22 04:03:46 +000020591 int i;
Eric Biederman90089602004-05-28 14:11:54 +000020592 for(i = 0; i < state->bb.last_vertex + 1; i++) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020593 struct flow_block *fblock;
20594 fblock = &scc->flow_blocks[i];
20595 if (fblock->edges) {
20596 xfree(fblock->edges);
20597 fblock->edges = 0;
20598 }
20599 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020600 xfree(scc->flow_blocks);
20601 xfree(scc->ssa_edges);
20602 xfree(scc->lattice);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020603
Eric Biedermanb138ac82003-04-22 18:44:01 +000020604}
20605
20606static struct lattice_node *triple_to_lattice(
20607 struct compile_state *state, struct scc_state *scc, struct triple *ins)
20608{
20609 if (ins->id <= 0) {
20610 internal_error(state, ins, "bad id");
20611 }
20612 return &scc->lattice[ins->id];
20613}
20614
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020615static struct triple *preserve_lval(
20616 struct compile_state *state, struct lattice_node *lnode)
20617{
20618 struct triple *old;
20619 /* Preserve the original value */
20620 if (lnode->val) {
20621 old = dup_triple(state, lnode->val);
20622 if (lnode->val != lnode->def) {
20623 xfree(lnode->val);
20624 }
20625 lnode->val = 0;
20626 } else {
20627 old = 0;
20628 }
20629 return old;
20630}
20631
20632static int lval_changed(struct compile_state *state,
20633 struct triple *old, struct lattice_node *lnode)
20634{
20635 int changed;
20636 /* See if the lattice value has changed */
20637 changed = 1;
20638 if (!old && !lnode->val) {
20639 changed = 0;
20640 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020641 if (changed &&
20642 lnode->val && old &&
20643 (memcmp(lnode->val->param, old->param,
Eric Biederman90089602004-05-28 14:11:54 +000020644 TRIPLE_SIZE(lnode->val) * sizeof(lnode->val->param[0])) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020645 (memcmp(&lnode->val->u, &old->u, sizeof(old->u)) == 0)) {
20646 changed = 0;
20647 }
20648 if (old) {
20649 xfree(old);
20650 }
20651 return changed;
20652
20653}
20654
Eric Biederman5ade04a2003-10-22 04:03:46 +000020655static void scc_debug_lnode(
Eric Biederman90089602004-05-28 14:11:54 +000020656 struct compile_state *state, struct scc_state *scc,
20657 struct lattice_node *lnode, int changed)
Eric Biederman5ade04a2003-10-22 04:03:46 +000020658{
Eric Biederman90089602004-05-28 14:11:54 +000020659 if ((state->compiler->debug & DEBUG_SCC_TRANSFORM2) && lnode->val) {
20660 display_triple_changes(state->errout, lnode->val, lnode->def);
20661 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020662 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020663 FILE *fp = state->errout;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020664 struct triple *val, **expr;
20665 val = lnode->val? lnode->val : lnode->def;
20666 fprintf(fp, "%p %s %3d %10s (",
20667 lnode->def,
20668 ((lnode->def->op == OP_PHI)? "phi: ": "expr:"),
20669 lnode->def->id,
20670 tops(lnode->def->op));
20671 expr = triple_rhs(state, lnode->def, 0);
20672 for(;expr;expr = triple_rhs(state, lnode->def, expr)) {
20673 if (*expr) {
20674 fprintf(fp, " %d", (*expr)->id);
20675 }
20676 }
20677 if (val->op == OP_INTCONST) {
20678 fprintf(fp, " <0x%08lx>", (unsigned long)(val->u.cval));
20679 }
20680 fprintf(fp, " ) -> %s %s\n",
Eric Biederman90089602004-05-28 14:11:54 +000020681 (is_lattice_hi(state, lnode)? "hi":
20682 is_lattice_const(state, lnode)? "const" : "lo"),
Eric Biederman5ade04a2003-10-22 04:03:46 +000020683 changed? "changed" : ""
20684 );
20685 }
20686}
20687
Eric Biedermanb138ac82003-04-22 18:44:01 +000020688static int compute_lnode_val(struct compile_state *state, struct scc_state *scc,
20689 struct lattice_node *lnode)
20690{
20691 int changed;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020692 struct triple *old, *scratch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020693 struct triple **dexpr, **vexpr;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020694 int count, i;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020695
20696 /* Store the original value */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020697 old = preserve_lval(state, lnode);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020698
Eric Biedermanb138ac82003-04-22 18:44:01 +000020699 /* Reinitialize the value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020700 lnode->val = scratch = dup_triple(state, lnode->def);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020701 scratch->id = lnode->old_id;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020702 scratch->next = scratch;
20703 scratch->prev = scratch;
20704 scratch->use = 0;
20705
Eric Biederman90089602004-05-28 14:11:54 +000020706 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020707 for(i = 0; i < count; i++) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020708 dexpr = &lnode->def->param[i];
20709 vexpr = &scratch->param[i];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020710 *vexpr = *dexpr;
Eric Biederman90089602004-05-28 14:11:54 +000020711 if (((i < TRIPLE_MISC_OFF(scratch)) ||
20712 (i >= TRIPLE_TARG_OFF(scratch))) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020713 *dexpr) {
20714 struct lattice_node *tmp;
Eric Biederman0babc1c2003-05-09 02:39:00 +000020715 tmp = triple_to_lattice(state, scc, *dexpr);
20716 *vexpr = (tmp->val)? tmp->val : tmp->def;
20717 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020718 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020719 if (triple_is_branch(state, scratch)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000020720 scratch->next = lnode->def->next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020721 }
20722 /* Recompute the value */
Stefan Reinauer50542a82007-10-24 11:14:14 +000020723#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000020724#warning "FIXME see if simplify does anything bad"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020725#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000020726 /* So far it looks like only the strength reduction
20727 * optimization are things I need to worry about.
20728 */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020729 simplify(state, scratch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020730 /* Cleanup my value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020731 if (scratch->use) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020732 internal_error(state, lnode->def, "scratch used?");
20733 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020734 if ((scratch->prev != scratch) ||
20735 ((scratch->next != scratch) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000020736 (!triple_is_branch(state, lnode->def) ||
Eric Biederman0babc1c2003-05-09 02:39:00 +000020737 (scratch->next != lnode->def->next)))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020738 internal_error(state, lnode->def, "scratch in list?");
20739 }
20740 /* undo any uses... */
Eric Biederman90089602004-05-28 14:11:54 +000020741 count = TRIPLE_SIZE(scratch);
Eric Biederman0babc1c2003-05-09 02:39:00 +000020742 for(i = 0; i < count; i++) {
20743 vexpr = &scratch->param[i];
20744 if (*vexpr) {
20745 unuse_triple(*vexpr, scratch);
20746 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020747 }
Eric Biederman90089602004-05-28 14:11:54 +000020748 if (lnode->val->op == OP_UNKNOWNVAL) {
20749 lnode->val = 0; /* Lattice low by definition */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020750 }
Eric Biederman90089602004-05-28 14:11:54 +000020751 /* Find the case when I am lattice high */
Eric Biedermanb138ac82003-04-22 18:44:01 +000020752 if (lnode->val &&
20753 (lnode->val->op == lnode->def->op) &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020754 (memcmp(lnode->val->param, lnode->def->param,
20755 count * sizeof(lnode->val->param[0])) == 0) &&
20756 (memcmp(&lnode->val->u, &lnode->def->u, sizeof(lnode->def->u)) == 0)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020757 lnode->val = lnode->def;
20758 }
Eric Biederman90089602004-05-28 14:11:54 +000020759 /* Only allow lattice high when all of my inputs
20760 * are also lattice high. Occassionally I can
20761 * have constants with a lattice low input, so
20762 * I do not need to check that case.
20763 */
20764 if (is_lattice_hi(state, lnode)) {
20765 struct lattice_node *tmp;
20766 int rhs;
20767 rhs = lnode->val->rhs;
20768 for(i = 0; i < rhs; i++) {
20769 tmp = triple_to_lattice(state, scc, RHS(lnode->val, i));
20770 if (!is_lattice_hi(state, tmp)) {
20771 lnode->val = 0;
20772 break;
20773 }
20774 }
20775 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020776 /* Find the cases that are always lattice lo */
20777 if (lnode->val &&
Eric Biederman0babc1c2003-05-09 02:39:00 +000020778 triple_is_def(state, lnode->val) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020779 !triple_is_pure(state, lnode->val, lnode->old_id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020780 lnode->val = 0;
20781 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000020782 /* See if the lattice value has changed */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020783 changed = lval_changed(state, old, lnode);
Eric Biederman83b991a2003-10-11 06:20:25 +000020784 /* See if this value should not change */
Eric Biederman90089602004-05-28 14:11:54 +000020785 if ((lnode->val != lnode->def) &&
Eric Biederman83b991a2003-10-11 06:20:25 +000020786 (( !triple_is_def(state, lnode->def) &&
Eric Biederman90089602004-05-28 14:11:54 +000020787 !triple_is_cbranch(state, lnode->def)) ||
Eric Biederman83b991a2003-10-11 06:20:25 +000020788 (lnode->def->op == OP_PIECE))) {
Stefan Reinauer50542a82007-10-24 11:14:14 +000020789#if DEBUG_ROMCC_WARNINGS
Eric Biederman83b991a2003-10-11 06:20:25 +000020790#warning "FIXME constant propogate through expressions with multiple left hand sides"
Stefan Reinauer50542a82007-10-24 11:14:14 +000020791#endif
Eric Biederman83b991a2003-10-11 06:20:25 +000020792 if (changed) {
20793 internal_warning(state, lnode->def, "non def changes value?");
20794 }
20795 lnode->val = 0;
20796 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000020797
Eric Biederman83b991a2003-10-11 06:20:25 +000020798 /* See if we need to free the scratch value */
Eric Biederman0babc1c2003-05-09 02:39:00 +000020799 if (lnode->val != scratch) {
20800 xfree(scratch);
20801 }
Eric Biederman90089602004-05-28 14:11:54 +000020802
Eric Biedermanb138ac82003-04-22 18:44:01 +000020803 return changed;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020804}
Eric Biederman0babc1c2003-05-09 02:39:00 +000020805
Eric Biederman90089602004-05-28 14:11:54 +000020806
20807static void scc_visit_cbranch(struct compile_state *state, struct scc_state *scc,
Eric Biedermanb138ac82003-04-22 18:44:01 +000020808 struct lattice_node *lnode)
20809{
20810 struct lattice_node *cond;
Eric Biederman5ade04a2003-10-22 04:03:46 +000020811 struct flow_edge *left, *right;
Eric Biederman90089602004-05-28 14:11:54 +000020812 int changed;
20813
20814 /* Update the branch value */
20815 changed = compute_lnode_val(state, scc, lnode);
20816 scc_debug_lnode(state, scc, lnode, changed);
20817
20818 /* This only applies to conditional branches */
20819 if (!triple_is_cbranch(state, lnode->def)) {
20820 internal_error(state, lnode->def, "not a conditional branch");
20821 }
20822
Eric Biederman5ade04a2003-10-22 04:03:46 +000020823 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020824 struct flow_edge *fedge;
Eric Biederman90089602004-05-28 14:11:54 +000020825 FILE *fp = state->errout;
20826 fprintf(fp, "%s: %d (",
Eric Biederman5ade04a2003-10-22 04:03:46 +000020827 tops(lnode->def->op),
Eric Biedermanb138ac82003-04-22 18:44:01 +000020828 lnode->def->id);
20829
20830 for(fedge = lnode->fblock->out; fedge; fedge = fedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020831 fprintf(fp, " %d", fedge->dst->block->vertex);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020832 }
Eric Biederman90089602004-05-28 14:11:54 +000020833 fprintf(fp, " )");
20834 if (lnode->def->rhs > 0) {
20835 fprintf(fp, " <- %d",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000020836 RHS(lnode->def, 0)->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020837 }
Eric Biederman90089602004-05-28 14:11:54 +000020838 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020839 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000020840 cond = triple_to_lattice(state, scc, RHS(lnode->def,0));
Eric Biederman5ade04a2003-10-22 04:03:46 +000020841 for(left = cond->fblock->out; left; left = left->out_next) {
20842 if (left->dst->block->first == lnode->def->next) {
20843 break;
20844 }
20845 }
20846 if (!left) {
20847 internal_error(state, lnode->def, "Cannot find left branch edge");
20848 }
20849 for(right = cond->fblock->out; right; right = right->out_next) {
20850 if (right->dst->block->first == TARG(lnode->def, 0)) {
20851 break;
20852 }
20853 }
20854 if (!right) {
20855 internal_error(state, lnode->def, "Cannot find right branch edge");
20856 }
Eric Biederman90089602004-05-28 14:11:54 +000020857 /* I should only come here if the controlling expressions value
20858 * has changed, which means it must be either a constant or lo.
20859 */
20860 if (is_lattice_hi(state, cond)) {
20861 internal_error(state, cond->def, "condition high?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020862 return;
20863 }
Eric Biederman90089602004-05-28 14:11:54 +000020864 if (is_lattice_lo(state, cond)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020865 scc_add_fedge(state, scc, left);
20866 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020867 }
20868 else if (cond->val->u.cval) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020869 scc_add_fedge(state, scc, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020870 } else {
Eric Biederman5ade04a2003-10-22 04:03:46 +000020871 scc_add_fedge(state, scc, left);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020872 }
20873
20874}
20875
Eric Biederman90089602004-05-28 14:11:54 +000020876
20877static void scc_add_sedge_dst(struct compile_state *state,
20878 struct scc_state *scc, struct ssa_edge *sedge)
20879{
Eric Biederman5a78bd02004-05-29 21:26:03 +000020880 if (triple_is_cbranch(state, sedge->dst->def)) {
Eric Biederman90089602004-05-28 14:11:54 +000020881 scc_visit_cbranch(state, scc, sedge->dst);
20882 }
20883 else if (triple_is_def(state, sedge->dst->def)) {
20884 scc_add_sedge(state, scc, sedge);
20885 }
20886}
20887
20888static void scc_visit_phi(struct compile_state *state, struct scc_state *scc,
20889 struct lattice_node *lnode)
20890{
20891 struct lattice_node *tmp;
20892 struct triple **slot, *old;
20893 struct flow_edge *fedge;
20894 int changed;
20895 int index;
20896 if (lnode->def->op != OP_PHI) {
20897 internal_error(state, lnode->def, "not phi");
20898 }
20899 /* Store the original value */
20900 old = preserve_lval(state, lnode);
20901
20902 /* default to lattice high */
20903 lnode->val = lnode->def;
20904 slot = &RHS(lnode->def, 0);
20905 index = 0;
20906 for(fedge = lnode->fblock->in; fedge; index++, fedge = fedge->in_next) {
20907 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
20908 fprintf(state->errout, "Examining edge: %d vertex: %d executable: %d\n",
20909 index,
20910 fedge->dst->block->vertex,
20911 fedge->executable
20912 );
20913 }
20914 if (!fedge->executable) {
20915 continue;
20916 }
20917 if (!slot[index]) {
20918 internal_error(state, lnode->def, "no phi value");
20919 }
20920 tmp = triple_to_lattice(state, scc, slot[index]);
20921 /* meet(X, lattice low) = lattice low */
20922 if (is_lattice_lo(state, tmp)) {
20923 lnode->val = 0;
20924 }
20925 /* meet(X, lattice high) = X */
20926 else if (is_lattice_hi(state, tmp)) {
20927 lnode->val = lnode->val;
20928 }
20929 /* meet(lattice high, X) = X */
20930 else if (is_lattice_hi(state, lnode)) {
20931 lnode->val = dup_triple(state, tmp->val);
20932 /* Only change the type if necessary */
20933 if (!is_subset_type(lnode->def->type, tmp->val->type)) {
20934 lnode->val->type = lnode->def->type;
20935 }
20936 }
20937 /* meet(const, const) = const or lattice low */
20938 else if (!constants_equal(state, lnode->val, tmp->val)) {
20939 lnode->val = 0;
20940 }
20941
20942 /* meet(lattice low, X) = lattice low */
20943 if (is_lattice_lo(state, lnode)) {
20944 lnode->val = 0;
20945 break;
20946 }
20947 }
20948 changed = lval_changed(state, old, lnode);
20949 scc_debug_lnode(state, scc, lnode, changed);
20950
20951 /* If the lattice value has changed update the work lists. */
20952 if (changed) {
20953 struct ssa_edge *sedge;
20954 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
20955 scc_add_sedge_dst(state, scc, sedge);
20956 }
20957 }
20958}
20959
20960
Eric Biedermanb138ac82003-04-22 18:44:01 +000020961static void scc_visit_expr(struct compile_state *state, struct scc_state *scc,
20962 struct lattice_node *lnode)
20963{
20964 int changed;
20965
Eric Biederman90089602004-05-28 14:11:54 +000020966 if (!triple_is_def(state, lnode->def)) {
20967 internal_warning(state, lnode->def, "not visiting an expression?");
Eric Biedermanb138ac82003-04-22 18:44:01 +000020968 }
Eric Biederman90089602004-05-28 14:11:54 +000020969 changed = compute_lnode_val(state, scc, lnode);
20970 scc_debug_lnode(state, scc, lnode, changed);
20971
20972 if (changed) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000020973 struct ssa_edge *sedge;
20974 for(sedge = lnode->out; sedge; sedge = sedge->out_next) {
Eric Biederman90089602004-05-28 14:11:54 +000020975 scc_add_sedge_dst(state, scc, sedge);
Eric Biedermanb138ac82003-04-22 18:44:01 +000020976 }
20977 }
20978}
20979
20980static void scc_writeback_values(
20981 struct compile_state *state, struct scc_state *scc)
20982{
20983 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000020984 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000020985 ins = first;
20986 do {
20987 struct lattice_node *lnode;
20988 lnode = triple_to_lattice(state, scc, ins);
Eric Biederman5ade04a2003-10-22 04:03:46 +000020989 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000020990 if (is_lattice_hi(state, lnode) &&
20991 (lnode->val->op != OP_NOOP))
Eric Biederman5ade04a2003-10-22 04:03:46 +000020992 {
20993 struct flow_edge *fedge;
20994 int executable;
20995 executable = 0;
20996 for(fedge = lnode->fblock->in;
20997 !executable && fedge; fedge = fedge->in_next) {
20998 executable |= fedge->executable;
20999 }
21000 if (executable) {
Eric Biederman90089602004-05-28 14:11:54 +000021001 internal_warning(state, lnode->def,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021002 "lattice node %d %s->%s still high?",
21003 ins->id,
21004 tops(lnode->def->op),
21005 tops(lnode->val->op));
21006 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021007 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021008 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021009
Eric Biederman83b991a2003-10-11 06:20:25 +000021010 /* Restore id */
21011 ins->id = lnode->old_id;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021012 if (lnode->val && (lnode->val != ins)) {
21013 /* See if it something I know how to write back */
21014 switch(lnode->val->op) {
21015 case OP_INTCONST:
21016 mkconst(state, ins, lnode->val->u.cval);
21017 break;
21018 case OP_ADDRCONST:
21019 mkaddr_const(state, ins,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021020 MISC(lnode->val, 0), lnode->val->u.cval);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021021 break;
21022 default:
21023 /* By default don't copy the changes,
21024 * recompute them in place instead.
21025 */
21026 simplify(state, ins);
21027 break;
21028 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021029 if (is_const(lnode->val) &&
21030 !constants_equal(state, lnode->val, ins)) {
21031 internal_error(state, 0, "constants not equal");
21032 }
21033 /* Free the lattice nodes */
21034 xfree(lnode->val);
21035 lnode->val = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021036 }
21037 ins = ins->next;
21038 } while(ins != first);
21039}
21040
21041static void scc_transform(struct compile_state *state)
21042{
21043 struct scc_state scc;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021044 if (!(state->compiler->flags & COMPILER_SCC_TRANSFORM)) {
21045 return;
21046 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021047
21048 initialize_scc_state(state, &scc);
21049
21050 while(scc.flow_work_list || scc.ssa_work_list) {
21051 struct flow_edge *fedge;
21052 struct ssa_edge *sedge;
21053 struct flow_edge *fptr;
21054 while((fedge = scc_next_fedge(state, &scc))) {
21055 struct block *block;
21056 struct triple *ptr;
21057 struct flow_block *fblock;
Eric Biederman83b991a2003-10-11 06:20:25 +000021058 int reps;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021059 int done;
21060 if (fedge->executable) {
21061 continue;
21062 }
21063 if (!fedge->dst) {
21064 internal_error(state, 0, "fedge without dst");
21065 }
21066 if (!fedge->src) {
21067 internal_error(state, 0, "fedge without src");
21068 }
21069 fedge->executable = 1;
21070 fblock = fedge->dst;
21071 block = fblock->block;
Eric Biederman83b991a2003-10-11 06:20:25 +000021072 reps = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021073 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21074 if (fptr->executable) {
Eric Biederman83b991a2003-10-11 06:20:25 +000021075 reps++;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021076 }
21077 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021078
Eric Biederman5ade04a2003-10-22 04:03:46 +000021079 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Eric Biederman90089602004-05-28 14:11:54 +000021080 fprintf(state->errout, "vertex: %d reps: %d\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021081 block->vertex, reps);
21082 }
21083
Eric Biedermanb138ac82003-04-22 18:44:01 +000021084 done = 0;
21085 for(ptr = block->first; !done; ptr = ptr->next) {
21086 struct lattice_node *lnode;
21087 done = (ptr == block->last);
21088 lnode = &scc.lattice[ptr->id];
21089 if (ptr->op == OP_PHI) {
21090 scc_visit_phi(state, &scc, lnode);
21091 }
Eric Biederman90089602004-05-28 14:11:54 +000021092 else if ((reps == 1) && triple_is_def(state, ptr))
21093 {
Eric Biedermanb138ac82003-04-22 18:44:01 +000021094 scc_visit_expr(state, &scc, lnode);
21095 }
21096 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021097 /* Add unconditional branch edges */
Eric Biederman90089602004-05-28 14:11:54 +000021098 if (!triple_is_cbranch(state, fblock->block->last)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021099 struct flow_edge *out;
21100 for(out = fblock->out; out; out = out->out_next) {
21101 scc_add_fedge(state, &scc, out);
21102 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021103 }
21104 }
21105 while((sedge = scc_next_sedge(state, &scc))) {
21106 struct lattice_node *lnode;
21107 struct flow_block *fblock;
21108 lnode = sedge->dst;
21109 fblock = lnode->fblock;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021110
21111 if (state->compiler->debug & DEBUG_SCC_TRANSFORM) {
Myles Watsonf9a72602009-11-12 16:20:04 +000021112 fprintf(state->errout, "sedge: %5ld (%5d -> %5d)\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000021113 sedge - scc.ssa_edges,
21114 sedge->src->def->id,
21115 sedge->dst->def->id);
21116 }
21117
Eric Biedermanb138ac82003-04-22 18:44:01 +000021118 if (lnode->def->op == OP_PHI) {
21119 scc_visit_phi(state, &scc, lnode);
21120 }
21121 else {
21122 for(fptr = fblock->in; fptr; fptr = fptr->in_next) {
21123 if (fptr->executable) {
21124 break;
21125 }
21126 }
21127 if (fptr) {
21128 scc_visit_expr(state, &scc, lnode);
21129 }
21130 }
21131 }
21132 }
21133
21134 scc_writeback_values(state, &scc);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021135 free_scc_state(state, &scc);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021136 rebuild_ssa_form(state);
21137
Eric Biederman90089602004-05-28 14:11:54 +000021138 print_blocks(state, __func__, state->dbgout);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021139}
21140
21141
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021142static void transform_to_arch_instructions(struct compile_state *state)
21143{
21144 struct triple *ins, *first;
Eric Biederman83b991a2003-10-11 06:20:25 +000021145 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021146 ins = first;
21147 do {
21148 ins = transform_to_arch_instruction(state, ins);
21149 } while(ins != first);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021150
Eric Biederman90089602004-05-28 14:11:54 +000021151 print_blocks(state, __func__, state->dbgout);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021152}
Eric Biedermanb138ac82003-04-22 18:44:01 +000021153
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021154#if DEBUG_CONSISTENCY
21155static void verify_uses(struct compile_state *state)
21156{
21157 struct triple *first, *ins;
21158 struct triple_set *set;
Eric Biederman83b991a2003-10-11 06:20:25 +000021159 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021160 ins = first;
21161 do {
21162 struct triple **expr;
21163 expr = triple_rhs(state, ins, 0);
21164 for(; expr; expr = triple_rhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021165 struct triple *rhs;
21166 rhs = *expr;
21167 for(set = rhs?rhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021168 if (set->member == ins) {
21169 break;
21170 }
21171 }
21172 if (!set) {
21173 internal_error(state, ins, "rhs not used");
21174 }
21175 }
21176 expr = triple_lhs(state, ins, 0);
21177 for(; expr; expr = triple_lhs(state, ins, expr)) {
Eric Biederman8d9c1232003-06-17 08:42:17 +000021178 struct triple *lhs;
21179 lhs = *expr;
21180 for(set = lhs?lhs->use:0; set; set = set->next) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021181 if (set->member == ins) {
21182 break;
21183 }
21184 }
21185 if (!set) {
21186 internal_error(state, ins, "lhs not used");
21187 }
21188 }
Eric Biederman90089602004-05-28 14:11:54 +000021189 expr = triple_misc(state, ins, 0);
21190 if (ins->op != OP_PHI) {
21191 for(; expr; expr = triple_targ(state, ins, expr)) {
21192 struct triple *misc;
21193 misc = *expr;
21194 for(set = misc?misc->use:0; set; set = set->next) {
21195 if (set->member == ins) {
21196 break;
21197 }
21198 }
21199 if (!set) {
21200 internal_error(state, ins, "misc not used");
21201 }
21202 }
21203 }
21204 if (!triple_is_ret(state, ins)) {
21205 expr = triple_targ(state, ins, 0);
21206 for(; expr; expr = triple_targ(state, ins, expr)) {
21207 struct triple *targ;
21208 targ = *expr;
21209 for(set = targ?targ->use:0; set; set = set->next) {
21210 if (set->member == ins) {
21211 break;
21212 }
21213 }
21214 if (!set) {
21215 internal_error(state, ins, "targ not used");
21216 }
21217 }
21218 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021219 ins = ins->next;
21220 } while(ins != first);
21221
21222}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021223static void verify_blocks_present(struct compile_state *state)
21224{
21225 struct triple *first, *ins;
Eric Biederman90089602004-05-28 14:11:54 +000021226 if (!state->bb.first_block) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000021227 return;
21228 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021229 first = state->first;
Eric Biedermand1ea5392003-06-28 06:49:45 +000021230 ins = first;
21231 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021232 valid_ins(state, ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021233 if (triple_stores_block(state, ins)) {
21234 if (!ins->u.block) {
21235 internal_error(state, ins,
Eric Biederman90089602004-05-28 14:11:54 +000021236 "%p not in a block?", ins);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021237 }
21238 }
21239 ins = ins->next;
21240 } while(ins != first);
21241
21242
21243}
Eric Biederman5ade04a2003-10-22 04:03:46 +000021244
21245static int edge_present(struct compile_state *state, struct block *block, struct triple *edge)
21246{
21247 struct block_set *bedge;
21248 struct block *targ;
21249 targ = block_of_triple(state, edge);
21250 for(bedge = block->edges; bedge; bedge = bedge->next) {
21251 if (bedge->member == targ) {
21252 return 1;
21253 }
21254 }
21255 return 0;
21256}
21257
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021258static void verify_blocks(struct compile_state *state)
21259{
21260 struct triple *ins;
21261 struct block *block;
Eric Biederman530b5192003-07-01 10:05:30 +000021262 int blocks;
Eric Biederman90089602004-05-28 14:11:54 +000021263 block = state->bb.first_block;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021264 if (!block) {
21265 return;
21266 }
Eric Biederman530b5192003-07-01 10:05:30 +000021267 blocks = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021268 do {
Eric Biederman530b5192003-07-01 10:05:30 +000021269 int users;
Eric Biederman5ade04a2003-10-22 04:03:46 +000021270 struct block_set *user, *edge;
Eric Biederman530b5192003-07-01 10:05:30 +000021271 blocks++;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021272 for(ins = block->first; ins != block->last->next; ins = ins->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021273 if (triple_stores_block(state, ins) && (ins->u.block != block)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021274 internal_error(state, ins, "inconsitent block specified");
21275 }
Eric Biederman530b5192003-07-01 10:05:30 +000021276 valid_ins(state, ins);
21277 }
21278 users = 0;
21279 for(user = block->use; user; user = user->next) {
21280 users++;
Eric Biederman83b991a2003-10-11 06:20:25 +000021281 if (!user->member->first) {
21282 internal_error(state, block->first, "user is empty");
21283 }
Eric Biederman90089602004-05-28 14:11:54 +000021284 if ((block == state->bb.last_block) &&
21285 (user->member == state->bb.first_block)) {
Eric Biederman530b5192003-07-01 10:05:30 +000021286 continue;
21287 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021288 for(edge = user->member->edges; edge; edge = edge->next) {
21289 if (edge->member == block) {
21290 break;
21291 }
21292 }
21293 if (!edge) {
Eric Biederman530b5192003-07-01 10:05:30 +000021294 internal_error(state, user->member->first,
21295 "user does not use block");
21296 }
21297 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021298 if (triple_is_branch(state, block->last)) {
21299 struct triple **expr;
Eric Biederman90089602004-05-28 14:11:54 +000021300 expr = triple_edge_targ(state, block->last, 0);
21301 for(;expr; expr = triple_edge_targ(state, block->last, expr)) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000021302 if (*expr && !edge_present(state, block, *expr)) {
21303 internal_error(state, block->last, "no edge to targ");
21304 }
21305 }
Eric Biederman530b5192003-07-01 10:05:30 +000021306 }
Eric Biederman90089602004-05-28 14:11:54 +000021307 if (!triple_is_ubranch(state, block->last) &&
21308 (block != state->bb.last_block) &&
Eric Biederman5ade04a2003-10-22 04:03:46 +000021309 !edge_present(state, block, block->last->next)) {
21310 internal_error(state, block->last, "no edge to block->last->next");
Eric Biederman530b5192003-07-01 10:05:30 +000021311 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021312 for(edge = block->edges; edge; edge = edge->next) {
21313 for(user = edge->member->use; user; user = user->next) {
Eric Biederman530b5192003-07-01 10:05:30 +000021314 if (user->member == block) {
21315 break;
21316 }
21317 }
21318 if (!user || user->member != block) {
21319 internal_error(state, block->first,
Eric Biederman5ade04a2003-10-22 04:03:46 +000021320 "block does not use edge");
Eric Biederman530b5192003-07-01 10:05:30 +000021321 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021322 if (!edge->member->first) {
21323 internal_error(state, block->first, "edge block is empty");
Eric Biederman83b991a2003-10-11 06:20:25 +000021324 }
Eric Biederman530b5192003-07-01 10:05:30 +000021325 }
21326 if (block->users != users) {
21327 internal_error(state, block->first,
Eric Biederman90089602004-05-28 14:11:54 +000021328 "computed users %d != stored users %d",
Eric Biederman530b5192003-07-01 10:05:30 +000021329 users, block->users);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021330 }
21331 if (!triple_stores_block(state, block->last->next)) {
21332 internal_error(state, block->last->next,
21333 "cannot find next block");
21334 }
21335 block = block->last->next->u.block;
21336 if (!block) {
21337 internal_error(state, block->last->next,
21338 "bad next block");
21339 }
Eric Biederman90089602004-05-28 14:11:54 +000021340 } while(block != state->bb.first_block);
21341 if (blocks != state->bb.last_vertex) {
21342 internal_error(state, 0, "computed blocks: %d != stored blocks %d",
21343 blocks, state->bb.last_vertex);
Eric Biederman530b5192003-07-01 10:05:30 +000021344 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021345}
21346
21347static void verify_domination(struct compile_state *state)
21348{
21349 struct triple *first, *ins;
21350 struct triple_set *set;
Eric Biederman90089602004-05-28 14:11:54 +000021351 if (!state->bb.first_block) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021352 return;
21353 }
21354
Eric Biederman83b991a2003-10-11 06:20:25 +000021355 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021356 ins = first;
21357 do {
21358 for(set = ins->use; set; set = set->next) {
Eric Biederman66fe2222003-07-04 15:14:04 +000021359 struct triple **slot;
21360 struct triple *use_point;
21361 int i, zrhs;
21362 use_point = 0;
Eric Biederman90089602004-05-28 14:11:54 +000021363 zrhs = set->member->rhs;
Eric Biederman66fe2222003-07-04 15:14:04 +000021364 slot = &RHS(set->member, 0);
21365 /* See if the use is on the right hand side */
21366 for(i = 0; i < zrhs; i++) {
21367 if (slot[i] == ins) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021368 break;
21369 }
21370 }
Eric Biederman66fe2222003-07-04 15:14:04 +000021371 if (i < zrhs) {
21372 use_point = set->member;
21373 if (set->member->op == OP_PHI) {
21374 struct block_set *bset;
21375 int edge;
21376 bset = set->member->u.block->use;
21377 for(edge = 0; bset && (edge < i); edge++) {
21378 bset = bset->next;
21379 }
21380 if (!bset) {
21381 internal_error(state, set->member,
Eric Biederman90089602004-05-28 14:11:54 +000021382 "no edge for phi rhs %d", i);
Eric Biederman66fe2222003-07-04 15:14:04 +000021383 }
21384 use_point = bset->member->last;
21385 }
21386 }
21387 if (use_point &&
21388 !tdominates(state, ins, use_point)) {
Eric Biederman90089602004-05-28 14:11:54 +000021389 if (is_const(ins)) {
21390 internal_warning(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021391 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021392 }
21393 else {
21394 internal_error(state, ins,
Eric Biederman7dea9552004-06-29 05:38:37 +000021395 "non dominated rhs use point %p?", use_point);
Eric Biederman90089602004-05-28 14:11:54 +000021396 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021397 }
21398 }
21399 ins = ins->next;
21400 } while(ins != first);
21401}
21402
Eric Biederman83b991a2003-10-11 06:20:25 +000021403static void verify_rhs(struct compile_state *state)
21404{
21405 struct triple *first, *ins;
21406 first = state->first;
21407 ins = first;
21408 do {
21409 struct triple **slot;
21410 int zrhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021411 zrhs = ins->rhs;
Eric Biederman83b991a2003-10-11 06:20:25 +000021412 slot = &RHS(ins, 0);
21413 for(i = 0; i < zrhs; i++) {
21414 if (slot[i] == 0) {
21415 internal_error(state, ins,
21416 "missing rhs %d on %s",
21417 i, tops(ins->op));
21418 }
21419 if ((ins->op != OP_PHI) && (slot[i] == ins)) {
21420 internal_error(state, ins,
21421 "ins == rhs[%d] on %s",
21422 i, tops(ins->op));
21423 }
21424 }
21425 ins = ins->next;
21426 } while(ins != first);
21427}
21428
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021429static void verify_piece(struct compile_state *state)
21430{
21431 struct triple *first, *ins;
Eric Biederman83b991a2003-10-11 06:20:25 +000021432 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021433 ins = first;
21434 do {
21435 struct triple *ptr;
21436 int lhs, i;
Eric Biederman90089602004-05-28 14:11:54 +000021437 lhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021438 for(ptr = ins->next, i = 0; i < lhs; i++, ptr = ptr->next) {
21439 if (ptr != LHS(ins, i)) {
21440 internal_error(state, ins, "malformed lhs on %s",
21441 tops(ins->op));
21442 }
21443 if (ptr->op != OP_PIECE) {
21444 internal_error(state, ins, "bad lhs op %s at %d on %s",
21445 tops(ptr->op), i, tops(ins->op));
21446 }
21447 if (ptr->u.cval != i) {
21448 internal_error(state, ins, "bad u.cval of %d %d expected",
21449 ptr->u.cval, i);
21450 }
21451 }
21452 ins = ins->next;
21453 } while(ins != first);
21454}
Eric Biederman83b991a2003-10-11 06:20:25 +000021455
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021456static void verify_ins_colors(struct compile_state *state)
21457{
21458 struct triple *first, *ins;
21459
Eric Biederman83b991a2003-10-11 06:20:25 +000021460 first = state->first;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021461 ins = first;
21462 do {
21463 ins = ins->next;
21464 } while(ins != first);
21465}
Eric Biederman90089602004-05-28 14:11:54 +000021466
21467static void verify_unknown(struct compile_state *state)
21468{
21469 struct triple *first, *ins;
21470 if ( (unknown_triple.next != &unknown_triple) ||
21471 (unknown_triple.prev != &unknown_triple) ||
21472#if 0
21473 (unknown_triple.use != 0) ||
21474#endif
21475 (unknown_triple.op != OP_UNKNOWNVAL) ||
21476 (unknown_triple.lhs != 0) ||
21477 (unknown_triple.rhs != 0) ||
21478 (unknown_triple.misc != 0) ||
21479 (unknown_triple.targ != 0) ||
21480 (unknown_triple.template_id != 0) ||
21481 (unknown_triple.id != -1) ||
21482 (unknown_triple.type != &unknown_type) ||
21483 (unknown_triple.occurance != &dummy_occurance) ||
21484 (unknown_triple.param[0] != 0) ||
21485 (unknown_triple.param[1] != 0)) {
21486 internal_error(state, &unknown_triple, "unknown_triple corrupted!");
21487 }
21488 if ( (dummy_occurance.count != 2) ||
21489 (strcmp(dummy_occurance.filename, __FILE__) != 0) ||
21490 (strcmp(dummy_occurance.function, "") != 0) ||
21491 (dummy_occurance.col != 0) ||
21492 (dummy_occurance.parent != 0)) {
21493 internal_error(state, &unknown_triple, "dummy_occurance corrupted!");
21494 }
21495 if ( (unknown_type.type != TYPE_UNKNOWN)) {
21496 internal_error(state, &unknown_triple, "unknown_type corrupted!");
21497 }
21498 first = state->first;
21499 ins = first;
21500 do {
21501 int params, i;
21502 if (ins == &unknown_triple) {
21503 internal_error(state, ins, "unknown triple in list");
21504 }
21505 params = TRIPLE_SIZE(ins);
21506 for(i = 0; i < params; i++) {
21507 if (ins->param[i] == &unknown_triple) {
21508 internal_error(state, ins, "unknown triple used!");
21509 }
21510 }
21511 ins = ins->next;
21512 } while(ins != first);
21513}
21514
21515static void verify_types(struct compile_state *state)
21516{
21517 struct triple *first, *ins;
21518 first = state->first;
21519 ins = first;
21520 do {
21521 struct type *invalid;
21522 invalid = invalid_type(state, ins->type);
21523 if (invalid) {
21524 FILE *fp = state->errout;
21525 fprintf(fp, "type: ");
21526 name_of(fp, ins->type);
21527 fprintf(fp, "\n");
21528 fprintf(fp, "invalid type: ");
21529 name_of(fp, invalid);
21530 fprintf(fp, "\n");
21531 internal_error(state, ins, "invalid ins type");
21532 }
21533 } while(ins != first);
21534}
21535
21536static void verify_copy(struct compile_state *state)
21537{
21538 struct triple *first, *ins, *next;
21539 first = state->first;
21540 next = ins = first;
21541 do {
21542 ins = next;
21543 next = ins->next;
21544 if (ins->op != OP_COPY) {
21545 continue;
21546 }
21547 if (!equiv_types(ins->type, RHS(ins, 0)->type)) {
21548 FILE *fp = state->errout;
21549 fprintf(fp, "src type: ");
21550 name_of(fp, RHS(ins, 0)->type);
21551 fprintf(fp, "\n");
21552 fprintf(fp, "dst type: ");
21553 name_of(fp, ins->type);
21554 fprintf(fp, "\n");
21555 internal_error(state, ins, "type mismatch in copy");
21556 }
21557 } while(next != first);
21558}
21559
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021560static void verify_consistency(struct compile_state *state)
21561{
Eric Biederman90089602004-05-28 14:11:54 +000021562 verify_unknown(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021563 verify_uses(state);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021564 verify_blocks_present(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021565 verify_blocks(state);
21566 verify_domination(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021567 verify_rhs(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021568 verify_piece(state);
21569 verify_ins_colors(state);
Eric Biederman90089602004-05-28 14:11:54 +000021570 verify_types(state);
21571 verify_copy(state);
21572 if (state->compiler->debug & DEBUG_VERIFICATION) {
21573 fprintf(state->dbgout, "consistency verified\n");
21574 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021575}
21576#else
Eric Biederman153ea352003-06-20 14:43:20 +000021577static void verify_consistency(struct compile_state *state) {}
Eric Biederman83b991a2003-10-11 06:20:25 +000021578#endif /* DEBUG_CONSISTENCY */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021579
21580static void optimize(struct compile_state *state)
21581{
Eric Biederman90089602004-05-28 14:11:54 +000021582 /* Join all of the functions into one giant function */
21583 join_functions(state);
21584
Eric Biederman5ade04a2003-10-22 04:03:46 +000021585 /* Dump what the instruction graph intially looks like */
21586 print_triples(state);
21587
Eric Biederman0babc1c2003-05-09 02:39:00 +000021588 /* Replace structures with simpler data types */
Eric Biederman90089602004-05-28 14:11:54 +000021589 decompose_compound_types(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021590 print_triples(state);
21591
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021592 verify_consistency(state);
Eric Biederman41203d92004-11-08 09:31:09 +000021593 /* Analyze the intermediate code */
Eric Biederman90089602004-05-28 14:11:54 +000021594 state->bb.first = state->first;
21595 analyze_basic_blocks(state, &state->bb);
Eric Biedermand1ea5392003-06-28 06:49:45 +000021596
Eric Biederman530b5192003-07-01 10:05:30 +000021597 /* Transform the code to ssa form. */
21598 /*
21599 * The transformation to ssa form puts a phi function
21600 * on each of edge of a dominance frontier where that
21601 * phi function might be needed. At -O2 if we don't
21602 * eleminate the excess phi functions we can get an
21603 * exponential code size growth. So I kill the extra
21604 * phi functions early and I kill them often.
21605 */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021606 transform_to_ssa_form(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021607 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021608
Eric Biederman83b991a2003-10-11 06:20:25 +000021609 /* Remove dead code */
21610 eliminate_inefectual_code(state);
Eric Biederman83b991a2003-10-11 06:20:25 +000021611 verify_consistency(state);
21612
Eric Biedermanb138ac82003-04-22 18:44:01 +000021613 /* Do strength reduction and simple constant optimizations */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021614 simplify_all(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021615 verify_consistency(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021616 /* Propogate constants throughout the code */
Eric Biederman5ade04a2003-10-22 04:03:46 +000021617 scc_transform(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021618 verify_consistency(state);
Stefan Reinauer50542a82007-10-24 11:14:14 +000021619#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021620#warning "WISHLIST implement single use constants (least possible register pressure)"
21621#warning "WISHLIST implement induction variable elimination"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021622#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021623 /* Select architecture instructions and an initial partial
21624 * coloring based on architecture constraints.
21625 */
21626 transform_to_arch_instructions(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021627 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021628
Eric Biederman83b991a2003-10-11 06:20:25 +000021629 /* Remove dead code */
Eric Biedermanb138ac82003-04-22 18:44:01 +000021630 eliminate_inefectual_code(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021631 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021632
Eric Biedermanb138ac82003-04-22 18:44:01 +000021633 /* Color all of the variables to see if they will fit in registers */
21634 insert_copies_to_phi(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021635 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021636
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021637 insert_mandatory_copies(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021638 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021639
Eric Biedermanb138ac82003-04-22 18:44:01 +000021640 allocate_registers(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021641 verify_consistency(state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000021642
Eric Biedermanb138ac82003-04-22 18:44:01 +000021643 /* Remove the optimization information.
21644 * This is more to check for memory consistency than to free memory.
21645 */
Eric Biederman90089602004-05-28 14:11:54 +000021646 free_basic_blocks(state, &state->bb);
Eric Biedermanb138ac82003-04-22 18:44:01 +000021647}
21648
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021649static void print_op_asm(struct compile_state *state,
21650 struct triple *ins, FILE *fp)
21651{
21652 struct asm_info *info;
21653 const char *ptr;
21654 unsigned lhs, rhs, i;
21655 info = ins->u.ainfo;
Eric Biederman90089602004-05-28 14:11:54 +000021656 lhs = ins->lhs;
21657 rhs = ins->rhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021658 /* Don't count the clobbers in lhs */
21659 for(i = 0; i < lhs; i++) {
21660 if (LHS(ins, i)->type == &void_type) {
21661 break;
21662 }
21663 }
21664 lhs = i;
Eric Biederman8d9c1232003-06-17 08:42:17 +000021665 fprintf(fp, "#ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021666 fputc('\t', fp);
21667 for(ptr = info->str; *ptr; ptr++) {
21668 char *next;
21669 unsigned long param;
21670 struct triple *piece;
21671 if (*ptr != '%') {
21672 fputc(*ptr, fp);
21673 continue;
21674 }
21675 ptr++;
21676 if (*ptr == '%') {
21677 fputc('%', fp);
21678 continue;
21679 }
21680 param = strtoul(ptr, &next, 10);
21681 if (ptr == next) {
21682 error(state, ins, "Invalid asm template");
21683 }
21684 if (param >= (lhs + rhs)) {
21685 error(state, ins, "Invalid param %%%u in asm template",
21686 param);
21687 }
21688 piece = (param < lhs)? LHS(ins, param) : RHS(ins, param - lhs);
21689 fprintf(fp, "%s",
21690 arch_reg_str(ID_REG(piece->id)));
Eric Biederman8d9c1232003-06-17 08:42:17 +000021691 ptr = next -1;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021692 }
Eric Biederman8d9c1232003-06-17 08:42:17 +000021693 fprintf(fp, "\n#NOT ASM\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021694}
21695
21696
21697/* Only use the low x86 byte registers. This allows me
21698 * allocate the entire register when a byte register is used.
21699 */
21700#define X86_4_8BIT_GPRS 1
21701
Eric Biederman83b991a2003-10-11 06:20:25 +000021702/* x86 featrues */
Eric Biederman90089602004-05-28 14:11:54 +000021703#define X86_MMX_REGS (1<<0)
21704#define X86_XMM_REGS (1<<1)
21705#define X86_NOOP_COPY (1<<2)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021706
Eric Biedermanb138ac82003-04-22 18:44:01 +000021707/* The x86 register classes */
Eric Biederman530b5192003-07-01 10:05:30 +000021708#define REGC_FLAGS 0
21709#define REGC_GPR8 1
21710#define REGC_GPR16 2
21711#define REGC_GPR32 3
21712#define REGC_DIVIDEND64 4
21713#define REGC_DIVIDEND32 5
21714#define REGC_MMX 6
21715#define REGC_XMM 7
21716#define REGC_GPR32_8 8
21717#define REGC_GPR16_8 9
21718#define REGC_GPR8_LO 10
21719#define REGC_IMM32 11
21720#define REGC_IMM16 12
21721#define REGC_IMM8 13
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021722#define LAST_REGC REGC_IMM8
Eric Biedermanb138ac82003-04-22 18:44:01 +000021723#if LAST_REGC >= MAX_REGC
21724#error "MAX_REGC is to low"
21725#endif
21726
21727/* Register class masks */
Eric Biederman530b5192003-07-01 10:05:30 +000021728#define REGCM_FLAGS (1 << REGC_FLAGS)
21729#define REGCM_GPR8 (1 << REGC_GPR8)
21730#define REGCM_GPR16 (1 << REGC_GPR16)
21731#define REGCM_GPR32 (1 << REGC_GPR32)
21732#define REGCM_DIVIDEND64 (1 << REGC_DIVIDEND64)
21733#define REGCM_DIVIDEND32 (1 << REGC_DIVIDEND32)
21734#define REGCM_MMX (1 << REGC_MMX)
21735#define REGCM_XMM (1 << REGC_XMM)
21736#define REGCM_GPR32_8 (1 << REGC_GPR32_8)
21737#define REGCM_GPR16_8 (1 << REGC_GPR16_8)
21738#define REGCM_GPR8_LO (1 << REGC_GPR8_LO)
21739#define REGCM_IMM32 (1 << REGC_IMM32)
21740#define REGCM_IMM16 (1 << REGC_IMM16)
21741#define REGCM_IMM8 (1 << REGC_IMM8)
21742#define REGCM_ALL ((1 << (LAST_REGC + 1)) - 1)
Eric Biederman90089602004-05-28 14:11:54 +000021743#define REGCM_IMMALL (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)
Eric Biedermanb138ac82003-04-22 18:44:01 +000021744
21745/* The x86 registers */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021746#define REG_EFLAGS 2
Eric Biedermanb138ac82003-04-22 18:44:01 +000021747#define REGC_FLAGS_FIRST REG_EFLAGS
21748#define REGC_FLAGS_LAST REG_EFLAGS
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021749#define REG_AL 3
21750#define REG_BL 4
21751#define REG_CL 5
21752#define REG_DL 6
21753#define REG_AH 7
21754#define REG_BH 8
21755#define REG_CH 9
21756#define REG_DH 10
Eric Biederman530b5192003-07-01 10:05:30 +000021757#define REGC_GPR8_LO_FIRST REG_AL
21758#define REGC_GPR8_LO_LAST REG_DL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021759#define REGC_GPR8_FIRST REG_AL
Eric Biedermanb138ac82003-04-22 18:44:01 +000021760#define REGC_GPR8_LAST REG_DH
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021761#define REG_AX 11
21762#define REG_BX 12
21763#define REG_CX 13
21764#define REG_DX 14
21765#define REG_SI 15
21766#define REG_DI 16
21767#define REG_BP 17
21768#define REG_SP 18
Eric Biedermanb138ac82003-04-22 18:44:01 +000021769#define REGC_GPR16_FIRST REG_AX
21770#define REGC_GPR16_LAST REG_SP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021771#define REG_EAX 19
21772#define REG_EBX 20
21773#define REG_ECX 21
21774#define REG_EDX 22
21775#define REG_ESI 23
21776#define REG_EDI 24
21777#define REG_EBP 25
21778#define REG_ESP 26
Eric Biedermanb138ac82003-04-22 18:44:01 +000021779#define REGC_GPR32_FIRST REG_EAX
21780#define REGC_GPR32_LAST REG_ESP
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021781#define REG_EDXEAX 27
Eric Biederman530b5192003-07-01 10:05:30 +000021782#define REGC_DIVIDEND64_FIRST REG_EDXEAX
21783#define REGC_DIVIDEND64_LAST REG_EDXEAX
21784#define REG_DXAX 28
21785#define REGC_DIVIDEND32_FIRST REG_DXAX
21786#define REGC_DIVIDEND32_LAST REG_DXAX
21787#define REG_MMX0 29
21788#define REG_MMX1 30
21789#define REG_MMX2 31
21790#define REG_MMX3 32
21791#define REG_MMX4 33
21792#define REG_MMX5 34
21793#define REG_MMX6 35
21794#define REG_MMX7 36
Eric Biedermanb138ac82003-04-22 18:44:01 +000021795#define REGC_MMX_FIRST REG_MMX0
21796#define REGC_MMX_LAST REG_MMX7
Eric Biederman530b5192003-07-01 10:05:30 +000021797#define REG_XMM0 37
21798#define REG_XMM1 38
21799#define REG_XMM2 39
21800#define REG_XMM3 40
21801#define REG_XMM4 41
21802#define REG_XMM5 42
21803#define REG_XMM6 43
21804#define REG_XMM7 44
Eric Biedermanb138ac82003-04-22 18:44:01 +000021805#define REGC_XMM_FIRST REG_XMM0
21806#define REGC_XMM_LAST REG_XMM7
Stefan Reinauer50542a82007-10-24 11:14:14 +000021807
21808#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000021809#warning "WISHLIST figure out how to use pinsrw and pextrw to better use extended regs"
Stefan Reinauer50542a82007-10-24 11:14:14 +000021810#endif
21811
Eric Biedermanb138ac82003-04-22 18:44:01 +000021812#define LAST_REG REG_XMM7
21813
21814#define REGC_GPR32_8_FIRST REG_EAX
21815#define REGC_GPR32_8_LAST REG_EDX
21816#define REGC_GPR16_8_FIRST REG_AX
21817#define REGC_GPR16_8_LAST REG_DX
21818
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021819#define REGC_IMM8_FIRST -1
21820#define REGC_IMM8_LAST -1
21821#define REGC_IMM16_FIRST -2
21822#define REGC_IMM16_LAST -1
21823#define REGC_IMM32_FIRST -4
21824#define REGC_IMM32_LAST -1
21825
Eric Biedermanb138ac82003-04-22 18:44:01 +000021826#if LAST_REG >= MAX_REGISTERS
21827#error "MAX_REGISTERS to low"
21828#endif
21829
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021830
21831static unsigned regc_size[LAST_REGC +1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021832 [REGC_FLAGS] = REGC_FLAGS_LAST - REGC_FLAGS_FIRST + 1,
21833 [REGC_GPR8] = REGC_GPR8_LAST - REGC_GPR8_FIRST + 1,
21834 [REGC_GPR16] = REGC_GPR16_LAST - REGC_GPR16_FIRST + 1,
21835 [REGC_GPR32] = REGC_GPR32_LAST - REGC_GPR32_FIRST + 1,
21836 [REGC_DIVIDEND64] = REGC_DIVIDEND64_LAST - REGC_DIVIDEND64_FIRST + 1,
21837 [REGC_DIVIDEND32] = REGC_DIVIDEND32_LAST - REGC_DIVIDEND32_FIRST + 1,
21838 [REGC_MMX] = REGC_MMX_LAST - REGC_MMX_FIRST + 1,
21839 [REGC_XMM] = REGC_XMM_LAST - REGC_XMM_FIRST + 1,
21840 [REGC_GPR32_8] = REGC_GPR32_8_LAST - REGC_GPR32_8_FIRST + 1,
21841 [REGC_GPR16_8] = REGC_GPR16_8_LAST - REGC_GPR16_8_FIRST + 1,
21842 [REGC_GPR8_LO] = REGC_GPR8_LO_LAST - REGC_GPR8_LO_FIRST + 1,
21843 [REGC_IMM32] = 0,
21844 [REGC_IMM16] = 0,
21845 [REGC_IMM8] = 0,
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021846};
21847
21848static const struct {
21849 int first, last;
21850} regcm_bound[LAST_REGC + 1] = {
Eric Biederman530b5192003-07-01 10:05:30 +000021851 [REGC_FLAGS] = { REGC_FLAGS_FIRST, REGC_FLAGS_LAST },
21852 [REGC_GPR8] = { REGC_GPR8_FIRST, REGC_GPR8_LAST },
21853 [REGC_GPR16] = { REGC_GPR16_FIRST, REGC_GPR16_LAST },
21854 [REGC_GPR32] = { REGC_GPR32_FIRST, REGC_GPR32_LAST },
21855 [REGC_DIVIDEND64] = { REGC_DIVIDEND64_FIRST, REGC_DIVIDEND64_LAST },
21856 [REGC_DIVIDEND32] = { REGC_DIVIDEND32_FIRST, REGC_DIVIDEND32_LAST },
21857 [REGC_MMX] = { REGC_MMX_FIRST, REGC_MMX_LAST },
21858 [REGC_XMM] = { REGC_XMM_FIRST, REGC_XMM_LAST },
21859 [REGC_GPR32_8] = { REGC_GPR32_8_FIRST, REGC_GPR32_8_LAST },
21860 [REGC_GPR16_8] = { REGC_GPR16_8_FIRST, REGC_GPR16_8_LAST },
21861 [REGC_GPR8_LO] = { REGC_GPR8_LO_FIRST, REGC_GPR8_LO_LAST },
21862 [REGC_IMM32] = { REGC_IMM32_FIRST, REGC_IMM32_LAST },
21863 [REGC_IMM16] = { REGC_IMM16_FIRST, REGC_IMM16_LAST },
21864 [REGC_IMM8] = { REGC_IMM8_FIRST, REGC_IMM8_LAST },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021865};
21866
Eric Biederman90089602004-05-28 14:11:54 +000021867#if ARCH_INPUT_REGS != 4
21868#error ARCH_INPUT_REGS size mismatch
21869#endif
21870static const struct reg_info arch_input_regs[ARCH_INPUT_REGS] = {
21871 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21872 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21873 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21874 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21875};
21876
21877#if ARCH_OUTPUT_REGS != 4
21878#error ARCH_INPUT_REGS size mismatch
21879#endif
21880static const struct reg_info arch_output_regs[ARCH_OUTPUT_REGS] = {
21881 { .reg = REG_EAX, .regcm = REGCM_GPR32 },
21882 { .reg = REG_EBX, .regcm = REGCM_GPR32 },
21883 { .reg = REG_ECX, .regcm = REGCM_GPR32 },
21884 { .reg = REG_EDX, .regcm = REGCM_GPR32 },
21885};
21886
Eric Biederman5ade04a2003-10-22 04:03:46 +000021887static void init_arch_state(struct arch_state *arch)
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021888{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021889 memset(arch, 0, sizeof(*arch));
21890 arch->features = 0;
21891}
21892
Eric Biederman90089602004-05-28 14:11:54 +000021893static const struct compiler_flag arch_flags[] = {
21894 { "mmx", X86_MMX_REGS },
21895 { "sse", X86_XMM_REGS },
21896 { "noop-copy", X86_NOOP_COPY },
21897 { 0, 0 },
21898};
21899static const struct compiler_flag arch_cpus[] = {
21900 { "i386", 0 },
21901 { "p2", X86_MMX_REGS },
21902 { "p3", X86_MMX_REGS | X86_XMM_REGS },
21903 { "p4", X86_MMX_REGS | X86_XMM_REGS },
21904 { "k7", X86_MMX_REGS },
21905 { "k8", X86_MMX_REGS | X86_XMM_REGS },
21906 { "c3", X86_MMX_REGS },
21907 { "c3-2", X86_MMX_REGS | X86_XMM_REGS }, /* Nehemiah */
21908 { 0, 0 }
21909};
Eric Biederman5ade04a2003-10-22 04:03:46 +000021910static int arch_encode_flag(struct arch_state *arch, const char *flag)
21911{
Eric Biederman5ade04a2003-10-22 04:03:46 +000021912 int result;
21913 int act;
21914
21915 act = 1;
21916 result = -1;
21917 if (strncmp(flag, "no-", 3) == 0) {
21918 flag += 3;
21919 act = 0;
Eric Biederman83b991a2003-10-11 06:20:25 +000021920 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000021921 if (act && strncmp(flag, "cpu=", 4) == 0) {
21922 flag += 4;
Eric Biederman90089602004-05-28 14:11:54 +000021923 result = set_flag(arch_cpus, &arch->features, 1, flag);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021924 }
Eric Biederman83b991a2003-10-11 06:20:25 +000021925 else {
Eric Biederman90089602004-05-28 14:11:54 +000021926 result = set_flag(arch_flags, &arch->features, act, flag);
Eric Biederman83b991a2003-10-11 06:20:25 +000021927 }
21928 return result;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021929}
21930
Eric Biederman90089602004-05-28 14:11:54 +000021931static void arch_usage(FILE *fp)
21932{
21933 flag_usage(fp, arch_flags, "-m", "-mno-");
21934 flag_usage(fp, arch_cpus, "-mcpu=", 0);
21935}
21936
Eric Biedermanb138ac82003-04-22 18:44:01 +000021937static unsigned arch_regc_size(struct compile_state *state, int class)
21938{
Eric Biedermanb138ac82003-04-22 18:44:01 +000021939 if ((class < 0) || (class > LAST_REGC)) {
21940 return 0;
21941 }
21942 return regc_size[class];
21943}
Eric Biedermand1ea5392003-06-28 06:49:45 +000021944
Eric Biedermanb138ac82003-04-22 18:44:01 +000021945static int arch_regcm_intersect(unsigned regcm1, unsigned regcm2)
21946{
21947 /* See if two register classes may have overlapping registers */
Eric Biederman530b5192003-07-01 10:05:30 +000021948 unsigned gpr_mask = REGCM_GPR8 | REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
21949 REGCM_GPR32_8 | REGCM_GPR32 |
21950 REGCM_DIVIDEND32 | REGCM_DIVIDEND64;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021951
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021952 /* Special case for the immediates */
21953 if ((regcm1 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21954 ((regcm1 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0) &&
21955 (regcm2 & (REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) &&
21956 ((regcm2 & ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8)) == 0)) {
21957 return 0;
21958 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000021959 return (regcm1 & regcm2) ||
21960 ((regcm1 & gpr_mask) && (regcm2 & gpr_mask));
21961}
21962
21963static void arch_reg_equivs(
21964 struct compile_state *state, unsigned *equiv, int reg)
21965{
21966 if ((reg < 0) || (reg > LAST_REG)) {
21967 internal_error(state, 0, "invalid register");
21968 }
21969 *equiv++ = reg;
21970 switch(reg) {
21971 case REG_AL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021972#if X86_4_8BIT_GPRS
21973 *equiv++ = REG_AH;
21974#endif
21975 *equiv++ = REG_AX;
21976 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021977 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021978 *equiv++ = REG_EDXEAX;
21979 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021980 case REG_AH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021981#if X86_4_8BIT_GPRS
21982 *equiv++ = REG_AL;
21983#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000021984 *equiv++ = REG_AX;
21985 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000021986 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000021987 *equiv++ = REG_EDXEAX;
21988 break;
21989 case REG_BL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021990#if X86_4_8BIT_GPRS
21991 *equiv++ = REG_BH;
21992#endif
21993 *equiv++ = REG_BX;
21994 *equiv++ = REG_EBX;
21995 break;
21996
Eric Biedermanb138ac82003-04-22 18:44:01 +000021997 case REG_BH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000021998#if X86_4_8BIT_GPRS
21999 *equiv++ = REG_BL;
22000#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022001 *equiv++ = REG_BX;
22002 *equiv++ = REG_EBX;
22003 break;
22004 case REG_CL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022005#if X86_4_8BIT_GPRS
22006 *equiv++ = REG_CH;
22007#endif
22008 *equiv++ = REG_CX;
22009 *equiv++ = REG_ECX;
22010 break;
22011
Eric Biedermanb138ac82003-04-22 18:44:01 +000022012 case REG_CH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022013#if X86_4_8BIT_GPRS
22014 *equiv++ = REG_CL;
22015#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022016 *equiv++ = REG_CX;
22017 *equiv++ = REG_ECX;
22018 break;
22019 case REG_DL:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022020#if X86_4_8BIT_GPRS
22021 *equiv++ = REG_DH;
22022#endif
22023 *equiv++ = REG_DX;
22024 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022025 *equiv++ = REG_DXAX;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022026 *equiv++ = REG_EDXEAX;
22027 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022028 case REG_DH:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022029#if X86_4_8BIT_GPRS
22030 *equiv++ = REG_DL;
22031#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022032 *equiv++ = REG_DX;
22033 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022034 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022035 *equiv++ = REG_EDXEAX;
22036 break;
22037 case REG_AX:
22038 *equiv++ = REG_AL;
22039 *equiv++ = REG_AH;
22040 *equiv++ = REG_EAX;
Eric Biederman530b5192003-07-01 10:05:30 +000022041 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022042 *equiv++ = REG_EDXEAX;
22043 break;
22044 case REG_BX:
22045 *equiv++ = REG_BL;
22046 *equiv++ = REG_BH;
22047 *equiv++ = REG_EBX;
22048 break;
22049 case REG_CX:
22050 *equiv++ = REG_CL;
22051 *equiv++ = REG_CH;
22052 *equiv++ = REG_ECX;
22053 break;
22054 case REG_DX:
22055 *equiv++ = REG_DL;
22056 *equiv++ = REG_DH;
22057 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022058 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022059 *equiv++ = REG_EDXEAX;
22060 break;
22061 case REG_SI:
22062 *equiv++ = REG_ESI;
22063 break;
22064 case REG_DI:
22065 *equiv++ = REG_EDI;
22066 break;
22067 case REG_BP:
22068 *equiv++ = REG_EBP;
22069 break;
22070 case REG_SP:
22071 *equiv++ = REG_ESP;
22072 break;
22073 case REG_EAX:
22074 *equiv++ = REG_AL;
22075 *equiv++ = REG_AH;
22076 *equiv++ = REG_AX;
Eric Biederman530b5192003-07-01 10:05:30 +000022077 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022078 *equiv++ = REG_EDXEAX;
22079 break;
22080 case REG_EBX:
22081 *equiv++ = REG_BL;
22082 *equiv++ = REG_BH;
22083 *equiv++ = REG_BX;
22084 break;
22085 case REG_ECX:
22086 *equiv++ = REG_CL;
22087 *equiv++ = REG_CH;
22088 *equiv++ = REG_CX;
22089 break;
22090 case REG_EDX:
22091 *equiv++ = REG_DL;
22092 *equiv++ = REG_DH;
22093 *equiv++ = REG_DX;
Eric Biederman530b5192003-07-01 10:05:30 +000022094 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022095 *equiv++ = REG_EDXEAX;
22096 break;
22097 case REG_ESI:
22098 *equiv++ = REG_SI;
22099 break;
22100 case REG_EDI:
22101 *equiv++ = REG_DI;
22102 break;
22103 case REG_EBP:
22104 *equiv++ = REG_BP;
22105 break;
22106 case REG_ESP:
22107 *equiv++ = REG_SP;
22108 break;
Eric Biederman530b5192003-07-01 10:05:30 +000022109 case REG_DXAX:
22110 *equiv++ = REG_AL;
22111 *equiv++ = REG_AH;
22112 *equiv++ = REG_DL;
22113 *equiv++ = REG_DH;
22114 *equiv++ = REG_AX;
22115 *equiv++ = REG_DX;
22116 *equiv++ = REG_EAX;
22117 *equiv++ = REG_EDX;
22118 *equiv++ = REG_EDXEAX;
22119 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022120 case REG_EDXEAX:
22121 *equiv++ = REG_AL;
22122 *equiv++ = REG_AH;
22123 *equiv++ = REG_DL;
22124 *equiv++ = REG_DH;
22125 *equiv++ = REG_AX;
22126 *equiv++ = REG_DX;
22127 *equiv++ = REG_EAX;
22128 *equiv++ = REG_EDX;
Eric Biederman530b5192003-07-01 10:05:30 +000022129 *equiv++ = REG_DXAX;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022130 break;
22131 }
22132 *equiv++ = REG_UNSET;
22133}
22134
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022135static unsigned arch_avail_mask(struct compile_state *state)
22136{
22137 unsigned avail_mask;
Eric Biederman530b5192003-07-01 10:05:30 +000022138 /* REGCM_GPR8 is not available */
22139 avail_mask = REGCM_GPR8_LO | REGCM_GPR16_8 | REGCM_GPR16 |
22140 REGCM_GPR32 | REGCM_GPR32_8 |
22141 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022142 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 | REGCM_FLAGS;
Eric Biederman5ade04a2003-10-22 04:03:46 +000022143 if (state->arch->features & X86_MMX_REGS) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022144 avail_mask |= REGCM_MMX;
Eric Biederman83b991a2003-10-11 06:20:25 +000022145 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000022146 if (state->arch->features & X86_XMM_REGS) {
Eric Biederman83b991a2003-10-11 06:20:25 +000022147 avail_mask |= REGCM_XMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022148 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022149 return avail_mask;
22150}
22151
22152static unsigned arch_regcm_normalize(struct compile_state *state, unsigned regcm)
22153{
22154 unsigned mask, result;
22155 int class, class2;
22156 result = regcm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022157
22158 for(class = 0, mask = 1; mask; mask <<= 1, class++) {
22159 if ((result & mask) == 0) {
22160 continue;
22161 }
22162 if (class > LAST_REGC) {
22163 result &= ~mask;
22164 }
22165 for(class2 = 0; class2 <= LAST_REGC; class2++) {
22166 if ((regcm_bound[class2].first >= regcm_bound[class].first) &&
22167 (regcm_bound[class2].last <= regcm_bound[class].last)) {
22168 result |= (1 << class2);
22169 }
22170 }
22171 }
Eric Biederman530b5192003-07-01 10:05:30 +000022172 result &= arch_avail_mask(state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022173 return result;
22174}
Eric Biedermanb138ac82003-04-22 18:44:01 +000022175
Eric Biedermand1ea5392003-06-28 06:49:45 +000022176static unsigned arch_regcm_reg_normalize(struct compile_state *state, unsigned regcm)
22177{
22178 /* Like arch_regcm_normalize except immediate register classes are excluded */
22179 regcm = arch_regcm_normalize(state, regcm);
22180 /* Remove the immediate register classes */
22181 regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
22182 return regcm;
22183
22184}
22185
Eric Biedermanb138ac82003-04-22 18:44:01 +000022186static unsigned arch_reg_regcm(struct compile_state *state, int reg)
22187{
Eric Biedermanb138ac82003-04-22 18:44:01 +000022188 unsigned mask;
22189 int class;
22190 mask = 0;
22191 for(class = 0; class <= LAST_REGC; class++) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022192 if ((reg >= regcm_bound[class].first) &&
22193 (reg <= regcm_bound[class].last)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022194 mask |= (1 << class);
22195 }
22196 }
22197 if (!mask) {
22198 internal_error(state, 0, "reg %d not in any class", reg);
22199 }
22200 return mask;
22201}
22202
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022203static struct reg_info arch_reg_constraint(
22204 struct compile_state *state, struct type *type, const char *constraint)
22205{
22206 static const struct {
22207 char class;
22208 unsigned int mask;
22209 unsigned int reg;
22210 } constraints[] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022211 { 'r', REGCM_GPR32, REG_UNSET },
22212 { 'g', REGCM_GPR32, REG_UNSET },
22213 { 'p', REGCM_GPR32, REG_UNSET },
22214 { 'q', REGCM_GPR8_LO, REG_UNSET },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022215 { 'Q', REGCM_GPR32_8, REG_UNSET },
Eric Biederman530b5192003-07-01 10:05:30 +000022216 { 'x', REGCM_XMM, REG_UNSET },
22217 { 'y', REGCM_MMX, REG_UNSET },
22218 { 'a', REGCM_GPR32, REG_EAX },
22219 { 'b', REGCM_GPR32, REG_EBX },
22220 { 'c', REGCM_GPR32, REG_ECX },
22221 { 'd', REGCM_GPR32, REG_EDX },
22222 { 'D', REGCM_GPR32, REG_EDI },
22223 { 'S', REGCM_GPR32, REG_ESI },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022224 { '\0', 0, REG_UNSET },
22225 };
22226 unsigned int regcm;
22227 unsigned int mask, reg;
22228 struct reg_info result;
22229 const char *ptr;
22230 regcm = arch_type_to_regcm(state, type);
22231 reg = REG_UNSET;
22232 mask = 0;
22233 for(ptr = constraint; *ptr; ptr++) {
22234 int i;
22235 if (*ptr == ' ') {
22236 continue;
22237 }
22238 for(i = 0; constraints[i].class != '\0'; i++) {
22239 if (constraints[i].class == *ptr) {
22240 break;
22241 }
22242 }
22243 if (constraints[i].class == '\0') {
22244 error(state, 0, "invalid register constraint ``%c''", *ptr);
22245 break;
22246 }
22247 if ((constraints[i].mask & regcm) == 0) {
22248 error(state, 0, "invalid register class %c specified",
22249 *ptr);
22250 }
22251 mask |= constraints[i].mask;
22252 if (constraints[i].reg != REG_UNSET) {
22253 if ((reg != REG_UNSET) && (reg != constraints[i].reg)) {
22254 error(state, 0, "Only one register may be specified");
22255 }
22256 reg = constraints[i].reg;
22257 }
22258 }
22259 result.reg = reg;
22260 result.regcm = mask;
22261 return result;
22262}
22263
22264static struct reg_info arch_reg_clobber(
22265 struct compile_state *state, const char *clobber)
22266{
22267 struct reg_info result;
22268 if (strcmp(clobber, "memory") == 0) {
22269 result.reg = REG_UNSET;
22270 result.regcm = 0;
22271 }
Eric Biederman90089602004-05-28 14:11:54 +000022272 else if (strcmp(clobber, "eax") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022273 result.reg = REG_EAX;
22274 result.regcm = REGCM_GPR32;
22275 }
Eric Biederman90089602004-05-28 14:11:54 +000022276 else if (strcmp(clobber, "ebx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022277 result.reg = REG_EBX;
22278 result.regcm = REGCM_GPR32;
22279 }
Eric Biederman90089602004-05-28 14:11:54 +000022280 else if (strcmp(clobber, "ecx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022281 result.reg = REG_ECX;
22282 result.regcm = REGCM_GPR32;
22283 }
Eric Biederman90089602004-05-28 14:11:54 +000022284 else if (strcmp(clobber, "edx") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022285 result.reg = REG_EDX;
22286 result.regcm = REGCM_GPR32;
22287 }
Eric Biederman90089602004-05-28 14:11:54 +000022288 else if (strcmp(clobber, "esi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022289 result.reg = REG_ESI;
22290 result.regcm = REGCM_GPR32;
22291 }
Eric Biederman90089602004-05-28 14:11:54 +000022292 else if (strcmp(clobber, "edi") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022293 result.reg = REG_EDI;
22294 result.regcm = REGCM_GPR32;
22295 }
Eric Biederman90089602004-05-28 14:11:54 +000022296 else if (strcmp(clobber, "ebp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022297 result.reg = REG_EBP;
22298 result.regcm = REGCM_GPR32;
22299 }
Eric Biederman90089602004-05-28 14:11:54 +000022300 else if (strcmp(clobber, "esp") == 0) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022301 result.reg = REG_ESP;
22302 result.regcm = REGCM_GPR32;
22303 }
22304 else if (strcmp(clobber, "cc") == 0) {
22305 result.reg = REG_EFLAGS;
22306 result.regcm = REGCM_FLAGS;
22307 }
22308 else if ((strncmp(clobber, "xmm", 3) == 0) &&
22309 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22310 result.reg = REG_XMM0 + octdigval(clobber[3]);
22311 result.regcm = REGCM_XMM;
22312 }
Eric Biederman90089602004-05-28 14:11:54 +000022313 else if ((strncmp(clobber, "mm", 2) == 0) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022314 octdigitp(clobber[3]) && (clobber[4] == '\0')) {
22315 result.reg = REG_MMX0 + octdigval(clobber[3]);
22316 result.regcm = REGCM_MMX;
22317 }
22318 else {
Eric Biederman90089602004-05-28 14:11:54 +000022319 error(state, 0, "unknown register name `%s' in asm",
22320 clobber);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022321 result.reg = REG_UNSET;
22322 result.regcm = 0;
22323 }
22324 return result;
22325}
22326
Eric Biedermanb138ac82003-04-22 18:44:01 +000022327static int do_select_reg(struct compile_state *state,
22328 char *used, int reg, unsigned classes)
22329{
22330 unsigned mask;
22331 if (used[reg]) {
22332 return REG_UNSET;
22333 }
22334 mask = arch_reg_regcm(state, reg);
22335 return (classes & mask) ? reg : REG_UNSET;
22336}
22337
22338static int arch_select_free_register(
22339 struct compile_state *state, char *used, int classes)
22340{
Eric Biedermand1ea5392003-06-28 06:49:45 +000022341 /* Live ranges with the most neighbors are colored first.
22342 *
22343 * Generally it does not matter which colors are given
22344 * as the register allocator attempts to color live ranges
22345 * in an order where you are guaranteed not to run out of colors.
22346 *
22347 * Occasionally the register allocator cannot find an order
22348 * of register selection that will find a free color. To
22349 * increase the odds the register allocator will work when
22350 * it guesses first give out registers from register classes
22351 * least likely to run out of registers.
22352 *
Eric Biedermanb138ac82003-04-22 18:44:01 +000022353 */
22354 int i, reg;
22355 reg = REG_UNSET;
Eric Biedermand1ea5392003-06-28 06:49:45 +000022356 for(i = REGC_XMM_FIRST; (reg == REG_UNSET) && (i <= REGC_XMM_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022357 reg = do_select_reg(state, used, i, classes);
22358 }
22359 for(i = REGC_MMX_FIRST; (reg == REG_UNSET) && (i <= REGC_MMX_LAST); i++) {
22360 reg = do_select_reg(state, used, i, classes);
22361 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022362 for(i = REGC_GPR32_LAST; (reg == REG_UNSET) && (i >= REGC_GPR32_FIRST); i--) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022363 reg = do_select_reg(state, used, i, classes);
22364 }
22365 for(i = REGC_GPR16_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR16_LAST); i++) {
22366 reg = do_select_reg(state, used, i, classes);
22367 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022368 for(i = REGC_GPR8_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LAST); i++) {
22369 reg = do_select_reg(state, used, i, classes);
22370 }
Eric Biederman530b5192003-07-01 10:05:30 +000022371 for(i = REGC_GPR8_LO_FIRST; (reg == REG_UNSET) && (i <= REGC_GPR8_LO_LAST); i++) {
22372 reg = do_select_reg(state, used, i, classes);
22373 }
22374 for(i = REGC_DIVIDEND32_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND32_LAST); i++) {
22375 reg = do_select_reg(state, used, i, classes);
22376 }
22377 for(i = REGC_DIVIDEND64_FIRST; (reg == REG_UNSET) && (i <= REGC_DIVIDEND64_LAST); i++) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000022378 reg = do_select_reg(state, used, i, classes);
22379 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022380 for(i = REGC_FLAGS_FIRST; (reg == REG_UNSET) && (i <= REGC_FLAGS_LAST); i++) {
22381 reg = do_select_reg(state, used, i, classes);
22382 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022383 return reg;
22384}
22385
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022386
Eric Biedermanb138ac82003-04-22 18:44:01 +000022387static unsigned arch_type_to_regcm(struct compile_state *state, struct type *type)
22388{
Stefan Reinauer50542a82007-10-24 11:14:14 +000022389
22390#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000022391#warning "FIXME force types smaller (if legal) before I get here"
Stefan Reinauer50542a82007-10-24 11:14:14 +000022392#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022393 unsigned mask;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022394 mask = 0;
22395 switch(type->type & TYPE_MASK) {
22396 case TYPE_ARRAY:
22397 case TYPE_VOID:
22398 mask = 0;
22399 break;
22400 case TYPE_CHAR:
22401 case TYPE_UCHAR:
Eric Biederman530b5192003-07-01 10:05:30 +000022402 mask = REGCM_GPR8 | REGCM_GPR8_LO |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022403 REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022404 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022405 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022406 REGCM_MMX | REGCM_XMM |
22407 REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022408 break;
22409 case TYPE_SHORT:
22410 case TYPE_USHORT:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022411 mask = REGCM_GPR16 | REGCM_GPR16_8 |
Eric Biedermanb138ac82003-04-22 18:44:01 +000022412 REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022413 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022414 REGCM_MMX | REGCM_XMM |
22415 REGCM_IMM32 | REGCM_IMM16;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022416 break;
Eric Biederman90089602004-05-28 14:11:54 +000022417 case TYPE_ENUM:
Eric Biedermanb138ac82003-04-22 18:44:01 +000022418 case TYPE_INT:
22419 case TYPE_UINT:
22420 case TYPE_LONG:
22421 case TYPE_ULONG:
22422 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022423 mask = REGCM_GPR32 | REGCM_GPR32_8 |
Eric Biederman530b5192003-07-01 10:05:30 +000022424 REGCM_DIVIDEND32 | REGCM_DIVIDEND64 |
22425 REGCM_MMX | REGCM_XMM |
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022426 REGCM_IMM32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022427 break;
Eric Biederman90089602004-05-28 14:11:54 +000022428 case TYPE_JOIN:
22429 case TYPE_UNION:
22430 mask = arch_type_to_regcm(state, type->left);
22431 break;
22432 case TYPE_OVERLAP:
22433 mask = arch_type_to_regcm(state, type->left) &
22434 arch_type_to_regcm(state, type->right);
22435 break;
22436 case TYPE_BITFIELD:
22437 mask = arch_type_to_regcm(state, type->left);
22438 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022439 default:
Eric Biederman90089602004-05-28 14:11:54 +000022440 fprintf(state->errout, "type: ");
22441 name_of(state->errout, type);
22442 fprintf(state->errout, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000022443 internal_error(state, 0, "no register class for type");
22444 break;
22445 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000022446 mask = arch_regcm_normalize(state, mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022447 return mask;
22448}
22449
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022450static int is_imm32(struct triple *imm)
22451{
22452 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffffffffUL)) ||
22453 (imm->op == OP_ADDRCONST);
22454
22455}
22456static int is_imm16(struct triple *imm)
22457{
22458 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xffff));
22459}
22460static int is_imm8(struct triple *imm)
22461{
22462 return ((imm->op == OP_INTCONST) && (imm->u.cval <= 0xff));
22463}
22464
22465static int get_imm32(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022466{
22467 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022468 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022469 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022470 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022471 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022472 if (!is_imm32(imm)) {
22473 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022474 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000022475 unuse_triple(*expr, ins);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022476 use_triple(imm, ins);
22477 *expr = imm;
22478 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022479}
22480
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022481static int get_imm8(struct triple *ins, struct triple **expr)
Eric Biedermanb138ac82003-04-22 18:44:01 +000022482{
22483 struct triple *imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022484 imm = *expr;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022485 while(imm->op == OP_COPY) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000022486 imm = RHS(imm, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022487 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022488 if (!is_imm8(imm)) {
22489 return 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022490 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022491 unuse_triple(*expr, ins);
22492 use_triple(imm, ins);
Eric Biedermanb138ac82003-04-22 18:44:01 +000022493 *expr = imm;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022494 return 1;
Eric Biedermanb138ac82003-04-22 18:44:01 +000022495}
22496
Eric Biederman530b5192003-07-01 10:05:30 +000022497#define TEMPLATE_NOP 0
22498#define TEMPLATE_INTCONST8 1
22499#define TEMPLATE_INTCONST32 2
Eric Biederman90089602004-05-28 14:11:54 +000022500#define TEMPLATE_UNKNOWNVAL 3
22501#define TEMPLATE_COPY8_REG 5
22502#define TEMPLATE_COPY16_REG 6
22503#define TEMPLATE_COPY32_REG 7
22504#define TEMPLATE_COPY_IMM8 8
22505#define TEMPLATE_COPY_IMM16 9
22506#define TEMPLATE_COPY_IMM32 10
22507#define TEMPLATE_PHI8 11
22508#define TEMPLATE_PHI16 12
22509#define TEMPLATE_PHI32 13
22510#define TEMPLATE_STORE8 14
22511#define TEMPLATE_STORE16 15
22512#define TEMPLATE_STORE32 16
22513#define TEMPLATE_LOAD8 17
22514#define TEMPLATE_LOAD16 18
22515#define TEMPLATE_LOAD32 19
22516#define TEMPLATE_BINARY8_REG 20
22517#define TEMPLATE_BINARY16_REG 21
22518#define TEMPLATE_BINARY32_REG 22
22519#define TEMPLATE_BINARY8_IMM 23
22520#define TEMPLATE_BINARY16_IMM 24
22521#define TEMPLATE_BINARY32_IMM 25
22522#define TEMPLATE_SL8_CL 26
22523#define TEMPLATE_SL16_CL 27
22524#define TEMPLATE_SL32_CL 28
22525#define TEMPLATE_SL8_IMM 29
22526#define TEMPLATE_SL16_IMM 30
22527#define TEMPLATE_SL32_IMM 31
22528#define TEMPLATE_UNARY8 32
22529#define TEMPLATE_UNARY16 33
22530#define TEMPLATE_UNARY32 34
22531#define TEMPLATE_CMP8_REG 35
22532#define TEMPLATE_CMP16_REG 36
22533#define TEMPLATE_CMP32_REG 37
22534#define TEMPLATE_CMP8_IMM 38
22535#define TEMPLATE_CMP16_IMM 39
22536#define TEMPLATE_CMP32_IMM 40
22537#define TEMPLATE_TEST8 41
22538#define TEMPLATE_TEST16 42
22539#define TEMPLATE_TEST32 43
22540#define TEMPLATE_SET 44
22541#define TEMPLATE_JMP 45
22542#define TEMPLATE_RET 46
22543#define TEMPLATE_INB_DX 47
22544#define TEMPLATE_INB_IMM 48
22545#define TEMPLATE_INW_DX 49
22546#define TEMPLATE_INW_IMM 50
22547#define TEMPLATE_INL_DX 51
22548#define TEMPLATE_INL_IMM 52
22549#define TEMPLATE_OUTB_DX 53
22550#define TEMPLATE_OUTB_IMM 54
22551#define TEMPLATE_OUTW_DX 55
22552#define TEMPLATE_OUTW_IMM 56
22553#define TEMPLATE_OUTL_DX 57
22554#define TEMPLATE_OUTL_IMM 58
22555#define TEMPLATE_BSF 59
22556#define TEMPLATE_RDMSR 60
22557#define TEMPLATE_WRMSR 61
22558#define TEMPLATE_UMUL8 62
22559#define TEMPLATE_UMUL16 63
22560#define TEMPLATE_UMUL32 64
22561#define TEMPLATE_DIV8 65
22562#define TEMPLATE_DIV16 66
22563#define TEMPLATE_DIV32 67
Eric Biederman530b5192003-07-01 10:05:30 +000022564#define LAST_TEMPLATE TEMPLATE_DIV32
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022565#if LAST_TEMPLATE >= MAX_TEMPLATES
22566#error "MAX_TEMPLATES to low"
22567#endif
Eric Biedermanb138ac82003-04-22 18:44:01 +000022568
Eric Biederman530b5192003-07-01 10:05:30 +000022569#define COPY8_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO | REGCM_MMX | REGCM_XMM)
22570#define COPY16_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_GPR16 | REGCM_MMX | REGCM_XMM)
22571#define COPY32_REGCM (REGCM_DIVIDEND64 | REGCM_DIVIDEND32 | REGCM_GPR32 | REGCM_MMX | REGCM_XMM)
Eric Biedermand1ea5392003-06-28 06:49:45 +000022572
Eric Biedermanb138ac82003-04-22 18:44:01 +000022573
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022574static struct ins_template templates[] = {
Eric Biederman90089602004-05-28 14:11:54 +000022575 [TEMPLATE_NOP] = {
22576 .lhs = {
22577 [ 0] = { REG_UNNEEDED, REGCM_IMMALL },
22578 [ 1] = { REG_UNNEEDED, REGCM_IMMALL },
22579 [ 2] = { REG_UNNEEDED, REGCM_IMMALL },
22580 [ 3] = { REG_UNNEEDED, REGCM_IMMALL },
22581 [ 4] = { REG_UNNEEDED, REGCM_IMMALL },
22582 [ 5] = { REG_UNNEEDED, REGCM_IMMALL },
22583 [ 6] = { REG_UNNEEDED, REGCM_IMMALL },
22584 [ 7] = { REG_UNNEEDED, REGCM_IMMALL },
22585 [ 8] = { REG_UNNEEDED, REGCM_IMMALL },
22586 [ 9] = { REG_UNNEEDED, REGCM_IMMALL },
22587 [10] = { REG_UNNEEDED, REGCM_IMMALL },
22588 [11] = { REG_UNNEEDED, REGCM_IMMALL },
22589 [12] = { REG_UNNEEDED, REGCM_IMMALL },
22590 [13] = { REG_UNNEEDED, REGCM_IMMALL },
22591 [14] = { REG_UNNEEDED, REGCM_IMMALL },
22592 [15] = { REG_UNNEEDED, REGCM_IMMALL },
22593 [16] = { REG_UNNEEDED, REGCM_IMMALL },
22594 [17] = { REG_UNNEEDED, REGCM_IMMALL },
22595 [18] = { REG_UNNEEDED, REGCM_IMMALL },
22596 [19] = { REG_UNNEEDED, REGCM_IMMALL },
22597 [20] = { REG_UNNEEDED, REGCM_IMMALL },
22598 [21] = { REG_UNNEEDED, REGCM_IMMALL },
22599 [22] = { REG_UNNEEDED, REGCM_IMMALL },
22600 [23] = { REG_UNNEEDED, REGCM_IMMALL },
22601 [24] = { REG_UNNEEDED, REGCM_IMMALL },
22602 [25] = { REG_UNNEEDED, REGCM_IMMALL },
22603 [26] = { REG_UNNEEDED, REGCM_IMMALL },
22604 [27] = { REG_UNNEEDED, REGCM_IMMALL },
22605 [28] = { REG_UNNEEDED, REGCM_IMMALL },
22606 [29] = { REG_UNNEEDED, REGCM_IMMALL },
22607 [30] = { REG_UNNEEDED, REGCM_IMMALL },
22608 [31] = { REG_UNNEEDED, REGCM_IMMALL },
22609 [32] = { REG_UNNEEDED, REGCM_IMMALL },
22610 [33] = { REG_UNNEEDED, REGCM_IMMALL },
22611 [34] = { REG_UNNEEDED, REGCM_IMMALL },
22612 [35] = { REG_UNNEEDED, REGCM_IMMALL },
22613 [36] = { REG_UNNEEDED, REGCM_IMMALL },
22614 [37] = { REG_UNNEEDED, REGCM_IMMALL },
22615 [38] = { REG_UNNEEDED, REGCM_IMMALL },
22616 [39] = { REG_UNNEEDED, REGCM_IMMALL },
22617 [40] = { REG_UNNEEDED, REGCM_IMMALL },
22618 [41] = { REG_UNNEEDED, REGCM_IMMALL },
22619 [42] = { REG_UNNEEDED, REGCM_IMMALL },
22620 [43] = { REG_UNNEEDED, REGCM_IMMALL },
22621 [44] = { REG_UNNEEDED, REGCM_IMMALL },
22622 [45] = { REG_UNNEEDED, REGCM_IMMALL },
22623 [46] = { REG_UNNEEDED, REGCM_IMMALL },
22624 [47] = { REG_UNNEEDED, REGCM_IMMALL },
22625 [48] = { REG_UNNEEDED, REGCM_IMMALL },
22626 [49] = { REG_UNNEEDED, REGCM_IMMALL },
22627 [50] = { REG_UNNEEDED, REGCM_IMMALL },
22628 [51] = { REG_UNNEEDED, REGCM_IMMALL },
22629 [52] = { REG_UNNEEDED, REGCM_IMMALL },
22630 [53] = { REG_UNNEEDED, REGCM_IMMALL },
22631 [54] = { REG_UNNEEDED, REGCM_IMMALL },
22632 [55] = { REG_UNNEEDED, REGCM_IMMALL },
22633 [56] = { REG_UNNEEDED, REGCM_IMMALL },
22634 [57] = { REG_UNNEEDED, REGCM_IMMALL },
22635 [58] = { REG_UNNEEDED, REGCM_IMMALL },
22636 [59] = { REG_UNNEEDED, REGCM_IMMALL },
22637 [60] = { REG_UNNEEDED, REGCM_IMMALL },
22638 [61] = { REG_UNNEEDED, REGCM_IMMALL },
22639 [62] = { REG_UNNEEDED, REGCM_IMMALL },
22640 [63] = { REG_UNNEEDED, REGCM_IMMALL },
22641 },
22642 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022643 [TEMPLATE_INTCONST8] = {
22644 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22645 },
22646 [TEMPLATE_INTCONST32] = {
22647 .lhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 } },
22648 },
Eric Biederman90089602004-05-28 14:11:54 +000022649 [TEMPLATE_UNKNOWNVAL] = {
22650 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
22651 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022652 [TEMPLATE_COPY8_REG] = {
22653 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
22654 .rhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022655 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022656 [TEMPLATE_COPY16_REG] = {
22657 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22658 .rhs = { [0] = { REG_UNSET, COPY16_REGCM } },
22659 },
22660 [TEMPLATE_COPY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022661 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022662 .rhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022663 },
22664 [TEMPLATE_COPY_IMM8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022665 .lhs = { [0] = { REG_UNSET, COPY8_REGCM } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022666 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22667 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022668 [TEMPLATE_COPY_IMM16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022669 .lhs = { [0] = { REG_UNSET, COPY16_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022670 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM16 | REGCM_IMM8 } },
22671 },
22672 [TEMPLATE_COPY_IMM32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022673 .lhs = { [0] = { REG_UNSET, COPY32_REGCM } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022674 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8 } },
22675 },
22676 [TEMPLATE_PHI8] = {
22677 .lhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022678 .rhs = { [0] = { REG_VIRT0, COPY8_REGCM } },
22679 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022680 [TEMPLATE_PHI16] = {
22681 .lhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022682 .rhs = { [0] = { REG_VIRT0, COPY16_REGCM } },
22683 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022684 [TEMPLATE_PHI32] = {
22685 .lhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
Eric Biederman90089602004-05-28 14:11:54 +000022686 .rhs = { [0] = { REG_VIRT0, COPY32_REGCM } },
22687 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022688 [TEMPLATE_STORE8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022689 .rhs = {
22690 [0] = { REG_UNSET, REGCM_GPR32 },
22691 [1] = { REG_UNSET, REGCM_GPR8_LO },
22692 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022693 },
22694 [TEMPLATE_STORE16] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022695 .rhs = {
22696 [0] = { REG_UNSET, REGCM_GPR32 },
22697 [1] = { REG_UNSET, REGCM_GPR16 },
22698 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022699 },
22700 [TEMPLATE_STORE32] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022701 .rhs = {
22702 [0] = { REG_UNSET, REGCM_GPR32 },
22703 [1] = { REG_UNSET, REGCM_GPR32 },
22704 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022705 },
22706 [TEMPLATE_LOAD8] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022707 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022708 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22709 },
22710 [TEMPLATE_LOAD16] = {
22711 .lhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22712 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22713 },
22714 [TEMPLATE_LOAD32] = {
22715 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22716 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22717 },
Eric Biederman530b5192003-07-01 10:05:30 +000022718 [TEMPLATE_BINARY8_REG] = {
22719 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22720 .rhs = {
22721 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22722 [1] = { REG_UNSET, REGCM_GPR8_LO },
22723 },
22724 },
22725 [TEMPLATE_BINARY16_REG] = {
22726 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22727 .rhs = {
22728 [0] = { REG_VIRT0, REGCM_GPR16 },
22729 [1] = { REG_UNSET, REGCM_GPR16 },
22730 },
22731 },
22732 [TEMPLATE_BINARY32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022733 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22734 .rhs = {
22735 [0] = { REG_VIRT0, REGCM_GPR32 },
22736 [1] = { REG_UNSET, REGCM_GPR32 },
22737 },
22738 },
Eric Biederman530b5192003-07-01 10:05:30 +000022739 [TEMPLATE_BINARY8_IMM] = {
22740 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22741 .rhs = {
22742 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22743 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22744 },
22745 },
22746 [TEMPLATE_BINARY16_IMM] = {
22747 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22748 .rhs = {
22749 [0] = { REG_VIRT0, REGCM_GPR16 },
22750 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22751 },
22752 },
22753 [TEMPLATE_BINARY32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022754 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22755 .rhs = {
22756 [0] = { REG_VIRT0, REGCM_GPR32 },
22757 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22758 },
22759 },
Eric Biederman530b5192003-07-01 10:05:30 +000022760 [TEMPLATE_SL8_CL] = {
22761 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22762 .rhs = {
22763 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22764 [1] = { REG_CL, REGCM_GPR8_LO },
22765 },
22766 },
22767 [TEMPLATE_SL16_CL] = {
22768 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22769 .rhs = {
22770 [0] = { REG_VIRT0, REGCM_GPR16 },
22771 [1] = { REG_CL, REGCM_GPR8_LO },
22772 },
22773 },
22774 [TEMPLATE_SL32_CL] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022775 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22776 .rhs = {
22777 [0] = { REG_VIRT0, REGCM_GPR32 },
Eric Biederman530b5192003-07-01 10:05:30 +000022778 [1] = { REG_CL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022779 },
22780 },
Eric Biederman530b5192003-07-01 10:05:30 +000022781 [TEMPLATE_SL8_IMM] = {
22782 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22783 .rhs = {
22784 [0] = { REG_VIRT0, REGCM_GPR8_LO },
22785 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22786 },
22787 },
22788 [TEMPLATE_SL16_IMM] = {
22789 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22790 .rhs = {
22791 [0] = { REG_VIRT0, REGCM_GPR16 },
22792 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22793 },
22794 },
22795 [TEMPLATE_SL32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022796 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22797 .rhs = {
22798 [0] = { REG_VIRT0, REGCM_GPR32 },
22799 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22800 },
22801 },
Eric Biederman530b5192003-07-01 10:05:30 +000022802 [TEMPLATE_UNARY8] = {
22803 .lhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22804 .rhs = { [0] = { REG_VIRT0, REGCM_GPR8_LO } },
22805 },
22806 [TEMPLATE_UNARY16] = {
22807 .lhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22808 .rhs = { [0] = { REG_VIRT0, REGCM_GPR16 } },
22809 },
22810 [TEMPLATE_UNARY32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022811 .lhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22812 .rhs = { [0] = { REG_VIRT0, REGCM_GPR32 } },
22813 },
Eric Biederman530b5192003-07-01 10:05:30 +000022814 [TEMPLATE_CMP8_REG] = {
22815 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22816 .rhs = {
22817 [0] = { REG_UNSET, REGCM_GPR8_LO },
22818 [1] = { REG_UNSET, REGCM_GPR8_LO },
22819 },
22820 },
22821 [TEMPLATE_CMP16_REG] = {
22822 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22823 .rhs = {
22824 [0] = { REG_UNSET, REGCM_GPR16 },
22825 [1] = { REG_UNSET, REGCM_GPR16 },
22826 },
22827 },
22828 [TEMPLATE_CMP32_REG] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022829 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22830 .rhs = {
22831 [0] = { REG_UNSET, REGCM_GPR32 },
22832 [1] = { REG_UNSET, REGCM_GPR32 },
22833 },
22834 },
Eric Biederman530b5192003-07-01 10:05:30 +000022835 [TEMPLATE_CMP8_IMM] = {
22836 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22837 .rhs = {
22838 [0] = { REG_UNSET, REGCM_GPR8_LO },
22839 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22840 },
22841 },
22842 [TEMPLATE_CMP16_IMM] = {
22843 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22844 .rhs = {
22845 [0] = { REG_UNSET, REGCM_GPR16 },
22846 [1] = { REG_UNNEEDED, REGCM_IMM16 },
22847 },
22848 },
22849 [TEMPLATE_CMP32_IMM] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022850 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22851 .rhs = {
22852 [0] = { REG_UNSET, REGCM_GPR32 },
22853 [1] = { REG_UNNEEDED, REGCM_IMM32 },
22854 },
22855 },
Eric Biederman530b5192003-07-01 10:05:30 +000022856 [TEMPLATE_TEST8] = {
22857 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22858 .rhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
22859 },
22860 [TEMPLATE_TEST16] = {
22861 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22862 .rhs = { [0] = { REG_UNSET, REGCM_GPR16 } },
22863 },
22864 [TEMPLATE_TEST32] = {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022865 .lhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22866 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22867 },
22868 [TEMPLATE_SET] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022869 .lhs = { [0] = { REG_UNSET, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022870 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22871 },
22872 [TEMPLATE_JMP] = {
22873 .rhs = { [0] = { REG_EFLAGS, REGCM_FLAGS } },
22874 },
Eric Biederman5ade04a2003-10-22 04:03:46 +000022875 [TEMPLATE_RET] = {
22876 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22877 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022878 [TEMPLATE_INB_DX] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022879 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022880 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22881 },
22882 [TEMPLATE_INB_IMM] = {
Eric Biederman530b5192003-07-01 10:05:30 +000022883 .lhs = { [0] = { REG_AL, REGCM_GPR8_LO } },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022884 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22885 },
22886 [TEMPLATE_INW_DX] = {
22887 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22888 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22889 },
22890 [TEMPLATE_INW_IMM] = {
22891 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22892 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22893 },
22894 [TEMPLATE_INL_DX] = {
22895 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22896 .rhs = { [0] = { REG_DX, REGCM_GPR16 } },
22897 },
22898 [TEMPLATE_INL_IMM] = {
22899 .lhs = { [0] = { REG_EAX, REGCM_GPR32 } },
22900 .rhs = { [0] = { REG_UNNEEDED, REGCM_IMM8 } },
22901 },
22902 [TEMPLATE_OUTB_DX] = {
22903 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022904 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022905 [1] = { REG_DX, REGCM_GPR16 },
22906 },
22907 },
22908 [TEMPLATE_OUTB_IMM] = {
22909 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000022910 [0] = { REG_AL, REGCM_GPR8_LO },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000022911 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22912 },
22913 },
22914 [TEMPLATE_OUTW_DX] = {
22915 .rhs = {
22916 [0] = { REG_AX, REGCM_GPR16 },
22917 [1] = { REG_DX, REGCM_GPR16 },
22918 },
22919 },
22920 [TEMPLATE_OUTW_IMM] = {
22921 .rhs = {
22922 [0] = { REG_AX, REGCM_GPR16 },
22923 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22924 },
22925 },
22926 [TEMPLATE_OUTL_DX] = {
22927 .rhs = {
22928 [0] = { REG_EAX, REGCM_GPR32 },
22929 [1] = { REG_DX, REGCM_GPR16 },
22930 },
22931 },
22932 [TEMPLATE_OUTL_IMM] = {
22933 .rhs = {
22934 [0] = { REG_EAX, REGCM_GPR32 },
22935 [1] = { REG_UNNEEDED, REGCM_IMM8 },
22936 },
22937 },
22938 [TEMPLATE_BSF] = {
22939 .lhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22940 .rhs = { [0] = { REG_UNSET, REGCM_GPR32 } },
22941 },
22942 [TEMPLATE_RDMSR] = {
22943 .lhs = {
22944 [0] = { REG_EAX, REGCM_GPR32 },
22945 [1] = { REG_EDX, REGCM_GPR32 },
22946 },
22947 .rhs = { [0] = { REG_ECX, REGCM_GPR32 } },
22948 },
22949 [TEMPLATE_WRMSR] = {
22950 .rhs = {
22951 [0] = { REG_ECX, REGCM_GPR32 },
22952 [1] = { REG_EAX, REGCM_GPR32 },
22953 [2] = { REG_EDX, REGCM_GPR32 },
22954 },
22955 },
Eric Biederman530b5192003-07-01 10:05:30 +000022956 [TEMPLATE_UMUL8] = {
22957 .lhs = { [0] = { REG_AX, REGCM_GPR16 } },
22958 .rhs = {
22959 [0] = { REG_AL, REGCM_GPR8_LO },
22960 [1] = { REG_UNSET, REGCM_GPR8_LO },
22961 },
22962 },
22963 [TEMPLATE_UMUL16] = {
22964 .lhs = { [0] = { REG_DXAX, REGCM_DIVIDEND32 } },
22965 .rhs = {
22966 [0] = { REG_AX, REGCM_GPR16 },
22967 [1] = { REG_UNSET, REGCM_GPR16 },
22968 },
22969 },
22970 [TEMPLATE_UMUL32] = {
22971 .lhs = { [0] = { REG_EDXEAX, REGCM_DIVIDEND64 } },
Eric Biedermand1ea5392003-06-28 06:49:45 +000022972 .rhs = {
22973 [0] = { REG_EAX, REGCM_GPR32 },
22974 [1] = { REG_UNSET, REGCM_GPR32 },
22975 },
22976 },
Eric Biederman530b5192003-07-01 10:05:30 +000022977 [TEMPLATE_DIV8] = {
22978 .lhs = {
22979 [0] = { REG_AL, REGCM_GPR8_LO },
22980 [1] = { REG_AH, REGCM_GPR8 },
22981 },
22982 .rhs = {
22983 [0] = { REG_AX, REGCM_GPR16 },
22984 [1] = { REG_UNSET, REGCM_GPR8_LO },
22985 },
22986 },
22987 [TEMPLATE_DIV16] = {
22988 .lhs = {
22989 [0] = { REG_AX, REGCM_GPR16 },
22990 [1] = { REG_DX, REGCM_GPR16 },
22991 },
22992 .rhs = {
22993 [0] = { REG_DXAX, REGCM_DIVIDEND32 },
22994 [1] = { REG_UNSET, REGCM_GPR16 },
22995 },
22996 },
22997 [TEMPLATE_DIV32] = {
Eric Biedermand1ea5392003-06-28 06:49:45 +000022998 .lhs = {
22999 [0] = { REG_EAX, REGCM_GPR32 },
23000 [1] = { REG_EDX, REGCM_GPR32 },
23001 },
23002 .rhs = {
Eric Biederman530b5192003-07-01 10:05:30 +000023003 [0] = { REG_EDXEAX, REGCM_DIVIDEND64 },
Eric Biedermand1ea5392003-06-28 06:49:45 +000023004 [1] = { REG_UNSET, REGCM_GPR32 },
23005 },
23006 },
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023007};
Eric Biedermanb138ac82003-04-22 18:44:01 +000023008
Eric Biederman83b991a2003-10-11 06:20:25 +000023009static void fixup_branch(struct compile_state *state,
23010 struct triple *branch, int jmp_op, int cmp_op, struct type *cmp_type,
23011 struct triple *left, struct triple *right)
23012{
23013 struct triple *test;
23014 if (!left) {
23015 internal_error(state, branch, "no branch test?");
23016 }
23017 test = pre_triple(state, branch,
23018 cmp_op, cmp_type, left, right);
23019 test->template_id = TEMPLATE_TEST32;
23020 if (cmp_op == OP_CMP) {
23021 test->template_id = TEMPLATE_CMP32_REG;
23022 if (get_imm32(test, &RHS(test, 1))) {
23023 test->template_id = TEMPLATE_CMP32_IMM;
23024 }
23025 }
23026 use_triple(RHS(test, 0), test);
23027 use_triple(RHS(test, 1), test);
23028 unuse_triple(RHS(branch, 0), branch);
23029 RHS(branch, 0) = test;
23030 branch->op = jmp_op;
23031 branch->template_id = TEMPLATE_JMP;
23032 use_triple(RHS(branch, 0), branch);
23033}
23034
Eric Biedermanb138ac82003-04-22 18:44:01 +000023035static void fixup_branches(struct compile_state *state,
23036 struct triple *cmp, struct triple *use, int jmp_op)
23037{
23038 struct triple_set *entry, *next;
23039 for(entry = use->use; entry; entry = next) {
23040 next = entry->next;
23041 if (entry->member->op == OP_COPY) {
23042 fixup_branches(state, cmp, entry->member, jmp_op);
23043 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023044 else if (entry->member->op == OP_CBRANCH) {
Eric Biederman83b991a2003-10-11 06:20:25 +000023045 struct triple *branch;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023046 struct triple *left, *right;
23047 left = right = 0;
23048 left = RHS(cmp, 0);
Eric Biederman90089602004-05-28 14:11:54 +000023049 if (cmp->rhs > 1) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000023050 right = RHS(cmp, 1);
23051 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023052 branch = entry->member;
Eric Biederman83b991a2003-10-11 06:20:25 +000023053 fixup_branch(state, branch, jmp_op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023054 cmp->op, cmp->type, left, right);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023055 }
23056 }
23057}
23058
23059static void bool_cmp(struct compile_state *state,
23060 struct triple *ins, int cmp_op, int jmp_op, int set_op)
23061{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023062 struct triple_set *entry, *next;
Eric Biederman90089602004-05-28 14:11:54 +000023063 struct triple *set, *convert;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023064
23065 /* Put a barrier up before the cmp which preceeds the
23066 * copy instruction. If a set actually occurs this gives
23067 * us a chance to move variables in registers out of the way.
23068 */
23069
23070 /* Modify the comparison operator */
23071 ins->op = cmp_op;
Eric Biederman530b5192003-07-01 10:05:30 +000023072 ins->template_id = TEMPLATE_TEST32;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023073 if (cmp_op == OP_CMP) {
Eric Biederman530b5192003-07-01 10:05:30 +000023074 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023075 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023076 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023077 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023078 }
23079 /* Generate the instruction sequence that will transform the
23080 * result of the comparison into a logical value.
23081 */
Eric Biederman90089602004-05-28 14:11:54 +000023082 set = post_triple(state, ins, set_op, &uchar_type, ins, 0);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023083 use_triple(ins, set);
23084 set->template_id = TEMPLATE_SET;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023085
Eric Biederman90089602004-05-28 14:11:54 +000023086 convert = set;
23087 if (!equiv_types(ins->type, set->type)) {
23088 convert = post_triple(state, set, OP_CONVERT, ins->type, set, 0);
23089 use_triple(set, convert);
23090 convert->template_id = TEMPLATE_COPY32_REG;
23091 }
23092
Eric Biedermanb138ac82003-04-22 18:44:01 +000023093 for(entry = ins->use; entry; entry = next) {
23094 next = entry->next;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023095 if (entry->member == set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023096 continue;
23097 }
Eric Biederman90089602004-05-28 14:11:54 +000023098 replace_rhs_use(state, ins, convert, entry->member);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023099 }
Eric Biederman90089602004-05-28 14:11:54 +000023100 fixup_branches(state, ins, convert, jmp_op);
Eric Biederman0babc1c2003-05-09 02:39:00 +000023101}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023102
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023103struct reg_info arch_reg_lhs(struct compile_state *state, struct triple *ins, int index)
23104{
23105 struct ins_template *template;
23106 struct reg_info result;
23107 int zlhs;
23108 if (ins->op == OP_PIECE) {
23109 index = ins->u.cval;
23110 ins = MISC(ins, 0);
23111 }
Eric Biederman90089602004-05-28 14:11:54 +000023112 zlhs = ins->lhs;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023113 if (triple_is_def(state, ins)) {
23114 zlhs = 1;
23115 }
23116 if (index >= zlhs) {
Eric Biederman90089602004-05-28 14:11:54 +000023117 internal_error(state, ins, "index %d out of range for %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023118 index, tops(ins->op));
23119 }
23120 switch(ins->op) {
23121 case OP_ASM:
23122 template = &ins->u.ainfo->tmpl;
23123 break;
23124 default:
23125 if (ins->template_id > LAST_TEMPLATE) {
23126 internal_error(state, ins, "bad template number %d",
23127 ins->template_id);
23128 }
23129 template = &templates[ins->template_id];
23130 break;
23131 }
23132 result = template->lhs[index];
23133 result.regcm = arch_regcm_normalize(state, result.regcm);
23134 if (result.reg != REG_UNNEEDED) {
23135 result.regcm &= ~(REGCM_IMM32 | REGCM_IMM16 | REGCM_IMM8);
23136 }
23137 if (result.regcm == 0) {
23138 internal_error(state, ins, "lhs %d regcm == 0", index);
23139 }
23140 return result;
23141}
23142
23143struct reg_info arch_reg_rhs(struct compile_state *state, struct triple *ins, int index)
23144{
23145 struct reg_info result;
23146 struct ins_template *template;
Eric Biederman90089602004-05-28 14:11:54 +000023147 if ((index > ins->rhs) ||
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023148 (ins->op == OP_PIECE)) {
23149 internal_error(state, ins, "index %d out of range for %s\n",
23150 index, tops(ins->op));
23151 }
23152 switch(ins->op) {
23153 case OP_ASM:
23154 template = &ins->u.ainfo->tmpl;
23155 break;
Eric Biederman90089602004-05-28 14:11:54 +000023156 case OP_PHI:
23157 index = 0;
23158 /* Fall through */
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023159 default:
23160 if (ins->template_id > LAST_TEMPLATE) {
23161 internal_error(state, ins, "bad template number %d",
23162 ins->template_id);
23163 }
23164 template = &templates[ins->template_id];
23165 break;
23166 }
23167 result = template->rhs[index];
23168 result.regcm = arch_regcm_normalize(state, result.regcm);
23169 if (result.regcm == 0) {
23170 internal_error(state, ins, "rhs %d regcm == 0", index);
23171 }
23172 return result;
23173}
23174
Eric Biederman530b5192003-07-01 10:05:30 +000023175static struct triple *mod_div(struct compile_state *state,
23176 struct triple *ins, int div_op, int index)
23177{
23178 struct triple *div, *piece0, *piece1;
23179
Eric Biederman530b5192003-07-01 10:05:30 +000023180 /* Generate the appropriate division instruction */
23181 div = post_triple(state, ins, div_op, ins->type, 0, 0);
23182 RHS(div, 0) = RHS(ins, 0);
23183 RHS(div, 1) = RHS(ins, 1);
Eric Biederman90089602004-05-28 14:11:54 +000023184 piece0 = LHS(div, 0);
23185 piece1 = LHS(div, 1);
Eric Biederman530b5192003-07-01 10:05:30 +000023186 div->template_id = TEMPLATE_DIV32;
23187 use_triple(RHS(div, 0), div);
23188 use_triple(RHS(div, 1), div);
23189 use_triple(LHS(div, 0), div);
23190 use_triple(LHS(div, 1), div);
23191
Eric Biederman530b5192003-07-01 10:05:30 +000023192 /* Replate uses of ins with the appropriate piece of the div */
23193 propogate_use(state, ins, LHS(div, index));
23194 release_triple(state, ins);
23195
23196 /* Return the address of the next instruction */
23197 return piece1->next;
23198}
23199
Eric Biederman90089602004-05-28 14:11:54 +000023200static int noop_adecl(struct triple *adecl)
23201{
23202 struct triple_set *use;
23203 /* It's a noop if it doesn't specify stoorage */
23204 if (adecl->lhs == 0) {
23205 return 1;
23206 }
23207 /* Is the adecl used? If not it's a noop */
23208 for(use = adecl->use; use ; use = use->next) {
23209 if ((use->member->op != OP_PIECE) ||
23210 (MISC(use->member, 0) != adecl)) {
23211 return 0;
23212 }
23213 }
23214 return 1;
23215}
23216
23217static struct triple *x86_deposit(struct compile_state *state, struct triple *ins)
23218{
23219 struct triple *mask, *nmask, *shift;
23220 struct triple *val, *val_mask, *val_shift;
23221 struct triple *targ, *targ_mask;
23222 struct triple *new;
23223 ulong_t the_mask, the_nmask;
23224
23225 targ = RHS(ins, 0);
23226 val = RHS(ins, 1);
23227
23228 /* Get constant for the mask value */
23229 the_mask = 1;
23230 the_mask <<= ins->u.bitfield.size;
23231 the_mask -= 1;
23232 the_mask <<= ins->u.bitfield.offset;
23233 mask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23234 mask->u.cval = the_mask;
23235
23236 /* Get the inverted mask value */
23237 the_nmask = ~the_mask;
23238 nmask = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23239 nmask->u.cval = the_nmask;
23240
23241 /* Get constant for the shift value */
23242 shift = pre_triple(state, ins, OP_INTCONST, &uint_type, 0, 0);
23243 shift->u.cval = ins->u.bitfield.offset;
23244
23245 /* Shift and mask the source value */
23246 val_shift = val;
23247 if (shift->u.cval != 0) {
23248 val_shift = pre_triple(state, ins, OP_SL, val->type, val, shift);
23249 use_triple(val, val_shift);
23250 use_triple(shift, val_shift);
23251 }
23252 val_mask = val_shift;
23253 if (is_signed(val->type)) {
23254 val_mask = pre_triple(state, ins, OP_AND, val->type, val_shift, mask);
23255 use_triple(val_shift, val_mask);
23256 use_triple(mask, val_mask);
23257 }
23258
23259 /* Mask the target value */
23260 targ_mask = pre_triple(state, ins, OP_AND, targ->type, targ, nmask);
23261 use_triple(targ, targ_mask);
23262 use_triple(nmask, targ_mask);
23263
23264 /* Now combined them together */
23265 new = pre_triple(state, ins, OP_OR, targ->type, targ_mask, val_mask);
23266 use_triple(targ_mask, new);
23267 use_triple(val_mask, new);
23268
23269 /* Move all of the users over to the new expression */
23270 propogate_use(state, ins, new);
23271
23272 /* Delete the original triple */
23273 release_triple(state, ins);
23274
23275 /* Restart the transformation at mask */
23276 return mask;
23277}
23278
23279static struct triple *x86_extract(struct compile_state *state, struct triple *ins)
23280{
23281 struct triple *mask, *shift;
23282 struct triple *val, *val_mask, *val_shift;
23283 ulong_t the_mask;
23284
23285 val = RHS(ins, 0);
23286
23287 /* Get constant for the mask value */
23288 the_mask = 1;
23289 the_mask <<= ins->u.bitfield.size;
23290 the_mask -= 1;
23291 mask = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23292 mask->u.cval = the_mask;
23293
23294 /* Get constant for the right shift value */
23295 shift = pre_triple(state, ins, OP_INTCONST, &int_type, 0, 0);
23296 shift->u.cval = ins->u.bitfield.offset;
23297
23298 /* Shift arithmetic right, to correct the sign */
23299 val_shift = val;
23300 if (shift->u.cval != 0) {
23301 int op;
23302 if (ins->op == OP_SEXTRACT) {
23303 op = OP_SSR;
23304 } else {
23305 op = OP_USR;
23306 }
23307 val_shift = pre_triple(state, ins, op, val->type, val, shift);
23308 use_triple(val, val_shift);
23309 use_triple(shift, val_shift);
23310 }
23311
23312 /* Finally mask the value */
23313 val_mask = pre_triple(state, ins, OP_AND, ins->type, val_shift, mask);
23314 use_triple(val_shift, val_mask);
23315 use_triple(mask, val_mask);
23316
23317 /* Move all of the users over to the new expression */
23318 propogate_use(state, ins, val_mask);
23319
23320 /* Release the original instruction */
23321 release_triple(state, ins);
23322
23323 return mask;
23324
23325}
23326
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023327static struct triple *transform_to_arch_instruction(
23328 struct compile_state *state, struct triple *ins)
Eric Biedermanb138ac82003-04-22 18:44:01 +000023329{
23330 /* Transform from generic 3 address instructions
23331 * to archtecture specific instructions.
Eric Biedermand1ea5392003-06-28 06:49:45 +000023332 * And apply architecture specific constraints to instructions.
Eric Biedermanb138ac82003-04-22 18:44:01 +000023333 * Copies are inserted to preserve the register flexibility
23334 * of 3 address instructions.
23335 */
Eric Biederman90089602004-05-28 14:11:54 +000023336 struct triple *next, *value;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023337 size_t size;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023338 next = ins->next;
23339 switch(ins->op) {
23340 case OP_INTCONST:
23341 ins->template_id = TEMPLATE_INTCONST32;
23342 if (ins->u.cval < 256) {
23343 ins->template_id = TEMPLATE_INTCONST8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023344 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023345 break;
23346 case OP_ADDRCONST:
23347 ins->template_id = TEMPLATE_INTCONST32;
23348 break;
Eric Biederman90089602004-05-28 14:11:54 +000023349 case OP_UNKNOWNVAL:
23350 ins->template_id = TEMPLATE_UNKNOWNVAL;
23351 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023352 case OP_NOOP:
23353 case OP_SDECL:
23354 case OP_BLOBCONST:
23355 case OP_LABEL:
23356 ins->template_id = TEMPLATE_NOP;
23357 break;
23358 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000023359 case OP_CONVERT:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023360 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023361 value = RHS(ins, 0);
23362 if (is_imm8(value) && (size <= SIZEOF_I8)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023363 ins->template_id = TEMPLATE_COPY_IMM8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023364 }
Eric Biederman90089602004-05-28 14:11:54 +000023365 else if (is_imm16(value) && (size <= SIZEOF_I16)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023366 ins->template_id = TEMPLATE_COPY_IMM16;
23367 }
Eric Biederman90089602004-05-28 14:11:54 +000023368 else if (is_imm32(value) && (size <= SIZEOF_I32)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023369 ins->template_id = TEMPLATE_COPY_IMM32;
23370 }
Eric Biederman90089602004-05-28 14:11:54 +000023371 else if (is_const(value)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023372 internal_error(state, ins, "bad constant passed to copy");
23373 }
Eric Biederman90089602004-05-28 14:11:54 +000023374 else if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023375 ins->template_id = TEMPLATE_COPY8_REG;
23376 }
Eric Biederman90089602004-05-28 14:11:54 +000023377 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023378 ins->template_id = TEMPLATE_COPY16_REG;
23379 }
Eric Biederman90089602004-05-28 14:11:54 +000023380 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023381 ins->template_id = TEMPLATE_COPY32_REG;
23382 }
23383 else {
23384 internal_error(state, ins, "bad type passed to copy");
23385 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023386 break;
23387 case OP_PHI:
Eric Biedermand1ea5392003-06-28 06:49:45 +000023388 size = size_of(state, ins->type);
Eric Biederman90089602004-05-28 14:11:54 +000023389 if (size <= SIZEOF_I8) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023390 ins->template_id = TEMPLATE_PHI8;
23391 }
Eric Biederman90089602004-05-28 14:11:54 +000023392 else if (size <= SIZEOF_I16) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023393 ins->template_id = TEMPLATE_PHI16;
23394 }
Eric Biederman90089602004-05-28 14:11:54 +000023395 else if (size <= SIZEOF_I32) {
Eric Biedermand1ea5392003-06-28 06:49:45 +000023396 ins->template_id = TEMPLATE_PHI32;
23397 }
23398 else {
23399 internal_error(state, ins, "bad type passed to phi");
23400 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023401 break;
Eric Biederman90089602004-05-28 14:11:54 +000023402 case OP_ADECL:
23403 /* Adecls should always be treated as dead code and
23404 * removed. If we are not optimizing they may linger.
23405 */
23406 if (!noop_adecl(ins)) {
23407 internal_error(state, ins, "adecl remains?");
23408 }
23409 ins->template_id = TEMPLATE_NOP;
23410 next = after_lhs(state, ins);
23411 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023412 case OP_STORE:
23413 switch(ins->type->type & TYPE_MASK) {
23414 case TYPE_CHAR: case TYPE_UCHAR:
23415 ins->template_id = TEMPLATE_STORE8;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023416 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023417 case TYPE_SHORT: case TYPE_USHORT:
23418 ins->template_id = TEMPLATE_STORE16;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023419 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023420 case TYPE_INT: case TYPE_UINT:
23421 case TYPE_LONG: case TYPE_ULONG:
23422 case TYPE_POINTER:
23423 ins->template_id = TEMPLATE_STORE32;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023424 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023425 default:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023426 internal_error(state, ins, "unknown type in store");
Eric Biedermanb138ac82003-04-22 18:44:01 +000023427 break;
23428 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023429 break;
23430 case OP_LOAD:
23431 switch(ins->type->type & TYPE_MASK) {
23432 case TYPE_CHAR: case TYPE_UCHAR:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023433 case TYPE_SHORT: case TYPE_USHORT:
23434 case TYPE_INT: case TYPE_UINT:
23435 case TYPE_LONG: case TYPE_ULONG:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023436 case TYPE_POINTER:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023437 break;
23438 default:
23439 internal_error(state, ins, "unknown type in load");
23440 break;
23441 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000023442 ins->template_id = TEMPLATE_LOAD32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023443 break;
23444 case OP_ADD:
23445 case OP_SUB:
23446 case OP_AND:
23447 case OP_XOR:
23448 case OP_OR:
23449 case OP_SMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023450 ins->template_id = TEMPLATE_BINARY32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023451 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023452 ins->template_id = TEMPLATE_BINARY32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023453 }
23454 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023455 case OP_SDIVT:
23456 case OP_UDIVT:
23457 ins->template_id = TEMPLATE_DIV32;
23458 next = after_lhs(state, ins);
23459 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023460 case OP_UMUL:
Eric Biederman530b5192003-07-01 10:05:30 +000023461 ins->template_id = TEMPLATE_UMUL32;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023462 break;
23463 case OP_UDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023464 next = mod_div(state, ins, OP_UDIVT, 0);
23465 break;
Eric Biedermand1ea5392003-06-28 06:49:45 +000023466 case OP_SDIV:
Eric Biederman530b5192003-07-01 10:05:30 +000023467 next = mod_div(state, ins, OP_SDIVT, 0);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023468 break;
23469 case OP_UMOD:
Eric Biederman530b5192003-07-01 10:05:30 +000023470 next = mod_div(state, ins, OP_UDIVT, 1);
Eric Biedermand1ea5392003-06-28 06:49:45 +000023471 break;
Eric Biederman530b5192003-07-01 10:05:30 +000023472 case OP_SMOD:
23473 next = mod_div(state, ins, OP_SDIVT, 1);
23474 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023475 case OP_SL:
23476 case OP_SSR:
23477 case OP_USR:
Eric Biederman530b5192003-07-01 10:05:30 +000023478 ins->template_id = TEMPLATE_SL32_CL;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023479 if (get_imm8(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023480 ins->template_id = TEMPLATE_SL32_IMM;
Eric Biederman90089602004-05-28 14:11:54 +000023481 } else if (size_of(state, RHS(ins, 1)->type) > SIZEOF_CHAR) {
23482 typed_pre_copy(state, &uchar_type, ins, 1);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023483 }
23484 break;
23485 case OP_INVERT:
23486 case OP_NEG:
Eric Biederman530b5192003-07-01 10:05:30 +000023487 ins->template_id = TEMPLATE_UNARY32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023488 break;
23489 case OP_EQ:
23490 bool_cmp(state, ins, OP_CMP, OP_JMP_EQ, OP_SET_EQ);
23491 break;
23492 case OP_NOTEQ:
23493 bool_cmp(state, ins, OP_CMP, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23494 break;
23495 case OP_SLESS:
23496 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESS, OP_SET_SLESS);
23497 break;
23498 case OP_ULESS:
23499 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESS, OP_SET_ULESS);
23500 break;
23501 case OP_SMORE:
23502 bool_cmp(state, ins, OP_CMP, OP_JMP_SMORE, OP_SET_SMORE);
23503 break;
23504 case OP_UMORE:
23505 bool_cmp(state, ins, OP_CMP, OP_JMP_UMORE, OP_SET_UMORE);
23506 break;
23507 case OP_SLESSEQ:
23508 bool_cmp(state, ins, OP_CMP, OP_JMP_SLESSEQ, OP_SET_SLESSEQ);
23509 break;
23510 case OP_ULESSEQ:
23511 bool_cmp(state, ins, OP_CMP, OP_JMP_ULESSEQ, OP_SET_ULESSEQ);
23512 break;
23513 case OP_SMOREEQ:
23514 bool_cmp(state, ins, OP_CMP, OP_JMP_SMOREEQ, OP_SET_SMOREEQ);
23515 break;
23516 case OP_UMOREEQ:
23517 bool_cmp(state, ins, OP_CMP, OP_JMP_UMOREEQ, OP_SET_UMOREEQ);
23518 break;
23519 case OP_LTRUE:
23520 bool_cmp(state, ins, OP_TEST, OP_JMP_NOTEQ, OP_SET_NOTEQ);
23521 break;
23522 case OP_LFALSE:
23523 bool_cmp(state, ins, OP_TEST, OP_JMP_EQ, OP_SET_EQ);
23524 break;
23525 case OP_BRANCH:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023526 ins->op = OP_JMP;
23527 ins->template_id = TEMPLATE_NOP;
23528 break;
23529 case OP_CBRANCH:
23530 fixup_branch(state, ins, OP_JMP_NOTEQ, OP_TEST,
23531 RHS(ins, 0)->type, RHS(ins, 0), 0);
23532 break;
23533 case OP_CALL:
23534 ins->template_id = TEMPLATE_NOP;
23535 break;
23536 case OP_RET:
23537 ins->template_id = TEMPLATE_RET;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023538 break;
23539 case OP_INB:
23540 case OP_INW:
23541 case OP_INL:
23542 switch(ins->op) {
23543 case OP_INB: ins->template_id = TEMPLATE_INB_DX; break;
23544 case OP_INW: ins->template_id = TEMPLATE_INW_DX; break;
23545 case OP_INL: ins->template_id = TEMPLATE_INL_DX; break;
23546 }
23547 if (get_imm8(ins, &RHS(ins, 0))) {
23548 ins->template_id += 1;
23549 }
23550 break;
23551 case OP_OUTB:
23552 case OP_OUTW:
23553 case OP_OUTL:
23554 switch(ins->op) {
23555 case OP_OUTB: ins->template_id = TEMPLATE_OUTB_DX; break;
23556 case OP_OUTW: ins->template_id = TEMPLATE_OUTW_DX; break;
23557 case OP_OUTL: ins->template_id = TEMPLATE_OUTL_DX; break;
23558 }
23559 if (get_imm8(ins, &RHS(ins, 1))) {
23560 ins->template_id += 1;
23561 }
23562 break;
23563 case OP_BSF:
23564 case OP_BSR:
23565 ins->template_id = TEMPLATE_BSF;
23566 break;
23567 case OP_RDMSR:
23568 ins->template_id = TEMPLATE_RDMSR;
23569 next = after_lhs(state, ins);
23570 break;
23571 case OP_WRMSR:
23572 ins->template_id = TEMPLATE_WRMSR;
23573 break;
23574 case OP_HLT:
23575 ins->template_id = TEMPLATE_NOP;
23576 break;
23577 case OP_ASM:
23578 ins->template_id = TEMPLATE_NOP;
23579 next = after_lhs(state, ins);
23580 break;
23581 /* Already transformed instructions */
23582 case OP_TEST:
Eric Biederman530b5192003-07-01 10:05:30 +000023583 ins->template_id = TEMPLATE_TEST32;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023584 break;
23585 case OP_CMP:
Eric Biederman530b5192003-07-01 10:05:30 +000023586 ins->template_id = TEMPLATE_CMP32_REG;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023587 if (get_imm32(ins, &RHS(ins, 1))) {
Eric Biederman530b5192003-07-01 10:05:30 +000023588 ins->template_id = TEMPLATE_CMP32_IMM;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023589 }
23590 break;
Eric Biederman83b991a2003-10-11 06:20:25 +000023591 case OP_JMP:
23592 ins->template_id = TEMPLATE_NOP;
23593 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023594 case OP_JMP_EQ: case OP_JMP_NOTEQ:
23595 case OP_JMP_SLESS: case OP_JMP_ULESS:
23596 case OP_JMP_SMORE: case OP_JMP_UMORE:
23597 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
23598 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
23599 ins->template_id = TEMPLATE_JMP;
23600 break;
23601 case OP_SET_EQ: case OP_SET_NOTEQ:
23602 case OP_SET_SLESS: case OP_SET_ULESS:
23603 case OP_SET_SMORE: case OP_SET_UMORE:
23604 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
23605 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
23606 ins->template_id = TEMPLATE_SET;
23607 break;
Eric Biederman90089602004-05-28 14:11:54 +000023608 case OP_DEPOSIT:
23609 next = x86_deposit(state, ins);
23610 break;
23611 case OP_SEXTRACT:
23612 case OP_UEXTRACT:
23613 next = x86_extract(state, ins);
23614 break;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023615 /* Unhandled instructions */
23616 case OP_PIECE:
23617 default:
Eric Biederman90089602004-05-28 14:11:54 +000023618 internal_error(state, ins, "unhandled ins: %d %s",
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023619 ins->op, tops(ins->op));
23620 break;
23621 }
23622 return next;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023623}
23624
Eric Biederman530b5192003-07-01 10:05:30 +000023625static long next_label(struct compile_state *state)
23626{
Eric Biederman90089602004-05-28 14:11:54 +000023627 static long label_counter = 1000;
Eric Biederman530b5192003-07-01 10:05:30 +000023628 return ++label_counter;
23629}
Eric Biedermanb138ac82003-04-22 18:44:01 +000023630static void generate_local_labels(struct compile_state *state)
23631{
23632 struct triple *first, *label;
Eric Biederman83b991a2003-10-11 06:20:25 +000023633 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023634 label = first;
23635 do {
23636 if ((label->op == OP_LABEL) ||
23637 (label->op == OP_SDECL)) {
23638 if (label->use) {
Eric Biederman530b5192003-07-01 10:05:30 +000023639 label->u.cval = next_label(state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023640 } else {
23641 label->u.cval = 0;
23642 }
23643
23644 }
23645 label = label->next;
23646 } while(label != first);
23647}
23648
23649static int check_reg(struct compile_state *state,
23650 struct triple *triple, int classes)
23651{
23652 unsigned mask;
23653 int reg;
23654 reg = ID_REG(triple->id);
23655 if (reg == REG_UNSET) {
23656 internal_error(state, triple, "register not set");
23657 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000023658 mask = arch_reg_regcm(state, reg);
23659 if (!(classes & mask)) {
23660 internal_error(state, triple, "reg %d in wrong class",
23661 reg);
23662 }
23663 return reg;
23664}
23665
Eric Biederman90089602004-05-28 14:11:54 +000023666
Eric Biederman530b5192003-07-01 10:05:30 +000023667#if REG_XMM7 != 44
23668#error "Registers have renumberd fix arch_reg_str"
23669#endif
Eric Biederman90089602004-05-28 14:11:54 +000023670static const char *arch_regs[] = {
23671 "%unset",
23672 "%unneeded",
23673 "%eflags",
23674 "%al", "%bl", "%cl", "%dl", "%ah", "%bh", "%ch", "%dh",
23675 "%ax", "%bx", "%cx", "%dx", "%si", "%di", "%bp", "%sp",
23676 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp",
23677 "%edx:%eax",
23678 "%dx:%ax",
23679 "%mm0", "%mm1", "%mm2", "%mm3", "%mm4", "%mm5", "%mm6", "%mm7",
23680 "%xmm0", "%xmm1", "%xmm2", "%xmm3",
23681 "%xmm4", "%xmm5", "%xmm6", "%xmm7",
23682};
23683static const char *arch_reg_str(int reg)
23684{
Eric Biedermanb138ac82003-04-22 18:44:01 +000023685 if (!((reg >= REG_EFLAGS) && (reg <= REG_XMM7))) {
23686 reg = 0;
23687 }
Eric Biederman90089602004-05-28 14:11:54 +000023688 return arch_regs[reg];
Eric Biedermanb138ac82003-04-22 18:44:01 +000023689}
23690
23691static const char *reg(struct compile_state *state, struct triple *triple,
23692 int classes)
23693{
23694 int reg;
23695 reg = check_reg(state, triple, classes);
23696 return arch_reg_str(reg);
23697}
23698
Eric Biederman90089602004-05-28 14:11:54 +000023699static int arch_reg_size(int reg)
23700{
23701 int size;
23702 size = 0;
23703 if (reg == REG_EFLAGS) {
23704 size = 32;
23705 }
23706 else if ((reg >= REG_AL) && (reg <= REG_DH)) {
23707 size = 8;
23708 }
23709 else if ((reg >= REG_AX) && (reg <= REG_SP)) {
23710 size = 16;
23711 }
23712 else if ((reg >= REG_EAX) && (reg <= REG_ESP)) {
23713 size = 32;
23714 }
23715 else if (reg == REG_EDXEAX) {
23716 size = 64;
23717 }
23718 else if (reg == REG_DXAX) {
23719 size = 32;
23720 }
23721 else if ((reg >= REG_MMX0) && (reg <= REG_MMX7)) {
23722 size = 64;
23723 }
23724 else if ((reg >= REG_XMM0) && (reg <= REG_XMM7)) {
23725 size = 128;
23726 }
23727 return size;
23728}
23729
23730static int reg_size(struct compile_state *state, struct triple *ins)
23731{
23732 int reg;
23733 reg = ID_REG(ins->id);
23734 if (reg == REG_UNSET) {
23735 internal_error(state, ins, "register not set");
23736 }
23737 return arch_reg_size(reg);
23738}
23739
23740
23741
Eric Biedermanb138ac82003-04-22 18:44:01 +000023742const char *type_suffix(struct compile_state *state, struct type *type)
23743{
23744 const char *suffix;
23745 switch(size_of(state, type)) {
Eric Biederman90089602004-05-28 14:11:54 +000023746 case SIZEOF_I8: suffix = "b"; break;
23747 case SIZEOF_I16: suffix = "w"; break;
23748 case SIZEOF_I32: suffix = "l"; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023749 default:
23750 internal_error(state, 0, "unknown suffix");
23751 suffix = 0;
23752 break;
23753 }
23754 return suffix;
23755}
23756
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023757static void print_const_val(
23758 struct compile_state *state, struct triple *ins, FILE *fp)
23759{
23760 switch(ins->op) {
23761 case OP_INTCONST:
23762 fprintf(fp, " $%ld ",
Eric Biederman83b991a2003-10-11 06:20:25 +000023763 (long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023764 break;
23765 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023766 if ((MISC(ins, 0)->op != OP_SDECL) &&
23767 (MISC(ins, 0)->op != OP_LABEL))
23768 {
Eric Biederman830c9882003-07-04 00:27:33 +000023769 internal_error(state, ins, "bad base for addrconst");
23770 }
23771 if (MISC(ins, 0)->u.cval <= 0) {
23772 internal_error(state, ins, "unlabeled constant");
23773 }
Eric Biederman05f26fc2003-06-11 21:55:00 +000023774 fprintf(fp, " $L%s%lu+%lu ",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023775 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023776 (unsigned long)(MISC(ins, 0)->u.cval),
23777 (unsigned long)(ins->u.cval));
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023778 break;
23779 default:
23780 internal_error(state, ins, "unknown constant type");
23781 break;
23782 }
23783}
23784
Eric Biederman530b5192003-07-01 10:05:30 +000023785static void print_const(struct compile_state *state,
23786 struct triple *ins, FILE *fp)
23787{
23788 switch(ins->op) {
23789 case OP_INTCONST:
23790 switch(ins->type->type & TYPE_MASK) {
23791 case TYPE_CHAR:
23792 case TYPE_UCHAR:
Eric Biederman83b991a2003-10-11 06:20:25 +000023793 fprintf(fp, ".byte 0x%02lx\n",
23794 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023795 break;
23796 case TYPE_SHORT:
23797 case TYPE_USHORT:
Eric Biederman83b991a2003-10-11 06:20:25 +000023798 fprintf(fp, ".short 0x%04lx\n",
23799 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023800 break;
23801 case TYPE_INT:
23802 case TYPE_UINT:
23803 case TYPE_LONG:
23804 case TYPE_ULONG:
Eric Biederman5cd81732004-03-11 15:01:31 +000023805 case TYPE_POINTER:
Eric Biederman83b991a2003-10-11 06:20:25 +000023806 fprintf(fp, ".int %lu\n",
23807 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023808 break;
23809 default:
Eric Biederman90089602004-05-28 14:11:54 +000023810 fprintf(state->errout, "type: ");
23811 name_of(state->errout, ins->type);
23812 fprintf(state->errout, "\n");
23813 internal_error(state, ins, "Unknown constant type. Val: %lu",
23814 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023815 }
Eric Biederman90089602004-05-28 14:11:54 +000023816
Eric Biederman530b5192003-07-01 10:05:30 +000023817 break;
23818 case OP_ADDRCONST:
Eric Biederman5ade04a2003-10-22 04:03:46 +000023819 if ((MISC(ins, 0)->op != OP_SDECL) &&
23820 (MISC(ins, 0)->op != OP_LABEL)) {
Eric Biederman830c9882003-07-04 00:27:33 +000023821 internal_error(state, ins, "bad base for addrconst");
23822 }
23823 if (MISC(ins, 0)->u.cval <= 0) {
23824 internal_error(state, ins, "unlabeled constant");
23825 }
23826 fprintf(fp, ".int L%s%lu+%lu\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000023827 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000023828 (unsigned long)(MISC(ins, 0)->u.cval),
23829 (unsigned long)(ins->u.cval));
Eric Biederman530b5192003-07-01 10:05:30 +000023830 break;
23831 case OP_BLOBCONST:
23832 {
23833 unsigned char *blob;
23834 size_t size, i;
Eric Biederman90089602004-05-28 14:11:54 +000023835 size = size_of_in_bytes(state, ins->type);
Eric Biederman530b5192003-07-01 10:05:30 +000023836 blob = ins->u.blob;
23837 for(i = 0; i < size; i++) {
23838 fprintf(fp, ".byte 0x%02x\n",
23839 blob[i]);
23840 }
23841 break;
23842 }
23843 default:
23844 internal_error(state, ins, "Unknown constant type");
23845 break;
23846 }
23847}
23848
23849#define TEXT_SECTION ".rom.text"
23850#define DATA_SECTION ".rom.data"
23851
23852static long get_const_pool_ref(
Eric Biederman90089602004-05-28 14:11:54 +000023853 struct compile_state *state, struct triple *ins, size_t size, FILE *fp)
Eric Biederman530b5192003-07-01 10:05:30 +000023854{
Eric Biederman90089602004-05-28 14:11:54 +000023855 size_t fill_bytes;
Eric Biederman530b5192003-07-01 10:05:30 +000023856 long ref;
23857 ref = next_label(state);
23858 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000023859 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman5ade04a2003-10-22 04:03:46 +000023860 fprintf(fp, "L%s%lu:\n", state->compiler->label_prefix, ref);
Eric Biederman530b5192003-07-01 10:05:30 +000023861 print_const(state, ins, fp);
Eric Biederman90089602004-05-28 14:11:54 +000023862 fill_bytes = bits_to_bytes(size - size_of(state, ins->type));
23863 if (fill_bytes) {
Uwe Hermann312673c2009-10-27 21:49:33 +000023864 fprintf(fp, ".fill %ld, 1, 0\n", (long int)fill_bytes);
Eric Biederman90089602004-05-28 14:11:54 +000023865 }
Eric Biederman530b5192003-07-01 10:05:30 +000023866 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
23867 return ref;
23868}
23869
Eric Biederman90089602004-05-28 14:11:54 +000023870static long get_mask_pool_ref(
23871 struct compile_state *state, struct triple *ins, unsigned long mask, FILE *fp)
23872{
23873 long ref;
23874 if (mask == 0xff) {
23875 ref = 1;
23876 }
23877 else if (mask == 0xffff) {
23878 ref = 2;
23879 }
23880 else {
23881 ref = 0;
23882 internal_error(state, ins, "unhandled mask value");
23883 }
23884 return ref;
23885}
23886
Eric Biedermanb138ac82003-04-22 18:44:01 +000023887static void print_binary_op(struct compile_state *state,
23888 const char *op, struct triple *ins, FILE *fp)
23889{
23890 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023891 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023892 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023893 internal_error(state, ins, "invalid register assignment");
23894 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023895 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023896 fprintf(fp, "\t%s ", op);
23897 print_const_val(state, RHS(ins, 1), fp);
23898 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023899 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023900 }
23901 else {
23902 unsigned lmask, rmask;
23903 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023904 lreg = check_reg(state, RHS(ins, 0), mask);
23905 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023906 lmask = arch_reg_regcm(state, lreg);
23907 rmask = arch_reg_regcm(state, rreg);
23908 mask = lmask & rmask;
23909 fprintf(fp, "\t%s %s, %s\n",
23910 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023911 reg(state, RHS(ins, 1), mask),
23912 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023913 }
23914}
23915static void print_unary_op(struct compile_state *state,
23916 const char *op, struct triple *ins, FILE *fp)
23917{
23918 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023919 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023920 fprintf(fp, "\t%s %s\n",
23921 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023922 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023923}
23924
23925static void print_op_shift(struct compile_state *state,
23926 const char *op, struct triple *ins, FILE *fp)
23927{
23928 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000023929 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biederman83b991a2003-10-11 06:20:25 +000023930 if (ID_REG(RHS(ins, 0)->id) != ID_REG(ins->id)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000023931 internal_error(state, ins, "invalid register assignment");
23932 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023933 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023934 fprintf(fp, "\t%s ", op);
23935 print_const_val(state, RHS(ins, 1), fp);
23936 fprintf(fp, ", %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000023937 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023938 }
23939 else {
23940 fprintf(fp, "\t%s %s, %s\n",
23941 op,
Eric Biederman530b5192003-07-01 10:05:30 +000023942 reg(state, RHS(ins, 1), REGCM_GPR8_LO),
Eric Biederman0babc1c2003-05-09 02:39:00 +000023943 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000023944 }
23945}
23946
23947static void print_op_in(struct compile_state *state, struct triple *ins, FILE *fp)
23948{
23949 const char *op;
23950 int mask;
23951 int dreg;
23952 mask = 0;
23953 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023954 case OP_INB: op = "inb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023955 case OP_INW: op = "inw", mask = REGCM_GPR16; break;
23956 case OP_INL: op = "inl", mask = REGCM_GPR32; break;
23957 default:
23958 internal_error(state, ins, "not an in operation");
23959 op = 0;
23960 break;
23961 }
23962 dreg = check_reg(state, ins, mask);
23963 if (!reg_is_reg(state, dreg, REG_EAX)) {
23964 internal_error(state, ins, "dst != %%eax");
23965 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000023966 if (is_const(RHS(ins, 0))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000023967 fprintf(fp, "\t%s ", op);
23968 print_const_val(state, RHS(ins, 0), fp);
23969 fprintf(fp, ", %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000023970 reg(state, ins, mask));
23971 }
23972 else {
23973 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000023974 addr_reg = check_reg(state, RHS(ins, 0), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000023975 if (!reg_is_reg(state, addr_reg, REG_DX)) {
23976 internal_error(state, ins, "src != %%dx");
23977 }
23978 fprintf(fp, "\t%s %s, %s\n",
23979 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000023980 reg(state, RHS(ins, 0), REGCM_GPR16),
Eric Biedermanb138ac82003-04-22 18:44:01 +000023981 reg(state, ins, mask));
23982 }
23983}
23984
23985static void print_op_out(struct compile_state *state, struct triple *ins, FILE *fp)
23986{
23987 const char *op;
23988 int mask;
23989 int lreg;
23990 mask = 0;
23991 switch(ins->op) {
Eric Biederman530b5192003-07-01 10:05:30 +000023992 case OP_OUTB: op = "outb", mask = REGCM_GPR8_LO; break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000023993 case OP_OUTW: op = "outw", mask = REGCM_GPR16; break;
23994 case OP_OUTL: op = "outl", mask = REGCM_GPR32; break;
23995 default:
23996 internal_error(state, ins, "not an out operation");
23997 op = 0;
23998 break;
23999 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024000 lreg = check_reg(state, RHS(ins, 0), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024001 if (!reg_is_reg(state, lreg, REG_EAX)) {
24002 internal_error(state, ins, "src != %%eax");
24003 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024004 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024005 fprintf(fp, "\t%s %s,",
24006 op, reg(state, RHS(ins, 0), mask));
24007 print_const_val(state, RHS(ins, 1), fp);
24008 fprintf(fp, "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024009 }
24010 else {
24011 int addr_reg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024012 addr_reg = check_reg(state, RHS(ins, 1), REGCM_GPR16);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024013 if (!reg_is_reg(state, addr_reg, REG_DX)) {
24014 internal_error(state, ins, "dst != %%dx");
24015 }
24016 fprintf(fp, "\t%s %s, %s\n",
24017 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024018 reg(state, RHS(ins, 0), mask),
24019 reg(state, RHS(ins, 1), REGCM_GPR16));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024020 }
24021}
24022
24023static void print_op_move(struct compile_state *state,
24024 struct triple *ins, FILE *fp)
24025{
24026 /* op_move is complex because there are many types
24027 * of registers we can move between.
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024028 * Because OP_COPY will be introduced in arbitrary locations
24029 * OP_COPY must not affect flags.
Eric Biederman90089602004-05-28 14:11:54 +000024030 * OP_CONVERT can change the flags and it is the only operation
24031 * where it is expected the types in the registers can change.
Eric Biedermanb138ac82003-04-22 18:44:01 +000024032 */
24033 int omit_copy = 1; /* Is it o.k. to omit a noop copy? */
24034 struct triple *dst, *src;
Eric Biederman90089602004-05-28 14:11:54 +000024035 if (state->arch->features & X86_NOOP_COPY) {
24036 omit_copy = 0;
24037 }
24038 if ((ins->op == OP_COPY) || (ins->op == OP_CONVERT)) {
Eric Biederman0babc1c2003-05-09 02:39:00 +000024039 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024040 dst = ins;
24041 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024042 else {
24043 internal_error(state, ins, "unknown move operation");
24044 src = dst = 0;
24045 }
Eric Biederman90089602004-05-28 14:11:54 +000024046 if (reg_size(state, dst) < size_of(state, dst->type)) {
24047 internal_error(state, ins, "Invalid destination register");
24048 }
24049 if (!equiv_types(src->type, dst->type) && (dst->op == OP_COPY)) {
24050 fprintf(state->errout, "src type: ");
24051 name_of(state->errout, src->type);
24052 fprintf(state->errout, "\n");
24053 fprintf(state->errout, "dst type: ");
24054 name_of(state->errout, dst->type);
24055 fprintf(state->errout, "\n");
24056 internal_error(state, ins, "Type mismatch for OP_COPY");
24057 }
24058
Eric Biederman0babc1c2003-05-09 02:39:00 +000024059 if (!is_const(src)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024060 int src_reg, dst_reg;
24061 int src_regcm, dst_regcm;
Eric Biederman530b5192003-07-01 10:05:30 +000024062 src_reg = ID_REG(src->id);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024063 dst_reg = ID_REG(dst->id);
24064 src_regcm = arch_reg_regcm(state, src_reg);
Eric Biederman530b5192003-07-01 10:05:30 +000024065 dst_regcm = arch_reg_regcm(state, dst_reg);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024066 /* If the class is the same just move the register */
24067 if (src_regcm & dst_regcm &
Eric Biederman530b5192003-07-01 10:05:30 +000024068 (REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024069 if ((src_reg != dst_reg) || !omit_copy) {
24070 fprintf(fp, "\tmov %s, %s\n",
24071 reg(state, src, src_regcm),
24072 reg(state, dst, dst_regcm));
24073 }
24074 }
24075 /* Move 32bit to 16bit */
24076 else if ((src_regcm & REGCM_GPR32) &&
24077 (dst_regcm & REGCM_GPR16)) {
24078 src_reg = (src_reg - REGC_GPR32_FIRST) + REGC_GPR16_FIRST;
24079 if ((src_reg != dst_reg) || !omit_copy) {
24080 fprintf(fp, "\tmovw %s, %s\n",
24081 arch_reg_str(src_reg),
24082 arch_reg_str(dst_reg));
24083 }
24084 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024085 /* Move from 32bit gprs to 16bit gprs */
24086 else if ((src_regcm & REGCM_GPR32) &&
24087 (dst_regcm & REGCM_GPR16)) {
24088 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24089 if ((src_reg != dst_reg) || !omit_copy) {
24090 fprintf(fp, "\tmov %s, %s\n",
24091 arch_reg_str(src_reg),
24092 arch_reg_str(dst_reg));
24093 }
24094 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024095 /* Move 32bit to 8bit */
24096 else if ((src_regcm & REGCM_GPR32_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024097 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024098 {
24099 src_reg = (src_reg - REGC_GPR32_8_FIRST) + REGC_GPR8_FIRST;
24100 if ((src_reg != dst_reg) || !omit_copy) {
24101 fprintf(fp, "\tmovb %s, %s\n",
24102 arch_reg_str(src_reg),
24103 arch_reg_str(dst_reg));
24104 }
24105 }
24106 /* Move 16bit to 8bit */
24107 else if ((src_regcm & REGCM_GPR16_8) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024108 (dst_regcm & REGCM_GPR8_LO))
Eric Biedermanb138ac82003-04-22 18:44:01 +000024109 {
24110 src_reg = (src_reg - REGC_GPR16_8_FIRST) + REGC_GPR8_FIRST;
24111 if ((src_reg != dst_reg) || !omit_copy) {
24112 fprintf(fp, "\tmovb %s, %s\n",
24113 arch_reg_str(src_reg),
24114 arch_reg_str(dst_reg));
24115 }
24116 }
24117 /* Move 8/16bit to 16/32bit */
Eric Biederman530b5192003-07-01 10:05:30 +000024118 else if ((src_regcm & (REGCM_GPR8_LO | REGCM_GPR16)) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024119 (dst_regcm & (REGCM_GPR16 | REGCM_GPR32))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024120 const char *op;
24121 op = is_signed(src->type)? "movsx": "movzx";
24122 fprintf(fp, "\t%s %s, %s\n",
24123 op,
24124 reg(state, src, src_regcm),
24125 reg(state, dst, dst_regcm));
24126 }
24127 /* Move between sse registers */
24128 else if ((src_regcm & dst_regcm & REGCM_XMM)) {
24129 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024130 fprintf(fp, "\tmovdqa %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024131 reg(state, src, src_regcm),
24132 reg(state, dst, dst_regcm));
24133 }
24134 }
Eric Biederman530b5192003-07-01 10:05:30 +000024135 /* Move between mmx registers */
24136 else if ((src_regcm & dst_regcm & REGCM_MMX)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024137 if ((src_reg != dst_reg) || !omit_copy) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024138 fprintf(fp, "\tmovq %s, %s\n",
Eric Biedermanb138ac82003-04-22 18:44:01 +000024139 reg(state, src, src_regcm),
24140 reg(state, dst, dst_regcm));
24141 }
24142 }
Eric Biederman530b5192003-07-01 10:05:30 +000024143 /* Move from sse to mmx registers */
24144 else if ((src_regcm & REGCM_XMM) && (dst_regcm & REGCM_MMX)) {
24145 fprintf(fp, "\tmovdq2q %s, %s\n",
24146 reg(state, src, src_regcm),
24147 reg(state, dst, dst_regcm));
24148 }
24149 /* Move from mmx to sse registers */
24150 else if ((src_regcm & REGCM_MMX) && (dst_regcm & REGCM_XMM)) {
24151 fprintf(fp, "\tmovq2dq %s, %s\n",
24152 reg(state, src, src_regcm),
24153 reg(state, dst, dst_regcm));
24154 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024155 /* Move between 32bit gprs & mmx/sse registers */
24156 else if ((src_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM)) &&
24157 (dst_regcm & (REGCM_GPR32 | REGCM_MMX | REGCM_XMM))) {
24158 fprintf(fp, "\tmovd %s, %s\n",
24159 reg(state, src, src_regcm),
24160 reg(state, dst, dst_regcm));
24161 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024162 /* Move from 16bit gprs & mmx/sse registers */
24163 else if ((src_regcm & REGCM_GPR16) &&
24164 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24165 const char *op;
24166 int mid_reg;
Eric Biederman678d8162003-07-03 03:59:38 +000024167 op = is_signed(src->type)? "movsx":"movzx";
Eric Biedermand1ea5392003-06-28 06:49:45 +000024168 mid_reg = (src_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24169 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24170 op,
24171 arch_reg_str(src_reg),
24172 arch_reg_str(mid_reg),
24173 arch_reg_str(mid_reg),
24174 arch_reg_str(dst_reg));
24175 }
Eric Biedermand1ea5392003-06-28 06:49:45 +000024176 /* Move from mmx/sse registers to 16bit gprs */
24177 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24178 (dst_regcm & REGCM_GPR16)) {
24179 dst_reg = (dst_reg - REGC_GPR16_FIRST) + REGC_GPR32_FIRST;
24180 fprintf(fp, "\tmovd %s, %s\n",
24181 arch_reg_str(src_reg),
24182 arch_reg_str(dst_reg));
24183 }
Eric Biederman530b5192003-07-01 10:05:30 +000024184 /* Move from gpr to 64bit dividend */
24185 else if ((src_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) &&
24186 (dst_regcm & REGCM_DIVIDEND64)) {
24187 const char *extend;
24188 extend = is_signed(src->type)? "cltd":"movl $0, %edx";
24189 fprintf(fp, "\tmov %s, %%eax\n\t%s\n",
24190 arch_reg_str(src_reg),
24191 extend);
24192 }
24193 /* Move from 64bit gpr to gpr */
24194 else if ((src_regcm & REGCM_DIVIDEND64) &&
24195 (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO))) {
24196 if (dst_regcm & REGCM_GPR32) {
24197 src_reg = REG_EAX;
24198 }
24199 else if (dst_regcm & REGCM_GPR16) {
24200 src_reg = REG_AX;
24201 }
24202 else if (dst_regcm & REGCM_GPR8_LO) {
24203 src_reg = REG_AL;
24204 }
24205 fprintf(fp, "\tmov %s, %s\n",
24206 arch_reg_str(src_reg),
24207 arch_reg_str(dst_reg));
24208 }
24209 /* Move from mmx/sse registers to 64bit gpr */
24210 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
24211 (dst_regcm & REGCM_DIVIDEND64)) {
24212 const char *extend;
24213 extend = is_signed(src->type)? "cltd": "movl $0, %edx";
24214 fprintf(fp, "\tmovd %s, %%eax\n\t%s\n",
24215 arch_reg_str(src_reg),
24216 extend);
24217 }
24218 /* Move from 64bit gpr to mmx/sse register */
24219 else if ((src_regcm & REGCM_DIVIDEND64) &&
24220 (dst_regcm & (REGCM_XMM | REGCM_MMX))) {
24221 fprintf(fp, "\tmovd %%eax, %s\n",
24222 arch_reg_str(dst_reg));
24223 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024224#if X86_4_8BIT_GPRS
24225 /* Move from 8bit gprs to mmx/sse registers */
Eric Biederman530b5192003-07-01 10:05:30 +000024226 else if ((src_regcm & REGCM_GPR8_LO) && (src_reg <= REG_DL) &&
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024227 (dst_regcm & (REGCM_MMX | REGCM_XMM))) {
24228 const char *op;
24229 int mid_reg;
24230 op = is_signed(src->type)? "movsx":"movzx";
24231 mid_reg = (src_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24232 fprintf(fp, "\t%s %s, %s\n\tmovd %s, %s\n",
24233 op,
24234 reg(state, src, src_regcm),
24235 arch_reg_str(mid_reg),
24236 arch_reg_str(mid_reg),
24237 reg(state, dst, dst_regcm));
24238 }
24239 /* Move from mmx/sse registers and 8bit gprs */
24240 else if ((src_regcm & (REGCM_MMX | REGCM_XMM)) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024241 (dst_regcm & REGCM_GPR8_LO) && (dst_reg <= REG_DL)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024242 int mid_reg;
24243 mid_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24244 fprintf(fp, "\tmovd %s, %s\n",
24245 reg(state, src, src_regcm),
24246 arch_reg_str(mid_reg));
24247 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024248 /* Move from 32bit gprs to 8bit gprs */
24249 else if ((src_regcm & REGCM_GPR32) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024250 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024251 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR32_FIRST;
24252 if ((src_reg != dst_reg) || !omit_copy) {
24253 fprintf(fp, "\tmov %s, %s\n",
24254 arch_reg_str(src_reg),
24255 arch_reg_str(dst_reg));
24256 }
24257 }
24258 /* Move from 16bit gprs to 8bit gprs */
24259 else if ((src_regcm & REGCM_GPR16) &&
Eric Biederman530b5192003-07-01 10:05:30 +000024260 (dst_regcm & REGCM_GPR8_LO)) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024261 dst_reg = (dst_reg - REGC_GPR8_FIRST) + REGC_GPR16_FIRST;
24262 if ((src_reg != dst_reg) || !omit_copy) {
24263 fprintf(fp, "\tmov %s, %s\n",
24264 arch_reg_str(src_reg),
24265 arch_reg_str(dst_reg));
24266 }
24267 }
24268#endif /* X86_4_8BIT_GPRS */
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000024269 /* Move from %eax:%edx to %eax:%edx */
24270 else if ((src_regcm & REGCM_DIVIDEND64) &&
24271 (dst_regcm & REGCM_DIVIDEND64) &&
24272 (src_reg == dst_reg)) {
24273 if (!omit_copy) {
24274 fprintf(fp, "\t/*mov %s, %s*/\n",
24275 arch_reg_str(src_reg),
24276 arch_reg_str(dst_reg));
24277 }
24278 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024279 else {
Eric Biederman90089602004-05-28 14:11:54 +000024280 if ((src_regcm & ~REGCM_FLAGS) == 0) {
24281 internal_error(state, ins, "attempt to copy from %%eflags!");
24282 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024283 internal_error(state, ins, "unknown copy type");
24284 }
24285 }
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024286 else {
Eric Biederman90089602004-05-28 14:11:54 +000024287 size_t dst_size;
Eric Biederman530b5192003-07-01 10:05:30 +000024288 int dst_reg;
24289 int dst_regcm;
Eric Biederman90089602004-05-28 14:11:54 +000024290 dst_size = size_of(state, dst->type);
Eric Biederman530b5192003-07-01 10:05:30 +000024291 dst_reg = ID_REG(dst->id);
24292 dst_regcm = arch_reg_regcm(state, dst_reg);
24293 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24294 fprintf(fp, "\tmov ");
24295 print_const_val(state, src, fp);
24296 fprintf(fp, ", %s\n",
24297 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24298 }
24299 else if (dst_regcm & REGCM_DIVIDEND64) {
Eric Biederman90089602004-05-28 14:11:54 +000024300 if (dst_size > SIZEOF_I32) {
24301 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024302 }
24303 fprintf(fp, "\tmov $0, %%edx\n");
24304 fprintf(fp, "\tmov ");
24305 print_const_val(state, src, fp);
24306 fprintf(fp, ", %%eax\n");
24307 }
24308 else if (dst_regcm & REGCM_DIVIDEND32) {
Eric Biederman90089602004-05-28 14:11:54 +000024309 if (dst_size > SIZEOF_I16) {
24310 internal_error(state, ins, "%dbit constant...", dst_size);
Eric Biederman530b5192003-07-01 10:05:30 +000024311 }
24312 fprintf(fp, "\tmov $0, %%dx\n");
24313 fprintf(fp, "\tmov ");
24314 print_const_val(state, src, fp);
24315 fprintf(fp, ", %%ax");
24316 }
24317 else if (dst_regcm & (REGCM_XMM | REGCM_MMX)) {
24318 long ref;
Eric Biederman90089602004-05-28 14:11:54 +000024319 if (dst_size > SIZEOF_I32) {
24320 internal_error(state, ins, "%d bit constant...", dst_size);
24321 }
24322 ref = get_const_pool_ref(state, src, SIZEOF_I32, fp);
Eric Biederman5ade04a2003-10-22 04:03:46 +000024323 fprintf(fp, "\tmovd L%s%lu, %s\n",
24324 state->compiler->label_prefix, ref,
Eric Biederman530b5192003-07-01 10:05:30 +000024325 reg(state, dst, (REGCM_XMM | REGCM_MMX)));
24326 }
24327 else {
24328 internal_error(state, ins, "unknown copy immediate type");
24329 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024330 }
Eric Biederman90089602004-05-28 14:11:54 +000024331 /* Leave now if this is not a type conversion */
24332 if (ins->op != OP_CONVERT) {
24333 return;
24334 }
24335 /* Now make certain I have not logically overflowed the destination */
24336 if ((size_of(state, src->type) > size_of(state, dst->type)) &&
24337 (size_of(state, dst->type) < reg_size(state, dst)))
24338 {
24339 unsigned long mask;
24340 int dst_reg;
24341 int dst_regcm;
24342 if (size_of(state, dst->type) >= 32) {
24343 fprintf(state->errout, "dst type: ");
24344 name_of(state->errout, dst->type);
24345 fprintf(state->errout, "\n");
24346 internal_error(state, dst, "unhandled dst type size");
24347 }
24348 mask = 1;
24349 mask <<= size_of(state, dst->type);
24350 mask -= 1;
24351
24352 dst_reg = ID_REG(dst->id);
24353 dst_regcm = arch_reg_regcm(state, dst_reg);
24354
24355 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24356 fprintf(fp, "\tand $0x%lx, %s\n",
24357 mask, reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24358 }
24359 else if (dst_regcm & REGCM_MMX) {
24360 long ref;
24361 ref = get_mask_pool_ref(state, dst, mask, fp);
24362 fprintf(fp, "\tpand L%s%lu, %s\n",
24363 state->compiler->label_prefix, ref,
24364 reg(state, dst, REGCM_MMX));
24365 }
24366 else if (dst_regcm & REGCM_XMM) {
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_XMM));
24372 }
24373 else {
24374 fprintf(state->errout, "dst type: ");
24375 name_of(state->errout, dst->type);
24376 fprintf(state->errout, "\n");
24377 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24378 internal_error(state, dst, "failed to trunc value: mask %lx", mask);
24379 }
24380 }
24381 /* Make certain I am properly sign extended */
24382 if ((size_of(state, src->type) < size_of(state, dst->type)) &&
24383 (is_signed(src->type)))
24384 {
24385 int bits, reg_bits, shift_bits;
24386 int dst_reg;
24387 int dst_regcm;
24388
24389 bits = size_of(state, src->type);
24390 reg_bits = reg_size(state, dst);
24391 if (reg_bits > 32) {
24392 reg_bits = 32;
24393 }
24394 shift_bits = reg_bits - size_of(state, src->type);
24395 dst_reg = ID_REG(dst->id);
24396 dst_regcm = arch_reg_regcm(state, dst_reg);
24397
24398 if (shift_bits < 0) {
24399 internal_error(state, dst, "negative shift?");
24400 }
24401
24402 if (dst_regcm & (REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO)) {
24403 fprintf(fp, "\tshl $%d, %s\n",
24404 shift_bits,
24405 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24406 fprintf(fp, "\tsar $%d, %s\n",
24407 shift_bits,
24408 reg(state, dst, REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO));
24409 }
24410 else if (dst_regcm & (REGCM_MMX | REGCM_XMM)) {
24411 fprintf(fp, "\tpslld $%d, %s\n",
24412 shift_bits,
24413 reg(state, dst, REGCM_MMX | REGCM_XMM));
24414 fprintf(fp, "\tpsrad $%d, %s\n",
24415 shift_bits,
24416 reg(state, dst, REGCM_MMX | REGCM_XMM));
24417 }
24418 else {
24419 fprintf(state->errout, "dst type: ");
24420 name_of(state->errout, dst->type);
24421 fprintf(state->errout, "\n");
24422 fprintf(state->errout, "dst: %s\n", reg(state, dst, REGCM_ALL));
24423 internal_error(state, dst, "failed to signed extend value");
24424 }
24425 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024426}
24427
24428static void print_op_load(struct compile_state *state,
24429 struct triple *ins, FILE *fp)
24430{
24431 struct triple *dst, *src;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024432 const char *op;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024433 dst = ins;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024434 src = RHS(ins, 0);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024435 if (is_const(src) || is_const(dst)) {
24436 internal_error(state, ins, "unknown load operation");
24437 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000024438 switch(ins->type->type & TYPE_MASK) {
24439 case TYPE_CHAR: op = "movsbl"; break;
24440 case TYPE_UCHAR: op = "movzbl"; break;
24441 case TYPE_SHORT: op = "movswl"; break;
24442 case TYPE_USHORT: op = "movzwl"; break;
24443 case TYPE_INT: case TYPE_UINT:
24444 case TYPE_LONG: case TYPE_ULONG:
24445 case TYPE_POINTER:
24446 op = "movl";
24447 break;
24448 default:
24449 internal_error(state, ins, "unknown type in load");
24450 op = "<invalid opcode>";
24451 break;
24452 }
24453 fprintf(fp, "\t%s (%s), %s\n",
24454 op,
Eric Biedermanb138ac82003-04-22 18:44:01 +000024455 reg(state, src, REGCM_GPR32),
Eric Biederman5ade04a2003-10-22 04:03:46 +000024456 reg(state, dst, REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024457}
24458
24459
24460static void print_op_store(struct compile_state *state,
24461 struct triple *ins, FILE *fp)
24462{
24463 struct triple *dst, *src;
Eric Biederman530b5192003-07-01 10:05:30 +000024464 dst = RHS(ins, 0);
24465 src = RHS(ins, 1);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024466 if (is_const(src) && (src->op == OP_INTCONST)) {
24467 long_t value;
24468 value = (long_t)(src->u.cval);
24469 fprintf(fp, "\tmov%s $%ld, (%s)\n",
24470 type_suffix(state, src->type),
Eric Biederman83b991a2003-10-11 06:20:25 +000024471 (long)(value),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024472 reg(state, dst, REGCM_GPR32));
24473 }
24474 else if (is_const(dst) && (dst->op == OP_INTCONST)) {
24475 fprintf(fp, "\tmov%s %s, 0x%08lx\n",
24476 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024477 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biederman83b991a2003-10-11 06:20:25 +000024478 (unsigned long)(dst->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024479 }
24480 else {
24481 if (is_const(src) || is_const(dst)) {
24482 internal_error(state, ins, "unknown store operation");
24483 }
24484 fprintf(fp, "\tmov%s %s, (%s)\n",
24485 type_suffix(state, src->type),
Eric Biederman530b5192003-07-01 10:05:30 +000024486 reg(state, src, REGCM_GPR8_LO | REGCM_GPR16 | REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024487 reg(state, dst, REGCM_GPR32));
24488 }
24489
24490
24491}
24492
24493static void print_op_smul(struct compile_state *state,
24494 struct triple *ins, FILE *fp)
24495{
Eric Biederman0babc1c2003-05-09 02:39:00 +000024496 if (!is_const(RHS(ins, 1))) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024497 fprintf(fp, "\timul %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024498 reg(state, RHS(ins, 1), REGCM_GPR32),
24499 reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024500 }
24501 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024502 fprintf(fp, "\timul ");
24503 print_const_val(state, RHS(ins, 1), fp);
24504 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), REGCM_GPR32));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024505 }
24506}
24507
24508static void print_op_cmp(struct compile_state *state,
24509 struct triple *ins, FILE *fp)
24510{
24511 unsigned mask;
24512 int dreg;
Eric Biederman530b5192003-07-01 10:05:30 +000024513 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024514 dreg = check_reg(state, ins, REGCM_FLAGS);
24515 if (!reg_is_reg(state, dreg, REG_EFLAGS)) {
24516 internal_error(state, ins, "bad dest register for cmp");
24517 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024518 if (is_const(RHS(ins, 1))) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024519 fprintf(fp, "\tcmp ");
24520 print_const_val(state, RHS(ins, 1), fp);
24521 fprintf(fp, ", %s\n", reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024522 }
24523 else {
24524 unsigned lmask, rmask;
24525 int lreg, rreg;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024526 lreg = check_reg(state, RHS(ins, 0), mask);
24527 rreg = check_reg(state, RHS(ins, 1), mask);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024528 lmask = arch_reg_regcm(state, lreg);
24529 rmask = arch_reg_regcm(state, rreg);
24530 mask = lmask & rmask;
24531 fprintf(fp, "\tcmp %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024532 reg(state, RHS(ins, 1), mask),
24533 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024534 }
24535}
24536
24537static void print_op_test(struct compile_state *state,
24538 struct triple *ins, FILE *fp)
24539{
24540 unsigned mask;
Eric Biederman530b5192003-07-01 10:05:30 +000024541 mask = REGCM_GPR32 | REGCM_GPR16 | REGCM_GPR8_LO;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024542 fprintf(fp, "\ttest %s, %s\n",
Eric Biederman0babc1c2003-05-09 02:39:00 +000024543 reg(state, RHS(ins, 0), mask),
24544 reg(state, RHS(ins, 0), mask));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024545}
24546
24547static void print_op_branch(struct compile_state *state,
24548 struct triple *branch, FILE *fp)
24549{
24550 const char *bop = "j";
Eric Biederman5ade04a2003-10-22 04:03:46 +000024551 if ((branch->op == OP_JMP) || (branch->op == OP_CALL)) {
Eric Biederman90089602004-05-28 14:11:54 +000024552 if (branch->rhs != 0) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024553 internal_error(state, branch, "jmp with condition?");
24554 }
24555 bop = "jmp";
24556 }
24557 else {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024558 struct triple *ptr;
Eric Biederman90089602004-05-28 14:11:54 +000024559 if (branch->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024560 internal_error(state, branch, "jmpcc without condition?");
24561 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024562 check_reg(state, RHS(branch, 0), REGCM_FLAGS);
24563 if ((RHS(branch, 0)->op != OP_CMP) &&
24564 (RHS(branch, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024565 internal_error(state, branch, "bad branch test");
24566 }
Stefan Reinauer50542a82007-10-24 11:14:14 +000024567#if DEBUG_ROMCC_WARNINGS
Eric Biedermanb138ac82003-04-22 18:44:01 +000024568#warning "FIXME I have observed instructions between the test and branch instructions"
Stefan Reinauer50542a82007-10-24 11:14:14 +000024569#endif
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024570 ptr = RHS(branch, 0);
24571 for(ptr = RHS(branch, 0)->next; ptr != branch; ptr = ptr->next) {
24572 if (ptr->op != OP_COPY) {
24573 internal_error(state, branch, "branch does not follow test");
24574 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024575 }
24576 switch(branch->op) {
24577 case OP_JMP_EQ: bop = "jz"; break;
24578 case OP_JMP_NOTEQ: bop = "jnz"; break;
24579 case OP_JMP_SLESS: bop = "jl"; break;
24580 case OP_JMP_ULESS: bop = "jb"; break;
24581 case OP_JMP_SMORE: bop = "jg"; break;
24582 case OP_JMP_UMORE: bop = "ja"; break;
24583 case OP_JMP_SLESSEQ: bop = "jle"; break;
24584 case OP_JMP_ULESSEQ: bop = "jbe"; break;
24585 case OP_JMP_SMOREEQ: bop = "jge"; break;
24586 case OP_JMP_UMOREEQ: bop = "jae"; break;
24587 default:
24588 internal_error(state, branch, "Invalid branch op");
24589 break;
24590 }
24591
24592 }
Eric Biederman90089602004-05-28 14:11:54 +000024593#if 1
24594 if (branch->op == OP_CALL) {
24595 fprintf(fp, "\t/* call */\n");
24596 }
24597#endif
Eric Biederman05f26fc2003-06-11 21:55:00 +000024598 fprintf(fp, "\t%s L%s%lu\n",
24599 bop,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024600 state->compiler->label_prefix,
Eric Biederman83b991a2003-10-11 06:20:25 +000024601 (unsigned long)(TARG(branch, 0)->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024602}
24603
Eric Biederman5ade04a2003-10-22 04:03:46 +000024604static void print_op_ret(struct compile_state *state,
24605 struct triple *branch, FILE *fp)
24606{
24607 fprintf(fp, "\tjmp *%s\n",
24608 reg(state, RHS(branch, 0), REGCM_GPR32));
24609}
24610
Eric Biedermanb138ac82003-04-22 18:44:01 +000024611static void print_op_set(struct compile_state *state,
24612 struct triple *set, FILE *fp)
24613{
24614 const char *sop = "set";
Eric Biederman90089602004-05-28 14:11:54 +000024615 if (set->rhs != 1) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024616 internal_error(state, set, "setcc without condition?");
24617 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024618 check_reg(state, RHS(set, 0), REGCM_FLAGS);
24619 if ((RHS(set, 0)->op != OP_CMP) &&
24620 (RHS(set, 0)->op != OP_TEST)) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024621 internal_error(state, set, "bad set test");
24622 }
Eric Biederman0babc1c2003-05-09 02:39:00 +000024623 if (RHS(set, 0)->next != set) {
Eric Biedermanb138ac82003-04-22 18:44:01 +000024624 internal_error(state, set, "set does not follow test");
24625 }
24626 switch(set->op) {
24627 case OP_SET_EQ: sop = "setz"; break;
24628 case OP_SET_NOTEQ: sop = "setnz"; break;
24629 case OP_SET_SLESS: sop = "setl"; break;
24630 case OP_SET_ULESS: sop = "setb"; break;
24631 case OP_SET_SMORE: sop = "setg"; break;
24632 case OP_SET_UMORE: sop = "seta"; break;
24633 case OP_SET_SLESSEQ: sop = "setle"; break;
24634 case OP_SET_ULESSEQ: sop = "setbe"; break;
24635 case OP_SET_SMOREEQ: sop = "setge"; break;
24636 case OP_SET_UMOREEQ: sop = "setae"; break;
24637 default:
24638 internal_error(state, set, "Invalid set op");
24639 break;
24640 }
24641 fprintf(fp, "\t%s %s\n",
Eric Biederman530b5192003-07-01 10:05:30 +000024642 sop, reg(state, set, REGCM_GPR8_LO));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024643}
24644
24645static void print_op_bit_scan(struct compile_state *state,
24646 struct triple *ins, FILE *fp)
24647{
24648 const char *op;
24649 switch(ins->op) {
24650 case OP_BSF: op = "bsf"; break;
24651 case OP_BSR: op = "bsr"; break;
24652 default:
24653 internal_error(state, ins, "unknown bit scan");
24654 op = 0;
24655 break;
24656 }
24657 fprintf(fp,
24658 "\t%s %s, %s\n"
24659 "\tjnz 1f\n"
24660 "\tmovl $-1, %s\n"
24661 "1:\n",
24662 op,
Eric Biederman0babc1c2003-05-09 02:39:00 +000024663 reg(state, RHS(ins, 0), REGCM_GPR32),
Eric Biedermanb138ac82003-04-22 18:44:01 +000024664 reg(state, ins, REGCM_GPR32),
24665 reg(state, ins, REGCM_GPR32));
24666}
24667
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024668
Eric Biedermanb138ac82003-04-22 18:44:01 +000024669static void print_sdecl(struct compile_state *state,
24670 struct triple *ins, FILE *fp)
24671{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024672 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
Uwe Hermann312673c2009-10-27 21:49:33 +000024673 fprintf(fp, ".balign %ld\n", (long int)align_of_in_bytes(state, ins->type));
Eric Biederman83b991a2003-10-11 06:20:25 +000024674 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024675 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biederman0babc1c2003-05-09 02:39:00 +000024676 print_const(state, MISC(ins, 0), fp);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024677 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000024678
24679}
24680
24681static void print_instruction(struct compile_state *state,
24682 struct triple *ins, FILE *fp)
24683{
24684 /* Assumption: after I have exted the register allocator
24685 * everything is in a valid register.
24686 */
24687 switch(ins->op) {
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024688 case OP_ASM:
24689 print_op_asm(state, ins, fp);
24690 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024691 case OP_ADD: print_binary_op(state, "add", ins, fp); break;
24692 case OP_SUB: print_binary_op(state, "sub", ins, fp); break;
24693 case OP_AND: print_binary_op(state, "and", ins, fp); break;
24694 case OP_XOR: print_binary_op(state, "xor", ins, fp); break;
24695 case OP_OR: print_binary_op(state, "or", ins, fp); break;
24696 case OP_SL: print_op_shift(state, "shl", ins, fp); break;
24697 case OP_USR: print_op_shift(state, "shr", ins, fp); break;
24698 case OP_SSR: print_op_shift(state, "sar", ins, fp); break;
24699 case OP_POS: break;
24700 case OP_NEG: print_unary_op(state, "neg", ins, fp); break;
24701 case OP_INVERT: print_unary_op(state, "not", ins, fp); break;
Eric Biederman90089602004-05-28 14:11:54 +000024702 case OP_NOOP:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024703 case OP_INTCONST:
24704 case OP_ADDRCONST:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024705 case OP_BLOBCONST:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024706 /* Don't generate anything here for constants */
24707 case OP_PHI:
24708 /* Don't generate anything for variable declarations. */
24709 break;
Eric Biederman90089602004-05-28 14:11:54 +000024710 case OP_UNKNOWNVAL:
24711 fprintf(fp, " /* unknown %s */\n",
24712 reg(state, ins, REGCM_ALL));
24713 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024714 case OP_SDECL:
24715 print_sdecl(state, ins, fp);
24716 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024717 case OP_COPY:
Eric Biederman90089602004-05-28 14:11:54 +000024718 case OP_CONVERT:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024719 print_op_move(state, ins, fp);
24720 break;
24721 case OP_LOAD:
24722 print_op_load(state, ins, fp);
24723 break;
24724 case OP_STORE:
24725 print_op_store(state, ins, fp);
24726 break;
24727 case OP_SMUL:
24728 print_op_smul(state, ins, fp);
24729 break;
24730 case OP_CMP: print_op_cmp(state, ins, fp); break;
24731 case OP_TEST: print_op_test(state, ins, fp); break;
24732 case OP_JMP:
24733 case OP_JMP_EQ: case OP_JMP_NOTEQ:
24734 case OP_JMP_SLESS: case OP_JMP_ULESS:
24735 case OP_JMP_SMORE: case OP_JMP_UMORE:
24736 case OP_JMP_SLESSEQ: case OP_JMP_ULESSEQ:
24737 case OP_JMP_SMOREEQ: case OP_JMP_UMOREEQ:
Eric Biederman5ade04a2003-10-22 04:03:46 +000024738 case OP_CALL:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024739 print_op_branch(state, ins, fp);
24740 break;
Eric Biederman5ade04a2003-10-22 04:03:46 +000024741 case OP_RET:
24742 print_op_ret(state, ins, fp);
24743 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024744 case OP_SET_EQ: case OP_SET_NOTEQ:
24745 case OP_SET_SLESS: case OP_SET_ULESS:
24746 case OP_SET_SMORE: case OP_SET_UMORE:
24747 case OP_SET_SLESSEQ: case OP_SET_ULESSEQ:
24748 case OP_SET_SMOREEQ: case OP_SET_UMOREEQ:
24749 print_op_set(state, ins, fp);
24750 break;
24751 case OP_INB: case OP_INW: case OP_INL:
24752 print_op_in(state, ins, fp);
24753 break;
24754 case OP_OUTB: case OP_OUTW: case OP_OUTL:
24755 print_op_out(state, ins, fp);
24756 break;
24757 case OP_BSF:
24758 case OP_BSR:
24759 print_op_bit_scan(state, ins, fp);
24760 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024761 case OP_RDMSR:
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024762 after_lhs(state, ins);
Eric Biederman0babc1c2003-05-09 02:39:00 +000024763 fprintf(fp, "\trdmsr\n");
24764 break;
24765 case OP_WRMSR:
24766 fprintf(fp, "\twrmsr\n");
24767 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024768 case OP_HLT:
24769 fprintf(fp, "\thlt\n");
24770 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024771 case OP_SDIVT:
24772 fprintf(fp, "\tidiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24773 break;
24774 case OP_UDIVT:
24775 fprintf(fp, "\tdiv %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24776 break;
24777 case OP_UMUL:
24778 fprintf(fp, "\tmul %s\n", reg(state, RHS(ins, 1), REGCM_GPR32));
24779 break;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024780 case OP_LABEL:
24781 if (!ins->use) {
24782 return;
24783 }
Eric Biederman83b991a2003-10-11 06:20:25 +000024784 fprintf(fp, "L%s%lu:\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024785 state->compiler->label_prefix, (unsigned long)(ins->u.cval));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024786 break;
Eric Biederman90089602004-05-28 14:11:54 +000024787 case OP_ADECL:
24788 /* Ignore adecls with no registers error otherwise */
24789 if (!noop_adecl(ins)) {
24790 internal_error(state, ins, "adecl remains?");
24791 }
24792 break;
Eric Biederman0babc1c2003-05-09 02:39:00 +000024793 /* Ignore OP_PIECE */
24794 case OP_PIECE:
24795 break;
Eric Biederman530b5192003-07-01 10:05:30 +000024796 /* Operations that should never get here */
Eric Biedermanb138ac82003-04-22 18:44:01 +000024797 case OP_SDIV: case OP_UDIV:
24798 case OP_SMOD: case OP_UMOD:
Eric Biedermanb138ac82003-04-22 18:44:01 +000024799 case OP_LTRUE: case OP_LFALSE: case OP_EQ: case OP_NOTEQ:
24800 case OP_SLESS: case OP_ULESS: case OP_SMORE: case OP_UMORE:
24801 case OP_SLESSEQ: case OP_ULESSEQ: case OP_SMOREEQ: case OP_UMOREEQ:
24802 default:
24803 internal_error(state, ins, "unknown op: %d %s",
24804 ins->op, tops(ins->op));
24805 break;
24806 }
24807}
24808
24809static void print_instructions(struct compile_state *state)
24810{
24811 struct triple *first, *ins;
24812 int print_location;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024813 struct occurance *last_occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024814 FILE *fp;
Eric Biederman530b5192003-07-01 10:05:30 +000024815 int max_inline_depth;
24816 max_inline_depth = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024817 print_location = 1;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024818 last_occurance = 0;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024819 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024820 /* Masks for common sizes */
24821 fprintf(fp, ".section \"" DATA_SECTION "\"\n");
24822 fprintf(fp, ".balign 16\n");
24823 fprintf(fp, "L%s1:\n", state->compiler->label_prefix);
24824 fprintf(fp, ".int 0xff, 0, 0, 0\n");
24825 fprintf(fp, "L%s2:\n", state->compiler->label_prefix);
24826 fprintf(fp, ".int 0xffff, 0, 0, 0\n");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024827 fprintf(fp, ".section \"" TEXT_SECTION "\"\n");
Eric Biederman83b991a2003-10-11 06:20:25 +000024828 first = state->first;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024829 ins = first;
24830 do {
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024831 if (print_location &&
24832 last_occurance != ins->occurance) {
24833 if (!ins->occurance->parent) {
24834 fprintf(fp, "\t/* %s,%s:%d.%d */\n",
Patrick Georgia84a99b2009-05-26 14:03:51 +000024835 ins->occurance->function?ins->occurance->function:"(null)",
24836 ins->occurance->filename?ins->occurance->filename:"(null)",
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024837 ins->occurance->line,
24838 ins->occurance->col);
24839 }
24840 else {
24841 struct occurance *ptr;
Eric Biederman530b5192003-07-01 10:05:30 +000024842 int inline_depth;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024843 fprintf(fp, "\t/*\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024844 inline_depth = 0;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024845 for(ptr = ins->occurance; ptr; ptr = ptr->parent) {
Eric Biederman530b5192003-07-01 10:05:30 +000024846 inline_depth++;
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024847 fprintf(fp, "\t * %s,%s:%d.%d\n",
24848 ptr->function,
24849 ptr->filename,
24850 ptr->line,
24851 ptr->col);
24852 }
24853 fprintf(fp, "\t */\n");
Eric Biederman530b5192003-07-01 10:05:30 +000024854 if (inline_depth > max_inline_depth) {
24855 max_inline_depth = inline_depth;
24856 }
Eric Biedermanf7a0ba82003-06-19 15:14:52 +000024857 }
24858 if (last_occurance) {
24859 put_occurance(last_occurance);
24860 }
24861 get_occurance(ins->occurance);
24862 last_occurance = ins->occurance;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024863 }
24864
24865 print_instruction(state, ins, fp);
24866 ins = ins->next;
24867 } while(ins != first);
Eric Biederman530b5192003-07-01 10:05:30 +000024868 if (print_location) {
24869 fprintf(fp, "/* max inline depth %d */\n",
24870 max_inline_depth);
24871 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024872}
Eric Biederman530b5192003-07-01 10:05:30 +000024873
Eric Biedermanb138ac82003-04-22 18:44:01 +000024874static void generate_code(struct compile_state *state)
24875{
24876 generate_local_labels(state);
24877 print_instructions(state);
24878
24879}
24880
Eric Biederman90089602004-05-28 14:11:54 +000024881static void print_preprocessed_tokens(struct compile_state *state)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024882{
Eric Biederman90089602004-05-28 14:11:54 +000024883 int tok;
24884 FILE *fp;
24885 int line;
24886 const char *filename;
24887 fp = state->output;
Eric Biederman90089602004-05-28 14:11:54 +000024888 filename = 0;
24889 line = 0;
24890 for(;;) {
Eric Biederman132368b2004-11-09 08:59:23 +000024891 struct file_state *file;
Eric Biederman41203d92004-11-08 09:31:09 +000024892 struct token *tk;
Eric Biederman90089602004-05-28 14:11:54 +000024893 const char *token_str;
24894 tok = peek(state);
24895 if (tok == TOK_EOF) {
24896 break;
24897 }
Eric Biederman41203d92004-11-08 09:31:09 +000024898 tk = eat(state, tok);
Eric Biederman90089602004-05-28 14:11:54 +000024899 token_str =
Eric Biedermanb138ac82003-04-22 18:44:01 +000024900 tk->ident ? tk->ident->name :
Eric Biederman90089602004-05-28 14:11:54 +000024901 tk->str_len ? tk->val.str :
24902 tokens[tk->tok];
Eric Biederman132368b2004-11-09 08:59:23 +000024903
24904 file = state->file;
24905 while(file->macro && file->prev) {
24906 file = file->prev;
24907 }
24908 if (!file->macro &&
24909 ((file->line != line) || (file->basename != filename)))
24910 {
Eric Biederman90089602004-05-28 14:11:54 +000024911 int i, col;
Eric Biederman132368b2004-11-09 08:59:23 +000024912 if ((file->basename == filename) &&
24913 (line < file->line)) {
24914 while(line < file->line) {
Eric Biederman90089602004-05-28 14:11:54 +000024915 fprintf(fp, "\n");
24916 line++;
24917 }
24918 }
24919 else {
24920 fprintf(fp, "\n#line %d \"%s\"\n",
Eric Biederman132368b2004-11-09 08:59:23 +000024921 file->line, file->basename);
Eric Biederman90089602004-05-28 14:11:54 +000024922 }
Eric Biederman132368b2004-11-09 08:59:23 +000024923 line = file->line;
24924 filename = file->basename;
24925 col = get_col(file) - strlen(token_str);
Eric Biederman90089602004-05-28 14:11:54 +000024926 for(i = 0; i < col; i++) {
24927 fprintf(fp, " ");
24928 }
24929 }
24930
24931 fprintf(fp, "%s ", token_str);
24932
24933 if (state->compiler->debug & DEBUG_TOKENS) {
24934 loc(state->dbgout, state, 0);
24935 fprintf(state->dbgout, "%s <- `%s'\n",
24936 tokens[tok], token_str);
24937 }
24938 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000024939}
24940
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024941static void compile(const char *filename,
Eric Biederman5ade04a2003-10-22 04:03:46 +000024942 struct compiler_state *compiler, struct arch_state *arch)
Eric Biedermanb138ac82003-04-22 18:44:01 +000024943{
24944 int i;
24945 struct compile_state state;
Eric Biederman83b991a2003-10-11 06:20:25 +000024946 struct triple *ptr;
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000024947 struct filelist *includes = include_filelist;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024948 memset(&state, 0, sizeof(state));
Eric Biederman5ade04a2003-10-22 04:03:46 +000024949 state.compiler = compiler;
24950 state.arch = arch;
Eric Biedermanb138ac82003-04-22 18:44:01 +000024951 state.file = 0;
24952 for(i = 0; i < sizeof(state.token)/sizeof(state.token[0]); i++) {
24953 memset(&state.token[i], 0, sizeof(state.token[i]));
24954 state.token[i].tok = -1;
24955 }
Eric Biederman90089602004-05-28 14:11:54 +000024956 /* Remember the output descriptors */
24957 state.errout = stderr;
24958 state.dbgout = stdout;
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024959 /* Remember the output filename */
Eric Biederman5ade04a2003-10-22 04:03:46 +000024960 state.output = fopen(state.compiler->ofilename, "w");
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024961 if (!state.output) {
24962 error(&state, 0, "Cannot open output file %s\n",
Eric Biederman5ade04a2003-10-22 04:03:46 +000024963 state.compiler->ofilename);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000024964 }
Eric Biederman90089602004-05-28 14:11:54 +000024965 /* Make certain a good cleanup happens */
24966 exit_state = &state;
24967 atexit(exit_cleanup);
24968
Eric Biedermanb138ac82003-04-22 18:44:01 +000024969 /* Prep the preprocessor */
24970 state.if_depth = 0;
Eric Biederman90089602004-05-28 14:11:54 +000024971 memset(state.if_bytes, 0, sizeof(state.if_bytes));
Eric Biedermanb138ac82003-04-22 18:44:01 +000024972 /* register the C keywords */
24973 register_keywords(&state);
24974 /* register the keywords the macro preprocessor knows */
24975 register_macro_keywords(&state);
Eric Biederman90089602004-05-28 14:11:54 +000024976 /* generate some builtin macros */
24977 register_builtin_macros(&state);
Eric Biedermanb138ac82003-04-22 18:44:01 +000024978 /* Memorize where some special keywords are. */
Eric Biederman90089602004-05-28 14:11:54 +000024979 state.i_switch = lookup(&state, "switch", 6);
24980 state.i_case = lookup(&state, "case", 4);
24981 state.i_continue = lookup(&state, "continue", 8);
24982 state.i_break = lookup(&state, "break", 5);
24983 state.i_default = lookup(&state, "default", 7);
24984 state.i_return = lookup(&state, "return", 6);
24985 /* Memorize where predefined macros are. */
Eric Biederman90089602004-05-28 14:11:54 +000024986 state.i___VA_ARGS__ = lookup(&state, "__VA_ARGS__", 11);
24987 state.i___FILE__ = lookup(&state, "__FILE__", 8);
24988 state.i___LINE__ = lookup(&state, "__LINE__", 8);
24989 /* Memorize where predefined identifiers are. */
24990 state.i___func__ = lookup(&state, "__func__", 8);
24991 /* Memorize where some attribute keywords are. */
24992 state.i_noinline = lookup(&state, "noinline", 8);
24993 state.i_always_inline = lookup(&state, "always_inline", 13);
24994
24995 /* Process the command line macros */
24996 process_cmdline_macros(&state);
Eric Biederman83b991a2003-10-11 06:20:25 +000024997
24998 /* Allocate beginning bounding labels for the function list */
24999 state.first = label(&state);
25000 state.first->id |= TRIPLE_FLAG_VOLATILE;
25001 use_triple(state.first, state.first);
25002 ptr = label(&state);
25003 ptr->id |= TRIPLE_FLAG_VOLATILE;
25004 use_triple(ptr, ptr);
25005 flatten(&state, state.first, ptr);
25006
Eric Biederman5ade04a2003-10-22 04:03:46 +000025007 /* Allocate a label for the pool of global variables */
25008 state.global_pool = label(&state);
25009 state.global_pool->id |= TRIPLE_FLAG_VOLATILE;
25010 flatten(&state, state.first, state.global_pool);
25011
Eric Biedermanb138ac82003-04-22 18:44:01 +000025012 /* Enter the globl definition scope */
25013 start_scope(&state);
25014 register_builtins(&state);
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025015
Eric Biedermanb138ac82003-04-22 18:44:01 +000025016 compile_file(&state, filename, 1);
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025017
25018 while (includes) {
25019 compile_file(&state, includes->filename, 1);
25020 includes=includes->next;
25021 }
Eric Biederman90089602004-05-28 14:11:54 +000025022
25023 /* Stop if all we want is preprocessor output */
Eric Biedermancb364952004-11-15 10:46:44 +000025024 if (state.compiler->flags & COMPILER_PP_ONLY) {
Eric Biederman90089602004-05-28 14:11:54 +000025025 print_preprocessed_tokens(&state);
25026 return;
25027 }
25028
Eric Biedermanb138ac82003-04-22 18:44:01 +000025029 decls(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025030
Eric Biedermanb138ac82003-04-22 18:44:01 +000025031 /* Exit the global definition scope */
25032 end_scope(&state);
25033
25034 /* Now that basic compilation has happened
25035 * optimize the intermediate code
25036 */
25037 optimize(&state);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025038
Eric Biedermanb138ac82003-04-22 18:44:01 +000025039 generate_code(&state);
Eric Biederman5ade04a2003-10-22 04:03:46 +000025040 if (state.compiler->debug) {
Eric Biederman90089602004-05-28 14:11:54 +000025041 fprintf(state.errout, "done\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025042 }
Eric Biederman90089602004-05-28 14:11:54 +000025043 exit_state = 0;
Eric Biedermanb138ac82003-04-22 18:44:01 +000025044}
25045
Eric Biederman90089602004-05-28 14:11:54 +000025046static void version(FILE *fp)
Eric Biedermanb138ac82003-04-22 18:44:01 +000025047{
Eric Biederman90089602004-05-28 14:11:54 +000025048 fprintf(fp, "romcc " VERSION " released " RELEASE_DATE "\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025049}
25050
25051static void usage(void)
25052{
Eric Biederman90089602004-05-28 14:11:54 +000025053 FILE *fp = stdout;
25054 version(fp);
25055 fprintf(fp,
25056 "\nUsage: romcc [options] <source>.c\n"
25057 "Compile a C source file generating a binary that does not implicilty use RAM\n"
25058 "Options: \n"
25059 "-o <output file name>\n"
25060 "-f<option> Specify a generic compiler option\n"
25061 "-m<option> Specify a arch dependent option\n"
25062 "-- Specify this is the last option\n"
25063 "\nGeneric compiler options:\n"
25064 );
25065 compiler_usage(fp);
25066 fprintf(fp,
25067 "\nArchitecture compiler options:\n"
25068 );
25069 arch_usage(fp);
25070 fprintf(fp,
25071 "\n"
Eric Biedermanb138ac82003-04-22 18:44:01 +000025072 );
25073}
25074
25075static void arg_error(char *fmt, ...)
25076{
25077 va_list args;
25078 va_start(args, fmt);
25079 vfprintf(stderr, fmt, args);
25080 va_end(args);
25081 usage();
25082 exit(1);
25083}
25084
25085int main(int argc, char **argv)
25086{
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025087 const char *filename;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025088 struct compiler_state compiler;
25089 struct arch_state arch;
25090 int all_opts;
Eric Biederman90089602004-05-28 14:11:54 +000025091
25092
25093 /* I don't want any surprises */
25094 setlocale(LC_ALL, "C");
25095
Eric Biederman5ade04a2003-10-22 04:03:46 +000025096 init_compiler_state(&compiler);
25097 init_arch_state(&arch);
25098 filename = 0;
25099 all_opts = 0;
25100 while(argc > 1) {
25101 if (!all_opts && (strcmp(argv[1], "-o") == 0) && (argc > 2)) {
25102 compiler.ofilename = argv[2];
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025103 argv += 2;
25104 argc -= 2;
25105 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025106 else if (!all_opts && argv[1][0] == '-') {
Eric Biederman83b991a2003-10-11 06:20:25 +000025107 int result;
Eric Biederman5ade04a2003-10-22 04:03:46 +000025108 result = -1;
25109 if (strcmp(argv[1], "--") == 0) {
25110 result = 0;
25111 all_opts = 1;
25112 }
Eric Biederman90089602004-05-28 14:11:54 +000025113 else if (strncmp(argv[1], "-E", 2) == 0) {
25114 result = compiler_encode_flag(&compiler, argv[1]);
25115 }
25116 else if (strncmp(argv[1], "-O", 2) == 0) {
25117 result = compiler_encode_flag(&compiler, argv[1]);
25118 }
25119 else if (strncmp(argv[1], "-I", 2) == 0) {
25120 result = compiler_encode_flag(&compiler, argv[1]);
25121 }
25122 else if (strncmp(argv[1], "-D", 2) == 0) {
25123 result = compiler_encode_flag(&compiler, argv[1]);
25124 }
25125 else if (strncmp(argv[1], "-U", 2) == 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025126 result = compiler_encode_flag(&compiler, argv[1]);
25127 }
25128 else if (strncmp(argv[1], "--label-prefix=", 15) == 0) {
25129 result = compiler_encode_flag(&compiler, argv[1]+2);
25130 }
25131 else if (strncmp(argv[1], "-f", 2) == 0) {
25132 result = compiler_encode_flag(&compiler, argv[1]+2);
25133 }
25134 else if (strncmp(argv[1], "-m", 2) == 0) {
25135 result = arch_encode_flag(&arch, argv[1]+2);
25136 }
Patrick Georgi5cda45d2009-04-21 12:41:55 +000025137 else if (strncmp(argv[1], "-include", 10) == 0) {
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025138 struct filelist *old_head = include_filelist;
25139 include_filelist = malloc(sizeof(struct filelist));
25140 if (!include_filelist) {
25141 die("Out of memory.\n");
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025142 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025143 argv++;
25144 argc--;
25145 include_filelist->filename = argv[1];
25146 include_filelist->next = old_head;
25147 result = 0;
Stefan Reinauer1b9a5c62009-04-03 14:04:06 +000025148 }
Eric Biederman83b991a2003-10-11 06:20:25 +000025149 if (result < 0) {
Eric Biederman5ade04a2003-10-22 04:03:46 +000025150 arg_error("Invalid option specified: %s\n",
25151 argv[1]);
Eric Biederman6aa31cc2003-06-10 21:22:07 +000025152 }
25153 argv++;
25154 argc--;
25155 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025156 else {
25157 if (filename) {
25158 arg_error("Only one filename may be specified\n");
25159 }
25160 filename = argv[1];
25161 argv++;
25162 argc--;
25163 }
Eric Biedermanb138ac82003-04-22 18:44:01 +000025164 }
Eric Biederman5ade04a2003-10-22 04:03:46 +000025165 if (!filename) {
25166 arg_error("No filename specified\n");
Eric Biedermanb138ac82003-04-22 18:44:01 +000025167 }
Stefan Reinauerb4d3af82010-02-11 03:21:29 +000025168 compile(filename, &compiler, &arch);
Eric Biedermanb138ac82003-04-22 18:44:01 +000025169
25170 return 0;
25171}