]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - exec.c
gitignore: add gmqcc, gmqpak, qmcvm, testsuite, pak.
[xonotic/gmqcc.git] / exec.c
diff --git a/exec.c b/exec.c
index c90444ad180831e65a6debc635f920d00b8aefba..0ef32119a0a475a3c9446b314c423ea2f2152622 100644 (file)
--- a/exec.c
+++ b/exec.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012
+ * Copyright (C) 2012, 2013
  *     Wolfgang Bumiller
  *     Dale Weiler
  *
 
 #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,   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_VEC_FUNCTIONS(qc_program,   size_t, profile)
-MEM_VEC_FUN_RESIZE(qc_program,  size_t, profile)
-
-MEM_VEC_FUNCTIONS(qc_program,   prog_builtin, builtins)
-
 static void loaderror(const char *fmt, ...)
 {
     int     err = errno;
@@ -57,7 +36,7 @@ static void loaderror(const char *fmt, ...)
     va_start(ap, fmt);
     vprintf(fmt, ap);
     va_end(ap);
-    printf(": %s\n", strerror(err));
+    printf(": %s\n", util_strerror(err));
 }
 
 static void qcvmerror(qc_program *prog, const char *fmt, ...)
@@ -72,33 +51,31 @@ static void qcvmerror(qc_program *prog, const char *fmt, ...)
     putchar('\n');
 }
 
-qc_program* prog_load(const char *filename)
+qc_program* prog_load(const char *filename, bool skipversion)
 {
-    qc_program *prog;
-    prog_header header;
-    size_t      i;
-    FILE *file;
+    qc_program   *prog;
+    prog_header   header;
+    FILE         *file   = fs_file_open(filename, "rb");
 
-    file = util_fopen(filename, "rb");
     if (!file)
         return NULL;
 
-    if (fread(&header, sizeof(header), 1, file) != 1) {
+    if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
         loaderror("failed to read header from '%s'", filename);
-        fclose(file);
+        fs_file_close(file);
         return NULL;
     }
 
-    if (header.version != 6) {
+    if (!skipversion && header.version != 6) {
         loaderror("header says this is a version %i progs, we need version 6\n", header.version);
-        fclose(file);
+        fs_file_close(file);
         return NULL;
     }
 
     prog = (qc_program*)mem_a(sizeof(qc_program));
     if (!prog) {
-        fclose(file);
-        printf("failed to allocate program data\n");
+        fs_file_close(file);
+        fprintf(stderr, "failed to allocate program data\n");
         return NULL;
     }
     memset(prog, 0, sizeof(*prog));
@@ -112,67 +89,59 @@ qc_program* prog_load(const char *filename)
         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_data(hdrvar, progvar, reserved)                           \
+    if (fs_file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) {        \
+        loaderror("seek failed");                                      \
+        goto error;                                                    \
+    }                                                                  \
+    if (fs_file_read (                                                    \
+            vec_add(prog->progvar, header.hdrvar.length + reserved),   \
+            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)
+#define read_data1(x)    read_data(x, x, 0)
+#define read_data2(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);
+    read_data (statements, code, 0);
+    read_data1(defs);
+    read_data1(fields);
+    read_data1(functions);
+    read_data1(strings);
+    read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
 
-    fclose(file);
+    fs_file_close(file);
 
     /* profile counters */
-    if (!qc_program_profile_resize(prog, prog->code_count))
-        goto error;
+    memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
 
     /* 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;
+    prog->tempstring_start = vec_size(prog->strings);
+    prog->tempstring_at    = vec_size(prog->strings);
+    memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
 
     /* 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;
-        }
-    }
+    vec_push(prog->entitypool, true);
+    memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
     prog->entities = 1;
 
     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);
-    if (prog->entitypool) mem_d(prog->entitypool);
+    if (prog->filename)
+        mem_d(prog->filename);
+    vec_free(prog->code);
+    vec_free(prog->defs);
+    vec_free(prog->fields);
+    vec_free(prog->functions);
+    vec_free(prog->strings);
+    vec_free(prog->globals);
+    vec_free(prog->entitydata);
+    vec_free(prog->entitypool);
     mem_d(prog);
     return NULL;
 }
@@ -180,22 +149,17 @@ error:
 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, 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 */
+    vec_free(prog->code);
+    vec_free(prog->defs);
+    vec_free(prog->fields);
+    vec_free(prog->functions);
+    vec_free(prog->strings);
+    vec_free(prog->globals);
+    vec_free(prog->entitydata);
+    vec_free(prog->entitypool);
+    vec_free(prog->localstack);
+    vec_free(prog->stack);
+    vec_free(prog->profile);
     mem_d(prog);
 }
 
@@ -205,15 +169,16 @@ void prog_delete(qc_program *prog)
 
 char* prog_getstring(qc_program *prog, qcint str)
 {
-    if (str < 0 || str >= prog->strings_count)
-        return "<<<invalid string>>>";
+    /* cast for return required for C++ */
+    if (str < 0 || str >= (qcint)vec_size(prog->strings))
+        return (char*)"<<<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) {
+    for (i = 0; i < vec_size(prog->fields); ++i) {
         if (prog->fields[i].offset == off)
             return (prog->fields + i);
     }
@@ -223,7 +188,7 @@ prog_section_def* prog_entfield(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) {
+    for (i = 0; i < vec_size(prog->defs); ++i) {
         if (prog->defs[i].offset == off)
             return (prog->defs + i);
     }
@@ -232,9 +197,9 @@ prog_section_def* prog_getdef(qc_program *prog, qcint off)
 
 qcany* prog_getedict(qc_program *prog, qcint e)
 {
-    if (e >= prog->entitypool_count) {
+    if (e >= (qcint)vec_size(prog->entitypool)) {
         prog->vmerror++;
-        printf("Accessing out of bounds edict %i\n", (int)e);
+        fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
         e = 0;
     }
     return (qcany*)(prog->entitydata + (prog->entityfields * e));
@@ -243,28 +208,17 @@ qcany* prog_getedict(qc_program *prog, qcint e)
 qcint prog_spawn_entity(qc_program *prog)
 {
     char  *data;
-    size_t i;
     qcint  e;
-    for (e = 0; e < (qcint)prog->entitypool_count; ++e) {
+    for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) {
         if (!prog->entitypool[e]) {
             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;
-    }
+    vec_push(prog->entitypool, true);
     prog->entities++;
-    for (i = 0; i < prog->entityfields; ++i) {
-        if (!qc_program_entitydata_add(prog, 0)) {
-            printf("Failed to allocate entity\n");
-            return 0;
-        }
-    }
-    data = (char*)(prog->entitydata + (prog->entityfields * e));
+    data = (char*)vec_add(prog->entitydata, prog->entityfields);
     memset(data, 0, prog->entityfields * sizeof(qcint));
     return e;
 }
@@ -273,44 +227,36 @@ void prog_free_entity(qc_program *prog, qcint e)
 {
     if (!e) {
         prog->vmerror++;
-        printf("Trying to free world entity\n");
+        fprintf(stderr, "Trying to free world entity\n");
         return;
     }
-    if (e >= prog->entitypool_count) {
+    if (e >= (qcint)vec_size(prog->entitypool)) {
         prog->vmerror++;
-        printf("Trying to free out of bounds entity\n");
+        fprintf(stderr, "Trying to free out of bounds entity\n");
         return;
     }
     if (!prog->entitypool[e]) {
         prog->vmerror++;
-        printf("Double free on entity\n");
+        fprintf(stderr, "Double free on entity\n");
         return;
     }
     prog->entitypool[e] = false;
 }
 
-qcint prog_tempstring(qc_program *prog, const char *_str)
+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)
+    if (at + len >= vec_size(prog->strings))
         at = prog->tempstring_start;
 
     /* when it doesn't fit, reallocate */
-    if (at + len >= prog->strings_count)
+    if (at + len >= vec_size(prog->strings))
     {
-        prog->strings_count = at;
-        if (!qc_program_strings_append(prog, str, len+1)) {
-            prog->vmerror = VMERR_TEMPSTRING_ALLOC;
-            return 0;
-        }
+        (void)vec_add(prog->strings, len+1);
+        memcpy(prog->strings + at, str, len+1);
         return at;
     }
 
@@ -320,9 +266,9 @@ qcint prog_tempstring(qc_program *prog, const char *_str)
     return at;
 }
 
-static int print_escaped_string(const char *str, size_t maxlen)
+static size_t print_escaped_string(const char *str, size_t maxlen)
 {
-    int len = 2;
+    size_t len = 2;
     putchar('"');
     --maxlen; /* because we're lazy and have escape sequences */
     while (*str) {
@@ -369,16 +315,15 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
     def = prog_getdef(prog, glob);
     value = (qcany*)(&prog->globals[glob]);
 
+    len = printf("[@%u] ", glob);
     if (def) {
         const char *name = prog_getstring(prog, def->name);
         if (name[0] == '#')
-            len = printf("$");
+            len += printf("$");
         else
-            len = printf("%s ", name);
+            len += printf("%s ", name);
         vtype = def->type & DEF_TYPEMASK;
     }
-    else
-        len = printf("[@%u] ", glob);
 
     switch (vtype) {
         case TYPE_VOID:
@@ -394,7 +339,10 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
                                          value->vector[2]);
             break;
         case TYPE_STRING:
-            len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
+            if (value->string)
+                len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
+            else
+                len += printf("(null)");
             len += printf(",");
             /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
             break;
@@ -404,9 +352,9 @@ static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
             break;
     }
 done:
-    if (len < sizeof(spaces)-1) {
+    if (len < (int)sizeof(spaces)-1) {
         spaces[sizeof(spaces)-1-len] = 0;
-        printf(spaces);
+        fs_file_puts(stdout, spaces);
         spaces[sizeof(spaces)-1-len] = ' ';
     }
 }
@@ -417,6 +365,12 @@ static void prog_print_statement(qc_program *prog, prog_section_statement *st)
         printf("<illegal instruction %d>\n", st->opcode);
         return;
     }
+    if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
+        size_t i;
+        for (i = 0; i < vec_size(prog->function_stack); ++i)
+            printf("->");
+        printf("%s:", vec_last(prog->function_stack));
+    }
     printf(" <> %-12s", asm_instr[st->opcode].m);
     if (st->opcode >= INSTR_IF &&
         st->opcode <= INSTR_IFNOT)
@@ -501,42 +455,39 @@ static void prog_print_statement(qc_program *prog, prog_section_statement *st)
         else           printf("(none)");
         printf("\n");
     }
-    fflush(stdout);
 }
 
 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
 {
     qc_exec_stack st;
-    size_t p, parampos;
+    size_t  parampos;
+    int32_t p;
 
     /* back up locals */
-    st.localsp  = prog->localstack_count;
+    st.localsp  = vec_size(prog->localstack);
     st.stmt     = prog->statement;
     st.function = func;
 
+    if (prog->xflags & VMXF_TRACE) {
+        const char *str = prog_getstring(prog, func->name);
+        vec_push(prog->function_stack, str);
+    }
+
 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
-    if (prog->stack_count)
+    if (vec_size(prog->stack))
     {
         prog_section_function *cur;
-        cur = prog->stack[prog->stack_count-1].function;
+        cur = prog->stack[vec_size(prog->stack)-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);
-            }
+            vec_append(prog->localstack, cur->locals, globals);
         }
     }
 #else
     {
         qcint *globals = prog->globals + func->firstlocal;
-        if (!qc_program_localstack_append(prog, globals, func->locals))
-        {
-            printf("out of memory\n");
-            exit(1);
-        }
+        vec_append(prog->localstack, func->locals, globals);
     }
 #endif
 
@@ -551,10 +502,7 @@ static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
         }
     }
 
-    if (!qc_program_stack_add(prog, st)) {
-        printf("out of memory\n");
-        exit(1);
-    }
+    vec_push(prog->stack, st);
 
     return func->entry;
 }
@@ -564,30 +512,30 @@ 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];
+    qc_exec_stack st = vec_last(prog->stack);
+
+    if (prog->xflags & VMXF_TRACE) {
+        if (vec_size(prog->function_stack))
+            vec_pop(prog->function_stack);
+    }
 
 #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;
+    if (vec_size(prog->stack) > 1) {
+        prev  = prog->stack[vec_size(prog->stack)-2].function;
+        oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
     }
 #else
-    prev  = prog->stack[prog->stack_count-1].function;
-    oldsp = prog->stack[prog->stack_count-1].localsp;
+    prev  = prog->stack[vec_size(prog->stack)-1].function;
+    oldsp = prog->stack[vec_size(prog->stack)-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);
-        }
+        memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
+        /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
+        vec_shrinkto(prog->localstack, oldsp);
     }
 
-    if (!qc_program_stack_remove(prog, prog->stack_count-1)) {
-        printf("out of memory\n");
-        exit(1);
-    }
+    vec_pop(prog->stack);
 
     return st.stmt - 1; /* offset the ++st */
 }
@@ -612,35 +560,31 @@ bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long
 #define QCVM_PROFILE 0
 #define QCVM_TRACE   0
 #           include __FILE__
-            break;
         }
         case (VMXF_TRACE):
         {
 #define QCVM_PROFILE 0
 #define QCVM_TRACE   1
 #           include __FILE__
-            break;
         }
         case (VMXF_PROFILE):
         {
 #define QCVM_PROFILE 1
 #define QCVM_TRACE   0
 #           include __FILE__
-            break;
         }
         case (VMXF_TRACE|VMXF_PROFILE):
         {
 #define QCVM_PROFILE 1
 #define QCVM_TRACE   1
 #           include __FILE__
-            break;
         }
     };
 
 cleanup:
     prog->xflags = oldxflags;
-    prog->localstack_count = 0;
-    prog->stack_count = 0;
+    vec_free(prog->localstack);
+    vec_free(prog->stack);
     if (prog->vmerror)
         return false;
     return true;
@@ -653,6 +597,8 @@ cleanup:
 #if defined(QCVM_EXECUTOR)
 #include <math.h>
 
+opts_cmd_t opts;
+
 const char *type_name[TYPE_COUNT] = {
     "void",
     "string",
@@ -662,26 +608,29 @@ const char *type_name[TYPE_COUNT] = {
     "field",
     "function",
     "pointer",
-#if 0
     "integer",
-#endif
-    "variant"
-};
 
-bool        opts_debug    = false;
-bool        opts_memchk   = false;
+    "variant",
+
+    "struct",
+    "union",
+    "array",
+
+    "nil",
+    "noexpr"
+};
 
 typedef struct {
     int         vtype;
     const char *value;
 } qcvm_parameter;
 
-VECTOR_MAKE(qcvm_parameter, main_params);
+static qcvm_parameter *main_params = NULL;
 
 #define CheckArgs(num) do {                                                    \
     if (prog->argc != (num)) {                                                 \
         prog->vmerror++;                                                       \
-        printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
+        fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
         __FUNCTION__, prog->argc, (num));                                      \
         return -1;                                                             \
     }                                                                          \
@@ -695,9 +644,10 @@ static int qc_print(qc_program *prog)
 {
     size_t i;
     const char *laststr = NULL;
-    for (i = 0; i < prog->argc; ++i) {
+    for (i = 0; i < (size_t)prog->argc; ++i) {
         qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
-        printf("%s", (laststr = prog_getstring(prog, str->string)));
+        laststr = prog_getstring(prog, str->string);
+        printf("%s", laststr);
     }
     if (laststr && (prog->xflags & VMXF_TRACE)) {
         size_t len = strlen(laststr);
@@ -709,7 +659,7 @@ static int qc_print(qc_program *prog)
 
 static int qc_error(qc_program *prog)
 {
-    printf("*** VM raised an error:\n");
+    fprintf(stderr, "*** VM raised an error:\n");
     qc_print(prog);
     prog->vmerror++;
     return -1;
@@ -722,12 +672,23 @@ static int qc_ftos(qc_program *prog)
     qcany str;
     CheckArgs(1);
     num = GetArg(0);
-    snprintf(buffer, sizeof(buffer), "%g", num->_float);
+    util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
     str.string = prog_tempstring(prog, buffer);
     Return(str);
     return 0;
 }
 
+static int qc_stof(qc_program *prog)
+{
+    qcany *str;
+    qcany num;
+    CheckArgs(1);
+    str = GetArg(0);
+    num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
+    Return(num);
+    return 0;
+}
+
 static int qc_vtos(qc_program *prog)
 {
     char buffer[512];
@@ -735,7 +696,7 @@ static int qc_vtos(qc_program *prog)
     qcany str;
     CheckArgs(1);
     num = GetArg(0);
-    snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
+    util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
     str.string = prog_tempstring(prog, buffer);
     Return(str);
     return 0;
@@ -748,7 +709,7 @@ static int qc_etos(qc_program *prog)
     qcany str;
     CheckArgs(1);
     num = GetArg(0);
-    snprintf(buffer, sizeof(buffer), "%i", num->_int);
+    util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
     str.string = prog_tempstring(prog, buffer);
     Return(str);
     return 0;
@@ -772,37 +733,157 @@ static int qc_kill(qc_program *prog)
     return 0;
 }
 
+static int qc_sqrt(qc_program *prog)
+{
+    qcany *num, out;
+    CheckArgs(1);
+    num = GetArg(0);
+    out._float = sqrt(num->_float);
+    Return(out);
+    return 0;
+}
+
 static int qc_vlen(qc_program *prog)
 {
     qcany *vec, len;
     CheckArgs(1);
     vec = GetArg(0);
-    len._float = sqrt(vec->vector[0] * vec->vector[0] + 
+    len._float = sqrt(vec->vector[0] * vec->vector[0] +
                       vec->vector[1] * vec->vector[1] +
                       vec->vector[2] * vec->vector[2]);
     Return(len);
     return 0;
 }
 
+static int qc_normalize(qc_program *prog)
+{
+    double len;
+    qcany *vec;
+    qcany out;
+    CheckArgs(1);
+    vec = GetArg(0);
+    len = sqrt(vec->vector[0] * vec->vector[0] +
+               vec->vector[1] * vec->vector[1] +
+               vec->vector[2] * vec->vector[2]);
+    if (len)
+        len = 1.0 / len;
+    else
+        len = 0;
+    out.vector[0] = len * vec->vector[0];
+    out.vector[1] = len * vec->vector[1];
+    out.vector[2] = len * vec->vector[2];
+    Return(out);
+    return 0;
+}
+
+static int qc_strcat(qc_program *prog)
+{
+    char  *buffer;
+    size_t len1,   len2;
+    char  *cstr1, *cstr2;
+    qcany *str1,  *str2;
+    qcany  out;
+
+    CheckArgs(2);
+    str1 = GetArg(0);
+    str2 = GetArg(1);
+    cstr1 = prog_getstring(prog, str1->string);
+    cstr2 = prog_getstring(prog, str2->string);
+    len1 = strlen(cstr1);
+    len2 = strlen(cstr2);
+    buffer = (char*)mem_a(len1 + len2 + 1);
+    memcpy(buffer, cstr1, len1);
+    memcpy(buffer+len1, cstr2, len2+1);
+    out.string = prog_tempstring(prog, buffer);
+    mem_d(buffer);
+    Return(out);
+    return 0;
+}
+
+static int qc_strcmp(qc_program *prog)
+{
+    char  *cstr1, *cstr2;
+    qcany *str1,  *str2;
+    qcany out;
+
+    if (prog->argc != 2 && prog->argc != 3) {
+        fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
+               prog->argc);
+        return -1;
+    }
+
+    str1 = GetArg(0);
+    str2 = GetArg(1);
+    cstr1 = prog_getstring(prog, str1->string);
+    cstr2 = prog_getstring(prog, str2->string);
+    if (prog->argc == 3)
+        out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
+    else
+        out._float = strcmp(cstr1, cstr2);
+    Return(out);
+    return 0;
+}
+
+static int qc_floor(qc_program *prog)
+{
+    qcany *num, out;
+    CheckArgs(1);
+    num = GetArg(0);
+    out._float = floor(num->_float);
+    Return(out);
+    return 0;
+}
+
 static prog_builtin qc_builtins[] = {
     NULL,
-    &qc_print, /*   1   */
-    &qc_ftos,  /*   2   */
-    &qc_spawn, /*   3   */
-    &qc_kill,  /*   4   */
-    &qc_vtos,  /*   5   */
-    &qc_error, /*   6   */
-    &qc_vlen,  /*   7   */
-    &qc_etos   /*   8   */
+    &qc_print,       /*   1   */
+    &qc_ftos,        /*   2   */
+    &qc_spawn,       /*   3   */
+    &qc_kill,        /*   4   */
+    &qc_vtos,        /*   5   */
+    &qc_error,       /*   6   */
+    &qc_vlen,        /*   7   */
+    &qc_etos,        /*   8   */
+    &qc_stof,        /*   9   */
+    &qc_strcat,      /*   10  */
+    &qc_strcmp,      /*   11  */
+    &qc_normalize,   /*   12  */
+    &qc_sqrt,        /*   13  */
+    &qc_floor        /*   14  */
 };
 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
 
 static const char *arg0 = NULL;
 
-void usage()
+static void version() {
+    printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
+           GMQCC_VERSION_MAJOR,
+           GMQCC_VERSION_MINOR,
+           GMQCC_VERSION_PATCH,
+           __DATE__,
+           __TIME__
+    );
+}
+
+static void usage()
 {
-    printf("usage: [-debug] %s file\n", arg0);
-    exit(1);
+    printf("usage: %s [options] [parameters] file\n", arg0);
+    printf("options:\n");
+    printf("  -h, --help         print this message\n"
+           "  -trace             trace the execution\n"
+           "  -profile           perform profiling during execution\n"
+           "  -info              print information from the prog's header\n"
+           "  -disasm            disassemble and exit\n"
+           "  -disasm-func func  disassemble and exit\n"
+           "  -printdefs         list the defs section\n"
+           "  -printfields       list the field section\n"
+           "  -printfuns         list functions information\n"
+           "  -v                 be verbose\n"
+           "  -vv                be even more verbose\n");
+    printf("parameters:\n");
+    printf("  -vector <V>   pass a vector parameter to main()\n"
+           "  -float  <f>   pass a float parameter to main()\n"
+           "  -string <s>   pass a string parameter to main() \n");
 }
 
 static void prog_main_setparams(qc_program *prog)
@@ -810,33 +891,33 @@ static void prog_main_setparams(qc_program *prog)
     size_t i;
     qcany *arg;
 
-    for (i = 0; i < main_params_elements; ++i) {
+    for (i = 0; i < vec_size(main_params); ++i) {
         arg = GetGlobal(OFS_PARM0 + 3*i);
         arg->vector[0] = 0;
         arg->vector[1] = 0;
         arg->vector[2] = 0;
-        switch (main_params_data[i].vtype) {
+        switch (main_params[i].vtype) {
             case TYPE_VECTOR:
-#ifdef WIN32
-                (void)sscanf_s(main_params_data[i].value, " %f %f %f ",
+#ifdef _MSC_VER
+                (void)sscanf_s(main_params[i].value, " %f %f %f ",
                                &arg->vector[0],
                                &arg->vector[1],
                                &arg->vector[2]);
 #else
-                (void)sscanf(main_params_data[i].value, " %f %f %f ",
+                (void)sscanf(main_params[i].value, " %f %f %f ",
                              &arg->vector[0],
                              &arg->vector[1],
                              &arg->vector[2]);
 #endif
                 break;
             case TYPE_FLOAT:
-                arg->_float = atof(main_params_data[i].value);
+                arg->_float = atof(main_params[i].value);
                 break;
             case TYPE_STRING:
-                arg->string = prog_tempstring(prog, main_params_data[i].value);
+                arg->string = prog_tempstring(prog, main_params[i].value);
                 break;
             default:
-                printf("error: unhandled parameter type: %i\n", main_params_data[i].vtype);
+                fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
                 break;
         }
     }
@@ -851,16 +932,54 @@ int main(int argc, char **argv)
     size_t      xflags = VMXF_DEFAULT;
     bool        opts_printfields = false;
     bool        opts_printdefs   = false;
+    bool        opts_printfuns   = false;
     bool        opts_disasm      = false;
-    bool        opts_info  = false;
+    bool        opts_info        = false;
+    bool        noexec           = false;
+    const char *progsfile        = NULL;
+    const char **dis_list        = NULL;
+    int         opts_v           = 0;
 
     arg0 = argv[0];
 
-    if (argc < 2)
+    if (argc < 2) {
         usage();
+        exit(1);
+    }
 
-    while (argc > 2) {
-        if (!strcmp(argv[1], "-trace")) {
+    while (argc > 1) {
+        if (!strcmp(argv[1], "-h") ||
+            !strcmp(argv[1], "-help") ||
+            !strcmp(argv[1], "--help"))
+        {
+            usage();
+            exit(0);
+        }
+        else if (!strcmp(argv[1], "-v")) {
+            ++opts_v;
+            --argc;
+            ++argv;
+        }
+        else if (!strncmp(argv[1], "-vv", 3)) {
+            const char *av = argv[1]+1;
+            for (; *av; ++av) {
+                if (*av == 'v')
+                    ++opts_v;
+                else {
+                    usage();
+                    exit(1);
+                }
+            }
+            --argc;
+            ++argv;
+        }
+        else if (!strcmp(argv[1], "-version") ||
+                 !strcmp(argv[1], "--version"))
+        {
+            version();
+            exit(0);
+        }
+        else if (!strcmp(argv[1], "-trace")) {
             --argc;
             ++argv;
             xflags |= VMXF_TRACE;
@@ -874,21 +993,43 @@ int main(int argc, char **argv)
             --argc;
             ++argv;
             opts_info = true;
+            noexec = true;
         }
         else if (!strcmp(argv[1], "-disasm")) {
             --argc;
             ++argv;
             opts_disasm = true;
+            noexec = true;
+        }
+        else if (!strcmp(argv[1], "-disasm-func")) {
+            --argc;
+            ++argv;
+            if (argc <= 1) {
+                usage();
+                exit(1);
+            }
+            vec_push(dis_list, argv[1]);
+            --argc;
+            ++argv;
+            noexec = true;
         }
         else if (!strcmp(argv[1], "-printdefs")) {
             --argc;
             ++argv;
             opts_printdefs = true;
+            noexec = true;
+        }
+        else if (!strcmp(argv[1], "-printfuns")) {
+            --argc;
+            ++argv;
+            opts_printfuns = true;
+            noexec = true;
         }
         else if (!strcmp(argv[1], "-printfields")) {
             --argc;
             ++argv;
             opts_printfields = true;
+            noexec = true;
         }
         else if (!strcmp(argv[1], "-vector") ||
                  !strcmp(argv[1], "-string") ||
@@ -904,79 +1045,187 @@ int main(int argc, char **argv)
 
             --argc;
             ++argv;
-            if (argc < 3)
+            if (argc < 2) {
                 usage();
+                exit(1);
+            }
             p.value = argv[1];
 
-            if (main_params_add(p) < 0) {
-                if (main_params_data)
-                    mem_d(main_params_data);
-                printf("cannot add parameter\n");
+            vec_push(main_params, p);
+            --argc;
+            ++argv;
+        }
+        else if (!strcmp(argv[1], "--")) {
+            --argc;
+            ++argv;
+            break;
+        }
+        else if (argv[1][0] != '-') {
+            if (progsfile) {
+                fprintf(stderr, "only 1 program file may be specified\n");
+                usage();
                 exit(1);
             }
+            progsfile = argv[1];
             --argc;
             ++argv;
         }
         else
+        {
+            fprintf(stderr, "unknown parameter: %s\n", argv[1]);
             usage();
+            exit(1);
+        }
     }
 
+    if (argc == 2 && !progsfile) {
+        progsfile = argv[1];
+        --argc;
+        ++argv;
+    }
 
-    prog = prog_load(argv[1]);
+    if (!progsfile) {
+        fprintf(stderr, "must specify a program to execute\n");
+        usage();
+        exit(1);
+    }
+
+    prog = prog_load(progsfile, noexec);
     if (!prog) {
-        printf("failed to load program '%s'\n", argv[1]);
+        fprintf(stderr, "failed to load program '%s'\n", progsfile);
         exit(1);
     }
 
     prog->builtins       = qc_builtins;
     prog->builtins_count = qc_builtins_count;
-    prog->builtins_alloc = 0;
 
     if (opts_info) {
-        printf("Program's system-checksum = 0x%04x\n", (int)prog->crc16);
-        printf("Entity field space: %i\n", (int)prog->entityfields);
+        printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
+        printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
+        printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
+        printf("Counts:\n"
+               "      code: %lu\n"
+               "      defs: %lu\n"
+               "    fields: %lu\n"
+               " functions: %lu\n"
+               "   strings: %lu\n",
+               (unsigned long)vec_size(prog->code),
+               (unsigned long)vec_size(prog->defs),
+               (unsigned long)vec_size(prog->fields),
+               (unsigned long)vec_size(prog->functions),
+               (unsigned long)vec_size(prog->strings));
     }
 
-    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 (opts_info) {
         prog_delete(prog);
         return 0;
     }
+    for (i = 0; i < vec_size(dis_list); ++i) {
+        size_t k;
+        printf("Looking for `%s`\n", dis_list[i]);
+        for (k = 1; k < vec_size(prog->functions); ++k) {
+            const char *name = prog_getstring(prog, prog->functions[k].name);
+            if (!strcmp(name, dis_list[i])) {
+                prog_disasm_function(prog, k);
+                break;
+            }
+        }
+    }
     if (opts_disasm) {
-        for (i = 1; i < prog->functions_count; ++i)
+        for (i = 1; i < vec_size(prog->functions); ++i)
             prog_disasm_function(prog, i);
         return 0;
     }
     if (opts_printdefs) {
-        for (i = 0; i < prog->defs_count; ++i) {
-            printf("Global: %8s %-16s at %u\n",
+        for (i = 0; i < vec_size(prog->defs); ++i) {
+            printf("Global: %8s %-16s at %u%s",
                    type_name[prog->defs[i].type & DEF_TYPEMASK],
                    prog_getstring(prog, prog->defs[i].name),
-                   (unsigned int)prog->defs[i].offset);
+                   (unsigned int)prog->defs[i].offset,
+                   ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
+            if (opts_v) {
+                switch (prog->defs[i].type & DEF_TYPEMASK) {
+                    case TYPE_FLOAT:
+                        printf(" [init: %g]", ((qcany*)(prog->globals + prog->defs[i].offset))->_float);
+                        break;
+                    case TYPE_INTEGER:
+                        printf(" [init: %i]", (int)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
+                        break;
+                    case TYPE_ENTITY:
+                    case TYPE_FUNCTION:
+                    case TYPE_FIELD:
+                    case TYPE_POINTER:
+                        printf(" [init: %u]", (unsigned)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
+                        break;
+                    case TYPE_STRING:
+                        printf(" [init: `%s`]", prog_getstring(prog, ((qcany*)(prog->globals + prog->defs[i].offset))->string ));
+                        break;
+                    default:
+                        break;
+                }
+            }
+            printf("\n");
         }
     }
-    else if (opts_printfields) {
-        for (i = 0; i < prog->fields_count; ++i) {
-            printf("Field: %8s %-16s at %u\n",
+    if (opts_printfields) {
+        for (i = 0; i < vec_size(prog->fields); ++i) {
+            printf("Field: %8s %-16s at %u%s\n",
                    type_name[prog->fields[i].type],
                    prog_getstring(prog, prog->fields[i].name),
-                   (unsigned int)prog->fields[i].offset);
+                   (unsigned int)prog->fields[i].offset,
+                   ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
         }
     }
-    else
-    {
+    if (opts_printfuns) {
+        for (i = 0; i < vec_size(prog->functions); ++i) {
+            int32_t a;
+            printf("Function: %-16s taking %i parameters:(",
+                   prog_getstring(prog, prog->functions[i].name),
+                   (unsigned int)prog->functions[i].nargs);
+            for (a = 0; a < prog->functions[i].nargs; ++a) {
+                printf(" %i", prog->functions[i].argsize[a]);
+            }
+            if (opts_v > 1) {
+                int32_t start = prog->functions[i].entry;
+                if (start < 0)
+                    printf(") builtin %i\n", (int)-start);
+                else {
+                    size_t funsize = 0;
+                    prog_section_statement *st = prog->code + start;
+                    for (;st->opcode != INSTR_DONE; ++st)
+                        ++funsize;
+                    printf(") - %lu instructions", (unsigned long)funsize);
+                    if (opts_v > 2) {
+                        printf(" - locals: %i + %i\n",
+                               prog->functions[i].firstlocal,
+                               prog->functions[i].locals);
+                    }
+                    else
+                        printf("\n");
+                }
+            }
+            else if (opts_v) {
+                printf(") locals: %i + %i\n",
+                       prog->functions[i].firstlocal,
+                       prog->functions[i].locals);
+            }
+            else
+                printf(")\n");
+        }
+    }
+    if (!noexec) {
+        for (i = 1; i < vec_size(prog->functions); ++i) {
+            const char *name = prog_getstring(prog, prog->functions[i].name);
+            if (!strcmp(name, "main"))
+                fnmain = (qcint)i;
+        }
         if (fnmain > 0)
         {
             prog_main_setparams(prog);
             prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
         }
         else
-            printf("No main function found\n");
+            fprintf(stderr, "No main function found\n");
     }
 
     prog_delete(prog);
@@ -996,7 +1245,7 @@ void prog_disasm_function(qc_program *prog, size_t id)
         printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
 
     st = prog->code + fdef->entry;
-    while (st->opcode != AINSTR_END) {
+    while (st->opcode != INSTR_DONE) {
         prog_print_statement(prog, st);
         ++st;
     }
@@ -1023,9 +1272,9 @@ void prog_disasm_function(qc_program *prog, size_t id)
 #endif
 
 while (1) {
-       prog_section_function  *newf;
-       qcany          *ed;
-       qcany          *ptr;
+    prog_section_function  *newf;
+    qcany          *ed;
+    qcany          *ptr;
 
     ++st;
 
@@ -1043,313 +1292,319 @@ while (1) {
             qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
             goto cleanup;
 
-               case INSTR_DONE:
-               case INSTR_RETURN:
-                       /* TODO: 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];
+        case INSTR_DONE:
+        case INSTR_RETURN:
+            /* TODO: 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(prog);
-            if (!prog->stack_count)
+            if (!vec_size(prog->stack))
                 goto cleanup;
 
             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;
-                       break;
-
-               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_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(prog, OPA->string),
-                                             prog_getstring(prog, 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(prog, OPA->string),
-                                              prog_getstring(prog, 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_LE:
-                       OPC->_float = (OPA->_float <= OPB->_float);
-                       break;
-               case INSTR_GE:
-                       OPC->_float = (OPA->_float >= OPB->_float);
-                       break;
-               case INSTR_LT:
-                       OPC->_float = (OPA->_float < OPB->_float);
-                       break;
-               case INSTR_GT:
-                       OPC->_float = (OPA->_float > OPB->_float);
-                       break;
-
-               case INSTR_LOAD_F:
-               case INSTR_LOAD_S:
-               case INSTR_LOAD_FLD:
-               case INSTR_LOAD_ENT:
-               case INSTR_LOAD_FNC:
-                       if (OPA->edict < 0 || OPA->edict >= prog->entities) {
-                           qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
-                               goto cleanup;
-                       }
-                       if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
-                               qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
-                                         prog->filename,
-                                         OPB->_int);
-                               goto cleanup;
-                       }
-                       ed = prog_getedict(prog, OPA->edict);
-                       OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
-                       break;
-               case INSTR_LOAD_V:
-                       if (OPA->edict < 0 || OPA->edict >= prog->entities) {
-                           qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
-                               goto cleanup;
-                       }
-                       if (OPB->_int < 0 || OPB->_int + 3 > prog->entityfields)
-                       {
-                               qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
-                                         prog->filename,
-                                         OPB->_int + 2);
-                               goto cleanup;
-                       }
-                       ed = prog_getedict(prog, 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_ADDRESS:
-                       if (OPA->edict < 0 || OPA->edict >= prog->entities) {
-                               qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
-                               goto cleanup;
-                       }
-                       if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
-                       {
-                               qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
-                                         prog->filename,
-                                         OPB->_int);
-                               goto cleanup;
-                       }
-
-                       ed = prog_getedict(prog, OPA->edict);
-                       OPC->_int = ((qcint*)ed) - prog->entitydata;
-                       OPC->_int += OPB->_int;
-                       break;
-
-               case INSTR_STORE_F:
-               case INSTR_STORE_S:
-               case INSTR_STORE_ENT:
-               case INSTR_STORE_FLD:
-               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_S:
-               case INSTR_STOREP_ENT:
-               case INSTR_STOREP_FLD:
-               case INSTR_STOREP_FNC:
-                       if (OPB->_int < 0 || OPB->_int >= prog->entitydata_count) {
-                               qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
-                               goto cleanup;
-                       }
-                       if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
-                               qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
-                                         prog->filename,
-                                         prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
-                                         OPB->_int);
-                       ptr = (qcany*)(prog->entitydata + OPB->_int);
-                       ptr->_int = OPA->_int;
-                       break;
-               case INSTR_STOREP_V:
-                       if (OPB->_int < 0 || OPB->_int + 2 >= prog->entitydata_count) {
-                               qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
-                               goto cleanup;
-                       }
-                       if (OPB->_int < prog->entityfields && !prog->allowworldwrites)
-                               qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
-                                         prog->filename,
-                                         prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
-                                         OPB->_int);
-                       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_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(prog, OPA->string);
-                       break;
-               case INSTR_NOT_ENT:
-                       OPC->_float = (OPA->edict == 0);
-                       break;
-               case INSTR_NOT_FNC:
-                       OPC->_float = !OPA->function;
-                       break;
-
-               case INSTR_IF:
-                   /* this is consistent with darkplaces' behaviour */
-                       if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
-                       {
-                               st += st->o2.s1 - 1;    /* offset the s++ */
-                               if (++jumpcount >= maxjumps)
-                                       qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
-                       }
-                       break;
-               case INSTR_IFNOT:
-                       if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
-                       {
-                               st += st->o2.s1 - 1;    /* offset the s++ */
-                               if (++jumpcount >= maxjumps)
-                                       qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, 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)
-                               qcvmerror(prog, "NULL function in `%s`", prog->filename);
-
-                       if(!OPA->function || OPA->function >= (unsigned int)prog->functions_count)
-                       {
-                               qcvmerror(prog, "CALL outside the program in `%s` (%i)", prog->filename, (int)OPA->function);
-                               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
-                                       qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
-                                                 builtinnumber, prog->filename);
-                       }
-                       else
-                               st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
-                       if (prog->vmerror)
-                               goto cleanup;
-                       break;
-
-               case INSTR_STATE:
-                   qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
-                       break;
-
-               case INSTR_GOTO:
-                       st += st->o1.s1 - 1;    /* offset the s++ */
-                       if (++jumpcount == 10000000)
-                                       qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
-                       break;
-
-               case INSTR_AND:
-                       OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
-                                     FLOAT_IS_TRUE_FOR_INT(OPB->_int);
-                       break;
-               case INSTR_OR:
-                       OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
-                                     FLOAT_IS_TRUE_FOR_INT(OPB->_int);
-                       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_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:
+        {
+            qcfloat f = OPA->_float;
+            OPC->vector[0] = f * OPB->vector[0];
+            OPC->vector[1] = f * OPB->vector[1];
+            OPC->vector[2] = f * OPB->vector[2];
+            break;
+        }
+        case INSTR_MUL_VF:
+        {
+            qcfloat f = OPB->_float;
+            OPC->vector[0] = f * OPA->vector[0];
+            OPC->vector[1] = f * OPA->vector[1];
+            OPC->vector[2] = f * OPA->vector[2];
+            break;
+        }
+        case INSTR_DIV_F:
+            if (OPB->_float != 0.0f)
+                OPC->_float = OPA->_float / OPB->_float;
+            else
+                OPC->_float = 0;
+            break;
+
+        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_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(prog, OPA->string),
+                                  prog_getstring(prog, 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(prog, OPA->string),
+                                   prog_getstring(prog, 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_LE:
+            OPC->_float = (OPA->_float <= OPB->_float);
+            break;
+        case INSTR_GE:
+            OPC->_float = (OPA->_float >= OPB->_float);
+            break;
+        case INSTR_LT:
+            OPC->_float = (OPA->_float < OPB->_float);
+            break;
+        case INSTR_GT:
+            OPC->_float = (OPA->_float > OPB->_float);
+            break;
+
+        case INSTR_LOAD_F:
+        case INSTR_LOAD_S:
+        case INSTR_LOAD_FLD:
+        case INSTR_LOAD_ENT:
+        case INSTR_LOAD_FNC:
+            if (OPA->edict < 0 || OPA->edict >= prog->entities) {
+                qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
+                goto cleanup;
+            }
+            if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
+                qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
+                          prog->filename,
+                          OPB->_int);
+                goto cleanup;
+            }
+            ed = prog_getedict(prog, OPA->edict);
+            OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
+            break;
+        case INSTR_LOAD_V:
+            if (OPA->edict < 0 || OPA->edict >= prog->entities) {
+                qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
+                goto cleanup;
+            }
+            if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
+            {
+                qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
+                          prog->filename,
+                          OPB->_int + 2);
+                goto cleanup;
+            }
+            ed = prog_getedict(prog, OPA->edict);
+            ptr = (qcany*)( ((qcint*)ed) + OPB->_int );
+            OPC->ivector[0] = ptr->ivector[0];
+            OPC->ivector[1] = ptr->ivector[1];
+            OPC->ivector[2] = ptr->ivector[2];
+            break;
+
+        case INSTR_ADDRESS:
+            if (OPA->edict < 0 || OPA->edict >= prog->entities) {
+                qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
+                goto cleanup;
+            }
+            if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
+            {
+                qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
+                          prog->filename,
+                          OPB->_int);
+                goto cleanup;
+            }
+
+            ed = prog_getedict(prog, OPA->edict);
+            OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
+            break;
+
+        case INSTR_STORE_F:
+        case INSTR_STORE_S:
+        case INSTR_STORE_ENT:
+        case INSTR_STORE_FLD:
+        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_S:
+        case INSTR_STOREP_ENT:
+        case INSTR_STOREP_FLD:
+        case INSTR_STOREP_FNC:
+            if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
+                qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
+                goto cleanup;
+            }
+            if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
+                qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
+                          prog->filename,
+                          prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
+                          OPB->_int);
+            ptr = (qcany*)(prog->entitydata + OPB->_int);
+            ptr->_int = OPA->_int;
+            break;
+        case INSTR_STOREP_V:
+            if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
+                qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
+                goto cleanup;
+            }
+            if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
+                qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
+                          prog->filename,
+                          prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
+                          OPB->_int);
+            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_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(prog, OPA->string);
+            break;
+        case INSTR_NOT_ENT:
+            OPC->_float = (OPA->edict == 0);
+            break;
+        case INSTR_NOT_FNC:
+            OPC->_float = !OPA->function;
+            break;
+
+        case INSTR_IF:
+            /* this is consistent with darkplaces' behaviour */
+            if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
+            {
+                st += st->o2.s1 - 1;    /* offset the s++ */
+                if (++jumpcount >= maxjumps)
+                    qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
+            }
+            break;
+        case INSTR_IFNOT:
+            if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
+            {
+                st += st->o2.s1 - 1;    /* offset the s++ */
+                if (++jumpcount >= maxjumps)
+                    qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, 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)
+                qcvmerror(prog, "NULL function in `%s`", prog->filename);
+
+            if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
+            {
+                qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
+                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 */
+                qcint builtinnumber = -newf->entry;
+                if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
+                    prog->builtins[builtinnumber](prog);
+                else
+                    qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
+                              builtinnumber, prog->filename);
+            }
+            else
+                st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
+            if (prog->vmerror)
+                goto cleanup;
+            break;
+
+        case INSTR_STATE:
+            qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
+            break;
+
+        case INSTR_GOTO:
+            st += st->o1.s1 - 1;    /* offset the s++ */
+            if (++jumpcount == 10000000)
+                qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
+            break;
+
+        case INSTR_AND:
+            OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
+                          FLOAT_IS_TRUE_FOR_INT(OPB->_int);
+            break;
+        case INSTR_OR:
+            OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
+                          FLOAT_IS_TRUE_FOR_INT(OPB->_int);
+            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;
     }
 }