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