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