2012-06-27 13:34:26 +00:00
|
|
|
#include <errno.h>
|
2012-08-16 09:36:28 +00:00
|
|
|
#include <stdio.h>
|
2012-06-27 13:34:26 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
#include "gmqcc.h"
|
|
|
|
|
2012-06-27 20:31:56 +00:00
|
|
|
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)
|
2012-07-27 17:18:04 +00:00
|
|
|
MEM_VEC_FUN_APPEND(qc_program, char, strings)
|
|
|
|
MEM_VEC_FUN_RESIZE(qc_program, char, strings)
|
2012-06-27 20:31:56 +00:00
|
|
|
MEM_VEC_FUNCTIONS(qc_program, qcint, globals)
|
|
|
|
MEM_VEC_FUNCTIONS(qc_program, qcint, entitydata)
|
2012-08-11 15:31:08 +00:00
|
|
|
MEM_VEC_FUNCTIONS(qc_program, bool, entitypool)
|
2012-06-27 20:31:56 +00:00
|
|
|
|
|
|
|
MEM_VEC_FUNCTIONS(qc_program, qcint, localstack)
|
2012-07-27 17:18:04 +00:00
|
|
|
MEM_VEC_FUN_APPEND(qc_program, qcint, localstack)
|
|
|
|
MEM_VEC_FUN_RESIZE(qc_program, qcint, localstack)
|
2012-06-27 20:31:56 +00:00
|
|
|
MEM_VEC_FUNCTIONS(qc_program, qc_exec_stack, stack)
|
|
|
|
|
|
|
|
MEM_VEC_FUNCTIONS(qc_program, size_t, profile)
|
2012-07-27 17:18:04 +00:00
|
|
|
MEM_VEC_FUN_RESIZE(qc_program, size_t, profile)
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-07-27 17:27:56 +00:00
|
|
|
MEM_VEC_FUNCTIONS(qc_program, prog_builtin, builtins)
|
|
|
|
|
2012-06-27 13:34:26 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2012-08-16 10:10:24 +00:00
|
|
|
static void qcvmerror(qc_program *prog, const char *fmt, ...)
|
2012-08-11 15:31:08 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
2012-08-16 10:10:24 +00:00
|
|
|
|
|
|
|
prog->vmerror++;
|
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
qc_program* prog_load(const char *filename)
|
|
|
|
{
|
|
|
|
qc_program *prog;
|
|
|
|
prog_header header;
|
2012-08-11 15:31:08 +00:00
|
|
|
size_t i;
|
2012-06-25 21:58:47 +00:00
|
|
|
FILE *file;
|
|
|
|
|
2012-08-16 12:01:47 +00:00
|
|
|
file = util_fopen(filename, "rb");
|
2012-06-25 21:58:47 +00:00
|
|
|
if (!file)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (fread(&header, sizeof(header), 1, file) != 1) {
|
2012-06-27 13:34:26 +00:00
|
|
|
loaderror("failed to read header from '%s'", filename);
|
2012-06-25 21:58:47 +00:00
|
|
|
fclose(file);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header.version != 6) {
|
2012-06-27 13:34:26 +00:00
|
|
|
loaderror("header says this is a version %i progs, we need version 6\n", header.version);
|
2012-06-25 21:58:47 +00:00
|
|
|
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));
|
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
prog->entityfields = header.entfield;
|
2012-08-22 12:00:57 +00:00
|
|
|
prog->crc16 = header.crc16;
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
prog->filename = util_strdup(filename);
|
2012-06-27 13:34:26 +00:00
|
|
|
if (!prog->filename) {
|
|
|
|
loaderror("failed to store program name");
|
2012-06-25 21:58:47 +00:00
|
|
|
goto error;
|
2012-06-27 13:34:26 +00:00
|
|
|
}
|
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
#define read_data(hdrvar, progvar, type) \
|
|
|
|
if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
|
2012-06-27 13:34:26 +00:00
|
|
|
loaderror("seek failed"); \
|
2012-06-25 21:58:47 +00:00
|
|
|
goto error; \
|
|
|
|
} \
|
2012-06-26 11:10:10 +00:00
|
|
|
prog->progvar##_alloc = header.hdrvar.length; \
|
|
|
|
prog->progvar##_count = header.hdrvar.length; \
|
2012-06-25 21:58:47 +00:00
|
|
|
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) { \
|
2012-06-27 13:34:26 +00:00
|
|
|
loaderror("read failed"); \
|
2012-06-25 21:58:47 +00:00
|
|
|
goto error; \
|
|
|
|
}
|
|
|
|
#define read_data1(x, y) read_data(x, x, y)
|
|
|
|
|
2012-06-27 20:29:31 +00:00
|
|
|
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);
|
2012-06-25 21:58:47 +00:00
|
|
|
read_data1(strings, char);
|
|
|
|
read_data1(globals, qcint);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
/* profile counters */
|
|
|
|
if (!qc_program_profile_resize(prog, prog->code_count))
|
|
|
|
goto error;
|
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
/* 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;
|
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
/* spawn the world entity */
|
|
|
|
if (!qc_program_entitypool_add(prog, true)) {
|
|
|
|
loaderror("failed to allocate world entity\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
for (i = 0; i < prog->entityfields; ++i) {
|
|
|
|
if (!qc_program_entitydata_add(prog, 0)) {
|
|
|
|
loaderror("failed to allocate world data\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2012-08-11 15:35:55 +00:00
|
|
|
prog->entities = 1;
|
2012-08-11 15:31:08 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
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);
|
2012-08-11 15:31:08 +00:00
|
|
|
if (prog->entitypool) mem_d(prog->entitypool);
|
2012-06-25 21:58:47 +00:00
|
|
|
mem_d(prog);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void prog_delete(qc_program *prog)
|
|
|
|
{
|
2012-06-26 11:10:10 +00:00
|
|
|
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);
|
2012-08-11 15:31:08 +00:00
|
|
|
MEM_VECTOR_CLEAR(prog, entitypool);
|
2012-06-26 11:10:10 +00:00
|
|
|
MEM_VECTOR_CLEAR(prog, localstack);
|
2012-06-27 11:21:37 +00:00
|
|
|
MEM_VECTOR_CLEAR(prog, stack);
|
|
|
|
MEM_VECTOR_CLEAR(prog, profile);
|
2012-07-27 17:27:56 +00:00
|
|
|
|
|
|
|
if (prog->builtins_alloc) {
|
|
|
|
MEM_VECTOR_CLEAR(prog, builtins);
|
|
|
|
}
|
|
|
|
/* otherwise the builtins were statically allocated */
|
2012-06-25 21:58:47 +00:00
|
|
|
mem_d(prog);
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* VM code
|
|
|
|
*/
|
|
|
|
|
|
|
|
char* prog_getstring(qc_program *prog, qcint str)
|
|
|
|
{
|
|
|
|
if (str < 0 || str >= prog->strings_count)
|
2012-06-27 11:21:37 +00:00
|
|
|
return "<<<invalid string>>>";
|
2012-06-26 11:10:10 +00:00
|
|
|
return prog->strings + str;
|
|
|
|
}
|
|
|
|
|
2012-06-27 20:29:31 +00:00
|
|
|
prog_section_def* prog_entfield(qc_program *prog, qcint off)
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < prog->fields_count; ++i) {
|
|
|
|
if (prog->fields[i].offset == off)
|
|
|
|
return (prog->fields + i);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-27 20:29:31 +00:00
|
|
|
prog_section_def* prog_getdef(qc_program *prog, qcint off)
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2012-08-11 15:31:08 +00:00
|
|
|
if (e >= prog->entitypool_count) {
|
|
|
|
prog->vmerror++;
|
|
|
|
printf("Accessing out of bounds edict %i\n", (int)e);
|
|
|
|
e = 0;
|
|
|
|
}
|
|
|
|
return (qcany*)(prog->entitydata + (prog->entityfields * e));
|
|
|
|
}
|
|
|
|
|
|
|
|
qcint prog_spawn_entity(qc_program *prog)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
qcint e;
|
|
|
|
for (e = 0; e < (qcint)prog->entitypool_count; ++e) {
|
|
|
|
if (!prog->entitypool[e]) {
|
|
|
|
char *data = (char*)(prog->entitydata + (prog->entityfields * e));
|
|
|
|
memset(data, 0, prog->entityfields * sizeof(qcint));
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!qc_program_entitypool_add(prog, true)) {
|
|
|
|
prog->vmerror++;
|
|
|
|
printf("Failed to allocate entity\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-11 15:35:55 +00:00
|
|
|
prog->entities++;
|
2012-08-11 15:31:08 +00:00
|
|
|
for (i = 0; i < prog->entityfields; ++i) {
|
|
|
|
if (!qc_program_entitydata_add(prog, 0)) {
|
|
|
|
printf("Failed to allocate entity\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void prog_free_entity(qc_program *prog, qcint e)
|
|
|
|
{
|
|
|
|
if (!e) {
|
|
|
|
prog->vmerror++;
|
|
|
|
printf("Trying to free world entity\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e >= prog->entitypool_count) {
|
|
|
|
prog->vmerror++;
|
|
|
|
printf("Trying to free out of bounds entity\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!prog->entitypool[e]) {
|
|
|
|
prog->vmerror++;
|
|
|
|
printf("Double free on entity\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prog->entitypool[e] = false;
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-11 09:31:50 +00:00
|
|
|
static int print_escaped_string(const char *str)
|
|
|
|
{
|
|
|
|
int len = 2;
|
|
|
|
putchar('"');
|
|
|
|
while (*str) {
|
|
|
|
switch (*str) {
|
|
|
|
case '\a': len += 2; putchar('\\'); putchar('a'); break;
|
|
|
|
case '\b': len += 2; putchar('\\'); putchar('b'); break;
|
|
|
|
case '\r': len += 2; putchar('\\'); putchar('r'); break;
|
|
|
|
case '\n': len += 2; putchar('\\'); putchar('n'); break;
|
|
|
|
case '\t': len += 2; putchar('\\'); putchar('t'); break;
|
|
|
|
case '\f': len += 2; putchar('\\'); putchar('f'); break;
|
|
|
|
case '\v': len += 2; putchar('\\'); putchar('v'); break;
|
|
|
|
case '\\': len += 2; putchar('\\'); putchar('\\'); break;
|
|
|
|
case '"': len += 2; putchar('\\'); putchar('"'); break;
|
|
|
|
default:
|
|
|
|
++len;
|
|
|
|
putchar(*str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
putchar('"');
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
|
|
|
|
{
|
2012-08-19 14:42:12 +00:00
|
|
|
static char spaces[40+1] = " ";
|
2012-06-27 20:29:31 +00:00
|
|
|
prog_section_def *def;
|
2012-06-27 11:21:37 +00:00
|
|
|
qcany *value;
|
|
|
|
int len;
|
|
|
|
|
2012-08-19 14:42:12 +00:00
|
|
|
if (!glob) {
|
|
|
|
len = printf("<null>,");
|
|
|
|
goto done;
|
|
|
|
}
|
2012-06-27 11:21:37 +00:00
|
|
|
|
|
|
|
def = prog_getdef(prog, glob);
|
|
|
|
value = (qcany*)(&prog->globals[glob]);
|
|
|
|
|
|
|
|
if (def) {
|
|
|
|
len = printf("[%s] ", prog_getstring(prog, def->name));
|
|
|
|
vtype = def->type;
|
|
|
|
}
|
|
|
|
else
|
2012-08-11 09:46:44 +00:00
|
|
|
len = printf("[@%u] ", glob);
|
2012-06-27 11:21:37 +00:00
|
|
|
|
|
|
|
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:
|
2012-08-11 09:31:50 +00:00
|
|
|
len += print_escaped_string(prog_getstring(prog, value->string));
|
2012-08-19 14:42:12 +00:00
|
|
|
len += printf(",");
|
2012-08-11 09:31:50 +00:00
|
|
|
/* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
|
2012-06-27 11:21:37 +00:00
|
|
|
break;
|
|
|
|
case TYPE_FLOAT:
|
|
|
|
default:
|
|
|
|
len += printf("%g,", value->_float);
|
|
|
|
break;
|
|
|
|
}
|
2012-08-19 14:42:12 +00:00
|
|
|
done:
|
|
|
|
if (len < sizeof(spaces)-1) {
|
|
|
|
spaces[sizeof(spaces)-1-len] = 0;
|
2012-06-27 11:21:37 +00:00
|
|
|
printf(spaces);
|
2012-08-19 14:42:12 +00:00
|
|
|
spaces[sizeof(spaces)-1-len] = ' ';
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-27 20:29:31 +00:00
|
|
|
static void prog_print_statement(qc_program *prog, prog_section_statement *st)
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
|
|
|
if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
|
|
|
|
printf("<illegal instruction %d>\n", st->opcode);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-11 09:31:50 +00:00
|
|
|
printf(" <> %-12s", asm_instr[st->opcode].m);
|
2012-06-27 11:21:37 +00:00
|
|
|
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;
|
2012-08-19 14:42:12 +00:00
|
|
|
case INSTR_STORE_F:
|
|
|
|
case INSTR_STOREP_F:
|
|
|
|
t[2] = -1;
|
|
|
|
break;
|
2012-06-27 11:21:37 +00:00
|
|
|
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;
|
2012-08-19 14:42:12 +00:00
|
|
|
case INSTR_STORE_ENT:
|
|
|
|
t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
|
|
|
|
break;
|
|
|
|
case INSTR_STORE_FLD:
|
|
|
|
t[0] = t[1] = TYPE_FIELD; t[2] = -1;
|
|
|
|
break;
|
|
|
|
case INSTR_STORE_FNC:
|
|
|
|
t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
|
|
|
|
break;
|
|
|
|
case INSTR_STOREP_V:
|
|
|
|
t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
|
|
|
|
break;
|
|
|
|
case INSTR_STOREP_S:
|
|
|
|
t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
|
|
|
|
break;
|
|
|
|
case INSTR_STOREP_ENT:
|
|
|
|
t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
|
|
|
|
break;
|
|
|
|
case INSTR_STOREP_FLD:
|
|
|
|
t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
|
|
|
|
break;
|
|
|
|
case INSTR_STOREP_FNC:
|
|
|
|
t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
|
|
|
|
break;
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
|
2012-08-19 14:42:12 +00:00
|
|
|
else printf("(none), ");
|
2012-06-27 11:21:37 +00:00
|
|
|
if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
|
2012-08-19 14:42:12 +00:00
|
|
|
else printf("(none), ");
|
2012-06-27 11:21:37 +00:00
|
|
|
if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
|
2012-08-19 14:42:12 +00:00
|
|
|
else printf("(none)");
|
2012-06-27 11:21:37 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
2012-08-19 14:42:12 +00:00
|
|
|
fflush(stdout);
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 20:29:31 +00:00
|
|
|
static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
|
|
|
qc_exec_stack st;
|
2012-06-29 14:04:24 +00:00
|
|
|
size_t p, parampos;
|
2012-06-27 11:21:37 +00:00
|
|
|
|
|
|
|
/* back up locals */
|
|
|
|
st.localsp = prog->localstack_count;
|
|
|
|
st.stmt = prog->statement;
|
|
|
|
st.function = func;
|
|
|
|
|
2012-06-29 14:04:24 +00:00
|
|
|
#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
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
2012-06-29 14:04:24 +00:00
|
|
|
qcint *globals = prog->globals + func->firstlocal;
|
|
|
|
if (!qc_program_localstack_append(prog, globals, func->locals))
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
|
|
|
printf("out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2012-06-29 14:04:24 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
2012-06-27 11:21:37 +00:00
|
|
|
|
|
|
|
if (!qc_program_stack_add(prog, st)) {
|
|
|
|
printf("out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return func->entry;
|
|
|
|
}
|
|
|
|
|
2012-06-27 11:23:27 +00:00
|
|
|
static qcint prog_leavefunction(qc_program *prog)
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
2012-06-29 14:04:24 +00:00
|
|
|
prog_section_function *prev = NULL;
|
|
|
|
size_t oldsp;
|
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
qc_exec_stack st = prog->stack[prog->stack_count-1];
|
|
|
|
|
2012-06-29 14:04:24 +00:00
|
|
|
#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)) {
|
2012-06-27 11:21:37 +00:00
|
|
|
printf("out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 14:04:24 +00:00
|
|
|
if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
|
|
|
|
printf("out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-08-16 14:28:59 +00:00
|
|
|
return st.stmt - 1; /* offset the ++st */
|
2012-06-27 11:21:37 +00:00
|
|
|
}
|
|
|
|
|
2012-06-27 20:29:31 +00:00
|
|
|
bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
|
2012-06-27 11:21:37 +00:00
|
|
|
{
|
|
|
|
long jumpcount = 0;
|
2012-06-27 20:29:31 +00:00
|
|
|
prog_section_statement *st;
|
2012-06-27 11:21:37 +00:00
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
prog->vmerror = 0;
|
|
|
|
|
2012-06-27 11:21:37 +00:00
|
|
|
st = prog->code + prog_enterfunction(prog, func);
|
|
|
|
--st;
|
|
|
|
switch (flags)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
#define QCVM_PROFILE 0
|
|
|
|
#define QCVM_TRACE 0
|
2012-06-27 17:37:00 +00:00
|
|
|
# include "execloop.h"
|
2012-06-27 11:21:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (VMXF_TRACE):
|
|
|
|
{
|
|
|
|
#define QCVM_PROFILE 0
|
|
|
|
#define QCVM_TRACE 1
|
2012-06-27 17:37:00 +00:00
|
|
|
# include "execloop.h"
|
2012-06-27 11:21:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (VMXF_PROFILE):
|
|
|
|
{
|
|
|
|
#define QCVM_PROFILE 1
|
|
|
|
#define QCVM_TRACE 0
|
2012-06-27 17:37:00 +00:00
|
|
|
# include "execloop.h"
|
2012-06-27 11:21:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case (VMXF_TRACE|VMXF_PROFILE):
|
|
|
|
{
|
|
|
|
#define QCVM_PROFILE 1
|
|
|
|
#define QCVM_TRACE 1
|
2012-06-27 17:37:00 +00:00
|
|
|
# include "execloop.h"
|
2012-06-27 11:21:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
prog->localstack_count = 0;
|
|
|
|
prog->stack_count = 0;
|
2012-08-11 15:31:08 +00:00
|
|
|
if (prog->vmerror)
|
|
|
|
return false;
|
2012-06-27 11:21:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-26 11:10:10 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* main for when building the standalone executor
|
|
|
|
*/
|
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
#if defined(QCVM_EXECUTOR)
|
2012-08-22 12:00:57 +00:00
|
|
|
const char *type_name[TYPE_COUNT] = {
|
|
|
|
"void",
|
|
|
|
"string",
|
|
|
|
"float",
|
|
|
|
"vector",
|
|
|
|
"entity",
|
|
|
|
"field",
|
|
|
|
"function",
|
|
|
|
"pointer",
|
|
|
|
#if 0
|
|
|
|
"integer",
|
|
|
|
#endif
|
|
|
|
"variant"
|
|
|
|
};
|
|
|
|
|
2012-08-08 10:07:37 +00:00
|
|
|
bool opts_debug = false;
|
|
|
|
bool opts_memchk = false;
|
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
#define CheckArgs(num) do { \
|
|
|
|
if (prog->argc != (num)) { \
|
|
|
|
prog->vmerror++; \
|
|
|
|
printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
|
|
|
|
__FUNCTION__, prog->argc, (num)); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
|
|
|
|
#define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
|
|
|
|
#define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
|
|
|
|
|
2012-07-27 17:27:56 +00:00
|
|
|
static int qc_print(qc_program *prog)
|
|
|
|
{
|
2012-08-11 19:18:15 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < prog->argc; ++i) {
|
|
|
|
qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
|
|
|
|
printf("%s", prog_getstring(prog, str->string));
|
|
|
|
}
|
2012-07-27 17:27:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-11 15:31:08 +00:00
|
|
|
static int qc_ftos(qc_program *prog)
|
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
qcany *num;
|
|
|
|
qcany str;
|
|
|
|
CheckArgs(1);
|
|
|
|
num = GetArg(0);
|
|
|
|
snprintf(buffer, sizeof(buffer), "%g", num->_float);
|
|
|
|
str.string = prog_tempstring(prog, buffer);
|
|
|
|
Return(str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qc_spawn(qc_program *prog)
|
|
|
|
{
|
|
|
|
qcany ent;
|
|
|
|
CheckArgs(0);
|
|
|
|
ent.edict = prog_spawn_entity(prog);
|
|
|
|
Return(ent);
|
|
|
|
return (ent.edict ? 0 : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int qc_kill(qc_program *prog)
|
|
|
|
{
|
|
|
|
qcany *ent;
|
|
|
|
CheckArgs(1);
|
|
|
|
ent = GetArg(0);
|
|
|
|
prog_free_entity(prog, ent->edict);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-27 17:27:56 +00:00
|
|
|
static prog_builtin qc_builtins[] = {
|
|
|
|
NULL,
|
2012-08-11 15:31:08 +00:00
|
|
|
&qc_print,
|
|
|
|
&qc_ftos,
|
|
|
|
&qc_spawn,
|
|
|
|
&qc_kill
|
2012-07-27 17:27:56 +00:00
|
|
|
};
|
|
|
|
static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
|
|
|
|
|
2012-08-19 13:19:43 +00:00
|
|
|
static const char *arg0 = NULL;
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
printf("usage: [-debug] %s file\n", arg0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2012-06-26 11:10:10 +00:00
|
|
|
size_t i;
|
2012-06-26 11:22:37 +00:00
|
|
|
qcint fnmain = -1;
|
2012-06-25 21:58:47 +00:00
|
|
|
qc_program *prog;
|
2012-08-19 13:19:43 +00:00
|
|
|
size_t xflags = VMXF_DEFAULT;
|
2012-08-22 12:00:57 +00:00
|
|
|
bool opts_printfields = false;
|
2012-08-22 12:15:35 +00:00
|
|
|
bool opts_printdefs = false;
|
2012-08-22 12:00:57 +00:00
|
|
|
bool opts_info = false;
|
2012-06-25 21:58:47 +00:00
|
|
|
|
2012-08-19 13:19:43 +00:00
|
|
|
arg0 = argv[0];
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
while (argc > 2) {
|
|
|
|
if (!strcmp(argv[1], "-trace")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
xflags |= VMXF_TRACE;
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[1], "-profile")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
xflags |= VMXF_PROFILE;
|
|
|
|
}
|
2012-08-22 12:00:57 +00:00
|
|
|
else if (!strcmp(argv[1], "-info")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_info = true;
|
|
|
|
}
|
2012-08-22 12:15:35 +00:00
|
|
|
else if (!strcmp(argv[1], "-printdefs")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_printdefs = true;
|
|
|
|
}
|
2012-08-22 12:00:57 +00:00
|
|
|
else if (!strcmp(argv[1], "-printfields")) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
opts_printfields = true;
|
|
|
|
}
|
2012-08-19 13:19:43 +00:00
|
|
|
else
|
|
|
|
usage();
|
2012-06-25 21:58:47 +00:00
|
|
|
}
|
|
|
|
|
2012-08-19 13:19:43 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
prog = prog_load(argv[1]);
|
|
|
|
if (!prog) {
|
|
|
|
printf("failed to load program '%s'\n", argv[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-07-27 17:27:56 +00:00
|
|
|
prog->builtins = qc_builtins;
|
|
|
|
prog->builtins_count = qc_builtins_count;
|
|
|
|
prog->builtins_alloc = 0;
|
|
|
|
|
2012-08-22 12:00:57 +00:00
|
|
|
printf("Program's system-checksum = 0x%04x\n", (int)prog->crc16);
|
|
|
|
|
2012-06-26 11:21:09 +00:00
|
|
|
for (i = 1; i < prog->functions_count; ++i) {
|
2012-06-26 11:22:37 +00:00
|
|
|
const char *name = prog_getstring(prog, prog->functions[i].name);
|
2012-08-19 13:19:43 +00:00
|
|
|
/* printf("Found function: %s\n", name); */
|
2012-06-26 11:22:37 +00:00
|
|
|
if (!strcmp(name, "main"))
|
|
|
|
fnmain = (qcint)i;
|
2012-06-26 11:10:10 +00:00
|
|
|
}
|
2012-08-11 19:18:15 +00:00
|
|
|
printf("Entity field space: %i\n", (int)prog->entityfields);
|
2012-08-22 12:00:57 +00:00
|
|
|
if (opts_info) {
|
|
|
|
prog_delete(prog);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-08-22 12:15:35 +00:00
|
|
|
if (opts_printdefs) {
|
|
|
|
for (i = 0; i < prog->defs_count; ++i) {
|
|
|
|
printf("Global: %8s %-16s at %u\n",
|
|
|
|
type_name[prog->defs[i].type],
|
|
|
|
prog_getstring(prog, prog->defs[i].name),
|
|
|
|
(unsigned int)prog->defs[i].offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (opts_printfields) {
|
2012-08-22 12:00:57 +00:00
|
|
|
for (i = 0; i < prog->fields_count; ++i) {
|
|
|
|
printf("Field: %8s %-16s at %u\n",
|
|
|
|
type_name[prog->fields[i].type],
|
|
|
|
prog_getstring(prog, prog->fields[i].name),
|
|
|
|
(unsigned int)prog->fields[i].offset);
|
|
|
|
}
|
2012-06-26 11:22:37 +00:00
|
|
|
}
|
|
|
|
else
|
2012-08-22 12:00:57 +00:00
|
|
|
{
|
|
|
|
if (fnmain > 0)
|
|
|
|
{
|
|
|
|
prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("No main function found\n");
|
|
|
|
}
|
2012-06-26 11:10:10 +00:00
|
|
|
|
2012-06-25 21:58:47 +00:00
|
|
|
prog_delete(prog);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|