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