1 /*
   2  * Copyright (c) 1997, 2018, 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_TEMPLATETABLE_HPP
  26 #define SHARE_VM_INTERPRETER_TEMPLATETABLE_HPP
  27 
  28 #include "interpreter/bytecodes.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "runtime/frame.hpp"
  31 #include "utilities/macros.hpp"
  32 
  33 #ifndef CC_INTERP
  34 // All the necessary definitions used for (bytecode) template generation. Instead of
  35 // spreading the implementation functionality for each bytecode in the interpreter
  36 // and the snippet generator, a template is assigned to each bytecode which can be
  37 // used to generate the bytecode's implementation if needed.
  38 
  39 class BarrierSet;
  40 class InterpreterMacroAssembler;
  41 
  42 // A Template describes the properties of a code template for a given bytecode
  43 // and provides a generator to generate the code template.
  44 
  45 class Template {
  46  private:
  47   enum Flags {
  48     uses_bcp_bit,                                // set if template needs the bcp pointing to bytecode
  49     does_dispatch_bit,                           // set if template dispatches on its own
  50     calls_vm_bit,                                // set if template calls the vm
  51     wide_bit                                     // set if template belongs to a wide instruction
  52   };
  53 
  54   typedef void (*generator)(int arg);
  55 
  56   int       _flags;                              // describes interpreter template properties (bcp unknown)
  57   TosState  _tos_in;                             // tos cache state before template execution
  58   TosState  _tos_out;                            // tos cache state after  template execution
  59   generator _gen;                                // template code generator
  60   int       _arg;                                // argument for template code generator
  61 
  62   void      initialize(int flags, TosState tos_in, TosState tos_out, generator gen, int arg);
  63 
  64   friend class TemplateTable;
  65 
  66  public:
  67   Bytecodes::Code bytecode() const;
  68   bool      is_valid() const                     { return _gen != NULL; }
  69   bool      uses_bcp() const                     { return (_flags & (1 << uses_bcp_bit     )) != 0; }
  70   bool      does_dispatch() const                { return (_flags & (1 << does_dispatch_bit)) != 0; }
  71   bool      calls_vm() const                     { return (_flags & (1 << calls_vm_bit     )) != 0; }
  72   bool      is_wide() const                      { return (_flags & (1 << wide_bit         )) != 0; }
  73   TosState  tos_in() const                       { return _tos_in; }
  74   TosState  tos_out() const                      { return _tos_out; }
  75   void      generate(InterpreterMacroAssembler* masm);
  76 };
  77 
  78 
  79 // The TemplateTable defines all Templates and provides accessor functions
  80 // to get the template for a given bytecode.
  81 
  82 class TemplateTable: AllStatic {
  83  public:
  84   enum Operation { add, sub, mul, div, rem, _and, _or, _xor, shl, shr, ushr };
  85   enum Condition { equal, not_equal, less, less_equal, greater, greater_equal };
  86   enum CacheByte { f1_byte = 1, f2_byte = 2 };  // byte_no codes
  87   enum RewriteControl { may_rewrite, may_not_rewrite };  // control for fast code under CDS
  88 
  89  private:
  90   static bool            _is_initialized;        // true if TemplateTable has been initialized
  91   static Template        _template_table     [Bytecodes::number_of_codes];
  92   static Template        _template_table_wide[Bytecodes::number_of_codes];
  93 
  94   static Template*       _desc;                  // the current template to be generated
  95   static Bytecodes::Code bytecode()              { return _desc->bytecode(); }
  96 
  97   static BarrierSet*     _bs;                    // Cache the barrier set.
  98  public:
  99   //%note templates_1
 100   static InterpreterMacroAssembler* _masm;       // the assembler used when generating templates
 101 
 102  private:
 103 
 104   // special registers
 105   static inline Address at_bcp(int offset);
 106 
 107   // helpers
 108   static void unimplemented_bc();
 109   static void patch_bytecode(Bytecodes::Code bc, Register bc_reg,
 110                              Register temp_reg, bool load_bc_into_bc_reg = true, int byte_no = -1);
 111 
 112   // C calls
 113   static void call_VM(Register oop_result, address entry_point);
 114   static void call_VM(Register oop_result, address entry_point, Register arg_1);
 115   static void call_VM(Register oop_result, address entry_point, Register arg_1, Register arg_2);
 116   static void call_VM(Register oop_result, address entry_point, Register arg_1, Register arg_2, Register arg_3);
 117 
 118   // these overloadings are not presently used on SPARC:
 119   static void call_VM(Register oop_result, Register last_java_sp, address entry_point);
 120   static void call_VM(Register oop_result, Register last_java_sp, address entry_point, Register arg_1);
 121   static void call_VM(Register oop_result, Register last_java_sp, address entry_point, Register arg_1, Register arg_2);
 122   static void call_VM(Register oop_result, Register last_java_sp, address entry_point, Register arg_1, Register arg_2, Register arg_3);
 123 
 124   // bytecodes
 125   static void nop();
 126 
 127   static void aconst_null();
 128   static void iconst(int value);
 129   static void lconst(int value);
 130   static void fconst(int value);
 131   static void dconst(int value);
 132 
 133   static void bipush();
 134   static void sipush();
 135   static void ldc(bool wide);
 136   static void ldc2_w();
 137   static void fast_aldc(bool wide);
 138 
 139   static void locals_index(Register reg, int offset = 1);
 140   static void iload();
 141   static void fast_iload();
 142   static void fast_iload2();
 143   static void fast_icaload();
 144   static void lload();
 145   static void fload();
 146   static void dload();
 147   static void aload();
 148 
 149   static void locals_index_wide(Register reg);
 150   static void wide_iload();
 151   static void wide_lload();
 152   static void wide_fload();
 153   static void wide_dload();
 154   static void wide_aload();
 155 
 156   static void iaload();
 157   static void laload();
 158   static void faload();
 159   static void daload();
 160   static void aaload();
 161   static void baload();
 162   static void caload();
 163   static void saload();
 164 
 165   static void iload(int n);
 166   static void lload(int n);
 167   static void fload(int n);
 168   static void dload(int n);
 169   static void aload(int n);
 170   static void aload_0();
 171   static void nofast_aload_0();
 172   static void nofast_iload();
 173   static void iload_internal(RewriteControl rc = may_rewrite);
 174   static void aload_0_internal(RewriteControl rc = may_rewrite);
 175 
 176   static void istore();
 177   static void lstore();
 178   static void fstore();
 179   static void dstore();
 180   static void astore();
 181 
 182   static void wide_istore();
 183   static void wide_lstore();
 184   static void wide_fstore();
 185   static void wide_dstore();
 186   static void wide_astore();
 187 
 188   static void iastore();
 189   static void lastore();
 190   static void fastore();
 191   static void dastore();
 192   static void aastore();
 193   static void bastore();
 194   static void castore();
 195   static void sastore();
 196 
 197   static void istore(int n);
 198   static void lstore(int n);
 199   static void fstore(int n);
 200   static void dstore(int n);
 201   static void astore(int n);
 202 
 203   static void pop();
 204   static void pop2();
 205   static void dup();
 206   static void dup_x1();
 207   static void dup_x2();
 208   static void dup2();
 209   static void dup2_x1();
 210   static void dup2_x2();
 211   static void swap();
 212 
 213   static void iop2(Operation op);
 214   static void lop2(Operation op);
 215   static void fop2(Operation op);
 216   static void dop2(Operation op);
 217 
 218   static void idiv();
 219   static void irem();
 220 
 221   static void lmul();
 222   static void ldiv();
 223   static void lrem();
 224   static void lshl();
 225   static void lshr();
 226   static void lushr();
 227 
 228   static void ineg();
 229   static void lneg();
 230   static void fneg();
 231   static void dneg();
 232 
 233   static void iinc();
 234   static void wide_iinc();
 235   static void convert();
 236   static void lcmp();
 237 
 238   static void float_cmp (bool is_float, int unordered_result);
 239   static void float_cmp (int unordered_result);
 240   static void double_cmp(int unordered_result);
 241 
 242   static void count_calls(Register method, Register temp);
 243   static void branch(bool is_jsr, bool is_wide);
 244   static void if_0cmp   (Condition cc);
 245   static void if_icmp   (Condition cc);
 246   static void if_nullcmp(Condition cc);
 247   static void if_acmp   (Condition cc);
 248 
 249   static void _goto();
 250   static void jsr();
 251   static void ret();
 252   static void wide_ret();
 253 
 254   static void goto_w();
 255   static void jsr_w();
 256 
 257   static void tableswitch();
 258   static void lookupswitch();
 259   static void fast_linearswitch();
 260   static void fast_binaryswitch();
 261 
 262   static void _return(TosState state);
 263 
 264   static void resolve_cache_and_index(int byte_no,       // one of 1,2,11
 265                                       Register cache,    // output for CP cache
 266                                       Register index,    // output for CP index
 267                                       size_t index_size); // one of 1,2,4
 268   static void load_invoke_cp_cache_entry(int byte_no,
 269                                          Register method,
 270                                          Register itable_index,
 271                                          Register flags,
 272                                          bool is_invokevirtual,
 273                                          bool is_virtual_final,
 274                                          bool is_invokedynamic);
 275   static void load_field_cp_cache_entry(Register obj,
 276                                         Register cache,
 277                                         Register index,
 278                                         Register offset,
 279                                         Register flags,
 280                                         bool is_static);
 281   static void invokevirtual(int byte_no);
 282   static void invokespecial(int byte_no);
 283   static void invokestatic(int byte_no);
 284   static void invokeinterface(int byte_no);
 285   static void invokedynamic(int byte_no);
 286   static void invokehandle(int byte_no);
 287   static void fast_invokevfinal(int byte_no);
 288 
 289   static void getfield_or_static(int byte_no, bool is_static, RewriteControl rc = may_rewrite);
 290   static void putfield_or_static(int byte_no, bool is_static, RewriteControl rc = may_rewrite);
 291 
 292   static void getfield(int byte_no);
 293   static void putfield(int byte_no);
 294   static void nofast_getfield(int byte_no);
 295   static void nofast_putfield(int byte_no);
 296   static void getstatic(int byte_no);
 297   static void putstatic(int byte_no);
 298   static void pop_and_check_object(Register obj);
 299   static void condy_helper(Label& Done);  // shared by ldc instances
 300 
 301   static void _new();
 302   static void newarray();
 303   static void anewarray();
 304   static void arraylength();
 305   static void checkcast();
 306   static void instanceof();
 307 
 308   static void athrow();
 309 
 310   static void monitorenter();
 311   static void monitorexit();
 312 
 313   static void wide();
 314   static void multianewarray();
 315 
 316   static void fast_xaccess(TosState state);
 317   static void fast_accessfield(TosState state);
 318   static void fast_storefield(TosState state);
 319 
 320   static void _breakpoint();
 321 
 322   static void shouldnotreachhere();
 323 
 324   // jvmti support
 325   static void jvmti_post_field_access(Register cache, Register index, bool is_static, bool has_tos);
 326   static void jvmti_post_field_mod(Register cache, Register index, bool is_static);
 327   static void jvmti_post_fast_field_mod();
 328 
 329   // debugging of TemplateGenerator
 330   static void transition(TosState tos_in, TosState tos_out);// checks if in/out states expected by template generator correspond to table entries
 331 
 332   // initialization helpers
 333   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(            ), char filler );
 334   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(int arg     ), int arg     );
 335  static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(bool arg    ), bool arg    );
 336   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(TosState tos), TosState tos);
 337   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(Operation op), Operation op);
 338   static void def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(Condition cc), Condition cc);
 339 
 340   friend class Template;
 341 
 342   // InterpreterMacroAssembler::is_a(), etc., need TemplateTable::call_VM().
 343   friend class InterpreterMacroAssembler;
 344 
 345  public:
 346   // Initialization
 347   static void initialize();
 348   static void pd_initialize();
 349 
 350   // Templates
 351   static Template* template_for     (Bytecodes::Code code)  { Bytecodes::check     (code); return &_template_table     [code]; }
 352   static Template* template_for_wide(Bytecodes::Code code)  { Bytecodes::wide_check(code); return &_template_table_wide[code]; }
 353 
 354   // Platform specifics
 355 #include CPU_HEADER(templateTable)
 356 
 357 };
 358 #endif /* !CC_INTERP */
 359 
 360 #endif // SHARE_VM_INTERPRETER_TEMPLATETABLE_HPP