home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__src3 / tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  62.4 KB  |  2,364 lines

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