1 /*
   2  * Copyright (c) 2002, 2014, 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_BYTECODEINTERPRETER_HPP
  26 #define SHARE_VM_INTERPRETER_BYTECODEINTERPRETER_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "oops/methodData.hpp"
  30 #include "oops/method.hpp"
  31 #include "runtime/basicLock.hpp"
  32 #include "runtime/frame.hpp"
  33 #include "runtime/globals.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 
  36 #ifdef CC_INTERP
  37 
  38 // JavaStack Implementation
  39 #define MORE_STACK(count)  \
  40     (topOfStack -= ((count) * Interpreter::stackElementWords))
  41 
  42 // CVM definitions find hotspot equivalents...
  43 
  44 class InterpreterMacroAssembler;
  45 
  46 union VMJavaVal64 {
  47     jlong   l;
  48     jdouble d;
  49     uint32_t      v[2];
  50 };
  51 
  52 
  53 typedef class BytecodeInterpreter* interpreterState;
  54 
  55 struct call_message {
  56   class Method* _callee;           // method to call during call_method request
  57   address _callee_entry_point;     // address to jump to for call_method request
  58   int _bcp_advance;                // size of the invoke bytecode operation
  59 };
  60 
  61 struct osr_message {
  62   address _osr_buf;                 // the osr buffer
  63   address _osr_entry;               // the entry to the osr method
  64 };
  65 
  66 struct osr_result {
  67   nmethod* nm;                      // osr nmethod
  68   address return_addr;              // osr blob return address
  69 };
  70 
  71 // Result returned to frame manager
  72 union frame_manager_message {
  73   call_message _to_call;            // describes callee
  74   osr_message _osr;                 // describes the osr
  75   osr_result _osr_result;           // result of OSR request
  76 };
  77 
  78 class BytecodeInterpreter : StackObj {
  79 friend class SharedRuntime;
  80 friend class AbstractInterpreterGenerator;
  81 friend class CppInterpreterGenerator;
  82 friend class InterpreterGenerator;
  83 friend class InterpreterMacroAssembler;
  84 friend class frame;
  85 friend class VMStructs;
  86 
  87 public:
  88     enum messages {
  89          no_request = 0,            // unused
  90          initialize,                // Perform one time interpreter initializations (assumes all switches set)
  91          // status message to C++ interpreter
  92          method_entry,              // initial method entry to interpreter
  93          method_resume,             // frame manager response to return_from_method request (assuming a frame to resume)
  94          deopt_resume,              // returning from a native call into a deopted frame
  95          deopt_resume2,             // deopt resume as a result of a PopFrame
  96          got_monitors,              // frame manager response to more_monitors request
  97          rethrow_exception,         // unwinding and throwing exception
  98          // requests to frame manager from C++ interpreter
  99          call_method,               // request for new frame from interpreter, manager responds with method_entry
 100          return_from_method,        // request from interpreter to unwind, manager responds with method_continue
 101          more_monitors,             // need a new monitor
 102          throwing_exception,        // unwind stack and rethrow
 103          popping_frame,             // unwind call and retry call
 104          do_osr,                    // request this invocation be OSR's
 105          early_return               // early return as commanded by jvmti
 106     };
 107 
 108 private:
 109     JavaThread*           _thread;        // the vm's java thread pointer
 110     address               _bcp;           // instruction pointer
 111     intptr_t*             _locals;        // local variable pointer
 112     ConstantPoolCache*    _constants;     // constant pool cache
 113     Method*               _method;        // method being executed
 114     DataLayout*           _mdx;           // compiler profiling data for current bytecode
 115     intptr_t*             _stack;         // expression stack
 116     messages              _msg;           // frame manager <-> interpreter message
 117     frame_manager_message _result;        // result to frame manager
 118     interpreterState      _prev_link;     // previous interpreter state
 119     oop                   _oop_temp;      // mirror for interpreted native, null otherwise
 120     intptr_t*             _stack_base;    // base of expression stack
 121     intptr_t*             _stack_limit;   // limit of expression stack
 122     BasicObjectLock*      _monitor_base;  // base of monitors on the native stack
 123 
 124 
 125 public:
 126   // Constructor is only used by the initialization step. All other instances are created
 127   // by the frame manager.
 128   BytecodeInterpreter(messages msg);
 129 
 130 //
 131 // Deoptimization support
 132 //
 133 static void layout_interpreterState(interpreterState to_fill,
 134                                     frame* caller,
 135                                     frame* interpreter_frame,
 136                                     Method* method,
 137                                     intptr_t* locals,
 138                                     intptr_t* stack,
 139                                     intptr_t* stack_base,
 140                                     intptr_t* monitor_base,
 141                                     intptr_t* frame_bottom,
 142                                     bool top_frame);
 143 
 144 /*
 145  * Generic 32-bit wide "Java slot" definition. This type occurs
 146  * in operand stacks, Java locals, object fields, constant pools.
 147  */
 148 union VMJavaVal32 {
 149     jint     i;
 150     jfloat   f;
 151     class oopDesc*   r;
 152     uint32_t raw;
 153 };
 154 
 155 /*
 156  * Generic 64-bit Java value definition
 157  */
 158 union VMJavaVal64 {
 159     jlong   l;
 160     jdouble d;
 161     uint32_t      v[2];
 162 };
 163 
 164 /*
 165  * Generic 32-bit wide "Java slot" definition. This type occurs
 166  * in Java locals, object fields, constant pools, and
 167  * operand stacks (as a CVMStackVal32).
 168  */
 169 typedef union VMSlotVal32 {
 170     VMJavaVal32    j;     /* For "Java" values */
 171     address        a;     /* a return created by jsr or jsr_w */
 172 } VMSlotVal32;
 173 
 174 
 175 /*
 176  * Generic 32-bit wide stack slot definition.
 177  */
 178 union VMStackVal32 {
 179     VMJavaVal32    j;     /* For "Java" values */
 180     VMSlotVal32    s;     /* any value from a "slot" or locals[] */
 181 };
 182 
 183 inline JavaThread* thread() { return _thread; }
 184 
 185 inline address bcp() { return _bcp; }
 186 inline void set_bcp(address new_bcp) { _bcp = new_bcp; }
 187 
 188 inline intptr_t* locals() { return _locals; }
 189 
 190 inline ConstantPoolCache* constants() { return _constants; }
 191 inline Method* method() { return _method; }
 192 inline DataLayout* mdx() { return _mdx; }
 193 inline void set_mdx(DataLayout *new_mdx) { _mdx = new_mdx; }
 194 
 195 inline messages msg() { return _msg; }
 196 inline void set_msg(messages new_msg) { _msg = new_msg; }
 197 
 198 inline Method* callee() { return _result._to_call._callee; }
 199 inline void set_callee(Method* new_callee) { _result._to_call._callee = new_callee; }
 200 inline void set_callee_entry_point(address entry) { _result._to_call._callee_entry_point = entry; }
 201 inline void set_osr_buf(address buf) { _result._osr._osr_buf = buf; }
 202 inline void set_osr_entry(address entry) { _result._osr._osr_entry = entry; }
 203 inline int bcp_advance() { return _result._to_call._bcp_advance; }
 204 inline void set_bcp_advance(int count) { _result._to_call._bcp_advance = count; }
 205 
 206 inline interpreterState prev() { return _prev_link; }
 207 
 208 inline intptr_t* stack() { return _stack; }
 209 inline void set_stack(intptr_t* new_stack) { _stack = new_stack; }
 210 
 211 
 212 inline intptr_t* stack_base() { return _stack_base; }
 213 inline intptr_t* stack_limit() { return _stack_limit; }
 214 
 215 inline BasicObjectLock* monitor_base() { return _monitor_base; }
 216 
 217 /*
 218  * 64-bit Arithmetic:
 219  *
 220  * The functions below follow the semantics of the
 221  * ladd, land, ldiv, lmul, lor, lxor, and lrem bytecodes,
 222  * respectively.
 223  */
 224 
 225 static jlong VMlongAdd(jlong op1, jlong op2);
 226 static jlong VMlongAnd(jlong op1, jlong op2);
 227 static jlong VMlongDiv(jlong op1, jlong op2);
 228 static jlong VMlongMul(jlong op1, jlong op2);
 229 static jlong VMlongOr (jlong op1, jlong op2);
 230 static jlong VMlongSub(jlong op1, jlong op2);
 231 static jlong VMlongXor(jlong op1, jlong op2);
 232 static jlong VMlongRem(jlong op1, jlong op2);
 233 
 234 /*
 235  * Shift:
 236  *
 237  * The functions below follow the semantics of the
 238  * lushr, lshl, and lshr bytecodes, respectively.
 239  */
 240 
 241 static jlong VMlongUshr(jlong op1, jint op2);
 242 static jlong VMlongShl (jlong op1, jint op2);
 243 static jlong VMlongShr (jlong op1, jint op2);
 244 
 245 /*
 246  * Unary:
 247  *
 248  * Return the negation of "op" (-op), according to
 249  * the semantics of the lneg bytecode.
 250  */
 251 
 252 static jlong VMlongNeg(jlong op);
 253 
 254 /*
 255  * Return the complement of "op" (~op)
 256  */
 257 
 258 static jlong VMlongNot(jlong op);
 259 
 260 
 261 /*
 262  * Comparisons to 0:
 263  */
 264 
 265 static int32_t VMlongLtz(jlong op);     /* op <= 0 */
 266 static int32_t VMlongGez(jlong op);     /* op >= 0 */
 267 static int32_t VMlongEqz(jlong op);     /* op == 0 */
 268 
 269 /*
 270  * Between operands:
 271  */
 272 
 273 static int32_t VMlongEq(jlong op1, jlong op2);    /* op1 == op2 */
 274 static int32_t VMlongNe(jlong op1, jlong op2);    /* op1 != op2 */
 275 static int32_t VMlongGe(jlong op1, jlong op2);    /* op1 >= op2 */
 276 static int32_t VMlongLe(jlong op1, jlong op2);    /* op1 <= op2 */
 277 static int32_t VMlongLt(jlong op1, jlong op2);    /* op1 <  op2 */
 278 static int32_t VMlongGt(jlong op1, jlong op2);    /* op1 >  op2 */
 279 
 280 /*
 281  * Comparisons (returning an jint value: 0, 1, or -1)
 282  *
 283  * Between operands:
 284  *
 285  * Compare "op1" and "op2" according to the semantics of the
 286  * "lcmp" bytecode.
 287  */
 288 
 289 static int32_t VMlongCompare(jlong op1, jlong op2);
 290 
 291 /*
 292  * Convert int to long, according to "i2l" bytecode semantics
 293  */
 294 static jlong VMint2Long(jint val);
 295 
 296 /*
 297  * Convert long to int, according to "l2i" bytecode semantics
 298  */
 299 static jint VMlong2Int(jlong val);
 300 
 301 /*
 302  * Convert long to float, according to "l2f" bytecode semantics
 303  */
 304 static jfloat VMlong2Float(jlong val);
 305 
 306 /*
 307  * Convert long to double, according to "l2d" bytecode semantics
 308  */
 309 static jdouble VMlong2Double(jlong val);
 310 
 311 /*
 312  * Java floating-point float value manipulation.
 313  *
 314  * The result argument is, once again, an lvalue.
 315  *
 316  * Arithmetic:
 317  *
 318  * The functions below follow the semantics of the
 319  * fadd, fsub, fmul, fdiv, and frem bytecodes,
 320  * respectively.
 321  */
 322 
 323 static jfloat VMfloatAdd(jfloat op1, jfloat op2);
 324 static jfloat VMfloatSub(jfloat op1, jfloat op2);
 325 static jfloat VMfloatMul(jfloat op1, jfloat op2);
 326 static jfloat VMfloatDiv(jfloat op1, jfloat op2);
 327 static jfloat VMfloatRem(jfloat op1, jfloat op2);
 328 
 329 /*
 330  * Unary:
 331  *
 332  * Return the negation of "op" (-op), according to
 333  * the semantics of the fneg bytecode.
 334  */
 335 
 336 static jfloat VMfloatNeg(jfloat op);
 337 
 338 /*
 339  * Comparisons (returning an int value: 0, 1, or -1)
 340  *
 341  * Between operands:
 342  *
 343  * Compare "op1" and "op2" according to the semantics of the
 344  * "fcmpl" (direction is -1) or "fcmpg" (direction is 1) bytecodes.
 345  */
 346 
 347 static int32_t VMfloatCompare(jfloat op1, jfloat op2,
 348                               int32_t direction);
 349 /*
 350  * Conversion:
 351  */
 352 
 353 /*
 354  * Convert float to double, according to "f2d" bytecode semantics
 355  */
 356 
 357 static jdouble VMfloat2Double(jfloat op);
 358 
 359 /*
 360  ******************************************
 361  * Java double floating-point manipulation.
 362  ******************************************
 363  *
 364  * The result argument is, once again, an lvalue.
 365  *
 366  * Conversions:
 367  */
 368 
 369 /*
 370  * Convert double to int, according to "d2i" bytecode semantics
 371  */
 372 
 373 static jint VMdouble2Int(jdouble val);
 374 
 375 /*
 376  * Convert double to float, according to "d2f" bytecode semantics
 377  */
 378 
 379 static jfloat VMdouble2Float(jdouble val);
 380 
 381 /*
 382  * Convert int to double, according to "i2d" bytecode semantics
 383  */
 384 
 385 static jdouble VMint2Double(jint val);
 386 
 387 /*
 388  * Arithmetic:
 389  *
 390  * The functions below follow the semantics of the
 391  * dadd, dsub, ddiv, dmul, and drem bytecodes, respectively.
 392  */
 393 
 394 static jdouble VMdoubleAdd(jdouble op1, jdouble op2);
 395 static jdouble VMdoubleSub(jdouble op1, jdouble op2);
 396 static jdouble VMdoubleDiv(jdouble op1, jdouble op2);
 397 static jdouble VMdoubleMul(jdouble op1, jdouble op2);
 398 static jdouble VMdoubleRem(jdouble op1, jdouble op2);
 399 
 400 /*
 401  * Unary:
 402  *
 403  * Return the negation of "op" (-op), according to
 404  * the semantics of the dneg bytecode.
 405  */
 406 
 407 static jdouble VMdoubleNeg(jdouble op);
 408 
 409 /*
 410  * Comparisons (returning an int32_t value: 0, 1, or -1)
 411  *
 412  * Between operands:
 413  *
 414  * Compare "op1" and "op2" according to the semantics of the
 415  * "dcmpl" (direction is -1) or "dcmpg" (direction is 1) bytecodes.
 416  */
 417 
 418 static int32_t VMdoubleCompare(jdouble op1, jdouble op2, int32_t direction);
 419 
 420 /*
 421  * Copy two typeless 32-bit words from one location to another.
 422  * This is semantically equivalent to:
 423  *
 424  * to[0] = from[0];
 425  * to[1] = from[1];
 426  *
 427  * but this interface is provided for those platforms that could
 428  * optimize this into a single 64-bit transfer.
 429  */
 430 
 431 static void VMmemCopy64(uint32_t to[2], const uint32_t from[2]);
 432 
 433 
 434 // Arithmetic operations
 435 
 436 /*
 437  * Java arithmetic methods.
 438  * The functions below follow the semantics of the
 439  * iadd, isub, imul, idiv, irem, iand, ior, ixor,
 440  * and ineg bytecodes, respectively.
 441  */
 442 
 443 static jint VMintAdd(jint op1, jint op2);
 444 static jint VMintSub(jint op1, jint op2);
 445 static jint VMintMul(jint op1, jint op2);
 446 static jint VMintDiv(jint op1, jint op2);
 447 static jint VMintRem(jint op1, jint op2);
 448 static jint VMintAnd(jint op1, jint op2);
 449 static jint VMintOr (jint op1, jint op2);
 450 static jint VMintXor(jint op1, jint op2);
 451 
 452 /*
 453  * Shift Operation:
 454  * The functions below follow the semantics of the
 455  * iushr, ishl, and ishr bytecodes, respectively.
 456  */
 457 
 458 static juint VMintUshr(jint op, jint num);
 459 static jint VMintShl (jint op, jint num);
 460 static jint VMintShr (jint op, jint num);
 461 
 462 /*
 463  * Unary Operation:
 464  *
 465  * Return the negation of "op" (-op), according to
 466  * the semantics of the ineg bytecode.
 467  */
 468 
 469 static jint VMintNeg(jint op);
 470 
 471 /*
 472  * Int Conversions:
 473  */
 474 
 475 /*
 476  * Convert int to float, according to "i2f" bytecode semantics
 477  */
 478 
 479 static jfloat VMint2Float(jint val);
 480 
 481 /*
 482  * Convert int to byte, according to "i2b" bytecode semantics
 483  */
 484 
 485 static jbyte VMint2Byte(jint val);
 486 
 487 /*
 488  * Convert int to char, according to "i2c" bytecode semantics
 489  */
 490 
 491 static jchar VMint2Char(jint val);
 492 
 493 /*
 494  * Convert int to short, according to "i2s" bytecode semantics
 495  */
 496 
 497 static jshort VMint2Short(jint val);
 498 
 499 /*=========================================================================
 500  * Bytecode interpreter operations
 501  *=======================================================================*/
 502 
 503 static void dup(intptr_t *tos);
 504 static void dup2(intptr_t *tos);
 505 static void dup_x1(intptr_t *tos);    /* insert top word two down */
 506 static void dup_x2(intptr_t *tos);    /* insert top word three down  */
 507 static void dup2_x1(intptr_t *tos);   /* insert top 2 slots three down */
 508 static void dup2_x2(intptr_t *tos);   /* insert top 2 slots four down */
 509 static void swap(intptr_t *tos);      /* swap top two elements */
 510 
 511 // umm don't like this method modifies its object
 512 
 513 // The Interpreter used when
 514 static void run(interpreterState istate);
 515 // The interpreter used if JVMTI needs interpreter events
 516 static void runWithChecks(interpreterState istate);
 517 static void End_Of_Interpreter(void);
 518 
 519 // Inline static functions for Java Stack and Local manipulation
 520 
 521 static address stack_slot(intptr_t *tos, int offset);
 522 static jint stack_int(intptr_t *tos, int offset);
 523 static jfloat stack_float(intptr_t *tos, int offset);
 524 static oop stack_object(intptr_t *tos, int offset);
 525 static jdouble stack_double(intptr_t *tos, int offset);
 526 static jlong stack_long(intptr_t *tos, int offset);
 527 
 528 // only used for value types
 529 static void set_stack_slot(intptr_t *tos, address value, int offset);
 530 static void set_stack_int(intptr_t *tos, int value, int offset);
 531 static void set_stack_float(intptr_t *tos, jfloat value, int offset);
 532 static void set_stack_object(intptr_t *tos, oop value, int offset);
 533 
 534 // needs to be platform dep for the 32 bit platforms.
 535 static void set_stack_double(intptr_t *tos, jdouble value, int offset);
 536 static void set_stack_long(intptr_t *tos, jlong value, int offset);
 537 
 538 static void set_stack_double_from_addr(intptr_t *tos, address addr, int offset);
 539 static void set_stack_long_from_addr(intptr_t *tos, address addr, int offset);
 540 
 541 // Locals
 542 
 543 static address locals_slot(intptr_t* locals, int offset);
 544 static jint locals_int(intptr_t* locals, int offset);
 545 static jfloat locals_float(intptr_t* locals, int offset);
 546 static oop locals_object(intptr_t* locals, int offset);
 547 static jdouble locals_double(intptr_t* locals, int offset);
 548 static jlong locals_long(intptr_t* locals, int offset);
 549 
 550 static address locals_long_at(intptr_t* locals, int offset);
 551 static address locals_double_at(intptr_t* locals, int offset);
 552 
 553 static void set_locals_slot(intptr_t *locals, address value, int offset);
 554 static void set_locals_int(intptr_t *locals, jint value, int offset);
 555 static void set_locals_float(intptr_t *locals, jfloat value, int offset);
 556 static void set_locals_object(intptr_t *locals, oop value, int offset);
 557 static void set_locals_double(intptr_t *locals, jdouble value, int offset);
 558 static void set_locals_long(intptr_t *locals, jlong value, int offset);
 559 static void set_locals_double_from_addr(intptr_t *locals,
 560                                    address addr, int offset);
 561 static void set_locals_long_from_addr(intptr_t *locals,
 562                                    address addr, int offset);
 563 
 564 static void astore(intptr_t* topOfStack, int stack_offset,
 565                    intptr_t* locals,     int locals_offset);
 566 
 567 // Support for dup and swap
 568 static void copy_stack_slot(intptr_t *tos, int from_offset, int to_offset);
 569 
 570 #ifndef PRODUCT
 571 static const char* C_msg(BytecodeInterpreter::messages msg);
 572 void print();
 573 #endif // PRODUCT
 574 
 575     // Platform fields/methods
 576 #ifdef TARGET_ARCH_x86
 577 # include "bytecodeInterpreter_x86.hpp"
 578 #endif
 579 #ifdef TARGET_ARCH_sparc
 580 # include "bytecodeInterpreter_sparc.hpp"
 581 #endif
 582 #ifdef TARGET_ARCH_zero
 583 # include "bytecodeInterpreter_zero.hpp"
 584 #endif
 585 #ifdef TARGET_ARCH_arm
 586 # include "bytecodeInterpreter_arm.hpp"
 587 #endif
 588 #ifdef TARGET_ARCH_ppc
 589 # include "bytecodeInterpreter_ppc.hpp"
 590 #endif
 591 #ifdef TARGET_ARCH_aarch64
 592 # include "bytecodeInterpreter_aarch64.hpp"
 593 #endif
 594 
 595 
 596 }; // BytecodeInterpreter
 597 
 598 #endif // CC_INTERP
 599 
 600 #endif // SHARE_VM_INTERPRETER_BYTECODEINTERPRETER_HPP