2016-12-18 04:43:04 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
Copyright (C) 1999-2005 Id Software, Inc.
|
|
|
|
|
|
|
|
This file is part of Quake III Arena source code.
|
|
|
|
|
|
|
|
Quake III Arena source code is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
Quake III Arena source code is distributed in the hope that it will be
|
|
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Quake III Arena source code; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
#include "q_shared.h"
|
|
|
|
#include "qcommon.h"
|
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
#define MAX_OPSTACK_SIZE 512
|
|
|
|
#define PROC_OPSTACK_SIZE 30
|
|
|
|
|
|
|
|
#define VMMAIN_CALL_ARGS 13
|
|
|
|
|
|
|
|
// hardcoded in q3asm and reserved at end of bss
|
|
|
|
#define PROGRAM_STACK_SIZE 0x10000
|
2016-12-18 04:43:04 +00:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
OP_UNDEF,
|
|
|
|
|
|
|
|
OP_IGNORE,
|
|
|
|
|
|
|
|
OP_BREAK,
|
|
|
|
|
|
|
|
OP_ENTER,
|
|
|
|
OP_LEAVE,
|
|
|
|
OP_CALL,
|
|
|
|
OP_PUSH,
|
|
|
|
OP_POP,
|
|
|
|
|
|
|
|
OP_CONST,
|
|
|
|
OP_LOCAL,
|
|
|
|
|
|
|
|
OP_JUMP,
|
|
|
|
|
|
|
|
//-------------------
|
|
|
|
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04 +00:00
|
|
|
} opcode_t;
|
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
// macro opcode sequences
|
|
|
|
typedef enum {
|
|
|
|
MOP_UNDEF = OP_MAX,
|
|
|
|
MOP_IGNORE4,
|
|
|
|
MOP_ADD4,
|
|
|
|
MOP_SUB4,
|
|
|
|
MOP_BAND4,
|
|
|
|
MOP_BOR4,
|
|
|
|
MOP_CALCF4,
|
|
|
|
} macro_op_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int value;
|
|
|
|
byte op;
|
|
|
|
byte opStack;
|
|
|
|
byte jused; // boolean
|
|
|
|
byte swtch; // boolean
|
|
|
|
} instruction_t;
|
|
|
|
|
|
|
|
extern const char *opname[OP_MAX];
|
|
|
|
|
|
|
|
typedef int vmptr_t;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
|
|
|
typedef struct vmSymbol_s {
|
|
|
|
struct vmSymbol_s *next;
|
|
|
|
int symValue;
|
|
|
|
int profileCount;
|
|
|
|
char symName[1]; // variable sized
|
|
|
|
} vmSymbol_t;
|
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
typedef union vmFunc_u {
|
|
|
|
byte *ptr;
|
|
|
|
void (*func)(void);
|
|
|
|
} vmFunc_t;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
2017-03-14 04:11:12 +00:00
|
|
|
#define MAX_VM_CALL_STACK_DEPTH 64
|
|
|
|
|
2016-12-18 04:43:04 +00:00
|
|
|
struct vm_s {
|
|
|
|
// DO NOT MOVE OR CHANGE THESE WITHOUT CHANGING THE VM_OFFSET_* DEFINES
|
|
|
|
// USED BY THE ASM CODE
|
|
|
|
int programStack; // the vm may be recursively entered
|
2017-01-11 13:24:58 +00:00
|
|
|
syscall_t systemCall;
|
|
|
|
byte *dataBase;
|
|
|
|
int *opStack; // pointer to local function stack
|
|
|
|
|
|
|
|
int instructionCount;
|
|
|
|
intptr_t *instructionPointers;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
const char *name;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
|
|
|
// for dynamic linked modules
|
|
|
|
void *dllHandle;
|
2017-01-11 13:24:58 +00:00
|
|
|
dllSyscall_t entryPoint;
|
|
|
|
void (*destroy)(vm_t* self);
|
2016-12-18 04:43:04 +00:00
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
// for interpreted modules
|
|
|
|
qboolean currentlyInterpreting;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
qboolean compiled;
|
|
|
|
|
|
|
|
vmFunc_t codeBase;
|
|
|
|
int codeLength;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
2017-03-08 15:10:34 +00:00
|
|
|
int allocSize; // total allocation size, in bytes
|
|
|
|
|
2016-12-18 04:43:04 +00:00
|
|
|
int dataMask;
|
2017-01-11 13:24:58 +00:00
|
|
|
int dataLength; // exact data segment length
|
|
|
|
|
|
|
|
int stackBottom; // if programStack < stackBottom, error
|
|
|
|
int *opStackTop;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
|
|
|
int numSymbols;
|
|
|
|
vmSymbol_t *symbols;
|
|
|
|
|
2017-03-08 15:10:34 +00:00
|
|
|
int callLevel; // counts recursive VM_Call
|
2016-12-18 04:43:04 +00:00
|
|
|
int breakFunction; // increment breakCount on function entry to this
|
|
|
|
int breakCount;
|
|
|
|
|
|
|
|
byte *jumpTableTargets;
|
|
|
|
int numJumpTableTargets;
|
2017-01-11 13:24:58 +00:00
|
|
|
|
|
|
|
vmIndex_t index;
|
2017-03-14 04:11:12 +00:00
|
|
|
|
|
|
|
int callStackDepth;
|
|
|
|
int lastCallStackDepth;
|
|
|
|
int callStackDepthTemp; // only for vm_x86.cpp
|
|
|
|
int callStack[MAX_VM_CALL_STACK_DEPTH];
|
2016-12-18 04:43:04 +00:00
|
|
|
};
|
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
extern vm_t *currentVM;
|
2016-12-18 04:43:04 +00:00
|
|
|
|
|
|
|
#define VM_MAGIC 0x12721444
|
|
|
|
typedef struct {
|
|
|
|
int vmMagic;
|
|
|
|
|
|
|
|
int instructionCount;
|
|
|
|
|
|
|
|
int codeOffset;
|
|
|
|
int codeLength;
|
|
|
|
|
|
|
|
int dataOffset;
|
|
|
|
int dataLength;
|
|
|
|
int litLength; // ( dataLength - litLength ) should be byteswapped on load
|
|
|
|
int bssLength; // zero filled memory appended to datalength
|
|
|
|
} vmHeader_t;
|
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
qboolean VM_Compile( vm_t *vm, vmHeader_t *header );
|
|
|
|
int VM_CallCompiled( vm_t *vm, int *args );
|
2016-12-18 04:43:04 +00:00
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
qboolean VM_PrepareInterpreter2( vm_t *vm, vmHeader_t *header );
|
|
|
|
int VM_CallInterpreted2( vm_t *vm, int *args );
|
2016-12-18 04:43:04 +00:00
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
const char *VM_LoadInstructions( const vmHeader_t *header, instruction_t *buf );
|
|
|
|
const char *VM_CheckInstructions( instruction_t *buf, int instructionCount,
|
|
|
|
const byte *jumpTableTargets,
|
|
|
|
int numJumpTableTargets,
|
|
|
|
int dataLength );
|
2016-12-18 04:43:04 +00:00
|
|
|
|
|
|
|
intptr_t VM_ArgPtr( intptr_t intValue );
|
|
|
|
intptr_t VM_ExplicitArgPtr( const vm_t* vm, intptr_t intValue );
|
|
|
|
|
|
|
|
static ID_INLINE float _vmf(intptr_t x)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
intptr_t l;
|
|
|
|
float f;
|
|
|
|
} t;
|
|
|
|
t.l = x;
|
|
|
|
return t.f;
|
|
|
|
}
|
|
|
|
#define VMF(x) _vmf(args[x])
|
|
|
|
|
2017-01-11 13:24:58 +00:00
|
|
|
#define VM_OF_JUMP (1<<0)
|
|
|
|
|
|
|
|
typedef struct opcode_info_s
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int stack;
|
|
|
|
int nargs;
|
|
|
|
int flags;
|
|
|
|
} opcode_info_t ;
|
|
|
|
|
|
|
|
extern opcode_info_t ops[ OP_MAX ];
|
|
|
|
|