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