root/src/VM/cil/cilVMLocalVariable.cpp

Revision 138, 5.9 kB (checked in by hak, 2 years ago)

- Added CCilVm::reset()
- Fixed VM status management (ticket #5)
- Fixed API names not to have upper case as 1st letter.
- Fixed sample build break
- Added new sample, "vm_contol" to demonstrate VM status control API

Line 
1 /****************************************************************************
2  *
3  * CRI Middleware SDK
4  *
5  * Copyright (c) 2008 CRI Middleware, Inc. All rights reserved.
6  *
7  * Use, modification and distribution are subject to the CRI Script Software
8  * License, Version 1.0(see accompanying file "CriScript_License_1_0.txt" or
9  * copy at www.criscript.com/trac/wiki/CRI%20Script%20Software%20License%201.0 ).
10  *
11  *
12  * Library  : CRIScript
13  * Module   : cil Virtual Machine
14  * File     : cilVMLocalVariable.cpp
15  * Date     :
16  * Version  :
17  *
18  ****************************************************************************/
19
20 /***************************************************************************
21  *      Include file
22  ***************************************************************************/
23 #include "stdafx.h"
24 #include "criscript.h"
25 #include "cilVm.h"
26 namespace cri {
27 /***************************************************************************
28  *      Variables
29  ***************************************************************************/
30
31 /***************************************************************************
32  *      Methods
33  ***************************************************************************/
34
35 /***************************************************************************
36  *      Store from top of the stack to a Local field
37  ***************************************************************************/
38 void CCilVm::storeLocalVariable( const int32_t iIndex )
39 {
40         VM_CALLSTACK* stack = getCurrentCallStack( VM_CALLSTACK_CALLSTACK );
41         assert( stack->iLocalVariableStartIndex + iIndex <= (int32_t)m_iLocalVariableListIndex );
42
43         OPERAND_FLAG flag =  m_LocalVariableList[ stack->iLocalVariableStartIndex + iIndex ].getFlag();
44         if( flag & OPERAND_FLAG_READONLY )
45         {
46                 //Read only variable
47                 popEvalStack();
48                 return;
49         }
50
51         if( flag & OPERAND_FLAG_STRICT )
52         {
53                 //Test type constraint
54                 storeAsRestrictedType( m_LocalVariableList[ stack->iLocalVariableStartIndex + iIndex ]  );
55                 popEvalStack();
56                 return;
57         }
58
59         getEvalStackFirstEntry().moveTo( m_LocalVariableList[ stack->iLocalVariableStartIndex + iIndex ] );
60         popEvalStackFast();
61 }
62
63 /***************************************************************************
64  *      Load from a Local field to Top of the stack
65  ***************************************************************************/
66 void CCilVm::loadLocalVariable( const int32_t iIndex )
67 {
68         VM_CALLSTACK* stack = getCurrentCallStack( VM_CALLSTACK_CALLSTACK );
69         assert( stack->iLocalVariableStartIndex + iIndex <= (int32_t)m_iLocalVariableListIndex );
70
71         pushEvalStack( m_LocalVariableList[ stack->iLocalVariableStartIndex + iIndex ] );
72 }
73
74
75 /***************************************************************************
76  *      get LocalVariable Value (Debugger helper)
77  ***************************************************************************/
78 CVariable& CCilVm::getLocalVariable( const int32_t iIndex )
79 {
80         assert( iIndex < MAX_LOCAL_VARIABLE );
81
82         VM_CALLSTACK* stack = getCurrentCallStack( VM_CALLSTACK_CALLSTACK );
83         assert( stack->iLocalVariableStartIndex + iIndex <= (int32_t)m_iLocalVariableListIndex );
84         return m_LocalVariableList[ stack->iLocalVariableStartIndex + iIndex ];
85 }
86
87 /***************************************************************************
88  *      Set LocalVariable Value (Debugger helper)
89  ***************************************************************************/
90 bool CCilVm::setLocalVariable( const int32_t iIndex, const CVariable& value )
91 {
92         assert( iIndex < MAX_LOCAL_VARIABLE );
93
94         VM_CALLSTACK* stack = getCurrentCallStack( VM_CALLSTACK_CALLSTACK );
95         assert( stack->iLocalVariableStartIndex + iIndex <= (int32_t)m_iLocalVariableListIndex );
96         m_LocalVariableList[ stack->iLocalVariableStartIndex + iIndex ] = value;
97         return true;
98 }
99
100 /***************************************************************************
101  *      VM local variable management
102  *              reserve local variable slot for future use
103  ***************************************************************************/
104 void CCilVm::reserveLocalVariables( const RID ridLocalsToReserve )
105 {
106         assert( TypeFromToken( ridLocalsToReserve ) == MDT_PARAMDEF );
107         vector< OPERAND_TYPE_FLAG >* pParamList;
108         int32_t iIndex;
109         if( isBuiltinRid( ridLocalsToReserve ) )
110         {
111                 //Built in methods
112                 iIndex = IndexFromBuiltinRid( ridLocalsToReserve );
113                 pParamList = &m_MetaData.getBuiltinParamListTable()[ iIndex ];
114
115         }
116         else
117         {
118                 iIndex = RidFromToken( ridLocalsToReserve );
119                 pParamList = &m_MetaData.getParamListTable()[ iIndex ];
120         }
121
122         //Add param list
123         uint32_t iSize = (uint32_t)pParamList->size();
124         assert( m_iLocalVariableListIndex + iSize < MAX_LOCAL_VARIABLE );
125
126         CVariable variable;
127         for( uint32_t i = 0; i < iSize; i++ )
128         {
129                 RID ridConstraint =  (*pParamList)[ i ].ridConstraintType;
130                 if( ridConstraint )
131                 {
132                         //With Object type constraint
133                         VMOBJECTREF obj = createObject( MDT_TYPEDEF | ridConstraint );
134                         variable.setObjectRefWithFlags( obj,
135                                 (*pParamList)[ i ].flag,
136                                 MDT_TYPEDEF | ridConstraint );
137                 }
138                 else
139                 {
140                         variable.iOperandType = (*pParamList)[ i ].type;
141                         variable.iOperandFlag = (*pParamList)[ i ].flag;
142                         variable.ridConstraintType = 0;
143                 }
144
145                 m_LocalVariableList[ m_iLocalVariableListIndex ] = variable;
146                 updateLocalVariableListIndex();
147         }
148 }
149
150 /***************************************************************************
151  *      VM local variable management
152  *              dispose local variable slot for future use
153  ***************************************************************************/
154 void CCilVm::disposeLocalVariables( const uint32_t iLocalsToDispose )
155 {
156         assert( iLocalsToDispose <= m_iLocalVariableListIndex );
157
158 #ifdef VM_TRACK_ERASEWHENPOP
159         for( uint32_t i = 0; i < iLocalsToDispose; ++i )
160                 m_LocalVariableList[ --m_iLocalVariableListIndex ].erase();
161 #else
162         m_iLocalVariableListIndex -= iLocalsToDispose;
163 #endif
164 }
165        
166 } //namespace CRI
Note: See TracBrowser for help on using the browser.