blob: f1ac1c2489ecf5315c43d3a26dad21e8f14554f6 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2008 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#ifdef WITH_JIT
17
18/*
19 * Target independent portion of Android's Jit
20 */
21
22#include "Dalvik.h"
23#include "Jit.h"
24
25
26#include "dexdump/OpCodeNames.h"
27#include <unistd.h>
28#include <pthread.h>
29#include <sys/time.h>
30#include <signal.h>
31#include "compiler/Compiler.h"
Bill Buzbee6e963e12009-06-17 16:56:19 -070032#include "compiler/CompilerUtility.h"
33#include "compiler/CompilerIR.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070034#include <errno.h>
35
Jeff Hao97319a82009-08-12 16:57:15 -070036#if defined(WITH_SELF_VERIFICATION)
37/* Allocate space for per-thread ShadowSpace data structures */
38void* dvmSelfVerificationShadowSpaceAlloc(Thread* self)
39{
40 self->shadowSpace = (ShadowSpace*) calloc(1, sizeof(ShadowSpace));
41 if (self->shadowSpace == NULL)
42 return NULL;
43
44 self->shadowSpace->registerSpaceSize = REG_SPACE;
45 self->shadowSpace->registerSpace =
46 (int*) calloc(self->shadowSpace->registerSpaceSize, sizeof(int));
47
48 return self->shadowSpace->registerSpace;
49}
50
51/* Free per-thread ShadowSpace data structures */
52void dvmSelfVerificationShadowSpaceFree(Thread* self)
53{
54 free(self->shadowSpace->registerSpace);
55 free(self->shadowSpace);
56}
57
58/*
59 * Save out PC, FP, InterpState, and registers to shadow space.
60 * Return a pointer to the shadow space for JIT to use.
61 */
62void* dvmSelfVerificationSaveState(const u2* pc, const void* fp,
63 void* interpStatePtr)
64{
65 Thread *self = dvmThreadSelf();
66 ShadowSpace *shadowSpace = self->shadowSpace;
67 InterpState *interpState = (InterpState *) interpStatePtr;
68 int preBytes = interpState->method->outsSize*4 + sizeof(StackSaveArea);
69 int postBytes = interpState->method->registersSize*4;
70
71 //LOGD("### selfVerificationSaveState(%d) pc: 0x%x fp: 0x%x",
72 // self->threadId, (int)pc, (int)fp);
73
74 if (shadowSpace->selfVerificationState != kSVSIdle) {
75 LOGD("~~~ Save: INCORRECT PREVIOUS STATE(%d): %d",
76 self->threadId, shadowSpace->selfVerificationState);
77 LOGD("********** SHADOW STATE DUMP **********");
78 LOGD("* PC: 0x%x FP: 0x%x", (int)pc, (int)fp);
79 }
80 shadowSpace->selfVerificationState = kSVSStart;
81
82 // Dynamically grow shadow register space if necessary
83 while (preBytes + postBytes > shadowSpace->registerSpaceSize) {
84 shadowSpace->registerSpaceSize *= 2;
85 free(shadowSpace->registerSpace);
86 shadowSpace->registerSpace =
87 (int*) calloc(shadowSpace->registerSpaceSize, sizeof(int));
88 }
89
90 // Remember original state
91 shadowSpace->startPC = pc;
92 shadowSpace->fp = fp;
93 shadowSpace->glue = interpStatePtr;
94 shadowSpace->shadowFP = shadowSpace->registerSpace +
95 shadowSpace->registerSpaceSize - postBytes/4;
96
97 // Create a copy of the InterpState
98 memcpy(&(shadowSpace->interpState), interpStatePtr, sizeof(InterpState));
99 shadowSpace->interpState.fp = shadowSpace->shadowFP;
100 shadowSpace->interpState.interpStackEnd = (u1*)shadowSpace->registerSpace;
101
102 // Create a copy of the stack
103 memcpy(((char*)shadowSpace->shadowFP)-preBytes, ((char*)fp)-preBytes,
104 preBytes+postBytes);
105
106 // Setup the shadowed heap space
107 shadowSpace->heapSpaceTail = shadowSpace->heapSpace;
108
109 // Reset trace length
110 shadowSpace->traceLength = 0;
111
112 return shadowSpace;
113}
114
115/*
116 * Save ending PC, FP and compiled code exit point to shadow space.
117 * Return a pointer to the shadow space for JIT to restore state.
118 */
119void* dvmSelfVerificationRestoreState(const u2* pc, const void* fp,
120 SelfVerificationState exitPoint)
121{
122 Thread *self = dvmThreadSelf();
123 ShadowSpace *shadowSpace = self->shadowSpace;
124 shadowSpace->endPC = pc;
125 shadowSpace->endShadowFP = fp;
126
127 //LOGD("### selfVerificationRestoreState(%d) pc: 0x%x fp: 0x%x endPC: 0x%x",
128 // self->threadId, (int)shadowSpace->startPC, (int)shadowSpace->fp,
129 // (int)pc);
130
131 if (shadowSpace->selfVerificationState != kSVSStart) {
132 LOGD("~~~ Restore: INCORRECT PREVIOUS STATE(%d): %d",
133 self->threadId, shadowSpace->selfVerificationState);
134 LOGD("********** SHADOW STATE DUMP **********");
135 LOGD("* Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
136 (int)shadowSpace->endPC);
137 LOGD("* Interp FP: 0x%x", (int)shadowSpace->fp);
138 LOGD("* Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
139 (int)shadowSpace->endShadowFP);
140 }
141
142 // Special case when punting after a single instruction
143 if (exitPoint == kSVSPunt && pc == shadowSpace->startPC) {
144 shadowSpace->selfVerificationState = kSVSIdle;
145 } else {
146 shadowSpace->selfVerificationState = exitPoint;
147 }
148
149 return shadowSpace;
150}
151
152/* Print contents of virtual registers */
153static void selfVerificationPrintRegisters(int* addr, int numWords)
154{
155 int i;
156 for (i = 0; i < numWords; i++) {
157 LOGD("* 0x%x: (v%d) 0x%8x", (int)(addr+i), i, *(addr+i));
158 }
159}
160
161/* Print values maintained in shadowSpace */
162static void selfVerificationDumpState(const u2* pc, Thread* self)
163{
164 ShadowSpace* shadowSpace = self->shadowSpace;
165 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
166 int frameBytes = (int) shadowSpace->registerSpace +
167 shadowSpace->registerSpaceSize*4 -
168 (int) shadowSpace->shadowFP;
169 int localRegs = 0;
170 int frameBytes2 = 0;
171 if (self->curFrame < shadowSpace->fp) {
172 localRegs = (stackSave->method->registersSize -
173 stackSave->method->insSize)*4;
174 frameBytes2 = (int) shadowSpace->fp - (int) self->curFrame - localRegs;
175 }
176 LOGD("********** SHADOW STATE DUMP **********");
177 LOGD("* CurrentPC: 0x%x, Offset: 0x%04x", (int)pc,
178 (int)(pc - stackSave->method->insns));
179 LOGD("* Class: %s Method: %s", stackSave->method->clazz->descriptor,
180 stackSave->method->name);
181 LOGD("* Dalvik PC: 0x%x endPC: 0x%x", (int)shadowSpace->startPC,
182 (int)shadowSpace->endPC);
183 LOGD("* Interp FP: 0x%x endFP: 0x%x", (int)shadowSpace->fp,
184 (int)self->curFrame);
185 LOGD("* Shadow FP: 0x%x endFP: 0x%x", (int)shadowSpace->shadowFP,
186 (int)shadowSpace->endShadowFP);
187 LOGD("* Frame1 Bytes: %d Frame2 Local: %d Bytes: %d", frameBytes,
188 localRegs, frameBytes2);
189 LOGD("* Trace length: %d State: %d", shadowSpace->traceLength,
190 shadowSpace->selfVerificationState);
191}
192
193/* Print decoded instructions in the current trace */
194static void selfVerificationDumpTrace(const u2* pc, Thread* self)
195{
196 ShadowSpace* shadowSpace = self->shadowSpace;
197 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700198 int i, addr, offset;
199 DecodedInstruction *decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700200
201 LOGD("********** SHADOW TRACE DUMP **********");
202 for (i = 0; i < shadowSpace->traceLength; i++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700203 addr = shadowSpace->trace[i].addr;
204 offset = (int)((u2*)addr - stackSave->method->insns);
205 decInsn = &(shadowSpace->trace[i].decInsn);
206 /* Not properly decoding instruction, some registers may be garbage */
207 LOGD("* 0x%x: (0x%04x) %s v%d, v%d, v%d", addr, offset,
208 getOpcodeName(decInsn->opCode), decInsn->vA, decInsn->vB,
209 decInsn->vC);
Jeff Hao97319a82009-08-12 16:57:15 -0700210 }
211}
212
Ben Chengbcdc1de2009-08-21 16:18:46 -0700213/* Code is forced into this spin loop when a divergence is detected */
214static void selfVerificationSpinLoop()
215{
216 gDvmJit.selfVerificationSpin = true;
217 while(gDvmJit.selfVerificationSpin) sleep(10);
218}
219
Jeff Hao97319a82009-08-12 16:57:15 -0700220/* Manage self verification while in the debug interpreter */
221static bool selfVerificationDebugInterp(const u2* pc, Thread* self)
222{
223 ShadowSpace *shadowSpace = self->shadowSpace;
Jeff Hao97319a82009-08-12 16:57:15 -0700224 SelfVerificationState state = shadowSpace->selfVerificationState;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700225
226 DecodedInstruction decInsn;
227 dexDecodeInstruction(gDvm.instrFormat, pc, &decInsn);
228
Jeff Hao97319a82009-08-12 16:57:15 -0700229 //LOGD("### DbgIntp(%d): PC: 0x%x endPC: 0x%x state: %d len: %d %s",
230 // self->threadId, (int)pc, (int)shadowSpace->endPC, state,
Ben Chengbcdc1de2009-08-21 16:18:46 -0700231 // shadowSpace->traceLength, getOpcodeName(decInsn.opCode));
Jeff Hao97319a82009-08-12 16:57:15 -0700232
233 if (state == kSVSIdle || state == kSVSStart) {
234 LOGD("~~~ DbgIntrp: INCORRECT PREVIOUS STATE(%d): %d",
235 self->threadId, state);
236 selfVerificationDumpState(pc, self);
237 selfVerificationDumpTrace(pc, self);
238 }
239
240 /* Skip endPC once when trace has a backward branch */
241 if ((state == kSVSBackwardBranch && pc == shadowSpace->endPC) ||
242 state != kSVSBackwardBranch) {
243 shadowSpace->selfVerificationState = kSVSDebugInterp;
244 }
245
246 /* Check that the current pc is the end of the trace */
247 if ((state == kSVSSingleStep || state == kSVSDebugInterp) &&
248 pc == shadowSpace->endPC) {
249
250 shadowSpace->selfVerificationState = kSVSIdle;
251
252 /* Check register space */
253 int frameBytes = (int) shadowSpace->registerSpace +
254 shadowSpace->registerSpaceSize*4 -
255 (int) shadowSpace->shadowFP;
256 if (memcmp(shadowSpace->fp, shadowSpace->shadowFP, frameBytes)) {
257 LOGD("~~~ DbgIntp(%d): REGISTERS UNEQUAL!", self->threadId);
258 selfVerificationDumpState(pc, self);
259 selfVerificationDumpTrace(pc, self);
260 LOGD("*** Interp Registers: addr: 0x%x bytes: %d",
261 (int)shadowSpace->fp, frameBytes);
262 selfVerificationPrintRegisters((int*)shadowSpace->fp, frameBytes/4);
263 LOGD("*** Shadow Registers: addr: 0x%x bytes: %d",
264 (int)shadowSpace->shadowFP, frameBytes);
265 selfVerificationPrintRegisters((int*)shadowSpace->shadowFP,
266 frameBytes/4);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700267 selfVerificationSpinLoop();
Jeff Hao97319a82009-08-12 16:57:15 -0700268 }
269 /* Check new frame if it exists (invokes only) */
270 if (self->curFrame < shadowSpace->fp) {
271 StackSaveArea* stackSave = SAVEAREA_FROM_FP(self->curFrame);
272 int localRegs = (stackSave->method->registersSize -
273 stackSave->method->insSize)*4;
274 int frameBytes2 = (int) shadowSpace->fp -
275 (int) self->curFrame - localRegs;
276 if (memcmp(((char*)self->curFrame)+localRegs,
277 ((char*)shadowSpace->endShadowFP)+localRegs, frameBytes2)) {
278 LOGD("~~~ DbgIntp(%d): REGISTERS (FRAME2) UNEQUAL!",
279 self->threadId);
280 selfVerificationDumpState(pc, self);
281 selfVerificationDumpTrace(pc, self);
282 LOGD("*** Interp Registers: addr: 0x%x l: %d bytes: %d",
283 (int)self->curFrame, localRegs, frameBytes2);
284 selfVerificationPrintRegisters((int*)self->curFrame,
285 (frameBytes2+localRegs)/4);
286 LOGD("*** Shadow Registers: addr: 0x%x l: %d bytes: %d",
287 (int)shadowSpace->endShadowFP, localRegs, frameBytes2);
288 selfVerificationPrintRegisters((int*)shadowSpace->endShadowFP,
289 (frameBytes2+localRegs)/4);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700290 selfVerificationSpinLoop();
Jeff Hao97319a82009-08-12 16:57:15 -0700291 }
292 }
293
294 /* Check memory space */
Ben Chengbcdc1de2009-08-21 16:18:46 -0700295 bool memDiff = false;
Jeff Hao97319a82009-08-12 16:57:15 -0700296 ShadowHeap* heapSpacePtr;
297 for (heapSpacePtr = shadowSpace->heapSpace;
298 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
Ben Chengbcdc1de2009-08-21 16:18:46 -0700299 int memData = *((unsigned int*) heapSpacePtr->addr);
300 if (heapSpacePtr->data != memData) {
Jeff Hao97319a82009-08-12 16:57:15 -0700301 LOGD("~~~ DbgIntp(%d): MEMORY UNEQUAL!", self->threadId);
302 LOGD("* Addr: 0x%x Intrp Data: 0x%x Jit Data: 0x%x",
Ben Chengbcdc1de2009-08-21 16:18:46 -0700303 heapSpacePtr->addr, memData, heapSpacePtr->data);
Jeff Hao97319a82009-08-12 16:57:15 -0700304 selfVerificationDumpState(pc, self);
305 selfVerificationDumpTrace(pc, self);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700306 memDiff = true;
Jeff Hao97319a82009-08-12 16:57:15 -0700307 }
308 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700309 if (memDiff) selfVerificationSpinLoop();
Jeff Hao97319a82009-08-12 16:57:15 -0700310 return true;
311
312 /* If end not been reached, make sure max length not exceeded */
313 } else if (shadowSpace->traceLength >= JIT_MAX_TRACE_LEN) {
314 LOGD("~~~ DbgIntp(%d): CONTROL DIVERGENCE!", self->threadId);
315 LOGD("* startPC: 0x%x endPC: 0x%x currPC: 0x%x",
316 (int)shadowSpace->startPC, (int)shadowSpace->endPC, (int)pc);
317 selfVerificationDumpState(pc, self);
318 selfVerificationDumpTrace(pc, self);
Ben Chengbcdc1de2009-08-21 16:18:46 -0700319 selfVerificationSpinLoop();
Jeff Hao97319a82009-08-12 16:57:15 -0700320
321 return true;
322 }
Ben Chengbcdc1de2009-08-21 16:18:46 -0700323 /* Log the instruction address and decoded instruction for debug */
Jeff Hao97319a82009-08-12 16:57:15 -0700324 shadowSpace->trace[shadowSpace->traceLength].addr = (int)pc;
Ben Chengbcdc1de2009-08-21 16:18:46 -0700325 shadowSpace->trace[shadowSpace->traceLength].decInsn = decInsn;
Jeff Hao97319a82009-08-12 16:57:15 -0700326 shadowSpace->traceLength++;
327
328 return false;
329}
330#endif
331
Ben Chengba4fc8b2009-06-01 13:00:29 -0700332int dvmJitStartup(void)
333{
334 unsigned int i;
335 bool res = true; /* Assume success */
336
337 // Create the compiler thread and setup miscellaneous chores */
338 res &= dvmCompilerStartup();
339
340 dvmInitMutex(&gDvmJit.tableLock);
341 if (res && gDvm.executionMode == kExecutionModeJit) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700342 JitEntry *pJitTable = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700343 unsigned char *pJitProfTable = NULL;
Ben Cheng3f02aa42009-08-14 13:52:09 -0700344 // Power of 2?
345 assert(gDvmJit.jitTableSize &&
346 !(gDvmJit.jitTableSize & (gDvmJit.jitTableSize - 1)));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700347 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee716f1202009-07-23 13:22:09 -0700348 pJitTable = (JitEntry*)
Bill Buzbee27176222009-06-09 09:20:16 -0700349 calloc(gDvmJit.jitTableSize, sizeof(*pJitTable));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700350 if (!pJitTable) {
351 LOGE("jit table allocation failed\n");
352 res = false;
353 goto done;
354 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700355 /*
356 * NOTE: the profile table must only be allocated once, globally.
357 * Profiling is turned on and off by nulling out gDvm.pJitProfTable
358 * and then restoring its original value. However, this action
359 * is not syncronized for speed so threads may continue to hold
360 * and update the profile table after profiling has been turned
361 * off by null'ng the global pointer. Be aware.
362 */
363 pJitProfTable = (unsigned char *)malloc(JIT_PROF_SIZE);
364 if (!pJitProfTable) {
365 LOGE("jit prof table allocation failed\n");
366 res = false;
367 goto done;
368 }
369 memset(pJitProfTable,0,JIT_PROF_SIZE);
Bill Buzbee27176222009-06-09 09:20:16 -0700370 for (i=0; i < gDvmJit.jitTableSize; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700371 pJitTable[i].u.info.chain = gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700372 }
373 /* Is chain field wide enough for termination pattern? */
Ben Cheng3f02aa42009-08-14 13:52:09 -0700374 assert(pJitTable[0].u.info.chain == gDvmJit.jitTableSize);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700375
376done:
377 gDvmJit.pJitEntryTable = pJitTable;
Bill Buzbee27176222009-06-09 09:20:16 -0700378 gDvmJit.jitTableMask = gDvmJit.jitTableSize - 1;
379 gDvmJit.jitTableEntriesUsed = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700380 gDvmJit.pProfTableCopy = gDvmJit.pProfTable = pJitProfTable;
381 dvmUnlockMutex(&gDvmJit.tableLock);
382 }
383 return res;
384}
385
386/*
387 * If one of our fixed tables or the translation buffer fills up,
388 * call this routine to avoid wasting cycles on future translation requests.
389 */
390void dvmJitStopTranslationRequests()
391{
392 /*
393 * Note 1: This won't necessarily stop all translation requests, and
394 * operates on a delayed mechanism. Running threads look to the copy
395 * of this value in their private InterpState structures and won't see
396 * this change until it is refreshed (which happens on interpreter
397 * entry).
398 * Note 2: This is a one-shot memory leak on this table. Because this is a
399 * permanent off switch for Jit profiling, it is a one-time leak of 1K
400 * bytes, and no further attempt will be made to re-allocate it. Can't
401 * free it because some thread may be holding a reference.
402 */
403 gDvmJit.pProfTable = gDvmJit.pProfTableCopy = NULL;
404}
405
406#if defined(EXIT_STATS)
407/* Convenience function to increment counter from assembly code */
408void dvmBumpNoChain()
409{
410 gDvm.jitNoChainExit++;
411}
412
413/* Convenience function to increment counter from assembly code */
414void dvmBumpNormal()
415{
416 gDvm.jitNormalExit++;
417}
418
419/* Convenience function to increment counter from assembly code */
420void dvmBumpPunt(int from)
421{
422 gDvm.jitPuntExit++;
423}
424#endif
425
426/* Dumps debugging & tuning stats to the log */
427void dvmJitStats()
428{
429 int i;
430 int hit;
431 int not_hit;
432 int chains;
433 if (gDvmJit.pJitEntryTable) {
434 for (i=0, chains=hit=not_hit=0;
Bill Buzbee27176222009-06-09 09:20:16 -0700435 i < (int) gDvmJit.jitTableSize;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700436 i++) {
437 if (gDvmJit.pJitEntryTable[i].dPC != 0)
438 hit++;
439 else
440 not_hit++;
Bill Buzbee716f1202009-07-23 13:22:09 -0700441 if (gDvmJit.pJitEntryTable[i].u.info.chain != gDvmJit.jitTableSize)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700442 chains++;
443 }
444 LOGD(
445 "JIT: %d traces, %d slots, %d chains, %d maxQ, %d thresh, %s",
446 hit, not_hit + hit, chains, gDvmJit.compilerMaxQueued,
447 gDvmJit.threshold, gDvmJit.blockingMode ? "Blocking" : "Non-blocking");
448#if defined(EXIT_STATS)
449 LOGD(
450 "JIT: Lookups: %d hits, %d misses; %d NoChain, %d normal, %d punt",
451 gDvmJit.addrLookupsFound, gDvmJit.addrLookupsNotFound,
452 gDvmJit.noChainExit, gDvmJit.normalExit, gDvmJit.puntExit);
453#endif
454 LOGD("JIT: %d Translation chains", gDvmJit.translationChains);
455#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -0700456 LOGD("JIT: Invoke: %d chainable, %d pred. chain, %d native, "
457 "%d return",
458 gDvmJit.invokeChain, gDvmJit.invokePredictedChain,
459 gDvmJit.invokeNative, gDvmJit.returnOp);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700460#endif
Ben Chenge80cd942009-07-17 15:54:23 -0700461 if (gDvmJit.profile) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700462 dvmCompilerSortAndPrintTraceProfiles();
Bill Buzbee6e963e12009-06-17 16:56:19 -0700463 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700464 }
465}
466
Bill Buzbee716f1202009-07-23 13:22:09 -0700467
Ben Chengba4fc8b2009-06-01 13:00:29 -0700468/*
469 * Final JIT shutdown. Only do this once, and do not attempt to restart
470 * the JIT later.
471 */
472void dvmJitShutdown(void)
473{
474 /* Shutdown the compiler thread */
475 dvmCompilerShutdown();
476
477 dvmCompilerDumpStats();
478
479 dvmDestroyMutex(&gDvmJit.tableLock);
480
481 if (gDvmJit.pJitEntryTable) {
482 free(gDvmJit.pJitEntryTable);
483 gDvmJit.pJitEntryTable = NULL;
484 }
485
486 if (gDvmJit.pProfTable) {
487 free(gDvmJit.pProfTable);
488 gDvmJit.pProfTable = NULL;
489 }
490}
491
Ben Chengba4fc8b2009-06-01 13:00:29 -0700492/*
493 * Adds to the current trace request one instruction at a time, just
494 * before that instruction is interpreted. This is the primary trace
495 * selection function. NOTE: return instruction are handled a little
496 * differently. In general, instructions are "proposed" to be added
497 * to the current trace prior to interpretation. If the interpreter
498 * then successfully completes the instruction, is will be considered
499 * part of the request. This allows us to examine machine state prior
500 * to interpretation, and also abort the trace request if the instruction
501 * throws or does something unexpected. However, return instructions
502 * will cause an immediate end to the translation request - which will
503 * be passed to the compiler before the return completes. This is done
504 * in response to special handling of returns by the interpreter (and
505 * because returns cannot throw in a way that causes problems for the
506 * translated code.
507 */
Ben Chengba4fc8b2009-06-01 13:00:29 -0700508int dvmCheckJit(const u2* pc, Thread* self, InterpState* interpState)
509{
510 int flags,i,len;
511 int switchInterp = false;
512 int debugOrProfile = (gDvm.debuggerActive || self->suspendCount
513#if defined(WITH_PROFILER)
514 || gDvm.activeProfilers
515#endif
516 );
517
518 switch (interpState->jitState) {
519 char* nopStr;
520 int target;
521 int offset;
522 DecodedInstruction decInsn;
523 case kJitTSelect:
524 dexDecodeInstruction(gDvm.instrFormat, pc, &decInsn);
525#if defined(SHOW_TRACE)
526 LOGD("TraceGen: adding %s",getOpcodeName(decInsn.opCode));
527#endif
528 flags = dexGetInstrFlags(gDvm.instrFlags, decInsn.opCode);
529 len = dexGetInstrOrTableWidthAbs(gDvm.instrWidth, pc);
530 offset = pc - interpState->method->insns;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700531 if (pc != interpState->currRunHead + interpState->currRunLen) {
532 int currTraceRun;
533 /* We need to start a new trace run */
534 currTraceRun = ++interpState->currTraceRun;
535 interpState->currRunLen = 0;
536 interpState->currRunHead = (u2*)pc;
537 interpState->trace[currTraceRun].frag.startOffset = offset;
538 interpState->trace[currTraceRun].frag.numInsts = 0;
539 interpState->trace[currTraceRun].frag.runEnd = false;
540 interpState->trace[currTraceRun].frag.hint = kJitHintNone;
541 }
542 interpState->trace[interpState->currTraceRun].frag.numInsts++;
543 interpState->totalTraceLen++;
544 interpState->currRunLen += len;
545 if ( ((flags & kInstrUnconditional) == 0) &&
Bill Buzbeef4ce16f2009-07-28 13:28:25 -0700546 /* don't end trace on INVOKE_DIRECT_EMPTY */
547 (decInsn.opCode != OP_INVOKE_DIRECT_EMPTY) &&
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700548 ((flags & (kInstrCanBranch |
549 kInstrCanSwitch |
550 kInstrCanReturn |
551 kInstrInvoke)) != 0)) {
552 interpState->jitState = kJitTSelectEnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700553#if defined(SHOW_TRACE)
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700554 LOGD("TraceGen: ending on %s, basic block end",
555 getOpcodeName(decInsn.opCode));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700556#endif
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700557 }
558 if (decInsn.opCode == OP_THROW) {
559 interpState->jitState = kJitTSelectEnd;
560 }
561 if (interpState->totalTraceLen >= JIT_MAX_TRACE_LEN) {
562 interpState->jitState = kJitTSelectEnd;
563 }
564 if (debugOrProfile) {
565 interpState->jitState = kJitTSelectAbort;
566 switchInterp = !debugOrProfile;
567 break;
568 }
569 if ((flags & kInstrCanReturn) != kInstrCanReturn) {
570 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700571 }
572 /* NOTE: intentional fallthrough for returns */
573 case kJitTSelectEnd:
574 {
575 if (interpState->totalTraceLen == 0) {
576 switchInterp = !debugOrProfile;
577 break;
578 }
579 JitTraceDescription* desc =
580 (JitTraceDescription*)malloc(sizeof(JitTraceDescription) +
581 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
582 if (desc == NULL) {
583 LOGE("Out of memory in trace selection");
584 dvmJitStopTranslationRequests();
585 interpState->jitState = kJitTSelectAbort;
586 switchInterp = !debugOrProfile;
587 break;
588 }
589 interpState->trace[interpState->currTraceRun].frag.runEnd =
590 true;
591 interpState->jitState = kJitNormal;
592 desc->method = interpState->method;
593 memcpy((char*)&(desc->trace[0]),
594 (char*)&(interpState->trace[0]),
595 sizeof(JitTraceRun) * (interpState->currTraceRun+1));
596#if defined(SHOW_TRACE)
597 LOGD("TraceGen: trace done, adding to queue");
598#endif
599 dvmCompilerWorkEnqueue(
600 interpState->currTraceHead,kWorkOrderTrace,desc);
601 if (gDvmJit.blockingMode) {
602 dvmCompilerDrainQueue();
603 }
604 switchInterp = !debugOrProfile;
605 }
606 break;
607 case kJitSingleStep:
608 interpState->jitState = kJitSingleStepEnd;
609 break;
610 case kJitSingleStepEnd:
611 interpState->entryPoint = kInterpEntryResume;
612 switchInterp = !debugOrProfile;
613 break;
614 case kJitTSelectAbort:
615#if defined(SHOW_TRACE)
616 LOGD("TraceGen: trace abort");
617#endif
618 interpState->jitState = kJitNormal;
619 switchInterp = !debugOrProfile;
620 break;
621 case kJitNormal:
Ben Cheng38329f52009-07-07 14:19:20 -0700622 switchInterp = !debugOrProfile;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700623 break;
Jeff Hao97319a82009-08-12 16:57:15 -0700624#if defined(WITH_SELF_VERIFICATION)
625 case kJitSelfVerification:
626 if (selfVerificationDebugInterp(pc, self)) {
627 interpState->jitState = kJitNormal;
628 switchInterp = !debugOrProfile;
629 }
630 break;
631#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700632 default:
633 dvmAbort();
634 }
635 return switchInterp;
636}
637
Bill Buzbee716f1202009-07-23 13:22:09 -0700638static inline JitEntry *findJitEntry(const u2* pc)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700639{
640 int idx = dvmJitHash(pc);
641
642 /* Expect a high hit rate on 1st shot */
643 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
644 return &gDvmJit.pJitEntryTable[idx];
645 else {
Bill Buzbee27176222009-06-09 09:20:16 -0700646 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700647 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
648 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700649 if (gDvmJit.pJitEntryTable[idx].dPC == pc)
650 return &gDvmJit.pJitEntryTable[idx];
651 }
652 }
653 return NULL;
654}
655
Bill Buzbee716f1202009-07-23 13:22:09 -0700656JitEntry *dvmFindJitEntry(const u2* pc)
Bill Buzbee27176222009-06-09 09:20:16 -0700657{
658 return findJitEntry(pc);
659}
660
661/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700662 * If a translated code address exists for the davik byte code
663 * pointer return it. This routine needs to be fast.
664 */
665void* dvmJitGetCodeAddr(const u2* dPC)
666{
667 int idx = dvmJitHash(dPC);
668
Bill Buzbee46cd5b62009-06-05 15:36:06 -0700669 /* If anything is suspended, don't re-enter the code cache */
670 if (gDvm.sumThreadSuspendCount > 0) {
671 return NULL;
672 }
673
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674 /* Expect a high hit rate on 1st shot */
675 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
676#if defined(EXIT_STATS)
677 gDvmJit.addrLookupsFound++;
678#endif
679 return gDvmJit.pJitEntryTable[idx].codeAddress;
680 } else {
Bill Buzbee27176222009-06-09 09:20:16 -0700681 int chainEndMarker = gDvmJit.jitTableSize;
Bill Buzbee716f1202009-07-23 13:22:09 -0700682 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
683 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700684 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
685#if defined(EXIT_STATS)
686 gDvmJit.addrLookupsFound++;
687#endif
688 return gDvmJit.pJitEntryTable[idx].codeAddress;
689 }
690 }
691 }
692#if defined(EXIT_STATS)
693 gDvmJit.addrLookupsNotFound++;
694#endif
695 return NULL;
696}
697
698/*
Bill Buzbee716f1202009-07-23 13:22:09 -0700699 * Find an entry in the JitTable, creating if necessary.
700 * Returns null if table is full.
701 */
702JitEntry *dvmJitLookupAndAdd(const u2* dPC)
703{
704 u4 chainEndMarker = gDvmJit.jitTableSize;
705 u4 idx = dvmJitHash(dPC);
706
707 /* Walk the bucket chain to find an exact match for our PC */
708 while ((gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) &&
709 (gDvmJit.pJitEntryTable[idx].dPC != dPC)) {
710 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
711 }
712
713 if (gDvmJit.pJitEntryTable[idx].dPC != dPC) {
714 /*
715 * No match. Aquire jitTableLock and find the last
716 * slot in the chain. Possibly continue the chain walk in case
717 * some other thread allocated the slot we were looking
718 * at previuosly (perhaps even the dPC we're trying to enter).
719 */
720 dvmLockMutex(&gDvmJit.tableLock);
721 /*
722 * At this point, if .dPC is NULL, then the slot we're
723 * looking at is the target slot from the primary hash
724 * (the simple, and common case). Otherwise we're going
725 * to have to find a free slot and chain it.
726 */
727 MEM_BARRIER(); /* Make sure we reload [].dPC after lock */
728 if (gDvmJit.pJitEntryTable[idx].dPC != NULL) {
729 u4 prev;
730 while (gDvmJit.pJitEntryTable[idx].u.info.chain != chainEndMarker) {
731 if (gDvmJit.pJitEntryTable[idx].dPC == dPC) {
732 /* Another thread got there first for this dPC */
733 dvmUnlockMutex(&gDvmJit.tableLock);
734 return &gDvmJit.pJitEntryTable[idx];
735 }
736 idx = gDvmJit.pJitEntryTable[idx].u.info.chain;
737 }
738 /* Here, idx should be pointing to the last cell of an
739 * active chain whose last member contains a valid dPC */
740 assert(gDvmJit.pJitEntryTable[idx].dPC != NULL);
741 /* Linear walk to find a free cell and add it to the end */
742 prev = idx;
743 while (true) {
744 idx++;
745 if (idx == chainEndMarker)
746 idx = 0; /* Wraparound */
747 if ((gDvmJit.pJitEntryTable[idx].dPC == NULL) ||
748 (idx == prev))
749 break;
750 }
751 if (idx != prev) {
752 JitEntryInfoUnion oldValue;
753 JitEntryInfoUnion newValue;
754 /*
755 * Although we hold the lock so that noone else will
756 * be trying to update a chain field, the other fields
757 * packed into the word may be in use by other threads.
758 */
759 do {
760 oldValue = gDvmJit.pJitEntryTable[prev].u;
761 newValue = oldValue;
762 newValue.info.chain = idx;
763 } while (!ATOMIC_CMP_SWAP(
764 &gDvmJit.pJitEntryTable[prev].u.infoWord,
765 oldValue.infoWord, newValue.infoWord));
766 }
767 }
768 if (gDvmJit.pJitEntryTable[idx].dPC == NULL) {
769 /* Allocate the slot */
770 gDvmJit.pJitEntryTable[idx].dPC = dPC;
771 gDvmJit.jitTableEntriesUsed++;
772 } else {
773 /* Table is full */
774 idx = chainEndMarker;
775 }
776 dvmUnlockMutex(&gDvmJit.tableLock);
777 }
778 return (idx == chainEndMarker) ? NULL : &gDvmJit.pJitEntryTable[idx];
779}
780/*
Ben Chengba4fc8b2009-06-01 13:00:29 -0700781 * Register the translated code pointer into the JitTable.
782 * NOTE: Once a codeAddress field transitions from NULL to
783 * JIT'd code, it must not be altered without first halting all
Bill Buzbee716f1202009-07-23 13:22:09 -0700784 * threads. This routine should only be called by the compiler
785 * thread.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786 */
Bill Buzbee716f1202009-07-23 13:22:09 -0700787void dvmJitSetCodeAddr(const u2* dPC, void *nPC, JitInstructionSetType set) {
788 JitEntryInfoUnion oldValue;
789 JitEntryInfoUnion newValue;
790 JitEntry *jitEntry = dvmJitLookupAndAdd(dPC);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700791 assert(jitEntry);
Bill Buzbee716f1202009-07-23 13:22:09 -0700792 /* Note: order of update is important */
793 do {
794 oldValue = jitEntry->u;
795 newValue = oldValue;
796 newValue.info.instructionSet = set;
797 } while (!ATOMIC_CMP_SWAP(
798 &jitEntry->u.infoWord,
799 oldValue.infoWord, newValue.infoWord));
800 jitEntry->codeAddress = nPC;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700801}
802
803/*
804 * Determine if valid trace-bulding request is active. Return true
805 * if we need to abort and switch back to the fast interpreter, false
806 * otherwise. NOTE: may be called even when trace selection is not being
807 * requested
808 */
809
Ben Chengba4fc8b2009-06-01 13:00:29 -0700810bool dvmJitCheckTraceRequest(Thread* self, InterpState* interpState)
811{
Bill Buzbee48f18242009-06-19 16:02:27 -0700812 bool res = false; /* Assume success */
813 int i;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700814 if (gDvmJit.pJitEntryTable != NULL) {
Bill Buzbee48f18242009-06-19 16:02:27 -0700815 /* Two-level filtering scheme */
816 for (i=0; i< JIT_TRACE_THRESH_FILTER_SIZE; i++) {
817 if (interpState->pc == interpState->threshFilter[i]) {
818 break;
819 }
820 }
821 if (i == JIT_TRACE_THRESH_FILTER_SIZE) {
822 /*
823 * Use random replacement policy - otherwise we could miss a large
824 * loop that contains more traces than the size of our filter array.
825 */
826 i = rand() % JIT_TRACE_THRESH_FILTER_SIZE;
827 interpState->threshFilter[i] = interpState->pc;
828 res = true;
829 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700830 /*
831 * If the compiler is backlogged, or if a debugger or profiler is
832 * active, cancel any JIT actions
833 */
Bill Buzbee48f18242009-06-19 16:02:27 -0700834 if ( res || (gDvmJit.compilerQueueLength >= gDvmJit.compilerHighWater) ||
Ben Chengba4fc8b2009-06-01 13:00:29 -0700835 gDvm.debuggerActive || self->suspendCount
836#if defined(WITH_PROFILER)
837 || gDvm.activeProfilers
838#endif
839 ) {
840 if (interpState->jitState != kJitOff) {
841 interpState->jitState = kJitNormal;
842 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700843 } else if (interpState->jitState == kJitTSelectRequest) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700844 JitEntry *slot = dvmJitLookupAndAdd(interpState->pc);
845 if (slot == NULL) {
Ben Chengba4fc8b2009-06-01 13:00:29 -0700846 /*
Bill Buzbee716f1202009-07-23 13:22:09 -0700847 * Table is full. This should have been
848 * detected by the compiler thread and the table
849 * resized before we run into it here. Assume bad things
850 * are afoot and disable profiling.
Ben Chengba4fc8b2009-06-01 13:00:29 -0700851 */
852 interpState->jitState = kJitTSelectAbort;
Bill Buzbee716f1202009-07-23 13:22:09 -0700853 LOGD("JIT: JitTable full, disabling profiling");
854 dvmJitStopTranslationRequests();
855 } else if (slot->u.info.traceRequested) {
856 /* Trace already requested - revert to interpreter */
857 interpState->jitState = kJitTSelectAbort;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700858 } else {
Bill Buzbee716f1202009-07-23 13:22:09 -0700859 /* Mark request */
860 JitEntryInfoUnion oldValue;
861 JitEntryInfoUnion newValue;
862 do {
863 oldValue = slot->u;
864 newValue = oldValue;
865 newValue.info.traceRequested = true;
866 } while (!ATOMIC_CMP_SWAP( &slot->u.infoWord,
867 oldValue.infoWord, newValue.infoWord));
Ben Chengba4fc8b2009-06-01 13:00:29 -0700868 }
869 }
870 switch (interpState->jitState) {
871 case kJitTSelectRequest:
872 interpState->jitState = kJitTSelect;
873 interpState->currTraceHead = interpState->pc;
874 interpState->currTraceRun = 0;
875 interpState->totalTraceLen = 0;
876 interpState->currRunHead = interpState->pc;
877 interpState->currRunLen = 0;
878 interpState->trace[0].frag.startOffset =
879 interpState->pc - interpState->method->insns;
880 interpState->trace[0].frag.numInsts = 0;
881 interpState->trace[0].frag.runEnd = false;
882 interpState->trace[0].frag.hint = kJitHintNone;
883 break;
884 case kJitTSelect:
885 case kJitTSelectAbort:
886 res = true;
887 case kJitSingleStep:
888 case kJitSingleStepEnd:
889 case kJitOff:
890 case kJitNormal:
Jeff Hao97319a82009-08-12 16:57:15 -0700891#if defined(WITH_SELF_VERIFICATION)
892 case kJitSelfVerification:
893#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700894 break;
895 default:
896 dvmAbort();
897 }
898 }
899 return res;
900}
901
Bill Buzbee27176222009-06-09 09:20:16 -0700902/*
903 * Resizes the JitTable. Must be a power of 2, and returns true on failure.
904 * Stops all threads, and thus is a heavyweight operation.
905 */
906bool dvmJitResizeJitTable( unsigned int size )
907{
Bill Buzbee716f1202009-07-23 13:22:09 -0700908 JitEntry *pNewTable;
909 JitEntry *pOldTable;
Bill Buzbee27176222009-06-09 09:20:16 -0700910 u4 newMask;
Bill Buzbee716f1202009-07-23 13:22:09 -0700911 unsigned int oldSize;
Bill Buzbee27176222009-06-09 09:20:16 -0700912 unsigned int i;
913
Ben Cheng3f02aa42009-08-14 13:52:09 -0700914 assert(gDvmJit.pJitEntryTable != NULL);
Bill Buzbee27176222009-06-09 09:20:16 -0700915 assert(size && !(size & (size - 1))); /* Is power of 2? */
916
917 LOGD("Jit: resizing JitTable from %d to %d", gDvmJit.jitTableSize, size);
918
919 newMask = size - 1;
920
921 if (size <= gDvmJit.jitTableSize) {
922 return true;
923 }
924
Bill Buzbee716f1202009-07-23 13:22:09 -0700925 pNewTable = (JitEntry*)calloc(size, sizeof(*pNewTable));
Bill Buzbee27176222009-06-09 09:20:16 -0700926 if (pNewTable == NULL) {
927 return true;
928 }
929 for (i=0; i< size; i++) {
Bill Buzbee716f1202009-07-23 13:22:09 -0700930 pNewTable[i].u.info.chain = size; /* Initialize chain termination */
Bill Buzbee27176222009-06-09 09:20:16 -0700931 }
932
933 /* Stop all other interpreting/jit'ng threads */
934 dvmSuspendAllThreads(SUSPEND_FOR_JIT);
935
Bill Buzbee716f1202009-07-23 13:22:09 -0700936 pOldTable = gDvmJit.pJitEntryTable;
937 oldSize = gDvmJit.jitTableSize;
Bill Buzbee27176222009-06-09 09:20:16 -0700938
939 dvmLockMutex(&gDvmJit.tableLock);
Bill Buzbee27176222009-06-09 09:20:16 -0700940 gDvmJit.pJitEntryTable = pNewTable;
941 gDvmJit.jitTableSize = size;
942 gDvmJit.jitTableMask = size - 1;
Bill Buzbee716f1202009-07-23 13:22:09 -0700943 gDvmJit.jitTableEntriesUsed = 0;
Bill Buzbee27176222009-06-09 09:20:16 -0700944 dvmUnlockMutex(&gDvmJit.tableLock);
945
Bill Buzbee716f1202009-07-23 13:22:09 -0700946 for (i=0; i < oldSize; i++) {
947 if (pOldTable[i].dPC) {
948 JitEntry *p;
949 u2 chain;
950 p = dvmJitLookupAndAdd(pOldTable[i].dPC);
951 p->dPC = pOldTable[i].dPC;
952 /*
953 * Compiler thread may have just updated the new entry's
954 * code address field, so don't blindly copy null.
955 */
956 if (pOldTable[i].codeAddress != NULL) {
957 p->codeAddress = pOldTable[i].codeAddress;
958 }
959 /* We need to preserve the new chain field, but copy the rest */
960 dvmLockMutex(&gDvmJit.tableLock);
961 chain = p->u.info.chain;
962 p->u = pOldTable[i].u;
963 p->u.info.chain = chain;
964 dvmUnlockMutex(&gDvmJit.tableLock);
965 }
966 }
967
968 free(pOldTable);
969
Bill Buzbee27176222009-06-09 09:20:16 -0700970 /* Restart the world */
971 dvmResumeAllThreads(SUSPEND_FOR_JIT);
972
973 return false;
974}
975
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700976/*
977 * Float/double conversion requires clamping to min and max of integer form. If
978 * target doesn't support this normally, use these.
979 */
980s8 dvmJitd2l(double d)
981{
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700982 static const double kMaxLong = (double)(s8)0x7fffffffffffffffULL;
983 static const double kMinLong = (double)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700984 if (d >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700985 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700986 else if (d <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700987 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700988 else if (d != d) // NaN case
989 return 0;
990 else
991 return (s8)d;
992}
993
994s8 dvmJitf2l(float f)
995{
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700996 static const float kMaxLong = (float)(s8)0x7fffffffffffffffULL;
997 static const float kMinLong = (float)(s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -0700998 if (f >= kMaxLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700999 return (s8)0x7fffffffffffffffULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001000 else if (f <= kMinLong)
Bill Buzbee9727c3d2009-08-01 11:32:36 -07001001 return (s8)0x8000000000000000ULL;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001002 else if (f != f) // NaN case
1003 return 0;
1004 else
1005 return (s8)f;
1006}
1007
Bill Buzbee27176222009-06-09 09:20:16 -07001008
Ben Chengba4fc8b2009-06-01 13:00:29 -07001009#endif /* WITH_JIT */