1*0957b409SSimon J. Gerraty /* 2*0957b409SSimon J. Gerraty * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org> 3*0957b409SSimon J. Gerraty * 4*0957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining 5*0957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the 6*0957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including 7*0957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish, 8*0957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to 9*0957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to 10*0957b409SSimon J. Gerraty * the following conditions: 11*0957b409SSimon J. Gerraty * 12*0957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be 13*0957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software. 14*0957b409SSimon J. Gerraty * 15*0957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*0957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*0957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*0957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*0957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*0957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*0957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*0957b409SSimon J. Gerraty * SOFTWARE. 23*0957b409SSimon J. Gerraty */ 24*0957b409SSimon J. Gerraty 25*0957b409SSimon J. Gerraty using System; 26*0957b409SSimon J. Gerraty using System.Collections.Generic; 27*0957b409SSimon J. Gerraty 28*0957b409SSimon J. Gerraty /* 29*0957b409SSimon J. Gerraty * Execution of code during compilation is done in a virtual CPU 30*0957b409SSimon J. Gerraty * incarnated by this class, that contains the relevant registers. 31*0957b409SSimon J. Gerraty * 32*0957b409SSimon J. Gerraty * Accesses to the data on the stack are mapped to accesses to an 33*0957b409SSimon J. Gerraty * internal array, with no explicit control on boundaries. Since the 34*0957b409SSimon J. Gerraty * internal array may be larger than the actual stack contents, 35*0957b409SSimon J. Gerraty * nonsensical accesses may still "work" to some extent. The whole 36*0957b409SSimon J. Gerraty * thing won't derail beyond the CLR VM, though. 37*0957b409SSimon J. Gerraty */ 38*0957b409SSimon J. Gerraty 39*0957b409SSimon J. Gerraty class CPU { 40*0957b409SSimon J. Gerraty 41*0957b409SSimon J. Gerraty /* 42*0957b409SSimon J. Gerraty * Next instruction to execute is in ipBuf[ipOff]. 43*0957b409SSimon J. Gerraty */ 44*0957b409SSimon J. Gerraty internal Opcode[] ipBuf; 45*0957b409SSimon J. Gerraty internal int ipOff; 46*0957b409SSimon J. Gerraty 47*0957b409SSimon J. Gerraty /* 48*0957b409SSimon J. Gerraty * stackBuf and stackPtr implement the data stack. The system 49*0957b409SSimon J. Gerraty * stack uses frames; 'rsp' points to the current top frame. 50*0957b409SSimon J. Gerraty */ 51*0957b409SSimon J. Gerraty TValue[] stackBuf; 52*0957b409SSimon J. Gerraty int stackPtr; 53*0957b409SSimon J. Gerraty Frame rsp; 54*0957b409SSimon J. Gerraty CPU()55*0957b409SSimon J. Gerraty internal CPU() 56*0957b409SSimon J. Gerraty { 57*0957b409SSimon J. Gerraty stackBuf = new TValue[16]; 58*0957b409SSimon J. Gerraty stackPtr = -1; 59*0957b409SSimon J. Gerraty rsp = null; 60*0957b409SSimon J. Gerraty } 61*0957b409SSimon J. Gerraty 62*0957b409SSimon J. Gerraty /* 63*0957b409SSimon J. Gerraty * Enter a function, reserving space for 'numLocals' local variables. 64*0957b409SSimon J. Gerraty */ Enter(Opcode[] code, int numLocals)65*0957b409SSimon J. Gerraty internal void Enter(Opcode[] code, int numLocals) 66*0957b409SSimon J. Gerraty { 67*0957b409SSimon J. Gerraty Frame f = new Frame(rsp, numLocals); 68*0957b409SSimon J. Gerraty rsp = f; 69*0957b409SSimon J. Gerraty f.savedIpBuf = ipBuf; 70*0957b409SSimon J. Gerraty f.savedIpOff = ipOff; 71*0957b409SSimon J. Gerraty ipBuf = code; 72*0957b409SSimon J. Gerraty ipOff = 0; 73*0957b409SSimon J. Gerraty } 74*0957b409SSimon J. Gerraty 75*0957b409SSimon J. Gerraty /* 76*0957b409SSimon J. Gerraty * Exit the current function. 77*0957b409SSimon J. Gerraty */ Exit()78*0957b409SSimon J. Gerraty internal void Exit() 79*0957b409SSimon J. Gerraty { 80*0957b409SSimon J. Gerraty ipBuf = rsp.savedIpBuf; 81*0957b409SSimon J. Gerraty ipOff = rsp.savedIpOff; 82*0957b409SSimon J. Gerraty rsp = rsp.upper; 83*0957b409SSimon J. Gerraty } 84*0957b409SSimon J. Gerraty 85*0957b409SSimon J. Gerraty /* 86*0957b409SSimon J. Gerraty * Get the current stack depth (number of elements). 87*0957b409SSimon J. Gerraty */ 88*0957b409SSimon J. Gerraty internal int Depth { 89*0957b409SSimon J. Gerraty get { 90*0957b409SSimon J. Gerraty return stackPtr + 1; 91*0957b409SSimon J. Gerraty } 92*0957b409SSimon J. Gerraty } 93*0957b409SSimon J. Gerraty 94*0957b409SSimon J. Gerraty /* 95*0957b409SSimon J. Gerraty * Pop a value from the stack. 96*0957b409SSimon J. Gerraty */ Pop()97*0957b409SSimon J. Gerraty internal TValue Pop() 98*0957b409SSimon J. Gerraty { 99*0957b409SSimon J. Gerraty return stackBuf[stackPtr --]; 100*0957b409SSimon J. Gerraty } 101*0957b409SSimon J. Gerraty 102*0957b409SSimon J. Gerraty /* 103*0957b409SSimon J. Gerraty * Push a value on the stack. 104*0957b409SSimon J. Gerraty */ Push(TValue v)105*0957b409SSimon J. Gerraty internal void Push(TValue v) 106*0957b409SSimon J. Gerraty { 107*0957b409SSimon J. Gerraty int len = stackBuf.Length; 108*0957b409SSimon J. Gerraty if (++ stackPtr == len) { 109*0957b409SSimon J. Gerraty TValue[] nbuf = new TValue[len << 1]; 110*0957b409SSimon J. Gerraty Array.Copy(stackBuf, 0, nbuf, 0, len); 111*0957b409SSimon J. Gerraty stackBuf = nbuf; 112*0957b409SSimon J. Gerraty } 113*0957b409SSimon J. Gerraty stackBuf[stackPtr] = v; 114*0957b409SSimon J. Gerraty } 115*0957b409SSimon J. Gerraty 116*0957b409SSimon J. Gerraty /* 117*0957b409SSimon J. Gerraty * Look at the value at depth 'depth' (0 is top of stack). The 118*0957b409SSimon J. Gerraty * stack is unchanged. 119*0957b409SSimon J. Gerraty */ Peek(int depth)120*0957b409SSimon J. Gerraty internal TValue Peek(int depth) 121*0957b409SSimon J. Gerraty { 122*0957b409SSimon J. Gerraty return stackBuf[stackPtr - depth]; 123*0957b409SSimon J. Gerraty } 124*0957b409SSimon J. Gerraty 125*0957b409SSimon J. Gerraty /* 126*0957b409SSimon J. Gerraty * Rotate the stack at depth 'depth': the value at that depth 127*0957b409SSimon J. Gerraty * is moved to the top of stack. 128*0957b409SSimon J. Gerraty */ Rot(int depth)129*0957b409SSimon J. Gerraty internal void Rot(int depth) 130*0957b409SSimon J. Gerraty { 131*0957b409SSimon J. Gerraty TValue v = stackBuf[stackPtr - depth]; 132*0957b409SSimon J. Gerraty Array.Copy(stackBuf, stackPtr - (depth - 1), 133*0957b409SSimon J. Gerraty stackBuf, stackPtr - depth, depth); 134*0957b409SSimon J. Gerraty stackBuf[stackPtr] = v; 135*0957b409SSimon J. Gerraty } 136*0957b409SSimon J. Gerraty 137*0957b409SSimon J. Gerraty /* 138*0957b409SSimon J. Gerraty * Inverse-rotate the stack at depth 'depth': the value at the 139*0957b409SSimon J. Gerraty * top of stack is moved to that depth. 140*0957b409SSimon J. Gerraty */ NRot(int depth)141*0957b409SSimon J. Gerraty internal void NRot(int depth) 142*0957b409SSimon J. Gerraty { 143*0957b409SSimon J. Gerraty TValue v = stackBuf[stackPtr]; 144*0957b409SSimon J. Gerraty Array.Copy(stackBuf, stackPtr - depth, 145*0957b409SSimon J. Gerraty stackBuf, stackPtr - (depth - 1), depth); 146*0957b409SSimon J. Gerraty stackBuf[stackPtr - depth] = v; 147*0957b409SSimon J. Gerraty } 148*0957b409SSimon J. Gerraty 149*0957b409SSimon J. Gerraty /* 150*0957b409SSimon J. Gerraty * Get the current contents of the local variable 'num'. 151*0957b409SSimon J. Gerraty */ GetLocal(int num)152*0957b409SSimon J. Gerraty internal TValue GetLocal(int num) 153*0957b409SSimon J. Gerraty { 154*0957b409SSimon J. Gerraty return rsp.locals[num]; 155*0957b409SSimon J. Gerraty } 156*0957b409SSimon J. Gerraty 157*0957b409SSimon J. Gerraty /* 158*0957b409SSimon J. Gerraty * Set the contents of the local variable 'num'. 159*0957b409SSimon J. Gerraty */ PutLocal(int num, TValue v)160*0957b409SSimon J. Gerraty internal void PutLocal(int num, TValue v) 161*0957b409SSimon J. Gerraty { 162*0957b409SSimon J. Gerraty rsp.locals[num] = v; 163*0957b409SSimon J. Gerraty } 164*0957b409SSimon J. Gerraty 165*0957b409SSimon J. Gerraty /* 166*0957b409SSimon J. Gerraty * The system stack really is a linked list of Frame instances. 167*0957b409SSimon J. Gerraty */ 168*0957b409SSimon J. Gerraty class Frame { 169*0957b409SSimon J. Gerraty 170*0957b409SSimon J. Gerraty internal Frame upper; 171*0957b409SSimon J. Gerraty internal Opcode[] savedIpBuf; 172*0957b409SSimon J. Gerraty internal int savedIpOff; 173*0957b409SSimon J. Gerraty internal TValue[] locals; 174*0957b409SSimon J. Gerraty Frame(Frame upper, int numLocals)175*0957b409SSimon J. Gerraty internal Frame(Frame upper, int numLocals) 176*0957b409SSimon J. Gerraty { 177*0957b409SSimon J. Gerraty this.upper = upper; 178*0957b409SSimon J. Gerraty locals = new TValue[numLocals]; 179*0957b409SSimon J. Gerraty } 180*0957b409SSimon J. Gerraty } 181*0957b409SSimon J. Gerraty } 182