OSDN Git Service

* Makefile.in (cs-tconfig.h): Pass USED_FOR_TARGET to mkconfig.sh
[pf3gnuchains/gcc-fork.git] / gcc / java / decl.c
1 /* Process declarations and variables for the GNU compiler for the
2    Java(TM) language.
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
26
27 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
28
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "real.h"
36 #include "toplev.h"
37 #include "flags.h"
38 #include "java-tree.h"
39 #include "jcf.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "libfuncs.h"
43 #include "except.h"
44 #include "java-except.h"
45 #include "ggc.h"
46 #include "timevar.h"
47 #include "cgraph.h"
48 #include "tree-inline.h"
49 #include "target.h"
50 #include "version.h"
51
52 #if defined (DEBUG_JAVA_BINDING_LEVELS)
53 extern void indent (void);
54 #endif
55
56 static tree push_jvm_slot (int, tree);
57 static tree lookup_name_current_level (tree);
58 static tree push_promoted_type (const char *, tree);
59 static struct binding_level *make_binding_level (void);
60 static tree create_primitive_vtable (const char *);
61 static tree check_local_unnamed_variable (tree, tree, tree);
62 static void parse_version (void);
63
64 /* Used when computing the ABI version.  */
65 #define GCJ_BINARYCOMPAT_ADDITION 5
66
67 /* The version of the BC ABI that we generate.  At the moment we are
68    compatible with what shipped in GCC 4.0.  This must be kept in sync
69    with parse_version(), libgcj, and reality (if the BC format
70    changes, this must change.  */
71 #define GCJ_CURRENT_BC_ABI_VERSION \
72   (4 * 10000 + 0 * 10 + GCJ_BINARYCOMPAT_ADDITION)
73
74 /* The ABI version number.  */
75 tree gcj_abi_version;
76
77 /* Name of the Cloneable class.  */
78 tree java_lang_cloneable_identifier_node;
79
80 /* Name of the Serializable class.  */
81 tree java_io_serializable_identifier_node;
82
83 /* The DECL_MAP is a mapping from (index, type) to a decl node.
84    If index < max_locals, it is the index of a local variable.
85    if index >= max_locals, then index-max_locals is a stack slot.
86    The DECL_MAP mapping is represented as a TREE_VEC whose elements
87    are a list of decls (VAR_DECL or PARM_DECL) chained by
88    DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
89    we search the chain for a decl with a matching TREE_TYPE. */
90
91 static GTY(()) tree decl_map;
92
93 /* The base_decl_map is contains one variable of ptr_type: this is
94    used to contain every variable of reference type that is ever
95    stored in a local variable slot.  */
96
97 static GTY(()) tree base_decl_map;
98
99 /* An index used to make temporary identifiers unique.  */
100 static int uniq;
101
102 /* A list of local variables VAR_DECLs for this method that we have seen
103    debug information, but we have not reached their starting (byte) PC yet. */
104
105 static GTY(()) tree pending_local_decls;
106
107 /* The decl for "_Jv_ResolvePoolEntry".  */
108 tree soft_resolvepoolentry_node;
109
110 #if defined(DEBUG_JAVA_BINDING_LEVELS)
111 int binding_depth = 0;
112 int is_class_level = 0;
113 int current_pc;
114
115 void
116 indent (void)
117 {
118   int i;
119
120   for (i = 0; i < binding_depth*2; i++)
121     putc (' ', stderr);
122 }
123 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
124
125 /* True if decl is a named local variable, i.e. if it is an alias
126    that's used only for debugging purposes.  */
127
128 static bool
129 debug_variable_p (tree decl)
130 {
131   if (TREE_CODE (decl) == PARM_DECL)
132     return false;
133
134   if (LOCAL_SLOT_P (decl))
135     return false;
136
137   return true;
138 }
139  
140 /* Copy the value in decl into every live alias in the same local
141    variable slot.  Some of these will be dead stores removed by the
142    optimizer.  */
143
144 void 
145 update_aliases (tree decl, int index, int pc)
146 {
147   tree decl_type = TREE_TYPE (decl);
148   tree tmp;
149
150   if (debug_variable_p (decl))
151     abort ();
152
153   for (tmp = TREE_VEC_ELT (decl_map, index); 
154        tmp != NULL_TREE; 
155        tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
156     {
157       tree tmp_type = TREE_TYPE (tmp);
158       if (tmp != decl
159           && LOCAL_SLOT_P (tmp) == 0
160           && (pc == -1
161               || (pc >= DECL_LOCAL_START_PC (tmp)
162                   && pc < DECL_LOCAL_END_PC (tmp)))
163           /* This test is < (rather than <=) because there's no point
164              updating an alias that's about to die at the end of this
165              instruction.  */
166           && (tmp_type == decl_type
167               || (INTEGRAL_TYPE_P (tmp_type)
168                   && INTEGRAL_TYPE_P (decl_type)
169                   && TYPE_PRECISION (decl_type) <= 32
170                   && TYPE_PRECISION (tmp_type) <= 32)
171               || (TREE_CODE (tmp_type) == POINTER_TYPE
172                   && TREE_CODE (decl_type) == POINTER_TYPE)))
173         {
174           tree src = build1 (NOP_EXPR, tmp_type, decl);
175           if (LOCAL_VAR_OUT_OF_SCOPE_P (tmp))
176             abort ();
177           java_add_stmt (build2 (MODIFY_EXPR, tmp_type, tmp, src));
178         }
179     }
180 }
181
182 static tree
183 push_jvm_slot (int index, tree decl)
184 {
185   DECL_CONTEXT (decl) = current_function_decl;
186   layout_decl (decl, 0);
187
188   /* Now link the decl into the decl_map. */
189   if (DECL_LANG_SPECIFIC (decl) == NULL)
190     {
191       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
192       DECL_LOCAL_START_PC (decl) = 0;
193       DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
194       DECL_LOCAL_SLOT_NUMBER (decl) = index;
195     }
196   DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
197   TREE_VEC_ELT (decl_map, index) = decl;
198
199   return decl;
200 }
201
202 /*  At the point of its creation a local variable decl inherits
203     whatever is already in the same slot.  In the case of a local
204     variable that is declared but unused, we won't find anything.  */
205
206 static void
207 initialize_local_variable (tree decl, int index)
208 {
209   tree decl_type = TREE_TYPE (decl);
210   if (TREE_CODE (decl_type) == POINTER_TYPE)
211     {
212       tree tmp = TREE_VEC_ELT (base_decl_map, index);
213
214       if (tmp)
215         {
216           /* At the point of its creation this decl inherits whatever
217              is in the slot.  */
218           tree src = build1 (NOP_EXPR, decl_type, tmp);
219           java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, src));   
220         }
221     }
222   else
223     {
224       tree tmp;
225   
226       for (tmp = TREE_VEC_ELT (decl_map, index); 
227            tmp != NULL_TREE; 
228            tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
229         {
230           tree tmp_type = TREE_TYPE (tmp);
231           if (tmp != decl
232               && ! debug_variable_p (tmp)
233               && (tmp_type == decl_type
234                   || (INTEGRAL_TYPE_P (tmp_type)
235                       && INTEGRAL_TYPE_P (decl_type)
236                       && TYPE_PRECISION (decl_type) <= 32
237                       && TYPE_PRECISION (tmp_type) <= 32
238                       && TYPE_PRECISION (tmp_type)
239                          >= TYPE_PRECISION (decl_type))))
240             {
241               java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, tmp));       
242               return;
243             }
244         }  
245     }
246 }
247
248 /* Find the best declaration based upon type.  If 'decl' fits 'type' better
249    than 'best', return 'decl'.  Otherwise return 'best'.  */
250
251 static tree
252 check_local_unnamed_variable (tree best, tree decl, tree type)
253 {
254   tree decl_type = TREE_TYPE (decl);
255   
256   if (LOCAL_VAR_OUT_OF_SCOPE_P (decl))
257     abort ();
258
259   /* Use the same decl for all integer types <= 32 bits.  This is
260      necessary because sometimes a value is stored as (for example)
261      boolean but loaded as int.  */
262   if (decl_type == type
263       || (INTEGRAL_TYPE_P (decl_type)
264           && INTEGRAL_TYPE_P (type)
265           && TYPE_PRECISION (decl_type) <= 32
266           && TYPE_PRECISION (type) <= 32
267           && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))      
268       /*  ptr_type_node is used for null pointers, which are
269           assignment compatible with everything.  */
270       || (TREE_CODE (decl_type) == POINTER_TYPE
271           && type == ptr_type_node)
272       /* Whenever anyone wants to use a slot that is initially
273          occupied by a PARM_DECL of pointer type they must get that
274          decl, even if they asked for a pointer to a different type.
275          However, if someone wants a scalar variable in a slot that
276          initially held a pointer arg -- or vice versa -- we create a
277          new VAR_DECL.  
278
279          ???: As long as verification is correct, this will be a
280          compatible type.  But maybe we should create a dummy variable
281          and replace all references to it with the DECL and a
282          NOP_EXPR.  
283       */
284       || (TREE_CODE (decl_type) == POINTER_TYPE
285           && TREE_CODE (decl) == PARM_DECL
286           && TREE_CODE (type) == POINTER_TYPE))
287     {
288       if (best == NULL_TREE
289           || (decl_type == type && TREE_TYPE (best) != type))
290         return decl;
291     }
292
293   return best;
294 }
295
296
297 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
298    that is valid at PC (or -1 if any pc).
299    If there is no existing matching decl, allocate one.  */
300
301 tree
302 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
303 {
304   tree tmp = TREE_VEC_ELT (decl_map, index);
305   tree decl = NULL_TREE;
306
307   /* Scan through every declaration that has been created in this
308      slot.  We're only looking for variables that correspond to local
309      index declarations and PARM_DECLs, not named variables: such
310      local variables are used only for debugging information.  */
311   while (tmp != NULL_TREE)
312     {
313       if (! debug_variable_p (tmp))
314         decl = check_local_unnamed_variable (decl, tmp, type);
315       tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
316     }
317
318   /* gcj has a function called promote_type(), which is used by both
319      the bytecode compiler and the source compiler.  Unfortunately,
320      the type systems for the Java VM and the Java language are not
321      the same: a boolean in the VM promotes to an int, not to a wide
322      boolean.  If our caller wants something to hold a boolean, that
323      had better be an int, because that slot might be re-used
324      later in integer context.  */
325   if (TREE_CODE (type) == BOOLEAN_TYPE)
326     type = integer_type_node;
327
328   /* If we don't find a match, create one with the type passed in.
329      The name of the variable is #n#m, which n is the variable index
330      in the local variable area and m is a dummy identifier for
331      uniqueness -- multiple variables may share the same local
332      variable index.  We don't call pushdecl() to push pointer types
333      into a binding expr because they'll all be replaced by a single
334      variable that is used for every reference in that local variable
335      slot.  */
336   if (! decl)
337     {
338       char buf[64];
339       tree name;
340       sprintf (buf, "#slot#%d#%d", index, uniq++);
341       name = get_identifier (buf);
342       decl = build_decl (VAR_DECL, name, type);
343       DECL_IGNORED_P (decl) = 1;
344       DECL_ARTIFICIAL (decl) = 1;
345       decl = push_jvm_slot (index, decl);
346       LOCAL_SLOT_P (decl) = 1;
347
348       if (TREE_CODE (type) != POINTER_TYPE)
349         pushdecl_function_level (decl);
350     }
351
352   /* As well as creating a local variable that matches the type, we
353      also create a base variable (of ptr_type) that will hold all its
354      aliases.  */
355   if (TREE_CODE (type) == POINTER_TYPE
356       && ! TREE_VEC_ELT (base_decl_map, index))
357     {
358       char buf[64];
359       tree name;
360       tree base_decl;
361       sprintf (buf, "#ref#%d#%d", index, uniq++);
362       name = get_identifier (buf);
363       base_decl
364         = TREE_VEC_ELT (base_decl_map, index)
365         = build_decl (VAR_DECL, name, ptr_type_node);
366       pushdecl_function_level (base_decl);
367       DECL_IGNORED_P (base_decl) = 1;
368       DECL_ARTIFICIAL (base_decl) = 1;
369     }
370
371   return decl;
372 }
373
374 /* Called during gimplification for every variable.  If the variable
375    is a temporary of pointer type, replace it with a common variable
376    thath is used to hold all pointer types that are ever stored in
377    that slot.  Set WANT_LVALUE if you want a variable that is to be
378    written to.  */
379
380 tree 
381 java_replace_reference (tree var_decl, bool want_lvalue)
382 {
383   tree decl_type;
384
385   if (! base_decl_map)
386     return var_decl;
387
388   decl_type = TREE_TYPE (var_decl);
389
390   if (TREE_CODE (decl_type) == POINTER_TYPE)
391     {
392       if (DECL_LANG_SPECIFIC (var_decl)
393           && LOCAL_SLOT_P (var_decl))
394         {
395           int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
396           tree base_decl = TREE_VEC_ELT (base_decl_map, index); 
397
398           if (! base_decl)
399             abort ();
400
401           if (! want_lvalue)
402             base_decl = build1 (NOP_EXPR, decl_type, base_decl);
403
404           return base_decl;
405         }
406     }
407
408   return var_decl;
409 }
410
411
412 /* Same as find_local_index, except that INDEX is a stack index. */
413
414 tree
415 find_stack_slot (int index, tree type)
416 {
417   return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
418                               type, -1);
419 }
420
421 struct binding_level GTY(())
422   {
423     /* A chain of _DECL nodes for all variables, constants, functions,
424      * and typedef types.  These are in the reverse of the order supplied.
425      */
426     tree names;
427
428     /* For each level, a list of shadowed outer-level local definitions
429        to be restored when this level is popped.
430        Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
431        whose TREE_VALUE is its old definition (a kind of ..._DECL node).  */
432     tree shadowed;
433
434     /* For each level (except not the global one),
435        a chain of BLOCK nodes for all the levels
436        that were entered and exited one level down.  */
437     tree blocks;
438
439     /* The binding level which this one is contained in (inherits from).  */
440     struct binding_level *level_chain;
441
442     /* The bytecode PC that marks the end of this level. */
443     int end_pc;
444     /* The bytecode PC that marks the start of this level. */
445     int start_pc;
446
447     /* The statements in this binding level.  */
448     tree stmts;
449
450     /* An exception range associated with this binding level.  */
451     struct eh_range * GTY((skip (""))) exception_range;
452
453     /* Binding depth at which this level began.  Used only for debugging.  */
454     unsigned binding_depth;
455   };
456
457 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
458
459 /* The binding level currently in effect.  */
460
461 static GTY(()) struct binding_level *current_binding_level;
462
463 /* A chain of binding_level structures awaiting reuse.  */
464
465 static GTY(()) struct binding_level *free_binding_level;
466
467 /* The outermost binding level, for names of file scope.
468    This is created when the compiler is started and exists
469    through the entire run.  */
470
471 static GTY(()) struct binding_level *global_binding_level;
472
473 /* The binding level that holds variables declared at the outermost
474    level within a function body.  */
475
476 static struct binding_level *function_binding_level;
477
478 /* A PC value bigger than any PC value we may ever may encounter. */
479
480 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
481
482 /* Binding level structures are initialized by copying this one.  */
483
484 static const struct binding_level clear_binding_level
485 = {
486     NULL_TREE, /* names */
487     NULL_TREE, /* shadowed */
488     NULL_TREE, /* blocks */
489     NULL_BINDING_LEVEL, /* level_chain */
490     LARGEST_PC, /* end_pc */
491     0, /* start_pc */
492     NULL, /* stmts */
493     NULL, /* exception_range */
494     0, /* binding_depth */
495   };
496
497 #if 0
498 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
499    that have names.  Here so we can clear out their names' definitions
500    at the end of the function.  */
501
502 static tree named_labels;
503
504 /* A list of LABEL_DECLs from outer contexts that are currently shadowed.  */
505
506 static tree shadowed_labels;
507 #endif
508
509 tree java_global_trees[JTI_MAX];
510   
511 /* Build (and pushdecl) a "promoted type" for all standard
512    types shorter than int.  */
513
514 static tree
515 push_promoted_type (const char *name, tree actual_type)
516 {
517   tree type = make_node (TREE_CODE (actual_type));
518 #if 1
519   tree in_min = TYPE_MIN_VALUE (int_type_node);
520   tree in_max = TYPE_MAX_VALUE (int_type_node);
521 #else
522   tree in_min = TYPE_MIN_VALUE (actual_type);
523   tree in_max = TYPE_MAX_VALUE (actual_type);
524 #endif
525   TYPE_MIN_VALUE (type) = copy_node (in_min);
526   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
527   TYPE_MAX_VALUE (type) = copy_node (in_max);
528   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
529   TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
530   layout_type (type);
531   pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
532   return type;
533 }
534
535 /* Return a definition for a builtin function named NAME and whose data type
536    is TYPE.  TYPE should be a function type with argument types.
537    FUNCTION_CODE tells later passes how to compile calls to this function.
538    See tree.h for its possible values.
539
540    If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
541    the name to be called if we can't opencode the function.  If
542    ATTRS is nonzero, use that for the function's attribute list.  */
543
544 tree
545 builtin_function (const char *name,
546                   tree type,
547                   int function_code,
548                   enum built_in_class cl,
549                   const char *library_name,
550                   tree ARG_UNUSED (attrs))
551 {
552   tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
553   DECL_EXTERNAL (decl) = 1;
554   TREE_PUBLIC (decl) = 1;
555   if (library_name)
556     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
557   make_decl_rtl (decl);
558   pushdecl (decl);
559   DECL_BUILT_IN_CLASS (decl) = cl;
560   DECL_FUNCTION_CODE (decl) = function_code;
561   return decl;
562 }
563
564 /* Return tree that represents a vtable for a primitive array.  */
565 static tree
566 create_primitive_vtable (const char *name)
567 {
568   tree r;
569   char buf[50];
570
571   sprintf (buf, "_Jv_%sVTable", name);
572   r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
573   DECL_EXTERNAL (r) = 1;
574   return r;
575 }
576
577 static tree
578 do_nothing (tree t)
579 {
580   return t;
581 }
582
583 /* Parse the version string and compute the ABI version number.  */
584 static void
585 parse_version (void)
586 {
587   const char *p = version_string;
588   unsigned int major = 0, minor = 0;
589   unsigned int abi_version;
590
591   /* Skip leading junk.  */
592   while (*p && !ISDIGIT (*p))
593     ++p;
594   gcc_assert (*p);
595
596   /* Extract major version.  */
597   while (ISDIGIT (*p))
598     {
599       major = major * 10 + *p - '0';
600       ++p;
601     }
602
603   gcc_assert (*p == '.' && ISDIGIT (p[1]));
604   ++p;
605
606   /* Extract minor version.  */
607   while (ISDIGIT (*p))
608     {
609       minor = minor * 10 + *p - '0';
610       ++p;
611     }
612
613   /* Implicit in this computation is the idea that we won't break the
614      old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
615      4.0.1).  */
616   abi_version = 10000 * major + 10 * minor;
617   /* It is helpful to distinguish BC ABI from ordinary ABI at this
618      level, since at some point we will recognize a variety of BC ABIs
619      (objects generated by different version of gcj), but will
620      probably always require strict matching for ordinary ABI.  */
621   if (flag_indirect_dispatch)
622     abi_version = GCJ_CURRENT_BC_ABI_VERSION;
623
624   gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
625 }
626
627 void
628 java_init_decl_processing (void)
629 {
630   tree endlink;
631   tree field = NULL_TREE;
632   tree t;
633
634   init_class_processing ();
635
636   current_function_decl = NULL;
637   current_binding_level = NULL_BINDING_LEVEL;
638   free_binding_level = NULL_BINDING_LEVEL;
639   pushlevel (0);        /* make the binding_level structure for global names */
640   global_binding_level = current_binding_level;
641
642   /* The code here must be similar to build_common_tree_nodes{,_2} in
643      tree.c, especially as to the order of initializing common nodes.  */
644   error_mark_node = make_node (ERROR_MARK);
645   TREE_TYPE (error_mark_node) = error_mark_node;
646
647   /* Create sizetype first - needed for other types. */
648   initialize_sizetypes (false);
649
650   byte_type_node = make_signed_type (8);
651   pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
652   short_type_node = make_signed_type (16);
653   pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
654   int_type_node = make_signed_type (32);
655   pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
656   long_type_node = make_signed_type (64);
657   pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
658
659   unsigned_byte_type_node = make_unsigned_type (8);
660   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
661                         unsigned_byte_type_node));
662   unsigned_short_type_node = make_unsigned_type (16);
663   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
664                         unsigned_short_type_node));
665   unsigned_int_type_node = make_unsigned_type (32);
666   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
667                         unsigned_int_type_node));
668   unsigned_long_type_node = make_unsigned_type (64);
669   pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
670                         unsigned_long_type_node));
671
672   /* This is not a java type, however tree-dfa requires a definition for
673      size_type_node.  */
674   size_type_node = make_unsigned_type (POINTER_SIZE);
675   set_sizetype (size_type_node);
676
677   /* Define these next since types below may used them.  */
678   integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
679   integer_zero_node = build_int_cst (NULL_TREE, 0);
680   integer_one_node = build_int_cst (NULL_TREE, 1);
681   integer_two_node = build_int_cst (NULL_TREE, 2);
682   integer_four_node = build_int_cst (NULL_TREE, 4);
683   integer_minus_one_node = build_int_cst (NULL_TREE, -1);
684
685   /* A few values used for range checking in the lexer.  */
686   decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
687 #if HOST_BITS_PER_WIDE_INT == 64
688   decimal_long_max = build_int_cstu (unsigned_long_type_node,
689                                      0x8000000000000000LL);
690 #elif HOST_BITS_PER_WIDE_INT == 32
691   decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
692                                          0, 0x80000000);
693 #else
694  #error "unsupported size"
695 #endif
696
697   size_zero_node = size_int (0);
698   size_one_node = size_int (1);
699   bitsize_zero_node = bitsize_int (0);
700   bitsize_one_node = bitsize_int (1);
701   bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
702
703   long_zero_node = build_int_cst (long_type_node, 0);
704
705   void_type_node = make_node (VOID_TYPE);
706   pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
707   layout_type (void_type_node); /* Uses size_zero_node */
708
709   ptr_type_node = build_pointer_type (void_type_node);
710   const_ptr_type_node
711     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
712
713   t = make_node (VOID_TYPE);
714   layout_type (t); /* Uses size_zero_node */
715   return_address_type_node = build_pointer_type (t);
716
717   null_pointer_node = build_int_cst (ptr_type_node, 0);
718
719 #if 0
720   /* Make a type to be the domain of a few array types
721      whose domains don't really matter.
722      200 is small enough that it always fits in size_t
723      and large enough that it can hold most function names for the
724      initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
725   short_array_type_node = build_prim_array_type (short_type_node, 200);
726 #endif
727   char_type_node = make_node (CHAR_TYPE);
728   TYPE_PRECISION (char_type_node) = 16;
729   fixup_unsigned_type (char_type_node);
730   pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
731
732   boolean_type_node = make_node (BOOLEAN_TYPE);
733   TYPE_PRECISION (boolean_type_node) = 1;
734   fixup_unsigned_type (boolean_type_node);
735   pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
736                         boolean_type_node));
737   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
738   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
739
740   promoted_byte_type_node
741     = push_promoted_type ("promoted_byte", byte_type_node);
742   promoted_short_type_node
743     = push_promoted_type ("promoted_short", short_type_node);
744   promoted_char_type_node
745     = push_promoted_type ("promoted_char", char_type_node);
746   promoted_boolean_type_node
747     = push_promoted_type ("promoted_boolean", boolean_type_node);
748
749   float_type_node = make_node (REAL_TYPE);
750   TYPE_PRECISION (float_type_node) = 32;
751   pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
752                         float_type_node));
753   layout_type (float_type_node);
754
755   double_type_node = make_node (REAL_TYPE);
756   TYPE_PRECISION (double_type_node) = 64;
757   pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
758                         double_type_node));
759   layout_type (double_type_node);
760
761   float_zero_node = build_real (float_type_node, dconst0);
762   double_zero_node = build_real (double_type_node, dconst0);
763
764   /* These are the vtables for arrays of primitives.  */
765   boolean_array_vtable = create_primitive_vtable ("boolean");
766   byte_array_vtable = create_primitive_vtable ("byte");
767   char_array_vtable = create_primitive_vtable ("char");
768   short_array_vtable = create_primitive_vtable ("short");
769   int_array_vtable = create_primitive_vtable ("int");
770   long_array_vtable = create_primitive_vtable ("long");
771   float_array_vtable = create_primitive_vtable ("float");
772   double_array_vtable = create_primitive_vtable ("double");
773
774   one_elt_array_domain_type = build_index_type (integer_one_node);
775   utf8const_type = make_node (RECORD_TYPE);
776   PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
777   PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
778   FINISH_RECORD (utf8const_type);
779   utf8const_ptr_type = build_pointer_type (utf8const_type);
780
781   atable_type = build_array_type (ptr_type_node, 
782                                   one_elt_array_domain_type);
783   TYPE_NONALIASED_COMPONENT (atable_type) = 1;
784   atable_ptr_type = build_pointer_type (atable_type);
785
786   itable_type = build_array_type (ptr_type_node, 
787                                   one_elt_array_domain_type);
788   TYPE_NONALIASED_COMPONENT (itable_type) = 1;
789   itable_ptr_type = build_pointer_type (itable_type);
790
791   symbol_type = make_node (RECORD_TYPE);
792   PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
793   PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
794   PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
795   FINISH_RECORD (symbol_type);
796
797   symbols_array_type = build_array_type (symbol_type, 
798                                          one_elt_array_domain_type);
799   symbols_array_ptr_type = build_pointer_type (symbols_array_type);
800
801   assertion_entry_type = make_node (RECORD_TYPE);
802   PUSH_FIELD (assertion_entry_type, field, "assertion_code", integer_type_node);
803   PUSH_FIELD (assertion_entry_type, field, "op1", utf8const_ptr_type);
804   PUSH_FIELD (assertion_entry_type, field, "op2", utf8const_ptr_type);
805   FINISH_RECORD (assertion_entry_type);
806   
807   assertion_table_type = build_array_type (assertion_entry_type,
808                                            one_elt_array_domain_type);
809
810   /* As you're adding items here, please update the code right after
811      this section, so that the filename containing the source code of
812      the pre-defined class gets registered correctly. */
813   unqualified_object_id_node = get_identifier ("Object");
814   object_type_node = lookup_class (get_identifier ("java.lang.Object"));
815   object_ptr_type_node = promote_type (object_type_node);
816   string_type_node = lookup_class (get_identifier ("java.lang.String"));
817   string_ptr_type_node = promote_type (string_type_node);
818   class_type_node = lookup_class (get_identifier ("java.lang.Class"));
819   throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
820   exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
821   runtime_exception_type_node = 
822     lookup_class (get_identifier ("java.lang.RuntimeException"));
823   error_exception_type_node = 
824     lookup_class (get_identifier ("java.lang.Error"));
825
826   rawdata_ptr_type_node
827     = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
828
829   add_predefined_file (get_identifier ("java/lang/Class.java"));
830   add_predefined_file (get_identifier ("java/lang/Error.java"));
831   add_predefined_file (get_identifier ("java/lang/Object.java"));
832   add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
833   add_predefined_file (get_identifier ("java/lang/String.java"));
834   add_predefined_file (get_identifier ("java/lang/Throwable.java"));
835   add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
836   add_predefined_file (get_identifier ("java/lang/Exception.java"));
837   add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
838   add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
839
840   methodtable_type = make_node (RECORD_TYPE);
841   layout_type (methodtable_type);
842   build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
843   methodtable_ptr_type = build_pointer_type (methodtable_type);
844
845   TYPE_identifier_node = get_identifier ("TYPE");
846   init_identifier_node = get_identifier ("<init>");
847   clinit_identifier_node = get_identifier ("<clinit>");
848   finit_identifier_node = get_identifier ("finit$");
849   instinit_identifier_node = get_identifier ("instinit$");
850   void_signature_node = get_identifier ("()V");
851   length_identifier_node = get_identifier ("length");
852   finalize_identifier_node = get_identifier ("finalize");
853   this_identifier_node = get_identifier ("this");
854   super_identifier_node = get_identifier ("super");
855   continue_identifier_node = get_identifier ("continue");
856   access0_identifier_node = get_identifier ("access$0");
857   classdollar_identifier_node = get_identifier ("class$");
858
859   java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
860   java_io_serializable_identifier_node =
861     get_identifier ("java.io.Serializable");
862
863   /* for lack of a better place to put this stub call */
864   init_expr_processing();
865
866   constants_type_node = make_node (RECORD_TYPE);
867   PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
868   PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
869   PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
870   FINISH_RECORD (constants_type_node);
871   build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
872
873   access_flags_type_node = unsigned_short_type_node;
874
875   dtable_type = make_node (RECORD_TYPE);
876   dtable_ptr_type = build_pointer_type (dtable_type);
877
878   otable_type = build_array_type (integer_type_node, 
879                                   one_elt_array_domain_type);
880   TYPE_NONALIASED_COMPONENT (otable_type) = 1;
881   otable_ptr_type = build_pointer_type (otable_type);
882
883   PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
884   DECL_FCONTEXT (field) = object_type_node;
885   TYPE_VFIELD (object_type_node) = field;
886
887   /* This isn't exactly true, but it is what we have in the source.
888      There is an unresolved issue here, which is whether the vtable
889      should be marked by the GC.  */
890   if (! flag_hash_synchronization)
891     PUSH_FIELD (object_type_node, field, "sync_info",
892                 build_pointer_type (object_type_node));
893   for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
894     FIELD_PRIVATE (t) = 1;
895   FINISH_RECORD (object_type_node);
896
897   field_type_node = make_node (RECORD_TYPE);
898   field_ptr_type_node = build_pointer_type (field_type_node);
899   method_type_node = make_node (RECORD_TYPE);
900   method_ptr_type_node = build_pointer_type (method_type_node);
901
902   set_super_info (0, class_type_node, object_type_node, 0);
903   set_super_info (0, string_type_node, object_type_node, 0);
904   class_ptr_type = build_pointer_type (class_type_node);
905
906   PUSH_FIELD (class_type_node, field, "next_or_version", class_ptr_type);
907   PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
908   PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
909   PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
910   PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
911   PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
912   PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
913   PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
914   PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
915   PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
916   PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
917   PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
918   PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
919   PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
920   PUSH_FIELD (class_type_node, field, "otable_syms", 
921               symbols_array_ptr_type);
922   PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
923   PUSH_FIELD (class_type_node, field, "atable_syms", 
924               symbols_array_ptr_type);
925   PUSH_FIELD (class_type_node, field, "itable", itable_ptr_type);
926   PUSH_FIELD (class_type_node, field, "itable_syms", 
927               symbols_array_ptr_type);
928   PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
929   PUSH_FIELD (class_type_node, field, "interfaces",
930               build_pointer_type (class_ptr_type));
931   PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
932   PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
933   PUSH_FIELD (class_type_node, field, "state", byte_type_node);
934   PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
935   PUSH_FIELD (class_type_node, field, "depth", short_type_node);
936   PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
937   PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);  
938   PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);  
939   PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
940   PUSH_FIELD (class_type_node, field, "assertion_table", ptr_type_node);
941   PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
942   PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
943   PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
944   PUSH_FIELD (class_type_node, field, "engine", ptr_type_node);
945   for (t = TYPE_FIELDS (class_type_node);  t != NULL_TREE;  t = TREE_CHAIN (t))
946     FIELD_PRIVATE (t) = 1;
947   push_super_field (class_type_node, object_type_node);
948
949   FINISH_RECORD (class_type_node);
950   build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
951
952   field_info_union_node = make_node (UNION_TYPE);
953   PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
954   PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
955 #if 0
956   PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
957 #endif
958   layout_type (field_info_union_node);
959
960   PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
961   PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
962   PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
963   PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
964   PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
965   FINISH_RECORD (field_type_node);
966   build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
967
968   nativecode_ptr_array_type_node
969     = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
970
971   PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
972   PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
973   FINISH_RECORD (dtable_type);
974   build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
975
976   jexception_type = make_node (RECORD_TYPE);
977   PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
978   PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
979   PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
980   PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
981   FINISH_RECORD (jexception_type);
982   build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
983   jexception_ptr_type = build_pointer_type (jexception_type);
984
985   lineNumberEntry_type = make_node (RECORD_TYPE);
986   PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
987   PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
988   FINISH_RECORD (lineNumberEntry_type);
989
990   lineNumbers_type = make_node (RECORD_TYPE);
991   PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
992   FINISH_RECORD (lineNumbers_type);
993
994   PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
995   PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
996   PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
997   PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
998   PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
999   PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
1000   FINISH_RECORD (method_type_node);
1001   build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
1002
1003   endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
1004
1005   t = tree_cons (NULL_TREE, class_ptr_type, endlink);
1006   alloc_object_node = builtin_function ("_Jv_AllocObject",
1007                                         build_function_type (ptr_type_node, t),
1008                                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1009   DECL_IS_MALLOC (alloc_object_node) = 1;
1010   alloc_no_finalizer_node = 
1011     builtin_function ("_Jv_AllocObjectNoFinalizer",
1012                       build_function_type (ptr_type_node, t),
1013                       0, NOT_BUILT_IN, NULL, NULL_TREE);
1014   DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
1015
1016   t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1017   soft_initclass_node = builtin_function ("_Jv_InitClass",
1018                                           build_function_type (void_type_node,
1019                                                                t),
1020                                           0, NOT_BUILT_IN, NULL, NULL_TREE);
1021   t = tree_cons (NULL_TREE, class_ptr_type,
1022                  tree_cons (NULL_TREE, int_type_node, endlink));
1023   soft_resolvepoolentry_node 
1024     = builtin_function ("_Jv_ResolvePoolEntry", 
1025                         build_function_type (ptr_type_node, t),
1026                         0,NOT_BUILT_IN, NULL, NULL_TREE);
1027   DECL_IS_PURE (soft_resolvepoolentry_node) = 1;
1028   throw_node = builtin_function ("_Jv_Throw",
1029                                  build_function_type (void_type_node, t),
1030                                  0, NOT_BUILT_IN, NULL, NULL_TREE);
1031   /* Mark throw_nodes as `noreturn' functions with side effects.  */
1032   TREE_THIS_VOLATILE (throw_node) = 1;
1033   TREE_SIDE_EFFECTS (throw_node) = 1;
1034
1035   t = build_function_type (void_type_node, tree_cons (NULL_TREE, ptr_type_node,
1036                                                       endlink));
1037   soft_monitorenter_node 
1038     = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
1039                         NULL, NULL_TREE);
1040   soft_monitorexit_node 
1041     = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
1042                         NULL, NULL_TREE);
1043
1044   t = tree_cons (NULL_TREE, ptr_type_node, 
1045                  tree_cons (NULL_TREE, int_type_node, endlink));
1046   soft_newarray_node
1047       = builtin_function ("_Jv_NewPrimArray",
1048                           build_function_type (ptr_type_node, t),
1049                           0, NOT_BUILT_IN, NULL, NULL_TREE);
1050   DECL_IS_MALLOC (soft_newarray_node) = 1;
1051
1052   t = tree_cons (NULL_TREE, int_type_node,
1053                  tree_cons (NULL_TREE, class_ptr_type,
1054                             tree_cons (NULL_TREE, object_ptr_type_node,
1055                                        endlink)));
1056   soft_anewarray_node
1057       = builtin_function ("_Jv_NewObjectArray",
1058                           build_function_type (ptr_type_node, t),
1059                           0, NOT_BUILT_IN, NULL, NULL_TREE);
1060   DECL_IS_MALLOC (soft_anewarray_node) = 1;
1061
1062   /* There is no endlink here because _Jv_NewMultiArray is a varargs
1063      function.  */
1064   t = tree_cons (NULL_TREE, ptr_type_node,
1065                  tree_cons (NULL_TREE, int_type_node, NULL_TREE));
1066   soft_multianewarray_node
1067       = builtin_function ("_Jv_NewMultiArray",
1068                           build_function_type (ptr_type_node, t),
1069                           0, NOT_BUILT_IN, NULL, NULL_TREE);
1070   DECL_IS_MALLOC (soft_multianewarray_node) = 1;
1071
1072   t = build_function_type (void_type_node, 
1073                            tree_cons (NULL_TREE, int_type_node, endlink));
1074   soft_badarrayindex_node
1075       = builtin_function ("_Jv_ThrowBadArrayIndex", t, 
1076                           0, NOT_BUILT_IN, NULL, NULL_TREE);
1077   /* Mark soft_badarrayindex_node as a `noreturn' function with side
1078      effects.  */
1079   TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
1080   TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
1081
1082   soft_nullpointer_node
1083     = builtin_function ("_Jv_ThrowNullPointerException",
1084                         build_function_type (void_type_node, endlink),
1085                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1086   /* Mark soft_nullpointer_node as a `noreturn' function with side
1087      effects.  */
1088   TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1089   TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1090
1091   t = tree_cons (NULL_TREE, class_ptr_type,
1092                  tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1093   soft_checkcast_node
1094     = builtin_function ("_Jv_CheckCast",
1095                         build_function_type (ptr_type_node, t),
1096                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1097   t = tree_cons (NULL_TREE, object_ptr_type_node,
1098                  tree_cons (NULL_TREE, class_ptr_type, endlink));
1099   soft_instanceof_node
1100     = builtin_function ("_Jv_IsInstanceOf",
1101                         build_function_type (boolean_type_node, t),
1102                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1103   DECL_IS_PURE (soft_instanceof_node) = 1;
1104   t = tree_cons (NULL_TREE, object_ptr_type_node,
1105                  tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1106   soft_checkarraystore_node
1107     = builtin_function ("_Jv_CheckArrayStore",
1108                         build_function_type (void_type_node, t),
1109                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1110   t = tree_cons (NULL_TREE, ptr_type_node,
1111                  tree_cons (NULL_TREE, ptr_type_node,
1112                             tree_cons (NULL_TREE, int_type_node, endlink)));
1113   soft_lookupinterfacemethod_node 
1114     = builtin_function ("_Jv_LookupInterfaceMethodIdx",
1115                         build_function_type (ptr_type_node, t),
1116                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1117   DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1118   t = tree_cons (NULL_TREE, ptr_type_node,
1119                  tree_cons (NULL_TREE, ptr_type_node,
1120                             tree_cons (NULL_TREE, ptr_type_node, endlink)));
1121   soft_lookupinterfacemethodbyname_node 
1122     = builtin_function ("_Jv_LookupInterfaceMethod",
1123                         build_function_type (ptr_type_node, t),
1124                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1125   t = tree_cons (NULL_TREE, object_ptr_type_node,
1126                  tree_cons (NULL_TREE, ptr_type_node,
1127                             tree_cons (NULL_TREE, ptr_type_node, 
1128                                        tree_cons (NULL_TREE, int_type_node, 
1129                                                   endlink))));
1130   soft_lookupjnimethod_node
1131     = builtin_function ("_Jv_LookupJNIMethod",
1132                         build_function_type (ptr_type_node, t),
1133                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1134   t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1135   soft_getjnienvnewframe_node
1136     = builtin_function ("_Jv_GetJNIEnvNewFrame",
1137                         build_function_type (ptr_type_node, t),
1138                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1139   soft_jnipopsystemframe_node
1140     = builtin_function ("_Jv_JNI_PopSystemFrame",
1141                         build_function_type (void_type_node, t),
1142                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1143
1144   t = tree_cons (NULL_TREE, int_type_node,
1145                  tree_cons (NULL_TREE, int_type_node, endlink));
1146   soft_idiv_node
1147     = builtin_function ("_Jv_divI",
1148                         build_function_type (int_type_node, t),
1149                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1150
1151   soft_irem_node
1152     = builtin_function ("_Jv_remI",
1153                         build_function_type (int_type_node, t),
1154                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1155
1156   t = tree_cons (NULL_TREE, long_type_node,
1157                  tree_cons (NULL_TREE, long_type_node, endlink));
1158   soft_ldiv_node
1159     = builtin_function ("_Jv_divJ",
1160                         build_function_type (long_type_node, t),
1161                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1162
1163   soft_lrem_node
1164     = builtin_function ("_Jv_remJ",
1165                         build_function_type (long_type_node, t),
1166                         0, NOT_BUILT_IN, NULL, NULL_TREE);
1167
1168   /* Initialize variables for except.c.  */
1169   eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1170                                              ? "__gcj_personality_sj0"
1171                                              : "__gcj_personality_v0");
1172
1173   lang_eh_runtime_type = do_nothing;
1174
1175   init_jcf_parse ();
1176     
1177   initialize_builtins ();
1178   soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1179 #if 0
1180   soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
1181 #endif
1182
1183   parse_version ();
1184 }
1185
1186
1187 /* Look up NAME in the current binding level and its superiors
1188    in the namespace of variables, functions and typedefs.
1189    Return a ..._DECL node of some kind representing its definition,
1190    or return 0 if it is undefined.  */
1191
1192 tree
1193 lookup_name (tree name)
1194 {
1195   tree val;
1196   if (current_binding_level != global_binding_level
1197       && IDENTIFIER_LOCAL_VALUE (name))
1198     val = IDENTIFIER_LOCAL_VALUE (name);
1199   else
1200     val = IDENTIFIER_GLOBAL_VALUE (name);
1201   return val;
1202 }
1203
1204 /* Similar to `lookup_name' but look only at current binding level and
1205    the previous one if its the parameter level.  */
1206
1207 static tree
1208 lookup_name_current_level (tree name)
1209 {
1210   tree t;
1211
1212   if (current_binding_level == global_binding_level)
1213     return IDENTIFIER_GLOBAL_VALUE (name);
1214
1215   if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1216     return 0;
1217
1218   for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1219     if (DECL_NAME (t) == name)
1220       break;
1221
1222   return t;
1223 }
1224
1225 /* Use a binding level to record a labeled block declaration */
1226
1227 void
1228 push_labeled_block (tree lb)
1229 {
1230   tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1231   struct binding_level *b = current_binding_level;
1232   tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1233   if (oldlocal != 0)
1234       b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1235   TREE_CHAIN (lb) = b->names;
1236   b->names = lb;
1237   IDENTIFIER_LOCAL_VALUE (name) = lb;
1238 }
1239
1240 /* Pop the current binding level, reinstalling values for the previous
1241    labeled block */
1242
1243 void
1244 pop_labeled_block (void)
1245 {
1246   struct binding_level *b = current_binding_level;
1247   tree label =  b->names;
1248   IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) = 
1249     NULL_TREE;
1250   if (b->shadowed)
1251     IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) = 
1252       TREE_VALUE (b->shadowed);
1253
1254   /* Pop the current level, and free the structure for reuse.  */
1255   current_binding_level = current_binding_level->level_chain;
1256   b->level_chain = free_binding_level;
1257   free_binding_level = b;
1258 }
1259
1260 /* Record a decl-node X as belonging to the current lexical scope.
1261    Check for errors (such as an incompatible declaration for the same
1262    name already seen in the same scope).
1263
1264    Returns either X or an old decl for the same name.
1265    If an old decl is returned, it may have been smashed
1266    to agree with what X says.  */
1267
1268 tree
1269 pushdecl (tree x)
1270 {
1271   tree t;
1272   tree name = DECL_NAME (x);
1273   struct binding_level *b = current_binding_level;
1274   
1275   if (TREE_CODE (x) != TYPE_DECL)
1276     DECL_CONTEXT (x) = current_function_decl;
1277   if (name)
1278     {
1279       t = lookup_name_current_level (name);
1280       if (t != 0 && t == error_mark_node)
1281         /* error_mark_node is 0 for a while during initialization!  */
1282         {
1283           t = 0;
1284           error ("%J'%D' used prior to declaration", x, x);
1285         }
1286
1287       /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1288          to point to the TYPE_DECL.
1289          Since Java does not have typedefs, a type can only have
1290          one (true) name, given by a class, interface, or builtin. */
1291       if (TREE_CODE (x) == TYPE_DECL
1292           && TYPE_NAME (TREE_TYPE (x)) == 0
1293           && TREE_TYPE (x) != error_mark_node)
1294         {
1295           TYPE_NAME (TREE_TYPE (x)) = x;
1296           TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1297         }
1298
1299       /* This name is new in its binding level.
1300          Install the new declaration and return it.  */
1301       if (b == global_binding_level)
1302         {
1303           /* Install a global value.  */
1304           
1305           IDENTIFIER_GLOBAL_VALUE (name) = x;
1306         }
1307       else
1308         {
1309           /* Here to install a non-global value.  */
1310           tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1311           IDENTIFIER_LOCAL_VALUE (name) = x;
1312
1313 #if 0
1314           /* Warn if shadowing an argument at the top level of the body.  */
1315           if (oldlocal != 0 && !DECL_EXTERNAL (x)
1316               /* This warning doesn't apply to the parms of a nested fcn.  */
1317               && ! current_binding_level->parm_flag
1318               /* Check that this is one level down from the parms.  */
1319               && current_binding_level->level_chain->parm_flag
1320               /* Check that the decl being shadowed
1321                  comes from the parm level, one level up.  */
1322               && chain_member (oldlocal, current_binding_level->level_chain->names))
1323             {
1324               if (TREE_CODE (oldlocal) == PARM_DECL)
1325                 pedwarn ("declaration of %qs shadows a parameter",
1326                          IDENTIFIER_POINTER (name));
1327               else
1328                 pedwarn ("declaration of %qs shadows a symbol from the parameter list",
1329                          IDENTIFIER_POINTER (name));
1330             }
1331
1332           /* Maybe warn if shadowing something else.  */
1333           else if (warn_shadow && !DECL_EXTERNAL (x)
1334                    /* No shadow warnings for internally generated vars.  */
1335                    && DECL_SOURCE_LINE (x) != 0
1336                    /* No shadow warnings for vars made for inlining.  */
1337                    && ! DECL_FROM_INLINE (x))
1338             {
1339               const char *warnstring = 0;
1340
1341               if (TREE_CODE (x) == PARM_DECL
1342                   && current_binding_level->level_chain->parm_flag)
1343                 /* Don't warn about the parm names in function declarator
1344                    within a function declarator.
1345                    It would be nice to avoid warning in any function
1346                    declarator in a declaration, as opposed to a definition,
1347                    but there is no way to tell it's not a definition.  */
1348                 ;
1349               else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1350                 warnstring = "declaration of %qs shadows a parameter";
1351               else if (oldlocal != 0)
1352                 warnstring = "declaration of %qs shadows previous local";
1353               else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1354                        && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1355                 warnstring = "declaration of %qs shadows global declaration";
1356
1357               if (warnstring)
1358                 warning (0, warnstring, IDENTIFIER_POINTER (name));
1359             }
1360 #endif
1361
1362           /* If storing a local value, there may already be one (inherited).
1363              If so, record it for restoration when this binding level ends.  */
1364           if (oldlocal != 0)
1365             b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1366         }
1367     }
1368
1369   /* Put decls on list in reverse order.
1370      We will reverse them later if necessary.  */
1371   TREE_CHAIN (x) = b->names;
1372   b->names = x;
1373
1374   return x;
1375 }
1376
1377 void
1378 pushdecl_force_head (tree x)
1379 {
1380   current_binding_level->names = x;
1381 }
1382
1383 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate.  */
1384
1385 tree
1386 pushdecl_top_level (tree x)
1387 {
1388   tree t;
1389   struct binding_level *b = current_binding_level;
1390
1391   current_binding_level = global_binding_level;
1392   t = pushdecl (x);
1393   current_binding_level = b;
1394   return t;
1395 }
1396
1397 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate.  */
1398
1399 tree
1400 pushdecl_function_level (tree x)
1401 {
1402   tree t;
1403   struct binding_level *b = current_binding_level;
1404
1405   current_binding_level = function_binding_level;
1406   t = pushdecl (x);
1407   current_binding_level = b;
1408   return t;
1409 }
1410
1411 /* Nonzero if we are currently in the global binding level.  */
1412
1413 int
1414 global_bindings_p (void)
1415 {
1416   return current_binding_level == global_binding_level;
1417 }
1418
1419 /* Return the list of declarations of the current level.
1420    Note that this list is in reverse order unless/until
1421    you nreverse it; and when you do nreverse it, you must
1422    store the result back using `storedecls' or you will lose.  */
1423
1424 tree
1425 getdecls (void)
1426 {
1427   return current_binding_level->names;
1428 }
1429
1430 /* Create a new `struct binding_level'.  */
1431
1432 static struct binding_level *
1433 make_binding_level (void)
1434 {
1435   /* NOSTRICT */
1436   return ggc_alloc_cleared (sizeof (struct binding_level));
1437 }
1438
1439 void
1440 pushlevel (int unused ATTRIBUTE_UNUSED)
1441 {
1442   struct binding_level *newlevel = NULL_BINDING_LEVEL;
1443
1444 #if 0
1445   /* If this is the top level of a function,
1446      just make sure that NAMED_LABELS is 0.  */
1447
1448   if (current_binding_level == global_binding_level)
1449     named_labels = 0;
1450 #endif
1451
1452   /* Reuse or create a struct for this binding level.  */
1453
1454   if (free_binding_level)
1455     {
1456       newlevel = free_binding_level;
1457       free_binding_level = free_binding_level->level_chain;
1458     }
1459   else
1460     {
1461       newlevel = make_binding_level ();
1462     }
1463
1464   /* Add this level to the front of the chain (stack) of levels that
1465      are active.  */
1466
1467   *newlevel = clear_binding_level;
1468   newlevel->level_chain = current_binding_level;
1469   current_binding_level = newlevel;
1470 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1471   newlevel->binding_depth = binding_depth;
1472   indent ();
1473   fprintf (stderr, "push %s level %p pc %d\n",
1474            (is_class_level) ? "class" : "block", newlevel, current_pc);
1475   is_class_level = 0;
1476   binding_depth++;
1477 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1478 }
1479
1480 /* Exit a binding level.
1481    Pop the level off, and restore the state of the identifier-decl mappings
1482    that were in effect when this level was entered.
1483
1484    If KEEP is nonzero, this level had explicit declarations, so
1485    and create a "block" (a BLOCK node) for the level
1486    to record its declarations and subblocks for symbol table output.
1487
1488    If FUNCTIONBODY is nonzero, this level is the body of a function,
1489    so create a block as if KEEP were set and also clear out all
1490    label names.
1491
1492    If REVERSE is nonzero, reverse the order of decls before putting
1493    them into the BLOCK.  */
1494
1495 tree
1496 poplevel (int keep, int reverse, int functionbody)
1497 {
1498   tree link;
1499   /* The chain of decls was accumulated in reverse order.
1500      Put it into forward order, just for cleanliness.  */
1501   tree decls;
1502   tree subblocks = current_binding_level->blocks;
1503   tree block = 0;
1504   tree decl;
1505   tree bind = 0;
1506
1507 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1508   binding_depth--;
1509   indent ();
1510   if (current_binding_level->end_pc != LARGEST_PC)
1511     fprintf (stderr, "pop  %s level %p pc %d (end pc %d)\n",
1512              (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1513              current_binding_level->end_pc);
1514   else
1515     fprintf (stderr, "pop  %s level %p pc %d\n",
1516              (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1517 #if 0
1518   if (is_class_level != (current_binding_level == class_binding_level))
1519     {
1520       indent ();
1521       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1522     }
1523   is_class_level = 0;
1524 #endif
1525 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1526
1527   /* Get the decls in the order they were written.
1528      Usually current_binding_level->names is in reverse order.
1529      But parameter decls were previously put in forward order.  */
1530
1531   if (reverse)
1532     current_binding_level->names
1533       = decls = nreverse (current_binding_level->names);
1534   else
1535     decls = current_binding_level->names;
1536
1537   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1538     if (TREE_CODE (decl) == VAR_DECL
1539         && DECL_LANG_SPECIFIC (decl) != NULL
1540         && DECL_LOCAL_SLOT_NUMBER (decl))
1541       LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1542
1543   /* If there were any declarations in that level,
1544      or if this level is a function body,
1545      create a BLOCK to record them for the life of this function.  */
1546
1547   block = 0;
1548   if (keep || functionbody)
1549     {
1550       block = make_node (BLOCK);
1551       TREE_TYPE (block) = void_type_node;
1552     }
1553
1554   if (current_binding_level->exception_range)
1555     expand_end_java_handler (current_binding_level->exception_range);
1556
1557   if (block != 0)
1558     {
1559       /* If any statements have been generated at this level, create a
1560          BIND_EXPR to hold them and copy the variables to it.  This
1561          only applies to the bytecode compiler.  */
1562       if (current_binding_level->stmts)
1563         {
1564           tree decl = decls;
1565           tree *var = &BLOCK_VARS (block);
1566
1567           /* Copy decls from names list, ignoring labels.  */
1568           while (decl)
1569             {
1570               tree next = TREE_CHAIN (decl);
1571               if (TREE_CODE (decl) != LABEL_DECL)
1572                 {
1573                   *var = decl;
1574                   var = &TREE_CHAIN (decl);
1575                 }
1576               decl = next;
1577             }
1578           *var = NULL;
1579             
1580           bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block), 
1581                          BLOCK_EXPR_BODY (block), block);
1582           BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1583           
1584           if (BIND_EXPR_BODY (bind)
1585               && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1586             TREE_SIDE_EFFECTS (bind) = 1;
1587           
1588           /* FIXME: gimplifier brain damage.  */
1589           if (BIND_EXPR_BODY (bind) == NULL)
1590             BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1591
1592           current_binding_level->stmts = NULL;
1593         }
1594       else
1595         {
1596           BLOCK_VARS (block) = decls;
1597         }
1598       BLOCK_SUBBLOCKS (block) = subblocks;
1599     }   
1600
1601   /* In each subblock, record that this is its superior.  */
1602
1603   for (link = subblocks; link; link = TREE_CHAIN (link))
1604     BLOCK_SUPERCONTEXT (link) = block;
1605
1606   /* Clear out the meanings of the local variables of this level.  */
1607
1608   for (link = decls; link; link = TREE_CHAIN (link))
1609     {
1610       tree name = DECL_NAME (link);
1611       if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1612         {
1613           /* If the ident. was used or addressed via a local extern decl,
1614              don't forget that fact.  */
1615           if (DECL_EXTERNAL (link))
1616             {
1617               if (TREE_USED (link))
1618                 TREE_USED (name) = 1;
1619               if (TREE_ADDRESSABLE (link))
1620                 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1621             }
1622           IDENTIFIER_LOCAL_VALUE (name) = 0;
1623         }
1624     }
1625
1626   /* Restore all name-meanings of the outer levels
1627      that were shadowed by this level.  */
1628
1629   for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1630     IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1631
1632   /* If the level being exited is the top level of a function,
1633      check over all the labels, and clear out the current
1634      (function local) meanings of their names.  */
1635
1636   if (functionbody)
1637     {
1638       /* If this is the top level block of a function,
1639          the vars are the function's parameters.
1640          Don't leave them in the BLOCK because they are
1641          found in the FUNCTION_DECL instead.  */
1642
1643       BLOCK_VARS (block) = 0;
1644
1645       /* Clear out the definitions of all label names,
1646          since their scopes end here,
1647          and add them to BLOCK_VARS.  */
1648
1649 #if 0
1650       for (link = named_labels; link; link = TREE_CHAIN (link))
1651         {
1652           tree label = TREE_VALUE (link);
1653
1654           if (DECL_INITIAL (label) == 0)
1655             {
1656               error ("%Jlabel '%D' used but not defined", label, label);
1657               /* Avoid crashing later.  */
1658               define_label (input_location, DECL_NAME (label));
1659             }
1660           else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1661             warning (0, "%Jlabel '%D' defined but not used", label, label);
1662           IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1663
1664           /* Put the labels into the "variables" of the
1665              top-level block, so debugger can see them.  */
1666           TREE_CHAIN (label) = BLOCK_VARS (block);
1667           BLOCK_VARS (block) = label;
1668         }
1669 #endif
1670     }
1671
1672   /* Pop the current level, and free the structure for reuse.  */
1673
1674   {
1675     struct binding_level *level = current_binding_level;
1676     current_binding_level = current_binding_level->level_chain;
1677
1678     level->level_chain = free_binding_level;
1679     free_binding_level = level;
1680   }
1681
1682   /* Dispose of the block that we just made inside some higher level.  */
1683   if (functionbody)
1684     {
1685       DECL_INITIAL (current_function_decl) = block;
1686       DECL_SAVED_TREE (current_function_decl) = bind;
1687     }
1688   else 
1689     {
1690       if (block)
1691         {
1692           current_binding_level->blocks
1693             = chainon (current_binding_level->blocks, block);
1694         }
1695       /* If we did not make a block for the level just exited,
1696          any blocks made for inner levels
1697          (since they cannot be recorded as subblocks in that level)
1698          must be carried forward so they will later become subblocks
1699          of something else.  */
1700       else if (subblocks)
1701         current_binding_level->blocks
1702           = chainon (current_binding_level->blocks, subblocks);
1703
1704       if (bind)
1705         java_add_stmt (bind);
1706     }
1707
1708   if (block)
1709     TREE_USED (block) = 1;
1710   return block;
1711 }
1712
1713 void
1714 maybe_pushlevels (int pc)
1715 {
1716 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1717   current_pc = pc;
1718 #endif
1719
1720   while (pending_local_decls != NULL_TREE &&
1721          DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1722     {
1723       tree *ptr = &pending_local_decls;
1724       tree decl = *ptr, next;
1725       int end_pc = DECL_LOCAL_END_PC (decl);
1726
1727       while (*ptr != NULL_TREE
1728              && DECL_LOCAL_START_PC (*ptr) <= pc
1729              && DECL_LOCAL_END_PC (*ptr) == end_pc)
1730         ptr = &TREE_CHAIN (*ptr);
1731       pending_local_decls = *ptr;
1732       *ptr = NULL_TREE;
1733
1734       /* Force non-nested range to be nested in current range by
1735          truncating variable lifetimes. */
1736       if (end_pc > current_binding_level->end_pc)
1737         {
1738           end_pc = current_binding_level->end_pc;
1739           DECL_LOCAL_END_PC (decl) = end_pc;
1740         }
1741
1742       maybe_start_try (pc, end_pc);
1743       
1744       pushlevel (1);
1745
1746       current_binding_level->end_pc = end_pc;
1747       current_binding_level->start_pc = pc;      
1748       current_binding_level->names = NULL;
1749       for ( ; decl != NULL_TREE; decl = next)
1750         {
1751           next = TREE_CHAIN (decl);
1752           push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1753           pushdecl (decl);
1754           initialize_local_variable (decl, DECL_LOCAL_SLOT_NUMBER (decl));
1755         }
1756     }      
1757
1758   maybe_start_try (pc, 0);
1759 }
1760
1761 void
1762 maybe_poplevels (int pc)
1763 {
1764 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1765   current_pc = pc;
1766 #endif
1767
1768   /* FIXME: I'm pretty sure that this is wrong.  Variable scopes are
1769      inclusive, so a variable is live if pc == end_pc.  Here, we
1770      terminate a range if the current pc is equal to the end of the
1771      range, and this is *before* we have generated code for the
1772      instruction at end_pc.  We're closing a binding level one
1773      instruction too early.*/
1774   while (current_binding_level->end_pc <= pc)
1775     poplevel (1, 0, 0);
1776 }
1777
1778 /* Terminate any binding which began during the range beginning at
1779    start_pc.  This tidies up improperly nested local variable ranges
1780    and exception handlers; a variable declared within an exception
1781    range is forcibly terminated when that exception ends. */
1782
1783 void
1784 force_poplevels (int start_pc)
1785 {
1786   while (current_binding_level->start_pc > start_pc)
1787     {
1788       if (pedantic && current_binding_level->start_pc > start_pc)
1789         warning (0, "%JIn %D: overlapped variable and exception ranges at %d",
1790                  current_function_decl, current_function_decl,
1791                  current_binding_level->start_pc);
1792       poplevel (1, 0, 0);
1793     }
1794 }
1795
1796 /* Insert BLOCK at the end of the list of subblocks of the
1797    current binding level.  This is used when a BIND_EXPR is expanded,
1798    to handle the BLOCK node inside the BIND_EXPR.  */
1799
1800 void
1801 insert_block (tree block)
1802 {
1803   TREE_USED (block) = 1;
1804   current_binding_level->blocks
1805     = chainon (current_binding_level->blocks, block);
1806 }
1807
1808 /* integrate_decl_tree calls this function. */
1809
1810 void
1811 java_dup_lang_specific_decl (tree node)
1812 {
1813   int lang_decl_size;
1814   struct lang_decl *x;
1815
1816   if (!DECL_LANG_SPECIFIC (node))
1817     return;
1818
1819   lang_decl_size = sizeof (struct lang_decl);
1820   x = ggc_alloc (lang_decl_size);
1821   memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1822   DECL_LANG_SPECIFIC (node) = x;
1823 }
1824
1825 void
1826 give_name_to_locals (JCF *jcf)
1827 {
1828   int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1829   int code_offset = DECL_CODE_OFFSET (current_function_decl);
1830   tree parm;
1831   pending_local_decls = NULL_TREE;
1832   if (n == 0)
1833     return;
1834   JCF_SEEK (jcf, n);
1835   n = JCF_readu2 (jcf);
1836   for (i = 0; i < n; i++)
1837     {
1838       int start_pc = JCF_readu2 (jcf);
1839       int length = JCF_readu2 (jcf);
1840       int name_index = JCF_readu2 (jcf);
1841       int signature_index = JCF_readu2 (jcf);
1842       int slot = JCF_readu2 (jcf);
1843       tree name = get_name_constant (jcf, name_index);
1844       tree type = parse_signature (jcf, signature_index);
1845       if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1846           && start_pc == 0
1847           && length == DECL_CODE_LENGTH (current_function_decl))
1848         {
1849           tree decl = TREE_VEC_ELT (decl_map, slot);
1850           DECL_NAME (decl) = name;
1851           SET_DECL_ASSEMBLER_NAME (decl, name);
1852           if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1853             warning (0, "bad type in parameter debug info");
1854         }
1855       else
1856         {
1857           tree *ptr;
1858           int end_pc = start_pc + length;
1859           tree decl = build_decl (VAR_DECL, name, type);
1860           if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1861             {
1862               warning (0, "%Jbad PC range for debug info for local '%D'",
1863                        decl, decl);
1864               end_pc = DECL_CODE_LENGTH (current_function_decl);
1865             }
1866
1867           /* Adjust start_pc if necessary so that the local's first
1868              store operation will use the relevant DECL as a
1869              destination. Fore more information, read the leading
1870              comments for expr.c:maybe_adjust_start_pc. */
1871           start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1872
1873           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1874           DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1875           DECL_LOCAL_START_PC (decl) = start_pc;
1876 #if 0
1877           /* FIXME: The range used internally for exceptions and local
1878              variable ranges, is a half-open interval: 
1879              start_pc <= pc < end_pc.  However, the range used in the
1880              Java VM spec is inclusive at both ends: 
1881              start_pc <= pc <= end_pc. */
1882           end_pc++;
1883 #endif
1884           DECL_LOCAL_END_PC (decl) = end_pc;
1885
1886           /* Now insert the new decl in the proper place in
1887              pending_local_decls.  We are essentially doing an insertion sort,
1888              which works fine, since the list input will normally already
1889              be sorted. */
1890           ptr = &pending_local_decls;
1891           while (*ptr != NULL_TREE
1892                  && (DECL_LOCAL_START_PC (*ptr) > start_pc
1893                      || (DECL_LOCAL_START_PC (*ptr) == start_pc
1894                          && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1895             ptr = &TREE_CHAIN (*ptr);
1896           TREE_CHAIN (decl) = *ptr;
1897           *ptr = decl;
1898         }
1899     }
1900
1901   pending_local_decls = nreverse (pending_local_decls);
1902
1903   /* Fill in default names for the parameters. */ 
1904   for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1905        parm != NULL_TREE;  parm = TREE_CHAIN (parm), i++)
1906     {
1907       if (DECL_NAME (parm) == NULL_TREE)
1908         {
1909           int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1910           if (arg_i == 0)
1911             DECL_NAME (parm) = get_identifier ("this");
1912           else
1913             {
1914               char buffer[12];
1915               sprintf (buffer, "ARG_%d", arg_i);
1916               DECL_NAME (parm) = get_identifier (buffer);
1917             }
1918           SET_DECL_ASSEMBLER_NAME (parm, DECL_NAME (parm));
1919         }
1920     }
1921 }
1922
1923 tree
1924 build_result_decl (tree fndecl)
1925 {
1926   tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1927   tree result = DECL_RESULT (fndecl);
1928   if (! result)
1929     {
1930       /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1931       if (INTEGRAL_TYPE_P (restype)
1932           && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1933         restype = integer_type_node;
1934       result = build_decl (RESULT_DECL, NULL_TREE, restype);
1935       DECL_ARTIFICIAL (result) = 1;
1936       DECL_IGNORED_P (result) = 1;
1937       DECL_CONTEXT (result) = fndecl;
1938       DECL_RESULT (fndecl) = result;
1939     }
1940   return result;
1941 }
1942
1943 void
1944 start_java_method (tree fndecl)
1945 {
1946   tree tem, *ptr;
1947   int i;
1948
1949   uniq = 0;
1950
1951   current_function_decl = fndecl;
1952   announce_function (fndecl);
1953
1954   i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1955   decl_map = make_tree_vec (i);
1956   base_decl_map = make_tree_vec (i);
1957   type_map = xrealloc (type_map, i * sizeof (tree));
1958
1959 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1960   fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1961   current_pc = 0;
1962 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1963   pushlevel (1);  /* Push parameters. */
1964
1965   ptr = &DECL_ARGUMENTS (fndecl);
1966   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1967        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1968     {
1969       tree parm_name = NULL_TREE, parm_decl;
1970       tree parm_type = TREE_VALUE (tem);
1971       if (i >= DECL_MAX_LOCALS (fndecl))
1972         abort ();
1973
1974       parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1975       DECL_CONTEXT (parm_decl) = fndecl;
1976       if (targetm.calls.promote_prototypes (parm_type)
1977           && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1978           && INTEGRAL_TYPE_P (parm_type))
1979         parm_type = integer_type_node;
1980       DECL_ARG_TYPE (parm_decl) = parm_type;
1981
1982       *ptr = parm_decl;
1983       ptr = &TREE_CHAIN (parm_decl);
1984
1985       /* Add parm_decl to the decl_map. */
1986       push_jvm_slot (i, parm_decl);
1987
1988       type_map[i] = TREE_TYPE (parm_decl);
1989       if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1990         {
1991           i++;
1992           type_map[i] = void_type_node;
1993         }
1994     }
1995   *ptr = NULL_TREE;
1996   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1997
1998   while (i < DECL_MAX_LOCALS(fndecl))
1999     type_map[i++] = NULL_TREE;
2000
2001   build_result_decl (fndecl);
2002
2003   /* Push local variables.  */
2004   pushlevel (2);
2005
2006   function_binding_level = current_binding_level;
2007 }
2008
2009 void
2010 end_java_method (void)
2011 {
2012   tree fndecl = current_function_decl;
2013
2014   /* pop out of function */
2015   poplevel (1, 1, 0);
2016
2017   /* pop out of its parameters */
2018   poplevel (1, 0, 1);
2019
2020   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2021   
2022   if (DECL_SAVED_TREE (fndecl))
2023     {
2024       tree fbody, block_body;
2025       /* Before we check initialization, attached all class initialization
2026          variable to the block_body */
2027       fbody = DECL_SAVED_TREE (fndecl);
2028       block_body = BIND_EXPR_BODY (fbody);
2029       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
2030                      attach_init_test_initialization_flags, block_body);
2031     }
2032
2033   flag_unit_at_a_time = 0;
2034   finish_method (fndecl);
2035
2036   if (! flag_unit_at_a_time)
2037     {
2038       /* Nulling these fields when we no longer need them saves
2039          memory.  */
2040       DECL_SAVED_TREE (fndecl) = NULL;
2041       DECL_STRUCT_FUNCTION (fndecl) = NULL;
2042       DECL_INITIAL (fndecl) = NULL_TREE;
2043     }
2044   current_function_decl = NULL_TREE;
2045 }
2046
2047 /* Prepare a method for expansion.  */
2048
2049 void
2050 finish_method (tree fndecl)
2051 {
2052   tree *tp = &DECL_SAVED_TREE (fndecl);
2053
2054   /* Wrap body of synchronized methods in a monitorenter,
2055      plus monitorexit cleanup.  */
2056   if (METHOD_SYNCHRONIZED (fndecl))
2057     {
2058       tree enter, exit, lock;
2059       if (METHOD_STATIC (fndecl))
2060         lock = build_class_ref (DECL_CONTEXT (fndecl));
2061       else
2062         lock = DECL_ARGUMENTS (fndecl);
2063       BUILD_MONITOR_ENTER (enter, lock);
2064       BUILD_MONITOR_EXIT (exit, lock);
2065       *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
2066                     build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
2067     }
2068
2069   /* Prepend class initialization for static methods reachable from
2070      other classes.  */
2071   if (METHOD_STATIC (fndecl)
2072       && (! METHOD_PRIVATE (fndecl)
2073           || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
2074       && ! DECL_CLINIT_P (fndecl)
2075       && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
2076     {
2077       tree clas = DECL_CONTEXT (fndecl);
2078       tree init = build3 (CALL_EXPR, void_type_node,
2079                           build_address_of (soft_initclass_node),
2080                           build_tree_list (NULL_TREE, build_class_ref (clas)),
2081                           NULL_TREE);
2082       *tp = build2 (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
2083     }
2084
2085   /* Convert function tree to GENERIC prior to inlining.  */
2086   java_genericize (fndecl);
2087
2088   /* Store the end of the function, so that we get good line number
2089      info for the epilogue.  */
2090   if (DECL_STRUCT_FUNCTION (fndecl))
2091     cfun = DECL_STRUCT_FUNCTION (fndecl);
2092   else
2093     allocate_struct_function (fndecl);
2094 #ifdef USE_MAPPED_LOCATION
2095   cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
2096 #else
2097   cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
2098   cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
2099 #endif
2100
2101   /* Defer inlining and expansion to the cgraph optimizers.  */
2102   cgraph_finalize_function (fndecl, false);
2103 }
2104
2105 /* Optimize and expand a function's entire body.  */
2106
2107 void
2108 java_expand_body (tree fndecl)
2109 {
2110   tree_rest_of_compilation (fndecl);
2111 }
2112
2113 /* We pessimistically marked all methods and fields external until we
2114    knew what set of classes we were planning to compile.  Now mark those
2115    associated with CLASS to be generated locally as not external.  */
2116
2117 static void
2118 java_mark_decl_local (tree decl)
2119 {
2120   DECL_EXTERNAL (decl) = 0;
2121
2122   /* If we've already constructed DECL_RTL, give encode_section_info
2123      a second chance, now that we've changed the flags.  */
2124   if (DECL_RTL_SET_P (decl))
2125     make_decl_rtl (decl);
2126 }
2127
2128 void
2129 java_mark_class_local (tree class)
2130 {
2131   tree t;
2132
2133   for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
2134     if (FIELD_STATIC (t))
2135       java_mark_decl_local (t);
2136
2137   for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
2138     if (!METHOD_ABSTRACT (t) && (!METHOD_NATIVE (t) || flag_jni))
2139       java_mark_decl_local (t);
2140 }
2141
2142 /* Add a statement to a compound_expr.  */
2143
2144 tree
2145 add_stmt_to_compound (tree existing, tree type, tree stmt)
2146 {
2147   if (!stmt)
2148     return existing;
2149   else if (existing)
2150     {
2151       tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2152       TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2153                                  | TREE_SIDE_EFFECTS (stmt);
2154       return expr;
2155     }
2156   else
2157     return stmt;
2158 }
2159
2160 /* Add a statement to the compound_expr currently being
2161    constructed.  */
2162
2163 tree
2164 java_add_stmt (tree stmt)
2165 {
2166   if (input_filename)
2167     SET_EXPR_LOCATION (stmt, input_location);
2168   
2169   return current_binding_level->stmts 
2170     = add_stmt_to_compound (current_binding_level->stmts, 
2171                             TREE_TYPE (stmt), stmt);
2172 }
2173
2174 /* Add a variable to the current scope.  */
2175
2176 tree
2177 java_add_local_var (tree decl)
2178 {
2179   tree *vars = &current_binding_level->names;
2180   tree next = *vars;
2181   TREE_CHAIN (decl) = next;
2182   *vars = decl;
2183   DECL_CONTEXT (decl) = current_function_decl;
2184   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2185   return decl;
2186 }
2187
2188 /* Return a pointer to the compound_expr currently being
2189    constructed.  */
2190
2191 tree *
2192 get_stmts (void)
2193 {
2194   return &current_binding_level->stmts;
2195 }
2196
2197 /* Register an exception range as belonging to the current binding
2198    level.  There may only be one: if there are more, we'll create more
2199    binding levels.  However, each range can have multiple handlers,
2200    and these are expanded when we call expand_end_java_handler().  */
2201
2202 void
2203 register_exception_range (struct eh_range *range, int pc, int end_pc)
2204 {
2205   if (current_binding_level->exception_range)
2206     abort ();
2207   current_binding_level->exception_range = range;
2208   current_binding_level->end_pc = end_pc;
2209   current_binding_level->start_pc = pc;      
2210 }
2211
2212 #include "gt-java-decl.h"