decl.c 65.6 KB
Newer Older
Anthony Green committed
1 2
/* Process declarations and variables for the GNU compiler for the
   Java(TM) language.
3
   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007,
4
   2005, 2006, 2007 Free Software Foundation, Inc.
Anthony Green committed
5

6
This file is part of GCC.
Anthony Green committed
7

8
GCC is free software; you can redistribute it and/or modify
Anthony Green committed
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3, or (at your option)
Anthony Green committed
11 12
any later version.

13
GCC is distributed in the hope that it will be useful,
Anthony Green committed
14 15 16 17 18
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
19 20
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.
Anthony Green committed
21 22 23 24 25 26 27 28

Java and all Java-based marks are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other countries.
The Free Software Foundation is independent of Sun Microsystems, Inc.  */

/* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */

#include "config.h"
29
#include "system.h"
30 31
#include "coretypes.h"
#include "tm.h"
Anthony Green committed
32
#include "tree.h"
33
#include "rtl.h"
34
#include "real.h"
35 36
#include "toplev.h"
#include "flags.h"
Anthony Green committed
37 38
#include "java-tree.h"
#include "jcf.h"
39
#include "function.h"
40
#include "expr.h"
41
#include "libfuncs.h"
42
#include "except.h"
43
#include "java-except.h"
44
#include "ggc.h"
45
#include "timevar.h"
46
#include "cgraph.h"
47
#include "tree-inline.h"
48
#include "target.h"
49
#include "version.h"
50
#include "tree-iterator.h"
51
#include "langhooks.h"
52 53

#if defined (DEBUG_JAVA_BINDING_LEVELS)
54
extern void indent (void);
55
#endif
Anthony Green committed
56

57 58 59 60 61 62
static tree push_jvm_slot (int, tree);
static tree lookup_name_current_level (tree);
static tree push_promoted_type (const char *, tree);
static struct binding_level *make_binding_level (void);
static tree create_primitive_vtable (const char *);
static tree check_local_unnamed_variable (tree, tree, tree);
63 64 65
static void parse_version (void);


66 67 68
/* The following ABI flags are used in the high-order bits of the version
   ID field. The version ID number itself should never be larger than 
   0xfffff, so it should be safe to use top 12 bits for these flags. */
69

70 71 72 73 74 75 76
#define FLAG_BINARYCOMPAT_ABI (1<<31)  /* Class is built with the BC-ABI. */

#define FLAG_BOOTSTRAP_LOADER (1<<30)  /* Used when defining a class that 
					  should be loaded by the bootstrap
					  loader.  */

/* If an ABI change is made within a GCC release series, rendering current
77
   binaries incompatible with the old runtimes, this number must be set to
78
   enforce the compatibility rules. */
79
#define MINOR_BINARYCOMPAT_ABI_VERSION 1
80 81 82 83 84 85 86 87

/* The runtime may recognize a variety of BC ABIs (objects generated by 
   different version of gcj), but will probably always require strict 
   matching for the ordinary (C++) ABI.  */

/* The version ID of the BC ABI that we generate.  This must be kept in
   sync with parse_version(), libgcj, and reality (if the BC format changes, 
   this must change).  */
88
#define GCJ_CURRENT_BC_ABI_VERSION \
89
  (4 * 100000 + 0 * 1000 + MINOR_BINARYCOMPAT_ABI_VERSION)
90

91 92
/* The ABI version number.  */
tree gcj_abi_version;
93

94 95 96 97 98 99
/* Name of the Cloneable class.  */
tree java_lang_cloneable_identifier_node;

/* Name of the Serializable class.  */
tree java_io_serializable_identifier_node;

Anthony Green committed
100 101 102 103 104 105 106 107
/* The DECL_MAP is a mapping from (index, type) to a decl node.
   If index < max_locals, it is the index of a local variable.
   if index >= max_locals, then index-max_locals is a stack slot.
   The DECL_MAP mapping is represented as a TREE_VEC whose elements
   are a list of decls (VAR_DECL or PARM_DECL) chained by
   DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
   we search the chain for a decl with a matching TREE_TYPE. */

108
static GTY(()) tree decl_map;
Anthony Green committed
109

110 111 112 113 114 115 116 117 118
/* The base_decl_map is contains one variable of ptr_type: this is
   used to contain every variable of reference type that is ever
   stored in a local variable slot.  */

static GTY(()) tree base_decl_map;

/* An index used to make temporary identifiers unique.  */
static int uniq;

Anthony Green committed
119 120 121
/* A list of local variables VAR_DECLs for this method that we have seen
   debug information, but we have not reached their starting (byte) PC yet. */

122
static GTY(()) tree pending_local_decls;
Anthony Green committed
123

124 125 126
/* The decl for "_Jv_ResolvePoolEntry".  */
tree soft_resolvepoolentry_node;

127 128 129 130 131 132
/* The decl for the .constants field of an instance of Class.  */
tree constants_field_decl_node;

/* The decl for the .data field of an instance of Class.  */
tree constants_data_field_decl_node;

133 134 135 136 137 138
#if defined(DEBUG_JAVA_BINDING_LEVELS)
int binding_depth = 0;
int is_class_level = 0;
int current_pc;

void
139
indent (void)
140
{
141
  int i;
142 143 144 145 146 147

  for (i = 0; i < binding_depth*2; i++)
    putc (' ', stderr);
}
#endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/* True if decl is a named local variable, i.e. if it is an alias
   that's used only for debugging purposes.  */

static bool
debug_variable_p (tree decl)
{
  if (TREE_CODE (decl) == PARM_DECL)
    return false;

  if (LOCAL_SLOT_P (decl))
    return false;

  return true;
}
 
163
static tree
164
push_jvm_slot (int index, tree decl)
Anthony Green committed
165 166 167 168 169 170 171
{
  DECL_CONTEXT (decl) = current_function_decl;
  layout_decl (decl, 0);

  /* Now link the decl into the decl_map. */
  if (DECL_LANG_SPECIFIC (decl) == NULL)
    {
172
      MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
Anthony Green committed
173 174 175 176 177 178
      DECL_LOCAL_START_PC (decl) = 0;
      DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
      DECL_LOCAL_SLOT_NUMBER (decl) = index;
    }
  DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
  TREE_VEC_ELT (decl_map, index) = decl;
179

Anthony Green committed
180 181 182
  return decl;
}

183 184 185 186
/* Find the best declaration based upon type.  If 'decl' fits 'type' better
   than 'best', return 'decl'.  Otherwise return 'best'.  */

static tree
187
check_local_unnamed_variable (tree best, tree decl, tree type)
188
{
189 190
  tree decl_type = TREE_TYPE (decl);
  
191
  gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (decl));
192 193 194 195 196 197 198 199

  /* Use the same decl for all integer types <= 32 bits.  This is
     necessary because sometimes a value is stored as (for example)
     boolean but loaded as int.  */
  if (decl_type == type
      || (INTEGRAL_TYPE_P (decl_type)
	  && INTEGRAL_TYPE_P (type)
	  && TYPE_PRECISION (decl_type) <= 32
200
	  && TYPE_PRECISION (type) <= 32
201
	  && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))      
202 203 204 205 206 207 208 209 210 211 212
      /*  ptr_type_node is used for null pointers, which are
	  assignment compatible with everything.  */
      || (TREE_CODE (decl_type) == POINTER_TYPE
	  && type == ptr_type_node)
      /* Whenever anyone wants to use a slot that is initially
	 occupied by a PARM_DECL of pointer type they must get that
	 decl, even if they asked for a pointer to a different type.
	 However, if someone wants a scalar variable in a slot that
	 initially held a pointer arg -- or vice versa -- we create a
	 new VAR_DECL.  

213
      	 ???: As long as verification is correct, this will be a
214
	 compatible type.  But maybe we should create a dummy variable
215
	 and replace all references to it with the DECL and a
216
	 NOP_EXPR.  
217 218 219
      */
      || (TREE_CODE (decl_type) == POINTER_TYPE
	  && TREE_CODE (decl) == PARM_DECL
220
	  && TREE_CODE (type) == POINTER_TYPE))
221 222
    {
      if (best == NULL_TREE
223
	  || (decl_type == type && TREE_TYPE (best) != type))
224 225
	return decl;
    }
226

227
  return best;
228 229 230
}


Anthony Green committed
231 232
/* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
   that is valid at PC (or -1 if any pc).
233
   If there is no existing matching decl, allocate one.  */
Anthony Green committed
234 235

tree
236
find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
Anthony Green committed
237
{
238 239
  tree tmp = TREE_VEC_ELT (decl_map, index);
  tree decl = NULL_TREE;
240

241 242 243 244 245
  /* Scan through every declaration that has been created in this
     slot.  We're only looking for variables that correspond to local
     index declarations and PARM_DECLs, not named variables: such
     local variables are used only for debugging information.  */
  while (tmp != NULL_TREE)
Anthony Green committed
246
    {
247 248 249
      if (! debug_variable_p (tmp))
	decl = check_local_unnamed_variable (decl, tmp, type);
      tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
Anthony Green committed
250
    }
251

252 253 254 255 256 257 258 259 260 261
  /* gcj has a function called promote_type(), which is used by both
     the bytecode compiler and the source compiler.  Unfortunately,
     the type systems for the Java VM and the Java language are not
     the same: a boolean in the VM promotes to an int, not to a wide
     boolean.  If our caller wants something to hold a boolean, that
     had better be an int, because that slot might be re-used
     later in integer context.  */
  if (TREE_CODE (type) == BOOLEAN_TYPE)
    type = integer_type_node;

262
  /* If we don't find a match, create one with the type passed in.
263
     The name of the variable is #n#m, which n is the variable index
264 265
     in the local variable area and m is a dummy identifier for
     uniqueness -- multiple variables may share the same local
266 267 268 269 270
     variable index.  We don't call pushdecl() to push pointer types
     into a binding expr because they'll all be replaced by a single
     variable that is used for every reference in that local variable
     slot.  */
  if (! decl)
271 272 273
    {
      char buf[64];
      tree name;
274 275 276 277 278 279 280 281 282 283 284
      sprintf (buf, "#slot#%d#%d", index, uniq++);
      name = get_identifier (buf);
      decl = build_decl (VAR_DECL, name, type);
      DECL_IGNORED_P (decl) = 1;
      DECL_ARTIFICIAL (decl) = 1;
      decl = push_jvm_slot (index, decl);
      LOCAL_SLOT_P (decl) = 1;

      if (TREE_CODE (type) != POINTER_TYPE)
	pushdecl_function_level (decl);
    }
285

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  /* As well as creating a local variable that matches the type, we
     also create a base variable (of ptr_type) that will hold all its
     aliases.  */
  if (TREE_CODE (type) == POINTER_TYPE
      && ! TREE_VEC_ELT (base_decl_map, index))
    {
      char buf[64];
      tree name;
      tree base_decl;
      sprintf (buf, "#ref#%d#%d", index, uniq++);
      name = get_identifier (buf);
      base_decl
	= TREE_VEC_ELT (base_decl_map, index)
	= build_decl (VAR_DECL, name, ptr_type_node);
      pushdecl_function_level (base_decl);
      DECL_IGNORED_P (base_decl) = 1;
      DECL_ARTIFICIAL (base_decl) = 1;
    }

  return decl;
}

/* Called during gimplification for every variable.  If the variable
   is a temporary of pointer type, replace it with a common variable
   thath is used to hold all pointer types that are ever stored in
   that slot.  Set WANT_LVALUE if you want a variable that is to be
   written to.  */

tree 
java_replace_reference (tree var_decl, bool want_lvalue)
{
  tree decl_type;

  if (! base_decl_map)
    return var_decl;

  decl_type = TREE_TYPE (var_decl);

  if (TREE_CODE (decl_type) == POINTER_TYPE)
    {
      if (DECL_LANG_SPECIFIC (var_decl)
	  && LOCAL_SLOT_P (var_decl))
	{
	  int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
	  tree base_decl = TREE_VEC_ELT (base_decl_map, index); 

332
	  gcc_assert (base_decl);
333 334 335 336 337 338 339 340
	  if (! want_lvalue)
	    base_decl = build1 (NOP_EXPR, decl_type, base_decl);

	  return base_decl;
	}
    }

  return var_decl;
Anthony Green committed
341 342 343 344 345 346
}


/* Same as find_local_index, except that INDEX is a stack index. */

tree
347
find_stack_slot (int index, tree type)
Anthony Green committed
348 349 350 351 352
{
  return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
			      type, -1);
}

353
struct binding_level GTY(())
Anthony Green committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  {
    /* A chain of _DECL nodes for all variables, constants, functions,
     * and typedef types.  These are in the reverse of the order supplied.
     */
    tree names;

    /* For each level, a list of shadowed outer-level local definitions
       to be restored when this level is popped.
       Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
       whose TREE_VALUE is its old definition (a kind of ..._DECL node).  */
    tree shadowed;

    /* For each level (except not the global one),
       a chain of BLOCK nodes for all the levels
       that were entered and exited one level down.  */
    tree blocks;

    /* The binding level which this one is contained in (inherits from).  */
    struct binding_level *level_chain;

    /* The bytecode PC that marks the end of this level. */
    int end_pc;
376
    /* The bytecode PC that marks the start of this level. */
377 378
    int start_pc;

379 380 381
    /* The statements in this binding level.  */
    tree stmts;

382 383 384
    /* An exception range associated with this binding level.  */
    struct eh_range * GTY((skip (""))) exception_range;

385
    /* Binding depth at which this level began.  Used only for debugging.  */
386
    unsigned binding_depth;
Anthony Green committed
387 388 389 390 391 392
  };

#define NULL_BINDING_LEVEL (struct binding_level *) NULL

/* The binding level currently in effect.  */

393
static GTY(()) struct binding_level *current_binding_level;
Anthony Green committed
394 395 396

/* A chain of binding_level structures awaiting reuse.  */

397
static GTY(()) struct binding_level *free_binding_level;
Anthony Green committed
398 399 400 401 402

/* The outermost binding level, for names of file scope.
   This is created when the compiler is started and exists
   through the entire run.  */

403
static GTY(()) struct binding_level *global_binding_level;
Anthony Green committed
404

405 406 407 408 409
/* The binding level that holds variables declared at the outermost
   level within a function body.  */

static struct binding_level *function_binding_level;

410 411 412 413
/* A PC value bigger than any PC value we may ever may encounter. */

#define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)

Anthony Green committed
414 415
/* Binding level structures are initialized by copying this one.  */

416
static const struct binding_level clear_binding_level
417 418 419 420 421 422 423 424 425 426 427
= {
    NULL_TREE, /* names */
    NULL_TREE, /* shadowed */
    NULL_TREE, /* blocks */
    NULL_BINDING_LEVEL, /* level_chain */
    LARGEST_PC, /* end_pc */
    0, /* start_pc */
    NULL, /* stmts */
    NULL, /* exception_range */
    0, /* binding_depth */
  };
Anthony Green committed
428

429
tree java_global_trees[JTI_MAX];
Anthony Green committed
430 431 432 433 434
  
/* Build (and pushdecl) a "promoted type" for all standard
   types shorter than int.  */

static tree
435
push_promoted_type (const char *name, tree actual_type)
Anthony Green committed
436 437 438 439 440 441 442 443 444
{
  tree type = make_node (TREE_CODE (actual_type));
#if 1
  tree in_min = TYPE_MIN_VALUE (int_type_node);
  tree in_max = TYPE_MAX_VALUE (int_type_node);
#else
  tree in_min = TYPE_MIN_VALUE (actual_type);
  tree in_max = TYPE_MAX_VALUE (actual_type);
#endif
445
  TYPE_MIN_VALUE (type) = copy_node (in_min);
Anthony Green committed
446
  TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
447
  TYPE_MAX_VALUE (type) = copy_node (in_max);
Anthony Green committed
448 449
  TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
450
  TYPE_STRING_FLAG (type) = TYPE_STRING_FLAG (actual_type);
Anthony Green committed
451 452 453 454 455
  layout_type (type);
  pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
  return type;
}

456 457
/* Return tree that represents a vtable for a primitive array.  */
static tree
458
create_primitive_vtable (const char *name)
459 460 461 462 463 464 465 466 467 468
{
  tree r;
  char buf[50];

  sprintf (buf, "_Jv_%sVTable", name);
  r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
  DECL_EXTERNAL (r) = 1;
  return r;
}

469 470 471 472 473 474
static tree
do_nothing (tree t)
{
  return t;
}

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
/* Parse the version string and compute the ABI version number.  */
static void
parse_version (void)
{
  const char *p = version_string;
  unsigned int major = 0, minor = 0;
  unsigned int abi_version;

  /* Skip leading junk.  */
  while (*p && !ISDIGIT (*p))
    ++p;
  gcc_assert (*p);

  /* Extract major version.  */
  while (ISDIGIT (*p))
    {
      major = major * 10 + *p - '0';
      ++p;
    }

  gcc_assert (*p == '.' && ISDIGIT (p[1]));
  ++p;

  /* Extract minor version.  */
  while (ISDIGIT (*p))
    {
      minor = minor * 10 + *p - '0';
      ++p;
    }

  if (flag_indirect_dispatch)
506 507 508 509 510 511 512 513 514 515 516
    {
      abi_version = GCJ_CURRENT_BC_ABI_VERSION;
      abi_version |= FLAG_BINARYCOMPAT_ABI;
    }
  else /* C++ ABI */
    {
      /* Implicit in this computation is the idea that we won't break the
	 old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
	 4.0.1).  */
      abi_version = 100000 * major + 1000 * minor;
    }
517
  if (flag_bootstrap_classes)
518
    abi_version |= FLAG_BOOTSTRAP_LOADER;
519 520 521

  gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
}
522

Anthony Green committed
523
void
524
java_init_decl_processing (void)
Anthony Green committed
525
{
526
  tree endlink;
Kaveh R. Ghazi committed
527
  tree field = NULL_TREE;
Anthony Green committed
528 529
  tree t;

530 531
  init_class_processing ();

Anthony Green committed
532 533 534 535 536 537
  current_function_decl = NULL;
  current_binding_level = NULL_BINDING_LEVEL;
  free_binding_level = NULL_BINDING_LEVEL;
  pushlevel (0);	/* make the binding_level structure for global names */
  global_binding_level = current_binding_level;

538 539
  /* The code here must be similar to build_common_tree_nodes{,_2} in
     tree.c, especially as to the order of initializing common nodes.  */
Anthony Green committed
540 541 542 543
  error_mark_node = make_node (ERROR_MARK);
  TREE_TYPE (error_mark_node) = error_mark_node;

  /* Create sizetype first - needed for other types. */
544
  initialize_sizetypes (false);
Anthony Green committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567

  byte_type_node = make_signed_type (8);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
  short_type_node = make_signed_type (16);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
  int_type_node = make_signed_type (32);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
  long_type_node = make_signed_type (64);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));

  unsigned_byte_type_node = make_unsigned_type (8);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
			unsigned_byte_type_node));
  unsigned_short_type_node = make_unsigned_type (16);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
			unsigned_short_type_node));
  unsigned_int_type_node = make_unsigned_type (32);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
			unsigned_int_type_node));
  unsigned_long_type_node = make_unsigned_type (64);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
			unsigned_long_type_node));

568 569 570 571
  /* This is not a java type, however tree-dfa requires a definition for
     size_type_node.  */
  size_type_node = make_unsigned_type (POINTER_SIZE);
  set_sizetype (size_type_node);
Anthony Green committed
572

573
  /* Define these next since types below may used them.  */
574
  integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
575 576 577 578 579
  integer_zero_node = build_int_cst (NULL_TREE, 0);
  integer_one_node = build_int_cst (NULL_TREE, 1);
  integer_two_node = build_int_cst (NULL_TREE, 2);
  integer_four_node = build_int_cst (NULL_TREE, 4);
  integer_minus_one_node = build_int_cst (NULL_TREE, -1);
Anthony Green committed
580

581
  /* A few values used for range checking in the lexer.  */
582
  decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
583
#if HOST_BITS_PER_WIDE_INT == 64
584 585 586 587 588
  decimal_long_max = build_int_cstu (unsigned_long_type_node,
				     0x8000000000000000LL);
#elif HOST_BITS_PER_WIDE_INT == 32
  decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
					 0, 0x80000000);
589 590 591 592
#else
 #error "unsupported size"
#endif

593 594 595 596 597 598
  size_zero_node = size_int (0);
  size_one_node = size_int (1);
  bitsize_zero_node = bitsize_int (0);
  bitsize_one_node = bitsize_int (1);
  bitsize_unit_node = bitsize_int (BITS_PER_UNIT);

599
  long_zero_node = build_int_cst (long_type_node, 0);
600

Anthony Green committed
601 602 603
  void_type_node = make_node (VOID_TYPE);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
  layout_type (void_type_node);	/* Uses size_zero_node */
604

Anthony Green committed
605
  ptr_type_node = build_pointer_type (void_type_node);
606 607 608
  const_ptr_type_node
    = build_pointer_type (build_type_variant (void_type_node, 1, 0));

Anthony Green committed
609 610 611 612
  t = make_node (VOID_TYPE);
  layout_type (t); /* Uses size_zero_node */
  return_address_type_node = build_pointer_type (t);

613
  null_pointer_node = build_int_cst (ptr_type_node, 0);
Anthony Green committed
614

615 616
  char_type_node = make_node (INTEGER_TYPE);
  TYPE_STRING_FLAG (char_type_node) = 1;
Anthony Green committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
  TYPE_PRECISION (char_type_node) = 16;
  fixup_unsigned_type (char_type_node);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));

  boolean_type_node = make_node (BOOLEAN_TYPE);
  TYPE_PRECISION (boolean_type_node) = 1;
  fixup_unsigned_type (boolean_type_node);
  pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
			boolean_type_node));
  boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
  boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);

  promoted_byte_type_node
    = push_promoted_type ("promoted_byte", byte_type_node);
  promoted_short_type_node
    = push_promoted_type ("promoted_short", short_type_node);
  promoted_char_type_node
    = push_promoted_type ("promoted_char", char_type_node);
  promoted_boolean_type_node
    = push_promoted_type ("promoted_boolean", boolean_type_node);

  float_type_node = make_node (REAL_TYPE);
  TYPE_PRECISION (float_type_node) = 32;
  pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
                        float_type_node));
  layout_type (float_type_node);

  double_type_node = make_node (REAL_TYPE);
  TYPE_PRECISION (double_type_node) = 64;
  pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
                        double_type_node));
  layout_type (double_type_node);

650 651 652
  float_zero_node = build_real (float_type_node, dconst0);
  double_zero_node = build_real (double_type_node, dconst0);

653 654 655 656 657 658 659 660 661 662
  /* These are the vtables for arrays of primitives.  */
  boolean_array_vtable = create_primitive_vtable ("boolean");
  byte_array_vtable = create_primitive_vtable ("byte");
  char_array_vtable = create_primitive_vtable ("char");
  short_array_vtable = create_primitive_vtable ("short");
  int_array_vtable = create_primitive_vtable ("int");
  long_array_vtable = create_primitive_vtable ("long");
  float_array_vtable = create_primitive_vtable ("float");
  double_array_vtable = create_primitive_vtable ("double");

663 664 665 666 667 668 669 670 671 672 673 674
  one_elt_array_domain_type = build_index_type (integer_one_node);
  utf8const_type = make_node (RECORD_TYPE);
  PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
  PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
  FINISH_RECORD (utf8const_type);
  utf8const_ptr_type = build_pointer_type (utf8const_type);

  atable_type = build_array_type (ptr_type_node, 
				  one_elt_array_domain_type);
  TYPE_NONALIASED_COMPONENT (atable_type) = 1;
  atable_ptr_type = build_pointer_type (atable_type);

675 676 677 678 679
  itable_type = build_array_type (ptr_type_node, 
				  one_elt_array_domain_type);
  TYPE_NONALIASED_COMPONENT (itable_type) = 1;
  itable_ptr_type = build_pointer_type (itable_type);

680 681 682 683 684 685 686 687 688 689
  symbol_type = make_node (RECORD_TYPE);
  PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
  PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
  PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
  FINISH_RECORD (symbol_type);

  symbols_array_type = build_array_type (symbol_type, 
					 one_elt_array_domain_type);
  symbols_array_ptr_type = build_pointer_type (symbols_array_type);

690 691 692 693 694 695 696 697 698
  assertion_entry_type = make_node (RECORD_TYPE);
  PUSH_FIELD (assertion_entry_type, field, "assertion_code", integer_type_node);
  PUSH_FIELD (assertion_entry_type, field, "op1", utf8const_ptr_type);
  PUSH_FIELD (assertion_entry_type, field, "op2", utf8const_ptr_type);
  FINISH_RECORD (assertion_entry_type);
  
  assertion_table_type = build_array_type (assertion_entry_type,
                                           one_elt_array_domain_type);

699
  /* As you're adding items here, please update the code right after
700 701
     this section, so that the filename containing the source code of
     the pre-defined class gets registered correctly. */
702
  unqualified_object_id_node = get_identifier ("Object");
Anthony Green committed
703 704 705
  object_type_node = lookup_class (get_identifier ("java.lang.Object"));
  object_ptr_type_node = promote_type (object_type_node);
  string_type_node = lookup_class (get_identifier ("java.lang.String"));
706
  string_ptr_type_node = promote_type (string_type_node);
Anthony Green committed
707 708
  class_type_node = lookup_class (get_identifier ("java.lang.Class"));
  throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
709
  exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
710 711 712 713
  runtime_exception_type_node = 
    lookup_class (get_identifier ("java.lang.RuntimeException"));
  error_exception_type_node = 
    lookup_class (get_identifier ("java.lang.Error"));
714

715 716
  rawdata_ptr_type_node
    = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
Anthony Green committed
717

718 719 720 721 722 723 724 725 726 727
  add_predefined_file (get_identifier ("java/lang/Class.java"));
  add_predefined_file (get_identifier ("java/lang/Error.java"));
  add_predefined_file (get_identifier ("java/lang/Object.java"));
  add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
  add_predefined_file (get_identifier ("java/lang/String.java"));
  add_predefined_file (get_identifier ("java/lang/Throwable.java"));
  add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
  add_predefined_file (get_identifier ("java/lang/Exception.java"));
  add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
  add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
728

Anthony Green committed
729 730
  methodtable_type = make_node (RECORD_TYPE);
  layout_type (methodtable_type);
731
  build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
Anthony Green committed
732 733 734 735 736 737
  methodtable_ptr_type = build_pointer_type (methodtable_type);

  TYPE_identifier_node = get_identifier ("TYPE");
  init_identifier_node = get_identifier ("<init>");
  clinit_identifier_node = get_identifier ("<clinit>");
  void_signature_node = get_identifier ("()V");
738
  finalize_identifier_node = get_identifier ("finalize");
Anthony Green committed
739
  this_identifier_node = get_identifier ("this");
740
  classdollar_identifier_node = get_identifier ("class$");
Anthony Green committed
741

742 743 744 745
  java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
  java_io_serializable_identifier_node =
    get_identifier ("java.io.Serializable");

Anthony Green committed
746 747 748 749 750 751 752
  /* for lack of a better place to put this stub call */
  init_expr_processing();

  constants_type_node = make_node (RECORD_TYPE);
  PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
  PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
  PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
753
  constants_data_field_decl_node = field;
Anthony Green committed
754
  FINISH_RECORD (constants_type_node);
755
  build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
Anthony Green committed
756 757 758 759 760 761

  access_flags_type_node = unsigned_short_type_node;

  dtable_type = make_node (RECORD_TYPE);
  dtable_ptr_type = build_pointer_type (dtable_type);

762 763
  otable_type = build_array_type (integer_type_node, 
				  one_elt_array_domain_type);
764
  TYPE_NONALIASED_COMPONENT (otable_type) = 1;
765
  otable_ptr_type = build_pointer_type (otable_type);
766

767
  PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
768 769 770
  DECL_FCONTEXT (field) = object_type_node;
  TYPE_VFIELD (object_type_node) = field;

771 772 773
  /* This isn't exactly true, but it is what we have in the source.
     There is an unresolved issue here, which is whether the vtable
     should be marked by the GC.  */
774 775 776
  if (! flag_hash_synchronization)
    PUSH_FIELD (object_type_node, field, "sync_info",
		build_pointer_type (object_type_node));
Anthony Green committed
777 778 779 780 781 782 783 784 785 786 787 788 789
  for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
    FIELD_PRIVATE (t) = 1;
  FINISH_RECORD (object_type_node);

  field_type_node = make_node (RECORD_TYPE);
  field_ptr_type_node = build_pointer_type (field_type_node);
  method_type_node = make_node (RECORD_TYPE);
  method_ptr_type_node = build_pointer_type (method_type_node);

  set_super_info (0, class_type_node, object_type_node, 0);
  set_super_info (0, string_type_node, object_type_node, 0);
  class_ptr_type = build_pointer_type (class_type_node);

790
  PUSH_FIELD (class_type_node, field, "next_or_version", class_ptr_type);
Anthony Green committed
791 792 793 794
  PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
  PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
  PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
  PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
795
  constants_field_decl_node = field;
Anthony Green committed
796
  PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
797
  PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
798
  PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
Anthony Green committed
799
  PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
800 801 802
  PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
  PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
  PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
803
  PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
804 805
  PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
  PUSH_FIELD (class_type_node, field, "otable_syms", 
806 807 808 809
  	      symbols_array_ptr_type);
  PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
  PUSH_FIELD (class_type_node, field, "atable_syms", 
  	      symbols_array_ptr_type);
810 811 812
  PUSH_FIELD (class_type_node, field, "itable", itable_ptr_type);
  PUSH_FIELD (class_type_node, field, "itable_syms", 
  	      symbols_array_ptr_type);
813
  PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
Anthony Green committed
814 815 816
  PUSH_FIELD (class_type_node, field, "interfaces",
	      build_pointer_type (class_ptr_type));
  PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
817
  PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
Anthony Green committed
818
  PUSH_FIELD (class_type_node, field, "state", byte_type_node);
8  
Per Bothner committed
819
  PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
820 821 822
  PUSH_FIELD (class_type_node, field, "depth", short_type_node);
  PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
  PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);  
Bryce McKinlay committed
823
  PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);  
824
  PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
825
  PUSH_FIELD (class_type_node, field, "assertion_table", ptr_type_node);
826
  PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
827
  PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
828
  PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
829
  PUSH_FIELD (class_type_node, field, "engine", ptr_type_node);
830
  PUSH_FIELD (class_type_node, field, "reflection_data", ptr_type_node);
Anthony Green committed
831 832 833
  for (t = TYPE_FIELDS (class_type_node);  t != NULL_TREE;  t = TREE_CHAIN (t))
    FIELD_PRIVATE (t) = 1;
  push_super_field (class_type_node, object_type_node);
834

Anthony Green committed
835
  FINISH_RECORD (class_type_node);
836
  build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
Anthony Green committed
837 838 839 840 841 842 843 844 845 846 847 848

  field_info_union_node = make_node (UNION_TYPE);
  PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
  PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
  layout_type (field_info_union_node);

  PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
  PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
  PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
  PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
  PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
  FINISH_RECORD (field_type_node);
849
  build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
Anthony Green committed
850 851 852 853 854 855 856

  nativecode_ptr_array_type_node
    = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);

  PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
  PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
  FINISH_RECORD (dtable_type);
857
  build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
Anthony Green committed
858 859 860 861 862 863 864

  jexception_type = make_node (RECORD_TYPE);
  PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
  PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
  PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
  PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
  FINISH_RECORD (jexception_type);
865
  build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
Anthony Green committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879
  jexception_ptr_type = build_pointer_type (jexception_type);

  lineNumberEntry_type = make_node (RECORD_TYPE);
  PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
  PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
  FINISH_RECORD (lineNumberEntry_type);

  lineNumbers_type = make_node (RECORD_TYPE);
  PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
  FINISH_RECORD (lineNumbers_type);

  PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
  PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
  PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
880
  PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
Anthony Green committed
881
  PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
882
  PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
Anthony Green committed
883
  FINISH_RECORD (method_type_node);
884
  build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
Anthony Green committed
885

886 887
  endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);

888
  t = tree_cons (NULL_TREE, class_ptr_type, endlink);
889 890 891
  alloc_object_node = add_builtin_function ("_Jv_AllocObject",
					    build_function_type (ptr_type_node, t),
					    0, NOT_BUILT_IN, NULL, NULL_TREE);
892
  DECL_IS_MALLOC (alloc_object_node) = 1;
893 894 895 896
  alloc_no_finalizer_node =
    add_builtin_function ("_Jv_AllocObjectNoFinalizer",
			  build_function_type (ptr_type_node, t),
			  0, NOT_BUILT_IN, NULL, NULL_TREE);
897
  DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
898 899

  t = tree_cons (NULL_TREE, ptr_type_node, endlink);
900 901 902 903
  soft_initclass_node = add_builtin_function ("_Jv_InitClass",
					      build_function_type (void_type_node,
								   t),
					      0, NOT_BUILT_IN, NULL, NULL_TREE);
904 905
  t = tree_cons (NULL_TREE, class_ptr_type,
		 tree_cons (NULL_TREE, int_type_node, endlink));
906 907 908 909
  soft_resolvepoolentry_node
    = add_builtin_function ("_Jv_ResolvePoolEntry",
			    build_function_type (ptr_type_node, t),
			    0,NOT_BUILT_IN, NULL, NULL_TREE);
910
  DECL_IS_PURE (soft_resolvepoolentry_node) = 1;
911 912 913
  throw_node = add_builtin_function ("_Jv_Throw",
				     build_function_type (void_type_node, t),
				     0, NOT_BUILT_IN, NULL, NULL_TREE);
914
  /* Mark throw_nodes as `noreturn' functions with side effects.  */
915 916
  TREE_THIS_VOLATILE (throw_node) = 1;
  TREE_SIDE_EFFECTS (throw_node) = 1;
917

918 919
  t = build_function_type (void_type_node, tree_cons (NULL_TREE, ptr_type_node,
						      endlink));
920 921 922 923 924 925 926 927
  soft_monitorenter_node
    = add_builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
			    NULL, NULL_TREE);
  soft_monitorexit_node
    = add_builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
			    NULL, NULL_TREE);

  t = tree_cons (NULL_TREE, ptr_type_node,
928
		 tree_cons (NULL_TREE, int_type_node, endlink));
Anthony Green committed
929
  soft_newarray_node
930 931 932
      = add_builtin_function ("_Jv_NewPrimArray",
			      build_function_type (ptr_type_node, t),
			      0, NOT_BUILT_IN, NULL, NULL_TREE);
933
  DECL_IS_MALLOC (soft_newarray_node) = 1;
Anthony Green committed
934 935 936

  t = tree_cons (NULL_TREE, int_type_node,
		 tree_cons (NULL_TREE, class_ptr_type,
937 938
			    tree_cons (NULL_TREE, object_ptr_type_node,
				       endlink)));
Anthony Green committed
939
  soft_anewarray_node
940 941 942
      = add_builtin_function ("_Jv_NewObjectArray",
			      build_function_type (ptr_type_node, t),
			      0, NOT_BUILT_IN, NULL, NULL_TREE);
943
  DECL_IS_MALLOC (soft_anewarray_node) = 1;
Anthony Green committed
944

945 946
  /* There is no endlink here because _Jv_NewMultiArray is a varargs
     function.  */
947
  t = tree_cons (NULL_TREE, ptr_type_node,
948
		 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
Anthony Green committed
949
  soft_multianewarray_node
950 951 952
      = add_builtin_function ("_Jv_NewMultiArray",
			      build_function_type (ptr_type_node, t),
			      0, NOT_BUILT_IN, NULL, NULL_TREE);
953
  DECL_IS_MALLOC (soft_multianewarray_node) = 1;
Anthony Green committed
954

8  
Per Bothner committed
955
  t = build_function_type (void_type_node, 
956
			   tree_cons (NULL_TREE, int_type_node, endlink));
Anthony Green committed
957
  soft_badarrayindex_node
958 959
      = add_builtin_function ("_Jv_ThrowBadArrayIndex", t,
			      0, NOT_BUILT_IN, NULL, NULL_TREE);
960 961
  /* Mark soft_badarrayindex_node as a `noreturn' function with side
     effects.  */
Anthony Green committed
962 963 964
  TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
  TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;

965
  soft_nullpointer_node
966 967 968
    = add_builtin_function ("_Jv_ThrowNullPointerException",
			    build_function_type (void_type_node, endlink),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
969 970 971 972 973
  /* Mark soft_nullpointer_node as a `noreturn' function with side
     effects.  */
  TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
  TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;

974
  soft_abstractmethod_node
975 976 977
    = add_builtin_function ("_Jv_ThrowAbstractMethodError",
			    build_function_type (void_type_node, endlink),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
978 979 980 981 982
  /* Mark soft_abstractmethod_node as a `noreturn' function with side
     effects.  */
  TREE_THIS_VOLATILE (soft_abstractmethod_node) = 1;
  TREE_SIDE_EFFECTS (soft_abstractmethod_node) = 1;

983
  soft_nosuchfield_node
984 985 986
    = add_builtin_function ("_Jv_ThrowNoSuchFieldError",
			    build_function_type (void_type_node, endlink),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
987 988 989 990 991
  /* Mark soft_nosuchfield_node as a `noreturn' function with side
     effects.  */
  TREE_THIS_VOLATILE (soft_nosuchfield_node) = 1;
  TREE_SIDE_EFFECTS (soft_nosuchfield_node) = 1;

Anthony Green committed
992
  t = tree_cons (NULL_TREE, class_ptr_type,
993
		 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
Anthony Green committed
994
  soft_checkcast_node
995 996 997
    = add_builtin_function ("_Jv_CheckCast",
			    build_function_type (ptr_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
Anthony Green committed
998
  t = tree_cons (NULL_TREE, object_ptr_type_node,
999
		 tree_cons (NULL_TREE, class_ptr_type, endlink));
Anthony Green committed
1000
  soft_instanceof_node
1001 1002 1003
    = add_builtin_function ("_Jv_IsInstanceOf",
			    build_function_type (boolean_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1004
  DECL_IS_PURE (soft_instanceof_node) = 1;
Anthony Green committed
1005
  t = tree_cons (NULL_TREE, object_ptr_type_node,
1006
		 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
Anthony Green committed
1007
  soft_checkarraystore_node
1008 1009 1010
    = add_builtin_function ("_Jv_CheckArrayStore",
			    build_function_type (void_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
Anthony Green committed
1011 1012
  t = tree_cons (NULL_TREE, ptr_type_node,
		 tree_cons (NULL_TREE, ptr_type_node,
1013
			    tree_cons (NULL_TREE, int_type_node, endlink)));
1014 1015 1016 1017
  soft_lookupinterfacemethod_node
    = add_builtin_function ("_Jv_LookupInterfaceMethodIdx",
			    build_function_type (ptr_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1018
  DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1019 1020 1021
  t = tree_cons (NULL_TREE, ptr_type_node,
		 tree_cons (NULL_TREE, ptr_type_node,
			    tree_cons (NULL_TREE, ptr_type_node, endlink)));
1022 1023 1024 1025
  soft_lookupinterfacemethodbyname_node
    = add_builtin_function ("_Jv_LookupInterfaceMethod",
			    build_function_type (ptr_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1026 1027
  t = tree_cons (NULL_TREE, object_ptr_type_node,
		 tree_cons (NULL_TREE, ptr_type_node,
1028 1029 1030
			    tree_cons (NULL_TREE, ptr_type_node, 
			               tree_cons (NULL_TREE, int_type_node, 
				                  endlink))));
1031
  soft_lookupjnimethod_node
1032 1033 1034
    = add_builtin_function ("_Jv_LookupJNIMethod",
			    build_function_type (ptr_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1035 1036
  t = tree_cons (NULL_TREE, ptr_type_node, endlink);
  soft_getjnienvnewframe_node
1037 1038 1039
    = add_builtin_function ("_Jv_GetJNIEnvNewFrame",
			    build_function_type (ptr_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1040
  soft_jnipopsystemframe_node
1041 1042 1043
    = add_builtin_function ("_Jv_JNI_PopSystemFrame",
			    build_function_type (void_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1044 1045 1046

  t = tree_cons (NULL_TREE, object_ptr_type_node, endlink);
  soft_unwrapjni_node
1047 1048 1049
    = add_builtin_function ("_Jv_UnwrapJNIweakReference",
			    build_function_type (object_ptr_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1050

1051 1052
  t = tree_cons (NULL_TREE, int_type_node,
		 tree_cons (NULL_TREE, int_type_node, endlink));
1053
  soft_idiv_node
1054 1055 1056
    = add_builtin_function ("_Jv_divI",
			    build_function_type (int_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1057 1058

  soft_irem_node
1059 1060 1061
    = add_builtin_function ("_Jv_remI",
			    build_function_type (int_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1062

1063 1064
  t = tree_cons (NULL_TREE, long_type_node,
		 tree_cons (NULL_TREE, long_type_node, endlink));
1065
  soft_ldiv_node
1066 1067 1068
    = add_builtin_function ("_Jv_divJ",
			    build_function_type (long_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1069 1070

  soft_lrem_node
1071 1072 1073
    = add_builtin_function ("_Jv_remJ",
			    build_function_type (long_type_node, t),
			    0, NOT_BUILT_IN, NULL, NULL_TREE);
1074

1075 1076 1077 1078
  /* Initialize variables for except.c.  */
  eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
                                             ? "__gcj_personality_sj0"
                                             : "__gcj_personality_v0");
1079
  default_init_unwind_resume_libfunc ();
1080 1081

  lang_eh_runtime_type = do_nothing;
1082

1083
  initialize_builtins ();
1084
  soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1085 1086

  parse_version ();
Anthony Green committed
1087 1088 1089 1090 1091 1092 1093 1094 1095
}


/* Look up NAME in the current binding level and its superiors
   in the namespace of variables, functions and typedefs.
   Return a ..._DECL node of some kind representing its definition,
   or return 0 if it is undefined.  */

tree
1096
lookup_name (tree name)
Anthony Green committed
1097
{
1098
  tree val;
Anthony Green committed
1099 1100 1101 1102 1103 1104 1105 1106 1107
  if (current_binding_level != global_binding_level
      && IDENTIFIER_LOCAL_VALUE (name))
    val = IDENTIFIER_LOCAL_VALUE (name);
  else
    val = IDENTIFIER_GLOBAL_VALUE (name);
  return val;
}

/* Similar to `lookup_name' but look only at current binding level and
1108
   the previous one if it's the parameter level.  */
Anthony Green committed
1109

1110
static tree
1111
lookup_name_current_level (tree name)
Anthony Green committed
1112
{
1113
  tree t;
Anthony Green committed
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

  if (current_binding_level == global_binding_level)
    return IDENTIFIER_GLOBAL_VALUE (name);

  if (IDENTIFIER_LOCAL_VALUE (name) == 0)
    return 0;

  for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
    if (DECL_NAME (t) == name)
      break;

  return t;
}

/* Record a decl-node X as belonging to the current lexical scope.
   Check for errors (such as an incompatible declaration for the same
   name already seen in the same scope).

   Returns either X or an old decl for the same name.
   If an old decl is returned, it may have been smashed
   to agree with what X says.  */

tree
1137
pushdecl (tree x)
Anthony Green committed
1138
{
1139 1140 1141
  tree t;
  tree name = DECL_NAME (x);
  struct binding_level *b = current_binding_level;
1142 1143 1144
  
  if (TREE_CODE (x) != TYPE_DECL)
    DECL_CONTEXT (x) = current_function_decl;
Anthony Green committed
1145 1146 1147 1148 1149 1150 1151
  if (name)
    {
      t = lookup_name_current_level (name);
      if (t != 0 && t == error_mark_node)
	/* error_mark_node is 0 for a while during initialization!  */
	{
	  t = 0;
1152
	  error ("%q+D used prior to declaration", x);
Anthony Green committed
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	}

      /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
	 to point to the TYPE_DECL.
	 Since Java does not have typedefs, a type can only have
	 one (true) name, given by a class, interface, or builtin. */
      if (TREE_CODE (x) == TYPE_DECL
	  && TYPE_NAME (TREE_TYPE (x)) == 0
	  && TREE_TYPE (x) != error_mark_node)
	{
	  TYPE_NAME (TREE_TYPE (x)) = x;
	  TYPE_STUB_DECL (TREE_TYPE (x)) = x;
	}

      /* This name is new in its binding level.
	 Install the new declaration and return it.  */
      if (b == global_binding_level)
	{
	  /* Install a global value.  */
	  
	  IDENTIFIER_GLOBAL_VALUE (name) = x;
	}
      else
	{
	  /* Here to install a non-global value.  */
	  tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
	  IDENTIFIER_LOCAL_VALUE (name) = x;

	  /* If storing a local value, there may already be one (inherited).
	     If so, record it for restoration when this binding level ends.  */
	  if (oldlocal != 0)
	    b->shadowed = tree_cons (name, oldlocal, b->shadowed);
	}
    }

  /* Put decls on list in reverse order.
     We will reverse them later if necessary.  */
  TREE_CHAIN (x) = b->names;
  b->names = x;

  return x;
}
1195

Anthony Green committed
1196
void
1197
pushdecl_force_head (tree x)
Anthony Green committed
1198 1199 1200 1201 1202 1203 1204
{
  current_binding_level->names = x;
}

/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate.  */

tree
1205
pushdecl_top_level (tree x)
Anthony Green committed
1206
{
1207 1208
  tree t;
  struct binding_level *b = current_binding_level;
Anthony Green committed
1209 1210 1211 1212 1213 1214 1215

  current_binding_level = global_binding_level;
  t = pushdecl (x);
  current_binding_level = b;
  return t;
}

1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
/* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate.  */

tree
pushdecl_function_level (tree x)
{
  tree t;
  struct binding_level *b = current_binding_level;

  current_binding_level = function_binding_level;
  t = pushdecl (x);
  current_binding_level = b;
  return t;
}

Anthony Green committed
1230 1231 1232
/* Nonzero if we are currently in the global binding level.  */

int
1233
global_bindings_p (void)
Anthony Green committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
{
  return current_binding_level == global_binding_level;
}

/* Return the list of declarations of the current level.
   Note that this list is in reverse order unless/until
   you nreverse it; and when you do nreverse it, you must
   store the result back using `storedecls' or you will lose.  */

tree
1244
getdecls (void)
Anthony Green committed
1245 1246 1247 1248 1249 1250
{
  return current_binding_level->names;
}

/* Create a new `struct binding_level'.  */

1251
static struct binding_level *
1252
make_binding_level (void)
Anthony Green committed
1253 1254
{
  /* NOSTRICT */
1255
  return ggc_alloc_cleared (sizeof (struct binding_level));
Anthony Green committed
1256 1257 1258
}

void
1259
pushlevel (int unused ATTRIBUTE_UNUSED)
Anthony Green committed
1260
{
1261
  struct binding_level *newlevel = NULL_BINDING_LEVEL;
Anthony Green committed
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280

  /* Reuse or create a struct for this binding level.  */

  if (free_binding_level)
    {
      newlevel = free_binding_level;
      free_binding_level = free_binding_level->level_chain;
    }
  else
    {
      newlevel = make_binding_level ();
    }

  /* Add this level to the front of the chain (stack) of levels that
     are active.  */

  *newlevel = clear_binding_level;
  newlevel->level_chain = current_binding_level;
  current_binding_level = newlevel;
1281 1282 1283
#if defined(DEBUG_JAVA_BINDING_LEVELS)
  newlevel->binding_depth = binding_depth;
  indent ();
1284
  fprintf (stderr, "push %s level %p pc %d\n",
1285 1286 1287 1288
	   (is_class_level) ? "class" : "block", newlevel, current_pc);
  is_class_level = 0;
  binding_depth++;
#endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
Anthony Green committed
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
}

/* Exit a binding level.
   Pop the level off, and restore the state of the identifier-decl mappings
   that were in effect when this level was entered.

   If KEEP is nonzero, this level had explicit declarations, so
   and create a "block" (a BLOCK node) for the level
   to record its declarations and subblocks for symbol table output.

   If FUNCTIONBODY is nonzero, this level is the body of a function,
   so create a block as if KEEP were set and also clear out all
   label names.

   If REVERSE is nonzero, reverse the order of decls before putting
   them into the BLOCK.  */

tree
1307
poplevel (int keep, int reverse, int functionbody)
Anthony Green committed
1308
{
1309
  tree link;
Anthony Green committed
1310 1311 1312 1313 1314 1315
  /* The chain of decls was accumulated in reverse order.
     Put it into forward order, just for cleanliness.  */
  tree decls;
  tree subblocks = current_binding_level->blocks;
  tree block = 0;
  tree decl;
1316
  tree bind = 0;
Anthony Green committed
1317

1318 1319 1320 1321
#if defined(DEBUG_JAVA_BINDING_LEVELS)
  binding_depth--;
  indent ();
  if (current_binding_level->end_pc != LARGEST_PC)
1322
    fprintf (stderr, "pop  %s level %p pc %d (end pc %d)\n",
1323 1324 1325
	     (is_class_level) ? "class" : "block", current_binding_level, current_pc,
	     current_binding_level->end_pc);
  else
1326
    fprintf (stderr, "pop  %s level %p pc %d\n",
1327 1328 1329
	     (is_class_level) ? "class" : "block", current_binding_level, current_pc);
#endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */

Anthony Green committed
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
  /* Get the decls in the order they were written.
     Usually current_binding_level->names is in reverse order.
     But parameter decls were previously put in forward order.  */

  if (reverse)
    current_binding_level->names
      = decls = nreverse (current_binding_level->names);
  else
    decls = current_binding_level->names;

  for (decl = decls; decl; decl = TREE_CHAIN (decl))
1341 1342 1343 1344
    if (TREE_CODE (decl) == VAR_DECL
	&& DECL_LANG_SPECIFIC (decl) != NULL
	&& DECL_LOCAL_SLOT_NUMBER (decl))
      LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
Anthony Green committed
1345 1346 1347 1348 1349 1350

  /* If there were any declarations in that level,
     or if this level is a function body,
     create a BLOCK to record them for the life of this function.  */

  block = 0;
1351
  if (keep || functionbody)
1352 1353 1354 1355 1356
    {
      block = make_node (BLOCK);
      TREE_TYPE (block) = void_type_node;
    }

1357 1358 1359
  if (current_binding_level->exception_range)
    expand_end_java_handler (current_binding_level->exception_range);

Anthony Green committed
1360 1361
  if (block != 0)
    {
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
      /* If any statements have been generated at this level, create a
	 BIND_EXPR to hold them and copy the variables to it.  This
	 only applies to the bytecode compiler.  */
      if (current_binding_level->stmts)
	{
	  tree decl = decls;
	  tree *var = &BLOCK_VARS (block);

	  /* Copy decls from names list, ignoring labels.  */
	  while (decl)
	    {
	      tree next = TREE_CHAIN (decl);
	      if (TREE_CODE (decl) != LABEL_DECL)
		{
		  *var = decl;
		  var = &TREE_CHAIN (decl);
		}
	      decl = next;
	    }
	  *var = NULL;
	    
1383
	  bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block), 
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
			 BLOCK_EXPR_BODY (block), block);
	  BIND_EXPR_BODY (bind) = current_binding_level->stmts;
	  
	  if (BIND_EXPR_BODY (bind)
	      && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
	    TREE_SIDE_EFFECTS (bind) = 1;
	  
	  /* FIXME: gimplifier brain damage.  */
	  if (BIND_EXPR_BODY (bind) == NULL)
	    BIND_EXPR_BODY (bind) = build_java_empty_stmt ();

	  current_binding_level->stmts = NULL;
	}
      else
	{
	  BLOCK_VARS (block) = decls;
	}
Anthony Green committed
1401
      BLOCK_SUBBLOCKS (block) = subblocks;
1402
    }	
Anthony Green committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451

  /* In each subblock, record that this is its superior.  */

  for (link = subblocks; link; link = TREE_CHAIN (link))
    BLOCK_SUPERCONTEXT (link) = block;

  /* Clear out the meanings of the local variables of this level.  */

  for (link = decls; link; link = TREE_CHAIN (link))
    {
      tree name = DECL_NAME (link);
      if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
	{
	  /* If the ident. was used or addressed via a local extern decl,
	     don't forget that fact.  */
	  if (DECL_EXTERNAL (link))
	    {
	      if (TREE_USED (link))
		TREE_USED (name) = 1;
	      if (TREE_ADDRESSABLE (link))
		TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
	    }
	  IDENTIFIER_LOCAL_VALUE (name) = 0;
	}
    }

  /* Restore all name-meanings of the outer levels
     that were shadowed by this level.  */

  for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
    IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);

  /* If the level being exited is the top level of a function,
     check over all the labels, and clear out the current
     (function local) meanings of their names.  */

  if (functionbody)
    {
      /* If this is the top level block of a function,
	 the vars are the function's parameters.
	 Don't leave them in the BLOCK because they are
	 found in the FUNCTION_DECL instead.  */

      BLOCK_VARS (block) = 0;
    }

  /* Pop the current level, and free the structure for reuse.  */

  {
1452
    struct binding_level *level = current_binding_level;
Anthony Green committed
1453 1454 1455 1456 1457 1458 1459 1460
    current_binding_level = current_binding_level->level_chain;

    level->level_chain = free_binding_level;
    free_binding_level = level;
  }

  /* Dispose of the block that we just made inside some higher level.  */
  if (functionbody)
1461 1462 1463 1464
    {
      DECL_INITIAL (current_function_decl) = block;
      DECL_SAVED_TREE (current_function_decl) = bind;
    }
1465
  else 
Anthony Green committed
1466
    {
1467 1468
      if (block)
	{
1469 1470
	  current_binding_level->blocks
	    = chainon (current_binding_level->blocks, block);
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	}
      /* If we did not make a block for the level just exited,
	 any blocks made for inner levels
	 (since they cannot be recorded as subblocks in that level)
	 must be carried forward so they will later become subblocks
	 of something else.  */
      else if (subblocks)
	current_binding_level->blocks
	  = chainon (current_binding_level->blocks, subblocks);

      if (bind)
	java_add_stmt (bind);
Anthony Green committed
1483
    }
1484

Anthony Green committed
1485 1486 1487 1488 1489 1490
  if (block)
    TREE_USED (block) = 1;
  return block;
}

void
1491
maybe_pushlevels (int pc)
Anthony Green committed
1492
{
1493 1494 1495 1496
#if defined(DEBUG_JAVA_BINDING_LEVELS)
  current_pc = pc;
#endif

Anthony Green committed
1497 1498 1499 1500
  while (pending_local_decls != NULL_TREE &&
	 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
    {
      tree *ptr = &pending_local_decls;
1501
      tree decl = *ptr, next;
Anthony Green committed
1502 1503 1504 1505 1506 1507 1508 1509 1510
      int end_pc = DECL_LOCAL_END_PC (decl);

      while (*ptr != NULL_TREE
	     && DECL_LOCAL_START_PC (*ptr) <= pc
	     && DECL_LOCAL_END_PC (*ptr) == end_pc)
	ptr = &TREE_CHAIN (*ptr);
      pending_local_decls = *ptr;
      *ptr = NULL_TREE;

1511 1512
      /* Force non-nested range to be nested in current range by
	 truncating variable lifetimes. */
Anthony Green committed
1513
      if (end_pc > current_binding_level->end_pc)
1514
	{
1515
	  tree t;
1516
	  end_pc = current_binding_level->end_pc;
1517 1518
	  for (t = decl; t != NULL_TREE; t = TREE_CHAIN (t))
	    DECL_LOCAL_END_PC (t) = end_pc;
1519
	}
Anthony Green committed
1520

1521 1522
      maybe_start_try (pc, end_pc);
      
Anthony Green committed
1523
      pushlevel (1);
1524

Anthony Green committed
1525
      current_binding_level->end_pc = end_pc;
1526
      current_binding_level->start_pc = pc;      
1527 1528
      current_binding_level->names = NULL;
      for ( ; decl != NULL_TREE; decl = next)
Anthony Green committed
1529
	{
1530 1531
	  int index = DECL_LOCAL_SLOT_NUMBER (decl);
	  tree base_decl;
1532
	  next = TREE_CHAIN (decl);
1533
	  push_jvm_slot (index, decl);
1534
	  pushdecl (decl);
1535 1536 1537 1538 1539 1540
	  base_decl
	    = find_local_variable (index, TREE_TYPE (decl), pc);
	  if (TREE_CODE (TREE_TYPE (base_decl)) == POINTER_TYPE)
	    base_decl = TREE_VEC_ELT (base_decl_map, index);
	  SET_DECL_VALUE_EXPR (decl, base_decl);
	  DECL_HAS_VALUE_EXPR_P (decl) = 1;
Anthony Green committed
1541
	}
1542 1543 1544
    }      

  maybe_start_try (pc, 0);
Anthony Green committed
1545 1546 1547
}

void
1548
maybe_poplevels (int pc)
Anthony Green committed
1549
{
1550 1551 1552 1553
#if defined(DEBUG_JAVA_BINDING_LEVELS)
  current_pc = pc;
#endif

1554 1555 1556 1557 1558 1559
  /* FIXME: I'm pretty sure that this is wrong.  Variable scopes are
     inclusive, so a variable is live if pc == end_pc.  Here, we
     terminate a range if the current pc is equal to the end of the
     range, and this is *before* we have generated code for the
     instruction at end_pc.  We're closing a binding level one
     instruction too early.*/
Anthony Green committed
1560
  while (current_binding_level->end_pc <= pc)
1561
    poplevel (1, 0, 0);
1562 1563 1564 1565 1566 1567 1568 1569
}

/* Terminate any binding which began during the range beginning at
   start_pc.  This tidies up improperly nested local variable ranges
   and exception handlers; a variable declared within an exception
   range is forcibly terminated when that exception ends. */

void
1570
force_poplevels (int start_pc)
1571 1572 1573 1574
{
  while (current_binding_level->start_pc > start_pc)
    {
      if (pedantic && current_binding_level->start_pc > start_pc)
1575 1576
	warning (0, "In %+D: overlapped variable and exception ranges at %d",
                 current_function_decl,
1577
		 current_binding_level->start_pc);
Anthony Green committed
1578 1579 1580 1581 1582 1583 1584 1585 1586
      poplevel (1, 0, 0);
    }
}

/* Insert BLOCK at the end of the list of subblocks of the
   current binding level.  This is used when a BIND_EXPR is expanded,
   to handle the BLOCK node inside the BIND_EXPR.  */

void
1587
insert_block (tree block)
Anthony Green committed
1588 1589 1590 1591 1592 1593 1594 1595 1596
{
  TREE_USED (block) = 1;
  current_binding_level->blocks
    = chainon (current_binding_level->blocks, block);
}

/* integrate_decl_tree calls this function. */

void
1597
java_dup_lang_specific_decl (tree node)
Anthony Green committed
1598
{
1599 1600 1601 1602 1603 1604
  int lang_decl_size;
  struct lang_decl *x;

  if (!DECL_LANG_SPECIFIC (node))
    return;

1605
  lang_decl_size = sizeof (struct lang_decl);
1606
  x = ggc_alloc (lang_decl_size);
1607
  memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
Anthony Green committed
1608 1609 1610 1611
  DECL_LANG_SPECIFIC (node) = x;
}

void
1612
give_name_to_locals (JCF *jcf)
Anthony Green committed
1613 1614
{
  int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1615
  int code_offset = DECL_CODE_OFFSET (current_function_decl);
Anthony Green committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
  tree parm;
  pending_local_decls = NULL_TREE;
  if (n == 0)
    return;
  JCF_SEEK (jcf, n);
  n = JCF_readu2 (jcf);
  for (i = 0; i < n; i++)
    {
      int start_pc = JCF_readu2 (jcf);
      int length = JCF_readu2 (jcf);
      int name_index = JCF_readu2 (jcf);
      int signature_index = JCF_readu2 (jcf);
      int slot = JCF_readu2 (jcf);
      tree name = get_name_constant (jcf, name_index);
8  
Per Bothner committed
1630
      tree type = parse_signature (jcf, signature_index);
Anthony Green committed
1631 1632 1633 1634 1635 1636 1637
      if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
	  && start_pc == 0
	  && length == DECL_CODE_LENGTH (current_function_decl))
	{
	  tree decl = TREE_VEC_ELT (decl_map, slot);
	  DECL_NAME (decl) = name;
	  if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1638
	    warning (0, "bad type in parameter debug info");
Anthony Green committed
1639 1640 1641 1642 1643 1644 1645 1646
	}
      else
	{
	  tree *ptr;
	  int end_pc = start_pc + length;
	  tree decl = build_decl (VAR_DECL, name, type);
	  if (end_pc > DECL_CODE_LENGTH (current_function_decl))
	    {
1647 1648
	      warning (0, "bad PC range for debug info for local %q+D",
                       decl);
Anthony Green committed
1649 1650
	      end_pc = DECL_CODE_LENGTH (current_function_decl);
	    }
1651 1652 1653 1654 1655 1656 1657

	  /* Adjust start_pc if necessary so that the local's first
	     store operation will use the relevant DECL as a
	     destination. Fore more information, read the leading
	     comments for expr.c:maybe_adjust_start_pc. */
	  start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);

1658
	  MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
Anthony Green committed
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698
	  DECL_LOCAL_SLOT_NUMBER (decl) = slot;
	  DECL_LOCAL_START_PC (decl) = start_pc;
	  DECL_LOCAL_END_PC (decl) = end_pc;

	  /* Now insert the new decl in the proper place in
	     pending_local_decls.  We are essentially doing an insertion sort,
	     which works fine, since the list input will normally already
	     be sorted. */
	  ptr = &pending_local_decls;
	  while (*ptr != NULL_TREE
		 && (DECL_LOCAL_START_PC (*ptr) > start_pc
		     || (DECL_LOCAL_START_PC (*ptr) == start_pc
			 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
	    ptr = &TREE_CHAIN (*ptr);
	  TREE_CHAIN (decl) = *ptr;
	  *ptr = decl;
	}
    }

  pending_local_decls = nreverse (pending_local_decls);

  /* Fill in default names for the parameters. */ 
  for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
       parm != NULL_TREE;  parm = TREE_CHAIN (parm), i++)
    {
      if (DECL_NAME (parm) == NULL_TREE)
	{
	  int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
	  if (arg_i == 0)
	    DECL_NAME (parm) = get_identifier ("this");
	  else
	    {
	      char buffer[12];
	      sprintf (buffer, "ARG_%d", arg_i);
	      DECL_NAME (parm) = get_identifier (buffer);
	    }
	}
    }
}

1699
tree
1700
build_result_decl (tree fndecl)
Anthony Green committed
1701
{
8  
Per Bothner committed
1702
  tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1703 1704 1705 1706 1707 1708 1709 1710
  tree result = DECL_RESULT (fndecl);
  if (! result)
    {
      /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
      if (INTEGRAL_TYPE_P (restype)
	  && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
	restype = integer_type_node;
      result = build_decl (RESULT_DECL, NULL_TREE, restype);
1711 1712
      DECL_ARTIFICIAL (result) = 1;
      DECL_IGNORED_P (result) = 1;
1713 1714 1715 1716
      DECL_CONTEXT (result) = fndecl;
      DECL_RESULT (fndecl) = result;
    }
  return result;
1717
}
Anthony Green committed
1718

1719
void
1720
start_java_method (tree fndecl)
Anthony Green committed
1721 1722 1723 1724
{
  tree tem, *ptr;
  int i;

1725 1726
  uniq = 0;

Anthony Green committed
1727 1728 1729 1730 1731
  current_function_decl = fndecl;
  announce_function (fndecl);

  i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
  decl_map = make_tree_vec (i);
1732
  base_decl_map = make_tree_vec (i);
1733
  type_map = xrealloc (type_map, i * sizeof (tree));
Anthony Green committed
1734

1735
#if defined(DEBUG_JAVA_BINDING_LEVELS)
Neil Booth committed
1736
  fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1737 1738
  current_pc = 0;
#endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
Anthony Green committed
1739 1740 1741 1742
  pushlevel (1);  /* Push parameters. */

  ptr = &DECL_ARGUMENTS (fndecl);
  for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1743
       tem != end_params_node; tem = TREE_CHAIN (tem), i++)
Anthony Green committed
1744 1745
    {
      tree parm_name = NULL_TREE, parm_decl;
8  
Per Bothner committed
1746
      tree parm_type = TREE_VALUE (tem);
1747
      gcc_assert (i < DECL_MAX_LOCALS (fndecl));
Anthony Green committed
1748

8  
Per Bothner committed
1749
      parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
Anthony Green committed
1750
      DECL_CONTEXT (parm_decl) = fndecl;
1751
      if (targetm.calls.promote_prototypes (parm_type)
1752
	  && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
8  
Per Bothner committed
1753 1754 1755
	  && INTEGRAL_TYPE_P (parm_type))
	parm_type = integer_type_node;
      DECL_ARG_TYPE (parm_decl) = parm_type;
Anthony Green committed
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775

      *ptr = parm_decl;
      ptr = &TREE_CHAIN (parm_decl);

      /* Add parm_decl to the decl_map. */
      push_jvm_slot (i, parm_decl);

      type_map[i] = TREE_TYPE (parm_decl);
      if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
	{
	  i++;
	  type_map[i] = void_type_node;
	}
    }
  *ptr = NULL_TREE;
  DECL_ARG_SLOT_COUNT (current_function_decl) = i;

  while (i < DECL_MAX_LOCALS(fndecl))
    type_map[i++] = NULL_TREE;

1776
  build_result_decl (fndecl);
1777 1778 1779

  /* Push local variables.  */
  pushlevel (2);
1780 1781

  function_binding_level = current_binding_level;
Anthony Green committed
1782 1783 1784
}

void
1785
end_java_method (void)
Anthony Green committed
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
{
  tree fndecl = current_function_decl;

  /* pop out of function */
  poplevel (1, 1, 0);

  /* pop out of its parameters */
  poplevel (1, 0, 1);

  BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
  
  if (DECL_SAVED_TREE (fndecl))
    {
      tree fbody, block_body;
      /* Before we check initialization, attached all class initialization
	 variable to the block_body */
      fbody = DECL_SAVED_TREE (fndecl);
      block_body = BIND_EXPR_BODY (fbody);
      htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
		     attach_init_test_initialization_flags, block_body);
    }
Anthony Green committed
1807

1808
  finish_method (fndecl);
Anthony Green committed
1809

1810 1811 1812 1813 1814 1815 1816 1817
  if (! flag_unit_at_a_time)
    {
      /* Nulling these fields when we no longer need them saves
	 memory.  */
      DECL_SAVED_TREE (fndecl) = NULL;
      DECL_STRUCT_FUNCTION (fndecl) = NULL;
      DECL_INITIAL (fndecl) = NULL_TREE;
    }
Anthony Green committed
1818 1819
  current_function_decl = NULL_TREE;
}
1820

1821
/* Prepare a method for expansion.  */
1822 1823

void
1824
finish_method (tree fndecl)
1825
{
1826
  tree *tp = &DECL_SAVED_TREE (fndecl);
1827

1828 1829 1830
  /* Wrap body of synchronized methods in a monitorenter,
     plus monitorexit cleanup.  */
  if (METHOD_SYNCHRONIZED (fndecl))
1831
    {
1832 1833 1834 1835 1836 1837 1838
      tree enter, exit, lock;
      if (METHOD_STATIC (fndecl))
	lock = build_class_ref (DECL_CONTEXT (fndecl));
      else
	lock = DECL_ARGUMENTS (fndecl);
      BUILD_MONITOR_ENTER (enter, lock);
      BUILD_MONITOR_EXIT (exit, lock);
1839 1840
      *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
		    build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
1841 1842
    }

1843 1844
  /* Convert function tree to GENERIC prior to inlining.  */
  java_genericize (fndecl);
1845

1846 1847 1848 1849 1850 1851
  /* Store the end of the function, so that we get good line number
     info for the epilogue.  */
  if (DECL_STRUCT_FUNCTION (fndecl))
    cfun = DECL_STRUCT_FUNCTION (fndecl);
  else
    allocate_struct_function (fndecl);
1852 1853 1854
#ifdef USE_MAPPED_LOCATION
  cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
#else
1855 1856
  cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
  cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
1857
#endif
1858

1859 1860 1861 1862 1863 1864 1865 1866 1867
  /* Defer inlining and expansion to the cgraph optimizers.  */
  cgraph_finalize_function (fndecl, false);
}

/* Optimize and expand a function's entire body.  */

void
java_expand_body (tree fndecl)
{
1868
  tree_rest_of_compilation (fndecl);
1869 1870
}

1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
/* We pessimistically marked all methods and fields external until we
   knew what set of classes we were planning to compile.  Now mark those
   associated with CLASS to be generated locally as not external.  */

static void
java_mark_decl_local (tree decl)
{
  DECL_EXTERNAL (decl) = 0;

  /* If we've already constructed DECL_RTL, give encode_section_info
     a second chance, now that we've changed the flags.  */
1882 1883 1884 1885 1886
  /* ??? Ideally, we'd have flag_unit_at_a_time set, and not have done
     anything that would have referenced DECL_RTL so far.  But at the
     moment we force flag_unit_at_a_time off due to excessive memory
     consumption when compiling large jar files.  Which probably means
     that we need to re-order how we process jar files...  */
1887
  if (DECL_RTL_SET_P (decl))
1888
    make_decl_rtl (decl);
1889 1890
}

1891 1892 1893 1894 1895 1896 1897 1898
/* Given appropriate target support, G++ will emit hidden aliases for native
   methods.  Using this hidden name is required for proper operation of
   _Jv_Method::ncode, but it doesn't hurt to use it everywhere.  Look for
   proper target support, then mark the method for aliasing.  */

static void
java_mark_cni_decl_local (tree decl)
{
1899
  /* Setting DECL_LOCAL_CNI_METHOD_P changes the behavior of the mangler.
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
     We expect that we should not yet have referenced this decl in a 
     context that requires it.  Check this invariant even if we don't have
     support for hidden aliases.  */
  gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));

#if !defined(HAVE_GAS_HIDDEN) || !defined(ASM_OUTPUT_DEF)
  return;
#endif

  DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
  DECL_LOCAL_CNI_METHOD_P (decl) = 1;
}

1913
/* Use the preceding two functions and mark all members of the class.  */
1914

1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
void
java_mark_class_local (tree class)
{
  tree t;

  for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
    if (FIELD_STATIC (t))
      java_mark_decl_local (t);

  for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
1925 1926 1927 1928 1929 1930 1931
    if (!METHOD_ABSTRACT (t))
      {
	if (METHOD_NATIVE (t) && !flag_jni)
	  java_mark_cni_decl_local (t);
        else
	  java_mark_decl_local (t);
      }
1932 1933
}

1934 1935 1936
/* Add a statement to a compound_expr.  */

tree
Ranjit Mathew committed
1937
add_stmt_to_compound (tree existing, tree type, tree stmt)
1938 1939 1940 1941 1942
{
  if (!stmt)
    return existing;
  else if (existing)
    {
1943 1944 1945
      tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
      TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
				 | TREE_SIDE_EFFECTS (stmt);
1946 1947 1948 1949 1950 1951
      return expr;
    }
  else
    return stmt;
}

1952 1953 1954 1955
/* Add a statement to the statement_list currently being constructed.
   If the statement_list is null, we don't create a singleton list.
   This is necessary because poplevel() assumes that adding a
   statement to a null statement_list returns the statement.  */
1956 1957

tree
1958
java_add_stmt (tree new_stmt)
1959
{
1960 1961 1962
  tree stmts = current_binding_level->stmts;
  tree_stmt_iterator i;

1963
  if (input_filename)
1964
    SET_EXPR_LOCATION (new_stmt, input_location);
1965
  
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
  if (stmts == NULL)
    return current_binding_level->stmts = new_stmt;

  /* Force STMTS to be a statement_list.  */
  if (TREE_CODE (stmts) != STATEMENT_LIST)
    {
      tree t = make_node (STATEMENT_LIST);
      i = tsi_last (t);
      tsi_link_after (&i, stmts, TSI_CONTINUE_LINKING);
      stmts = t;
    }  
      
  i = tsi_last (stmts);
  tsi_link_after (&i, new_stmt, TSI_CONTINUE_LINKING);
1980
  TREE_TYPE (stmts) = void_type_node;
1981 1982

  return current_binding_level->stmts = stmts;
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
}

/* Add a variable to the current scope.  */

tree
java_add_local_var (tree decl)
{
  tree *vars = &current_binding_level->names;
  tree next = *vars;
  TREE_CHAIN (decl) = next;
  *vars = decl;
  DECL_CONTEXT (decl) = current_function_decl;
  MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
  return decl;
}

/* Return a pointer to the compound_expr currently being
   constructed.  */

tree *
get_stmts (void)
{
  return &current_binding_level->stmts;
}

2008 2009 2010 2011 2012 2013 2014 2015
/* Register an exception range as belonging to the current binding
   level.  There may only be one: if there are more, we'll create more
   binding levels.  However, each range can have multiple handlers,
   and these are expanded when we call expand_end_java_handler().  */

void
register_exception_range (struct eh_range *range, int pc, int end_pc)
{
2016
  gcc_assert (! current_binding_level->exception_range);
2017 2018 2019 2020
  current_binding_level->exception_range = range;
  current_binding_level->end_pc = end_pc;
  current_binding_level->start_pc = pc;      
}
2021

2022
#include "gt-java-decl.h"