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