Skip to content

Commit

Permalink
Virtual machine
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Robertshaw committed Sep 9, 2012
1 parent ba822dc commit cd6c193
Show file tree
Hide file tree
Showing 6 changed files with 1,104 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/tests/VirtualMachineTest.cpp
@@ -0,0 +1,11 @@
#include <iostream>
#include "virtualmachine/VirtualMachine.h"

int main(int argc, char * argv[])
{
vm::VirtualMachine * vm = new vm::VirtualMachine(2);
vm->LoadProgram("test.qvm");
vm->Call(0/*, 0, 88, 12*/);
std::cout << "Return value: " << vm->Pop<vm::uint4_t>() << std::endl;
//vm->Run();
}
69 changes: 69 additions & 0 deletions src/virtualmachine/Exceptions.h
@@ -0,0 +1,69 @@
#pragma once
#include <stdexcept>
#include <cstring>
#include "Format.h"

namespace vm
{
class RuntimeException: public std::exception
{
public:
RuntimeException() {}
const char * what() const throw()
{
return "VirtualMachine runtime exception";
}
~RuntimeException() throw() {};
};

class StackOverflowException: public RuntimeException
{
public:
StackOverflowException() {}
const char * what() const throw()
{
return "VirtualMachine Stack overflow";
}
~StackOverflowException() throw() {};
};

class StackUnderflowException: public RuntimeException
{
public:
StackUnderflowException() {}
const char * what() const throw()
{
return "VirtualMachine Stack underflow";
}
~StackUnderflowException() throw() {};
};

class AccessViolationException: public RuntimeException
{
int address;
char * _what;
public:
AccessViolationException(int address = 0) : address(address)
{
_what = strdup(std::string("VirtualMachine Access violation at "+format::NumberToString<int>(address)).c_str());
}
const char * what() const throw()
{
if(address)
return _what;
return "VirtualMachine Access violation";
}
~AccessViolationException() throw() {};
};

class OutOfMemoryException: public RuntimeException
{
public:
OutOfMemoryException() {}
const char * what() const throw()
{
return "VirtualMachine Out of memory";
}
~OutOfMemoryException() throw() {};
};
}
60 changes: 60 additions & 0 deletions src/virtualmachine/OpCodes.inl
@@ -0,0 +1,60 @@
OPDEF(UNDEF)
OPDEF(IGNORE) /* no-op */
OPDEF(BREAK) /* ??? */
OPDEF(ENTER) /* Begin subroutine. */
OPDEF(LEAVE) /* End subroutine. */
OPDEF(CALL) /* Call subroutine. */
OPDEF(PUSH) /* push to stack. */
OPDEF(POP) /* discard top-of-stack. */
OPDEF(CONST) /* load constant to stack. */
OPDEF(LOCAL) /* get local variable. */
OPDEF(JUMP) /* unconditional jump. */
OPDEF(EQ) /* compare integers, jump if equal. */
OPDEF(NE) /* compare integers, jump if not equal. */
OPDEF(LTI) /* compare integers, jump if less-than. */
OPDEF(LEI) /* compare integers, jump if less-than-or-equal. */
OPDEF(GTI) /* compare integers, jump if greater-than. */
OPDEF(GEI) /* compare integers, jump if greater-than-or-equal. */
OPDEF(LTU) /* compare unsigned integers, jump if less-than */
OPDEF(LEU) /* compare unsigned integers, jump if less-than-or-equal */
OPDEF(GTU) /* compare unsigned integers, jump if greater-than */
OPDEF(GEU) /* compare unsigned integers, jump if greater-than-or-equal */
OPDEF(EQF) /* compare floats, jump if equal */
OPDEF(NEF) /* compare floats, jump if not-equal */
OPDEF(LTF) /* compare floats, jump if less-than */
OPDEF(LEF) /* compare floats, jump if less-than-or-equal */
OPDEF(GTF) /* compare floats, jump if greater-than */
OPDEF(GEF) /* compare floats, jump if greater-than-or-equal */
OPDEF(LOAD1) /* load 1-byte from memory */
OPDEF(LOAD2) /* load 2-byte from memory */
OPDEF(LOAD4) /* load 4-byte from memory */
OPDEF(STORE1) /* store 1-byte to memory */
OPDEF(STORE2) /* store 2-byte to memory */
OPDEF(STORE4) /* store 4-byte to memory */
OPDEF(ARG) /* marshal argument */
OPDEF(BLOCK_COPY) /* block copy... */
OPDEF(SEX8) /* Pedophilia */
OPDEF(SEX16) /* Sign-Extend 16-bit */
OPDEF(NEGI) /* Negate integer. */
OPDEF(ADD) /* Add integers (two's complement). */
OPDEF(SUB) /* Subtract integers (two's complement). */
OPDEF(DIVI) /* Divide signed integers. */
OPDEF(DIVU) /* Divide unsigned integers. */
OPDEF(MODI) /* Modulus (signed). */
OPDEF(MODU) /* Modulus (unsigned). */
OPDEF(MULI) /* Multiply signed integers. */
OPDEF(MULU) /* Multiply unsigned integers. */
OPDEF(BAND) /* Bitwise AND */
OPDEF(BOR) /* Bitwise OR */
OPDEF(BXOR) /* Bitwise eXclusive-OR */
OPDEF(BCOM) /* Bitwise COMplement */
OPDEF(LSH) /* Left-shift */
OPDEF(RSHI) /* Right-shift (algebraic; preserve sign) */
OPDEF(RSHU) /* Right-shift (bitwise; ignore sign) */
OPDEF(NEGF) /* Negate float */
OPDEF(ADDF) /* Add floats */
OPDEF(SUBF) /* Subtract floats */
OPDEF(DIVF) /* Divide floats */
OPDEF(MULF) /* Multiply floats */
OPDEF(CVIF) /* Convert to integer from float */
OPDEF(CVFI) /* Convert to float from integer */
57 changes: 57 additions & 0 deletions src/virtualmachine/Traps.inl
@@ -0,0 +1,57 @@
TRAPDEF(-1, Print)
TRAPDEF(-2, Error)
/*MAPTRAP(-3, Milliseconds)
MAPTRAP(-4, Cvar_Register)
MAPTRAP(-5, Cvar_Update)
MAPTRAP(-6, Cvar_Set)
MAPTRAP(-7, Cvar_VariableIntegerValue)
MAPTRAP(-8, Cvar_VariableStringBuffer)
MAPTRAP(-9, Argc)
MAPTRAP(-10, Argv)
MAPTRAP(-11, FS_FOpenFile)
MAPTRAP(-12, FS_Read)
MAPTRAP(-13, FS_Write)
MAPTRAP(-14, FS_FCloseFile)
MAPTRAP(-15, SendConsoleCommand)
MAPTRAP(-16, LocateGameData)
MAPTRAP(-17, DropClient)
MAPTRAP(-18, SendServerCommand)
MAPTRAP(-19, SetConfigstring)
MAPTRAP(-20, GetConfigstring)
MAPTRAP(-21, GetUserinfo)
MAPTRAP(-22, SetUserinfo)
MAPTRAP(-23, GetServerinfo)
MAPTRAP(-24, SetBrushModel)
MAPTRAP(-25, Trace)
MAPTRAP(-26, PointContents)
MAPTRAP(-27, InPVS)
MAPTRAP(-28, InPVSIgnorePortals)
MAPTRAP(-29, AdjustAreaPortalState)
MAPTRAP(-30, AreasConnected)
MAPTRAP(-31, LinkEntity)
MAPTRAP(-32, UnlinkEntity)
MAPTRAP(-33, EntitiesInBox)
MAPTRAP(-34, EntityContact)
MAPTRAP(-35, BotAllocateClient)
MAPTRAP(-36, BotFreeClient)
MAPTRAP(-37, GetUsercmd)
MAPTRAP(-38, GetEntityToken)
MAPTRAP(-39, FS_GetFileList)
MAPTRAP(-40, DebugPolygonCreate)
MAPTRAP(-41, DebugPolygonDelete)
MAPTRAP(-42, RealTime)
MAPTRAP(-43, SnapVector)
MAPTRAP(-44, TraceCapsule)
MAPTRAP(-45, EntityContactCapsule)
MAPTRAP(-46, FS_Seek)
MAPTRAP(-101, memset)
MAPTRAP(-102, memcpy)
MAPTRAP(-103, strncpy)
MAPTRAP(-104, sin)
MAPTRAP(-105, cos)
MAPTRAP(-106, atan2)
MAPTRAP(-107, sqrt)
MAPTRAP(-111, floor)
MAPTRAP(-112, ceil)
MAPTRAP(-113, testPrintInt)
MAPTRAP(-114, testPrintFloat)*/

0 comments on commit cd6c193

Please sign in to comment.