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