]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - exec.c
Working vector-field test
[xonotic/gmqcc.git] / exec.c
diff --git a/exec.c b/exec.c
index bb4b59cb4d04e77b8d780bd3630df90d6c2c9e26..b957a83a8722401f76a7e8f723e13f36661844f6 100644 (file)
--- a/exec.c
+++ b/exec.c
-#include "gmqcc.h"
-
-#define QCVM_EXECUTOR
-
-#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;                                                 \
-}
-
-/* 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(int)     == 4 ?1:-1];
-
-typedef struct {
-    uint32_t offset;
-    uint32_t length;
-} prog_section;
-
-typedef struct {
-    uint32_t     version;
-    uint16_t     crc16;
-    uint16_t     skip;
-
-    prog_section statements;
-    prog_section defs;
-    prog_section fields;
-    prog_section functions;
-    prog_section strings;
-    prog_section globals;
-    uint32_t     entfield;
-} prog_header;
-
-typedef prog_section_both      prog_def;
-typedef prog_section_function  prog_function;
-typedef prog_section_statement prog_statement;
-
-enum {
-    VMERR_OK,
-    VMERR_TEMPSTRING_ALLOC,
-
-    VMERR_END
-};
-
-#define JUMPS_DEFAULT 1000000
-
-#define VMXF_DEFAULT 0x0000
-#define VMXF_TRACE   0x0001
-#define VMXF_PROFILE 0x0002
-
-struct qc_program_s;
-
-typedef int (*prog_builtin)(struct qc_program_s *prog);
-
-typedef struct {
-    qcint          stmt;
-    size_t         localsp;
-    prog_function *function;
-} qc_exec_stack;
-
-typedef struct qc_program_s {
-    char           *filename;
+#include <errno.h>
+#include <string.h>
+#include <stdarg.h>
 
-    MEM_VECTOR_MAKE(prog_statement, code);
-    MEM_VECTOR_MAKE(prog_def,       defs);
-    MEM_VECTOR_MAKE(prog_def,       fields);
-    MEM_VECTOR_MAKE(prog_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);
+#include "gmqcc.h"
 
-    /* size_t ip; */
-    qcint  entities;
-    size_t entityfields;
-    bool   allowworldwrites;
+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,   bool,                   entitypool)
+
+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_VECTOR_MAKE(qcint,         localstack);
-    MEM_VECTOR_MAKE(qc_exec_stack, stack);
-    size_t statement;
+MEM_VEC_FUNCTIONS(qc_program,   size_t, profile)
+MEM_VEC_FUN_RESIZE(qc_program,  size_t, profile)
 
-    int    argc; /* current arg count for debugging */
-} qc_program;
-MEM_VEC_FUNCTIONS(qc_program, prog_statement, code)
-MEM_VEC_FUNCTIONS(qc_program, prog_def,       defs)
-MEM_VEC_FUNCTIONS(qc_program, prog_def,       fields)
-MEM_VEC_FUNCTIONS(qc_program, prog_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,   prog_builtin, builtins)
 
-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)
+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));
+}
 
-MEM_VEC_FUNCTIONS(qc_program,   size_t, profile)
-_MEM_VEC_FUN_RESIZE(qc_program, size_t, profile)
+static void printvmerr(const char *fmt, ...)
+{
+    va_list ap;
+    va_start(ap, fmt);
+    vprintf(fmt, ap);
+    va_end(ap);
+    putchar('\n');
+}
 
 qc_program* prog_load(const char *filename)
 {
     qc_program *prog;
     prog_header header;
+    size_t      i;
     FILE *file;
 
     file = fopen(filename, "rb");
@@ -179,14 +56,13 @@ qc_program* prog_load(const char *filename)
         return NULL;
 
     if (fread(&header, sizeof(header), 1, file) != 1) {
-        perror("read");
+        loaderror("failed to read header from '%s'", filename);
         fclose(file);
         return NULL;
     }
 
     if (header.version != 6) {
-        printf("header says this is a version %i progs, we need version 6\n",
-               header.version);
+        loaderror("header says this is a version %i progs, we need version 6\n", header.version);
         fclose(file);
         return NULL;
     }
@@ -202,11 +78,14 @@ qc_program* prog_load(const char *filename)
     prog->entityfields = header.entfield;
 
     prog->filename = util_strdup(filename);
-    if (!prog->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) {                      \
-        perror("fseek");                                                         \
+        loaderror("seek failed");                                                \
         goto error;                                                              \
     }                                                                            \
     prog->progvar##_alloc = header.hdrvar.length;                                \
@@ -216,15 +95,15 @@ qc_program* prog_load(const char *filename)
         goto error;                                                              \
     if (fread(prog->progvar, sizeof(*prog->progvar), header.hdrvar.length, file) \
         != header.hdrvar.length) {                                               \
-        perror("read");                                                          \
+        loaderror("read failed");                                                \
         goto error;                                                              \
     }
 #define read_data1(x, y) read_data(x, x, y)
 
-    read_data (statements, code, prog_statement);
-    read_data1(defs,             prog_def);
-    read_data1(fields,           prog_def);
-    read_data1(functions,        prog_function);
+    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);
 
@@ -240,6 +119,19 @@ qc_program* prog_load(const char *filename)
     if (!qc_program_strings_resize(prog, prog->strings_count + 16*1024))
         goto error;
 
+    /* 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;
+        }
+    }
+    prog->entities = 1;
+
     return prog;
 
 error:
@@ -251,6 +143,7 @@ error:
     if (prog->strings)    mem_d(prog->strings);
     if (prog->globals)    mem_d(prog->globals);
     if (prog->entitydata) mem_d(prog->entitydata);
+    if (prog->entitypool) mem_d(prog->entitypool);
     mem_d(prog);
     return NULL;
 }
@@ -265,9 +158,15 @@ void prog_delete(qc_program *prog)
     MEM_VECTOR_CLEAR(prog, strings);
     MEM_VECTOR_CLEAR(prog, globals);
     MEM_VECTOR_CLEAR(prog, entitydata);
+    MEM_VECTOR_CLEAR(prog, entitypool);
     MEM_VECTOR_CLEAR(prog, localstack);
     MEM_VECTOR_CLEAR(prog, stack);
     MEM_VECTOR_CLEAR(prog, profile);
+
+    if (prog->builtins_alloc) {
+        MEM_VECTOR_CLEAR(prog, builtins);
+    }
+    /* otherwise the builtins were statically allocated */
     mem_d(prog);
 }
 
@@ -282,7 +181,7 @@ char* prog_getstring(qc_program *prog, qcint str)
     return prog->strings + str;
 }
 
-prog_def* prog_entfield(qc_program *prog, qcint off)
+prog_section_def* prog_entfield(qc_program *prog, qcint off)
 {
     size_t i;
     for (i = 0; i < prog->fields_count; ++i) {
@@ -292,7 +191,7 @@ prog_def* prog_entfield(qc_program *prog, qcint off)
     return NULL;
 }
 
-prog_def* prog_getdef(qc_program *prog, qcint off)
+prog_section_def* prog_getdef(qc_program *prog, qcint off)
 {
     size_t i;
     for (i = 0; i < prog->defs_count; ++i) {
@@ -304,7 +203,58 @@ prog_def* prog_getdef(qc_program *prog, qcint off)
 
 qcany* prog_getedict(qc_program *prog, qcint e)
 {
-    return (qcany*)(prog->entitydata + (prog->entityfields + e));
+    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;
+    }
+    prog->entities++;
+    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;
 }
 
 qcint prog_tempstring(qc_program *prog, const char *_str)
@@ -338,10 +288,36 @@ qcint prog_tempstring(qc_program *prog, const char *_str)
     return at;
 }
 
+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;
+}
+
 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
 {
     static char spaces[16+1] = "            ";
-    prog_def *def;
+    prog_section_def *def;
     qcany    *value;
     int       len;
 
@@ -356,7 +332,7 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
         vtype = def->type;
     }
     else
-        len = printf("[#%u] ", glob);
+        len = printf("[@%u] ", glob);
 
     switch (vtype) {
         case TYPE_VOID:
@@ -372,7 +348,8 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
                                          value->vector[2]);
             break;
         case TYPE_STRING:
-            len += printf("\"%s\",", prog_getstring(prog, value->string));
+            len += print_escaped_string(prog_getstring(prog, value->string));
+            /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
             break;
         case TYPE_FLOAT:
         default:
@@ -386,13 +363,13 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
     }
 }
 
-static void prog_print_statement(qc_program *prog, prog_statement *st)
+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);
+    printf(" <> %-12s", asm_instr[st->opcode].m);
     if (st->opcode >= INSTR_IF &&
         st->opcode <= INSTR_IFNOT)
     {
@@ -446,28 +423,52 @@ static void prog_print_statement(qc_program *prog, prog_statement *st)
     }
 }
 
-static qcint prog_enterfunction(qc_program *prog, prog_function *func)
+static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
 {
     qc_exec_stack st;
-    prog_function *cur = NULL;
-
-    if (prog->stack_count)
-        cur = prog->stack[prog->stack_count-1].function;
+    size_t p, parampos;
 
     /* back up locals */
     st.localsp  = prog->localstack_count;
     st.stmt     = prog->statement;
     st.function = func;
 
-    if (cur)
+#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 + cur->firstlocal;
-        if (!qc_program_localstack_append(prog, globals, cur->locals))
+        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");
@@ -479,26 +480,43 @@ static qcint prog_enterfunction(qc_program *prog, prog_function *func)
 
 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];
-    if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
-        printf("out of memory\n");
-        exit(1);
-    }
 
-    if (st.localsp != prog->localstack_count) {
-        if (!qc_program_localstack_resize(prog, st.localsp)) {
+#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_function *func, size_t flags, long maxjumps)
+bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
 {
     long jumpcount = 0;
-    prog_statement *st;
+    prog_section_statement *st;
+
+    prog->vmerror = 0;
 
     st = prog->code + prog_enterfunction(prog, func);
     --st;
@@ -509,28 +527,28 @@ bool prog_exec(qc_program *prog, prog_function *func, size_t flags, long maxjump
         {
 #define QCVM_PROFILE 0
 #define QCVM_TRACE   0
-#           include "qcvm_execprogram.h"
+#           include "execloop.h"
             break;
         }
         case (VMXF_TRACE):
         {
 #define QCVM_PROFILE 0
 #define QCVM_TRACE   1
-#           include "qcvm_execprogram.h"
+#           include "execloop.h"
             break;
         }
         case (VMXF_PROFILE):
         {
 #define QCVM_PROFILE 1
 #define QCVM_TRACE   0
-#           include "qcvm_execprogram.h"
+#           include "execloop.h"
             break;
         }
         case (VMXF_TRACE|VMXF_PROFILE):
         {
 #define QCVM_PROFILE 1
 #define QCVM_TRACE   1
-#           include "qcvm_execprogram.h"
+#           include "execloop.h"
             break;
         }
     };
@@ -538,6 +556,8 @@ bool prog_exec(qc_program *prog, prog_function *func, size_t flags, long maxjump
 cleanup:
     prog->localstack_count = 0;
     prog->stack_count = 0;
+    if (prog->vmerror)
+        return false;
     return true;
 }
 
@@ -546,6 +566,72 @@ cleanup:
  */
 
 #if defined(QCVM_EXECUTOR)
+bool        opts_debug    = false;
+bool        opts_memchk   = false;
+
+#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)
+
+static int qc_print(qc_program *prog)
+{
+    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));
+    }
+    return 0;
+}
+
+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;
+}
+
+static prog_builtin qc_builtins[] = {
+    NULL,
+    &qc_print,
+    &qc_ftos,
+    &qc_spawn,
+    &qc_kill
+};
+static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
+
 int main(int argc, char **argv)
 {
     size_t      i;
@@ -563,15 +649,20 @@ int main(int argc, char **argv)
         exit(1);
     }
 
+    prog->builtins       = qc_builtins;
+    prog->builtins_count = qc_builtins_count;
+    prog->builtins_alloc = 0;
+
     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;
     }
+    printf("Entity field space: %i\n", (int)prog->entityfields);
     if (fnmain > 0)
     {
-        prog_exec(prog, &prog->functions[fnmain], VMXF_TRACE, JUMPS_DEFAULT);
+        prog_exec(prog, &prog->functions[fnmain], VMXF_TRACE, VM_JUMPS_DEFAULT);
     }
     else
         printf("No main function found\n");