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