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