lang.c 33.6 KB
Newer Older
Anthony Green committed
1
/* Java(TM) language-specific utility routines.
2
   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
Neil Booth committed
3
   Free Software Foundation, Inc.
Anthony Green committed
4

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

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

12
GCC is distributed in the hope that it will be useful,
Anthony Green committed
13 14 15 16 17
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
18
along with GCC; see the file COPYING.  If not, write to
Anthony Green committed
19 20 21 22 23 24 25 26 27 28
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. 

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 33
#include "tree.h"
#include "input.h"
Kaveh R. Ghazi committed
34 35
#include "rtl.h"
#include "expr.h"
Anthony Green committed
36 37
#include "java-tree.h"
#include "jcf.h"
38
#include "toplev.h"
39
#include "langhooks.h"
Neil Booth committed
40
#include "langhooks-def.h"
41
#include "flags.h"
42
#include "xref.h"
43
#include "ggc.h"
44
#include "diagnostic.h"
45
#include "tree-inline.h"
46
#include "splay-tree.h"
47
#include "tree-dump.h"
Neil Booth committed
48
#include "opts.h"
49
#include "options.h"
50

51
static bool java_init (void);
52
static void java_finish (void);
53
static unsigned int java_init_options (unsigned int, const char **);
54
static bool java_post_options (const char **);
55

56
static int java_handle_option (size_t scode, const char *arg, int value);
57 58 59 60 61 62 63 64 65
static void put_decl_string (const char *, int);
static void put_decl_node (tree);
static void java_print_error_function (diagnostic_context *, const char *);
static tree java_tree_inlining_walk_subtrees (tree *, int *, walk_tree_fn,
					      void *, void *);
static int java_unsafe_for_reeval (tree);
static int merge_init_test_initialization (void * *, void *);
static int inline_init_test_initialization (void * *, void *);
static bool java_can_use_bit_fields_p (void);
66
static bool java_dump_tree (void *, tree);
67
static void dump_compound_expr (dump_info_p, tree);
68
static bool java_decl_ok_for_sibcall (tree);
69
static int java_estimate_num_insns (tree);
70
static int java_start_inlining (tree);
71
static tree java_get_callee_fndecl (tree);
72

73 74
#ifndef TARGET_OBJECT_SUFFIX
# define TARGET_OBJECT_SUFFIX ".o"
75 76
#endif

Per Bothner committed
77 78 79 80 81 82
/* Table indexed by tree code giving a string containing a character
   classifying the tree code.  Possibilities are
   t, d, s, c, r, <, 1 and 2.  See java/java-tree.def for details.  */

#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,

83 84
const char tree_code_type[] = {
#include "tree.def"
Per Bothner committed
85 86 87 88 89 90 91 92 93 94 95
  'x',
#include "java-tree.def"
};
#undef DEFTREECODE

/* Table indexed by tree code giving number of expression
   operands beyond the fixed part of the node structure.
   Not used for types or decls.  */

#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,

96 97
const unsigned char tree_code_length[] = {
#include "tree.def"
Per Bothner committed
98 99 100 101 102 103 104 105 106
  0,
#include "java-tree.def"
};
#undef DEFTREECODE

/* Names of tree components.
   Used for printing out the tree and error messages.  */
#define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,

107 108
const char *const tree_code_name[] = {
#include "tree.def"
Per Bothner committed
109 110 111 112 113
  "@@dummy",
#include "java-tree.def"
};
#undef DEFTREECODE

114 115 116 117
/* Used to avoid printing error messages with bogus function
   prototypes.  Starts out false.  */
static bool inhibit_error_function_printing;

Anthony Green committed
118 119
int compiling_from_source;

120
const char *resource_name;
121

Anthony Green committed
122 123
int flag_emit_class_files = 0;

124 125 126 127
/* Nonzero if input file is a file with a list of filenames to compile. */

int flag_filelist_file = 0;

128
/* When nonzero, we emit xref strings. Values of the flag for xref
129 130 131 132
   backends are defined in xref_flag_table, xref.c.  */

int flag_emit_xref = 0;

133
/* When nonzero, -Wall was turned on.  */
134 135
int flag_wall = 0;

136
/* When nonzero, check for redundant modifier uses.  */
137 138
int flag_redundant = 0;

139
/* When nonzero, call a library routine to do integer divisions. */
140 141
int flag_use_divide_subroutine = 1;

142
/* When nonzero, generate code for the Boehm GC.  */
143 144
int flag_use_boehm_gc = 0;

145
/* When nonzero, assume the runtime uses a hash table to map an
146 147 148
   object to its synchronization structure.  */
int flag_hash_synchronization;

Anthony Green committed
149 150 151
/* When nonzero, permit the use of the assert keyword.  */
int flag_assert = 1;

152
/* When nonzero, assume all native functions are implemented with
153 154 155
   JNI, not CNI.  */
int flag_jni = 0;

156
/* When nonzero, warn when source file is newer than matching class
157 158 159
   file.  */
int flag_newer = 1;

160
/* When nonzero, generate checks for references to NULL.  */
161 162
int flag_check_references = 0;

163
/* The encoding of the source file.  */
164
const char *current_encoding = NULL;
165

166
/* When nonzero, report the now deprecated empty statements.  */
167 168
int flag_extraneous_semicolon;

169 170 171
/* When nonzero, report use of deprecated classes, methods, or fields.  */
int flag_deprecated = 1;

172
/* When nonzero, always check for a non gcj generated classes archive.  */
173 174
int flag_force_classes_archive_check;

175 176 177 178
/* When zero, don't optimize static class initialization. This flag shouldn't
   be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead.  */
int flag_optimize_sci = 1;

179
/* When nonzero, use offset tables for virtual method calls
180 181 182
   in order to improve binary compatibility. */
int flag_indirect_dispatch = 0;

183 184 185
/* When zero, don't generate runtime array store checks. */
int flag_store_check = 1;

186
/* When nonzero, print extra version information.  */
187
static int v_flag = 0;
188

189
/* Set nonzero if the user specified -finline-functions on the command
190 191 192
   line.  */
int flag_really_inline = 0;

Anthony Green committed
193 194
JCF *current_jcf;

195
/* Variable controlling how dependency tracking is enabled in
196
   java_init.  */
197 198 199 200 201
static int dependency_tracking = 0;

/* Flag values for DEPENDENCY_TRACKING.  */
#define DEPEND_SET_FILE 1
#define DEPEND_ENABLE   2
202 203
#define DEPEND_TARGET_SET 4
#define DEPEND_FILE_ALREADY_SET 8
204

205 206 207 208 209
struct language_function GTY(())
{
  int unused;
};

210 211
#undef LANG_HOOKS_NAME
#define LANG_HOOKS_NAME "GNU Java"
212 213
#undef LANG_HOOKS_INIT
#define LANG_HOOKS_INIT java_init
214 215
#undef LANG_HOOKS_FINISH
#define LANG_HOOKS_FINISH java_finish
216 217
#undef LANG_HOOKS_INIT_OPTIONS
#define LANG_HOOKS_INIT_OPTIONS java_init_options
218 219
#undef LANG_HOOKS_HANDLE_OPTION
#define LANG_HOOKS_HANDLE_OPTION java_handle_option
220 221
#undef LANG_HOOKS_POST_OPTIONS
#define LANG_HOOKS_POST_OPTIONS java_post_options
222 223
#undef LANG_HOOKS_PARSE_FILE
#define LANG_HOOKS_PARSE_FILE java_parse_file
224 225
#undef LANG_HOOKS_UNSAFE_FOR_REEVAL
#define LANG_HOOKS_UNSAFE_FOR_REEVAL java_unsafe_for_reeval
226 227
#undef LANG_HOOKS_MARK_ADDRESSABLE
#define LANG_HOOKS_MARK_ADDRESSABLE java_mark_addressable
228 229
#undef LANG_HOOKS_EXPAND_EXPR
#define LANG_HOOKS_EXPAND_EXPR java_expand_expr
230 231
#undef LANG_HOOKS_TRUTHVALUE_CONVERSION
#define LANG_HOOKS_TRUTHVALUE_CONVERSION java_truthvalue_conversion
232 233
#undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
#define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
Neil Booth committed
234 235
#undef LANG_HOOKS_DECL_PRINTABLE_NAME
#define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
236 237
#undef LANG_HOOKS_PRINT_ERROR_FUNCTION
#define LANG_HOOKS_PRINT_ERROR_FUNCTION	java_print_error_function
238 239
#undef LANG_HOOKS_CAN_USE_BIT_FIELDS_P
#define LANG_HOOKS_CAN_USE_BIT_FIELDS_P java_can_use_bit_fields_p
240

241 242 243 244
#undef LANG_HOOKS_TYPE_FOR_MODE
#define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
#undef LANG_HOOKS_TYPE_FOR_SIZE
#define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
245 246 247 248 249 250
#undef LANG_HOOKS_SIGNED_TYPE
#define LANG_HOOKS_SIGNED_TYPE java_signed_type
#undef LANG_HOOKS_UNSIGNED_TYPE
#define LANG_HOOKS_UNSIGNED_TYPE java_unsigned_type
#undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
#define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE java_signed_or_unsigned_type
251

252 253 254
#undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
#define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES java_tree_inlining_walk_subtrees

255 256 257
#undef LANG_HOOKS_TREE_INLINING_ESTIMATE_NUM_INSNS
#define LANG_HOOKS_TREE_INLINING_ESTIMATE_NUM_INSNS java_estimate_num_insns

258 259 260
#undef LANG_HOOKS_TREE_INLINING_START_INLINING
#define LANG_HOOKS_TREE_INLINING_START_INLINING java_start_inlining

261 262 263
#undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
#define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree

264 265 266
#undef LANG_HOOKS_DECL_OK_FOR_SIBCALL
#define LANG_HOOKS_DECL_OK_FOR_SIBCALL java_decl_ok_for_sibcall

267 268 269
#undef LANG_HOOKS_GET_CALLEE_FNDECL
#define LANG_HOOKS_GET_CALLEE_FNDECL java_get_callee_fndecl

270 271 272
#undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
#define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION java_expand_body

273
/* Each front end provides its own.  */
274
const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
275

Anthony Green committed
276 277
/*
 * process java-specific compiler command-line options
278
 * return 0, but do not complain if the option is not recognized.
Anthony Green committed
279
 */
280
static int
281
java_handle_option (size_t scode, const char *arg, int value)
Anthony Green committed
282
{
283
  enum opt_code code = (enum opt_code) scode;
284

285
  switch (code)
Anthony Green committed
286
    {
287
    default:
288
      abort();
Anthony Green committed
289

290 291 292
    case OPT_I:
      jcf_path_include_arg (arg);
      break;
293

294 295 296 297
    case OPT_M:
      jcf_dependency_init (1);
      dependency_tracking |= DEPEND_ENABLE;
      break;
298

299
    case OPT_MD_:
300 301
      jcf_dependency_init (1);
      dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
302 303 304 305
      break;

    case OPT_MF:
      jcf_dependency_set_dep_file (arg);
306
      dependency_tracking |= DEPEND_FILE_ALREADY_SET;
307 308 309
      break;

    case OPT_MM:
310 311
      jcf_dependency_init (0);
      dependency_tracking |= DEPEND_ENABLE;
312 313
      break;

314
    case OPT_MMD_:
315
      jcf_dependency_init (0);
316 317 318 319
      dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
      break;

    case OPT_MP:
320
      jcf_dependency_print_dummies ();
321 322 323 324
      break;

    case OPT_MT:
      jcf_dependency_set_target (arg);
325
      dependency_tracking |= DEPEND_TARGET_SET;
326
      break;
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    case OPT_Wall:
      flag_wall = value;
      flag_redundant = value;
      flag_extraneous_semicolon = value;
      /* When -Wall given, enable -Wunused.  We do this because the C
	 compiler does it, and people expect it.  */
      set_Wunused (value);
      break;

    case OPT_Wdeprecated:
      flag_deprecated = value;
      break;

    case OPT_Wextraneous_semicolon:
      flag_extraneous_semicolon = value;
      break;

    case OPT_Wout_of_date:
      flag_newer = value;
      break;

    case OPT_Wredundant_modifiers:
      flag_redundant = value;
      break;

    case OPT_fassert:
      flag_assert = value;
      break;

    case OPT_fassume_compiled_:
      add_assume_compiled (arg, !value);
      break;

    case OPT_fassume_compiled:
      add_assume_compiled ("", !value);
      break;

    case OPT_fbootclasspath_:
      jcf_path_bootclasspath_arg (arg);
      break;

    case OPT_fcheck_references:
      flag_check_references = value;
      break;

    case OPT_fclasspath_:
    case OPT_fCLASSPATH_:
      jcf_path_classpath_arg (arg);
      break;

    case OPT_fcompile_resource_:
      resource_name = arg;
      break;

    case OPT_fdump_:
383
      if (!dump_switch_p (arg))
384
	return 0;
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
      break;

    case OPT_femit_class_file:
    case OPT_femit_class_files:
      flag_emit_class_files = value;
      break;

    case OPT_fencoding_:
      current_encoding = arg;
      break;

    case OPT_fextdirs_:
      jcf_path_extdirs_arg (arg);
      break;

    case OPT_ffilelist_file:
      flag_filelist_file = value;
      break;

    case OPT_fforce_classes_archive_check:
      flag_force_classes_archive_check = value;
      break;

    case OPT_fhash_synchronization:
      flag_hash_synchronization = value;
      break;

    case OPT_findirect_dispatch:
      flag_indirect_dispatch = value;
      break;

    case OPT_finline_functions:
      flag_inline_functions = value;
      flag_really_inline = value;
      break;

    case OPT_fjni:
      flag_jni = value;
      break;

    case OPT_foptimize_static_class_initialization:
      flag_optimize_sci = value;
      break;

    case OPT_foutput_class_dir_:
      jcf_write_base_directory = arg;
      break;

    case OPT_fstore_check:
      flag_store_check = value;
      break;

    case OPT_fuse_boehm_gc:
      flag_use_boehm_gc = value;
      break;

    case OPT_fuse_divide_subroutine:
      flag_use_divide_subroutine = value;
      break;

    case OPT_version:
446
      v_flag = 1;
447 448 449 450
      break;
    }

  return 1;
Anthony Green committed
451 452
}

453
/* Global open file.  */
Anthony Green committed
454
FILE *finput;
455

456 457
static bool
java_init (void)
Anthony Green committed
458
{
459 460 461 462 463
#if 0
  extern int flag_minimal_debug;
  flag_minimal_debug = 0;
#endif

464 465 466
  if (flag_inline_functions)
    flag_inline_trees = 1;

467 468 469 470 471
  /* FIXME: Indirect dispatch isn't yet compatible with static class
     init optimization.  */
  if (flag_indirect_dispatch)
    always_initialize_class_p = true;

472 473 474 475 476 477 478
  /* Force minimum function alignment if g++ uses the least significant
     bit of function pointers to store the virtual bit. This is required
     to keep vtables compatible.  */
  if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
      && force_align_functions_log < 1)
    force_align_functions_log = 1;

479
  jcf_path_seal (v_flag);
480 481 482 483 484

  java_init_decl_processing ();

  using_eh_for_cleanups ();

485
  return true;
Anthony Green committed
486 487
}

488
static void
489
java_finish (void)
Anthony Green committed
490
{
491
  jcf_dependency_write ();
Anthony Green committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
}

/* Buffer used by lang_printable_name. */
static char *decl_buf = NULL;

/* Allocated size of decl_buf. */
static int decl_buflen = 0;

/* Length of used part of decl_buf;  position for next character. */
static int decl_bufpos = 0;

/* Append the string STR to decl_buf.
   It length is given by LEN;  -1 means the string is nul-terminated. */

static void
507
put_decl_string (const char *str, int len)
Anthony Green committed
508 509 510 511 512 513 514 515
{
  if (len < 0)
    len = strlen (str);
  if (decl_bufpos + len >= decl_buflen)
    {
      if (decl_buf == NULL)
	{
	  decl_buflen = len + 100;
516
	  decl_buf = xmalloc (decl_buflen);
Anthony Green committed
517 518 519 520
	}
      else
	{
	  decl_buflen *= 2;
521
	  decl_buf = xrealloc (decl_buf, decl_buflen);
Anthony Green committed
522 523 524 525 526 527 528 529 530
	}
    }
  strcpy (decl_buf + decl_bufpos, str);
  decl_bufpos += len;
}

/* Append to decl_buf a printable name for NODE. */

static void
531
put_decl_node (tree node)
Anthony Green committed
532 533 534 535 536 537 538 539 540 541
{
  int was_pointer = 0;
  if (TREE_CODE (node) == POINTER_TYPE)
    {
      node = TREE_TYPE (node);
      was_pointer = 1;
    }
  if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
      && DECL_NAME (node) != NULL_TREE)
    {
542
      if (TREE_CODE (node) == FUNCTION_DECL)
543
	{
544 545 546 547
	  /* We want to print the type the DECL belongs to. We don't do
	     that when we handle constructors. */
	  if (! DECL_CONSTRUCTOR_P (node)
	      && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node))
Anthony Green committed
548
	    {
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
	      put_decl_node (TYPE_NAME (DECL_CONTEXT (node)));
	      put_decl_string (".", 1);
	    }
	  if (! DECL_CONSTRUCTOR_P (node))
	    put_decl_node (DECL_NAME (node));
	  if (TREE_TYPE (node) != NULL_TREE)
	    {
	      int i = 0;
	      tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
	      if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
		args = TREE_CHAIN (args);
	      put_decl_string ("(", 1);
	      for ( ; args != end_params_node;  args = TREE_CHAIN (args), i++)
		{
		  if (i > 0)
		    put_decl_string (",", 1);
		  put_decl_node (TREE_VALUE (args));
		}
	      put_decl_string (")", 1);
Anthony Green committed
568 569
	    }
	}
570 571
      else
	put_decl_node (DECL_NAME (node));
Anthony Green committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    }
  else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
      && TYPE_NAME (node) != NULL_TREE)
    {
      if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
	{
	  put_decl_node (TYPE_ARRAY_ELEMENT (node));
	  put_decl_string("[]", 2);
	}
      else if (node == promoted_byte_type_node)
	put_decl_string ("byte", 4);
      else if (node == promoted_short_type_node)
	put_decl_string ("short", 5);
      else if (node == promoted_char_type_node)
	put_decl_string ("char", 4);
      else if (node == promoted_boolean_type_node)
	put_decl_string ("boolean", 7);
      else if (node == void_type_node && was_pointer)
	put_decl_string ("null", 4);
      else
	put_decl_node (TYPE_NAME (node));
    }
  else if (TREE_CODE (node) == IDENTIFIER_NODE)
    put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
  else
    put_decl_string ("<unknown>", -1);
}

/* Return a user-friendly name for DECL.
   The resulting string is only valid until the next call.
   The value of the hook decl_printable_name is this function,
603
   which is also called directly by java_print_error_function. */
Anthony Green committed
604

605
const char *
606
lang_printable_name (tree decl, int v  __attribute__ ((__unused__)))
Anthony Green committed
607 608 609 610 611 612 613
{
  decl_bufpos = 0;
  put_decl_node (decl);
  put_decl_string ("", 1);
  return decl_buf;
}

614 615 616 617
/* Does the same thing that lang_printable_name, but add a leading
   space to the DECL name string -- With Leading Space.  */

const char *
618
lang_printable_name_wls (tree decl, int v  __attribute__ ((__unused__)))
619 620 621 622 623 624 625 626
{
  decl_bufpos = 1;
  put_decl_node (decl);
  put_decl_string ("", 1);
  decl_buf [0] = ' ';
  return decl_buf;
}

Anthony Green committed
627
/* Print on stderr the current class and method context.  This function
628
   is the value of the hook print_error_function. */
Anthony Green committed
629

630 631
static GTY(()) tree last_error_function_context;
static GTY(()) tree last_error_function;
632
static void
633 634
java_print_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
			   const char *file)
Anthony Green committed
635
{
636 637 638 639
  /* Don't print error messages with bogus function prototypes.  */
  if (inhibit_error_function_printing)
    return;

Anthony Green committed
640 641 642 643 644 645 646 647
  if (current_function_decl != NULL
      && DECL_CONTEXT (current_function_decl) != last_error_function_context)
    {
      if (file)
	fprintf (stderr, "%s: ", file);

      last_error_function_context = DECL_CONTEXT (current_function_decl);
      fprintf (stderr, "In class `%s':\n",
648
	       lang_printable_name (last_error_function_context, 0));
Anthony Green committed
649 650 651 652 653 654 655 656 657 658
    }
  if (last_error_function != current_function_decl)
    {
      if (file)
	fprintf (stderr, "%s: ", file);

      if (current_function_decl == NULL)
	fprintf (stderr, "At top level:\n");
      else
	{
659
	  const char *name = lang_printable_name (current_function_decl, 2);
660 661 662 663
	  fprintf (stderr, "In %s `%s':\n",
		   (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor" 
		    : "method"),
		   name);
Anthony Green committed
664 665 666 667 668 669 670 671 672
	}

      last_error_function = current_function_decl;
    }

}

/* Called to install the PRINT_ERROR_FUNCTION hook differently
   according to LEVEL. LEVEL is 1 during early parsing, when function
673 674 675
   prototypes aren't fully resolved. java_print_error_function is set
   so it doesn't print incomplete function prototypes. When LEVEL is
   2, function prototypes are fully resolved and can be printed when
Anthony Green committed
676 677
   reporting errors.  */

678
void lang_init_source (int level)
Anthony Green committed
679
{
680
  inhibit_error_function_printing = (level == 1);
Anthony Green committed
681 682
}

683 684 685
static unsigned int
java_init_options (unsigned int argc ATTRIBUTE_UNUSED,
		   const char **argv ATTRIBUTE_UNUSED)
Anthony Green committed
686
{
687
  flag_bounds_check = 1;
688 689
  flag_exceptions = 1;
  flag_non_call_exceptions = 1;
Jan Hubicka committed
690 691 692

  /* In Java floating point operations never trap.  */
  flag_trapping_math = 0;
693 694 695

  /* In Java arithmetic overflow always wraps around.  */
  flag_wrapv = 1;
696

697 698 699
  /* Java requires left-to-right evaluation of subexpressions.  */
  flag_evaluation_order = 1;

700 701
  jcf_path_init ();

702
  return CL_Java;
Anthony Green committed
703
}
704

705
static bool
706
java_can_use_bit_fields_p (void)
707 708 709 710 711 712
{
  /* The bit-field optimizations cause problems when generating class
     files.  */
  return flag_emit_class_files ? false : true;
}

713
/* Post-switch processing.  */
714
static bool
715
java_post_options (const char **pfilename)
716
{
717 718
  const char *filename = *pfilename;

719 720 721
 /* Use tree inlining if possible.  Function instrumentation is only
     done in the RTL level, so we disable tree inlining.  */
  if (! flag_instrument_function_entry_exit)
722
    {
723 724 725 726 727 728 729
      if (!flag_no_inline)
	flag_no_inline = 1;
      if (flag_inline_functions)
	{
	  flag_inline_trees = 2;
	  flag_inline_functions = 0;
	}
730
    }
731

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  /* Open input file.  */

  if (filename == 0 || !strcmp (filename, "-"))
    {
      finput = stdin;
      filename = "stdin";

      if (dependency_tracking)
	error ("can't do dependency tracking with input from stdin");
    }
  else
    {
      if (dependency_tracking)
	{
	  char *dot;

	  /* If the target is set and the output filename is set, then
	     there's no processing to do here.  Otherwise we must
	     compute one or the other.  */
	  if (! ((dependency_tracking & DEPEND_TARGET_SET)
		 && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
	    {
	      dot = strrchr (filename, '.');
	      if (dot == NULL)
		error ("couldn't determine target name for dependency tracking");
	      else
		{
		  char *buf = xmalloc (dot - filename +
				       3 + sizeof (TARGET_OBJECT_SUFFIX));
		  strncpy (buf, filename, dot - filename);

		  /* If emitting class files, we might have multiple
		     targets.  The class generation code takes care of
		     registering them.  Otherwise we compute the
		     target name here.  */
		  if ((dependency_tracking & DEPEND_TARGET_SET))
		    ; /* Nothing.  */
		  else if (flag_emit_class_files)
		    jcf_dependency_set_target (NULL);
		  else
		    {
		      strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
		      jcf_dependency_set_target (buf);
		    }

		  if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
		    ; /* Nothing.  */
		  else if ((dependency_tracking & DEPEND_SET_FILE))
		    {
		      strcpy (buf + (dot - filename), ".d");
		      jcf_dependency_set_dep_file (buf);
		    }
		  else
		    jcf_dependency_set_dep_file ("-");

		  free (buf);
		}
	    }
	}
    }

793 794
  /* Initialize the compiler back end.  */
  return false;
795 796
}

797 798 799
/* Return either DECL or its known constant value (if it has one).  */

tree
800
decl_constant_value (tree decl)
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
{
  if (/* Don't change a variable array bound or initial value to a constant
	 in a place where a variable is invalid.  */
      current_function_decl != 0
      && ! TREE_THIS_VOLATILE (decl)
      && TREE_READONLY (decl)
      && DECL_INITIAL (decl) != 0
      && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
      /* This is invalid if initial value is not constant.
	 If it has either a function call, a memory reference,
	 or a variable, then re-evaluating it could give different results.  */
      && TREE_CONSTANT (DECL_INITIAL (decl))
      /* Check for cases where this is sub-optimal, even though valid.  */
      && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
    return DECL_INITIAL (decl);
  return decl;
}

/* Walk the language specific tree nodes during inlining.  */

static tree
822 823 824 825 826
java_tree_inlining_walk_subtrees (tree *tp ATTRIBUTE_UNUSED,
				  int *subtrees ATTRIBUTE_UNUSED,
				  walk_tree_fn func ATTRIBUTE_UNUSED,
				  void *data ATTRIBUTE_UNUSED,
				  void *htab ATTRIBUTE_UNUSED)
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
{
  enum tree_code code;
  tree result;

#define WALK_SUBTREE(NODE)				\
  do							\
    {							\
      result = walk_tree (&(NODE), func, data, htab);	\
      if (result)					\
	return result;					\
    }							\
  while (0)

  tree t = *tp;
  if (!t)
    return NULL_TREE;

  code = TREE_CODE (t);
  switch (code)
    {
    case BLOCK:
848
      WALK_SUBTREE (BLOCK_EXPR_BODY (t));
849 850 851 852 853
      return NULL_TREE;

    default:
      return NULL_TREE;
    }
854 855

  #undef WALK_SUBTREE
856 857
}

858 859
/* Called from unsafe_for_reeval.  */
static int
860
java_unsafe_for_reeval (tree t)
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
{
  switch (TREE_CODE (t))
    {
    case BLOCK:
      /* Our expander tries to expand the variables twice.  Boom.  */
      if (BLOCK_EXPR_DECLS (t) != NULL)
	return 2;
      return unsafe_for_reeval (BLOCK_EXPR_BODY (t));

    default:
      break;
    }

  return -1;
}

877 878 879 880 881
/* Every call to a static constructor has an associated boolean
   variable which is in the outermost scope of the calling method.
   This variable is used to avoid multiple calls to the static
   constructor for each class.  

882
   It looks something like this:
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

   foo ()
   {
      boolean dummy = OtherClass.is_initialized;
  
     ...
  
     if (! dummy)
       OtherClass.initialize();

     ... use OtherClass.data ...
   }

   Each of these boolean variables has an entry in the
   DECL_FUNCTION_INIT_TEST_TABLE of a method.  When inlining a method
   we must merge the DECL_FUNCTION_INIT_TEST_TABLE from the function
899
   being inlined and create the boolean variables in the outermost
900 901 902 903 904 905
   scope of the method being inlined into.  */

/* Create a mapping from a boolean variable in a method being inlined
   to one in the scope of the method being inlined into.  */

static int
906
merge_init_test_initialization (void **entry, void *x)
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
{
  struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
  splay_tree decl_map = (splay_tree)x;
  splay_tree_node n;
  tree *init_test_decl;
  
  /* See if we have remapped this declaration.  If we haven't there's
     a bug in the inliner.  */
  n = splay_tree_lookup (decl_map, (splay_tree_key) ite->value);
  if (! n)
    abort ();

  /* Create a new entry for the class and its remapped boolean
     variable.  If we already have a mapping for this class we've
     already initialized it, so don't overwrite the value.  */
  init_test_decl = java_treetreehash_new
    (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
  if (!*init_test_decl)
    *init_test_decl = (tree)n->value;

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
  /* This fixes a weird case.  

  The front end assumes that once we have called a method that
  initializes some class, we can assume the class is initialized.  It
  does this by setting the DECL_INITIAL of the init_test_decl for that
  class, and no initializations are emitted for that class.
  
  However, what if the method that is suppoed to do the initialization
  is itself inlined in the caller?  When expanding the called method
  we'll assume that the class initalization has already been done,
  because the DECL_INITIAL of the init_test_decl is set.
  
  To fix this we remove the DECL_INITIAL (in the caller scope) of all
  the init_test_decls corresponding to classes initialized by the
  inlined method.  This makes the caller no longer assume that the
  method being inlined does any class initializations.  */
  DECL_INITIAL (*init_test_decl) = NULL;

945 946 947 948 949 950 951
  return true;
}

/* Merge the DECL_FUNCTION_INIT_TEST_TABLE from the function we're
   inlining.  */

void
952
java_inlining_merge_static_initializers (tree fn, void *decl_map)
953 954 955 956 957 958 959 960 961 962 963 964 965
{
  htab_traverse 
    (DECL_FUNCTION_INIT_TEST_TABLE (fn),
     merge_init_test_initialization, decl_map);
}

/* Lookup a DECL_FUNCTION_INIT_TEST_TABLE entry in the method we're
   inlining into.  If we already have a corresponding entry in that
   class we don't need to create another one, so we create a mapping
   from the variable in the inlined class to the corresponding
   pre-existing one.  */

static int
966
inline_init_test_initialization (void **entry, void *x)
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
{
  struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
  splay_tree decl_map = (splay_tree)x;
  
  tree h = java_treetreehash_find 
    (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
  if (! h)
    return true;
  splay_tree_insert (decl_map,
		     (splay_tree_key) ite->value,
		     (splay_tree_value) h);
  return true;
}

/* Look up the boolean variables in the DECL_FUNCTION_INIT_TEST_TABLE
   of a method being inlined.  For each hone, if we already have a
   variable associated with the same class in the method being inlined
   into, create a new mapping for it.  */

void
987
java_inlining_map_static_initializers (tree fn, void *decl_map)
988 989 990 991 992 993
{
  htab_traverse 
    (DECL_FUNCTION_INIT_TEST_TABLE (fn),
     inline_init_test_initialization, decl_map);
}

994 995 996
/* Avoid voluminous output for deep recursion of compound exprs.  */

static void
997
dump_compound_expr (dump_info_p di, tree t)
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
{
  int i;

  for (i=0; i<2; i++)
    {
      switch (TREE_CODE (TREE_OPERAND (t, i)))
	{
	case COMPOUND_EXPR:
	  dump_compound_expr (di, TREE_OPERAND (t, i));
	  break;

	case EXPR_WITH_FILE_LOCATION:
	    {
	      tree wfl_node = EXPR_WFL_NODE (TREE_OPERAND (t, i));
	      dump_child ("expr", wfl_node);
	      break;
	    }

	default:
	  dump_child ("expr", TREE_OPERAND (t, i));
	}
    }
}
  
1022
static bool
1023
java_dump_tree (void *dump_info, tree t)
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
{
  enum tree_code code;
  dump_info_p di = (dump_info_p) dump_info;

  /* Figure out what kind of node this is.  */
  code = TREE_CODE (t);

  switch (code)
    {
    case FUNCTION_DECL:
      dump_child ("args", DECL_ARGUMENTS (t));
      if (DECL_EXTERNAL (t))
	dump_string (di, "undefined");
      if (TREE_PUBLIC (t))
	dump_string (di, "extern");
      else
	dump_string (di, "static");
      if (DECL_LANG_SPECIFIC (t))
	dump_child ("body", DECL_FUNCTION_BODY (t));
      if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
	dump_child ("inline body", DECL_SAVED_TREE (t));
1045
      return true;
1046 1047 1048

    case RETURN_EXPR:
      dump_child ("expr", TREE_OPERAND (t, 0));
1049
      return true;
1050 1051 1052

    case GOTO_EXPR:
      dump_child ("goto", TREE_OPERAND (t, 0));
1053
      return true;
1054 1055 1056

    case LABEL_EXPR:
      dump_child ("label", TREE_OPERAND (t, 0));
1057
      return true;
1058 1059 1060 1061

    case LABELED_BLOCK_EXPR:
      dump_child ("label", TREE_OPERAND (t, 0));
      dump_child ("block", TREE_OPERAND (t, 1));
1062
      return true;
1063 1064 1065 1066

    case EXIT_BLOCK_EXPR:
      dump_child ("block", TREE_OPERAND (t, 0));
      dump_child ("val", TREE_OPERAND (t, 1));
1067
      return true;
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

    case BLOCK:
      if (BLOCK_EXPR_BODY (t))
	{
	  tree local = BLOCK_VARS (t);
	  while (local)
	    {
	      tree next = TREE_CHAIN (local);
	      dump_child ("var", local);
	      local = next;
	    }
	  
	  {
	    tree block = BLOCK_EXPR_BODY (t);
	    dump_child ("body", block);
	    block = TREE_CHAIN (block);
	  }
	}
1086
      return true;
1087 1088 1089
      
    case COMPOUND_EXPR:
      if (!dump_flag (di, TDF_SLIM, t))
1090
	return false;
1091
      dump_compound_expr (di, t);
1092
      return true;
1093 1094 1095 1096

    default:
      break;
    }
1097
  return false;
1098
}
1099 1100 1101 1102 1103 1104 1105 1106 1107

/* Java calls can't, in general, be sibcalls because we need an
   accurate stack trace in order to guarantee correct operation of
   methods such as Class.forName(String) and
   SecurityManager.getClassContext().  */

static bool
java_decl_ok_for_sibcall (tree decl)
{
1108
  return decl != NULL && DECL_CONTEXT (decl) == output_class;
1109 1110
}

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
/* Used by estimate_num_insns.  Estimate number of instructions seen
   by given statement.  */
static tree
java_estimate_num_insns_1 (tree *tp, int *walk_subtrees, void *data)
{
  int *count = data;
  tree x = *tp;

  if (TYPE_P (x) || DECL_P (x))
    {
      *walk_subtrees = 0;
      return NULL;
    }
  /* Assume that constants and references counts nothing.  These should
Kazu Hirata committed
1125
     be majorized by amount of operations among them we count later
1126 1127 1128 1129 1130 1131
     and are common target of CSE and similar optimizations.  */
  if (TREE_CODE_CLASS (TREE_CODE (x)) == 'c'
      || TREE_CODE_CLASS (TREE_CODE (x)) == 'r')
    return NULL;
  switch (TREE_CODE (x))
    { 
Kazu Hirata committed
1132
    /* Recognize assignments of large structures and constructors of
1133 1134 1135 1136
       big arrays.  */
    case MODIFY_EXPR:
    case CONSTRUCTOR:
      {
1137
	HOST_WIDE_INT size;
1138

1139 1140 1141
	size = int_size_in_bytes (TREE_TYPE (x));

	if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO)
1142 1143
	  *count += 10;
	else
1144
	  *count += ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
1145 1146 1147 1148 1149 1150 1151 1152 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 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
      }
      break;
    /* Few special cases of expensive operations.  This is usefull
       to avoid inlining on functions having too many of these.  */
    case TRUNC_DIV_EXPR:
    case CEIL_DIV_EXPR:
    case FLOOR_DIV_EXPR:
    case ROUND_DIV_EXPR:
    case TRUNC_MOD_EXPR:
    case CEIL_MOD_EXPR:
    case FLOOR_MOD_EXPR:
    case ROUND_MOD_EXPR:
    case RDIV_EXPR:
    case CALL_EXPR:

    case NEW_ARRAY_EXPR:
    case NEW_ANONYMOUS_ARRAY_EXPR:
    case NEW_CLASS_EXPR:
      *count += 10;
      break;
    /* Various containers that will produce no code themselves.  */
    case INIT_EXPR:
    case TARGET_EXPR:
    case BIND_EXPR:
    case BLOCK:
    case TREE_LIST:
    case TREE_VEC:
    case IDENTIFIER_NODE:
    case PLACEHOLDER_EXPR:
    case WITH_CLEANUP_EXPR:
    case CLEANUP_POINT_EXPR:
    case NOP_EXPR:
    case VIEW_CONVERT_EXPR:
    case SAVE_EXPR:
    case UNSAVE_EXPR:
    case COMPLEX_EXPR:
    case REALPART_EXPR:
    case IMAGPART_EXPR:
    case TRY_CATCH_EXPR:
    case TRY_FINALLY_EXPR:
    case LABEL_EXPR:
    case EXIT_EXPR:
    case LABELED_BLOCK_EXPR:
    case EXIT_BLOCK_EXPR:
    case EXPR_WITH_FILE_LOCATION:
    case UNARY_PLUS_EXPR:
    case THIS_EXPR:
    case DEFAULT_EXPR:
    case TRY_EXPR:

      break;
    case CLASS_LITERAL:
      *walk_subtrees = 0;
      break;
    default:
      (*count)++;
    }
  return NULL;
}

/*  Estimate number of instructions that will be created by expanding the body.  */
static int
java_estimate_num_insns (tree decl)
{
  int num = 0;
1210 1211
  walk_tree_without_duplicates (&DECL_SAVED_TREE (decl),
				java_estimate_num_insns_1, &num);
1212 1213 1214
  return num;
}

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
/* Start inlining fn.  Called by the tree inliner via
   lang_hooks.tree_inlining.cannot_inline_tree_fn.  */

static int
java_start_inlining (tree fn)
{
  /* A java function's body doesn't have a BLOCK structure suitable
     for debug output until it is expanded.  Prevent inlining functions
     that are not yet expanded.  */
  return TREE_ASM_WRITTEN (fn) ? 1 : 0;
}

1227 1228 1229 1230 1231 1232 1233 1234
/* Given a call_expr, try to figure out what its target might be.  In
   the case of an indirection via the atable, search for the decl.  If
   the decl is external, we return NULL.  If we don't, the optimizer
   will replace the indirection with a direct call, which undoes the
   purpose of the atable indirection.  */
static tree
java_get_callee_fndecl (tree call_expr)
{
1235
  tree method, table, element, atable_methods;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245

  HOST_WIDE_INT index;

  if (TREE_CODE (call_expr) != CALL_EXPR)
    return NULL;
  method = TREE_OPERAND (call_expr, 0);
  STRIP_NOPS (method);
  if (TREE_CODE (method) != ARRAY_REF)
    return NULL;
  table = TREE_OPERAND (method, 0);
1246 1247 1248
  if (! DECL_LANG_SPECIFIC(table)
      || !DECL_OWNER (table) 
      || TYPE_ATABLE_DECL (DECL_OWNER (table)) != table)
1249 1250
    return NULL;

1251 1252 1253
  atable_methods = TYPE_ATABLE_METHODS (DECL_OWNER (table));
  index = TREE_INT_CST_LOW (TREE_OPERAND (method, 1));
  
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
  /* FIXME: Replace this for loop with a hash table lookup.  */
  for (element = atable_methods; element; element = TREE_CHAIN (element))
    {
      if (index == 1)
	{
	  tree purpose = TREE_PURPOSE (element);
	  if (TREE_CODE (purpose) == FUNCTION_DECL
	      && ! DECL_EXTERNAL (purpose))
	    return purpose;
	  else
	    return NULL;
	}
      --index;
    }
1268

1269 1270 1271
  return NULL;
}

1272
#include "gt-java-lang.h"