|
|
1.1 root 1: /* Language-indepednent node constructors for parse phase of GNU compiler.
1.1.1.2 root 2: Copyright (C) 1987, 1988 Free Software Foundation, Inc.
1.1 root 3:
4: This file is part of GNU CC.
5:
6: GNU CC is distributed in the hope that it will be useful,
7: but WITHOUT ANY WARRANTY. No author or distributor
8: accepts responsibility to anyone for the consequences of using it
9: or for whether it serves any particular purpose or works at all,
10: unless he says so in writing. Refer to the GNU CC General Public
11: License for full details.
12:
13: Everyone is granted permission to copy, modify and redistribute
14: GNU CC, but only under the conditions described in the
15: GNU CC General Public License. A copy of this license is
16: supposed to have been given to you along with GNU CC so you
17: can know your rights and responsibilities. It should be in a
18: file named COPYING. Among other things, the copyright notice
19: and this notice must be preserved on all copies. */
20:
21:
22: /* This file contains the low level primitives for operating on tree nodes,
23: including allocation, list operations, interning of identifiers,
24: construction of data type nodes and statement nodes,
25: and construction of type conversion nodes. It also contains
26: tables index by tree code that describe how to take apart
27: nodes of that code.
28:
29: It is intended to be language-independent, but occasionally
30: calls language-dependent routines defined (for C) in typecheck.c.
31:
32: The low-level allocation routines oballoc and permalloc
33: are used also for allocating many other kinds of objects
34: by all passes of the compiler. */
35:
36: #include "config.h"
37: #include <stdio.h>
38: #include "tree.h"
39: #include "obstack.h"
1.1.1.2 root 40: #include "varargs.h"
1.1 root 41:
42: #define obstack_chunk_alloc xmalloc
43: #define obstack_chunk_free free
44:
45: extern int xmalloc ();
46: extern void free ();
47:
48: /* Tree nodes of permanent duration are allocated in this obstack.
49: They are the identifier nodes, and everything outside of
50: the bodies and parameters of function definitions. */
51:
52: struct obstack permanent_obstack;
53:
1.1.1.2 root 54: /* The initial RTL, and all ..._TYPE nodes, in a function
55: are allocated in this obstack. Usually they are freed at the
56: end of the function, but if the function is inline they are saved. */
57:
58: struct obstack maybepermanent_obstack;
59:
1.1 root 60: /* The contents of the current function definition are allocated
61: in this obstack, and all are freed at the end of the function. */
62:
63: struct obstack temporary_obstack;
64:
1.1.1.2 root 65: /* The tree nodes of an expression are allocated
66: in this obstack, and all are freed at the end of the expression. */
67:
68: struct obstack momentary_obstack;
69:
70: /* This points at either permanent_obstack or maybepermanent_obstack. */
71:
72: struct obstack *saveable_obstack;
73:
74: /* This is same as saveable_obstack during parse and expansion phase;
75: it points to temporary_obstack during optimization.
76: This is the obstack to be used for creating rtl objects. */
77:
78: struct obstack *rtl_obstack;
79:
1.1 root 80: /* This points at either permanent_obstack or temporary_obstack. */
81:
82: struct obstack *current_obstack;
83:
1.1.1.2 root 84: /* This points at either permanent_obstack or temporary_obstack
85: or momentary_obstack. */
86:
87: struct obstack *expression_obstack;
88:
89: /* Addresses of first objects in some obstacks.
90: This is for freeing their entire contents. */
91: char *maybepermanent_firstobj;
92: char *temporary_firstobj;
93: char *momentary_firstobj;
94:
1.1.1.5 root 95: /* Nonzero means all ..._TYPE nodes should be allocated permanently. */
96:
97: int all_types_permanent;
98:
1.1.1.2 root 99: /* Stack of places to restore the momentary obstack back to. */
100:
101: struct momentary_level
102: {
103: /* Pointer back to previous such level. */
104: struct momentary_level *prev;
105: /* First object allocated within this level. */
106: char *base;
107: /* Value of expression_obstack saved at entry to this level. */
108: struct obstack *obstack;
109: };
110:
111: struct momentary_level *momentary_stack;
112:
1.1 root 113: /* Table indexed by tree code giving a string containing a character
114: classifying the tree code. Possibilities are
115: t, d, s, c, r and e. See tree.def for details. */
116:
117: #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
118:
119: char *tree_code_type[] = {
120: #include "tree.def"
121: };
122: #undef DEFTREECODE
123:
124: /* Table indexed by tree code giving number of expression
125: operands beyond the fixed part of the node structure.
126: Not used for types or decls. */
127:
128: #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
129:
130: int tree_code_length[] = {
131: #include "tree.def"
132: };
133: #undef DEFTREECODE
134:
135: /* Counter for assigning unique ids to all tree nodes. */
136:
137: int tree_node_counter = 0;
138:
139: /* Hash table for uniquizing IDENTIFIER_NODEs by name. */
140:
1.1.1.2 root 141: #define MAX_HASH_TABLE 1009
1.1 root 142: static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
143:
144: /* Init data for node creation, at the beginning of compilation. */
145:
146: void
147: init_tree ()
148: {
149: obstack_init (&permanent_obstack);
1.1.1.2 root 150:
151: obstack_init (&temporary_obstack);
152: temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
153: obstack_init (&momentary_obstack);
154: momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
155: obstack_init (&maybepermanent_obstack);
156: maybepermanent_firstobj
157: = (char *) obstack_alloc (&maybepermanent_obstack, 0);
158:
1.1 root 159: current_obstack = &permanent_obstack;
1.1.1.2 root 160: expression_obstack = &permanent_obstack;
161: rtl_obstack = saveable_obstack = &permanent_obstack;
1.1 root 162: tree_node_counter = 1;
163: bzero (hash_table, sizeof hash_table);
164: }
165:
166: /* Start allocating on the temporary (per function) obstack.
1.1.1.2 root 167: This is done in start_function before parsing the function body,
168: and before each initialization at top level, and to go back
169: to temporary allocation after doing end_temporary_allocation. */
1.1 root 170:
1.1.1.2 root 171: void
1.1 root 172: temporary_allocation ()
173: {
174: current_obstack = &temporary_obstack;
1.1.1.2 root 175: expression_obstack = &temporary_obstack;
176: rtl_obstack = saveable_obstack = &maybepermanent_obstack;
177: momentary_stack = 0;
178: }
179:
180: /* Start allocating on the permanent obstack but don't
181: free the temporary data. After calling this, call
182: `permanent_allocation' to fully resume permanent allocation status. */
183:
184: void
185: end_temporary_allocation ()
186: {
187: current_obstack = &permanent_obstack;
188: expression_obstack = &permanent_obstack;
189: rtl_obstack = saveable_obstack = &permanent_obstack;
1.1 root 190: }
191:
1.1.1.4 root 192: /* Resume allocating on the temporary obstack, undoing
193: effects of `end_temporary_allocation'. */
194:
195: void
196: resume_temporary_allocation ()
197: {
198: current_obstack = &temporary_obstack;
199: expression_obstack = &temporary_obstack;
200: rtl_obstack = saveable_obstack = &maybepermanent_obstack;
201: }
202:
1.1.1.5 root 203: /* Nonzero if temporary allocation is currently in effect.
204: Zero if currently doing permanent allocation. */
205:
206: int
207: allocation_temporary_p ()
208: {
209: return current_obstack == &temporary_obstack;
210: }
211:
1.1 root 212: /* Go back to allocating on the permanent obstack
213: and free everything in the temporary obstack.
214: This is done in finish_function after fully compiling a function. */
215:
1.1.1.2 root 216: void
1.1 root 217: permanent_allocation ()
218: {
219: /* Free up previous temporary obstack data */
1.1.1.2 root 220: obstack_free (&temporary_obstack, temporary_firstobj);
221: obstack_free (&momentary_obstack, momentary_firstobj);
222: obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
1.1 root 223:
224: current_obstack = &permanent_obstack;
1.1.1.2 root 225: expression_obstack = &permanent_obstack;
226: rtl_obstack = saveable_obstack = &permanent_obstack;
1.1 root 227: }
228:
1.1.1.2 root 229: /* Save permanently everything on the maybepermanent_obstack. */
230:
231: void
232: preserve_data ()
233: {
234: maybepermanent_firstobj
235: = (char *) obstack_alloc (&maybepermanent_obstack, 0);
236: }
237:
1.1 root 238: /* Allocate SIZE bytes in the current obstack
239: and return a pointer to them.
240: In practice the current obstack is always the temporary one. */
241:
242: char *
243: oballoc (size)
244: int size;
245: {
246: return (char *) obstack_alloc (current_obstack, size);
247: }
248:
249: /* Free the object PTR in the current obstack
250: as well as everything allocated since PTR.
251: In practice the current obstack is always the temporary one. */
252:
253: void
254: obfree (ptr)
255: char *ptr;
256: {
257: obstack_free (current_obstack, ptr);
258: }
259:
260: /* Allocate SIZE bytes in the permanent obstack
261: and return a pointer to them. */
262:
263: char *
264: permalloc (size)
265: long size;
266: {
267: return (char *) obstack_alloc (&permanent_obstack, size);
268: }
269:
1.1.1.2 root 270: /* Start a level of momentary allocation.
271: In C, each compound statement has its own level
272: and that level is freed at the end of each statement.
273: All expression nodes are allocated in the momentary allocation level. */
274:
275: void
276: push_momentary ()
277: {
278: struct momentary_level *tem
279: = (struct momentary_level *) obstack_alloc (&momentary_obstack,
280: sizeof (struct momentary_level));
281: tem->prev = momentary_stack;
282: tem->base = (char *) obstack_base (&momentary_obstack);
283: tem->obstack = expression_obstack;
284: momentary_stack = tem;
285: expression_obstack = &momentary_obstack;
286: }
287:
288: /* Free all the storage in the current momentary-allocation level.
289: In C, this happens at the end of each statement. */
290:
291: void
292: clear_momentary ()
293: {
294: obstack_free (&momentary_obstack, momentary_stack->base);
295: }
296:
297: /* Discard a level of momentary allocation.
298: In C, this happens at the end of each compound statement.
299: Restore the status of expression node allocation
300: that was in effect before this level was created. */
301:
302: void
303: pop_momentary ()
304: {
305: struct momentary_level *tem = momentary_stack;
306: momentary_stack = tem->prev;
307: obstack_free (&momentary_obstack, tem);
308: expression_obstack = tem->obstack;
309: }
310:
311: /* Call when starting to parse a declaration:
312: make expressions in the declaration last the length of the function.
313: Returns an argument that should be passed to resume_momentary later. */
314:
315: int
316: suspend_momentary ()
317: {
318: register int tem = expression_obstack == &momentary_obstack;
1.1.1.4 root 319: expression_obstack = saveable_obstack;
1.1.1.2 root 320: return tem;
321: }
322:
323: /* Call when finished parsing a declaration:
324: restore the treatment of node-allocation that was
325: in effect before the suspension.
326: YES should be the value previously returned by suspend_momentary. */
327:
328: void
329: resume_momentary (yes)
330: int yes;
331: {
332: if (yes)
333: expression_obstack = &momentary_obstack;
334: }
335:
1.1 root 336: /* Return a newly allocated node of code CODE.
337: Initialize the node's unique id and its TREE_PERMANENT flag.
338: For decl and type nodes, some other fields are initialized.
339: The rest of the node is initialized to zero.
340:
341: Achoo! I got a code in the node. */
342:
343: tree
344: make_node (code)
345: enum tree_code code;
346: {
347: register tree t;
348: register int type = *tree_code_type[(int) code];
349: register int length;
1.1.1.2 root 350: register struct obstack *obstack = current_obstack;
1.1 root 351: register int i;
352:
353: switch (type)
354: {
355: case 'd': /* A decl node */
356: length = sizeof (struct tree_decl);
1.1.1.2 root 357: /* All decls in an inline function need to be saved. */
358: if (obstack != &permanent_obstack)
359: obstack = saveable_obstack;
1.1 root 360: break;
361:
362: case 't': /* a type node */
363: length = sizeof (struct tree_type);
1.1.1.2 root 364: /* All data types are put where we can preserve them if nec. */
365: if (obstack != &permanent_obstack)
1.1.1.5 root 366: obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
1.1 root 367: break;
368:
369: case 's': /* a stmt node */
1.1.1.2 root 370: length = sizeof (struct tree_common)
1.1 root 371: + 2 * sizeof (int)
372: + tree_code_length[(int) code] * sizeof (char *);
1.1.1.2 root 373: /* All stmts are put where we can preserve them if nec. */
374: if (obstack != &permanent_obstack)
375: obstack = saveable_obstack;
1.1 root 376: break;
377:
1.1.1.2 root 378: case 'r': /* a reference */
379: case 'e': /* an expression */
380: obstack = expression_obstack;
381: length = sizeof (struct tree_exp)
382: + (tree_code_length[(int) code] - 1) * sizeof (char *);
383: break;
384:
385: case 'c': /* a constant */
386: obstack = expression_obstack;
387: /* We can't use tree_code_length for this, since the number of words
388: is machine-dependent due to varying alignment of `double'. */
389: if (code == REAL_CST)
390: {
391: length = sizeof (struct tree_real_cst);
392: break;
393: }
394:
395: case 'x': /* something random, like an identifier. */
396: length = sizeof (struct tree_common)
1.1 root 397: + tree_code_length[(int) code] * sizeof (char *);
1.1.1.2 root 398: /* Identifier nodes are always permanent since they are
399: unique in a compiler run. */
400: if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
1.1 root 401: }
402:
403: t = (tree) obstack_alloc (obstack, length);
404:
405: TREE_UID (t) = tree_node_counter++;
406: TREE_TYPE (t) = 0;
407: TREE_CHAIN (t) = 0;
408: for (i = (length / sizeof (int)) - 1;
1.1.1.2 root 409: i >= sizeof (struct tree_common) / sizeof (int) - 1;
1.1 root 410: i--)
411: ((int *) t)[i] = 0;
412:
413: TREE_SET_CODE (t, code);
414: if (obstack == &permanent_obstack)
415: TREE_PERMANENT (t) = 1;
416:
417: if (type == 'd')
418: {
419: extern int lineno;
420:
421: DECL_ALIGN (t) = 1;
422: DECL_SIZE_UNIT (t) = 1;
423: DECL_VOFFSET_UNIT (t) = 1;
424: DECL_SOURCE_LINE (t) = lineno;
425: DECL_SOURCE_FILE (t) = input_filename;
426: }
427:
428: if (type == 't')
429: {
430: TYPE_ALIGN (t) = 1;
431: TYPE_SIZE_UNIT (t) = 1;
432: TYPE_MAIN_VARIANT (t) = t;
433: }
434:
435: if (type == 'c')
436: {
437: TREE_LITERAL (t) = 1;
438: }
439:
440: return t;
441: }
442:
443: /* Return a new node with the same contents as NODE
444: except that its TREE_CHAIN is zero and it has a fresh uid. */
445:
446: tree
447: copy_node (node)
448: tree node;
449: {
450: register tree t;
451: register enum tree_code code = TREE_CODE (node);
452: register int length;
453: register int i;
454:
455: switch (*tree_code_type[(int) code])
456: {
457: case 'd': /* A decl node */
458: length = sizeof (struct tree_decl);
459: break;
460:
461: case 't': /* a type node */
462: length = sizeof (struct tree_type);
463: break;
464:
465: case 's':
1.1.1.2 root 466: length = sizeof (struct tree_common)
1.1 root 467: + 2 * sizeof (int)
468: + tree_code_length[(int) code] * sizeof (char *);
469: break;
470:
1.1.1.2 root 471: case 'r': /* a reference */
472: case 'e': /* a expression */
473: length = sizeof (struct tree_exp)
474: + (tree_code_length[(int) code] - 1) * sizeof (char *);
475: break;
476:
477: case 'c': /* a constant */
478: /* We can't use tree_code_length for this, since the number of words
479: is machine-dependent due to varying alignment of `double'. */
480: if (code == REAL_CST)
481: {
482: length = sizeof (struct tree_real_cst);
483: break;
484: }
485:
486: case 'x': /* something random, like an identifier. */
487: length = sizeof (struct tree_common)
1.1 root 488: + tree_code_length[(int) code] * sizeof (char *);
489: }
490:
491: t = (tree) obstack_alloc (current_obstack, length);
492:
1.1.1.5 root 493: for (i = ((length + sizeof (int) - 1) / sizeof (int)) - 1;
1.1 root 494: i >= 0;
495: i--)
496: ((int *) t)[i] = ((int *) node)[i];
497:
498: TREE_UID (t) = tree_node_counter++;
499: TREE_CHAIN (t) = 0;
500:
501: TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
502:
503: return t;
504: }
505:
506: #define HASHBITS 30
507:
508: /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
509: If an identifier with that name has previously been referred to,
510: the same node is returned this time. */
511:
512: tree
513: get_identifier (text)
514: register char *text;
515: {
516: register int hi;
517: register int i;
518: register tree idp;
519: register int len;
520:
521: /* Compute length of text in len. */
522: for (len = 0; text[len]; len++);
523:
524: /* Compute hash code */
525: hi = len;
526: for (i = 0; i < len; i++)
527: hi = ((hi * 613) + (unsigned)(text[i]));
528:
529: hi &= (1 << HASHBITS) - 1;
530: hi %= MAX_HASH_TABLE;
531:
532: /* Search table for identifier */
533: for (idp = hash_table[hi]; idp!=NULL; idp = TREE_CHAIN (idp))
534: if (IDENTIFIER_LENGTH (idp) == len &&
535: !strcmp (IDENTIFIER_POINTER (idp), text))
536: return idp; /* <-- return if found */
537:
538: /* Not found, create one, add to chain */
539: idp = make_node (IDENTIFIER_NODE);
540: IDENTIFIER_LENGTH (idp) = len;
541:
542: IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
543:
544: TREE_CHAIN (idp) = hash_table[hi];
545: hash_table[hi] = idp;
546: return idp; /* <-- return if created */
547: }
548:
549: /* Return a newly constructed INTEGER_CST node whose constant value
550: is specified by the two ints LOW and HI.
1.1.1.2 root 551: The TREE_TYPE is set to `int'. */
1.1 root 552:
553: tree
554: build_int_2 (low, hi)
555: int low, hi;
556: {
557: register tree t = make_node (INTEGER_CST);
558: TREE_INT_CST_LOW (t) = low;
559: TREE_INT_CST_HIGH (t) = hi;
560: TREE_TYPE (t) = integer_type_node;
561: return t;
562: }
563:
1.1.1.4 root 564: /* Return a new REAL_CST node whose type is TYPE and value is D. */
1.1 root 565:
566: tree
1.1.1.4 root 567: build_real (type, d)
568: tree type;
1.1.1.12! root 569: REAL_VALUE_TYPE d;
1.1 root 570: {
571: tree v;
572:
1.1.1.4 root 573: /* Check for valid float value for this type on this target machine;
574: if not, can print error message and store a valid value in D. */
575: #ifdef CHECK_FLOAT_VALUE
576: CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
577: #endif
578:
1.1 root 579: v = make_node (REAL_CST);
1.1.1.4 root 580: TREE_TYPE (v) = type;
1.1 root 581: TREE_REAL_CST (v) = d;
582: return v;
583: }
584:
1.1.1.4 root 585: /* Return a new REAL_CST node whose type is TYPE
586: and whose value is the integer value of the INTEGER_CST node I. */
1.1 root 587:
1.1.1.12! root 588: #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
! 589: /* This function can't be implemented if we can't do arithmetic
! 590: on the float representation. */
! 591:
1.1 root 592: tree
1.1.1.4 root 593: build_real_from_int_cst (type, i)
594: tree type;
1.1 root 595: tree i;
596: {
597: tree v;
1.1.1.2 root 598: double d;
1.1 root 599:
600: v = make_node (REAL_CST);
1.1.1.4 root 601: TREE_TYPE (v) = type;
1.1.1.2 root 602:
1.1.1.12! root 603: #ifdef REAL_ARITHMETIC
! 604: REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
! 605: #else /* not REAL_ARITHMETIC */
1.1.1.2 root 606: if (TREE_INT_CST_HIGH (i) < 0)
607: {
608: d = (double) (~ TREE_INT_CST_HIGH (i));
609: d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
610: * (double) (1 << (HOST_BITS_PER_INT / 2)));
611: d += (double) (unsigned) (~ TREE_INT_CST_LOW (i));
612: d = (- d - 1.0);
613: }
614: else
615: {
616: d = (double) TREE_INT_CST_HIGH (i);
617: d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
618: * (double) (1 << (HOST_BITS_PER_INT / 2)));
619: d += (double) (unsigned) TREE_INT_CST_LOW (i);
620: }
1.1.1.12! root 621: #endif /* not REAL_ARITHMETIC */
1.1.1.2 root 622:
1.1.1.6 root 623: /* Check for valid float value for this type on this target machine;
624: if not, can print error message and store a valid value in D. */
625: #ifdef CHECK_FLOAT_VALUE
626: CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
627: #endif
628:
1.1.1.2 root 629: TREE_REAL_CST (v) = d;
1.1 root 630: return v;
631: }
632:
1.1.1.12! root 633: #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
! 634:
1.1 root 635: /* Return a newly constructed STRING_CST node whose value is
636: the LEN characters at STR.
637: The TREE_TYPE is not initialized. */
638:
639: tree
640: build_string (len, str)
641: int len;
642: char *str;
643: {
644: register tree s = make_node (STRING_CST);
645: TREE_STRING_LENGTH (s) = len;
1.1.1.2 root 646: TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1.1 root 647: return s;
648: }
649:
650: /* Return a newly constructed COMPLEX_CST node whose value is
651: specified by the real and imaginary parts REAL and IMAG.
652: Both REAL and IMAG should be constant nodes.
653: The TREE_TYPE is not initialized. */
654:
655: tree
656: build_complex (real, imag)
657: tree real, imag;
658: {
659: register tree t = make_node (COMPLEX_CST);
660: TREE_REALPART (t) = real;
661: TREE_IMAGPART (t) = imag;
662: return t;
663: }
664:
665: /* Return 1 if EXPR is the integer constant zero. */
666:
667: int
668: integer_zerop (expr)
669: tree expr;
670: {
1.1.1.2 root 671: return (TREE_CODE (expr) == INTEGER_CST
672: && TREE_INT_CST_LOW (expr) == 0
673: && TREE_INT_CST_HIGH (expr) == 0);
1.1 root 674: }
675:
676: /* Return 1 if EXPR is the integer constant one. */
677:
678: int
679: integer_onep (expr)
680: tree expr;
681: {
1.1.1.2 root 682: return (TREE_CODE (expr) == INTEGER_CST
683: && TREE_INT_CST_LOW (expr) == 1
684: && TREE_INT_CST_HIGH (expr) == 0);
1.1 root 685: }
686:
687: /* Return 1 if EXPR is an integer containing all 1's
688: in as much precision as it contains. */
689:
690: int
691: integer_all_onesp (expr)
692: tree expr;
693: {
694: register int prec;
695: register int uns;
696:
697: if (TREE_CODE (expr) != INTEGER_CST)
698: return 0;
699:
1.1.1.2 root 700: uns = TREE_UNSIGNED (TREE_TYPE (expr));
1.1 root 701: if (!uns)
702: return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
703:
704: prec = TYPE_PRECISION (TREE_TYPE (expr));
705: if (prec >= HOST_BITS_PER_INT)
706: return TREE_INT_CST_LOW (expr) == -1
707: && TREE_INT_CST_HIGH (expr) == (1 << (prec - HOST_BITS_PER_INT)) - 1;
708: else
709: return TREE_INT_CST_LOW (expr) == (1 << prec) - 1;
710: }
711:
712: /* Return the length of a chain of nodes chained through TREE_CHAIN.
713: We expect a null pointer to mark the end of the chain.
714: This is the Lisp primitive `length'. */
715:
716: int
717: list_length (t)
718: tree t;
719: {
720: register tree tail;
721: register int len = 0;
722:
723: for (tail = t; tail; tail = TREE_CHAIN (tail))
724: len++;
725:
726: return len;
727: }
728:
729: /* Concatenate two chains of nodes (chained through TREE_CHAIN)
730: by modifying the last node in chain 1 to point to chain 2.
731: This is the Lisp primitive `nconc'. */
732:
733: tree
734: chainon (op1, op2)
735: tree op1, op2;
736: {
737: tree t;
738:
739: if (op1)
740: {
741: for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
742: if (t == op2) abort (); /* Circularity being created */
743: TREE_CHAIN (t) = op2;
744: return op1;
745: }
746: else return op2;
747: }
748:
749: /* Return a newly created TREE_LIST node whose
750: purpose and value fields are PARM and VALUE. */
751:
752: tree
753: build_tree_list (parm, value)
754: tree parm, value;
755: {
756: register tree t = make_node (TREE_LIST);
757: TREE_PURPOSE (t) = parm;
758: TREE_VALUE (t) = value;
759: return t;
760: }
761:
762: /* Return a newly created TREE_LIST node whose
763: purpose and value fields are PARM and VALUE
764: and whose TREE_CHAIN is CHAIN. */
765:
766: tree
767: tree_cons (purpose, value, chain)
768: tree purpose, value, chain;
769: {
770: register tree node = make_node (TREE_LIST);
771: TREE_CHAIN (node) = chain;
772: TREE_PURPOSE (node) = purpose;
773: TREE_VALUE (node) = value;
774: return node;
775: }
776:
1.1.1.2 root 777: /* Same as `tree_cons' but make a permanent object. */
778:
779: tree
780: perm_tree_cons (purpose, value, chain)
781: tree purpose, value, chain;
782: {
783: register tree node;
784: register struct obstack *ambient_obstack = current_obstack;
785: current_obstack = &permanent_obstack;
786:
787: node = make_node (TREE_LIST);
788: TREE_CHAIN (node) = chain;
789: TREE_PURPOSE (node) = purpose;
790: TREE_VALUE (node) = value;
791:
792: current_obstack = ambient_obstack;
793: return node;
794: }
795:
1.1.1.7 root 796: /* Same as `tree_cons', but make this node temporary, regardless. */
797:
798: tree
799: temp_tree_cons (purpose, value, chain)
800: tree purpose, value, chain;
801: {
802: register tree node;
803: register struct obstack *ambient_obstack = current_obstack;
804: current_obstack = &temporary_obstack;
805:
806: node = make_node (TREE_LIST);
807: TREE_CHAIN (node) = chain;
808: TREE_PURPOSE (node) = purpose;
809: TREE_VALUE (node) = value;
810:
811: current_obstack = ambient_obstack;
812: return node;
813: }
814:
1.1.1.9 root 815: /* Same as `tree_cons', but save this node if the function's RTL is saved. */
816:
817: tree
818: saveable_tree_cons (purpose, value, chain)
819: tree purpose, value, chain;
820: {
821: register tree node;
822: register struct obstack *ambient_obstack = current_obstack;
823: current_obstack = saveable_obstack;
824:
825: node = make_node (TREE_LIST);
826: TREE_CHAIN (node) = chain;
827: TREE_PURPOSE (node) = purpose;
828: TREE_VALUE (node) = value;
829:
830: current_obstack = ambient_obstack;
831: return node;
832: }
833:
1.1 root 834: /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
835:
836: tree
837: tree_last (chain)
838: register tree chain;
839: {
840: register tree next;
841: if (chain)
842: while (next = TREE_CHAIN (chain))
843: chain = next;
844: return chain;
845: }
846:
847: /* Reverse the order of elements in the chain T,
848: and return the new head of the chain (old last element). */
849:
850: tree
851: nreverse (t)
852: tree t;
853: {
854: register tree prev = 0, decl, next;
855: for (decl = t; decl; decl = next)
856: {
857: next = TREE_CHAIN (decl);
858: TREE_CHAIN (decl) = prev;
859: prev = decl;
860: }
861: return prev;
862: }
863:
864: /* Return the size nominally occupied by an object of type TYPE
865: when it resides in memory. The value is measured in units of bytes,
866: and its data type is that normally used for type sizes
867: (which is the first type created by make_signed_type or
868: make_unsigned_type). */
869:
870: tree
871: size_in_bytes (type)
872: tree type;
873: {
874: if (type == error_mark_node)
875: return integer_zero_node;
1.1.1.2 root 876: if (TYPE_SIZE (type) == 0)
877: {
878: incomplete_type_error (0, type);
879: return integer_zero_node;
880: }
1.1 root 881: return convert_units (TYPE_SIZE (type), TYPE_SIZE_UNIT (type),
882: BITS_PER_UNIT);
883: }
884:
1.1.1.2 root 885: /* Return the size of TYPE (in bytes) as an integer,
886: or return -1 if the size can vary. */
887:
888: int
889: int_size_in_bytes (type)
890: tree type;
891: {
892: int size;
893: if (type == error_mark_node)
894: return 0;
895: if (TYPE_SIZE (type) == 0)
896: return -1;
897: if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
898: return -1;
899: size = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
900: return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
901: }
902:
1.1.1.7 root 903: /* Return, as an INTEGER_CST node, the number of elements for
904: TYPE (which is an ARRAY_TYPE). */
905:
906: tree
907: array_type_nelts (type)
908: tree type;
909: {
910: tree index_type = TYPE_DOMAIN (type);
911: return (tree_int_cst_equal (TYPE_MIN_VALUE (index_type), integer_zero_node)
912: ? TYPE_MAX_VALUE (index_type)
913: : fold (build (MINUS_EXPR, integer_type_node,
914: TYPE_MAX_VALUE (index_type),
915: TYPE_MIN_VALUE (index_type))));
916: }
917:
1.1 root 918: /* Return nonzero if arg is static -- a reference to an object in
919: static storage. This is not the same as the C meaning of `static'. */
920:
921: int
922: staticp (arg)
923: tree arg;
924: {
925: register enum tree_code code = TREE_CODE (arg);
926:
1.1.1.2 root 927: if ((code == VAR_DECL || code == FUNCTION_DECL || code == CONSTRUCTOR)
1.1 root 928: && (TREE_STATIC (arg) || TREE_EXTERNAL (arg)))
929: return 1;
930:
1.1.1.2 root 931: if (code == STRING_CST)
932: return 1;
933:
1.1 root 934: if (code == COMPONENT_REF)
1.1.1.10 root 935: return (DECL_VOFFSET (TREE_OPERAND (arg, 1)) == 0
936: && staticp (TREE_OPERAND (arg, 0)));
937:
938: if (code == INDIRECT_REF)
939: return TREE_LITERAL (TREE_OPERAND (arg, 0));
1.1 root 940:
1.1.1.2 root 941: if (code == ARRAY_REF)
942: {
943: if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
944: && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
945: return staticp (TREE_OPERAND (arg, 0));
946: }
947:
1.1 root 948: return 0;
949: }
950:
1.1.1.2 root 951: /* Return nonzero if REF is an lvalue valid for this language.
952: Lvalues can be assigned, unless they have TREE_READONLY.
953: Lvalues can have their address taken, unless they have TREE_REGDECL. */
1.1 root 954:
955: int
1.1.1.2 root 956: lvalue_p (ref)
1.1 root 957: tree ref;
958: {
959: register enum tree_code code = TREE_CODE (ref);
960:
1.1.1.2 root 961: if (language_lvalue_valid (ref))
962: switch (code)
963: {
964: case COMPONENT_REF:
965: return lvalue_p (TREE_OPERAND (ref, 0));
966:
967: case STRING_CST:
968: return 1;
969:
970: case INDIRECT_REF:
971: case ARRAY_REF:
972: case VAR_DECL:
973: case PARM_DECL:
974: case RESULT_DECL:
975: case ERROR_MARK:
976: if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE)
977: return 1;
1.1.1.7 root 978: break;
979:
980: case CALL_EXPR:
981: if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
982: return 1;
1.1.1.2 root 983: }
1.1 root 984: return 0;
985: }
1.1.1.2 root 986:
987: /* Return nonzero if REF is an lvalue valid for this language;
988: otherwise, print an error message and return zero. */
989:
990: int
991: lvalue_or_else (ref, string)
992: tree ref;
993: char *string;
994: {
995: int win = lvalue_p (ref);
996: if (! win)
997: error ("invalid lvalue in %s", string);
998: return win;
999: }
1.1 root 1000:
1001: /* This should be applied to any node which may be used in more than one place,
1002: but must be evaluated only once. Normally, the code generator would
1003: reevaluate the node each time; this forces it to compute it once and save
1004: the result. This is done by encapsulating the node in a SAVE_EXPR. */
1005:
1006: tree
1007: save_expr (expr)
1008: tree expr;
1009: {
1010: register tree t = fold (expr);
1011:
1.1.1.2 root 1012: /* If the tree evaluates to a constant, then we don't want to hide that
1.1 root 1013: fact (i.e. this allows further folding, and direct checks for constants).
1014: Since it is no problem to reevaluate literals, we just return the
1015: literal node. */
1016:
1017: if (TREE_LITERAL (t) || TREE_READONLY (t) || TREE_CODE (t) == SAVE_EXPR)
1018: return t;
1019:
1.1.1.2 root 1020: return build (SAVE_EXPR, TREE_TYPE (expr), t, NULL);
1.1 root 1021: }
1022:
1023: /* Stabilize a reference so that we can use it any number of times
1024: without causing its operands to be evaluated more than once.
1.1.1.2 root 1025: Returns the stabilized reference.
1026:
1027: Also allows conversion expressions whose operands are references.
1028: Any other kind of expression is returned unchanged. */
1.1 root 1029:
1030: tree
1031: stabilize_reference (ref)
1032: tree ref;
1033: {
1034: register tree result;
1035: register enum tree_code code = TREE_CODE (ref);
1036:
1.1.1.2 root 1037: switch (code)
1.1 root 1038: {
1.1.1.2 root 1039: case VAR_DECL:
1040: case PARM_DECL:
1041: case RESULT_DECL:
1.1 root 1042: result = ref;
1.1.1.2 root 1043: break;
1044:
1045: case NOP_EXPR:
1046: case CONVERT_EXPR:
1047: case FLOAT_EXPR:
1048: case FIX_TRUNC_EXPR:
1049: case FIX_FLOOR_EXPR:
1050: case FIX_ROUND_EXPR:
1051: case FIX_CEIL_EXPR:
1052: result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
1053: break;
1054:
1055: case INDIRECT_REF:
1056: result = build_nt (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
1057: break;
1058:
1059: case COMPONENT_REF:
1060: result = build_nt (COMPONENT_REF,
1061: stabilize_reference (TREE_OPERAND (ref, 0)),
1062: TREE_OPERAND (ref, 1));
1063: break;
1064:
1065: case ARRAY_REF:
1066: result = build_nt (ARRAY_REF, stabilize_reference (TREE_OPERAND (ref, 0)),
1067: save_expr (TREE_OPERAND (ref, 1)));
1068: break;
1069:
1070: /* If arg isn't a kind of lvalue we recognize, make no change.
1071: Caller should recognize the error for an invalid lvalue. */
1072: default:
1073: return ref;
1074:
1075: case ERROR_MARK:
1.1 root 1076: return error_mark_node;
1077: }
1078:
1079: TREE_TYPE (result) = TREE_TYPE (ref);
1.1.1.2 root 1080: TREE_READONLY (result) = TREE_READONLY (ref);
1.1 root 1081: TREE_VOLATILE (result) = TREE_VOLATILE (ref);
1.1.1.2 root 1082: TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
1.1 root 1083:
1084: return result;
1085: }
1086:
1087: /* Low-level constructors for expressions. */
1088:
1.1.1.2 root 1089: /* Build an expression of code CODE, data type TYPE,
1090: and operands as specified by the arguments ARG1 and following arguments.
1091: Expressions and reference nodes can be created this way.
1092: Constants, decls, types and misc nodes cannot be. */
1.1 root 1093:
1094: tree
1.1.1.2 root 1095: build (va_alist)
1096: va_dcl
1.1 root 1097: {
1.1.1.2 root 1098: register va_list p;
1099: enum tree_code code;
1100: register tree t;
1101: register int length;
1102: register int i;
1103:
1104: va_start (p);
1.1 root 1105:
1.1.1.2 root 1106: code = va_arg (p, enum tree_code);
1107: t = make_node (code);
1108: length = tree_code_length[(int) code];
1109: TREE_TYPE (t) = va_arg (p, tree);
1110:
1111: if (length == 2)
1112: {
1113: /* This is equivalent to the loop below, but faster. */
1114: register tree arg0 = va_arg (p, tree);
1115: register tree arg1 = va_arg (p, tree);
1116: TREE_OPERAND (t, 0) = arg0;
1117: TREE_OPERAND (t, 1) = arg1;
1118: TREE_VOLATILE (t)
1119: = (arg0 && TREE_VOLATILE (arg0)) || (arg1 && TREE_VOLATILE (arg1));
1120: }
1121: else
1122: {
1123: for (i = 0; i < length; i++)
1124: {
1125: register tree operand = va_arg (p, tree);
1126: TREE_OPERAND (t, i) = operand;
1127: if (operand && TREE_VOLATILE (operand))
1128: TREE_VOLATILE (t) = 1;
1129: }
1130: }
1131: va_end (p);
1.1 root 1132: return t;
1133: }
1134:
1.1.1.2 root 1135: /* Similar except don't specify the TREE_TYPE
1136: and leave the TREE_VOLATILE as 0.
1137: It is permissible for arguments to be null,
1138: or even garbage if their values do not matter. */
1.1 root 1139:
1140: tree
1.1.1.2 root 1141: build_nt (va_alist)
1142: va_dcl
1.1 root 1143: {
1.1.1.2 root 1144: register va_list p;
1145: register enum tree_code code;
1146: register tree t;
1147: register int length;
1148: register int i;
1149:
1150: va_start (p);
1.1 root 1151:
1.1.1.2 root 1152: code = va_arg (p, enum tree_code);
1153: t = make_node (code);
1154: length = tree_code_length[(int) code];
1155:
1156: for (i = 0; i < length; i++)
1157: TREE_OPERAND (t, i) = va_arg (p, tree);
1158:
1159: va_end (p);
1.1 root 1160: return t;
1161: }
1.1.1.11 root 1162:
1163: tree
1164: build_op_identifier (op1, op2)
1165: tree op1, op2;
1166: {
1167: register tree t = make_node (OP_IDENTIFIER);
1168: TREE_PURPOSE (t) = op1;
1169: TREE_VALUE (t) = op2;
1170: return t;
1171: }
1.1.1.2 root 1172:
1173: /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
1174: We do NOT enter this node in any sort of symbol table.
1.1 root 1175:
1.1.1.2 root 1176: layout_decl is used to set up the decl's storage layout.
1177: Other slots are initialized to 0 or null pointers. */
1.1 root 1178:
1179: tree
1.1.1.2 root 1180: build_decl (code, name, type)
1181: enum tree_code code;
1182: tree name, type;
1183: {
1184: register tree t;
1185:
1186: t = make_node (code);
1187:
1188: /* if (type == error_mark_node)
1189: type = integer_type_node; */
1190: /* That is not done, deliberately, so that having error_mark_node
1191: as the type can suppress useless errors in the use of this variable. */
1192:
1193: DECL_NAME (t) = name;
1194: TREE_TYPE (t) = type;
1195: DECL_ARGUMENTS (t) = NULL_TREE;
1196: DECL_INITIAL (t) = NULL_TREE;
1197:
1198: if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
1199: layout_decl (t, 0);
1200: else if (code == FUNCTION_DECL)
1201: DECL_MODE (t) = FUNCTION_MODE;
1202:
1.1 root 1203: return t;
1204: }
1205:
1.1.1.7 root 1206: #if 0
1.1 root 1207: /* Low-level constructors for statements.
1208: These constructors all expect source file name and line number
1209: as arguments, as well as enough arguments to fill in the data
1210: in the statement node. */
1211:
1212: tree
1213: build_goto (filename, line, label)
1214: char *filename;
1215: int line;
1216: tree label;
1217: {
1218: register tree t = make_node (GOTO_STMT);
1219: STMT_SOURCE_FILE (t) = filename;
1220: STMT_SOURCE_LINE (t) = line;
1221: STMT_BODY (t) = label;
1222: return t;
1223: }
1224:
1225: tree
1226: build_return (filename, line, arg)
1227: char *filename;
1228: int line;
1229: tree arg;
1230: {
1231: register tree t = make_node (RETURN_STMT);
1232:
1233: STMT_SOURCE_FILE (t) = filename;
1234: STMT_SOURCE_LINE (t) = line;
1235: STMT_BODY (t) = arg;
1236: return t;
1237: }
1238:
1239: tree
1240: build_expr_stmt (filename, line, expr)
1241: char *filename;
1242: int line;
1243: tree expr;
1244: {
1245: register tree t = make_node (EXPR_STMT);
1246:
1247: STMT_SOURCE_FILE (t) = filename;
1248: STMT_SOURCE_LINE (t) = line;
1249: STMT_BODY (t) = expr;
1250: return t;
1251: }
1252:
1253: tree
1254: build_if (filename, line, cond, thenclause, elseclause)
1255: char *filename;
1256: int line;
1257: tree cond, thenclause, elseclause;
1258: {
1259: register tree t = make_node (IF_STMT);
1260:
1261: STMT_SOURCE_FILE (t) = filename;
1262: STMT_SOURCE_LINE (t) = line;
1263: STMT_COND (t) = cond;
1264: STMT_THEN (t) = thenclause;
1265: STMT_ELSE (t) = elseclause;
1266: return t;
1267: }
1268:
1269: tree
1270: build_exit (filename, line, cond)
1271: char *filename;
1272: int line;
1273: tree cond;
1274: {
1275: register tree t = make_node (EXIT_STMT);
1276: STMT_SOURCE_FILE (t) = filename;
1277: STMT_SOURCE_LINE (t) = line;
1278: STMT_BODY (t) = cond;
1279: return t;
1280: }
1281:
1282: tree
1283: build_asm_stmt (filename, line, asmcode)
1284: char *filename;
1285: int line;
1286: tree asmcode;
1287: {
1288: register tree t = make_node (ASM_STMT);
1289: STMT_SOURCE_FILE (t) = filename;
1290: STMT_SOURCE_LINE (t) = line;
1291: STMT_BODY (t) = asmcode;
1292: return t;
1293: }
1294:
1295: tree
1296: build_case (filename, line, object, cases)
1297: char *filename;
1298: int line;
1299: tree object, cases;
1300: {
1301: register tree t = make_node (CASE_STMT);
1302: STMT_SOURCE_FILE (t) = filename;
1303: STMT_SOURCE_LINE (t) = line;
1304: STMT_CASE_INDEX (t) = object;
1305: STMT_CASE_LIST (t) = cases;
1306: return t;
1307: }
1308:
1309: tree
1.1.1.7 root 1310: build_loop (filename, line, body)
1.1 root 1311: char *filename;
1312: int line;
1.1.1.7 root 1313: tree body;
1.1 root 1314: {
1.1.1.7 root 1315: register tree t = make_node (LOOP_STMT);
1.1 root 1316: STMT_SOURCE_FILE (t) = filename;
1317: STMT_SOURCE_LINE (t) = line;
1318: STMT_BODY (t) = body;
1319: return t;
1320: }
1321:
1322: tree
1.1.1.7 root 1323: build_compound (filename, line, body)
1.1 root 1324: char *filename;
1325: int line;
1326: tree body;
1327: {
1.1.1.7 root 1328: register tree t = make_node (COMPOUND_STMT);
1.1 root 1329: STMT_SOURCE_FILE (t) = filename;
1330: STMT_SOURCE_LINE (t) = line;
1331: STMT_BODY (t) = body;
1332: return t;
1333: }
1334:
1.1.1.7 root 1335: #endif /* 0 */
1336:
1337: /* LET_STMT nodes are used to represent the structure of binding contours
1338: and declarations, once those contours have been exited and their contents
1339: compiled. This information is used for outputting debugging info. */
1340:
1.1 root 1341: tree
1.1.1.7 root 1342: build_let (filename, line, vars, body, supercontext, tags)
1.1 root 1343: char *filename;
1344: int line;
1.1.1.7 root 1345: tree vars, body, supercontext, tags;
1.1 root 1346: {
1.1.1.7 root 1347: register tree t = make_node (LET_STMT);
1.1 root 1348: STMT_SOURCE_FILE (t) = filename;
1349: STMT_SOURCE_LINE (t) = line;
1.1.1.7 root 1350: STMT_VARS (t) = vars;
1.1 root 1351: STMT_BODY (t) = body;
1.1.1.7 root 1352: STMT_SUPERCONTEXT (t) = supercontext;
1353: STMT_BIND_SIZE (t) = 0;
1354: STMT_TYPE_TAGS (t) = tags;
1.1 root 1355: return t;
1356: }
1357:
1358: /* Return a type like TYPE except that its TREE_READONLY is CONSTP
1359: and its TREE_VOLATILE is VOLATILEP.
1360:
1361: Such variant types already made are recorded so that duplicates
1362: are not made.
1363:
1364: A variant types should never be used as the type of an expression.
1365: Always copy the variant information into the TREE_READONLY
1366: and TREE_VOLATILE of the expression, and then give the expression
1367: as its type the "main variant", the variant whose TREE_READONLY
1368: and TREE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
1369: main variant. */
1370:
1371: tree
1372: build_type_variant (type, constp, volatilep)
1373: tree type;
1374: int constp, volatilep;
1375: {
1376: register tree t, m = TYPE_MAIN_VARIANT (type);
1377: register struct obstack *ambient_obstack = current_obstack;
1378:
1379: /* Treat any nonzero argument as 1. */
1380: constp = !!constp;
1381: volatilep = !!volatilep;
1382:
1383: /* First search the chain variants for one that is what we want. */
1384:
1385: for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1386: if (constp == TREE_READONLY (t)
1387: && volatilep == TREE_VOLATILE (t))
1388: return t;
1389:
1390: /* We need a new one. */
1.1.1.2 root 1391: current_obstack
1392: = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
1.1 root 1393:
1394: t = copy_node (type);
1395: TREE_READONLY (t) = constp;
1396: TREE_VOLATILE (t) = volatilep;
1397: TYPE_POINTER_TO (t) = 0;
1.1.1.7 root 1398: TYPE_REFERENCE_TO (t) = 0;
1.1 root 1399:
1400: /* Add this type to the chain of variants of TYPE. */
1401: TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1402: TYPE_NEXT_VARIANT (m) = t;
1403:
1404: current_obstack = ambient_obstack;
1405: return t;
1406: }
1407:
1.1.1.2 root 1408: /* Hashing of types so that we don't make duplicates.
1409: The entry point is `type_hash_canon'. */
1410:
1411: /* Each hash table slot is a bucket containing a chain
1412: of these structures. */
1413:
1414: struct type_hash
1415: {
1416: struct type_hash *next; /* Next structure in the bucket. */
1417: int hashcode; /* Hash code of this type. */
1418: tree type; /* The type recorded here. */
1419: };
1420:
1421: /* Now here is the hash table. When recording a type, it is added
1422: to the slot whose index is the hash code mod the table size.
1423: Note that the hash table is used for several kinds of types
1424: (function types, array types and array index range types, for now).
1425: While all these live in the same table, they are completely independent,
1426: and the hash code is computed differently for each of these. */
1427:
1.1.1.7 root 1428: #define TYPE_HASH_SIZE 59
1.1.1.2 root 1429: struct type_hash *type_hash_table[TYPE_HASH_SIZE];
1430:
1431: /* Here is how primitive or already-canonicalized types' hash
1432: codes are made. */
1433: #define TYPE_HASH(TYPE) TREE_UID (TYPE)
1434:
1435: /* Compute a hash code for a list of types (chain of TREE_LIST nodes
1436: with types in the TREE_VALUE slots), by adding the hash codes
1437: of the individual types. */
1438:
1439: int
1440: type_hash_list (list)
1441: tree list;
1442: {
1443: register int hashcode;
1444: register tree tail;
1445: for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
1446: hashcode += TYPE_HASH (TREE_VALUE (tail));
1447: return hashcode;
1448: }
1449:
1450: /* Look in the type hash table for a type isomorphic to TYPE.
1451: If one is found, return it. Otherwise return 0. */
1452:
1453: tree
1454: type_hash_lookup (hashcode, type)
1455: int hashcode;
1456: tree type;
1457: {
1458: register struct type_hash *h;
1459: for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1460: if (h->hashcode == hashcode
1461: && TREE_CODE (h->type) == TREE_CODE (type)
1462: && TREE_TYPE (h->type) == TREE_TYPE (type)
1463: && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
1464: || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
1465: TYPE_MAX_VALUE (type)))
1466: && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
1467: || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
1468: TYPE_MIN_VALUE (type)))
1469: && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
1470: || (TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
1471: && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
1472: && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
1473: return h->type;
1474: return 0;
1475: }
1476:
1477: /* Add an entry to the type-hash-table
1478: for a type TYPE whose hash code is HASHCODE. */
1479:
1480: void
1481: type_hash_add (hashcode, type)
1482: int hashcode;
1483: tree type;
1484: {
1485: register struct type_hash *h;
1486:
1487: h = (struct type_hash *) oballoc (sizeof (struct type_hash));
1488: h->hashcode = hashcode;
1489: h->type = type;
1490: h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
1491: type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
1492: }
1493:
1494: /* Given TYPE, and HASHCODE its hash code, return the canonical
1495: object for an identical type if one already exists.
1496: Otherwise, return TYPE, and record it as the canonical object
1497: if it is a permanent object.
1498:
1499: To use this function, first create a type of the sort you want.
1500: Then compute its hash code from the fields of the type that
1501: make it different from other similar types.
1502: Then call this function and use the value.
1503: This function frees the type you pass in if it is a duplicate. */
1504:
1505: /* Set to 1 to debug without canonicalization. Never set by program. */
1506: int debug_no_type_hash = 0;
1507:
1508: tree
1509: type_hash_canon (hashcode, type)
1510: int hashcode;
1511: tree type;
1512: {
1513: tree t1;
1514:
1515: if (debug_no_type_hash)
1516: return type;
1517:
1518: t1 = type_hash_lookup (hashcode, type);
1519: if (t1 != 0)
1520: {
1521: struct obstack *o
1522: = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
1523: obstack_free (o, type);
1524: return t1;
1525: }
1526:
1527: /* If this is a new type, record it for later reuse. */
1528: if (current_obstack == &permanent_obstack)
1529: type_hash_add (hashcode, type);
1530:
1531: return type;
1532: }
1533:
1534: /* Given two lists of types
1535: (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
1.1.1.7 root 1536: return 1 if the lists contain the same types in the same order.
1537: Also, the TREE_PURPOSEs must match. */
1.1.1.2 root 1538:
1539: int
1540: type_list_equal (l1, l2)
1541: tree l1, l2;
1542: {
1543: register tree t1, t2;
1544: for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1.1.1.7 root 1545: {
1546: if (TREE_VALUE (t1) != TREE_VALUE (t2))
1547: return 0;
1548: if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
1549: && !simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
1550: return 0;
1551: }
1.1.1.2 root 1552:
1553: return t1 == t2;
1554: }
1555:
1556: /* Nonzero if integer constants T1 and T2
1557: represent the same constant value. */
1558:
1559: int
1560: tree_int_cst_equal (t1, t2)
1561: tree t1, t2;
1562: {
1563: if (t1 == t2)
1564: return 1;
1565: if (t1 == 0 || t2 == 0)
1566: return 0;
1567: if (TREE_CODE (t1) == INTEGER_CST
1568: && TREE_CODE (t2) == INTEGER_CST
1569: && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1570: && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
1571: return 1;
1572: return 0;
1573: }
1574:
1575: /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
1576: The precise way of comparison depends on their data type. */
1577:
1578: int
1579: tree_int_cst_lt (t1, t2)
1580: tree t1, t2;
1581: {
1582: if (t1 == t2)
1583: return 0;
1584:
1585: if (!TREE_UNSIGNED (TREE_TYPE (t1)))
1586: return INT_CST_LT (t1, t2);
1587: return INT_CST_LT_UNSIGNED (t1, t2);
1588: }
1.1.1.7 root 1589:
1590: /* Compare two constructor-element-type constants. */
1591:
1592: int
1593: simple_cst_equal (t1, t2)
1594: tree t1, t2;
1595: {
1596: register enum tree_code code1, code2;
1597:
1598: if (t1 == t2)
1599: return 1;
1600: if (t1 == 0 || t2 == 0)
1601: return 0;
1602:
1603: code1 = TREE_CODE (t1);
1604: code2 = TREE_CODE (t2);
1605:
1606: if (code1 == NOP_EXPR || code1 == CONVERT_EXPR)
1607: if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
1608: return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1609: else
1610: return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
1611: else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
1612: return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
1613:
1614: if (code1 != code2)
1615: return 0;
1616:
1617: switch (code1)
1618: {
1619: case INTEGER_CST:
1620: return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1621: && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1622:
1623: case REAL_CST:
1.1.1.12! root 1624: return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1.1.1.7 root 1625:
1626: case STRING_CST:
1627: return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1628: && !strcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2));
1629:
1630: case CONSTRUCTOR:
1631: abort ();
1632:
1633: case VAR_DECL:
1634: case PARM_DECL:
1635: case CONST_DECL:
1636: return 0;
1637:
1638: case PLUS_EXPR:
1639: case MINUS_EXPR:
1640: case MULT_EXPR:
1641: case TRUNC_DIV_EXPR:
1642: case TRUNC_MOD_EXPR:
1643: case LSHIFT_EXPR:
1644: case RSHIFT_EXPR:
1645: return (simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
1646: && simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
1647:
1648: case NEGATE_EXPR:
1649: case ADDR_EXPR:
1650: case REFERENCE_EXPR:
1651: return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1652:
1653: default:
1654: abort ();
1655: }
1656: }
1.1.1.2 root 1657:
1.1 root 1658: /* Constructors for pointer, array and function types.
1659: (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
1660: constructed by language-dependent code, not here.) */
1661:
1.1.1.2 root 1662: /* Construct, lay out and return the type of pointers to TO_TYPE.
1663: If such a type has already been constructed, reuse it. */
1664:
1.1 root 1665: tree
1666: build_pointer_type (to_type)
1667: tree to_type;
1668: {
1669: register tree t = TYPE_POINTER_TO (to_type);
1670: register struct obstack *ambient_obstack = current_obstack;
1.1.1.4 root 1671: register struct obstack *ambient_saveable_obstack = saveable_obstack;
1.1 root 1672:
1673: /* First, if we already have a type for pointers to TO_TYPE, use it. */
1674:
1675: if (t)
1676: return t;
1677:
1678: /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
1.1.1.4 root 1679: if (TREE_PERMANENT (to_type))
1680: {
1681: current_obstack = &permanent_obstack;
1682: saveable_obstack = &permanent_obstack;
1683: }
1.1 root 1684:
1685: t = make_node (POINTER_TYPE);
1686: TREE_TYPE (t) = to_type;
1687:
1688: /* Record this type as the pointer to TO_TYPE. */
1689: TYPE_POINTER_TO (to_type) = t;
1690:
1.1.1.2 root 1691: /* Lay out the type. This function has many callers that are concerned
1692: with expression-construction, and this simplifies them all.
1693: Also, it guarantees the TYPE_SIZE is permanent if the type is. */
1694: layout_type (t);
1.1 root 1695:
1696: current_obstack = ambient_obstack;
1.1.1.4 root 1697: saveable_obstack = ambient_saveable_obstack;
1.1 root 1698: return t;
1699: }
1700:
1.1.1.8 root 1701: /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
1702: MAXVAL should be the maximum value in the domain
1703: (one less than the length of the array). */
1704:
1705: tree
1706: build_index_type (maxval)
1707: tree maxval;
1708: {
1709: register tree itype = make_node (INTEGER_TYPE);
1710: int maxint = TREE_INT_CST_LOW (maxval);
1711: TYPE_PRECISION (itype) = BITS_PER_WORD;
1712: TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
1713: TREE_TYPE (TYPE_MIN_VALUE (itype)) = itype;
1714: TYPE_MAX_VALUE (itype) = maxval;
1715: TREE_TYPE (maxval) = itype;
1716: TYPE_MODE (itype) = SImode;
1717: TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
1718: TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
1719: TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
1720: return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
1721: }
1722:
1.1.1.2 root 1723: /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
1724: and number of elements specified by the range of values of INDEX_TYPE.
1725: If such a type has already been constructed, reuse it. */
1726:
1.1 root 1727: tree
1728: build_array_type (elt_type, index_type)
1729: tree elt_type, index_type;
1730: {
1731: register tree t = make_node (ARRAY_TYPE);
1.1.1.2 root 1732: int hashcode;
1.1 root 1733:
1734: if (TREE_CODE (elt_type) == FUNCTION_TYPE)
1735: {
1.1.1.2 root 1736: error ("arrays of functions are not meaningful");
1.1 root 1737: elt_type = integer_type_node;
1738: }
1739:
1740: TREE_TYPE (t) = elt_type;
1741: TYPE_DOMAIN (t) = index_type;
1.1.1.2 root 1742:
1.1 root 1743: /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
1744: build_pointer_type (elt_type);
1.1.1.2 root 1745:
1746: if (index_type == 0)
1747: return t;
1748:
1749: hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
1750: t = type_hash_canon (hashcode, t);
1751:
1752: if (TYPE_SIZE (t) == 0)
1753: layout_type (t);
1.1 root 1754: return t;
1755: }
1756:
1.1.1.2 root 1757: /* Construct, lay out and return
1758: the type of functions returning type VALUE_TYPE
1759: given arguments of types ARG_TYPES.
1760: ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
1761: are data type nodes for the arguments of the function.
1762: If such a type has already been constructed, reuse it. */
1.1 root 1763:
1764: tree
1765: build_function_type (value_type, arg_types)
1766: tree value_type, arg_types;
1767: {
1768: register tree t;
1.1.1.2 root 1769: int hashcode;
1.1 root 1770:
1.1.1.2 root 1771: if (TREE_CODE (value_type) == FUNCTION_TYPE
1.1 root 1772: || TREE_CODE (value_type) == ARRAY_TYPE)
1773: {
1.1.1.2 root 1774: error ("function return type cannot be function or array");
1.1 root 1775: value_type = integer_type_node;
1776: }
1777:
1.1.1.2 root 1778: /* Make a node of the sort we want. */
1.1 root 1779: t = make_node (FUNCTION_TYPE);
1780: TREE_TYPE (t) = value_type;
1781: TYPE_ARG_TYPES (t) = arg_types;
1.1.1.2 root 1782:
1783: /* If we already have such a type, use the old one and free this one. */
1784: hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
1785: t = type_hash_canon (hashcode, t);
1786:
1787: if (TYPE_SIZE (t) == 0)
1788: layout_type (t);
1.1 root 1789: return t;
1790: }
1.1.1.7 root 1791:
1792: /* Build the node for the type of references-to-TO_TYPE. */
1793:
1794: tree
1795: build_reference_type (to_type)
1796: tree to_type;
1797: {
1798: register tree t = TYPE_REFERENCE_TO (to_type);
1799: register struct obstack *ambient_obstack = current_obstack;
1800: register struct obstack *ambient_saveable_obstack = saveable_obstack;
1801:
1802: /* First, if we already have a type for pointers to TO_TYPE, use it. */
1803:
1804: if (t)
1805: return t;
1806:
1807: /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
1808: if (TREE_PERMANENT (to_type))
1809: {
1810: current_obstack = &permanent_obstack;
1811: saveable_obstack = &permanent_obstack;
1812: }
1813:
1814: t = make_node (REFERENCE_TYPE);
1815: TREE_TYPE (t) = to_type;
1816:
1817: /* Record this type as the pointer to TO_TYPE. */
1818: TYPE_REFERENCE_TO (to_type) = t;
1819:
1820: layout_type (t);
1821:
1822: current_obstack = ambient_obstack;
1823: saveable_obstack = ambient_saveable_obstack;
1824: return t;
1825: }
1826:
1827: /* Construct, lay out and return the type of methods belonging to class
1828: BASETYPE and whose arguments and values are described by TYPE.
1829: If that type exists already, reuse it.
1830: TYPE must be a FUNCTION_TYPE node. */
1831:
1832: tree
1833: build_method_type (basetype, type)
1834: tree basetype, type;
1835: {
1836: register tree t;
1837: int hashcode;
1838:
1839: /* Make a node of the sort we want. */
1840: t = make_node (METHOD_TYPE);
1841:
1842: if (TREE_CODE (type) != FUNCTION_TYPE)
1843: abort ();
1844:
1.1.1.11 root 1845: TYPE_METHOD_BASETYPE (t) = basetype;
1846: TREE_TYPE (t) = TREE_TYPE (type);
1.1.1.7 root 1847:
1848: /* The actual arglist for this function includes a "hidden" argument
1849: which is "this". Put it into the list of argument types. */
1850:
1851: TYPE_ARG_TYPES (t)
1852: = tree_cons (NULL, build_pointer_type (basetype), TYPE_ARG_TYPES (type));
1853:
1854: /* If we already have such a type, use the old one and free this one. */
1855: hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
1856: t = type_hash_canon (hashcode, t);
1857:
1858: if (TYPE_SIZE (t) == 0)
1859: layout_type (t);
1860:
1861: return t;
1862: }
1.1.1.11 root 1863:
1864: /* Construct, lay out and return the type of methods belonging to class
1865: BASETYPE and whose arguments and values are described by TYPE.
1866: If that type exists already, reuse it.
1867: TYPE must be a FUNCTION_TYPE node. */
1868:
1869: tree
1870: build_offset_type (basetype, type)
1871: tree basetype, type;
1872: {
1873: register tree t;
1874: int hashcode;
1875:
1876: /* Make a node of the sort we want. */
1877: t = make_node (OFFSET_TYPE);
1878:
1879: TYPE_OFFSET_BASETYPE (t) = basetype;
1880: TREE_TYPE (t) = type;
1881:
1882: /* If we already have such a type, use the old one and free this one. */
1883: hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
1884: t = type_hash_canon (hashcode, t);
1885:
1886: if (TYPE_SIZE (t) == 0)
1887: layout_type (t);
1888:
1889: return t;
1890: }
1.1 root 1891:
1892: /* Return OP, stripped of any conversions to wider types as much as is safe.
1893: Converting the value back to OP's type makes a value equivalent to OP.
1894:
1895: If FOR_TYPE is nonzero, we return a value which, if converted to
1896: type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
1897:
1.1.1.2 root 1898: If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
1899: narrowest type that can hold the value, even if they don't exactly fit.
1900: Otherwise, bit-field references are changed to a narrower type
1901: only if they can be fetched directly from memory in that type.
1902:
1.1 root 1903: OP must have integer, real or enumeral type. Pointers are not allowed!
1904:
1905: There are some cases where the obvious value we could return
1906: would regenerate to OP if converted to OP's type,
1907: but would not extend like OP to wider types.
1908: If FOR_TYPE indicates such extension is contemplated, we eschew such values.
1909: For example, if OP is (unsigned short)(signed char)-1,
1910: we avoid returning (signed char)-1 if FOR_TYPE is int,
1911: even though extending that to an unsigned short would regenerate OP,
1912: since the result of extending (signed char)-1 to (int)
1913: is different from (int) OP. */
1914:
1915: tree
1916: get_unwidened (op, for_type)
1917: register tree op;
1918: tree for_type;
1919: {
1920: /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
1921: /* TYPE_PRECISION is safe in place of type_precision since
1922: pointer types are not allowed. */
1923: register tree type = TREE_TYPE (op);
1924: register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
1925: register int uns
1926: = (for_type != 0 && for_type != type
1927: && final_prec > TYPE_PRECISION (type)
1.1.1.2 root 1928: && TREE_UNSIGNED (type));
1.1 root 1929: register tree win = op;
1930:
1931: while (TREE_CODE (op) == NOP_EXPR)
1932: {
1933: register int bitschange
1934: = TYPE_PRECISION (TREE_TYPE (op))
1935: - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
1936:
1937: /* Truncations are many-one so cannot be removed.
1938: Unless we are later going to truncate down even farther. */
1939: if (bitschange < 0
1940: && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
1941: break;
1942:
1943: /* See what's inside this conversion. If we decide to strip it,
1944: we will set WIN. */
1945: op = TREE_OPERAND (op, 0);
1946:
1947: /* If we have not stripped any zero-extensions (uns is 0),
1948: we can strip any kind of extension.
1949: If we have previously stripped a zero-extension,
1950: only zero-extensions can safely be stripped.
1951: Any extension can be stripped if the bits it would produce
1952: are all going to be discarded later by truncating to FOR_TYPE. */
1953:
1954: if (bitschange > 0)
1955: {
1956: if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
1957: win = op;
1.1.1.2 root 1958: /* TREE_UNSIGNED says whether this is a zero-extension.
1.1 root 1959: Let's avoid computing it if it does not affect WIN
1960: and if UNS will not be needed again. */
1961: if ((uns || TREE_CODE (op) == NOP_EXPR)
1.1.1.2 root 1962: && TREE_UNSIGNED (TREE_TYPE (op)))
1.1 root 1963: {
1964: uns = 1;
1965: win = op;
1966: }
1967: }
1968: }
1969:
1.1.1.2 root 1970: if (TREE_CODE (op) == COMPONENT_REF
1971: /* Since type_for_size always gives an integer type. */
1972: && TREE_CODE (type) != REAL_TYPE)
1973: {
1974: int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
1975: * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
1976: type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
1977:
1978: /* We can get this structure field in the narrowest type it fits in.
1979: If FOR_TYPE is 0, do this only for a field that matches the
1980: narrower type exactly and is aligned for it (i.e. mode isn't BI).
1981: The resulting extension to its nominal type (a fullword type)
1982: must fit the same conditions as for other extensions. */
1983:
1.1.1.3 root 1984: if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
1.1.1.2 root 1985: && (for_type || DECL_MODE (TREE_OPERAND (op, 1)) != BImode)
1986: && (! uns || final_prec <= innerprec
1987: || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
1988: && type != 0)
1989: {
1990: win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
1991: TREE_OPERAND (op, 1));
1992: TREE_VOLATILE (win) = TREE_VOLATILE (op);
1993: TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
1994: }
1995: }
1.1 root 1996: return win;
1997: }
1998:
1999: /* Return OP or a simpler expression for a narrower value
2000: which can be sign-extended or zero-extended to give back OP.
2001: Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
2002: or 0 if the value should be sign-extended. */
2003:
2004: tree
2005: get_narrower (op, unsignedp_ptr)
2006: register tree op;
2007: int *unsignedp_ptr;
2008: {
2009: register int uns = 0;
2010: int first = 1;
2011: register tree win = op;
2012:
2013: while (TREE_CODE (op) == NOP_EXPR)
2014: {
2015: register int bitschange
2016: = TYPE_PRECISION (TREE_TYPE (op))
2017: - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
2018:
2019: /* Truncations are many-one so cannot be removed. */
2020: if (bitschange < 0)
2021: break;
2022:
2023: /* See what's inside this conversion. If we decide to strip it,
2024: we will set WIN. */
2025: op = TREE_OPERAND (op, 0);
2026:
2027: if (bitschange > 0)
2028: {
2029: /* An extension: the outermost one can be stripped,
2030: but remember whether it is zero or sign extension. */
2031: if (first)
1.1.1.2 root 2032: uns = TREE_UNSIGNED (TREE_TYPE (op));
1.1 root 2033: /* Otherwise, if a sign extension has been stripped,
2034: only sign extensions can now be stripped;
2035: if a zero extension has been stripped, only zero-extensions. */
1.1.1.2 root 2036: else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
1.1 root 2037: break;
2038: first = 0;
2039: }
2040: /* A change in nominal type can always be stripped. */
2041:
2042: win = op;
2043: }
2044:
1.1.1.2 root 2045: if (TREE_CODE (op) == COMPONENT_REF
2046: /* Since type_for_size always gives an integer type. */
2047: && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
1.1 root 2048: {
2049: int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
2050: * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
1.1.1.2 root 2051: tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
1.1 root 2052:
2053: /* We can get this structure field in a narrower type that fits it,
2054: but the resulting extension to its nominal type (a fullword type)
1.1.1.2 root 2055: must satisfy the same conditions as for other extensions.
2056:
2057: Do this only for fields that are aligned (not BImode),
2058: because when bit-field insns will be used there is no
2059: advantage in doing this. */
1.1 root 2060:
2061: if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
1.1.1.2 root 2062: && DECL_MODE (TREE_OPERAND (op, 1)) != BImode
2063: && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
1.1 root 2064: && type != 0)
2065: {
1.1.1.2 root 2066: if (first)
2067: uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
2068: win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
2069: TREE_OPERAND (op, 1));
2070: TREE_VOLATILE (win) = TREE_VOLATILE (op);
2071: TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
1.1 root 2072: }
2073: }
2074: *unsignedp_ptr = uns;
2075: return win;
2076: }
2077:
2078: /* Return the precision of a type, for arithmetic purposes.
2079: Supports all types on which arithmetic is possible
2080: (including pointer types).
2081: It's not clear yet what will be right for complex types. */
2082:
2083: int
2084: type_precision (type)
2085: register tree type;
2086: {
2087: return ((TREE_CODE (type) == INTEGER_TYPE
2088: || TREE_CODE (type) == ENUMERAL_TYPE
2089: || TREE_CODE (type) == REAL_TYPE)
1.1.1.2 root 2090: ? TYPE_PRECISION (type) : POINTER_SIZE);
1.1 root 2091: }
2092:
2093: /* Nonzero if integer constant C has a value that is permissible
2094: for type TYPE (an INTEGER_TYPE). */
2095:
2096: int
2097: int_fits_type_p (c, type)
2098: tree c, type;
2099: {
1.1.1.2 root 2100: if (TREE_UNSIGNED (type))
1.1 root 2101: return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
2102: && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
2103: else
2104: return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
2105: && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
2106: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.