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 BlockBegin;
  26 class CompilationResourceObj;
  27 class XHandlers;
  28 class ExceptionInfo;
  29 class DebugInformationRecorder;
  30 class FrameMap;
  31 class IR;
  32 class IRScope;
  33 class Instruction;
  34 class LinearScan;
  35 class OopMap;
  36 class LIR_Emitter;
  37 class LIR_Assembler;
  38 class CodeEmitInfo;
  39 class ciEnv;
  40 class ciMethod;
  41 class ValueStack;
  42 class LIR_OprDesc;
  43 class C1_MacroAssembler;
  44 class CFGPrinter;
  45 typedef LIR_OprDesc* LIR_Opr;
  46 
  47 
  48 define_array(BasicTypeArray, BasicType)
  49 define_stack(BasicTypeList, BasicTypeArray)
  50 
  51 define_array(ExceptionInfoArray, ExceptionInfo*)
  52 define_stack(ExceptionInfoList,  ExceptionInfoArray)
  53 
  54 class Compilation: public StackObj {
  55   friend class CompilationResourceObj;
  56  private:
  57   // compilation specifics
  58   Arena* _arena;
  59   int _next_id;
  60   int _next_block_id;
  61   AbstractCompiler*  _compiler;
  62   ciEnv*             _env;
  63   ciMethod*          _method;
  64   int                _osr_bci;
  65   IR*                _hir;
  66   int                _max_spills;
  67   FrameMap*          _frame_map;
  68   C1_MacroAssembler* _masm;
  69   bool               _has_exception_handlers;
  70   bool               _has_fpu_code;
  71   bool               _has_unsafe_access;
  72   const char*        _bailout_msg;
  73   ExceptionInfoList* _exception_info_list;
  74   ExceptionHandlerTable _exception_handler_table;
  75   ImplicitExceptionTable _implicit_exception_table;
  76   LinearScan*        _allocator;
  77   CodeOffsets        _offsets;
  78   CodeBuffer         _code;
  79 
  80   // compilation helpers
  81   void initialize();
  82   void build_hir();
  83   void emit_lir();
  84 
  85   void emit_code_epilog(LIR_Assembler* assembler);
  86   int  emit_code_body();
  87 
  88   int  compile_java_method();
  89   void install_code(int frame_size);
  90   void compile_method();
  91 
  92   void generate_exception_handler_table();
  93 
  94   ExceptionInfoList* exception_info_list() const { return _exception_info_list; }
  95   ExceptionHandlerTable* exception_handler_table() { return &_exception_handler_table; }
  96 
  97   LinearScan* allocator()                          { return _allocator;      }
  98   void        set_allocator(LinearScan* allocator) { _allocator = allocator; }
  99 
 100   Instruction*       _current_instruction;       // the instruction currently being processed
 101 #ifndef PRODUCT
 102   Instruction*       _last_instruction_printed;  // the last instruction printed during traversal
 103 #endif // PRODUCT
 104 
 105  public:
 106   // creation
 107   Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method,
 108               int osr_bci, BufferBlob* buffer_blob);
 109   ~Compilation();
 110 
 111 
 112   static Compilation* current() {
 113     return (Compilation*) ciEnv::current()->compiler_data();
 114   }
 115 
 116   // accessors
 117   ciEnv* env() const                             { return _env; }
 118   AbstractCompiler* compiler() const             { return _compiler; }
 119   bool has_exception_handlers() const            { return _has_exception_handlers; }
 120   bool has_fpu_code() const                      { return _has_fpu_code; }
 121   bool has_unsafe_access() const                 { return _has_unsafe_access; }
 122   ciMethod* method() const                       { return _method; }
 123   int osr_bci() const                            { return _osr_bci; }
 124   bool is_osr_compile() const                    { return osr_bci() >= 0; }
 125   IR* hir() const                                { return _hir; }
 126   int max_spills() const                         { return _max_spills; }
 127   FrameMap* frame_map() const                    { return _frame_map; }
 128   CodeBuffer* code()                             { return &_code; }
 129   C1_MacroAssembler* masm() const                { return _masm; }
 130   CodeOffsets* offsets()                         { return &_offsets; }
 131   Arena* arena()                                 { return _arena; }
 132 
 133   // Instruction ids
 134   int get_next_id()                              { return _next_id++; }
 135   int number_of_instructions() const             { return _next_id; }
 136 
 137   // BlockBegin ids
 138   int get_next_block_id()                        { return _next_block_id++; }
 139   int number_of_blocks() const                   { return _next_block_id; }
 140 
 141   // setters
 142   void set_has_exception_handlers(bool f)        { _has_exception_handlers = f; }
 143   void set_has_fpu_code(bool f)                  { _has_fpu_code = f; }
 144   void set_has_unsafe_access(bool f)             { _has_unsafe_access = f; }
 145   // Add a set of exception handlers covering the given PC offset
 146   void add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers);
 147   // Statistics gathering
 148   void notice_inlined_method(ciMethod* method);
 149 
 150   DebugInformationRecorder* debug_info_recorder() const; // = _env->debug_info();
 151   Dependencies* dependency_recorder() const; // = _env->dependencies()
 152   ImplicitExceptionTable* implicit_exception_table()     { return &_implicit_exception_table; }
 153 
 154   Instruction* current_instruction() const       { return _current_instruction; }
 155   Instruction* set_current_instruction(Instruction* instr) {
 156     Instruction* previous = _current_instruction;
 157     _current_instruction = instr;
 158     return previous;
 159   }
 160 
 161 #ifndef PRODUCT
 162   void maybe_print_current_instruction();
 163 #endif // PRODUCT
 164 
 165   // error handling
 166   void bailout(const char* msg);
 167   bool bailed_out() const                        { return _bailout_msg != NULL; }
 168   const char* bailout_msg() const                { return _bailout_msg; }
 169 
 170   static int desired_max_code_buffer_size() {
 171     return (int) NMethodSizeLimit;  // default 256K or 512K
 172   }
 173   static int desired_max_constant_size() {
 174     return (int) NMethodSizeLimit / 10;  // about 25K
 175   }
 176 
 177   static void setup_code_buffer(CodeBuffer* cb, int call_stub_estimate);
 178 
 179   // timers
 180   static void print_timers();
 181 
 182 #ifndef PRODUCT
 183   // debugging support.
 184   // produces a file named c1compileonly in the current directory with
 185   // directives to compile only the current method and it's inlines.
 186   // The file can be passed to the command line option -XX:Flags=<filename>
 187   void compile_only_this_method();
 188   void compile_only_this_scope(outputStream* st, IRScope* scope);
 189   void exclude_this_method();
 190 #endif // PRODUCT
 191 };
 192 
 193 
 194 // Macro definitions for unified bailout-support
 195 // The methods bailout() and bailed_out() are present in all classes
 196 // that might bailout, but forward all calls to Compilation
 197 #define BAILOUT(msg)               { bailout(msg); return;              }
 198 #define BAILOUT_(msg, res)         { bailout(msg); return res;          }
 199 
 200 #define CHECK_BAILOUT()            { if (bailed_out()) return;          }
 201 #define CHECK_BAILOUT_(res)        { if (bailed_out()) return res;      }
 202 
 203 
 204 class InstructionMark: public StackObj {
 205  private:
 206   Compilation* _compilation;
 207   Instruction*  _previous;
 208 
 209  public:
 210   InstructionMark(Compilation* compilation, Instruction* instr) {
 211     _compilation = compilation;
 212     _previous = _compilation->set_current_instruction(instr);
 213   }
 214   ~InstructionMark() {
 215     _compilation->set_current_instruction(_previous);
 216   }
 217 };
 218 
 219 
 220 //----------------------------------------------------------------------
 221 // Base class for objects allocated by the compiler in the compilation arena
 222 class CompilationResourceObj ALLOCATION_SUPER_CLASS_SPEC {
 223  public:
 224   void* operator new(size_t size) { return Compilation::current()->arena()->Amalloc(size); }
 225   void* operator new(size_t size, Arena* arena) {
 226     return arena->Amalloc(size);
 227   }
 228   void  operator delete(void* p) {} // nothing to do
 229 };
 230 
 231 
 232 //----------------------------------------------------------------------
 233 // Class for aggregating exception handler information.
 234 
 235 // Effectively extends XHandlers class with PC offset of
 236 // potentially exception-throwing instruction.
 237 // This class is used at the end of the compilation to build the
 238 // ExceptionHandlerTable.
 239 class ExceptionInfo: public CompilationResourceObj {
 240  private:
 241   int             _pco;                // PC of potentially exception-throwing instruction
 242   XHandlers*      _exception_handlers; // flat list of exception handlers covering this PC
 243 
 244  public:
 245   ExceptionInfo(int pco, XHandlers* exception_handlers)
 246     : _pco(pco)
 247     , _exception_handlers(exception_handlers)
 248   { }
 249 
 250   int pco()                                      { return _pco; }
 251   XHandlers* exception_handlers()                { return _exception_handlers; }
 252 };