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