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