bytecodeInterpreter.hpp revision 1472:c18cbe5936b8
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 29union VMJavaVal64 { 30 jlong l; 31 jdouble d; 32 uint32_t v[2]; 33}; 34 35 36typedef class BytecodeInterpreter* interpreterState; 37 38struct 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 44struct osr_message { 45 address _osr_buf; /* the osr buffer */ 46 address _osr_entry; /* the entry to the osr method */ 47}; 48 49struct osr_result { 50 nmethod* nm; /* osr nmethod */ 51 address return_addr; /* osr blob return address */ 52}; 53 54// Result returned to frame manager 55union 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 62class BytecodeInterpreter : StackObj { 63friend class SharedRuntime; 64friend class AbstractInterpreterGenerator; 65friend class CppInterpreterGenerator; 66friend class InterpreterGenerator; 67friend class InterpreterMacroAssembler; 68friend class frame; 69friend class VMStructs; 70 71public: 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 91private: 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 108public: 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// 116static 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 */ 131union 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 */ 141union 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 */ 152typedef 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 */ 161union VMStackVal32 { 162 VMJavaVal32 j; /* For "Java" values */ 163 VMSlotVal32 s; /* any value from a "slot" or locals[] */ 164}; 165 166inline JavaThread* thread() { return _thread; } 167 168inline address bcp() { return _bcp; } 169inline void set_bcp(address new_bcp) { _bcp = new_bcp; } 170 171inline intptr_t* locals() { return _locals; } 172 173inline constantPoolCacheOop constants() { return _constants; } 174inline methodOop method() { return _method; } 175inline DataLayout* mdx() { return _mdx; } 176inline void set_mdx(DataLayout *new_mdx) { _mdx = new_mdx; } 177 178inline messages msg() { return _msg; } 179inline void set_msg(messages new_msg) { _msg = new_msg; } 180 181inline methodOop callee() { return _result._to_call._callee; } 182inline void set_callee(methodOop new_callee) { _result._to_call._callee = new_callee; } 183inline void set_callee_entry_point(address entry) { _result._to_call._callee_entry_point = entry; } 184inline void set_osr_buf(address buf) { _result._osr._osr_buf = buf; } 185inline void set_osr_entry(address entry) { _result._osr._osr_entry = entry; } 186inline int bcp_advance() { return _result._to_call._bcp_advance; } 187inline void set_bcp_advance(int count) { _result._to_call._bcp_advance = count; } 188 189inline void set_return_kind(Bytecodes::Code kind) { _result._return_kind = kind; } 190 191inline interpreterState prev() { return _prev_link; } 192 193inline intptr_t* stack() { return _stack; } 194inline void set_stack(intptr_t* new_stack) { _stack = new_stack; } 195 196 197inline intptr_t* stack_base() { return _stack_base; } 198inline intptr_t* stack_limit() { return _stack_limit; } 199 200inline 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 210static jlong VMlongAdd(jlong op1, jlong op2); 211static jlong VMlongAnd(jlong op1, jlong op2); 212static jlong VMlongDiv(jlong op1, jlong op2); 213static jlong VMlongMul(jlong op1, jlong op2); 214static jlong VMlongOr (jlong op1, jlong op2); 215static jlong VMlongSub(jlong op1, jlong op2); 216static jlong VMlongXor(jlong op1, jlong op2); 217static 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 226static jlong VMlongUshr(jlong op1, jint op2); 227static jlong VMlongShl (jlong op1, jint op2); 228static 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 237static jlong VMlongNeg(jlong op); 238 239/* 240 * Return the complement of "op" (~op) 241 */ 242 243static jlong VMlongNot(jlong op); 244 245 246/* 247 * Comparisons to 0: 248 */ 249 250static int32_t VMlongLtz(jlong op); /* op <= 0 */ 251static int32_t VMlongGez(jlong op); /* op >= 0 */ 252static int32_t VMlongEqz(jlong op); /* op == 0 */ 253 254/* 255 * Between operands: 256 */ 257 258static int32_t VMlongEq(jlong op1, jlong op2); /* op1 == op2 */ 259static int32_t VMlongNe(jlong op1, jlong op2); /* op1 != op2 */ 260static int32_t VMlongGe(jlong op1, jlong op2); /* op1 >= op2 */ 261static int32_t VMlongLe(jlong op1, jlong op2); /* op1 <= op2 */ 262static int32_t VMlongLt(jlong op1, jlong op2); /* op1 < op2 */ 263static 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 274static int32_t VMlongCompare(jlong op1, jlong op2); 275 276/* 277 * Convert int to long, according to "i2l" bytecode semantics 278 */ 279static jlong VMint2Long(jint val); 280 281/* 282 * Convert long to int, according to "l2i" bytecode semantics 283 */ 284static jint VMlong2Int(jlong val); 285 286/* 287 * Convert long to float, according to "l2f" bytecode semantics 288 */ 289static jfloat VMlong2Float(jlong val); 290 291/* 292 * Convert long to double, according to "l2d" bytecode semantics 293 */ 294static 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 308static jfloat VMfloatAdd(jfloat op1, jfloat op2); 309static jfloat VMfloatSub(jfloat op1, jfloat op2); 310static jfloat VMfloatMul(jfloat op1, jfloat op2); 311static jfloat VMfloatDiv(jfloat op1, jfloat op2); 312static 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 321static 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 332static 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 342static 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 358static jint VMdouble2Int(jdouble val); 359 360/* 361 * Convert double to float, according to "d2f" bytecode semantics 362 */ 363 364static jfloat VMdouble2Float(jdouble val); 365 366/* 367 * Convert int to double, according to "i2d" bytecode semantics 368 */ 369 370static 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 379static jdouble VMdoubleAdd(jdouble op1, jdouble op2); 380static jdouble VMdoubleSub(jdouble op1, jdouble op2); 381static jdouble VMdoubleDiv(jdouble op1, jdouble op2); 382static jdouble VMdoubleMul(jdouble op1, jdouble op2); 383static 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 392static 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 403static 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 416static 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 428static jint VMintAdd(jint op1, jint op2); 429static jint VMintSub(jint op1, jint op2); 430static jint VMintMul(jint op1, jint op2); 431static jint VMintDiv(jint op1, jint op2); 432static jint VMintRem(jint op1, jint op2); 433static jint VMintAnd(jint op1, jint op2); 434static jint VMintOr (jint op1, jint op2); 435static 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 443static jint VMintUshr(jint op, jint num); 444static jint VMintShl (jint op, jint num); 445static 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 454static jint VMintNeg(jint op); 455 456/* 457 * Int Conversions: 458 */ 459 460/* 461 * Convert int to float, according to "i2f" bytecode semantics 462 */ 463 464static jfloat VMint2Float(jint val); 465 466/* 467 * Convert int to byte, according to "i2b" bytecode semantics 468 */ 469 470static jbyte VMint2Byte(jint val); 471 472/* 473 * Convert int to char, according to "i2c" bytecode semantics 474 */ 475 476static jchar VMint2Char(jint val); 477 478/* 479 * Convert int to short, according to "i2s" bytecode semantics 480 */ 481 482static jshort VMint2Short(jint val); 483 484/*========================================================================= 485 * Bytecode interpreter operations 486 *=======================================================================*/ 487 488static void dup(intptr_t *tos); 489static void dup2(intptr_t *tos); 490static void dup_x1(intptr_t *tos); /* insert top word two down */ 491static void dup_x2(intptr_t *tos); /* insert top word three down */ 492static void dup2_x1(intptr_t *tos); /* insert top 2 slots three down */ 493static void dup2_x2(intptr_t *tos); /* insert top 2 slots four down */ 494static 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 499static void run(interpreterState istate); 500// The interpreter used if JVMTI needs interpreter events 501static void runWithChecks(interpreterState istate); 502static void End_Of_Interpreter(void); 503 504// Inline static functions for Java Stack and Local manipulation 505 506static address stack_slot(intptr_t *tos, int offset); 507static jint stack_int(intptr_t *tos, int offset); 508static jfloat stack_float(intptr_t *tos, int offset); 509static oop stack_object(intptr_t *tos, int offset); 510static jdouble stack_double(intptr_t *tos, int offset); 511static jlong stack_long(intptr_t *tos, int offset); 512 513// only used for value types 514static void set_stack_slot(intptr_t *tos, address value, int offset); 515static void set_stack_int(intptr_t *tos, int value, int offset); 516static void set_stack_float(intptr_t *tos, jfloat value, int offset); 517static void set_stack_object(intptr_t *tos, oop value, int offset); 518 519// needs to be platform dep for the 32 bit platforms. 520static void set_stack_double(intptr_t *tos, jdouble value, int offset); 521static void set_stack_long(intptr_t *tos, jlong value, int offset); 522 523static void set_stack_double_from_addr(intptr_t *tos, address addr, int offset); 524static void set_stack_long_from_addr(intptr_t *tos, address addr, int offset); 525 526// Locals 527 528static address locals_slot(intptr_t* locals, int offset); 529static jint locals_int(intptr_t* locals, int offset); 530static jfloat locals_float(intptr_t* locals, int offset); 531static oop locals_object(intptr_t* locals, int offset); 532static jdouble locals_double(intptr_t* locals, int offset); 533static jlong locals_long(intptr_t* locals, int offset); 534 535static address locals_long_at(intptr_t* locals, int offset); 536static address locals_double_at(intptr_t* locals, int offset); 537 538static void set_locals_slot(intptr_t *locals, address value, int offset); 539static void set_locals_int(intptr_t *locals, jint value, int offset); 540static void set_locals_float(intptr_t *locals, jfloat value, int offset); 541static void set_locals_object(intptr_t *locals, oop value, int offset); 542static void set_locals_double(intptr_t *locals, jdouble value, int offset); 543static void set_locals_long(intptr_t *locals, jlong value, int offset); 544static void set_locals_double_from_addr(intptr_t *locals, 545 address addr, int offset); 546static void set_locals_long_from_addr(intptr_t *locals, 547 address addr, int offset); 548 549static void astore(intptr_t* topOfStack, int stack_offset, 550 intptr_t* locals, int locals_offset); 551 552// Support for dup and swap 553static void copy_stack_slot(intptr_t *tos, int from_offset, int to_offset); 554 555#ifndef PRODUCT 556static const char* C_msg(BytecodeInterpreter::messages msg); 557void print(); 558#endif // PRODUCT 559 560 // Platform fields/methods 561# include "incls/_bytecodeInterpreter_pd.hpp.incl" 562 563}; // BytecodeInterpreter 564 565#endif // CC_INTERP 566