/*---------------------------------------------------------------------------* Project: Horizon File: os_ContinuationIterator.cpp Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 17849 $ *---------------------------------------------------------------------------*/ #include namespace nn { namespace os { namespace { const bit32 ITERATE_BEGIN = 1; const bit32 ITERATE_YIELD = 2; const bit32 ITERATE_END = 3; } struct ContinuationIteratorBase::Context { bit32 regs[8]; // r4-r11 bit32 sp; bit32 lr; }; asm bit32 ContinuationIteratorBase::ChangeContext(bit32 /* r0 */, Context* /* from */, Context* /* to */) { ARM stmia r1, {r4-r11,sp,lr} ldmia r2, {r4-r11,sp,pc} } asm bit32 ContinuationIteratorBase::ChangeContext(bit32 /* r0 */, Context* /* to */) { ARM ldmia r1, {r4-r11,sp,pc} } void ContinuationIteratorBase::Initialize(void *stackBottom) { bit32 sp = reinterpret_cast(stackBottom); #define AllocateFromStack(T, sp) reinterpret_cast((sp) -= sizeof(T)) this->m_MainContext = AllocateFromStack(Context, sp); this->m_IteratorContext = AllocateFromStack(Context, sp); #undef AllocateFromStack this->m_IteratorContext->lr = reinterpret_cast(&RunIterator); ChangeContext(reinterpret_cast(this), m_MainContext, m_IteratorContext); } inline void ContinuationIteratorBase::RunImpl() { ChangeContext(ITERATE_BEGIN, m_IteratorContext, m_MainContext); this->m_Result = Run(); this->m_IteratorContext->lr = 0; ChangeContext(ITERATE_END, m_MainContext); NN_TPANIC_("No more element."); } void ContinuationIteratorBase::RunIterator(ContinuationIteratorBase* _this) { _this->RunImpl(); } Result ContinuationIteratorBase::Yield() { ChangeContext(ITERATE_YIELD, m_IteratorContext, m_MainContext); return m_Result; } bool ContinuationIteratorBase::MoveNext(Result result) { this->m_Result = result; bit32 n = ChangeContext(0, m_MainContext, m_IteratorContext); if (n == ITERATE_YIELD) { NN_TASSERT_(result.IsSuccess()); return true; } else if (n == ITERATE_END) { return false; } NN_TPANIC_("Invalid iterator state."); return false; } }}