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