| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | * Jit control |
| 18 | */ |
| 19 | #ifndef _DALVIK_INTERP_JIT |
| 20 | #define _DALVIK_INTERP_JIT |
| 21 | |
| 22 | #include "InterpDefs.h" |
| Ben Cheng | 7b133ef | 2010-02-04 16:15:59 -0800 | [diff] [blame] | 23 | #include "mterp/common/jit-config.h" |
| Ben Cheng | 1efc9c5 | 2009-06-08 18:25:27 -0700 | [diff] [blame] | 24 | |
| 25 | #define JIT_MAX_TRACE_LEN 100 |
| 26 | |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 27 | #if defined (WITH_SELF_VERIFICATION) |
| 28 | |
| 29 | #define REG_SPACE 256 /* default size of shadow space */ |
| 30 | #define HEAP_SPACE JIT_MAX_TRACE_LEN /* default size of heap space */ |
| 31 | |
| 32 | typedef struct ShadowHeap { |
| 33 | int addr; |
| 34 | int data; |
| 35 | } ShadowHeap; |
| 36 | |
| 37 | typedef struct InstructionTrace { |
| 38 | int addr; |
| Ben Cheng | bcdc1de | 2009-08-21 16:18:46 -0700 | [diff] [blame] | 39 | DecodedInstruction decInsn; |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 40 | } InstructionTrace; |
| 41 | |
| 42 | typedef struct ShadowSpace { |
| 43 | const u2* startPC; /* starting pc of jitted region */ |
| 44 | const void* fp; /* starting fp of jitted region */ |
| Ben Cheng | d5adae1 | 2010-03-26 17:45:28 -0700 | [diff] [blame] | 45 | void* glue; /* starting glue of jitted region */ |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 46 | SelfVerificationState jitExitState; /* exit point for JIT'ed code */ |
| 47 | SelfVerificationState selfVerificationState; /* current SV running state */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 48 | const u2* endPC; /* ending pc of jitted region */ |
| 49 | void* shadowFP; /* pointer to fp in shadow space */ |
| 50 | InterpState interpState; /* copy of interpState */ |
| 51 | int* registerSpace; /* copy of register state */ |
| 52 | int registerSpaceSize; /* current size of register space */ |
| 53 | ShadowHeap heapSpace[HEAP_SPACE]; /* copy of heap space */ |
| 54 | ShadowHeap* heapSpaceTail; /* tail pointer to heapSpace */ |
| 55 | const void* endShadowFP; /* ending fp in shadow space */ |
| 56 | InstructionTrace trace[JIT_MAX_TRACE_LEN]; /* opcode trace for debugging */ |
| 57 | int traceLength; /* counter for current trace length */ |
| Ben Cheng | ccd6c01 | 2009-10-15 14:52:45 -0700 | [diff] [blame] | 58 | const Method* method; /* starting method of jitted region */ |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 59 | } ShadowSpace; |
| 60 | |
| 61 | /* |
| 62 | * Self verification functions. |
| 63 | */ |
| 64 | void* dvmSelfVerificationShadowSpaceAlloc(Thread* self); |
| 65 | void dvmSelfVerificationShadowSpaceFree(Thread* self); |
| 66 | void* dvmSelfVerificationSaveState(const u2* pc, const void* fp, |
| Bill Buzbee | 9a8c75a | 2009-11-08 14:31:20 -0800 | [diff] [blame] | 67 | InterpState* interpState, |
| 68 | int targetTrace); |
| Jeff Hao | 97319a8 | 2009-08-12 16:57:15 -0700 | [diff] [blame] | 69 | void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp, |
| 70 | SelfVerificationState exitPoint); |
| 71 | #endif |
| 72 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 73 | /* |
| 74 | * JitTable hash function. |
| 75 | */ |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 76 | |
| 77 | static inline u4 dvmJitHashMask( const u2* p, u4 mask ) { |
| 78 | return ((((u4)p>>12)^(u4)p)>>1) & (mask); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 81 | static inline u4 dvmJitHash( const u2* p ) { |
| 82 | return dvmJitHashMask( p, gDvmJit.jitTableMask ); |
| 83 | } |
| 84 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Entries in the JIT's address lookup hash table. |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 87 | * Fields which may be updated by multiple threads packed into a |
| 88 | * single 32-bit word to allow use of atomic update. |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 89 | */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 90 | |
| 91 | typedef struct JitEntryInfo { |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 92 | unsigned int traceConstruction:1; /* build underway? */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 93 | unsigned int isMethodEntry:1; |
| 94 | unsigned int inlineCandidate:1; |
| 95 | unsigned int profileEnabled:1; |
| 96 | JitInstructionSetType instructionSet:4; |
| 97 | unsigned int unused:8; |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 98 | u2 chain; /* Index of next in chain */ |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 99 | } JitEntryInfo; |
| 100 | |
| 101 | typedef union JitEntryInfoUnion { |
| 102 | JitEntryInfo info; |
| 103 | volatile int infoWord; |
| 104 | } JitEntryInfoUnion; |
| 105 | |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 106 | typedef struct JitEntry { |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 107 | JitEntryInfoUnion u; |
| 108 | const u2* dPC; /* Dalvik code address */ |
| 109 | void* codeAddress; /* Code address of native translation */ |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 110 | } JitEntry; |
| 111 | |
| Ben Cheng | 7a2697d | 2010-06-07 13:44:23 -0700 | [diff] [blame] | 112 | int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState, |
| 113 | const ClassObject *callsiteClass, const Method* curMethod); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 114 | void* dvmJitGetCodeAddr(const u2* dPC); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 115 | bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 116 | void dvmJitStopTranslationRequests(void); |
| 117 | void dvmJitStats(void); |
| 118 | bool dvmJitResizeJitTable(unsigned int size); |
| Ben Cheng | 60c24f4 | 2010-01-04 12:29:56 -0800 | [diff] [blame] | 119 | void dvmJitResetTable(void); |
| Bill Buzbee | 2717622 | 2009-06-09 09:20:16 -0700 | [diff] [blame] | 120 | struct JitEntry *dvmFindJitEntry(const u2* pc); |
| Bill Buzbee | 50a6bf2 | 2009-07-08 13:08:04 -0700 | [diff] [blame] | 121 | s8 dvmJitd2l(double d); |
| 122 | s8 dvmJitf2l(float f); |
| Bill Buzbee | 716f120 | 2009-07-23 13:22:09 -0700 | [diff] [blame] | 123 | void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set); |
| Bill Buzbee | d726991 | 2009-11-10 14:31:32 -0800 | [diff] [blame] | 124 | void dvmJitAbortTraceSelect(InterpState* interpState); |
| Ben Cheng | ba4fc8b | 2009-06-01 13:00:29 -0700 | [diff] [blame] | 125 | |
| 126 | #endif /*_DALVIK_INTERP_JIT*/ |