private void InterpretBool() { StackFrameMemory stack = PeekCallStack().Memory; int val = stack.PopValInt(); stack.PushValInt(val == 0 ? 0 : 1); }
private void InterpretLogicalNot() { StackFrameMemory stack = PeekCallStack().Memory; int val = stack.PopValInt(); stack.PushValInt(val == 0 ? 1 : 0); }
private void InterpretDeref(Instruction ins) { StackFrameMemory memory = PeekCallStack().Memory; IntPtr addr = memory.PopPointer(); memory.PushIndirect(addr, ins.SizeValue); }
private void InterpretAddressOf(Instruction ins) { StackFrameMemory memory = PeekCallStack().Memory; IntPtr addr = memory.AddressOf(ins.ImmediateValue); memory.PushPointer(addr); }
private void InterpretMod() { StackFrameMemory stack = PeekCallStack().Memory; int a = stack.PopValInt(); int b = stack.PopValInt(); stack.PushValInt(a % b); }
private void InterpretWrite(Instruction ins) { StackFrameMemory stack = PeekCallStack().Memory; int sourcePointer = stack.StackPointer - ins.SizeValue; for (int i = 0; i < ins.SizeValue; ++i) { stack.Write(ins.ImmediateValue + i, stack.Read(sourcePointer++)); } }
public void CopyFrom(StackFrameMemory source, int sourceOffset, int destOffset, int size) { unsafe { if (sourceOffset + size > source._capacity || sourceOffset < 0 || destOffset < 0) { throw new IndexOutOfRangeException(); } void *sourcePtr = IntPtr.Add(source._data, sourceOffset).ToPointer(); void *destPtr = IntPtr.Add(_data, destOffset).ToPointer(); Buffer.MemoryCopy(sourcePtr, destPtr, _capacity - destOffset, size); } }
public void PushFrom(StackFrameMemory source, int sourcePointer, int size) { unsafe { if (sourcePointer + size > source._capacity || sourcePointer < 0 || _stackPointer < 0) { throw new IndexOutOfRangeException(); } void *sourcePtr = IntPtr.Add(source._data, sourcePointer).ToPointer(); void *destPtr = IntPtr.Add(_data, _stackPointer).ToPointer(); Buffer.MemoryCopy(sourcePtr, destPtr, _capacity - _stackPointer, size); } _stackPointer += size; }
private void PushCall(VMModule module, StackFrameMemory sourceStack) { int functionIndex = sourceStack.PopValInt(); GetFunctionStackSizes(module, functionIndex, out int returnValueSize, out int argumentMemorySize, out int stackSize); StackFrame frame = AcquireFrame(stackSize); frame.Module = module; frame.Function = functionIndex; frame.Memory.CopyFrom(sourceStack, sourceStack.StackPointer - argumentMemorySize, returnValueSize, argumentMemorySize); sourceStack.Discard(argumentMemorySize); PushCallStack(frame); // TODO: PERF: Try to optimize out calls to HandleModuleAdded when we can deduce that the module has already // been seen. // For calls pushed by the CALL instruction, we know that the module will be the same and that we don't need // to call this function. _debugger?.HandleModuleAdded(module); }