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