1 /*
   2  * Copyright (c) 1997, 2014, 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 #ifndef SHARE_VM_INTERPRETER_TEMPLATEINTERPRETER_HPP
  26 #define SHARE_VM_INTERPRETER_TEMPLATEINTERPRETER_HPP
  27 
  28 #include "interpreter/abstractInterpreter.hpp"
  29 #include "interpreter/templateTable.hpp"
  30 
  31 // This file contains the platform-independent parts
  32 // of the template interpreter and the template interpreter generator.
  33 
  34 #ifndef CC_INTERP
  35 
  36 class InterpreterMacroAssembler;
  37 
  38 //------------------------------------------------------------------------------------------------------------------------
  39 // A little wrapper class to group tosca-specific entry points into a unit.
  40 // (tosca = Top-Of-Stack CAche)
  41 
  42 class EntryPoint VALUE_OBJ_CLASS_SPEC {
  43  private:
  44   address _entry[number_of_states];
  45 
  46  public:
  47   // Construction
  48   EntryPoint();
  49   EntryPoint(address bentry, address centry, address sentry, address aentry, address ientry, address lentry, address fentry, address dentry, address ventry);
  50 
  51   // Attributes
  52   address entry(TosState state) const;                // return target address for a given tosca state
  53   void    set_entry(TosState state, address entry);   // set    target address for a given tosca state
  54   void    print();
  55 
  56   // Comparison
  57   bool operator == (const EntryPoint& y);             // for debugging only
  58 };
  59 
  60 
  61 //------------------------------------------------------------------------------------------------------------------------
  62 // A little wrapper class to group tosca-specific dispatch tables into a unit.
  63 
  64 class DispatchTable VALUE_OBJ_CLASS_SPEC {
  65  public:
  66   enum { length = 1 << BitsPerByte };                 // an entry point for each byte value (also for undefined bytecodes)
  67 
  68  private:
  69   address _table[number_of_states][length];           // dispatch tables, indexed by tosca and bytecode
  70 
  71  public:
  72   // Attributes
  73   EntryPoint entry(int i) const;                      // return entry point for a given bytecode i
  74   void       set_entry(int i, EntryPoint& entry);     // set    entry point for a given bytecode i
  75   address*   table_for(TosState state)          { return _table[state]; }
  76   address*   table_for()                        { return table_for((TosState)0); }
  77   int        distance_from(address *table)      { return table - table_for(); }
  78   int        distance_from(TosState state)      { return distance_from(table_for(state)); }
  79 
  80   // Comparison
  81   bool operator == (DispatchTable& y);                // for debugging only
  82 };
  83 
  84 class TemplateInterpreter: public AbstractInterpreter {
  85   friend class VMStructs;
  86   friend class InterpreterMacroAssembler;
  87   friend class TemplateInterpreterGenerator;
  88   friend class InterpreterGenerator;
  89   friend class TemplateTable;
  90   // friend class Interpreter;
  91  public:
  92 
  93   enum MoreConstants {
  94     number_of_return_entries  = number_of_states,               // number of return entry points
  95     number_of_deopt_entries   = number_of_states,               // number of deoptimization entry points
  96     number_of_return_addrs    = number_of_states                // number of return addresses
  97   };
  98 
  99  protected:
 100 
 101   static address    _throw_ArrayIndexOutOfBoundsException_entry;
 102   static address    _throw_ArrayStoreException_entry;
 103   static address    _throw_ArithmeticException_entry;
 104   static address    _throw_ClassCastException_entry;
 105   static address    _throw_WrongMethodType_entry;
 106   static address    _throw_NullPointerException_entry;
 107   static address    _throw_exception_entry;
 108 
 109   static address    _throw_StackOverflowError_entry;
 110 
 111   static address    _remove_activation_entry;                   // continuation address if an exception is not handled by current frame
 112 #ifdef HOTSWAP
 113   static address    _remove_activation_preserving_args_entry;   // continuation address when current frame is being popped
 114 #endif // HOTSWAP
 115 
 116 #ifndef PRODUCT
 117   static EntryPoint _trace_code;
 118 #endif // !PRODUCT
 119   static EntryPoint _return_entry[number_of_return_entries];    // entry points to return to from a call
 120   static EntryPoint _earlyret_entry;                            // entry point to return early from a call
 121   static EntryPoint _deopt_entry[number_of_deopt_entries];      // entry points to return to from a deoptimization
 122   static EntryPoint _continuation_entry;
 123   static EntryPoint _safept_entry;
 124 
 125   static address _invoke_return_entry[number_of_return_addrs];           // for invokestatic, invokespecial, invokevirtual return entries
 126   static address _invokeinterface_return_entry[number_of_return_addrs];  // for invokeinterface return entries
 127   static address _invokedynamic_return_entry[number_of_return_addrs];    // for invokedynamic return entries
 128 
 129   static DispatchTable _active_table;                           // the active    dispatch table (used by the interpreter for dispatch)
 130   static DispatchTable _normal_table;                           // the normal    dispatch table (used to set the active table in normal mode)
 131   static DispatchTable _safept_table;                           // the safepoint dispatch table (used to set the active table for safepoints)
 132   static address       _wentry_point[DispatchTable::length];    // wide instructions only (vtos tosca always)
 133 
 134 
 135  public:
 136   // Initialization/debugging
 137   static void       initialize();
 138   // this only returns whether a pc is within generated code for the interpreter.
 139   static bool       contains(address pc)                        { return _code != NULL && _code->contains(pc); }
 140 
 141  public:
 142 
 143   static address    remove_activation_early_entry(TosState state) { return _earlyret_entry.entry(state); }
 144 #ifdef HOTSWAP
 145   static address    remove_activation_preserving_args_entry()   { return _remove_activation_preserving_args_entry; }
 146 #endif // HOTSWAP
 147 
 148   static address    remove_activation_entry()                   { return _remove_activation_entry; }
 149   static address    throw_exception_entry()                     { return _throw_exception_entry; }
 150   static address    throw_ArithmeticException_entry()           { return _throw_ArithmeticException_entry; }
 151   static address    throw_WrongMethodType_entry()               { return _throw_WrongMethodType_entry; }
 152   static address    throw_NullPointerException_entry()          { return _throw_NullPointerException_entry; }
 153   static address    throw_StackOverflowError_entry()            { return _throw_StackOverflowError_entry; }
 154 
 155   // Code generation
 156 #ifndef PRODUCT
 157   static address    trace_code    (TosState state)              { return _trace_code.entry(state); }
 158 #endif // !PRODUCT
 159   static address    continuation  (TosState state)              { return _continuation_entry.entry(state); }
 160   static address*   dispatch_table(TosState state)              { return _active_table.table_for(state); }
 161   static address*   dispatch_table()                            { return _active_table.table_for(); }
 162   static int        distance_from_dispatch_table(TosState state){ return _active_table.distance_from(state); }
 163   static address*   normal_table(TosState state)                { return _normal_table.table_for(state); }
 164   static address*   normal_table()                              { return _normal_table.table_for(); }
 165 
 166   // Support for invokes
 167   static address*   invoke_return_entry_table()                 { return _invoke_return_entry; }
 168   static address*   invokeinterface_return_entry_table()        { return _invokeinterface_return_entry; }
 169   static address*   invokedynamic_return_entry_table()          { return _invokedynamic_return_entry; }
 170   static int        TosState_as_index(TosState state);
 171 
 172   static address* invoke_return_entry_table_for(Bytecodes::Code code);
 173 
 174   static address deopt_entry(TosState state, int length);
 175   static address return_entry(TosState state, int length, Bytecodes::Code code);
 176 
 177   // Safepoint support
 178   static void       notice_safepoints();                        // stops the thread when reaching a safepoint
 179   static void       ignore_safepoints();                        // ignores safepoints
 180 
 181   // Deoptimization support
 182   // Compute the entry address for continuation after
 183   static address deopt_continue_after_entry(Method* method,
 184                                             address bcp,
 185                                             int callee_parameters,
 186                                             bool is_top_frame);
 187   // Deoptimization should reexecute this bytecode
 188   static bool    bytecode_should_reexecute(Bytecodes::Code code);
 189   // Compute the address for reexecution
 190   static address deopt_reexecute_entry(Method* method, address bcp);
 191 
 192 #ifdef TARGET_ARCH_x86
 193 # include "templateInterpreter_x86.hpp"
 194 #endif
 195 #ifdef TARGET_ARCH_sparc
 196 # include "templateInterpreter_sparc.hpp"
 197 #endif
 198 #ifdef TARGET_ARCH_zero
 199 # include "templateInterpreter_zero.hpp"
 200 #endif
 201 #ifdef TARGET_ARCH_arm
 202 # include "templateInterpreter_arm.hpp"
 203 #endif
 204 #ifdef TARGET_ARCH_ppc
 205 # include "templateInterpreter_ppc.hpp"
 206 #endif
 207 #ifdef TARGET_ARCH_aarch64
 208 # include "templateInterpreter_aarch64.hpp"
 209 #endif
 210 
 211 
 212 };
 213 
 214 #endif // !CC_INTERP
 215 
 216 #endif // SHARE_VM_INTERPRETER_TEMPLATEINTERPRETER_HPP