1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     os_ContinuationIterator.cpp
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 17849 $
14  *---------------------------------------------------------------------------*/
15 
16 #include <nn/os/os_ContinuationIterator.h>
17 
18 namespace nn { namespace os {
19 
20 namespace
21 {
22     const bit32 ITERATE_BEGIN = 1;
23     const bit32 ITERATE_YIELD = 2;
24     const bit32 ITERATE_END   = 3;
25 }
26 
27 struct ContinuationIteratorBase::Context
28 {
29     bit32 regs[8]; // r4-r11
30     bit32 sp;
31     bit32 lr;
32 };
33 
ChangeContext(bit32,Context *,Context *)34 asm bit32 ContinuationIteratorBase::ChangeContext(bit32 /* r0 */, Context* /* from */, Context* /* to */)
35 {
36     ARM
37 
38     stmia r1, {r4-r11,sp,lr}
39     ldmia r2, {r4-r11,sp,pc}
40 }
41 
ChangeContext(bit32,Context *)42 asm bit32 ContinuationIteratorBase::ChangeContext(bit32 /* r0 */, Context* /* to */)
43 {
44     ARM
45 
46     ldmia r1, {r4-r11,sp,pc}
47 }
48 
Initialize(void * stackBottom)49 void ContinuationIteratorBase::Initialize(void *stackBottom)
50 {
51     bit32 sp = reinterpret_cast<uptr>(stackBottom);
52 #define AllocateFromStack(T, sp) reinterpret_cast<T*>((sp) -= sizeof(T))
53     this->m_MainContext = AllocateFromStack(Context, sp);
54     this->m_IteratorContext = AllocateFromStack(Context, sp);
55 #undef AllocateFromStack
56 
57     this->m_IteratorContext->lr = reinterpret_cast<bit32>(&RunIterator);
58 
59     ChangeContext(reinterpret_cast<bit32>(this), m_MainContext, m_IteratorContext);
60 }
61 
RunImpl()62 inline void ContinuationIteratorBase::RunImpl()
63 {
64     ChangeContext(ITERATE_BEGIN, m_IteratorContext, m_MainContext);
65     this->m_Result = Run();
66     this->m_IteratorContext->lr = 0;
67     ChangeContext(ITERATE_END, m_MainContext);
68     NN_TPANIC_("No more element.");
69 }
70 
RunIterator(ContinuationIteratorBase * _this)71 void ContinuationIteratorBase::RunIterator(ContinuationIteratorBase* _this)
72 {
73     _this->RunImpl();
74 }
75 
Yield()76 Result ContinuationIteratorBase::Yield()
77 {
78     ChangeContext(ITERATE_YIELD, m_IteratorContext, m_MainContext);
79     return m_Result;
80 }
81 
MoveNext(Result result)82 bool ContinuationIteratorBase::MoveNext(Result result)
83 {
84     this->m_Result = result;
85     bit32 n = ChangeContext(0, m_MainContext, m_IteratorContext);
86     if (n == ITERATE_YIELD)
87     {
88         NN_TASSERT_(result.IsSuccess());
89         return true;
90     }
91     else if (n == ITERATE_END)
92     {
93         return false;
94     }
95     NN_TPANIC_("Invalid iterator state.");
96     return false;
97 }
98 
99 }}
100