HRESULT Library_corlib_native_System_Reflection_Assembly::GetVersion___VOID__BYREF_I4__BYREF_I4__BYREF_I4__BYREF_I4( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_StackFrame* caller = stack.Caller(); 
    if(caller == NULL) 
    {
        TINYCLR_SET_AND_LEAVE(S_OK);
    }
    else
    {
        CLR_RT_Assembly_Instance assm;
        
        TINYCLR_CHECK_HRESULT(GetTypeDescriptor( stack.Arg0(), assm ));

        const CLR_RECORD_VERSION& version = assm.m_assm->m_header->version;

        // we do not check for the reference not to be NULL because this is an internal method
        stack.Arg1().Dereference()->NumericByRef().s4 = version.iMajorVersion;
        stack.Arg2().Dereference()->NumericByRef().s4 = version.iMinorVersion;
        stack.Arg3().Dereference()->NumericByRef().s4 = version.iBuildNumber;
        stack.Arg4().Dereference()->NumericByRef().s4 = version.iRevisionNumber;
    }
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_Assembly::GetExecutingAssembly___STATIC__SystemReflectionAssembly( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock& top = stack.PushValueAndClear();

    CLR_RT_StackFrame* caller = stack.Caller(); if(caller == NULL) TINYCLR_SET_AND_LEAVE(S_OK);

    {
        CLR_RT_Assembly_Index idx; idx.Set( caller->MethodCall().m_assm->m_idx );

        top.SetReflection( idx );
    }
    
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Threading_Monitor::Exit___STATIC__VOID__OBJECT( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();
    
    CLR_RT_StackFrame* caller = NULL;

    if(stack.Arg0().Dereference() == NULL)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_ARGUMENT_NULL);
    }
    
    caller = stack.Caller(); FAULT_ON_NULL(caller); // We need to set the constraint on the caller, not on us...

    TINYCLR_SET_AND_LEAVE(g_CLR_RT_ExecutionEngine.UnlockObject( stack.Arg0(), caller->m_owningSubThread ));

    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Reflection_Assembly::GetExecutingAssembly___STATIC__SystemReflectionAssembly( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_HeapBlock& top = stack.PushValueAndClear();
    CLR_RT_HeapBlock* hbObj;

    CLR_RT_StackFrame* caller = stack.Caller(); if(caller == NULL) TINYCLR_SET_AND_LEAVE(S_OK);

    {
        CLR_RT_Assembly_Index idx; idx.Set( caller->MethodCall().m_assm->m_idx );

        TINYCLR_CHECK_HRESULT(g_CLR_RT_ExecutionEngine.NewObjectFromIndex(top, g_CLR_RT_WellKnownTypes.m_Assembly));
        
        hbObj = top.Dereference();
        hbObj->SetReflection( idx );
    }
    
    TINYCLR_NOCLEANUP();
}
HRESULT Library_corlib_native_System_Threading_Monitor::Enter___STATIC__VOID__OBJECT( CLR_RT_StackFrame& stack )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_StackFrame* caller = NULL;

    if(stack.Arg0().Dereference() == NULL)
    {
        TINYCLR_SET_AND_LEAVE(CLR_E_ARGUMENT_NULL);
    }

    switch(stack.m_customState)
    {
    case 0:
    {
        caller = stack.Caller();
        FAULT_ON_NULL(caller); // We need to set the constraint on the caller, not on us...

        hr = g_CLR_RT_ExecutionEngine.LockObject( stack.Arg0(), caller->m_owningSubThread, TIMEOUT_INFINITE, false );
        if(hr == CLR_E_THREAD_WAITING)
        {
            stack.m_customState = 1;
        }

        TINYCLR_LEAVE();
    }
    break;

    case 1:
        TINYCLR_SET_AND_LEAVE(S_OK);
        break;

    default:
        TINYCLR_SET_AND_LEAVE(CLR_E_FAIL);
    }

    TINYCLR_NOCLEANUP();
}
HRESULT CLR_RT_StackFrame::Push( CLR_RT_Thread* th, const CLR_RT_MethodDef_Instance& callInst, CLR_INT32 extraBlocks )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_StackFrame*               stack;
    CLR_RT_StackFrame*               caller;
    CLR_RT_Assembly*                 assm;
    const CLR_RECORD_METHODDEF*      md;
    const CLR_RT_MethodDef_Instance* callInstPtr = &callInst;
    CLR_UINT32                       sizeLocals;
    CLR_UINT32                       sizeEvalStack;

#if defined(PLATFORM_WINDOWS)
    if(s_CLR_RT_fTrace_SimulateSpeed > c_CLR_RT_Trace_None)
    {
        CLR_PROF_Handler::SuspendTime();

        HAL_Windows_FastSleep( g_HAL_Configuration_Windows.TicksPerMethodCall );

        CLR_PROF_Handler::ResumeTime();
    }
#endif

    assm          = callInstPtr->m_assm;
    md            = callInstPtr->m_target;

    sizeLocals    = md->numLocals;
    sizeEvalStack = md->lengthEvalStack + CLR_RT_StackFrame::c_OverheadForNewObjOrInteropMethod;

    //--//

    caller = th->CurrentFrame();

    //--//

    //
    // Allocate memory for the runtime state.
    //
    {
        CLR_UINT32 memorySize = sizeLocals + sizeEvalStack;

        if(extraBlocks > 0             ) memorySize += extraBlocks;
        if(memorySize  < c_MinimumStack) memorySize  = c_MinimumStack;

        memorySize += CONVERTFROMSIZETOHEAPBLOCKS(offsetof(CLR_RT_StackFrame,m_extension));

        stack = EVENTCACHE_EXTRACT_NODE_AS_BLOCKS(g_CLR_RT_EventCache,CLR_RT_StackFrame,DATATYPE_STACK_FRAME,0,memorySize);
        CHECK_ALLOCATION(stack);
    }

    //--//

    {   //
        stack->m_owningSubThread = th->CurrentSubThread();  // CLR_RT_SubThread*         m_owningSubThread;  // EVENT HEAP - NO RELOCATION -
        stack->m_owningThread    = th;                      // CLR_RT_Thread*            m_owningThread;     // EVENT HEAP - NO RELOCATION -
        // CLR_UINT32                m_flags;
        //
        stack->m_call            = *callInstPtr;            // CLR_RT_MethodDef_Instance m_call;
        //
        // CLR_RT_MethodHandler      m_nativeMethod;
        // CLR_PMETADATA             m_IPstart;          // ANY   HEAP - DO RELOCATION -
        // CLR_PMETADATA             m_IP;               // ANY   HEAP - DO RELOCATION -
        //
        stack->m_locals          = stack->m_extension;      // CLR_RT_HeapBlock*         m_locals;           // EVENT HEAP - NO RELOCATION -
        stack->m_evalStack       = stack->m_extension + sizeLocals;                      // CLR_RT_HeapBlock*         m_evalStack;        // EVENT HEAP - NO RELOCATION -
        stack->m_evalStackPos    = stack->m_evalStack;      // CLR_RT_HeapBlock*         m_evalStackPos;     // EVENT HEAP - NO RELOCATION -
        stack->m_evalStackEnd    = stack->m_evalStack + sizeEvalStack;                      // CLR_RT_HeapBlock*         m_evalStackEnd;     // EVENT HEAP - NO RELOCATION -
        stack->m_arguments       = NULL;                    // CLR_RT_HeapBlock*         m_arguments;        // EVENT HEAP - NO RELOCATION -
        //
        // union
        // {
        stack->m_customState     = 0;                       //    CLR_UINT32             m_customState;
        //    void*                  m_customPointer;
        // };
        //
#if defined(TINYCLR_PROFILE_NEW_CALLS)
        stack->m_callchain.Enter( stack );                  // CLR_PROF_CounterCallChain m_callchain;
#endif
        //
        // CLR_RT_HeapBlock          m_extension[1];
        //
#if defined(ENABLE_NATIVE_PROFILER)
        stack->m_fNativeProfiled = stack->m_owningThread->m_fNativeProfiled;
#endif
        CLR_RT_MethodHandler impl;

#if defined(TINYCLR_APPDOMAINS)
        stack->m_appDomain = g_CLR_RT_ExecutionEngine.GetCurrentAppDomain();
#endif

        if(md->flags & CLR_RECORD_METHODDEF::MD_DelegateInvoke) // Special case for delegate calls.
        {
            stack->m_nativeMethod = (CLR_RT_MethodHandler)CLR_RT_Thread::Execute_DelegateInvoke;

            stack->m_flags   = CLR_RT_StackFrame::c_MethodKind_Native;
            stack->m_IPstart = NULL;
        }
        else if(assm->m_nativeCode && (impl = assm->m_nativeCode[ stack->m_call.Method() ]) != NULL)
        {
            stack->m_nativeMethod = impl;

            stack->m_flags   = CLR_RT_StackFrame::c_MethodKind_Native;
            stack->m_IPstart = NULL;
            stack->m_IP      = NULL;
        }
#if defined(TINYCLR_JITTER)
        else if(assm->m_jittedCode && (impl = assm->m_jittedCode[ stack->m_call.Method() ]) != NULL)
        {
            stack->m_nativeMethod = (CLR_RT_MethodHandler)(size_t)g_thunkTable.m_address__Internal_Initialize;

            stack->m_flags   = CLR_RT_StackFrame::c_MethodKind_Jitted;
            stack->m_IPstart = (CLR_PMETADATA)impl;

            if(md->flags & CLR_RECORD_METHODDEF::MD_HasExceptionHandlers)
            {
                CLR_UINT32 numEh = *(CLR_UINT32*)stack->m_IPstart;

                stack->m_IP = stack->m_IPstart + sizeof(CLR_UINT32) + numEh * sizeof(CLR_RT_ExceptionHandler);
            }
            else
            {
                stack->m_IP = stack->m_IPstart;
            }
        }
#endif
        else
        {
            stack->m_nativeMethod = (CLR_RT_MethodHandler)CLR_RT_Thread::Execute_IL;

            if(md->RVA == CLR_EmptyIndex) TINYCLR_SET_AND_LEAVE(CLR_E_NOT_SUPPORTED);

            stack->m_flags   = CLR_RT_StackFrame::c_MethodKind_Interpreted;
            stack->m_IPstart = assm->GetByteCode( md->RVA );
            stack->m_IP      = stack->m_IPstart;
        }

#if defined(ENABLE_NATIVE_PROFILER)
        if(stack->m_owningThread->m_fNativeProfiled == false && md->flags & CLR_RECORD_METHODDEF::MD_NativeProfiled)
        {
            stack->m_flags |= CLR_RT_StackFrame::c_NativeProfiled;
            stack->m_owningThread->m_fNativeProfiled = true;
        }
#endif

        //--//

        th->m_stackFrames.LinkAtBack( stack );

#if defined(TINYCLR_PROFILE_NEW_CALLS)
        g_CLR_PRF_Profiler.RecordFunctionCall( th, callInst );
#endif
    }

    if(md->numLocals)
    {
        g_CLR_RT_ExecutionEngine.InitializeLocals( stack->m_locals, assm, md );
    }

    {
        CLR_UINT32 flags = md->flags & (md->MD_Synchronized | md->MD_GloballySynchronized);

        if(flags)
        {
            if(flags & md->MD_Synchronized        ) stack->m_flags |= c_NeedToSynchronize;
            if(flags & md->MD_GloballySynchronized) stack->m_flags |= c_NeedToSynchronizeGlobally;
        }
    }

#if defined(TINYCLR_ENABLE_SOURCELEVELDEBUGGING)
    stack->m_depth = stack->Caller()->Prev() ? stack->Caller()->m_depth + 1 : 0;

    if(g_CLR_RT_ExecutionEngine.m_breakpointsNum)
    {
        if(stack->m_call.DebuggingInfo().HasBreakpoint())
        {
            stack->m_flags |= CLR_RT_StackFrame::c_HasBreakpoint;
        }

        if(stack->m_owningThread->m_fHasJMCStepper || (stack->m_flags & c_HasBreakpoint) || (caller->Prev() != NULL && (caller->m_flags & c_HasBreakpoint)))
        {
            g_CLR_RT_ExecutionEngine.Breakpoint_StackFrame_Push( stack, CLR_DBG_Commands::Debugging_Execution_BreakpointDef::c_DEPTH_STEP_CALL );
        }
    }
#endif

    //--//

#if defined(TINYCLR_JITTER)
    if(s_CLR_RT_fJitter_Enabled && (stack->m_flags & CLR_RT_StackFrame::c_MethodKind_Mask) == CLR_RT_StackFrame::c_MethodKind_Interpreted)
    {
        CLR_RT_ExecutionEngine::ExecutionConstraint_Suspend();

        g_CLR_RT_ExecutionEngine.Compile( stack->m_call, CLR_RT_ExecutionEngine::c_Compile_ARM );

        CLR_RT_ExecutionEngine::ExecutionConstraint_Resume();

        if(assm->m_jittedCode)
        {
            CLR_PMETADATA ipStart = (CLR_PMETADATA)assm->m_jittedCode[ stack->m_call.Method() ];

            if(ipStart != NULL)
            {
                stack->m_nativeMethod = (CLR_RT_MethodHandler)(size_t)g_thunkTable.m_address__Internal_Initialize;

                stack->m_IPstart = ipStart;

                if(md->flags & CLR_RECORD_METHODDEF::MD_HasExceptionHandlers)
                {
                    CLR_UINT32 numEh = *(CLR_UINT32*)ipStart;

                    stack->m_IP = ipStart + sizeof(CLR_UINT32) + numEh * sizeof(CLR_RT_ExceptionHandler);
                }
                else
                {
                    stack->m_IP = ipStart;
                }

                stack->m_flags &= ~CLR_RT_StackFrame::c_MethodKind_Mask;
                stack->m_flags |=  CLR_RT_StackFrame::c_MethodKind_Jitted;
            }
        }
    }
#endif

    if(caller->Prev() != NULL && caller->m_nativeMethod == stack->m_nativeMethod)
    {
        if(stack->m_flags & CLR_RT_StackFrame::c_ProcessSynchronize)
        {
            stack->m_flags |= CLR_RT_StackFrame::c_CallerIsCompatibleForRet;
        }
        else
        {
            stack->m_flags |= CLR_RT_StackFrame::c_CallerIsCompatibleForCall | CLR_RT_StackFrame::c_CallerIsCompatibleForRet;
        }
    }

    //
    // If the arguments are in the caller's stack frame (var == 0), let's link the two.
    //
    if(extraBlocks < 0)
    {
#if defined(PLATFORM_WINDOWS) || (defined(PLATFORM_WINCE) && defined(_DEBUG))
        if(caller->m_evalStackPos > caller->m_evalStackEnd)
        {
            TINYCLR_SET_AND_LEAVE(CLR_E_STACK_OVERFLOW);
        }
#endif

        //
        // Everything is set up correctly, pop the operands.
        //
        stack->m_arguments = &caller->m_evalStackPos[ -md->numArgs ];

        caller->m_evalStackPos = stack->m_arguments;

#if defined(PLATFORM_WINDOWS) || (defined(PLATFORM_WINCE) && defined(_DEBUG))
        if(stack->m_arguments < caller->m_evalStack)
        {
            TINYCLR_SET_AND_LEAVE(CLR_E_STACK_UNDERFLOW);
        }
#endif
    }
    else
    {
        stack->m_arguments = stack->m_evalStackEnd;
    }

    TINYCLR_CLEANUP();


    TINYCLR_CLEANUP_END();
}