]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - exec.c
check for TYPE_NOEXPR in general when applying an operator
[xonotic/gmqcc.git] / exec.c
diff --git a/exec.c b/exec.c
index f50eac1b40098794d00a223f05ddfff505dbce81..8df96f01145007c37dc4ca4d9bc23c1bcab1109d 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"
 
-/*
-(prog_section_statement, code)
-(prog_section_def,       defs)
-(prog_section_def,       fields)
-(prog_section_function,  functions)
-(char,                   strings)
-(qcint,                  globals)
-(qcint,                  entitydata)
-(bool,                   entitypool)
-(qcint,         localstack)
-(qc_exec_stack, stack)
-(size_t, profile)
-(prog_builtin, builtins)
-(const char*, function_stack)
-*/
-
 static void loaderror(const char *fmt, ...)
 {
     int     err = errno;
@@ -69,29 +53,28 @@ static void qcvmerror(qc_program *prog, const char *fmt, ...)
 
 qc_program* prog_load(const char *filename)
 {
-    qc_program *prog;
-    prog_header header;
-    FILE *file;
+    qc_program   *prog;
+    prog_header   header;
+    FILE         *file   = file_open(filename, "rb");
 
-    file = util_fopen(filename, "rb");
     if (!file)
         return NULL;
 
-    if (fread(&header, sizeof(header), 1, file) != 1) {
+    if (file_read(&header, sizeof(header), 1, file) != 1) {
         loaderror("failed to read header from '%s'", filename);
-        fclose(file);
+        file_close(file);
         return NULL;
     }
 
     if (header.version != 6) {
         loaderror("header says this is a version %i progs, we need version 6\n", header.version);
-        fclose(file);
+        file_close(file);
         return NULL;
     }
 
     prog = (qc_program*)mem_a(sizeof(qc_program));
     if (!prog) {
-        fclose(file);
+        file_close(file);
         printf("failed to allocate program data\n");
         return NULL;
     }
@@ -107,15 +90,17 @@ qc_program* prog_load(const char *filename)
     }
 
 #define read_data(hdrvar, progvar, reserved)                           \
-    if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) {            \
+    if (file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) {        \
         loaderror("seek failed");                                      \
         goto error;                                                    \
     }                                                                  \
-    if (fread(vec_add(prog->progvar, header.hdrvar.length + reserved), \
-              sizeof(*prog->progvar),                                  \
-              header.hdrvar.length, file)                              \
-        != header.hdrvar.length)                                       \
-    {                                                                  \
+    if (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;                                                    \
     }
@@ -129,7 +114,7 @@ qc_program* prog_load(const char *filename)
     read_data1(strings);
     read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
 
-    fclose(file);
+    file_close(file);
 
     /* profile counters */
     memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
@@ -184,8 +169,9 @@ void prog_delete(qc_program *prog)
 
 char* prog_getstring(qc_program *prog, qcint str)
 {
-    if (str < 0 || str >= vec_size(prog->strings))
-        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;
 }
 
@@ -211,7 +197,7 @@ prog_section_def* prog_getdef(qc_program *prog, qcint off)
 
 qcany* prog_getedict(qc_program *prog, qcint e)
 {
-    if (e >= vec_size(prog->entitypool)) {
+    if (e >= (qcint)vec_size(prog->entitypool)) {
         prog->vmerror++;
         printf("Accessing out of bounds edict %i\n", (int)e);
         e = 0;
@@ -244,7 +230,7 @@ void prog_free_entity(qc_program *prog, qcint e)
         printf("Trying to free world entity\n");
         return;
     }
-    if (e >= vec_size(prog->entitypool)) {
+    if (e >= (qcint)vec_size(prog->entitypool)) {
         prog->vmerror++;
         printf("Trying to free out of bounds entity\n");
         return;
@@ -285,9 +271,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) {
@@ -358,7 +344,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;
@@ -368,9 +357,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);
+        file_puts(stdout, spaces);
         spaces[sizeof(spaces)-1-len] = ' ';
     }
 }
@@ -477,7 +466,8 @@ static void prog_print_statement(qc_program *prog, prog_section_statement *st)
 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  = vec_size(prog->localstack);
@@ -485,7 +475,8 @@ static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
     st.function = func;
 
     if (prog->xflags & VMXF_TRACE) {
-        vec_push(prog->function_stack, prog_getstring(prog, func->name));
+        const char *str = prog_getstring(prog, func->name);
+        vec_push(prog->function_stack, str);
     }
 
 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
@@ -545,7 +536,7 @@ static qcint prog_leavefunction(qc_program *prog)
 #endif
     if (prev) {
         qcint *globals = prog->globals + prev->firstlocal;
-        memcpy(globals, prog->localstack + oldsp, prev->locals);
+        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);
     }
@@ -575,28 +566,24 @@ 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;
         }
     };
 
@@ -616,6 +603,8 @@ cleanup:
 #if defined(QCVM_EXECUTOR)
 #include <math.h>
 
+opts_cmd_t opts;
+
 const char *type_name[TYPE_COUNT] = {
     "void",
     "string",
@@ -631,9 +620,6 @@ const char *type_name[TYPE_COUNT] = {
     "variant"
 };
 
-bool        opts_debug    = false;
-bool        opts_memchk   = false;
-
 typedef struct {
     int         vtype;
     const char *value;
@@ -658,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);
@@ -691,6 +678,17 @@ static int qc_ftos(qc_program *prog)
     return 0;
 }
 
+static int qc_stof(qc_program *prog)
+{
+    qcany *str;
+    qcany num;
+    CheckArgs(1);
+    str = GetArg(0);
+    num._float = strtof(prog_getstring(prog, str->string), NULL);
+    Return(num);
+    return 0;
+}
+
 static int qc_vtos(qc_program *prog)
 {
     char buffer[512];
@@ -740,32 +738,108 @@ 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_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) {
+        printf("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 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  */
 };
 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)
@@ -780,7 +854,7 @@ static void prog_main_setparams(qc_program *prog)
         arg->vector[2] = 0;
         switch (main_params[i].vtype) {
             case TYPE_VECTOR:
-#ifdef WIN32
+#ifdef _MSC_VER
                 (void)sscanf_s(main_params[i].value, " %f %f %f ",
                                &arg->vector[0],
                                &arg->vector[1],
@@ -814,16 +888,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;
@@ -837,21 +949,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") ||
@@ -867,22 +1001,61 @@ int main(int argc, char **argv)
 
             --argc;
             ++argv;
-            if (argc < 3)
+            if (argc < 3) {
                 usage();
+                exit(1);
+            }
             p.value = argv[1];
 
             vec_push(main_params, p);
             --argc;
             ++argv;
         }
+        else if (!strcmp(argv[1], "--")) {
+            --argc;
+            ++argv;
+            break;
+        }
+        else if (argv[1][0] != '-') {
+            if (progsfile) {
+                printf("only 1 program file may be specified\n");
+                usage();
+                exit(1);
+            }
+            progsfile = argv[1];
+            --argc;
+            ++argv;
+        }
         else
+        {
+            usage();
+            exit(1);
+        }
+    }
+
+    if (argc > 2) {
+        usage();
+        exit(1);
+    }
+    if (argc > 1) {
+        if (progsfile) {
+            printf("only 1 program file may be specified\n");
             usage();
+            exit(1);
+        }
+        progsfile = argv[1];
+        --argc;
+        ++argv;
     }
 
+    if (!progsfile) {
+        usage();
+        exit(1);
+    }
 
-    prog = prog_load(argv[1]);
+    prog = prog_load(progsfile);
     if (!prog) {
-        printf("failed to load program '%s'\n", argv[1]);
+        printf("failed to load program '%s'\n", progsfile);
         exit(1);
     }
 
@@ -890,20 +1063,37 @@ int main(int argc, char **argv)
     prog->builtins_count = qc_builtins_count;
 
     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 < vec_size(prog->functions); ++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 < vec_size(prog->functions); ++i)
             prog_disasm_function(prog, i);
@@ -911,22 +1101,65 @@ int main(int argc, char **argv)
     }
     if (opts_printdefs) {
         for (i = 0; i < vec_size(prog->defs); ++i) {
-            printf("Global: %8s %-16s at %u\n",
+            printf("Global: %8s %-16s at %u%s\n",
                    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]" : ""));
         }
     }
-    else if (opts_printfields) {
+    if (opts_printfields) {
         for (i = 0; i < vec_size(prog->fields); ++i) {
-            printf("Field: %8s %-16s at %u\n",
+            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);
@@ -953,7 +1186,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;
     }
@@ -1128,7 +1361,7 @@ while (1) {
                 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)
+            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,
@@ -1176,11 +1409,11 @@ while (1) {
         case INSTR_STOREP_ENT:
         case INSTR_STOREP_FLD:
         case INSTR_STOREP_FNC:
-            if (OPB->_int < 0 || OPB->_int >= vec_size(prog->entitydata)) {
+            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 < prog->entityfields && !prog->allowworldwrites)
+            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),
@@ -1189,11 +1422,11 @@ while (1) {
             ptr->_int = OPA->_int;
             break;
         case INSTR_STOREP_V:
-            if (OPB->_int < 0 || OPB->_int + 2 >= vec_size(prog->entitydata)) {
+            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 < prog->entityfields && !prog->allowworldwrites)
+            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),
@@ -1254,7 +1487,7 @@ while (1) {
             if (!OPA->function)
                 qcvmerror(prog, "NULL function in `%s`", prog->filename);
 
-            if(!OPA->function || OPA->function >= (unsigned int)vec_size(prog->functions))
+            if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
             {
                 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
                 goto cleanup;
@@ -1268,8 +1501,8 @@ while (1) {
             if (newf->entry < 0)
             {
                 /* negative statements are built in functions */
-                int builtinnumber = -newf->entry;
-                if (builtinnumber < prog->builtins_count && prog->builtins[builtinnumber])
+                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",