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