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