|
|
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:
1.1.1.19! root 1120: if (TREE_LITERAL (t) || (TREE_READONLY (t) && !TREE_VOLATILE (t))
! 1121: || TREE_CODE (t) == SAVE_EXPR)
1.1 root 1122: return t;
1123:
1.1.1.2 root 1124: return build (SAVE_EXPR, TREE_TYPE (expr), t, NULL);
1.1 root 1125: }
1126:
1127: /* Stabilize a reference so that we can use it any number of times
1128: without causing its operands to be evaluated more than once.
1.1.1.2 root 1129: Returns the stabilized reference.
1130:
1131: Also allows conversion expressions whose operands are references.
1132: Any other kind of expression is returned unchanged. */
1.1 root 1133:
1134: tree
1135: stabilize_reference (ref)
1136: tree ref;
1137: {
1138: register tree result;
1139: register enum tree_code code = TREE_CODE (ref);
1140:
1.1.1.2 root 1141: switch (code)
1.1 root 1142: {
1.1.1.2 root 1143: case VAR_DECL:
1144: case PARM_DECL:
1145: case RESULT_DECL:
1.1 root 1146: result = ref;
1.1.1.2 root 1147: break;
1148:
1149: case NOP_EXPR:
1150: case CONVERT_EXPR:
1151: case FLOAT_EXPR:
1152: case FIX_TRUNC_EXPR:
1153: case FIX_FLOOR_EXPR:
1154: case FIX_ROUND_EXPR:
1155: case FIX_CEIL_EXPR:
1156: result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
1157: break;
1158:
1159: case INDIRECT_REF:
1160: result = build_nt (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
1161: break;
1162:
1163: case COMPONENT_REF:
1164: result = build_nt (COMPONENT_REF,
1165: stabilize_reference (TREE_OPERAND (ref, 0)),
1166: TREE_OPERAND (ref, 1));
1167: break;
1168:
1169: case ARRAY_REF:
1170: result = build_nt (ARRAY_REF, stabilize_reference (TREE_OPERAND (ref, 0)),
1171: save_expr (TREE_OPERAND (ref, 1)));
1172: break;
1173:
1174: /* If arg isn't a kind of lvalue we recognize, make no change.
1175: Caller should recognize the error for an invalid lvalue. */
1176: default:
1177: return ref;
1178:
1179: case ERROR_MARK:
1.1 root 1180: return error_mark_node;
1181: }
1182:
1183: TREE_TYPE (result) = TREE_TYPE (ref);
1.1.1.2 root 1184: TREE_READONLY (result) = TREE_READONLY (ref);
1.1 root 1185: TREE_VOLATILE (result) = TREE_VOLATILE (ref);
1.1.1.2 root 1186: TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
1.1 root 1187:
1188: return result;
1189: }
1190:
1191: /* Low-level constructors for expressions. */
1192:
1.1.1.2 root 1193: /* Build an expression of code CODE, data type TYPE,
1194: and operands as specified by the arguments ARG1 and following arguments.
1195: Expressions and reference nodes can be created this way.
1196: Constants, decls, types and misc nodes cannot be. */
1.1 root 1197:
1198: tree
1.1.1.2 root 1199: build (va_alist)
1200: va_dcl
1.1 root 1201: {
1.1.1.2 root 1202: register va_list p;
1203: enum tree_code code;
1204: register tree t;
1205: register int length;
1206: register int i;
1207:
1208: va_start (p);
1.1 root 1209:
1.1.1.2 root 1210: code = va_arg (p, enum tree_code);
1211: t = make_node (code);
1212: length = tree_code_length[(int) code];
1213: TREE_TYPE (t) = va_arg (p, tree);
1214:
1215: if (length == 2)
1216: {
1217: /* This is equivalent to the loop below, but faster. */
1218: register tree arg0 = va_arg (p, tree);
1219: register tree arg1 = va_arg (p, tree);
1220: TREE_OPERAND (t, 0) = arg0;
1221: TREE_OPERAND (t, 1) = arg1;
1222: TREE_VOLATILE (t)
1223: = (arg0 && TREE_VOLATILE (arg0)) || (arg1 && TREE_VOLATILE (arg1));
1224: }
1225: else
1226: {
1227: for (i = 0; i < length; i++)
1228: {
1229: register tree operand = va_arg (p, tree);
1230: TREE_OPERAND (t, i) = operand;
1231: if (operand && TREE_VOLATILE (operand))
1232: TREE_VOLATILE (t) = 1;
1233: }
1234: }
1235: va_end (p);
1.1 root 1236: return t;
1237: }
1238:
1.1.1.2 root 1239: /* Similar except don't specify the TREE_TYPE
1240: and leave the TREE_VOLATILE as 0.
1241: It is permissible for arguments to be null,
1242: or even garbage if their values do not matter. */
1.1 root 1243:
1244: tree
1.1.1.2 root 1245: build_nt (va_alist)
1246: va_dcl
1.1 root 1247: {
1.1.1.2 root 1248: register va_list p;
1249: register enum tree_code code;
1250: register tree t;
1251: register int length;
1252: register int i;
1253:
1254: va_start (p);
1.1 root 1255:
1.1.1.2 root 1256: code = va_arg (p, enum tree_code);
1257: t = make_node (code);
1258: length = tree_code_length[(int) code];
1259:
1260: for (i = 0; i < length; i++)
1261: TREE_OPERAND (t, i) = va_arg (p, tree);
1262:
1263: va_end (p);
1.1 root 1264: return t;
1265: }
1.1.1.11 root 1266:
1267: tree
1268: build_op_identifier (op1, op2)
1269: tree op1, op2;
1270: {
1271: register tree t = make_node (OP_IDENTIFIER);
1272: TREE_PURPOSE (t) = op1;
1273: TREE_VALUE (t) = op2;
1274: return t;
1275: }
1.1.1.2 root 1276:
1277: /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
1278: We do NOT enter this node in any sort of symbol table.
1.1 root 1279:
1.1.1.2 root 1280: layout_decl is used to set up the decl's storage layout.
1281: Other slots are initialized to 0 or null pointers. */
1.1 root 1282:
1283: tree
1.1.1.2 root 1284: build_decl (code, name, type)
1285: enum tree_code code;
1286: tree name, type;
1287: {
1288: register tree t;
1289:
1290: t = make_node (code);
1291:
1292: /* if (type == error_mark_node)
1293: type = integer_type_node; */
1294: /* That is not done, deliberately, so that having error_mark_node
1295: as the type can suppress useless errors in the use of this variable. */
1296:
1297: DECL_NAME (t) = name;
1.1.1.15 root 1298: if (name)
1299: {
1300: DECL_PRINT_NAME (t) = IDENTIFIER_POINTER (name);
1301: DECL_ASSEMBLER_NAME (t) = IDENTIFIER_POINTER (name);
1302: }
1.1.1.2 root 1303: TREE_TYPE (t) = type;
1304: DECL_ARGUMENTS (t) = NULL_TREE;
1305: DECL_INITIAL (t) = NULL_TREE;
1306:
1307: if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
1308: layout_decl (t, 0);
1309: else if (code == FUNCTION_DECL)
1310: DECL_MODE (t) = FUNCTION_MODE;
1311:
1.1 root 1312: return t;
1313: }
1314:
1.1.1.7 root 1315: #if 0
1.1 root 1316: /* Low-level constructors for statements.
1317: These constructors all expect source file name and line number
1318: as arguments, as well as enough arguments to fill in the data
1319: in the statement node. */
1320:
1321: tree
1322: build_goto (filename, line, label)
1323: char *filename;
1324: int line;
1325: tree label;
1326: {
1327: register tree t = make_node (GOTO_STMT);
1328: STMT_SOURCE_FILE (t) = filename;
1329: STMT_SOURCE_LINE (t) = line;
1330: STMT_BODY (t) = label;
1331: return t;
1332: }
1333:
1334: tree
1335: build_return (filename, line, arg)
1336: char *filename;
1337: int line;
1338: tree arg;
1339: {
1340: register tree t = make_node (RETURN_STMT);
1341:
1342: STMT_SOURCE_FILE (t) = filename;
1343: STMT_SOURCE_LINE (t) = line;
1344: STMT_BODY (t) = arg;
1345: return t;
1346: }
1347:
1348: tree
1349: build_expr_stmt (filename, line, expr)
1350: char *filename;
1351: int line;
1352: tree expr;
1353: {
1354: register tree t = make_node (EXPR_STMT);
1355:
1356: STMT_SOURCE_FILE (t) = filename;
1357: STMT_SOURCE_LINE (t) = line;
1358: STMT_BODY (t) = expr;
1359: return t;
1360: }
1361:
1362: tree
1363: build_if (filename, line, cond, thenclause, elseclause)
1364: char *filename;
1365: int line;
1366: tree cond, thenclause, elseclause;
1367: {
1368: register tree t = make_node (IF_STMT);
1369:
1370: STMT_SOURCE_FILE (t) = filename;
1371: STMT_SOURCE_LINE (t) = line;
1372: STMT_COND (t) = cond;
1373: STMT_THEN (t) = thenclause;
1374: STMT_ELSE (t) = elseclause;
1375: return t;
1376: }
1377:
1378: tree
1379: build_exit (filename, line, cond)
1380: char *filename;
1381: int line;
1382: tree cond;
1383: {
1384: register tree t = make_node (EXIT_STMT);
1385: STMT_SOURCE_FILE (t) = filename;
1386: STMT_SOURCE_LINE (t) = line;
1387: STMT_BODY (t) = cond;
1388: return t;
1389: }
1390:
1391: tree
1392: build_asm_stmt (filename, line, asmcode)
1393: char *filename;
1394: int line;
1395: tree asmcode;
1396: {
1397: register tree t = make_node (ASM_STMT);
1398: STMT_SOURCE_FILE (t) = filename;
1399: STMT_SOURCE_LINE (t) = line;
1400: STMT_BODY (t) = asmcode;
1401: return t;
1402: }
1403:
1404: tree
1405: build_case (filename, line, object, cases)
1406: char *filename;
1407: int line;
1408: tree object, cases;
1409: {
1410: register tree t = make_node (CASE_STMT);
1411: STMT_SOURCE_FILE (t) = filename;
1412: STMT_SOURCE_LINE (t) = line;
1413: STMT_CASE_INDEX (t) = object;
1414: STMT_CASE_LIST (t) = cases;
1415: return t;
1416: }
1417:
1418: tree
1.1.1.7 root 1419: build_loop (filename, line, body)
1.1 root 1420: char *filename;
1421: int line;
1.1.1.7 root 1422: tree body;
1.1 root 1423: {
1.1.1.7 root 1424: register tree t = make_node (LOOP_STMT);
1.1 root 1425: STMT_SOURCE_FILE (t) = filename;
1426: STMT_SOURCE_LINE (t) = line;
1427: STMT_BODY (t) = body;
1428: return t;
1429: }
1430:
1431: tree
1.1.1.7 root 1432: build_compound (filename, line, body)
1.1 root 1433: char *filename;
1434: int line;
1435: tree body;
1436: {
1.1.1.7 root 1437: register tree t = make_node (COMPOUND_STMT);
1.1 root 1438: STMT_SOURCE_FILE (t) = filename;
1439: STMT_SOURCE_LINE (t) = line;
1440: STMT_BODY (t) = body;
1441: return t;
1442: }
1443:
1.1.1.7 root 1444: #endif /* 0 */
1445:
1446: /* LET_STMT nodes are used to represent the structure of binding contours
1447: and declarations, once those contours have been exited and their contents
1448: compiled. This information is used for outputting debugging info. */
1449:
1.1 root 1450: tree
1.1.1.15 root 1451: build_let (filename, line, vars, subblocks, supercontext, tags)
1.1 root 1452: char *filename;
1453: int line;
1.1.1.15 root 1454: tree vars, subblocks, supercontext, tags;
1.1 root 1455: {
1.1.1.7 root 1456: register tree t = make_node (LET_STMT);
1.1 root 1457: STMT_SOURCE_FILE (t) = filename;
1458: STMT_SOURCE_LINE (t) = line;
1.1.1.7 root 1459: STMT_VARS (t) = vars;
1.1.1.15 root 1460: STMT_SUBBLOCKS (t) = subblocks;
1.1.1.7 root 1461: STMT_SUPERCONTEXT (t) = supercontext;
1462: STMT_BIND_SIZE (t) = 0;
1463: STMT_TYPE_TAGS (t) = tags;
1.1 root 1464: return t;
1465: }
1466:
1467: /* Return a type like TYPE except that its TREE_READONLY is CONSTP
1468: and its TREE_VOLATILE is VOLATILEP.
1469:
1470: Such variant types already made are recorded so that duplicates
1471: are not made.
1472:
1473: A variant types should never be used as the type of an expression.
1474: Always copy the variant information into the TREE_READONLY
1475: and TREE_VOLATILE of the expression, and then give the expression
1476: as its type the "main variant", the variant whose TREE_READONLY
1477: and TREE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
1478: main variant. */
1479:
1480: tree
1481: build_type_variant (type, constp, volatilep)
1482: tree type;
1483: int constp, volatilep;
1484: {
1485: register tree t, m = TYPE_MAIN_VARIANT (type);
1486: register struct obstack *ambient_obstack = current_obstack;
1487:
1488: /* Treat any nonzero argument as 1. */
1489: constp = !!constp;
1490: volatilep = !!volatilep;
1491:
1492: /* First search the chain variants for one that is what we want. */
1493:
1494: for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1495: if (constp == TREE_READONLY (t)
1496: && volatilep == TREE_VOLATILE (t))
1497: return t;
1498:
1499: /* We need a new one. */
1.1.1.2 root 1500: current_obstack
1501: = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
1.1 root 1502:
1503: t = copy_node (type);
1504: TREE_READONLY (t) = constp;
1505: TREE_VOLATILE (t) = volatilep;
1506: TYPE_POINTER_TO (t) = 0;
1.1.1.7 root 1507: TYPE_REFERENCE_TO (t) = 0;
1.1 root 1508:
1509: /* Add this type to the chain of variants of TYPE. */
1510: TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1511: TYPE_NEXT_VARIANT (m) = t;
1512:
1513: current_obstack = ambient_obstack;
1514: return t;
1515: }
1516:
1.1.1.2 root 1517: /* Hashing of types so that we don't make duplicates.
1518: The entry point is `type_hash_canon'. */
1519:
1520: /* Each hash table slot is a bucket containing a chain
1521: of these structures. */
1522:
1523: struct type_hash
1524: {
1525: struct type_hash *next; /* Next structure in the bucket. */
1526: int hashcode; /* Hash code of this type. */
1527: tree type; /* The type recorded here. */
1528: };
1529:
1530: /* Now here is the hash table. When recording a type, it is added
1531: to the slot whose index is the hash code mod the table size.
1532: Note that the hash table is used for several kinds of types
1533: (function types, array types and array index range types, for now).
1534: While all these live in the same table, they are completely independent,
1535: and the hash code is computed differently for each of these. */
1536:
1.1.1.7 root 1537: #define TYPE_HASH_SIZE 59
1.1.1.2 root 1538: struct type_hash *type_hash_table[TYPE_HASH_SIZE];
1539:
1540: /* Here is how primitive or already-canonicalized types' hash
1541: codes are made. */
1542: #define TYPE_HASH(TYPE) TREE_UID (TYPE)
1543:
1544: /* Compute a hash code for a list of types (chain of TREE_LIST nodes
1545: with types in the TREE_VALUE slots), by adding the hash codes
1546: of the individual types. */
1547:
1548: int
1549: type_hash_list (list)
1550: tree list;
1551: {
1552: register int hashcode;
1553: register tree tail;
1554: for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
1555: hashcode += TYPE_HASH (TREE_VALUE (tail));
1556: return hashcode;
1557: }
1558:
1559: /* Look in the type hash table for a type isomorphic to TYPE.
1560: If one is found, return it. Otherwise return 0. */
1561:
1562: tree
1563: type_hash_lookup (hashcode, type)
1564: int hashcode;
1565: tree type;
1566: {
1567: register struct type_hash *h;
1568: for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1569: if (h->hashcode == hashcode
1570: && TREE_CODE (h->type) == TREE_CODE (type)
1571: && TREE_TYPE (h->type) == TREE_TYPE (type)
1572: && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
1573: || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
1574: TYPE_MAX_VALUE (type)))
1575: && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
1576: || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
1577: TYPE_MIN_VALUE (type)))
1578: && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
1579: || (TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
1580: && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
1581: && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
1582: return h->type;
1583: return 0;
1584: }
1585:
1586: /* Add an entry to the type-hash-table
1587: for a type TYPE whose hash code is HASHCODE. */
1588:
1589: void
1590: type_hash_add (hashcode, type)
1591: int hashcode;
1592: tree type;
1593: {
1594: register struct type_hash *h;
1595:
1596: h = (struct type_hash *) oballoc (sizeof (struct type_hash));
1597: h->hashcode = hashcode;
1598: h->type = type;
1599: h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
1600: type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
1601: }
1602:
1603: /* Given TYPE, and HASHCODE its hash code, return the canonical
1604: object for an identical type if one already exists.
1605: Otherwise, return TYPE, and record it as the canonical object
1606: if it is a permanent object.
1607:
1608: To use this function, first create a type of the sort you want.
1609: Then compute its hash code from the fields of the type that
1610: make it different from other similar types.
1611: Then call this function and use the value.
1612: This function frees the type you pass in if it is a duplicate. */
1613:
1614: /* Set to 1 to debug without canonicalization. Never set by program. */
1615: int debug_no_type_hash = 0;
1616:
1617: tree
1618: type_hash_canon (hashcode, type)
1619: int hashcode;
1620: tree type;
1621: {
1622: tree t1;
1623:
1624: if (debug_no_type_hash)
1625: return type;
1626:
1627: t1 = type_hash_lookup (hashcode, type);
1628: if (t1 != 0)
1629: {
1630: struct obstack *o
1631: = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
1632: obstack_free (o, type);
1633: return t1;
1634: }
1635:
1636: /* If this is a new type, record it for later reuse. */
1637: if (current_obstack == &permanent_obstack)
1638: type_hash_add (hashcode, type);
1639:
1640: return type;
1641: }
1642:
1643: /* Given two lists of types
1644: (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
1.1.1.7 root 1645: return 1 if the lists contain the same types in the same order.
1646: Also, the TREE_PURPOSEs must match. */
1.1.1.2 root 1647:
1648: int
1649: type_list_equal (l1, l2)
1650: tree l1, l2;
1651: {
1652: register tree t1, t2;
1653: for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1.1.1.7 root 1654: {
1655: if (TREE_VALUE (t1) != TREE_VALUE (t2))
1656: return 0;
1657: if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
1658: && !simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
1659: return 0;
1660: }
1.1.1.2 root 1661:
1662: return t1 == t2;
1663: }
1664:
1665: /* Nonzero if integer constants T1 and T2
1666: represent the same constant value. */
1667:
1668: int
1669: tree_int_cst_equal (t1, t2)
1670: tree t1, t2;
1671: {
1672: if (t1 == t2)
1673: return 1;
1674: if (t1 == 0 || t2 == 0)
1675: return 0;
1676: if (TREE_CODE (t1) == INTEGER_CST
1677: && TREE_CODE (t2) == INTEGER_CST
1678: && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1679: && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
1680: return 1;
1681: return 0;
1682: }
1683:
1684: /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
1685: The precise way of comparison depends on their data type. */
1686:
1687: int
1688: tree_int_cst_lt (t1, t2)
1689: tree t1, t2;
1690: {
1691: if (t1 == t2)
1692: return 0;
1693:
1694: if (!TREE_UNSIGNED (TREE_TYPE (t1)))
1695: return INT_CST_LT (t1, t2);
1696: return INT_CST_LT_UNSIGNED (t1, t2);
1697: }
1.1.1.7 root 1698:
1699: /* Compare two constructor-element-type constants. */
1700:
1701: int
1702: simple_cst_equal (t1, t2)
1703: tree t1, t2;
1704: {
1705: register enum tree_code code1, code2;
1706:
1707: if (t1 == t2)
1708: return 1;
1709: if (t1 == 0 || t2 == 0)
1710: return 0;
1711:
1712: code1 = TREE_CODE (t1);
1713: code2 = TREE_CODE (t2);
1714:
1715: if (code1 == NOP_EXPR || code1 == CONVERT_EXPR)
1716: if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
1717: return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1718: else
1719: return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
1720: else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
1721: return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
1722:
1723: if (code1 != code2)
1724: return 0;
1725:
1726: switch (code1)
1727: {
1728: case INTEGER_CST:
1729: return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1730: && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1731:
1732: case REAL_CST:
1.1.1.12 root 1733: return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1.1.1.7 root 1734:
1735: case STRING_CST:
1736: return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1737: && !strcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2));
1738:
1739: case CONSTRUCTOR:
1740: abort ();
1741:
1742: case VAR_DECL:
1743: case PARM_DECL:
1744: case CONST_DECL:
1745: return 0;
1746:
1747: case PLUS_EXPR:
1748: case MINUS_EXPR:
1749: case MULT_EXPR:
1750: case TRUNC_DIV_EXPR:
1751: case TRUNC_MOD_EXPR:
1752: case LSHIFT_EXPR:
1753: case RSHIFT_EXPR:
1754: return (simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
1755: && simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
1756:
1757: case NEGATE_EXPR:
1758: case ADDR_EXPR:
1759: case REFERENCE_EXPR:
1.1.1.15 root 1760: case INDIRECT_REF:
1.1.1.7 root 1761: return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1762:
1763: default:
1764: abort ();
1765: }
1766: }
1.1.1.2 root 1767:
1.1 root 1768: /* Constructors for pointer, array and function types.
1769: (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
1770: constructed by language-dependent code, not here.) */
1771:
1.1.1.2 root 1772: /* Construct, lay out and return the type of pointers to TO_TYPE.
1773: If such a type has already been constructed, reuse it. */
1774:
1.1 root 1775: tree
1776: build_pointer_type (to_type)
1777: tree to_type;
1778: {
1779: register tree t = TYPE_POINTER_TO (to_type);
1780: register struct obstack *ambient_obstack = current_obstack;
1.1.1.4 root 1781: register struct obstack *ambient_saveable_obstack = saveable_obstack;
1.1 root 1782:
1783: /* First, if we already have a type for pointers to TO_TYPE, use it. */
1784:
1785: if (t)
1786: return t;
1787:
1788: /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
1.1.1.4 root 1789: if (TREE_PERMANENT (to_type))
1790: {
1791: current_obstack = &permanent_obstack;
1792: saveable_obstack = &permanent_obstack;
1793: }
1.1 root 1794:
1795: t = make_node (POINTER_TYPE);
1796: TREE_TYPE (t) = to_type;
1797:
1798: /* Record this type as the pointer to TO_TYPE. */
1799: TYPE_POINTER_TO (to_type) = t;
1800:
1.1.1.2 root 1801: /* Lay out the type. This function has many callers that are concerned
1802: with expression-construction, and this simplifies them all.
1803: Also, it guarantees the TYPE_SIZE is permanent if the type is. */
1804: layout_type (t);
1.1 root 1805:
1806: current_obstack = ambient_obstack;
1.1.1.4 root 1807: saveable_obstack = ambient_saveable_obstack;
1.1 root 1808: return t;
1809: }
1810:
1.1.1.8 root 1811: /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
1812: MAXVAL should be the maximum value in the domain
1813: (one less than the length of the array). */
1814:
1815: tree
1816: build_index_type (maxval)
1817: tree maxval;
1818: {
1819: register tree itype = make_node (INTEGER_TYPE);
1820: TYPE_PRECISION (itype) = BITS_PER_WORD;
1821: TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
1.1.1.15 root 1822: TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
1823: TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
1.1.1.8 root 1824: TYPE_MODE (itype) = SImode;
1825: TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
1826: TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
1827: TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
1.1.1.17 root 1828: if (TREE_CODE (maxval) == INTEGER_CST)
1829: {
1830: int maxint = TREE_INT_CST_LOW (maxval);
1831: return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
1832: }
1833: else
1834: return itype;
1.1.1.8 root 1835: }
1836:
1.1.1.2 root 1837: /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
1838: and number of elements specified by the range of values of INDEX_TYPE.
1839: If such a type has already been constructed, reuse it. */
1840:
1.1 root 1841: tree
1842: build_array_type (elt_type, index_type)
1843: tree elt_type, index_type;
1844: {
1845: register tree t = make_node (ARRAY_TYPE);
1.1.1.2 root 1846: int hashcode;
1.1 root 1847:
1848: if (TREE_CODE (elt_type) == FUNCTION_TYPE)
1849: {
1.1.1.2 root 1850: error ("arrays of functions are not meaningful");
1.1 root 1851: elt_type = integer_type_node;
1852: }
1853:
1854: TREE_TYPE (t) = elt_type;
1855: TYPE_DOMAIN (t) = index_type;
1.1.1.2 root 1856:
1.1 root 1857: /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
1858: build_pointer_type (elt_type);
1.1.1.17 root 1859: #if 0
1860: /* Also that of the main variant, which is the type the element will have. */
1861: build_pointer_type (TYPE_MAIN_VARIANT (elt_type));
1862: #endif
1.1.1.2 root 1863:
1864: if (index_type == 0)
1865: return t;
1866:
1867: hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
1868: t = type_hash_canon (hashcode, t);
1869:
1870: if (TYPE_SIZE (t) == 0)
1871: layout_type (t);
1.1 root 1872: return t;
1873: }
1874:
1.1.1.2 root 1875: /* Construct, lay out and return
1876: the type of functions returning type VALUE_TYPE
1877: given arguments of types ARG_TYPES.
1878: ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
1879: are data type nodes for the arguments of the function.
1880: If such a type has already been constructed, reuse it. */
1.1 root 1881:
1882: tree
1883: build_function_type (value_type, arg_types)
1884: tree value_type, arg_types;
1885: {
1886: register tree t;
1.1.1.2 root 1887: int hashcode;
1.1 root 1888:
1.1.1.2 root 1889: if (TREE_CODE (value_type) == FUNCTION_TYPE
1.1 root 1890: || TREE_CODE (value_type) == ARRAY_TYPE)
1891: {
1.1.1.2 root 1892: error ("function return type cannot be function or array");
1.1 root 1893: value_type = integer_type_node;
1894: }
1895:
1.1.1.2 root 1896: /* Make a node of the sort we want. */
1.1 root 1897: t = make_node (FUNCTION_TYPE);
1898: TREE_TYPE (t) = value_type;
1899: TYPE_ARG_TYPES (t) = arg_types;
1.1.1.2 root 1900:
1901: /* If we already have such a type, use the old one and free this one. */
1902: hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
1903: t = type_hash_canon (hashcode, t);
1904:
1905: if (TYPE_SIZE (t) == 0)
1906: layout_type (t);
1.1 root 1907: return t;
1908: }
1.1.1.7 root 1909:
1910: /* Build the node for the type of references-to-TO_TYPE. */
1911:
1912: tree
1913: build_reference_type (to_type)
1914: tree to_type;
1915: {
1916: register tree t = TYPE_REFERENCE_TO (to_type);
1917: register struct obstack *ambient_obstack = current_obstack;
1918: register struct obstack *ambient_saveable_obstack = saveable_obstack;
1919:
1920: /* First, if we already have a type for pointers to TO_TYPE, use it. */
1921:
1922: if (t)
1923: return t;
1924:
1925: /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
1926: if (TREE_PERMANENT (to_type))
1927: {
1928: current_obstack = &permanent_obstack;
1929: saveable_obstack = &permanent_obstack;
1930: }
1931:
1932: t = make_node (REFERENCE_TYPE);
1933: TREE_TYPE (t) = to_type;
1934:
1935: /* Record this type as the pointer to TO_TYPE. */
1936: TYPE_REFERENCE_TO (to_type) = t;
1937:
1938: layout_type (t);
1939:
1940: current_obstack = ambient_obstack;
1941: saveable_obstack = ambient_saveable_obstack;
1942: return t;
1943: }
1944:
1945: /* Construct, lay out and return the type of methods belonging to class
1946: BASETYPE and whose arguments and values are described by TYPE.
1947: If that type exists already, reuse it.
1948: TYPE must be a FUNCTION_TYPE node. */
1949:
1950: tree
1951: build_method_type (basetype, type)
1952: tree basetype, type;
1953: {
1954: register tree t;
1955: int hashcode;
1956:
1957: /* Make a node of the sort we want. */
1958: t = make_node (METHOD_TYPE);
1959:
1960: if (TREE_CODE (type) != FUNCTION_TYPE)
1961: abort ();
1962:
1.1.1.11 root 1963: TYPE_METHOD_BASETYPE (t) = basetype;
1964: TREE_TYPE (t) = TREE_TYPE (type);
1.1.1.7 root 1965:
1966: /* The actual arglist for this function includes a "hidden" argument
1967: which is "this". Put it into the list of argument types. */
1968:
1969: TYPE_ARG_TYPES (t)
1970: = tree_cons (NULL, build_pointer_type (basetype), TYPE_ARG_TYPES (type));
1971:
1972: /* If we already have such a type, use the old one and free this one. */
1973: hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
1974: t = type_hash_canon (hashcode, t);
1975:
1976: if (TYPE_SIZE (t) == 0)
1977: layout_type (t);
1978:
1979: return t;
1980: }
1.1.1.11 root 1981:
1982: /* Construct, lay out and return the type of methods belonging to class
1983: BASETYPE and whose arguments and values are described by TYPE.
1984: If that type exists already, reuse it.
1985: TYPE must be a FUNCTION_TYPE node. */
1986:
1987: tree
1988: build_offset_type (basetype, type)
1989: tree basetype, type;
1990: {
1991: register tree t;
1992: int hashcode;
1993:
1994: /* Make a node of the sort we want. */
1995: t = make_node (OFFSET_TYPE);
1996:
1997: TYPE_OFFSET_BASETYPE (t) = basetype;
1998: TREE_TYPE (t) = type;
1999:
2000: /* If we already have such a type, use the old one and free this one. */
2001: hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
2002: t = type_hash_canon (hashcode, t);
2003:
2004: if (TYPE_SIZE (t) == 0)
2005: layout_type (t);
2006:
2007: return t;
2008: }
1.1 root 2009:
2010: /* Return OP, stripped of any conversions to wider types as much as is safe.
2011: Converting the value back to OP's type makes a value equivalent to OP.
2012:
2013: If FOR_TYPE is nonzero, we return a value which, if converted to
2014: type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
2015:
1.1.1.2 root 2016: If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
2017: narrowest type that can hold the value, even if they don't exactly fit.
2018: Otherwise, bit-field references are changed to a narrower type
2019: only if they can be fetched directly from memory in that type.
2020:
1.1 root 2021: OP must have integer, real or enumeral type. Pointers are not allowed!
2022:
2023: There are some cases where the obvious value we could return
2024: would regenerate to OP if converted to OP's type,
2025: but would not extend like OP to wider types.
2026: If FOR_TYPE indicates such extension is contemplated, we eschew such values.
2027: For example, if OP is (unsigned short)(signed char)-1,
2028: we avoid returning (signed char)-1 if FOR_TYPE is int,
2029: even though extending that to an unsigned short would regenerate OP,
2030: since the result of extending (signed char)-1 to (int)
2031: is different from (int) OP. */
2032:
2033: tree
2034: get_unwidened (op, for_type)
2035: register tree op;
2036: tree for_type;
2037: {
2038: /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
2039: /* TYPE_PRECISION is safe in place of type_precision since
2040: pointer types are not allowed. */
2041: register tree type = TREE_TYPE (op);
2042: register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
2043: register int uns
2044: = (for_type != 0 && for_type != type
2045: && final_prec > TYPE_PRECISION (type)
1.1.1.2 root 2046: && TREE_UNSIGNED (type));
1.1 root 2047: register tree win = op;
2048:
2049: while (TREE_CODE (op) == NOP_EXPR)
2050: {
2051: register int bitschange
2052: = TYPE_PRECISION (TREE_TYPE (op))
2053: - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
2054:
2055: /* Truncations are many-one so cannot be removed.
2056: Unless we are later going to truncate down even farther. */
2057: if (bitschange < 0
2058: && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
2059: break;
2060:
2061: /* See what's inside this conversion. If we decide to strip it,
2062: we will set WIN. */
2063: op = TREE_OPERAND (op, 0);
2064:
2065: /* If we have not stripped any zero-extensions (uns is 0),
2066: we can strip any kind of extension.
2067: If we have previously stripped a zero-extension,
2068: only zero-extensions can safely be stripped.
2069: Any extension can be stripped if the bits it would produce
2070: are all going to be discarded later by truncating to FOR_TYPE. */
2071:
2072: if (bitschange > 0)
2073: {
2074: if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
2075: win = op;
1.1.1.2 root 2076: /* TREE_UNSIGNED says whether this is a zero-extension.
1.1 root 2077: Let's avoid computing it if it does not affect WIN
2078: and if UNS will not be needed again. */
2079: if ((uns || TREE_CODE (op) == NOP_EXPR)
1.1.1.2 root 2080: && TREE_UNSIGNED (TREE_TYPE (op)))
1.1 root 2081: {
2082: uns = 1;
2083: win = op;
2084: }
2085: }
2086: }
2087:
1.1.1.2 root 2088: if (TREE_CODE (op) == COMPONENT_REF
2089: /* Since type_for_size always gives an integer type. */
2090: && TREE_CODE (type) != REAL_TYPE)
2091: {
2092: int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
2093: * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
2094: type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
2095:
2096: /* We can get this structure field in the narrowest type it fits in.
2097: If FOR_TYPE is 0, do this only for a field that matches the
2098: narrower type exactly and is aligned for it (i.e. mode isn't BI).
2099: The resulting extension to its nominal type (a fullword type)
2100: must fit the same conditions as for other extensions. */
2101:
1.1.1.3 root 2102: if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
1.1.1.2 root 2103: && (for_type || DECL_MODE (TREE_OPERAND (op, 1)) != BImode)
2104: && (! uns || final_prec <= innerprec
2105: || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
2106: && type != 0)
2107: {
2108: win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
2109: TREE_OPERAND (op, 1));
2110: TREE_VOLATILE (win) = TREE_VOLATILE (op);
2111: TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
2112: }
2113: }
1.1 root 2114: return win;
2115: }
2116:
2117: /* Return OP or a simpler expression for a narrower value
2118: which can be sign-extended or zero-extended to give back OP.
2119: Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
2120: or 0 if the value should be sign-extended. */
2121:
2122: tree
2123: get_narrower (op, unsignedp_ptr)
2124: register tree op;
2125: int *unsignedp_ptr;
2126: {
2127: register int uns = 0;
2128: int first = 1;
2129: register tree win = op;
2130:
2131: while (TREE_CODE (op) == NOP_EXPR)
2132: {
2133: register int bitschange
2134: = TYPE_PRECISION (TREE_TYPE (op))
2135: - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
2136:
2137: /* Truncations are many-one so cannot be removed. */
2138: if (bitschange < 0)
2139: break;
2140:
2141: /* See what's inside this conversion. If we decide to strip it,
2142: we will set WIN. */
2143: op = TREE_OPERAND (op, 0);
2144:
2145: if (bitschange > 0)
2146: {
2147: /* An extension: the outermost one can be stripped,
2148: but remember whether it is zero or sign extension. */
2149: if (first)
1.1.1.2 root 2150: uns = TREE_UNSIGNED (TREE_TYPE (op));
1.1 root 2151: /* Otherwise, if a sign extension has been stripped,
2152: only sign extensions can now be stripped;
2153: if a zero extension has been stripped, only zero-extensions. */
1.1.1.2 root 2154: else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
1.1 root 2155: break;
2156: first = 0;
2157: }
2158: /* A change in nominal type can always be stripped. */
2159:
2160: win = op;
2161: }
2162:
1.1.1.2 root 2163: if (TREE_CODE (op) == COMPONENT_REF
2164: /* Since type_for_size always gives an integer type. */
2165: && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
1.1 root 2166: {
2167: int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
2168: * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
1.1.1.2 root 2169: tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
1.1 root 2170:
2171: /* We can get this structure field in a narrower type that fits it,
2172: but the resulting extension to its nominal type (a fullword type)
1.1.1.2 root 2173: must satisfy the same conditions as for other extensions.
2174:
2175: Do this only for fields that are aligned (not BImode),
2176: because when bit-field insns will be used there is no
2177: advantage in doing this. */
1.1 root 2178:
2179: if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
1.1.1.2 root 2180: && DECL_MODE (TREE_OPERAND (op, 1)) != BImode
2181: && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
1.1 root 2182: && type != 0)
2183: {
1.1.1.2 root 2184: if (first)
2185: uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
2186: win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
2187: TREE_OPERAND (op, 1));
2188: TREE_VOLATILE (win) = TREE_VOLATILE (op);
2189: TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
1.1 root 2190: }
2191: }
2192: *unsignedp_ptr = uns;
2193: return win;
2194: }
2195:
2196: /* Return the precision of a type, for arithmetic purposes.
2197: Supports all types on which arithmetic is possible
2198: (including pointer types).
2199: It's not clear yet what will be right for complex types. */
2200:
2201: int
2202: type_precision (type)
2203: register tree type;
2204: {
2205: return ((TREE_CODE (type) == INTEGER_TYPE
2206: || TREE_CODE (type) == ENUMERAL_TYPE
2207: || TREE_CODE (type) == REAL_TYPE)
1.1.1.2 root 2208: ? TYPE_PRECISION (type) : POINTER_SIZE);
1.1 root 2209: }
2210:
2211: /* Nonzero if integer constant C has a value that is permissible
2212: for type TYPE (an INTEGER_TYPE). */
2213:
2214: int
2215: int_fits_type_p (c, type)
2216: tree c, type;
2217: {
1.1.1.2 root 2218: if (TREE_UNSIGNED (type))
1.1 root 2219: return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
2220: && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
2221: else
2222: return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
2223: && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
2224: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.