blob: 144cd2a17f81ce62df55baf59c0537a9be0ed7fa [file] [log] [blame]
buzbee9f601a92011-02-11 17:48:20 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/*
17 * Dalvik interpreter definitions. These are internal to the interpreter.
18 *
19 * This includes defines, types, function declarations, and inline functions
20 * that are common to all interpreter implementations.
21 *
22 * Functions and globals declared here are defined in Interp.c.
23 */
24#ifndef _DALVIK_INTERP_STATE
25#define _DALVIK_INTERP_STATE
26
Carl Shapirodabd15a2011-04-13 20:14:49 -070027#ifdef __cplusplus
28extern "C" {
29#endif
30
buzbee9f601a92011-02-11 17:48:20 -080031/*
buzbee9a3147c2011-03-02 15:43:48 -080032 * Execution mode, e.g. interpreter vs. JIT.
33 */
34typedef enum ExecutionMode {
35 kExecutionModeUnknown = 0,
36 kExecutionModeInterpPortable,
37 kExecutionModeInterpFast,
38#if defined(WITH_JIT)
39 kExecutionModeJit,
40#endif
41} ExecutionMode;
42
43/*
44 * Execution sub modes, e.g. debugging, profiling, etc.
45 * Treated as bit flags for fast access. These values are used directly
46 * by assembly code in the mterp interpeter and may also be used by
47 * code generated by the JIT. Take care when changing.
48 */
49typedef enum ExecutionSubModes {
50 kSubModeNormal = 0x00,
51 kSubModeMethodTrace = 0x01,
52 kSubModeEmulatorTrace = 0x02,
53 kSubModeInstCounting = 0x04,
54 kSubModeDebuggerActive = 0x08,
55#if defined(WITH_JIT)
56 kSubModeJitTraceBuild = 0x10,
57 kSubModeJitSV = 0x20,
58#endif
59 kSubModeDebugProfile = (kSubModeMethodTrace |
60 kSubModeEmulatorTrace |
61 kSubModeInstCounting |
62 kSubModeDebuggerActive)
63} ExecutionSubModes;
64
65/*
66 * Interpreter break flags. When set, causes the interpreter to
67 * break from normal execution and invoke the associated callback
68 * handler.
69 */
70
71typedef enum InterpBreakFlags {
72 kInterpNoBreak = 0x00,
73 kInterpSuspendBreak = 0x01,
74 kInterpInstCountBreak = 0x02,
75 kInterpDebugBreak = 0x04,
76 kInterpEmulatorTraceBreak = 0x08,
77 kInterpSingleStep = 0x10,
buzbee94d65252011-03-24 16:41:03 -070078 kInterpSafePointCallback = 0x20,
buzbee9a3147c2011-03-02 15:43:48 -080079#if defined(WITH_JIT)
buzbee94d65252011-03-24 16:41:03 -070080 kInterpJitBreak = 0x40,
buzbee9a3147c2011-03-02 15:43:48 -080081#endif
82} InterpBreakFlags;
83
buzbee94d65252011-03-24 16:41:03 -070084typedef bool (*SafePointCallback)(struct Thread* thread, void* arg);
85
buzbee9a3147c2011-03-02 15:43:48 -080086/*
87 * Identify which break and submode flags should be local
88 * to an interpreter activation.
89 */
90#if defined(WITH_JIT)
91#define LOCAL_SUBMODE (kSubModeJitTraceBuild)
92#define LOCAL_BREAKFLAGS (kInterpJitBreak | kInterpSingleStep)
93#else
94#define LOCAL_SUBMODE (0)
95#define LOCAL_BREAKFLAGS (0)
96#endif
97
98
99/*
buzbee9f601a92011-02-11 17:48:20 -0800100 * Specify the starting point when switching between interpreters.
101 */
102typedef enum InterpEntry {
103 kInterpEntryInstr = 0, // continue to next instruction
104 kInterpEntryReturn = 1, // jump to method return
105 kInterpEntryThrow = 2, // jump to exception throw
106#if defined(WITH_JIT)
107 kInterpEntryResume = 3, // Resume after single-step
108#endif
109} InterpEntry;
110
111typedef struct InterpSaveState {
112 const u2* pc; // Dalvik PC
buzbee30bc0d42011-04-22 10:27:14 -0700113 u4* curFrame; // Dalvik frame pointer
buzbee9f601a92011-02-11 17:48:20 -0800114 const Method *method; // Method being executed
115 DvmDex* methodClassDex;
116 void* bailPtr;
buzbee9f601a92011-02-11 17:48:20 -0800117#if defined(WITH_TRACKREF_CHECKS)
118 int debugTrackedRefStart;
119#else
120 int unused; // Keep struct size constant
121#endif
122 struct InterpSaveState* prev; // To follow nested activations
123} InterpSaveState;
124
125#ifdef WITH_JIT
126/*
127 * NOTE: Only entry points dispatched via [self + #offset] are put
128 * in this struct, and there are six of them:
129 * 1) dvmJitToInterpNormal: find if there is a corresponding compilation for
130 * the new dalvik PC. If so, chain the originating compilation with the
131 * target then jump to it. If the destination trace doesn't exist, update
132 * the profile count for that Dalvik PC.
133 * 2) dvmJitToInterpNoChain: similar to dvmJitToInterpNormal but chaining is
134 * not performed.
135 * 3) dvmJitToInterpPunt: use the fast interpreter to execute the next
136 * instruction(s) and stay there as long as it is appropriate to return
137 * to the compiled land. This is used when the jit'ed code is about to
138 * throw an exception.
139 * 4) dvmJitToInterpSingleStep: use the portable interpreter to execute the
140 * next instruction only and return to pre-specified location in the
141 * compiled code to resume execution. This is mainly used as debugging
142 * feature to bypass problematic opcode implementations without
143 * disturbing the trace formation.
144 * 5) dvmJitToTraceSelect: Similar to dvmJitToInterpNormal except for the
145 * profiling operation. If the new Dalvik PC is dominated by an already
146 * translated trace, directly request a new translation if the destinaion
147 * trace doesn't exist.
148 * 6) dvmJitToBackwardBranch: special case for SELF_VERIFICATION when the
149 * destination Dalvik PC is included by the trace itself.
150 */
151struct JitToInterpEntries {
Carl Shapiro1813ab22011-04-15 15:48:54 -0700152 void (*dvmJitToInterpNormal)(void);
153 void (*dvmJitToInterpNoChain)(void);
154 void (*dvmJitToInterpPunt)(void);
155 void (*dvmJitToInterpSingleStep)(void);
156 void (*dvmJitToInterpTraceSelect)(void);
buzbee9f601a92011-02-11 17:48:20 -0800157#if defined(WITH_SELF_VERIFICATION)
Carl Shapiro1813ab22011-04-15 15:48:54 -0700158 void (*dvmJitToInterpBackwardBranch)(void);
buzbee9f601a92011-02-11 17:48:20 -0800159#else
Carl Shapiro1813ab22011-04-15 15:48:54 -0700160 void (*unused)(void); // Keep structure size constant
buzbee9f601a92011-02-11 17:48:20 -0800161#endif
162};
163
buzbee9a3147c2011-03-02 15:43:48 -0800164/* States of the interpreter when serving a JIT-related request */
buzbee9f601a92011-02-11 17:48:20 -0800165typedef enum JitState {
166 /* Entering states in the debug interpreter */
167 kJitNot = 0, // Non-JIT related reasons */
168 kJitTSelectRequest = 1, // Request a trace (subject to filtering)
169 kJitTSelectRequestHot = 2, // Request a hot trace (bypass the filter)
170 kJitSelfVerification = 3, // Self Verification Mode
171
172 /* Operational states in the debug interpreter */
173 kJitTSelect = 4, // Actively selecting a trace
174 kJitTSelectEnd = 5, // Done with the trace - wrap it up
buzbee9a3147c2011-03-02 15:43:48 -0800175 kJitDone = 6, // No further JIT actions for interpBreak
buzbee9f601a92011-02-11 17:48:20 -0800176} JitState;
177
178#if defined(WITH_SELF_VERIFICATION)
179typedef enum SelfVerificationState {
180 kSVSIdle = 0, // Idle
181 kSVSStart = 1, // Shadow space set up, running compiled code
182 kSVSPunt = 2, // Exiting compiled code by punting
183 kSVSSingleStep = 3, // Exiting compiled code by single stepping
184 kSVSNoProfile = 4, // Exiting compiled code and don't collect profiles
185 kSVSTraceSelect = 5, // Exiting compiled code and compile the next pc
186 kSVSNormal = 6, // Exiting compiled code normally
187 kSVSNoChain = 7, // Exiting compiled code by no chain
188 kSVSBackwardBranch = 8, // Exiting compiled code with backward branch trace
189 kSVSDebugInterp = 9, // Normal state restored, running debug interpreter
190} SelfVerificationState;
191#endif
192
193/* Number of entries in the 2nd level JIT profiler filter cache */
194#define JIT_TRACE_THRESH_FILTER_SIZE 32
195/* Number of low dalvik pc address bits to include in 2nd level filter key */
196#define JIT_TRACE_THRESH_FILTER_PC_BITS 4
197#define MAX_JIT_RUN_LEN 64
198
199typedef enum JitHint {
200 kJitHintNone = 0,
201 kJitHintTaken = 1, // Last inst in run was taken branch
202 kJitHintNotTaken = 2, // Last inst in run was not taken branch
203 kJitHintNoBias = 3, // Last inst in run was unbiased branch
204} jitHint;
205
206/*
207 * Element of a Jit trace description. If the isCode bit is set, it describes
208 * a contiguous sequence of Dalvik byte codes.
209 */
210typedef struct {
buzbee9f601a92011-02-11 17:48:20 -0800211 unsigned numInsts:8; // Number of Byte codes in run
212 unsigned runEnd:1; // Run ends with last byte code
Ben Cheng385828e2011-03-04 16:48:33 -0800213 jitHint hint:7; // Hint to apply to final code of run
buzbee9f601a92011-02-11 17:48:20 -0800214 u2 startOffset; // Starting offset for trace run
215} JitCodeDesc;
216
217/*
218 * A complete list of trace runs passed to the compiler looks like the
219 * following:
220 * frag1
221 * frag2
222 * frag3
223 * meta1
Ben Cheng385828e2011-03-04 16:48:33 -0800224 * :
225 * metan
buzbee9f601a92011-02-11 17:48:20 -0800226 * frag4
227 *
Ben Cheng385828e2011-03-04 16:48:33 -0800228 * frags 1-4 have the "isCode" field set and describe the location/length of
229 * real code traces, while metas 1-n are misc information.
buzbee9f601a92011-02-11 17:48:20 -0800230 * The meaning of the meta content is loosely defined. It is usually the code
231 * fragment right before the first meta field (frag3 in this case) to
232 * understand and parse them. Frag4 could be a dummy one with 0 "numInsts" but
233 * the "runEnd" field set.
234 *
235 * For example, if a trace run contains a method inlining target, the class
Ben Cheng385828e2011-03-04 16:48:33 -0800236 * descriptor/loader of "this" and the currently resolved method pointer are
237 * three instances of meta information stored there.
buzbee9f601a92011-02-11 17:48:20 -0800238 */
Ben Cheng385828e2011-03-04 16:48:33 -0800239typedef struct {
240 union {
241 JitCodeDesc frag;
242 void* meta;
243 } info;
244 u4 isCode:1;
245 u4 unused:31;
buzbee9f601a92011-02-11 17:48:20 -0800246} JitTraceRun;
247
248#endif
249
Carl Shapirodabd15a2011-04-13 20:14:49 -0700250#ifdef __cplusplus
251}
252#endif
253
buzbee9f601a92011-02-11 17:48:20 -0800254#endif /*_DALVIK_INTERP_STATE*/