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