1 /*-
2  * Copyright (c) 2004 - 2011 CTPP Team
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 4. Neither the name of the CTPP Team nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *      CTPP2VMArgStack.hpp
29  *
30  * $CTPP$
31  */
32 #ifndef _CTPP2_VM_ARG_STACK_HPP__
33 #define _CTPP2_VM_ARG_STACK_HPP__ 1
34 
35 /**
36   @file CTPP2VMArgStack.hpp
37   @brief Virtual Machine stack of arguments
38 */
39 
40 #include "CDT.hpp"
41 
42 namespace CTPP // C++ Template Engine
43 {
44 
45 // FWD
46 class VM;
47 
48 /**
49   @class VMArgStack CTPP2VMCodeStack.hpp <CTPP2VMCodeStack.hpp>
50   @brief Virtual Machine stack of arguments
51 
52 <pre>
53     +----+  <-- iMaxStackSize
54     |void|
55     |void|
56     |----|  <-- iStackPointer
57     |data|
58     |data|
59     | .. |
60     |data|  <-- iBasePointer
61     |data|
62     | .. |
63     |data|
64     +----+  <-- 0
65 </pre>
66 
67 */
68 class CTPP2DECL VMArgStack
69 {
70 public:
71 	/**
72 	  @brief Constructor
73 	  @param iIMaxStackSize - maximal stack size
74 	*/
75 	VMArgStack(const INT_32  iIMaxStackSize = 4096);
76 
77 	/**
78 	  @brief Save base pointer
79 	  @param iOffset - offset from top of stack
80 	*/
81 	void SaveBasePointer(const UINT_32 iOffset = 0);
82 
83 	/**
84 	  @brief Restore previous base pointer
85 	*/
86 	void RestoreBasePointer();
87 
88 	/**
89 	  @brief Get stack usage factor
90 	  @return stack depth
91 	*/
92 	INT_32 GetSize() const;
93 
94 	/**
95 	  @brief Push element into stack
96 	  @param oCDT - element to push
97 	  @return stack depth
98 	*/
99 	INT_32 PushElement(const CDT & oCDT);
100 
101 	/**
102 	  @brief Remove top stack element
103 	  @return stack depth
104 	*/
105 	INT_32 PopElement();
106 
107 	/**
108 	  @brief Get element from specified position
109 	  @param iPos - element position
110 	  @return Stack element
111 	*/
112 	CDT & GetElement(const INT_32  iPos);
113 
114 	/**
115 	  @brief Get top stack element
116 	  @return Stack element
117 	*/
GetTopElement(const INT_32 iPos=0)118 	inline CDT & GetTopElement(const INT_32  iPos = 0) { return GetElement(iStackPointer + iPos); }
119 
120 	/**
121 	  @brief Clear stack on specified depth
122 	  @param iDepth - number of elements to clear
123 	  @return stack depth
124 	*/
125 	INT_32 ClearStack(const INT_32  iDepth);
126 
127 	/**
128 	  @brief Reset stack of arguments to default state
129 	*/
130 	void Reset();
131 
132 	/**
133 	  @brief A destructor
134 	*/
135 	~VMArgStack() throw();
136 
137 private:
138 	friend class VM;
139 
140 	/**
141 	  @brief Copy Constructor
142 	*/
143 	VMArgStack(const VMArgStack & oRhs);
144 
145 	/**
146 	  @brief Copy operator
147 	*/
148 	VMArgStack & operator=(const VMArgStack & oRhs);
149 
150 	/** Maximal stack size */
151 	const INT_32    iMaxStackSize;
152 	/** Current stack size */
153 	INT_32          iStackPointer;
154 	/** Base pointers      */
155 	STLW::vector<INT_32> vBasePointers;
156 
157 	/** Stack array        */
158 	CDT           * aStack;
159 
160 	/**
161 	  @brief Get stack frame from top of stack
162 	  @param iTopOffset - offset of frame
163 	  @return pointer to start of frame
164 	*/
GetStackFrame(const INT_32 iTopOffset=0)165 	inline CDT * GetStackFrame(const INT_32  iTopOffset = 0) { return &aStack[iStackPointer + iTopOffset]; }
166 
167 };
168 
169 } // namespace CTPP
170 #endif // _CTPP2_VM_ARG_STACK_HPP__
171 // End.
172