|
|
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 root 569: double d;
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:
588: tree
1.1.1.4 root 589: build_real_from_int_cst (type, i)
590: tree type;
1.1 root 591: tree i;
592: {
593: tree v;
1.1.1.2 root 594: double d;
1.1 root 595:
596: v = make_node (REAL_CST);
1.1.1.4 root 597: TREE_TYPE (v) = type;
1.1.1.2 root 598:
599: if (TREE_INT_CST_HIGH (i) < 0)
600: {
601: d = (double) (~ TREE_INT_CST_HIGH (i));
602: d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
603: * (double) (1 << (HOST_BITS_PER_INT / 2)));
604: d += (double) (unsigned) (~ TREE_INT_CST_LOW (i));
605: d = (- d - 1.0);
606: }
607: else
608: {
609: d = (double) TREE_INT_CST_HIGH (i);
610: d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
611: * (double) (1 << (HOST_BITS_PER_INT / 2)));
612: d += (double) (unsigned) TREE_INT_CST_LOW (i);
613: }
614:
1.1.1.6 root 615: /* Check for valid float value for this type on this target machine;
616: if not, can print error message and store a valid value in D. */
617: #ifdef CHECK_FLOAT_VALUE
618: CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
619: #endif
620:
1.1.1.2 root 621: TREE_REAL_CST (v) = d;
1.1 root 622: return v;
623: }
624:
625: /* Return a newly constructed STRING_CST node whose value is
626: the LEN characters at STR.
627: The TREE_TYPE is not initialized. */
628:
629: tree
630: build_string (len, str)
631: int len;
632: char *str;
633: {
634: register tree s = make_node (STRING_CST);
635: TREE_STRING_LENGTH (s) = len;
1.1.1.2 root 636: TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1.1 root 637: return s;
638: }
639:
640: /* Return a newly constructed COMPLEX_CST node whose value is
641: specified by the real and imaginary parts REAL and IMAG.
642: Both REAL and IMAG should be constant nodes.
643: The TREE_TYPE is not initialized. */
644:
645: tree
646: build_complex (real, imag)
647: tree real, imag;
648: {
649: register tree t = make_node (COMPLEX_CST);
650: TREE_REALPART (t) = real;
651: TREE_IMAGPART (t) = imag;
652: return t;
653: }
654:
655: /* Return 1 if EXPR is the integer constant zero. */
656:
657: int
658: integer_zerop (expr)
659: tree expr;
660: {
1.1.1.2 root 661: return (TREE_CODE (expr) == INTEGER_CST
662: && TREE_INT_CST_LOW (expr) == 0
663: && TREE_INT_CST_HIGH (expr) == 0);
1.1 root 664: }
665:
666: /* Return 1 if EXPR is the integer constant one. */
667:
668: int
669: integer_onep (expr)
670: tree expr;
671: {
1.1.1.2 root 672: return (TREE_CODE (expr) == INTEGER_CST
673: && TREE_INT_CST_LOW (expr) == 1
674: && TREE_INT_CST_HIGH (expr) == 0);
1.1 root 675: }
676:
677: /* Return 1 if EXPR is an integer containing all 1's
678: in as much precision as it contains. */
679:
680: int
681: integer_all_onesp (expr)
682: tree expr;
683: {
684: register int prec;
685: register int uns;
686:
687: if (TREE_CODE (expr) != INTEGER_CST)
688: return 0;
689:
1.1.1.2 root 690: uns = TREE_UNSIGNED (TREE_TYPE (expr));
1.1 root 691: if (!uns)
692: return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
693:
694: prec = TYPE_PRECISION (TREE_TYPE (expr));
695: if (prec >= HOST_BITS_PER_INT)
696: return TREE_INT_CST_LOW (expr) == -1
697: && TREE_INT_CST_HIGH (expr) == (1 << (prec - HOST_BITS_PER_INT)) - 1;
698: else
699: return TREE_INT_CST_LOW (expr) == (1 << prec) - 1;
700: }
701:
702: /* Return the length of a chain of nodes chained through TREE_CHAIN.
703: We expect a null pointer to mark the end of the chain.
704: This is the Lisp primitive `length'. */
705:
706: int
707: list_length (t)
708: tree t;
709: {
710: register tree tail;
711: register int len = 0;
712:
713: for (tail = t; tail; tail = TREE_CHAIN (tail))
714: len++;
715:
716: return len;
717: }
718:
719: /* Concatenate two chains of nodes (chained through TREE_CHAIN)
720: by modifying the last node in chain 1 to point to chain 2.
721: This is the Lisp primitive `nconc'. */
722:
723: tree
724: chainon (op1, op2)
725: tree op1, op2;
726: {
727: tree t;
728:
729: if (op1)
730: {
731: for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
732: if (t == op2) abort (); /* Circularity being created */
733: TREE_CHAIN (t) = op2;
734: return op1;
735: }
736: else return op2;
737: }
738:
739: /* Return a newly created TREE_LIST node whose
740: purpose and value fields are PARM and VALUE. */
741:
742: tree
743: build_tree_list (parm, value)
744: tree parm, value;
745: {
746: register tree t = make_node (TREE_LIST);
747: TREE_PURPOSE (t) = parm;
748: TREE_VALUE (t) = value;
749: return t;
750: }
751:
752: /* Return a newly created TREE_LIST node whose
753: purpose and value fields are PARM and VALUE
754: and whose TREE_CHAIN is CHAIN. */
755:
756: tree
757: tree_cons (purpose, value, chain)
758: tree purpose, value, chain;
759: {
760: register tree node = make_node (TREE_LIST);
761: TREE_CHAIN (node) = chain;
762: TREE_PURPOSE (node) = purpose;
763: TREE_VALUE (node) = value;
764: return node;
765: }
766:
1.1.1.2 root 767: /* Same as `tree_cons' but make a permanent object. */
768:
769: tree
770: perm_tree_cons (purpose, value, chain)
771: tree purpose, value, chain;
772: {
773: register tree node;
774: register struct obstack *ambient_obstack = current_obstack;
775: current_obstack = &permanent_obstack;
776:
777: node = make_node (TREE_LIST);
778: TREE_CHAIN (node) = chain;
779: TREE_PURPOSE (node) = purpose;
780: TREE_VALUE (node) = value;
781:
782: current_obstack = ambient_obstack;
783: return node;
784: }
785:
1.1.1.7 root 786: /* Same as `tree_cons', but make this node temporary, regardless. */
787:
788: tree
789: temp_tree_cons (purpose, value, chain)
790: tree purpose, value, chain;
791: {
792: register tree node;
793: register struct obstack *ambient_obstack = current_obstack;
794: current_obstack = &temporary_obstack;
795:
796: node = make_node (TREE_LIST);
797: TREE_CHAIN (node) = chain;
798: TREE_PURPOSE (node) = purpose;
799: TREE_VALUE (node) = value;
800:
801: current_obstack = ambient_obstack;
802: return node;
803: }
804:
1.1.1.9 root 805: /* Same as `tree_cons', but save this node if the function's RTL is saved. */
806:
807: tree
808: saveable_tree_cons (purpose, value, chain)
809: tree purpose, value, chain;
810: {
811: register tree node;
812: register struct obstack *ambient_obstack = current_obstack;
813: current_obstack = saveable_obstack;
814:
815: node = make_node (TREE_LIST);
816: TREE_CHAIN (node) = chain;
817: TREE_PURPOSE (node) = purpose;
818: TREE_VALUE (node) = value;
819:
820: current_obstack = ambient_obstack;
821: return node;
822: }
823:
1.1 root 824: /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
825:
826: tree
827: tree_last (chain)
828: register tree chain;
829: {
830: register tree next;
831: if (chain)
832: while (next = TREE_CHAIN (chain))
833: chain = next;
834: return chain;
835: }
836:
837: /* Reverse the order of elements in the chain T,
838: and return the new head of the chain (old last element). */
839:
840: tree
841: nreverse (t)
842: tree t;
843: {
844: register tree prev = 0, decl, next;
845: for (decl = t; decl; decl = next)
846: {
847: next = TREE_CHAIN (decl);
848: TREE_CHAIN (decl) = prev;
849: prev = decl;
850: }
851: return prev;
852: }
853:
854: /* Return the size nominally occupied by an object of type TYPE
855: when it resides in memory. The value is measured in units of bytes,
856: and its data type is that normally used for type sizes
857: (which is the first type created by make_signed_type or
858: make_unsigned_type). */
859:
860: tree
861: size_in_bytes (type)
862: tree type;
863: {
864: if (type == error_mark_node)
865: return integer_zero_node;
1.1.1.2 root 866: if (TYPE_SIZE (type) == 0)
867: {
868: incomplete_type_error (0, type);
869: return integer_zero_node;
870: }
1.1 root 871: return convert_units (TYPE_SIZE (type), TYPE_SIZE_UNIT (type),
872: BITS_PER_UNIT);
873: }
874:
1.1.1.2 root 875: /* Return the size of TYPE (in bytes) as an integer,
876: or return -1 if the size can vary. */
877:
878: int
879: int_size_in_bytes (type)
880: tree type;
881: {
882: int size;
883: if (type == error_mark_node)
884: return 0;
885: if (TYPE_SIZE (type) == 0)
886: return -1;
887: if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
888: return -1;
889: size = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
890: return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
891: }
892:
1.1.1.7 root 893: /* Return, as an INTEGER_CST node, the number of elements for
894: TYPE (which is an ARRAY_TYPE). */
895:
896: tree
897: array_type_nelts (type)
898: tree type;
899: {
900: tree index_type = TYPE_DOMAIN (type);
901: return (tree_int_cst_equal (TYPE_MIN_VALUE (index_type), integer_zero_node)
902: ? TYPE_MAX_VALUE (index_type)
903: : fold (build (MINUS_EXPR, integer_type_node,
904: TYPE_MAX_VALUE (index_type),
905: TYPE_MIN_VALUE (index_type))));
906: }
907:
1.1 root 908: /* Return nonzero if arg is static -- a reference to an object in
909: static storage. This is not the same as the C meaning of `static'. */
910:
911: int
912: staticp (arg)
913: tree arg;
914: {
915: register enum tree_code code = TREE_CODE (arg);
916:
1.1.1.2 root 917: if ((code == VAR_DECL || code == FUNCTION_DECL || code == CONSTRUCTOR)
1.1 root 918: && (TREE_STATIC (arg) || TREE_EXTERNAL (arg)))
919: return 1;
920:
1.1.1.2 root 921: if (code == STRING_CST)
922: return 1;
923:
1.1 root 924: if (code == COMPONENT_REF)
1.1.1.10 root 925: return (DECL_VOFFSET (TREE_OPERAND (arg, 1)) == 0
926: && staticp (TREE_OPERAND (arg, 0)));
927:
928: if (code == INDIRECT_REF)
929: return TREE_LITERAL (TREE_OPERAND (arg, 0));
1.1 root 930:
1.1.1.2 root 931: if (code == ARRAY_REF)
932: {
933: if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
934: && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
935: return staticp (TREE_OPERAND (arg, 0));
936: }
937:
1.1 root 938: return 0;
939: }
940:
1.1.1.2 root 941: /* Return nonzero if REF is an lvalue valid for this language.
942: Lvalues can be assigned, unless they have TREE_READONLY.
943: Lvalues can have their address taken, unless they have TREE_REGDECL. */
1.1 root 944:
945: int
1.1.1.2 root 946: lvalue_p (ref)
1.1 root 947: tree ref;
948: {
949: register enum tree_code code = TREE_CODE (ref);
950:
1.1.1.2 root 951: if (language_lvalue_valid (ref))
952: switch (code)
953: {
954: case COMPONENT_REF:
955: return lvalue_p (TREE_OPERAND (ref, 0));
956:
957: case STRING_CST:
958: return 1;
959:
960: case INDIRECT_REF:
961: case ARRAY_REF:
962: case VAR_DECL:
963: case PARM_DECL:
964: case RESULT_DECL:
965: case ERROR_MARK:
966: if (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE)
967: return 1;
1.1.1.7 root 968: break;
969:
970: case CALL_EXPR:
971: if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
972: return 1;
1.1.1.2 root 973: }
1.1 root 974: return 0;
975: }
1.1.1.2 root 976:
977: /* Return nonzero if REF is an lvalue valid for this language;
978: otherwise, print an error message and return zero. */
979:
980: int
981: lvalue_or_else (ref, string)
982: tree ref;
983: char *string;
984: {
985: int win = lvalue_p (ref);
986: if (! win)
987: error ("invalid lvalue in %s", string);
988: return win;
989: }
1.1 root 990:
991: /* This should be applied to any node which may be used in more than one place,
992: but must be evaluated only once. Normally, the code generator would
993: reevaluate the node each time; this forces it to compute it once and save
994: the result. This is done by encapsulating the node in a SAVE_EXPR. */
995:
996: tree
997: save_expr (expr)
998: tree expr;
999: {
1000: register tree t = fold (expr);
1001:
1.1.1.2 root 1002: /* If the tree evaluates to a constant, then we don't want to hide that
1.1 root 1003: fact (i.e. this allows further folding, and direct checks for constants).
1004: Since it is no problem to reevaluate literals, we just return the
1005: literal node. */
1006:
1007: if (TREE_LITERAL (t) || TREE_READONLY (t) || TREE_CODE (t) == SAVE_EXPR)
1008: return t;
1009:
1.1.1.2 root 1010: return build (SAVE_EXPR, TREE_TYPE (expr), t, NULL);
1.1 root 1011: }
1012:
1013: /* Stabilize a reference so that we can use it any number of times
1014: without causing its operands to be evaluated more than once.
1.1.1.2 root 1015: Returns the stabilized reference.
1016:
1017: Also allows conversion expressions whose operands are references.
1018: Any other kind of expression is returned unchanged. */
1.1 root 1019:
1020: tree
1021: stabilize_reference (ref)
1022: tree ref;
1023: {
1024: register tree result;
1025: register enum tree_code code = TREE_CODE (ref);
1026:
1.1.1.2 root 1027: switch (code)
1.1 root 1028: {
1.1.1.2 root 1029: case VAR_DECL:
1030: case PARM_DECL:
1031: case RESULT_DECL:
1.1 root 1032: result = ref;
1.1.1.2 root 1033: break;
1034:
1035: case NOP_EXPR:
1036: case CONVERT_EXPR:
1037: case FLOAT_EXPR:
1038: case FIX_TRUNC_EXPR:
1039: case FIX_FLOOR_EXPR:
1040: case FIX_ROUND_EXPR:
1041: case FIX_CEIL_EXPR:
1042: result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
1043: break;
1044:
1045: case INDIRECT_REF:
1046: result = build_nt (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
1047: break;
1048:
1049: case COMPONENT_REF:
1050: result = build_nt (COMPONENT_REF,
1051: stabilize_reference (TREE_OPERAND (ref, 0)),
1052: TREE_OPERAND (ref, 1));
1053: break;
1054:
1055: case ARRAY_REF:
1056: result = build_nt (ARRAY_REF, stabilize_reference (TREE_OPERAND (ref, 0)),
1057: save_expr (TREE_OPERAND (ref, 1)));
1058: break;
1059:
1060: /* If arg isn't a kind of lvalue we recognize, make no change.
1061: Caller should recognize the error for an invalid lvalue. */
1062: default:
1063: return ref;
1064:
1065: case ERROR_MARK:
1.1 root 1066: return error_mark_node;
1067: }
1068:
1069: TREE_TYPE (result) = TREE_TYPE (ref);
1.1.1.2 root 1070: TREE_READONLY (result) = TREE_READONLY (ref);
1.1 root 1071: TREE_VOLATILE (result) = TREE_VOLATILE (ref);
1.1.1.2 root 1072: TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
1.1 root 1073:
1074: return result;
1075: }
1076:
1077: /* Low-level constructors for expressions. */
1078:
1.1.1.2 root 1079: /* Build an expression of code CODE, data type TYPE,
1080: and operands as specified by the arguments ARG1 and following arguments.
1081: Expressions and reference nodes can be created this way.
1082: Constants, decls, types and misc nodes cannot be. */
1.1 root 1083:
1084: tree
1.1.1.2 root 1085: build (va_alist)
1086: va_dcl
1.1 root 1087: {
1.1.1.2 root 1088: register va_list p;
1089: enum tree_code code;
1090: register tree t;
1091: register int length;
1092: register int i;
1093:
1094: va_start (p);
1.1 root 1095:
1.1.1.2 root 1096: code = va_arg (p, enum tree_code);
1097: t = make_node (code);
1098: length = tree_code_length[(int) code];
1099: TREE_TYPE (t) = va_arg (p, tree);
1100:
1101: if (length == 2)
1102: {
1103: /* This is equivalent to the loop below, but faster. */
1104: register tree arg0 = va_arg (p, tree);
1105: register tree arg1 = va_arg (p, tree);
1106: TREE_OPERAND (t, 0) = arg0;
1107: TREE_OPERAND (t, 1) = arg1;
1108: TREE_VOLATILE (t)
1109: = (arg0 && TREE_VOLATILE (arg0)) || (arg1 && TREE_VOLATILE (arg1));
1110: }
1111: else
1112: {
1113: for (i = 0; i < length; i++)
1114: {
1115: register tree operand = va_arg (p, tree);
1116: TREE_OPERAND (t, i) = operand;
1117: if (operand && TREE_VOLATILE (operand))
1118: TREE_VOLATILE (t) = 1;
1119: }
1120: }
1121: va_end (p);
1.1 root 1122: return t;
1123: }
1124:
1.1.1.2 root 1125: /* Similar except don't specify the TREE_TYPE
1126: and leave the TREE_VOLATILE as 0.
1127: It is permissible for arguments to be null,
1128: or even garbage if their values do not matter. */
1.1 root 1129:
1130: tree
1.1.1.2 root 1131: build_nt (va_alist)
1132: va_dcl
1.1 root 1133: {
1.1.1.2 root 1134: register va_list p;
1135: register enum tree_code code;
1136: register tree t;
1137: register int length;
1138: register int i;
1139:
1140: va_start (p);
1.1 root 1141:
1.1.1.2 root 1142: code = va_arg (p, enum tree_code);
1143: t = make_node (code);
1144: length = tree_code_length[(int) code];
1145:
1146: for (i = 0; i < length; i++)
1147: TREE_OPERAND (t, i) = va_arg (p, tree);
1148:
1149: va_end (p);
1.1 root 1150: return t;
1151: }
1.1.1.11! root 1152:
! 1153: tree
! 1154: build_op_identifier (op1, op2)
! 1155: tree op1, op2;
! 1156: {
! 1157: register tree t = make_node (OP_IDENTIFIER);
! 1158: TREE_PURPOSE (t) = op1;
! 1159: TREE_VALUE (t) = op2;
! 1160: return t;
! 1161: }
1.1.1.2 root 1162:
1163: /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
1164: We do NOT enter this node in any sort of symbol table.
1.1 root 1165:
1.1.1.2 root 1166: layout_decl is used to set up the decl's storage layout.
1167: Other slots are initialized to 0 or null pointers. */
1.1 root 1168:
1169: tree
1.1.1.2 root 1170: build_decl (code, name, type)
1171: enum tree_code code;
1172: tree name, type;
1173: {
1174: register tree t;
1175:
1176: t = make_node (code);
1177:
1178: /* if (type == error_mark_node)
1179: type = integer_type_node; */
1180: /* That is not done, deliberately, so that having error_mark_node
1181: as the type can suppress useless errors in the use of this variable. */
1182:
1183: DECL_NAME (t) = name;
1184: TREE_TYPE (t) = type;
1185: DECL_ARGUMENTS (t) = NULL_TREE;
1186: DECL_INITIAL (t) = NULL_TREE;
1187:
1188: if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
1189: layout_decl (t, 0);
1190: else if (code == FUNCTION_DECL)
1191: DECL_MODE (t) = FUNCTION_MODE;
1192:
1.1 root 1193: return t;
1194: }
1195:
1.1.1.7 root 1196: #if 0
1.1 root 1197: /* Low-level constructors for statements.
1198: These constructors all expect source file name and line number
1199: as arguments, as well as enough arguments to fill in the data
1200: in the statement node. */
1201:
1202: tree
1203: build_goto (filename, line, label)
1204: char *filename;
1205: int line;
1206: tree label;
1207: {
1208: register tree t = make_node (GOTO_STMT);
1209: STMT_SOURCE_FILE (t) = filename;
1210: STMT_SOURCE_LINE (t) = line;
1211: STMT_BODY (t) = label;
1212: return t;
1213: }
1214:
1215: tree
1216: build_return (filename, line, arg)
1217: char *filename;
1218: int line;
1219: tree arg;
1220: {
1221: register tree t = make_node (RETURN_STMT);
1222:
1223: STMT_SOURCE_FILE (t) = filename;
1224: STMT_SOURCE_LINE (t) = line;
1225: STMT_BODY (t) = arg;
1226: return t;
1227: }
1228:
1229: tree
1230: build_expr_stmt (filename, line, expr)
1231: char *filename;
1232: int line;
1233: tree expr;
1234: {
1235: register tree t = make_node (EXPR_STMT);
1236:
1237: STMT_SOURCE_FILE (t) = filename;
1238: STMT_SOURCE_LINE (t) = line;
1239: STMT_BODY (t) = expr;
1240: return t;
1241: }
1242:
1243: tree
1244: build_if (filename, line, cond, thenclause, elseclause)
1245: char *filename;
1246: int line;
1247: tree cond, thenclause, elseclause;
1248: {
1249: register tree t = make_node (IF_STMT);
1250:
1251: STMT_SOURCE_FILE (t) = filename;
1252: STMT_SOURCE_LINE (t) = line;
1253: STMT_COND (t) = cond;
1254: STMT_THEN (t) = thenclause;
1255: STMT_ELSE (t) = elseclause;
1256: return t;
1257: }
1258:
1259: tree
1260: build_exit (filename, line, cond)
1261: char *filename;
1262: int line;
1263: tree cond;
1264: {
1265: register tree t = make_node (EXIT_STMT);
1266: STMT_SOURCE_FILE (t) = filename;
1267: STMT_SOURCE_LINE (t) = line;
1268: STMT_BODY (t) = cond;
1269: return t;
1270: }
1271:
1272: tree
1273: build_asm_stmt (filename, line, asmcode)
1274: char *filename;
1275: int line;
1276: tree asmcode;
1277: {
1278: register tree t = make_node (ASM_STMT);
1279: STMT_SOURCE_FILE (t) = filename;
1280: STMT_SOURCE_LINE (t) = line;
1281: STMT_BODY (t) = asmcode;
1282: return t;
1283: }
1284:
1285: tree
1286: build_case (filename, line, object, cases)
1287: char *filename;
1288: int line;
1289: tree object, cases;
1290: {
1291: register tree t = make_node (CASE_STMT);
1292: STMT_SOURCE_FILE (t) = filename;
1293: STMT_SOURCE_LINE (t) = line;
1294: STMT_CASE_INDEX (t) = object;
1295: STMT_CASE_LIST (t) = cases;
1296: return t;
1297: }
1298:
1299: tree
1.1.1.7 root 1300: build_loop (filename, line, body)
1.1 root 1301: char *filename;
1302: int line;
1.1.1.7 root 1303: tree body;
1.1 root 1304: {
1.1.1.7 root 1305: register tree t = make_node (LOOP_STMT);
1.1 root 1306: STMT_SOURCE_FILE (t) = filename;
1307: STMT_SOURCE_LINE (t) = line;
1308: STMT_BODY (t) = body;
1309: return t;
1310: }
1311:
1312: tree
1.1.1.7 root 1313: build_compound (filename, line, body)
1.1 root 1314: char *filename;
1315: int line;
1316: tree body;
1317: {
1.1.1.7 root 1318: register tree t = make_node (COMPOUND_STMT);
1.1 root 1319: STMT_SOURCE_FILE (t) = filename;
1320: STMT_SOURCE_LINE (t) = line;
1321: STMT_BODY (t) = body;
1322: return t;
1323: }
1324:
1.1.1.7 root 1325: #endif /* 0 */
1326:
1327: /* LET_STMT nodes are used to represent the structure of binding contours
1328: and declarations, once those contours have been exited and their contents
1329: compiled. This information is used for outputting debugging info. */
1330:
1.1 root 1331: tree
1.1.1.7 root 1332: build_let (filename, line, vars, body, supercontext, tags)
1.1 root 1333: char *filename;
1334: int line;
1.1.1.7 root 1335: tree vars, body, supercontext, tags;
1.1 root 1336: {
1.1.1.7 root 1337: register tree t = make_node (LET_STMT);
1.1 root 1338: STMT_SOURCE_FILE (t) = filename;
1339: STMT_SOURCE_LINE (t) = line;
1.1.1.7 root 1340: STMT_VARS (t) = vars;
1.1 root 1341: STMT_BODY (t) = body;
1.1.1.7 root 1342: STMT_SUPERCONTEXT (t) = supercontext;
1343: STMT_BIND_SIZE (t) = 0;
1344: STMT_TYPE_TAGS (t) = tags;
1.1 root 1345: return t;
1346: }
1347:
1348: /* Return a type like TYPE except that its TREE_READONLY is CONSTP
1349: and its TREE_VOLATILE is VOLATILEP.
1350:
1351: Such variant types already made are recorded so that duplicates
1352: are not made.
1353:
1354: A variant types should never be used as the type of an expression.
1355: Always copy the variant information into the TREE_READONLY
1356: and TREE_VOLATILE of the expression, and then give the expression
1357: as its type the "main variant", the variant whose TREE_READONLY
1358: and TREE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
1359: main variant. */
1360:
1361: tree
1362: build_type_variant (type, constp, volatilep)
1363: tree type;
1364: int constp, volatilep;
1365: {
1366: register tree t, m = TYPE_MAIN_VARIANT (type);
1367: register struct obstack *ambient_obstack = current_obstack;
1368:
1369: /* Treat any nonzero argument as 1. */
1370: constp = !!constp;
1371: volatilep = !!volatilep;
1372:
1373: /* First search the chain variants for one that is what we want. */
1374:
1375: for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1376: if (constp == TREE_READONLY (t)
1377: && volatilep == TREE_VOLATILE (t))
1378: return t;
1379:
1380: /* We need a new one. */
1.1.1.2 root 1381: current_obstack
1382: = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
1.1 root 1383:
1384: t = copy_node (type);
1385: TREE_READONLY (t) = constp;
1386: TREE_VOLATILE (t) = volatilep;
1387: TYPE_POINTER_TO (t) = 0;
1.1.1.7 root 1388: TYPE_REFERENCE_TO (t) = 0;
1.1 root 1389:
1390: /* Add this type to the chain of variants of TYPE. */
1391: TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1392: TYPE_NEXT_VARIANT (m) = t;
1393:
1394: current_obstack = ambient_obstack;
1395: return t;
1396: }
1397:
1.1.1.2 root 1398: /* Hashing of types so that we don't make duplicates.
1399: The entry point is `type_hash_canon'. */
1400:
1401: /* Each hash table slot is a bucket containing a chain
1402: of these structures. */
1403:
1404: struct type_hash
1405: {
1406: struct type_hash *next; /* Next structure in the bucket. */
1407: int hashcode; /* Hash code of this type. */
1408: tree type; /* The type recorded here. */
1409: };
1410:
1411: /* Now here is the hash table. When recording a type, it is added
1412: to the slot whose index is the hash code mod the table size.
1413: Note that the hash table is used for several kinds of types
1414: (function types, array types and array index range types, for now).
1415: While all these live in the same table, they are completely independent,
1416: and the hash code is computed differently for each of these. */
1417:
1.1.1.7 root 1418: #define TYPE_HASH_SIZE 59
1.1.1.2 root 1419: struct type_hash *type_hash_table[TYPE_HASH_SIZE];
1420:
1421: /* Here is how primitive or already-canonicalized types' hash
1422: codes are made. */
1423: #define TYPE_HASH(TYPE) TREE_UID (TYPE)
1424:
1425: /* Compute a hash code for a list of types (chain of TREE_LIST nodes
1426: with types in the TREE_VALUE slots), by adding the hash codes
1427: of the individual types. */
1428:
1429: int
1430: type_hash_list (list)
1431: tree list;
1432: {
1433: register int hashcode;
1434: register tree tail;
1435: for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
1436: hashcode += TYPE_HASH (TREE_VALUE (tail));
1437: return hashcode;
1438: }
1439:
1440: /* Look in the type hash table for a type isomorphic to TYPE.
1441: If one is found, return it. Otherwise return 0. */
1442:
1443: tree
1444: type_hash_lookup (hashcode, type)
1445: int hashcode;
1446: tree type;
1447: {
1448: register struct type_hash *h;
1449: for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1450: if (h->hashcode == hashcode
1451: && TREE_CODE (h->type) == TREE_CODE (type)
1452: && TREE_TYPE (h->type) == TREE_TYPE (type)
1453: && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
1454: || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
1455: TYPE_MAX_VALUE (type)))
1456: && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
1457: || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
1458: TYPE_MIN_VALUE (type)))
1459: && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
1460: || (TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
1461: && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
1462: && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
1463: return h->type;
1464: return 0;
1465: }
1466:
1467: /* Add an entry to the type-hash-table
1468: for a type TYPE whose hash code is HASHCODE. */
1469:
1470: void
1471: type_hash_add (hashcode, type)
1472: int hashcode;
1473: tree type;
1474: {
1475: register struct type_hash *h;
1476:
1477: h = (struct type_hash *) oballoc (sizeof (struct type_hash));
1478: h->hashcode = hashcode;
1479: h->type = type;
1480: h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
1481: type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
1482: }
1483:
1484: /* Given TYPE, and HASHCODE its hash code, return the canonical
1485: object for an identical type if one already exists.
1486: Otherwise, return TYPE, and record it as the canonical object
1487: if it is a permanent object.
1488:
1489: To use this function, first create a type of the sort you want.
1490: Then compute its hash code from the fields of the type that
1491: make it different from other similar types.
1492: Then call this function and use the value.
1493: This function frees the type you pass in if it is a duplicate. */
1494:
1495: /* Set to 1 to debug without canonicalization. Never set by program. */
1496: int debug_no_type_hash = 0;
1497:
1498: tree
1499: type_hash_canon (hashcode, type)
1500: int hashcode;
1501: tree type;
1502: {
1503: tree t1;
1504:
1505: if (debug_no_type_hash)
1506: return type;
1507:
1508: t1 = type_hash_lookup (hashcode, type);
1509: if (t1 != 0)
1510: {
1511: struct obstack *o
1512: = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
1513: obstack_free (o, type);
1514: return t1;
1515: }
1516:
1517: /* If this is a new type, record it for later reuse. */
1518: if (current_obstack == &permanent_obstack)
1519: type_hash_add (hashcode, type);
1520:
1521: return type;
1522: }
1523:
1524: /* Given two lists of types
1525: (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
1.1.1.7 root 1526: return 1 if the lists contain the same types in the same order.
1527: Also, the TREE_PURPOSEs must match. */
1.1.1.2 root 1528:
1529: int
1530: type_list_equal (l1, l2)
1531: tree l1, l2;
1532: {
1533: register tree t1, t2;
1534: for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1.1.1.7 root 1535: {
1536: if (TREE_VALUE (t1) != TREE_VALUE (t2))
1537: return 0;
1538: if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
1539: && !simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
1540: return 0;
1541: }
1.1.1.2 root 1542:
1543: return t1 == t2;
1544: }
1545:
1546: /* Nonzero if integer constants T1 and T2
1547: represent the same constant value. */
1548:
1549: int
1550: tree_int_cst_equal (t1, t2)
1551: tree t1, t2;
1552: {
1553: if (t1 == t2)
1554: return 1;
1555: if (t1 == 0 || t2 == 0)
1556: return 0;
1557: if (TREE_CODE (t1) == INTEGER_CST
1558: && TREE_CODE (t2) == INTEGER_CST
1559: && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1560: && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
1561: return 1;
1562: return 0;
1563: }
1564:
1565: /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
1566: The precise way of comparison depends on their data type. */
1567:
1568: int
1569: tree_int_cst_lt (t1, t2)
1570: tree t1, t2;
1571: {
1572: if (t1 == t2)
1573: return 0;
1574:
1575: if (!TREE_UNSIGNED (TREE_TYPE (t1)))
1576: return INT_CST_LT (t1, t2);
1577: return INT_CST_LT_UNSIGNED (t1, t2);
1578: }
1.1.1.7 root 1579:
1580: /* Compare two constructor-element-type constants. */
1581:
1582: int
1583: simple_cst_equal (t1, t2)
1584: tree t1, t2;
1585: {
1586: register enum tree_code code1, code2;
1587:
1588: if (t1 == t2)
1589: return 1;
1590: if (t1 == 0 || t2 == 0)
1591: return 0;
1592:
1593: code1 = TREE_CODE (t1);
1594: code2 = TREE_CODE (t2);
1595:
1596: if (code1 == NOP_EXPR || code1 == CONVERT_EXPR)
1597: if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
1598: return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1599: else
1600: return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
1601: else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
1602: return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
1603:
1604: if (code1 != code2)
1605: return 0;
1606:
1607: switch (code1)
1608: {
1609: case INTEGER_CST:
1610: return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1611: && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1612:
1613: case REAL_CST:
1614: return TREE_REAL_CST (t1) == TREE_REAL_CST (t2);
1615:
1616: case STRING_CST:
1617: return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1618: && !strcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2));
1619:
1620: case CONSTRUCTOR:
1621: abort ();
1622:
1623: case VAR_DECL:
1624: case PARM_DECL:
1625: case CONST_DECL:
1626: return 0;
1627:
1628: case PLUS_EXPR:
1629: case MINUS_EXPR:
1630: case MULT_EXPR:
1631: case TRUNC_DIV_EXPR:
1632: case TRUNC_MOD_EXPR:
1633: case LSHIFT_EXPR:
1634: case RSHIFT_EXPR:
1635: return (simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
1636: && simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
1637:
1638: case NEGATE_EXPR:
1639: case ADDR_EXPR:
1640: case REFERENCE_EXPR:
1641: return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1642:
1643: default:
1644: abort ();
1645: }
1646: }
1.1.1.2 root 1647:
1.1 root 1648: /* Constructors for pointer, array and function types.
1649: (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
1650: constructed by language-dependent code, not here.) */
1651:
1.1.1.2 root 1652: /* Construct, lay out and return the type of pointers to TO_TYPE.
1653: If such a type has already been constructed, reuse it. */
1654:
1.1 root 1655: tree
1656: build_pointer_type (to_type)
1657: tree to_type;
1658: {
1659: register tree t = TYPE_POINTER_TO (to_type);
1660: register struct obstack *ambient_obstack = current_obstack;
1.1.1.4 root 1661: register struct obstack *ambient_saveable_obstack = saveable_obstack;
1.1 root 1662:
1663: /* First, if we already have a type for pointers to TO_TYPE, use it. */
1664:
1665: if (t)
1666: return t;
1667:
1668: /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
1.1.1.4 root 1669: if (TREE_PERMANENT (to_type))
1670: {
1671: current_obstack = &permanent_obstack;
1672: saveable_obstack = &permanent_obstack;
1673: }
1.1 root 1674:
1675: t = make_node (POINTER_TYPE);
1676: TREE_TYPE (t) = to_type;
1677:
1678: /* Record this type as the pointer to TO_TYPE. */
1679: TYPE_POINTER_TO (to_type) = t;
1680:
1.1.1.2 root 1681: /* Lay out the type. This function has many callers that are concerned
1682: with expression-construction, and this simplifies them all.
1683: Also, it guarantees the TYPE_SIZE is permanent if the type is. */
1684: layout_type (t);
1.1 root 1685:
1686: current_obstack = ambient_obstack;
1.1.1.4 root 1687: saveable_obstack = ambient_saveable_obstack;
1.1 root 1688: return t;
1689: }
1690:
1.1.1.8 root 1691: /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
1692: MAXVAL should be the maximum value in the domain
1693: (one less than the length of the array). */
1694:
1695: tree
1696: build_index_type (maxval)
1697: tree maxval;
1698: {
1699: register tree itype = make_node (INTEGER_TYPE);
1700: int maxint = TREE_INT_CST_LOW (maxval);
1701: TYPE_PRECISION (itype) = BITS_PER_WORD;
1702: TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
1703: TREE_TYPE (TYPE_MIN_VALUE (itype)) = itype;
1704: TYPE_MAX_VALUE (itype) = maxval;
1705: TREE_TYPE (maxval) = itype;
1706: TYPE_MODE (itype) = SImode;
1707: TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
1708: TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
1709: TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
1710: return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
1711: }
1712:
1.1.1.2 root 1713: /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
1714: and number of elements specified by the range of values of INDEX_TYPE.
1715: If such a type has already been constructed, reuse it. */
1716:
1.1 root 1717: tree
1718: build_array_type (elt_type, index_type)
1719: tree elt_type, index_type;
1720: {
1721: register tree t = make_node (ARRAY_TYPE);
1.1.1.2 root 1722: int hashcode;
1.1 root 1723:
1724: if (TREE_CODE (elt_type) == FUNCTION_TYPE)
1725: {
1.1.1.2 root 1726: error ("arrays of functions are not meaningful");
1.1 root 1727: elt_type = integer_type_node;
1728: }
1729:
1730: TREE_TYPE (t) = elt_type;
1731: TYPE_DOMAIN (t) = index_type;
1.1.1.2 root 1732:
1.1 root 1733: /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
1734: build_pointer_type (elt_type);
1.1.1.2 root 1735:
1736: if (index_type == 0)
1737: return t;
1738:
1739: hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
1740: t = type_hash_canon (hashcode, t);
1741:
1742: if (TYPE_SIZE (t) == 0)
1743: layout_type (t);
1.1 root 1744: return t;
1745: }
1746:
1.1.1.2 root 1747: /* Construct, lay out and return
1748: the type of functions returning type VALUE_TYPE
1749: given arguments of types ARG_TYPES.
1750: ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
1751: are data type nodes for the arguments of the function.
1752: If such a type has already been constructed, reuse it. */
1.1 root 1753:
1754: tree
1755: build_function_type (value_type, arg_types)
1756: tree value_type, arg_types;
1757: {
1758: register tree t;
1.1.1.2 root 1759: int hashcode;
1.1 root 1760:
1.1.1.2 root 1761: if (TREE_CODE (value_type) == FUNCTION_TYPE
1.1 root 1762: || TREE_CODE (value_type) == ARRAY_TYPE)
1763: {
1.1.1.2 root 1764: error ("function return type cannot be function or array");
1.1 root 1765: value_type = integer_type_node;
1766: }
1767:
1.1.1.2 root 1768: /* Make a node of the sort we want. */
1.1 root 1769: t = make_node (FUNCTION_TYPE);
1770: TREE_TYPE (t) = value_type;
1771: TYPE_ARG_TYPES (t) = arg_types;
1.1.1.2 root 1772:
1773: /* If we already have such a type, use the old one and free this one. */
1774: hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
1775: t = type_hash_canon (hashcode, t);
1776:
1777: if (TYPE_SIZE (t) == 0)
1778: layout_type (t);
1.1 root 1779: return t;
1780: }
1.1.1.7 root 1781:
1782: /* Build the node for the type of references-to-TO_TYPE. */
1783:
1784: tree
1785: build_reference_type (to_type)
1786: tree to_type;
1787: {
1788: register tree t = TYPE_REFERENCE_TO (to_type);
1789: register struct obstack *ambient_obstack = current_obstack;
1790: register struct obstack *ambient_saveable_obstack = saveable_obstack;
1791:
1792: /* First, if we already have a type for pointers to TO_TYPE, use it. */
1793:
1794: if (t)
1795: return t;
1796:
1797: /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
1798: if (TREE_PERMANENT (to_type))
1799: {
1800: current_obstack = &permanent_obstack;
1801: saveable_obstack = &permanent_obstack;
1802: }
1803:
1804: t = make_node (REFERENCE_TYPE);
1805: TREE_TYPE (t) = to_type;
1806:
1807: /* Record this type as the pointer to TO_TYPE. */
1808: TYPE_REFERENCE_TO (to_type) = t;
1809:
1810: layout_type (t);
1811:
1812: current_obstack = ambient_obstack;
1813: saveable_obstack = ambient_saveable_obstack;
1814: return t;
1815: }
1816:
1817: /* Construct, lay out and return the type of methods belonging to class
1818: BASETYPE and whose arguments and values are described by TYPE.
1819: If that type exists already, reuse it.
1820: TYPE must be a FUNCTION_TYPE node. */
1821:
1822: tree
1823: build_method_type (basetype, type)
1824: tree basetype, type;
1825: {
1826: register tree t;
1827: int hashcode;
1828:
1829: /* Make a node of the sort we want. */
1830: t = make_node (METHOD_TYPE);
1831:
1832: if (TREE_CODE (type) != FUNCTION_TYPE)
1833: abort ();
1834:
1.1.1.11! root 1835: TYPE_METHOD_BASETYPE (t) = basetype;
! 1836: TREE_TYPE (t) = TREE_TYPE (type);
1.1.1.7 root 1837:
1838: /* The actual arglist for this function includes a "hidden" argument
1839: which is "this". Put it into the list of argument types. */
1840:
1841: TYPE_ARG_TYPES (t)
1842: = tree_cons (NULL, build_pointer_type (basetype), TYPE_ARG_TYPES (type));
1843:
1844: /* If we already have such a type, use the old one and free this one. */
1845: hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
1846: t = type_hash_canon (hashcode, t);
1847:
1848: if (TYPE_SIZE (t) == 0)
1849: layout_type (t);
1850:
1851: return t;
1852: }
1.1.1.11! root 1853:
! 1854: /* Construct, lay out and return the type of methods belonging to class
! 1855: BASETYPE and whose arguments and values are described by TYPE.
! 1856: If that type exists already, reuse it.
! 1857: TYPE must be a FUNCTION_TYPE node. */
! 1858:
! 1859: tree
! 1860: build_offset_type (basetype, type)
! 1861: tree basetype, type;
! 1862: {
! 1863: register tree t;
! 1864: int hashcode;
! 1865:
! 1866: /* Make a node of the sort we want. */
! 1867: t = make_node (OFFSET_TYPE);
! 1868:
! 1869: TYPE_OFFSET_BASETYPE (t) = basetype;
! 1870: TREE_TYPE (t) = type;
! 1871:
! 1872: /* If we already have such a type, use the old one and free this one. */
! 1873: hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
! 1874: t = type_hash_canon (hashcode, t);
! 1875:
! 1876: if (TYPE_SIZE (t) == 0)
! 1877: layout_type (t);
! 1878:
! 1879: return t;
! 1880: }
1.1 root 1881:
1882: /* Return OP, stripped of any conversions to wider types as much as is safe.
1883: Converting the value back to OP's type makes a value equivalent to OP.
1884:
1885: If FOR_TYPE is nonzero, we return a value which, if converted to
1886: type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
1887:
1.1.1.2 root 1888: If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
1889: narrowest type that can hold the value, even if they don't exactly fit.
1890: Otherwise, bit-field references are changed to a narrower type
1891: only if they can be fetched directly from memory in that type.
1892:
1.1 root 1893: OP must have integer, real or enumeral type. Pointers are not allowed!
1894:
1895: There are some cases where the obvious value we could return
1896: would regenerate to OP if converted to OP's type,
1897: but would not extend like OP to wider types.
1898: If FOR_TYPE indicates such extension is contemplated, we eschew such values.
1899: For example, if OP is (unsigned short)(signed char)-1,
1900: we avoid returning (signed char)-1 if FOR_TYPE is int,
1901: even though extending that to an unsigned short would regenerate OP,
1902: since the result of extending (signed char)-1 to (int)
1903: is different from (int) OP. */
1904:
1905: tree
1906: get_unwidened (op, for_type)
1907: register tree op;
1908: tree for_type;
1909: {
1910: /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
1911: /* TYPE_PRECISION is safe in place of type_precision since
1912: pointer types are not allowed. */
1913: register tree type = TREE_TYPE (op);
1914: register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
1915: register int uns
1916: = (for_type != 0 && for_type != type
1917: && final_prec > TYPE_PRECISION (type)
1.1.1.2 root 1918: && TREE_UNSIGNED (type));
1.1 root 1919: register tree win = op;
1920:
1921: while (TREE_CODE (op) == NOP_EXPR)
1922: {
1923: register int bitschange
1924: = TYPE_PRECISION (TREE_TYPE (op))
1925: - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
1926:
1927: /* Truncations are many-one so cannot be removed.
1928: Unless we are later going to truncate down even farther. */
1929: if (bitschange < 0
1930: && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
1931: break;
1932:
1933: /* See what's inside this conversion. If we decide to strip it,
1934: we will set WIN. */
1935: op = TREE_OPERAND (op, 0);
1936:
1937: /* If we have not stripped any zero-extensions (uns is 0),
1938: we can strip any kind of extension.
1939: If we have previously stripped a zero-extension,
1940: only zero-extensions can safely be stripped.
1941: Any extension can be stripped if the bits it would produce
1942: are all going to be discarded later by truncating to FOR_TYPE. */
1943:
1944: if (bitschange > 0)
1945: {
1946: if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
1947: win = op;
1.1.1.2 root 1948: /* TREE_UNSIGNED says whether this is a zero-extension.
1.1 root 1949: Let's avoid computing it if it does not affect WIN
1950: and if UNS will not be needed again. */
1951: if ((uns || TREE_CODE (op) == NOP_EXPR)
1.1.1.2 root 1952: && TREE_UNSIGNED (TREE_TYPE (op)))
1.1 root 1953: {
1954: uns = 1;
1955: win = op;
1956: }
1957: }
1958: }
1959:
1.1.1.2 root 1960: if (TREE_CODE (op) == COMPONENT_REF
1961: /* Since type_for_size always gives an integer type. */
1962: && TREE_CODE (type) != REAL_TYPE)
1963: {
1964: int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
1965: * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
1966: type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
1967:
1968: /* We can get this structure field in the narrowest type it fits in.
1969: If FOR_TYPE is 0, do this only for a field that matches the
1970: narrower type exactly and is aligned for it (i.e. mode isn't BI).
1971: The resulting extension to its nominal type (a fullword type)
1972: must fit the same conditions as for other extensions. */
1973:
1.1.1.3 root 1974: if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
1.1.1.2 root 1975: && (for_type || DECL_MODE (TREE_OPERAND (op, 1)) != BImode)
1976: && (! uns || final_prec <= innerprec
1977: || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
1978: && type != 0)
1979: {
1980: win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
1981: TREE_OPERAND (op, 1));
1982: TREE_VOLATILE (win) = TREE_VOLATILE (op);
1983: TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
1984: }
1985: }
1.1 root 1986: return win;
1987: }
1988:
1989: /* Return OP or a simpler expression for a narrower value
1990: which can be sign-extended or zero-extended to give back OP.
1991: Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
1992: or 0 if the value should be sign-extended. */
1993:
1994: tree
1995: get_narrower (op, unsignedp_ptr)
1996: register tree op;
1997: int *unsignedp_ptr;
1998: {
1999: register int uns = 0;
2000: int first = 1;
2001: register tree win = op;
2002:
2003: while (TREE_CODE (op) == NOP_EXPR)
2004: {
2005: register int bitschange
2006: = TYPE_PRECISION (TREE_TYPE (op))
2007: - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
2008:
2009: /* Truncations are many-one so cannot be removed. */
2010: if (bitschange < 0)
2011: break;
2012:
2013: /* See what's inside this conversion. If we decide to strip it,
2014: we will set WIN. */
2015: op = TREE_OPERAND (op, 0);
2016:
2017: if (bitschange > 0)
2018: {
2019: /* An extension: the outermost one can be stripped,
2020: but remember whether it is zero or sign extension. */
2021: if (first)
1.1.1.2 root 2022: uns = TREE_UNSIGNED (TREE_TYPE (op));
1.1 root 2023: /* Otherwise, if a sign extension has been stripped,
2024: only sign extensions can now be stripped;
2025: if a zero extension has been stripped, only zero-extensions. */
1.1.1.2 root 2026: else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
1.1 root 2027: break;
2028: first = 0;
2029: }
2030: /* A change in nominal type can always be stripped. */
2031:
2032: win = op;
2033: }
2034:
1.1.1.2 root 2035: if (TREE_CODE (op) == COMPONENT_REF
2036: /* Since type_for_size always gives an integer type. */
2037: && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
1.1 root 2038: {
2039: int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
2040: * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
1.1.1.2 root 2041: tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
1.1 root 2042:
2043: /* We can get this structure field in a narrower type that fits it,
2044: but the resulting extension to its nominal type (a fullword type)
1.1.1.2 root 2045: must satisfy the same conditions as for other extensions.
2046:
2047: Do this only for fields that are aligned (not BImode),
2048: because when bit-field insns will be used there is no
2049: advantage in doing this. */
1.1 root 2050:
2051: if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
1.1.1.2 root 2052: && DECL_MODE (TREE_OPERAND (op, 1)) != BImode
2053: && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
1.1 root 2054: && type != 0)
2055: {
1.1.1.2 root 2056: if (first)
2057: uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
2058: win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
2059: TREE_OPERAND (op, 1));
2060: TREE_VOLATILE (win) = TREE_VOLATILE (op);
2061: TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
1.1 root 2062: }
2063: }
2064: *unsignedp_ptr = uns;
2065: return win;
2066: }
2067:
2068: /* Return the precision of a type, for arithmetic purposes.
2069: Supports all types on which arithmetic is possible
2070: (including pointer types).
2071: It's not clear yet what will be right for complex types. */
2072:
2073: int
2074: type_precision (type)
2075: register tree type;
2076: {
2077: return ((TREE_CODE (type) == INTEGER_TYPE
2078: || TREE_CODE (type) == ENUMERAL_TYPE
2079: || TREE_CODE (type) == REAL_TYPE)
1.1.1.2 root 2080: ? TYPE_PRECISION (type) : POINTER_SIZE);
1.1 root 2081: }
2082:
2083: /* Nonzero if integer constant C has a value that is permissible
2084: for type TYPE (an INTEGER_TYPE). */
2085:
2086: int
2087: int_fits_type_p (c, type)
2088: tree c, type;
2089: {
1.1.1.2 root 2090: if (TREE_UNSIGNED (type))
1.1 root 2091: return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
2092: && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
2093: else
2094: return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
2095: && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
2096: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.