1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 class CompilationResourceObj;
  26 class XHandlers;
  27 class ExceptionInfo;
  28 class DebugInformationRecorder;
  29 class FrameMap;
  30 class IR;
  31 class IRScope;
  32 class Instruction;
  33 class LinearScan;
  34 class OopMap;
  35 class LIR_Emitter;
  36 class LIR_Assembler;
  37 class CodeEmitInfo;
  38 class ciEnv;
  39 class ciMethod;
  40 class ValueStack;
  41 class LIR_OprDesc;
  42 class C1_MacroAssembler;
  43 class CFGPrinter;
  44 typedef LIR_OprDesc* LIR_Opr;
  45 
  46 
  47 define_array(BasicTypeArray, BasicType)
  48 define_stack(BasicTypeList, BasicTypeArray)
  49 
  50 define_array(ExceptionInfoArray, ExceptionInfo*)
  51 define_stack(ExceptionInfoList,  ExceptionInfoArray)
  52 
  53 class Compilation: public StackObj {
  54   friend class CompilationResourceObj;
  55  private:
  56   // compilation specifics
  57   Arena* _arena;
  58   int _next_id;
  59   int _next_block_id;
  60   AbstractCompiler*  _compiler;
  61   ciEnv*             _env;
  62   ciMethod*          _method;
  63   int                _osr_bci;
  64   IR*                _hir;
  65   int                _max_spills;
  66   FrameMap*          _frame_map;
  67   C1_MacroAssembler* _masm;
  68   bool               _has_exception_handlers;
  69   bool               _has_fpu_code;
  70   bool               _has_unsafe_access;
  71   bool               _would_profile;
  72   bool               _has_method_handle_invokes;  // True if this method has MethodHandle invokes.
  73   const char*        _bailout_msg;
  74   ExceptionInfoList* _exception_info_list;
  75   ExceptionHandlerTable _exception_handler_table;
  76   ImplicitExceptionTable _implicit_exception_table;
  77   LinearScan*        _allocator;
  78   CodeOffsets        _offsets;
  79   CodeBuffer         _code;
  80 
  81   // compilation helpers
  82   void initialize();
  83   void build_hir();
  84   void emit_lir();
  85 
  86   void emit_code_epilog(LIR_Assembler* assembler);
  87   int  emit_code_body();
  88 
  89   int  compile_java_method();
  90   void install_code(int frame_size);
  91   void compile_method();
  92 
  93   void generate_exception_handler_table();
  94 
  95   ExceptionInfoList* exception_info_list() const { return _exception_info_list; }
  96   ExceptionHandlerTable* exception_handler_table() { return &_exception_handler_table; }
  97 
  98   LinearScan* allocator()                          { return _allocator;      }
  99   void        set_allocator(LinearScan* allocator) { _allocator = allocator; }
 100 
 101   Instruction*       _current_instruction;       // the instruction currently being processed
 102 #ifndef PRODUCT
 103   Instruction*       _last_instruction_printed;  // the last instruction printed during traversal
 104 #endif // PRODUCT
 105 
 106  public:
 107   // creation
 108   Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method,
 109               int osr_bci, BufferBlob* buffer_blob);
 110   ~Compilation();
 111 
 112 
 113   static Compilation* current() {
 114     return (Compilation*) ciEnv::current()->compiler_data();
 115   }
 116 
 117   // accessors
 118   ciEnv* env() const                             { return _env; }
 119   AbstractCompiler* compiler() const             { return _compiler; }
 120   bool has_exception_handlers() const            { return _has_exception_handlers; }
 121   bool has_fpu_code() const                      { return _has_fpu_code; }
 122   bool has_unsafe_access() const                 { return _has_unsafe_access; }
 123   ciMethod* method() const                       { return _method; }
 124   int osr_bci() const                            { return _osr_bci; }
 125   bool is_osr_compile() const                    { return osr_bci() >= 0; }
 126   IR* hir() const                                { return _hir; }
 127   int max_spills() const                         { return _max_spills; }
 128   FrameMap* frame_map() const                    { return _frame_map; }
 129   CodeBuffer* code()                             { return &_code; }
 130   C1_MacroAssembler* masm() const                { return _masm; }
 131   CodeOffsets* offsets()                         { return &_offsets; }
 132   Arena* arena()                                 { return _arena; }
 133 
 134   // Instruction ids
 135   int get_next_id()                              { return _next_id++; }
 136   int number_of_instructions() const             { return _next_id; }
 137 
 138   // BlockBegin ids
 139   int get_next_block_id()                        { return _next_block_id++; }
 140   int number_of_blocks() const                   { return _next_block_id; }
 141 
 142   // setters
 143   void set_has_exception_handlers(bool f)        { _has_exception_handlers = f; }
 144   void set_has_fpu_code(bool f)                  { _has_fpu_code = f; }
 145   void set_has_unsafe_access(bool f)             { _has_unsafe_access = f; }
 146   void set_would_profile(bool f)                 { _would_profile = f; }
 147   // Add a set of exception handlers covering the given PC offset
 148   void add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers);
 149   // Statistics gathering
 150   void notice_inlined_method(ciMethod* method);
 151 
 152   // JSR 292
 153   bool     has_method_handle_invokes() const { return _has_method_handle_invokes;     }
 154   void set_has_method_handle_invokes(bool z) {        _has_method_handle_invokes = z; }
 155 
 156   DebugInformationRecorder* debug_info_recorder() const; // = _env->debug_info();
 157   Dependencies* dependency_recorder() const; // = _env->dependencies()
 158   ImplicitExceptionTable* implicit_exception_table()     { return &_implicit_exception_table; }
 159 
 160   Instruction* current_instruction() const       { return _current_instruction; }
 161   Instruction* set_current_instruction(Instruction* instr) {
 162     Instruction* previous = _current_instruction;
 163     _current_instruction = instr;
 164     return previous;
 165   }
 166 
 167 #ifndef PRODUCT
 168   void maybe_print_current_instruction();
 169 #endif // PRODUCT
 170 
 171   // error handling
 172   void bailout(const char* msg);
 173   bool bailed_out() const                        { return _bailout_msg != NULL; }
 174   const char* bailout_msg() const                { return _bailout_msg; }
 175 
 176   static int desired_max_code_buffer_size() {
 177 #ifndef PPC
 178     return (int) NMethodSizeLimit;  // default 256K or 512K
 179 #else
 180     // conditional branches on PPC are restricted to 16 bit signed
 181     return MAX2((unsigned int)NMethodSizeLimit,32*K);
 182 #endif
 183   }
 184   static int desired_max_constant_size() {
 185 #ifndef PPC
 186     return (int) NMethodSizeLimit / 10;  // about 25K
 187 #else
 188     return (MAX2((unsigned int)NMethodSizeLimit, 32*K)) / 10;
 189 #endif
 190   }
 191 
 192   static void setup_code_buffer(CodeBuffer* cb, int call_stub_estimate);
 193 
 194   // timers
 195   static void print_timers();
 196 
 197 #ifndef PRODUCT
 198   // debugging support.
 199   // produces a file named c1compileonly in the current directory with
 200   // directives to compile only the current method and it's inlines.
 201   // The file can be passed to the command line option -XX:Flags=<filename>
 202   void compile_only_this_method();
 203   void compile_only_this_scope(outputStream* st, IRScope* scope);
 204   void exclude_this_method();
 205 #endif // PRODUCT
 206 
 207   bool is_profiling() {
 208     return env()->comp_level() == CompLevel_full_profile ||
 209            env()->comp_level() == CompLevel_limited_profile;
 210   }
 211   bool count_invocations() { return is_profiling(); }
 212   bool count_backedges()   { return is_profiling(); }
 213 
 214   // Helpers for generation of profile information
 215   bool profile_branches() {
 216     return env()->comp_level() == CompLevel_full_profile &&
 217       C1UpdateMethodData && C1ProfileBranches;
 218   }
 219   bool profile_calls() {
 220     return env()->comp_level() == CompLevel_full_profile &&
 221       C1UpdateMethodData && C1ProfileCalls;
 222   }
 223   bool profile_inlined_calls() {
 224     return profile_calls() && C1ProfileInlinedCalls;
 225   }
 226   bool profile_checkcasts() {
 227     return env()->comp_level() == CompLevel_full_profile &&
 228       C1UpdateMethodData && C1ProfileCheckcasts;
 229   }
 230 };
 231 
 232 
 233 // Macro definitions for unified bailout-support
 234 // The methods bailout() and bailed_out() are present in all classes
 235 // that might bailout, but forward all calls to Compilation
 236 #define BAILOUT(msg)               { bailout(msg); return;              }
 237 #define BAILOUT_(msg, res)         { bailout(msg); return res;          }
 238 
 239 #define CHECK_BAILOUT()            { if (bailed_out()) return;          }
 240 #define CHECK_BAILOUT_(res)        { if (bailed_out()) return res;      }
 241 
 242 
 243 class InstructionMark: public StackObj {
 244  private:
 245   Compilation* _compilation;
 246   Instruction*  _previous;
 247 
 248  public:
 249   InstructionMark(Compilation* compilation, Instruction* instr) {
 250     _compilation = compilation;
 251     _previous = _compilation->set_current_instruction(instr);
 252   }
 253   ~InstructionMark() {
 254     _compilation->set_current_instruction(_previous);
 255   }
 256 };
 257 
 258 
 259 //----------------------------------------------------------------------
 260 // Base class for objects allocated by the compiler in the compilation arena
 261 class CompilationResourceObj ALLOCATION_SUPER_CLASS_SPEC {
 262  public:
 263   void* operator new(size_t size) { return Compilation::current()->arena()->Amalloc(size); }
 264   void* operator new(size_t size, Arena* arena) {
 265     return arena->Amalloc(size);
 266   }
 267   void  operator delete(void* p) {} // nothing to do
 268 };
 269 
 270 
 271 //----------------------------------------------------------------------
 272 // Class for aggregating exception handler information.
 273 
 274 // Effectively extends XHandlers class with PC offset of
 275 // potentially exception-throwing instruction.
 276 // This class is used at the end of the compilation to build the
 277 // ExceptionHandlerTable.
 278 class ExceptionInfo: public CompilationResourceObj {
 279  private:
 280   int             _pco;                // PC of potentially exception-throwing instruction
 281   XHandlers*      _exception_handlers; // flat list of exception handlers covering this PC
 282 
 283  public:
 284   ExceptionInfo(int pco, XHandlers* exception_handlers)
 285     : _pco(pco)
 286     , _exception_handlers(exception_handlers)
 287   { }
 288 
 289   int pco()                                      { return _pco; }
 290   XHandlers* exception_handlers()                { return _exception_handlers; }
 291 };