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