2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 using System.Collections.Generic;
29 * Execution of code during compilation is done in a virtual CPU
30 * incarnated by this class, that contains the relevant registers.
32 * Accesses to the data on the stack are mapped to accesses to an
33 * internal array, with no explicit control on boundaries. Since the
34 * internal array may be larger than the actual stack contents,
35 * nonsensical accesses may still "work" to some extent. The whole
36 * thing won't derail beyond the CLR VM, though.
42 * Next instruction to execute is in ipBuf[ipOff].
44 internal Opcode[] ipBuf;
48 * stackBuf and stackPtr implement the data stack. The system
49 * stack uses frames; 'rsp' points to the current top frame.
57 stackBuf = new TValue[16];
63 * Enter a function, reserving space for 'numLocals' local variables.
65 internal void Enter(Opcode[] code, int numLocals)
67 Frame f = new Frame(rsp, numLocals);
76 * Exit the current function.
80 ipBuf = rsp.savedIpBuf;
81 ipOff = rsp.savedIpOff;
86 * Get the current stack depth (number of elements).
95 * Pop a value from the stack.
99 return stackBuf[stackPtr --];
103 * Push a value on the stack.
105 internal void Push(TValue v)
107 int len = stackBuf.Length;
108 if (++ stackPtr == len) {
109 TValue[] nbuf = new TValue[len << 1];
110 Array.Copy(stackBuf, 0, nbuf, 0, len);
113 stackBuf[stackPtr] = v;
117 * Look at the value at depth 'depth' (0 is top of stack). The
118 * stack is unchanged.
120 internal TValue Peek(int depth)
122 return stackBuf[stackPtr - depth];
126 * Rotate the stack at depth 'depth': the value at that depth
127 * is moved to the top of stack.
129 internal void Rot(int depth)
131 TValue v = stackBuf[stackPtr - depth];
132 Array.Copy(stackBuf, stackPtr - (depth - 1),
133 stackBuf, stackPtr - depth, depth);
134 stackBuf[stackPtr] = v;
138 * Inverse-rotate the stack at depth 'depth': the value at the
139 * top of stack is moved to that depth.
141 internal void NRot(int depth)
143 TValue v = stackBuf[stackPtr];
144 Array.Copy(stackBuf, stackPtr - depth,
145 stackBuf, stackPtr - (depth - 1), depth);
146 stackBuf[stackPtr - depth] = v;
150 * Get the current contents of the local variable 'num'.
152 internal TValue GetLocal(int num)
154 return rsp.locals[num];
158 * Set the contents of the local variable 'num'.
160 internal void PutLocal(int num, TValue v)
166 * The system stack really is a linked list of Frame instances.
170 internal Frame upper;
171 internal Opcode[] savedIpBuf;
172 internal int savedIpOff;
173 internal TValue[] locals;
175 internal Frame(Frame upper, int numLocals)
178 locals = new TValue[numLocals];