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