2010年10月20日 星期三

Dalvik Interpreter (2)

After pushing the to-be-executed method frame on the top of stack thread, Dalvik will call the main interpreter to start the interpreting.
-dvmInterpret(Thread* self, const Method* method, JValue* pResult)

1. dvmInterpret()

-arguments:

1: Thread* self --> current thread
2: const Method* method --> current to-be-executed method
3: the returned value

-function work flow

1. Initialize working state
-source
interpState.method = method;
interpState.fp = (u4*) self->curFrame;
interpState.pc = method->insns;
interpState.entryPoint = kInterpEntryInstr;


if (dvmDebuggerOrProfilerActive())
    interpState.nextMode = INTERP_DBG;
else
    interpState.nextMode = INTERP_STD;


2. Determine execution mode. Typically, Dalvik has two modes of 
    interpreter to choose (C or Assembly for target platform).
-source
typedef bool (*Interpreter)(Thread*, InterpState*);
Interpreter stdInterp;
if (gDvm.executionMode == kExecutionModeInterpFast)
    stdInterp = dvmMterpStd;
#if defined(WITH_JIT)

else if (gDvm.executionMode == kExecutionModeJit)
    stdInterp = dvmMterpStd;
#endif
else
    stdInterp = dvmInterpretStd;


3. Call the real interpreter with the thread and interpState parameters
change = true;
while (change) {
    switch (interpState.nextMode) {
        case INTERP_STD:

            change = (*stdInterp)(self, &interpState);
            break;
#if defined(WITH_PROFILER) || defined(WITH_DEBUGGER) || defined(WITH_JIT)
        case INTERP_DBG:
            change = dvmInterpretDbg(self, &interpState);
            break;
#endif
        default:
            dvmAbort();

    }
}

2010年10月12日 星期二

Thread's Stack Management in Dalvik VM

Thread Setup in Dalvik:
1. dvmThreadStartup() (vm/Thread.c)
   -called by dvmStartup()
   -This function will setup the thread list and the main
    thread's environment
   -important codes
    thread = allocThread(gDvm.stackSize);

2. allocThread(int interpStackSize)
   -Default stack size per stack: 3 * 4k pages
   -Important codes
    stackBottom = (u1*) malloc(interpStackSize);
     thread->interpStackSize = interpStackSize;
     thread->interpStackStart = stackBottom + interpStackSize;
     thread->interpStackEnd = stackBottom + 
     STACK_OVERFLOW_RESERVE;(768 bytes)
   -The initialized stack in a thread

   
Stack Push/Pop in Dalvik:
1. At first, dalvik vm main function will invoke   
    dvmCallMethodV() to interpret the java main
    method
2. dvmCallMethodV() will invoke callPrep to push the
    stack frame.
   -callPrep will invoke dvmPushInterpFrame() to push
    the related method frame on the thread's stack.
   -The Dalvik vm thread stack frame