Merging master with newly merged executor

This commit is contained in:
Wolfgang Bumiller 2012-07-27 19:17:03 +02:00
commit b76ffe8304
5 changed files with 1226 additions and 20 deletions

View file

@ -24,17 +24,25 @@ OBJ = \
OBJ_A = test/ast-test.o OBJ_A = test/ast-test.o
OBJ_I = test/ir-test.o OBJ_I = test/ir-test.o
OBJ_C = main.o lexer.o parser.o OBJ_C = main.o lexer.o parser.o
OBJ_X = exec-standalone.o util.o
#default is compiler only #default is compiler only
default: gmqcc default: gmqcc
%.o: %.c %.o: %.c
$(CC) -c $< -o $@ $(CFLAGS) $(CC) -c $< -o $@ $(CFLAGS)
exec-standalone.o: exec.c
$(CC) -c $< -o $@ $(CFLAGS) -DQCVM_EXECUTOR=1
# test targets # test targets
test_ast: $(OBJ_A) $(OBJ) test_ast: $(OBJ_A) $(OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(CC) -o $@ $^ $(CFLAGS)
test_ir: $(OBJ_I) $(OBJ) test_ir: $(OBJ_I) $(OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(CC) -o $@ $^ $(CFLAGS)
qcvm: $(OBJ_X)
$(CC) -o $@ $^ $(CFLAGS)
exec.o: execloop.h
exec-standalone.o: execloop.h
test: test_ast test_ir test: test_ast test_ir
# compiler target # compiler target
@ -45,6 +53,6 @@ gmqcc: $(OBJ_C) $(OBJ)
all: test gmqcc all: test gmqcc
clean: clean:
rm -f *.o gmqcc test_ast test_ir test/*.o rm -f *.o gmqcc qcvm test_ast test_ir test/*.o

19
code.c
View file

@ -22,25 +22,6 @@
*/ */
#include "gmqcc.h" #include "gmqcc.h"
typedef struct {
uint32_t offset; /* Offset in file of where data begins */
uint32_t length; /* Length of section (how many of) */
} prog_section;
typedef struct {
uint32_t version; /* Program version (6) */
uint16_t crc16; /* What is this? */
uint16_t skip; /* see propsal.txt */
prog_section statements; /* prog_section_statement */
prog_section defs; /* prog_section_def */
prog_section fields; /* prog_section_field */
prog_section functions; /* prog_section_function */
prog_section strings; /* What is this? */
prog_section globals; /* What is this? */
uint32_t entfield; /* Number of entity fields */
} prog_header;
/* /*
* The macros below expand to a typesafe vector implementation, which * The macros below expand to a typesafe vector implementation, which
* can be viewed in gmqcc.h * can be viewed in gmqcc.h

487
exec.c Normal file
View file

@ -0,0 +1,487 @@
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include "gmqcc.h"
MEM_VEC_FUNCTIONS(qc_program, prog_section_statement, code)
MEM_VEC_FUNCTIONS(qc_program, prog_section_def, defs)
MEM_VEC_FUNCTIONS(qc_program, prog_section_def, fields)
MEM_VEC_FUNCTIONS(qc_program, prog_section_function, functions)
MEM_VEC_FUNCTIONS(qc_program, char, strings)
_MEM_VEC_FUN_APPEND(qc_program, char, strings)
_MEM_VEC_FUN_RESIZE(qc_program, char, strings)
MEM_VEC_FUNCTIONS(qc_program, qcint, globals)
MEM_VEC_FUNCTIONS(qc_program, qcint, entitydata)
MEM_VEC_FUNCTIONS(qc_program, qcint, localstack)
_MEM_VEC_FUN_APPEND(qc_program, qcint, localstack)
_MEM_VEC_FUN_RESIZE(qc_program, qcint, localstack)
MEM_VEC_FUNCTIONS(qc_program, qc_exec_stack, stack)
MEM_VEC_FUNCTIONS(qc_program, size_t, profile)
_MEM_VEC_FUN_RESIZE(qc_program, size_t, profile)
static void loaderror(const char *fmt, ...)
{
int err = errno;
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf(": %s\n", strerror(err));
}
qc_program* prog_load(const char *filename)
{
qc_program *prog;
prog_header header;
FILE *file;
file = fopen(filename, "rb");
if (!file)
return NULL;
if (fread(&header, sizeof(header), 1, file) != 1) {
loaderror("failed to read header from '%s'", filename);
fclose(file);
return NULL;
}
if (header.version != 6) {
loaderror("header says this is a version %i progs, we need version 6\n", header.version);
fclose(file);
return NULL;
}
prog = (qc_program*)mem_a(sizeof(qc_program));
if (!prog) {
fclose(file);
printf("failed to allocate program data\n");
return NULL;
}
memset(prog, 0, sizeof(*prog));
prog->entityfields = header.entfield;
prog->filename = util_strdup(filename);
if (!prog->filename) {
loaderror("failed to store program name");
goto error;
}
#define read_data(hdrvar, progvar, type) \
if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
loaderror("seek failed"); \
goto error; \
} \
prog->progvar##_alloc = header.hdrvar.length; \
prog->progvar##_count = header.hdrvar.length; \
prog->progvar = (type*)mem_a(header.hdrvar.length * sizeof(*prog->progvar)); \
if (!prog->progvar) \
goto error; \
if (fread(prog->progvar, sizeof(*prog->progvar), header.hdrvar.length, file) \
!= header.hdrvar.length) { \
loaderror("read failed"); \
goto error; \
}
#define read_data1(x, y) read_data(x, x, y)
read_data (statements, code, prog_section_statement);
read_data1(defs, prog_section_def);
read_data1(fields, prog_section_def);
read_data1(functions, prog_section_function);
read_data1(strings, char);
read_data1(globals, qcint);
fclose(file);
/* profile counters */
if (!qc_program_profile_resize(prog, prog->code_count))
goto error;
/* Add tempstring area */
prog->tempstring_start = prog->strings_count;
prog->tempstring_at = prog->strings_count;
if (!qc_program_strings_resize(prog, prog->strings_count + 16*1024))
goto error;
return prog;
error:
if (prog->filename) mem_d(prog->filename);
if (prog->code) mem_d(prog->code);
if (prog->defs) mem_d(prog->defs);
if (prog->fields) mem_d(prog->fields);
if (prog->functions) mem_d(prog->functions);
if (prog->strings) mem_d(prog->strings);
if (prog->globals) mem_d(prog->globals);
if (prog->entitydata) mem_d(prog->entitydata);
mem_d(prog);
return NULL;
}
void prog_delete(qc_program *prog)
{
if (prog->filename) mem_d(prog->filename);
MEM_VECTOR_CLEAR(prog, code);
MEM_VECTOR_CLEAR(prog, defs);
MEM_VECTOR_CLEAR(prog, fields);
MEM_VECTOR_CLEAR(prog, functions);
MEM_VECTOR_CLEAR(prog, strings);
MEM_VECTOR_CLEAR(prog, globals);
MEM_VECTOR_CLEAR(prog, entitydata);
MEM_VECTOR_CLEAR(prog, localstack);
MEM_VECTOR_CLEAR(prog, stack);
MEM_VECTOR_CLEAR(prog, profile);
mem_d(prog);
}
/***********************************************************************
* VM code
*/
char* prog_getstring(qc_program *prog, qcint str)
{
if (str < 0 || str >= prog->strings_count)
return "<<<invalid string>>>";
return prog->strings + str;
}
prog_section_def* prog_entfield(qc_program *prog, qcint off)
{
size_t i;
for (i = 0; i < prog->fields_count; ++i) {
if (prog->fields[i].offset == off)
return (prog->fields + i);
}
return NULL;
}
prog_section_def* prog_getdef(qc_program *prog, qcint off)
{
size_t i;
for (i = 0; i < prog->defs_count; ++i) {
if (prog->defs[i].offset == off)
return (prog->defs + i);
}
return NULL;
}
qcany* prog_getedict(qc_program *prog, qcint e)
{
return (qcany*)(prog->entitydata + (prog->entityfields + e));
}
qcint prog_tempstring(qc_program *prog, const char *_str)
{
/* we don't access it, but the macro-generated functions don't use
* const
*/
char *str = (char*)_str;
size_t len = strlen(str);
size_t at = prog->tempstring_at;
/* when we reach the end we start over */
if (at + len >= prog->strings_count)
at = prog->tempstring_start;
/* when it doesn't fit, reallocate */
if (at + len >= prog->strings_count)
{
prog->strings_count = at;
if (!qc_program_strings_append(prog, str, len+1)) {
prog->vmerror = VMERR_TEMPSTRING_ALLOC;
return 0;
}
return at;
}
/* when it fits, just copy */
memcpy(prog->strings + at, str, len+1);
prog->tempstring_at += len+1;
return at;
}
static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
{
static char spaces[16+1] = " ";
prog_section_def *def;
qcany *value;
int len;
if (!glob)
return;
def = prog_getdef(prog, glob);
value = (qcany*)(&prog->globals[glob]);
if (def) {
len = printf("[%s] ", prog_getstring(prog, def->name));
vtype = def->type;
}
else
len = printf("[#%u] ", glob);
switch (vtype) {
case TYPE_VOID:
case TYPE_ENTITY:
case TYPE_FIELD:
case TYPE_FUNCTION:
case TYPE_POINTER:
len += printf("%i,", value->_int);
break;
case TYPE_VECTOR:
len += printf("'%g %g %g',", value->vector[0],
value->vector[1],
value->vector[2]);
break;
case TYPE_STRING:
len += printf("\"%s\",", prog_getstring(prog, value->string));
break;
case TYPE_FLOAT:
default:
len += printf("%g,", value->_float);
break;
}
if (len < 16) {
spaces[16-len] = 0;
printf(spaces);
spaces[16-len] = ' ';
}
}
static void prog_print_statement(qc_program *prog, prog_section_statement *st)
{
if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
printf("<illegal instruction %d>\n", st->opcode);
return;
}
printf("%-12s", asm_instr[st->opcode].m);
if (st->opcode >= INSTR_IF &&
st->opcode <= INSTR_IFNOT)
{
trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
printf("%d\n", st->o2.s1);
}
else if (st->opcode >= INSTR_CALL0 &&
st->opcode <= INSTR_CALL8)
{
printf("\n");
}
else if (st->opcode == INSTR_GOTO)
{
printf("%i\n", st->o1.s1);
}
else
{
int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
switch (st->opcode)
{
case INSTR_MUL_FV:
t[1] = t[2] = TYPE_VECTOR;
break;
case INSTR_MUL_VF:
t[0] = t[2] = TYPE_VECTOR;
break;
case INSTR_MUL_V:
t[0] = t[1] = TYPE_VECTOR;
break;
case INSTR_ADD_V:
case INSTR_SUB_V:
case INSTR_EQ_V:
case INSTR_NE_V:
t[0] = t[1] = t[2] = TYPE_VECTOR;
break;
case INSTR_EQ_S:
case INSTR_NE_S:
t[0] = t[1] = TYPE_STRING;
break;
case INSTR_STORE_V:
t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
break;
case INSTR_STORE_S:
t[0] = t[1] = TYPE_STRING; t[2] = -1;
break;
}
if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
printf("\n");
}
}
static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
{
qc_exec_stack st;
size_t p, parampos;
/* back up locals */
st.localsp = prog->localstack_count;
st.stmt = prog->statement;
st.function = func;
#ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
if (prog->stack_count)
{
prog_section_function *cur;
cur = prog->stack[prog->stack_count-1].function;
if (cur)
{
qcint *globals = prog->globals + cur->firstlocal;
if (!qc_program_localstack_append(prog, globals, cur->locals))
{
printf("out of memory\n");
exit(1);
}
}
}
#else
{
qcint *globals = prog->globals + func->firstlocal;
if (!qc_program_localstack_append(prog, globals, func->locals))
{
printf("out of memory\n");
exit(1);
}
}
#endif
/* copy parameters */
parampos = func->firstlocal;
for (p = 0; p < func->nargs; ++p)
{
size_t s;
for (s = 0; s < func->argsize[p]; ++s) {
prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
++parampos;
}
}
if (!qc_program_stack_add(prog, st)) {
printf("out of memory\n");
exit(1);
}
return func->entry;
}
static qcint prog_leavefunction(qc_program *prog)
{
prog_section_function *prev = NULL;
size_t oldsp;
qc_exec_stack st = prog->stack[prog->stack_count-1];
#ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
if (prog->stack_count > 1) {
prev = prog->stack[prog->stack_count-2].function;
oldsp = prog->stack[prog->stack_count-2].localsp;
}
#else
prev = prog->stack[prog->stack_count-1].function;
oldsp = prog->stack[prog->stack_count-1].localsp;
#endif
if (prev) {
qcint *globals = prog->globals + prev->firstlocal;
memcpy(globals, prog->localstack + oldsp, prev->locals);
if (!qc_program_localstack_resize(prog, oldsp)) {
printf("out of memory\n");
exit(1);
}
}
if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
printf("out of memory\n");
exit(1);
}
return st.stmt;
}
bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
{
long jumpcount = 0;
prog_section_statement *st;
st = prog->code + prog_enterfunction(prog, func);
--st;
switch (flags)
{
default:
case 0:
{
#define QCVM_PROFILE 0
#define QCVM_TRACE 0
# include "execloop.h"
break;
}
case (VMXF_TRACE):
{
#define QCVM_PROFILE 0
#define QCVM_TRACE 1
# include "execloop.h"
break;
}
case (VMXF_PROFILE):
{
#define QCVM_PROFILE 1
#define QCVM_TRACE 0
# include "execloop.h"
break;
}
case (VMXF_TRACE|VMXF_PROFILE):
{
#define QCVM_PROFILE 1
#define QCVM_TRACE 1
# include "execloop.h"
break;
}
};
cleanup:
prog->localstack_count = 0;
prog->stack_count = 0;
return true;
}
/***********************************************************************
* main for when building the standalone executor
*/
#if defined(QCVM_EXECUTOR)
int main(int argc, char **argv)
{
size_t i;
qcint fnmain = -1;
qc_program *prog;
if (argc != 2) {
printf("usage: %s file\n", argv[0]);
exit(1);
}
prog = prog_load(argv[1]);
if (!prog) {
printf("failed to load program '%s'\n", argv[1]);
exit(1);
}
for (i = 1; i < prog->functions_count; ++i) {
const char *name = prog_getstring(prog, prog->functions[i].name);
printf("Found function: %s\n", name);
if (!strcmp(name, "main"))
fnmain = (qcint)i;
}
if (fnmain > 0)
{
prog_exec(prog, &prog->functions[fnmain], VMXF_TRACE, VM_JUMPS_DEFAULT);
}
else
printf("No main function found\n");
prog_delete(prog);
return 0;
}
#endif

568
execloop.h Normal file
View file

@ -0,0 +1,568 @@
#if 0
/* Expected variables */
qc_program *prog;
#endif
#if !defined(QCVM_PROFILE)
# define QCVM_PROFILE 0
#endif
#if !defined(QCVM_TRACE)
# define QCVM_TRACE 0
#endif
#if !defined(FLOAT_IS_TRUE_FOR_INT)
# define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
#endif
#if !defined(PRVM_ERROR)
# define PRVM_ERROR printf
#endif
#if !defined(PRVM_NAME)
# define PRVM_NAME (prog->filename)
#endif
#if !defined(PROG_GETSTRING)
# define PROG_GETSTRING(x) prog_getstring(prog, (x))
#endif
#if !defined(PROG_ENTFIELD)
# define PROG_ENTFIELD(x) prog_entfield(prog, (x))
#endif
#if !defined(PROG_GETEDICT)
# define PROG_GETEDICT(x) prog_getedict(prog, (x))
#endif
#if !defined(PROG_ENTERFUNCTION)
# define PROG_ENTERFUNCTION(x) prog_enterfunction(prog, (x))
#endif
#if !defined(PROG_LEAVEFUNCTION)
# define PROG_LEAVEFUNCTION() prog_leavefunction(prog)
#endif
#define OPA ( (qcany*) (prog->globals + st->o1.u1) )
#define OPB ( (qcany*) (prog->globals + st->o2.u1) )
#define OPC ( (qcany*) (prog->globals + st->o3.u1) )
#define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
while (1)
{
prog_section_function *newf;
qcany *ed;
qcany *ptr;
++st;
/*
prog->ip++;
st = prog->code + prog->ip;
*/
#if QCVM_PROFILE
prog->profile[st - prog->code]++;
#endif
#if QCVM_TRACE
prog_print_statement(prog, st);
#endif
switch (st->opcode)
{
case INSTR_ADD_F:
OPC->_float = OPA->_float + OPB->_float;
break;
case INSTR_ADD_V:
OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
break;
case INSTR_SUB_F:
OPC->_float = OPA->_float - OPB->_float;
break;
case INSTR_SUB_V:
OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
break;
case INSTR_MUL_F:
OPC->_float = OPA->_float * OPB->_float;
break;
case INSTR_MUL_V:
OPC->_float = OPA->vector[0]*OPB->vector[0] + OPA->vector[1]*OPB->vector[1] + OPA->vector[2]*OPB->vector[2];
break;
case INSTR_MUL_FV:
OPC->vector[0] = OPA->_float * OPB->vector[0];
OPC->vector[1] = OPA->_float * OPB->vector[1];
OPC->vector[2] = OPA->_float * OPB->vector[2];
break;
case INSTR_MUL_VF:
OPC->vector[0] = OPB->_float * OPA->vector[0];
OPC->vector[1] = OPB->_float * OPA->vector[1];
OPC->vector[2] = OPB->_float * OPA->vector[2];
break;
case INSTR_DIV_F:
if( OPB->_float != 0.0f )
{
OPC->_float = OPA->_float / OPB->_float;
}
else
{
OPC->_float = 0.0f;
}
break;
case INSTR_BITAND:
OPC->_float = (int)OPA->_float & (int)OPB->_float;
break;
case INSTR_BITOR:
OPC->_float = (int)OPA->_float | (int)OPB->_float;
break;
case INSTR_GE:
OPC->_float = OPA->_float >= OPB->_float;
break;
case INSTR_LE:
OPC->_float = OPA->_float <= OPB->_float;
break;
case INSTR_GT:
OPC->_float = OPA->_float > OPB->_float;
break;
case INSTR_LT:
OPC->_float = OPA->_float < OPB->_float;
break;
case INSTR_AND:
/* TODO change this back to float, and add AND_I to be used by fteqcc for anything not a float */
OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) && FLOAT_IS_TRUE_FOR_INT(OPB->_int);
break;
case INSTR_OR:
/* TODO change this back to float, and add AND_I to be used by fteqcc for anything not a float */
OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) || FLOAT_IS_TRUE_FOR_INT(OPB->_int);
break;
case INSTR_NOT_F:
OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
break;
case INSTR_NOT_V:
OPC->_float = !OPA->vector[0] && !OPA->vector[1] && !OPA->vector[2];
break;
case INSTR_NOT_S:
OPC->_float = !OPA->string || !*PROG_GETSTRING(OPA->string);
break;
case INSTR_NOT_FNC:
OPC->_float = !OPA->function;
break;
case INSTR_NOT_ENT:
OPC->_float = (OPA->edict == 0);
break;
case INSTR_EQ_F:
OPC->_float = OPA->_float == OPB->_float;
break;
case INSTR_EQ_V:
OPC->_float = (OPA->vector[0] == OPB->vector[0]) &&
(OPA->vector[1] == OPB->vector[1]) &&
(OPA->vector[2] == OPB->vector[2]);
break;
case INSTR_EQ_S:
OPC->_float = !strcmp(PROG_GETSTRING(OPA->string),PROG_GETSTRING(OPB->string));
break;
case INSTR_EQ_E:
OPC->_float = OPA->_int == OPB->_int;
break;
case INSTR_EQ_FNC:
OPC->_float = OPA->function == OPB->function;
break;
case INSTR_NE_F:
OPC->_float = OPA->_float != OPB->_float;
break;
case INSTR_NE_V:
OPC->_float = (OPA->vector[0] != OPB->vector[0]) || (OPA->vector[1] != OPB->vector[1]) || (OPA->vector[2] != OPB->vector[2]);
break;
case INSTR_NE_S:
OPC->_float = strcmp(PROG_GETSTRING(OPA->string),PROG_GETSTRING(OPB->string));
break;
case INSTR_NE_E:
OPC->_float = OPA->_int != OPB->_int;
break;
case INSTR_NE_FNC:
OPC->_float = OPA->function != OPB->function;
break;
/*==================*/
case INSTR_STORE_F:
case INSTR_STORE_ENT:
case INSTR_STORE_FLD:
case INSTR_STORE_S:
case INSTR_STORE_FNC:
OPB->_int = OPA->_int;
break;
case INSTR_STORE_V:
OPB->ivector[0] = OPA->ivector[0];
OPB->ivector[1] = OPA->ivector[1];
OPB->ivector[2] = OPA->ivector[2];
break;
case INSTR_STOREP_F:
case INSTR_STOREP_ENT:
case INSTR_STOREP_FLD:
case INSTR_STOREP_S:
case INSTR_STOREP_FNC:
if (OPB->_int < 0 || OPB->_int >= prog->entitydata_count)
{
PRVM_ERROR("%s attempted to write to an out of bounds edict (%i)", PRVM_NAME, OPB->_int);
goto cleanup;
}
if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
PRVM_ERROR("ERROR: assignment to world.%s (field %i) in %s\n", PROG_GETSTRING(PROG_ENTFIELD(OPB->_int)->name), OPB->_int, PRVM_NAME);
ptr = (qcany*)(prog->entitydata + OPB->_int);
ptr->_int = OPA->_int;
break;
case INSTR_STOREP_V:
if (OPB->_int < 0 || OPB->_int + 3 >= prog->entitydata_count)
{
PRVM_ERROR("%s attempted to write to an out of bounds edict (%i)", PRVM_NAME, OPB->_int);
goto cleanup;
}
if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
PRVM_ERROR("ERROR: assignment to world.%s (field %i) in %s\n", PROG_GETSTRING(PROG_ENTFIELD(OPB->_int)->name), OPB->_int, PRVM_NAME);
ptr = (qcany*)(prog->entitydata + OPB->_int);
ptr->ivector[0] = OPA->ivector[0];
ptr->ivector[1] = OPA->ivector[1];
ptr->ivector[2] = OPA->ivector[2];
break;
case INSTR_ADDRESS:
if (OPA->edict < 0 || OPA->edict >= prog->entities)
{
PRVM_ERROR ("%s Progs attempted to address an out of bounds edict number", PRVM_NAME);
goto cleanup;
}
if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
{
PRVM_ERROR("%s attempted to address an invalid field (%i) in an edict", PRVM_NAME, OPB->_int);
goto cleanup;
}
ed = PROG_GETEDICT(OPA->edict);
OPC->_int = ((qcint*)ed) - prog->entitydata;
OPC->_int += OPB->_int;
break;
case INSTR_LOAD_F:
case INSTR_LOAD_FLD:
case INSTR_LOAD_ENT:
case INSTR_LOAD_S:
case INSTR_LOAD_FNC:
if (OPA->edict < 0 || OPA->edict >= prog->entities)
{
PRVM_ERROR ("%s Progs attempted to read an out of bounds edict number", PRVM_NAME);
goto cleanup;
}
if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
{
PRVM_ERROR("%s attempted to read an invalid field in an edict (%i)", PRVM_NAME, OPB->_int);
goto cleanup;
}
ed = PROG_GETEDICT(OPA->edict);
OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
break;
case INSTR_LOAD_V:
if (OPA->edict < 0 || OPA->edict >= prog->entities)
{
PRVM_ERROR ("%s Progs attempted to read an out of bounds edict number", PRVM_NAME);
goto cleanup;
}
if (OPB->_int < 0 || OPB->_int + 3 >= prog->entityfields)
{
PRVM_ERROR("%s attempted to read an invalid field in an edict (%i)", PRVM_NAME, OPB->_int);
goto cleanup;
}
ed = PROG_GETEDICT(OPA->edict);
OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
break;
/*==================*/
case INSTR_IFNOT:
if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
/* TODO add an "int-if", and change this one to OPA->_float
although mostly unneeded, thanks to the only float being false being 0x0 and 0x80000000 (negative zero)
and entity, string, field values can never have that value
*/
{
st += st->o2.s1 - 1; /* offset the s++ */
if (++jumpcount >= maxjumps)
{
PRVM_ERROR("%s runaway loop counter hit limit of %li jumps\ntip: read above for list of most-executed functions", PRVM_NAME, jumpcount);
}
}
break;
case INSTR_IF:
if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
{
st += st->o2.s1 - 1; /* offset the s++ */
/* no bounds check needed, it is done when loading progs */
if (++jumpcount >= maxjumps)
{
PRVM_ERROR("%s runaway loop counter hit limit of %li jumps\ntip: read above for list of most-executed functions", PRVM_NAME, jumpcount);
}
}
break;
case INSTR_GOTO:
st += st->o1.s1 - 1; /* offset the s++ */
/* no bounds check needed, it is done when loading progs */
if (++jumpcount == 10000000)
{
PRVM_ERROR("%s runaway loop counter hit limit of %li jumps\ntip: read above for list of most-executed functions", PRVM_NAME, jumpcount);
}
break;
case INSTR_CALL0:
case INSTR_CALL1:
case INSTR_CALL2:
case INSTR_CALL3:
case INSTR_CALL4:
case INSTR_CALL5:
case INSTR_CALL6:
case INSTR_CALL7:
case INSTR_CALL8:
prog->argc = st->opcode - INSTR_CALL0;
if (!OPA->function)
PRVM_ERROR("NULL function in %s", PRVM_NAME);
if(!OPA->function || OPA->function >= (unsigned int)prog->functions_count)
{
PRVM_ERROR("%s CALL outside the program", PRVM_NAME);
goto cleanup;
}
newf = &prog->functions[OPA->function];
newf->profile++;
prog->statement = (st - prog->code) + 1;
if (newf->entry < 0)
{
/* negative statements are built in functions */
int builtinnumber = -newf->entry;
if (builtinnumber < prog->builtins_count && prog->builtins[builtinnumber])
{
prog->builtins[builtinnumber](prog);
}
else
PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
}
else
st = prog->code + PROG_ENTERFUNCTION(newf);
break;
case INSTR_DONE:
case INSTR_RETURN:
/* add instruction count to function profile count */
GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
st = prog->code + PROG_LEAVEFUNCTION();
if (!prog->stack_count)
goto cleanup;
break;
case INSTR_STATE:
break;
/* LordHavoc: to be enabled when Progs version 7 (or whatever it will be numbered) is finalized */
/*
case INSTR_ADD_I:
OPC->_int = OPA->_int + OPB->_int;
break;
case INSTR_ADD_IF:
OPC->_int = OPA->_int + (int) OPB->_float;
break;
case INSTR_ADD_FI:
OPC->_float = OPA->_float + (float) OPB->_int;
break;
case INSTR_SUB_I:
OPC->_int = OPA->_int - OPB->_int;
break;
case INSTR_SUB_IF:
OPC->_int = OPA->_int - (int) OPB->_float;
break;
case INSTR_SUB_FI:
OPC->_float = OPA->_float - (float) OPB->_int;
break;
case INSTR_MUL_I:
OPC->_int = OPA->_int * OPB->_int;
break;
case INSTR_MUL_IF:
OPC->_int = OPA->_int * (int) OPB->_float;
break;
case INSTR_MUL_FI:
OPC->_float = OPA->_float * (float) OPB->_int;
break;
case INSTR_MUL_VI:
OPC->vector[0] = (float) OPB->_int * OPA->vector[0];
OPC->vector[1] = (float) OPB->_int * OPA->vector[1];
OPC->vector[2] = (float) OPB->_int * OPA->vector[2];
break;
case INSTR_DIV_VF:
{
float temp = 1.0f / OPB->_float;
OPC->vector[0] = temp * OPA->vector[0];
OPC->vector[1] = temp * OPA->vector[1];
OPC->vector[2] = temp * OPA->vector[2];
}
break;
case INSTR_DIV_I:
OPC->_int = OPA->_int / OPB->_int;
break;
case INSTR_DIV_IF:
OPC->_int = OPA->_int / (int) OPB->_float;
break;
case INSTR_DIV_FI:
OPC->_float = OPA->_float / (float) OPB->_int;
break;
case INSTR_CONV_IF:
OPC->_float = OPA->_int;
break;
case INSTR_CONV_FI:
OPC->_int = OPA->_float;
break;
case INSTR_BITAND_I:
OPC->_int = OPA->_int & OPB->_int;
break;
case INSTR_BITOR_I:
OPC->_int = OPA->_int | OPB->_int;
break;
case INSTR_BITAND_IF:
OPC->_int = OPA->_int & (int)OPB->_float;
break;
case INSTR_BITOR_IF:
OPC->_int = OPA->_int | (int)OPB->_float;
break;
case INSTR_BITAND_FI:
OPC->_float = (int)OPA->_float & OPB->_int;
break;
case INSTR_BITOR_FI:
OPC->_float = (int)OPA->_float | OPB->_int;
break;
case INSTR_GE_I:
OPC->_float = OPA->_int >= OPB->_int;
break;
case INSTR_LE_I:
OPC->_float = OPA->_int <= OPB->_int;
break;
case INSTR_GT_I:
OPC->_float = OPA->_int > OPB->_int;
break;
case INSTR_LT_I:
OPC->_float = OPA->_int < OPB->_int;
break;
case INSTR_AND_I:
OPC->_float = OPA->_int && OPB->_int;
break;
case INSTR_OR_I:
OPC->_float = OPA->_int || OPB->_int;
break;
case INSTR_GE_IF:
OPC->_float = (float)OPA->_int >= OPB->_float;
break;
case INSTR_LE_IF:
OPC->_float = (float)OPA->_int <= OPB->_float;
break;
case INSTR_GT_IF:
OPC->_float = (float)OPA->_int > OPB->_float;
break;
case INSTR_LT_IF:
OPC->_float = (float)OPA->_int < OPB->_float;
break;
case INSTR_AND_IF:
OPC->_float = (float)OPA->_int && OPB->_float;
break;
case INSTR_OR_IF:
OPC->_float = (float)OPA->_int || OPB->_float;
break;
case INSTR_GE_FI:
OPC->_float = OPA->_float >= (float)OPB->_int;
break;
case INSTR_LE_FI:
OPC->_float = OPA->_float <= (float)OPB->_int;
break;
case INSTR_GT_FI:
OPC->_float = OPA->_float > (float)OPB->_int;
break;
case INSTR_LT_FI:
OPC->_float = OPA->_float < (float)OPB->_int;
break;
case INSTR_AND_FI:
OPC->_float = OPA->_float && (float)OPB->_int;
break;
case INSTR_OR_FI:
OPC->_float = OPA->_float || (float)OPB->_int;
break;
case INSTR_NOT_I:
OPC->_float = !OPA->_int;
break;
case INSTR_EQ_I:
OPC->_float = OPA->_int == OPB->_int;
break;
case INSTR_EQ_IF:
OPC->_float = (float)OPA->_int == OPB->_float;
break;
case INSTR_EQ_FI:
OPC->_float = OPA->_float == (float)OPB->_int;
break;
case INSTR_NE_I:
OPC->_float = OPA->_int != OPB->_int;
break;
case INSTR_NE_IF:
OPC->_float = (float)OPA->_int != OPB->_float;
break;
case INSTR_NE_FI:
OPC->_float = OPA->_float != (float)OPB->_int;
break;
case INSTR_STORE_I:
OPB->_int = OPA->_int;
break;
case INSTR_STOREP_I:
#if PRBOUNDSCHECK
if (OPB->_int < 0 || OPB->_int + 4 > pr_edictareasize)
{
PRVM_ERROR ("%s Progs attempted to write to an out of bounds edict", PRVM_NAME);
goto cleanup;
}
#endif
ptr = (prvm_eval_t *)(prog->edictsfields + OPB->_int);
ptr->_int = OPA->_int;
break;
case INSTR_LOAD_I:
#if PRBOUNDSCHECK
if (OPA->edict < 0 || OPA->edict >= prog->max_edicts)
{
PRVM_ERROR ("%s Progs attempted to read an out of bounds edict number", PRVM_NAME);
goto cleanup;
}
if (OPB->_int < 0 || OPB->_int >= progs->entityfields)
{
PRVM_ERROR ("%s Progs attempted to read an invalid field in an edict", PRVM_NAME);
goto cleanup;
}
#endif
ed = PRVM_PROG_TO_EDICT(OPA->edict);
OPC->_int = ((prvm_eval_t *)((int *)ed->v + OPB->_int))->_int;
break;
}
*/
default:
PRVM_ERROR("Illegal instruction in %s\n", PRVM_NAME);
goto cleanup;
}
}
#undef QCVM_PROFILE
#undef QCVM_TRACE

162
gmqcc.h
View file

@ -294,6 +294,25 @@ extern uint16_t type_store_instr[TYPE_COUNT];
*/ */
extern uint16_t type_storep_instr[TYPE_COUNT]; extern uint16_t type_storep_instr[TYPE_COUNT];
typedef struct {
uint32_t offset; /* Offset in file of where data begins */
uint32_t length; /* Length of section (how many of) */
} prog_section;
typedef struct {
uint32_t version; /* Program version (6) */
uint16_t crc16; /* What is this? */
uint16_t skip; /* see propsal.txt */
prog_section statements; /* prog_section_statement */
prog_section defs; /* prog_section_def */
prog_section fields; /* prog_section_field */
prog_section functions; /* prog_section_function */
prog_section strings; /* What is this? */
prog_section globals; /* What is this? */
uint32_t entfield; /* Number of entity fields */
} prog_header;
/* /*
* Each paramater incerements by 3 since vector types hold * Each paramater incerements by 3 since vector types hold
* 3 components (x,y,z). * 3 components (x,y,z).
@ -694,6 +713,56 @@ bool GMQCC_WARN Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
return false; \ return false; \
} }
#define MEM_VEC_FUN_APPEND(Tself, Twhat, mem) \
bool GMQCC_WARN Tself##_##mem##_append(Tself *s, Twhat *p, size_t c) \
{ \
Twhat *reall; \
if (s->mem##_count+c >= s->mem##_alloc) { \
if (!s->mem##_alloc) { \
s->mem##_alloc = c < 16 ? 16 : c; \
} else { \
s->mem##_alloc *= 2; \
if (s->mem##_count+c >= s->mem##_alloc) { \
s->mem##_alloc = s->mem##_count+c; \
} \
} \
reall = (Twhat*)mem_a(sizeof(Twhat) * s->mem##_alloc); \
if (!reall) { \
return false; \
} \
memcpy(reall, s->mem, sizeof(Twhat) * s->mem##_count); \
mem_d(s->mem); \
s->mem = reall; \
} \
memcpy(&s->mem[s->mem##_count], p, c*sizeof(*p)); \
s->mem##_count += c; \
return true; \
}
#define MEM_VEC_FUN_RESIZE(Tself, Twhat, mem) \
bool GMQCC_WARN Tself##_##mem##_resize(Tself *s, size_t c) \
{ \
Twhat *reall; \
if (c > s->mem##_alloc) { \
reall = (Twhat*)mem_a(sizeof(Twhat) * c); \
if (!reall) { return false; } \
memcpy(reall, s->mem, sizeof(Twhat) * s->mem##_count); \
s->mem##_alloc = c; \
mem_d(s->mem); \
s->mem = reall; \
return true; \
} \
s->mem##_count = c; \
if (c < (s->mem##_alloc / 2)) { \
reall = (Twhat*)mem_a(sizeof(Twhat) * c); \
if (!reall) { return false; } \
memcpy(reall, s->mem, sizeof(Twhat) * c); \
mem_d(s->mem); \
s->mem = reall; \
} \
return true; \
}
#define MEM_VEC_FUN_CLEAR(Tself, mem) \ #define MEM_VEC_FUN_CLEAR(Tself, mem) \
void Tself##_##mem##_clear(Tself *self) \ void Tself##_##mem##_clear(Tself *self) \
{ \ { \
@ -766,4 +835,97 @@ typedef struct {
const char *file; const char *file;
size_t line; size_t line;
} lex_ctx; } lex_ctx;
/*===================================================================*/
/*============================= exec.c ==============================*/
/*===================================================================*/
/* darkplaces has (or will have) a 64 bit prog loader
* where the 32 bit qc program is autoconverted on load.
* Since we may want to support that as well, let's redefine
* float and int here.
*/
typedef float qcfloat;
typedef int32_t qcint;
typedef union {
qcint _int;
qcint string;
qcint function;
qcint edict;
qcfloat _float;
qcfloat vector[3];
qcint ivector[3];
} qcany;
typedef char qcfloat_size_is_correct [sizeof(qcfloat) == 4 ?1:-1];
typedef char qcint_size_is_correct [sizeof(qcint) == 4 ?1:-1];
enum {
VMERR_OK,
VMERR_TEMPSTRING_ALLOC,
VMERR_END
};
#define VM_JUMPS_DEFAULT 1000000
/* execute-flags */
#define VMXF_DEFAULT 0x0000 /* default flags - nothing */
#define VMXF_TRACE 0x0001 /* trace: print statements before executing */
#define VMXF_PROFILE 0x0002 /* profile: increment the profile counters */
struct qc_program_s;
typedef int (*prog_builtin)(struct qc_program_s *prog);
typedef struct {
qcint stmt;
size_t localsp;
prog_section_function *function;
} qc_exec_stack;
typedef struct qc_program_s {
char *filename;
MEM_VECTOR_MAKE(prog_section_statement, code);
MEM_VECTOR_MAKE(prog_section_def, defs);
MEM_VECTOR_MAKE(prog_section_def, fields);
MEM_VECTOR_MAKE(prog_section_function, functions);
MEM_VECTOR_MAKE(char, strings);
MEM_VECTOR_MAKE(qcint, globals);
MEM_VECTOR_MAKE(qcint, entitydata);
size_t tempstring_start;
size_t tempstring_at;
qcint vmerror;
MEM_VECTOR_MAKE(size_t, profile);
MEM_VECTOR_MAKE(prog_builtin, builtins);
/* size_t ip; */
qcint entities;
size_t entityfields;
bool allowworldwrites;
MEM_VECTOR_MAKE(qcint, localstack);
MEM_VECTOR_MAKE(qc_exec_stack, stack);
size_t statement;
int argc; /* current arg count for debugging */
} qc_program;
qc_program* prog_load(const char *filename);
void prog_delete(qc_program *prog);
bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps);
char* prog_getstring (qc_program *prog, qcint str);
prog_section_def* prog_entfield (qc_program *prog, qcint off);
prog_section_def* prog_getdef (qc_program *prog, qcint off);
qcany* prog_getedict (qc_program *prog, qcint e);
qcint prog_tempstring(qc_program *prog, const char *_str);
#endif #endif