]> git.xonotic.org Git - xonotic/gmqcc.git/blob - exec.c
Improved parameter handling of qcvm a little, added --version and --help
[xonotic/gmqcc.git] / exec.c
1 /*
2  * Copyright (C) 2012
3  *     Wolfgang Bumiller
4  *     Dale Weiler
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef QCVM_LOOP
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29
30 #include "gmqcc.h"
31
32 /*
33 (prog_section_statement, code)
34 (prog_section_def,       defs)
35 (prog_section_def,       fields)
36 (prog_section_function,  functions)
37 (char,                   strings)
38 (qcint,                  globals)
39 (qcint,                  entitydata)
40 (bool,                   entitypool)
41 (qcint,         localstack)
42 (qc_exec_stack, stack)
43 (size_t, profile)
44 (prog_builtin, builtins)
45 (const char*, function_stack)
46 */
47
48 static void loaderror(const char *fmt, ...)
49 {
50     int     err = errno;
51     va_list ap;
52     va_start(ap, fmt);
53     vprintf(fmt, ap);
54     va_end(ap);
55     printf(": %s\n", strerror(err));
56 }
57
58 static void qcvmerror(qc_program *prog, const char *fmt, ...)
59 {
60     va_list ap;
61
62     prog->vmerror++;
63
64     va_start(ap, fmt);
65     vprintf(fmt, ap);
66     va_end(ap);
67     putchar('\n');
68 }
69
70 qc_program* prog_load(const char *filename)
71 {
72     qc_program *prog;
73     prog_header header;
74     FILE *file;
75
76     file = util_fopen(filename, "rb");
77     if (!file)
78         return NULL;
79
80     if (fread(&header, sizeof(header), 1, file) != 1) {
81         loaderror("failed to read header from '%s'", filename);
82         fclose(file);
83         return NULL;
84     }
85
86     if (header.version != 6) {
87         loaderror("header says this is a version %i progs, we need version 6\n", header.version);
88         fclose(file);
89         return NULL;
90     }
91
92     prog = (qc_program*)mem_a(sizeof(qc_program));
93     if (!prog) {
94         fclose(file);
95         printf("failed to allocate program data\n");
96         return NULL;
97     }
98     memset(prog, 0, sizeof(*prog));
99
100     prog->entityfields = header.entfield;
101     prog->crc16 = header.crc16;
102
103     prog->filename = util_strdup(filename);
104     if (!prog->filename) {
105         loaderror("failed to store program name");
106         goto error;
107     }
108
109 #define read_data(hdrvar, progvar, reserved)                           \
110     if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) {            \
111         loaderror("seek failed");                                      \
112         goto error;                                                    \
113     }                                                                  \
114     if (fread(vec_add(prog->progvar, header.hdrvar.length + reserved), \
115               sizeof(*prog->progvar),                                  \
116               header.hdrvar.length, file)                              \
117         != header.hdrvar.length)                                       \
118     {                                                                  \
119         loaderror("read failed");                                      \
120         goto error;                                                    \
121     }
122 #define read_data1(x)    read_data(x, x, 0)
123 #define read_data2(x, y) read_data(x, x, y)
124
125     read_data (statements, code, 0);
126     read_data1(defs);
127     read_data1(fields);
128     read_data1(functions);
129     read_data1(strings);
130     read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
131
132     fclose(file);
133
134     /* profile counters */
135     memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
136
137     /* Add tempstring area */
138     prog->tempstring_start = vec_size(prog->strings);
139     prog->tempstring_at    = vec_size(prog->strings);
140     memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
141
142     /* spawn the world entity */
143     vec_push(prog->entitypool, true);
144     memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
145     prog->entities = 1;
146
147     return prog;
148
149 error:
150     if (prog->filename)
151         mem_d(prog->filename);
152     vec_free(prog->code);
153     vec_free(prog->defs);
154     vec_free(prog->fields);
155     vec_free(prog->functions);
156     vec_free(prog->strings);
157     vec_free(prog->globals);
158     vec_free(prog->entitydata);
159     vec_free(prog->entitypool);
160     mem_d(prog);
161     return NULL;
162 }
163
164 void prog_delete(qc_program *prog)
165 {
166     if (prog->filename) mem_d(prog->filename);
167     vec_free(prog->code);
168     vec_free(prog->defs);
169     vec_free(prog->fields);
170     vec_free(prog->functions);
171     vec_free(prog->strings);
172     vec_free(prog->globals);
173     vec_free(prog->entitydata);
174     vec_free(prog->entitypool);
175     vec_free(prog->localstack);
176     vec_free(prog->stack);
177     vec_free(prog->profile);
178     mem_d(prog);
179 }
180
181 /***********************************************************************
182  * VM code
183  */
184
185 char* prog_getstring(qc_program *prog, qcint str)
186 {
187     if (str < 0 || str >= (qcint)vec_size(prog->strings))
188         return "<<<invalid string>>>";
189     return prog->strings + str;
190 }
191
192 prog_section_def* prog_entfield(qc_program *prog, qcint off)
193 {
194     size_t i;
195     for (i = 0; i < vec_size(prog->fields); ++i) {
196         if (prog->fields[i].offset == off)
197             return (prog->fields + i);
198     }
199     return NULL;
200 }
201
202 prog_section_def* prog_getdef(qc_program *prog, qcint off)
203 {
204     size_t i;
205     for (i = 0; i < vec_size(prog->defs); ++i) {
206         if (prog->defs[i].offset == off)
207             return (prog->defs + i);
208     }
209     return NULL;
210 }
211
212 qcany* prog_getedict(qc_program *prog, qcint e)
213 {
214     if (e >= (qcint)vec_size(prog->entitypool)) {
215         prog->vmerror++;
216         printf("Accessing out of bounds edict %i\n", (int)e);
217         e = 0;
218     }
219     return (qcany*)(prog->entitydata + (prog->entityfields * e));
220 }
221
222 qcint prog_spawn_entity(qc_program *prog)
223 {
224     char  *data;
225     qcint  e;
226     for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) {
227         if (!prog->entitypool[e]) {
228             data = (char*)(prog->entitydata + (prog->entityfields * e));
229             memset(data, 0, prog->entityfields * sizeof(qcint));
230             return e;
231         }
232     }
233     vec_push(prog->entitypool, true);
234     prog->entities++;
235     data = (char*)vec_add(prog->entitydata, prog->entityfields);
236     memset(data, 0, prog->entityfields * sizeof(qcint));
237     return e;
238 }
239
240 void prog_free_entity(qc_program *prog, qcint e)
241 {
242     if (!e) {
243         prog->vmerror++;
244         printf("Trying to free world entity\n");
245         return;
246     }
247     if (e >= (qcint)vec_size(prog->entitypool)) {
248         prog->vmerror++;
249         printf("Trying to free out of bounds entity\n");
250         return;
251     }
252     if (!prog->entitypool[e]) {
253         prog->vmerror++;
254         printf("Double free on entity\n");
255         return;
256     }
257     prog->entitypool[e] = false;
258 }
259
260 qcint prog_tempstring(qc_program *prog, const char *_str)
261 {
262     /* we don't access it, but the macro-generated functions don't use
263      * const
264      */
265     char *str = (char*)_str;
266
267     size_t len = strlen(str);
268     size_t at = prog->tempstring_at;
269
270     /* when we reach the end we start over */
271     if (at + len >= vec_size(prog->strings))
272         at = prog->tempstring_start;
273
274     /* when it doesn't fit, reallocate */
275     if (at + len >= vec_size(prog->strings))
276     {
277         (void)vec_add(prog->strings, len+1);
278         memcpy(prog->strings + at, str, len+1);
279         return at;
280     }
281
282     /* when it fits, just copy */
283     memcpy(prog->strings + at, str, len+1);
284     prog->tempstring_at += len+1;
285     return at;
286 }
287
288 static size_t print_escaped_string(const char *str, size_t maxlen)
289 {
290     size_t len = 2;
291     putchar('"');
292     --maxlen; /* because we're lazy and have escape sequences */
293     while (*str) {
294         if (len >= maxlen) {
295             putchar('.');
296             putchar('.');
297             putchar('.');
298             len += 3;
299             break;
300         }
301         switch (*str) {
302             case '\a': len += 2; putchar('\\'); putchar('a'); break;
303             case '\b': len += 2; putchar('\\'); putchar('b'); break;
304             case '\r': len += 2; putchar('\\'); putchar('r'); break;
305             case '\n': len += 2; putchar('\\'); putchar('n'); break;
306             case '\t': len += 2; putchar('\\'); putchar('t'); break;
307             case '\f': len += 2; putchar('\\'); putchar('f'); break;
308             case '\v': len += 2; putchar('\\'); putchar('v'); break;
309             case '\\': len += 2; putchar('\\'); putchar('\\'); break;
310             case '"':  len += 2; putchar('\\'); putchar('"'); break;
311             default:
312                 ++len;
313                 putchar(*str);
314                 break;
315         }
316         ++str;
317     }
318     putchar('"');
319     return len;
320 }
321
322 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
323 {
324     static char spaces[28+1] = "                            ";
325     prog_section_def *def;
326     qcany    *value;
327     int       len;
328
329     if (!glob) {
330         len = printf("<null>,");
331         goto done;
332     }
333
334     def = prog_getdef(prog, glob);
335     value = (qcany*)(&prog->globals[glob]);
336
337     len = printf("[@%u] ", glob);
338     if (def) {
339         const char *name = prog_getstring(prog, def->name);
340         if (name[0] == '#')
341             len += printf("$");
342         else
343             len += printf("%s ", name);
344         vtype = def->type & DEF_TYPEMASK;
345     }
346
347     switch (vtype) {
348         case TYPE_VOID:
349         case TYPE_ENTITY:
350         case TYPE_FIELD:
351         case TYPE_FUNCTION:
352         case TYPE_POINTER:
353             len += printf("(%i),", value->_int);
354             break;
355         case TYPE_VECTOR:
356             len += printf("'%g %g %g',", value->vector[0],
357                                          value->vector[1],
358                                          value->vector[2]);
359             break;
360         case TYPE_STRING:
361             len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
362             len += printf(",");
363             /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
364             break;
365         case TYPE_FLOAT:
366         default:
367             len += printf("%g,", value->_float);
368             break;
369     }
370 done:
371     if (len < (int)sizeof(spaces)-1) {
372         spaces[sizeof(spaces)-1-len] = 0;
373         printf(spaces);
374         spaces[sizeof(spaces)-1-len] = ' ';
375     }
376 }
377
378 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
379 {
380     if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
381         printf("<illegal instruction %d>\n", st->opcode);
382         return;
383     }
384     if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
385         size_t i;
386         for (i = 0; i < vec_size(prog->function_stack); ++i)
387             printf("->");
388         printf("%s:", vec_last(prog->function_stack));
389     }
390     printf(" <> %-12s", asm_instr[st->opcode].m);
391     if (st->opcode >= INSTR_IF &&
392         st->opcode <= INSTR_IFNOT)
393     {
394         trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
395         printf("%d\n", st->o2.s1);
396     }
397     else if (st->opcode >= INSTR_CALL0 &&
398              st->opcode <= INSTR_CALL8)
399     {
400         trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
401         printf("\n");
402     }
403     else if (st->opcode == INSTR_GOTO)
404     {
405         printf("%i\n", st->o1.s1);
406     }
407     else
408     {
409         int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
410         switch (st->opcode)
411         {
412             case INSTR_MUL_FV:
413                 t[1] = t[2] = TYPE_VECTOR;
414                 break;
415             case INSTR_MUL_VF:
416                 t[0] = t[2] = TYPE_VECTOR;
417                 break;
418             case INSTR_MUL_V:
419                 t[0] = t[1] = TYPE_VECTOR;
420                 break;
421             case INSTR_ADD_V:
422             case INSTR_SUB_V:
423             case INSTR_EQ_V:
424             case INSTR_NE_V:
425                 t[0] = t[1] = t[2] = TYPE_VECTOR;
426                 break;
427             case INSTR_EQ_S:
428             case INSTR_NE_S:
429                 t[0] = t[1] = TYPE_STRING;
430                 break;
431             case INSTR_STORE_F:
432             case INSTR_STOREP_F:
433                 t[2] = -1;
434                 break;
435             case INSTR_STORE_V:
436                 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
437                 break;
438             case INSTR_STORE_S:
439                 t[0] = t[1] = TYPE_STRING; t[2] = -1;
440                 break;
441             case INSTR_STORE_ENT:
442                 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
443                 break;
444             case INSTR_STORE_FLD:
445                 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
446                 break;
447             case INSTR_STORE_FNC:
448                 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
449                 break;
450             case INSTR_STOREP_V:
451                 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
452                 break;
453             case INSTR_STOREP_S:
454                 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
455                 break;
456             case INSTR_STOREP_ENT:
457                 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
458                 break;
459             case INSTR_STOREP_FLD:
460                 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
461                 break;
462             case INSTR_STOREP_FNC:
463                 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
464                 break;
465         }
466         if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
467         else           printf("(none),          ");
468         if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
469         else           printf("(none),          ");
470         if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
471         else           printf("(none)");
472         printf("\n");
473     }
474     fflush(stdout);
475 }
476
477 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
478 {
479     qc_exec_stack st;
480     size_t  parampos;
481     int32_t p;
482
483     /* back up locals */
484     st.localsp  = vec_size(prog->localstack);
485     st.stmt     = prog->statement;
486     st.function = func;
487
488     if (prog->xflags & VMXF_TRACE) {
489         vec_push(prog->function_stack, prog_getstring(prog, func->name));
490     }
491
492 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
493     if (vec_size(prog->stack))
494     {
495         prog_section_function *cur;
496         cur = prog->stack[vec_size(prog->stack)-1].function;
497         if (cur)
498         {
499             qcint *globals = prog->globals + cur->firstlocal;
500             vec_append(prog->localstack, cur->locals, globals);
501         }
502     }
503 #else
504     {
505         qcint *globals = prog->globals + func->firstlocal;
506         vec_append(prog->localstack, func->locals, globals);
507     }
508 #endif
509
510     /* copy parameters */
511     parampos = func->firstlocal;
512     for (p = 0; p < func->nargs; ++p)
513     {
514         size_t s;
515         for (s = 0; s < func->argsize[p]; ++s) {
516             prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
517             ++parampos;
518         }
519     }
520
521     vec_push(prog->stack, st);
522
523     return func->entry;
524 }
525
526 static qcint prog_leavefunction(qc_program *prog)
527 {
528     prog_section_function *prev = NULL;
529     size_t oldsp;
530
531     qc_exec_stack st = vec_last(prog->stack);
532
533     if (prog->xflags & VMXF_TRACE) {
534         if (vec_size(prog->function_stack))
535             vec_pop(prog->function_stack);
536     }
537
538 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
539     if (vec_size(prog->stack) > 1) {
540         prev  = prog->stack[vec_size(prog->stack)-2].function;
541         oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
542     }
543 #else
544     prev  = prog->stack[vec_size(prog->stack)-1].function;
545     oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
546 #endif
547     if (prev) {
548         qcint *globals = prog->globals + prev->firstlocal;
549         memcpy(globals, prog->localstack + oldsp, prev->locals);
550         /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
551         vec_shrinkto(prog->localstack, oldsp);
552     }
553
554     vec_pop(prog->stack);
555
556     return st.stmt - 1; /* offset the ++st */
557 }
558
559 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
560 {
561     long jumpcount = 0;
562     size_t oldxflags = prog->xflags;
563     prog_section_statement *st;
564
565     prog->vmerror = 0;
566     prog->xflags = flags;
567
568     st = prog->code + prog_enterfunction(prog, func);
569     --st;
570     switch (flags)
571     {
572         default:
573         case 0:
574         {
575 #define QCVM_LOOP    1
576 #define QCVM_PROFILE 0
577 #define QCVM_TRACE   0
578 #           include __FILE__
579             break;
580         }
581         case (VMXF_TRACE):
582         {
583 #define QCVM_PROFILE 0
584 #define QCVM_TRACE   1
585 #           include __FILE__
586             break;
587         }
588         case (VMXF_PROFILE):
589         {
590 #define QCVM_PROFILE 1
591 #define QCVM_TRACE   0
592 #           include __FILE__
593             break;
594         }
595         case (VMXF_TRACE|VMXF_PROFILE):
596         {
597 #define QCVM_PROFILE 1
598 #define QCVM_TRACE   1
599 #           include __FILE__
600             break;
601         }
602     };
603
604 cleanup:
605     prog->xflags = oldxflags;
606     vec_free(prog->localstack);
607     vec_free(prog->stack);
608     if (prog->vmerror)
609         return false;
610     return true;
611 }
612
613 /***********************************************************************
614  * main for when building the standalone executor
615  */
616
617 #if defined(QCVM_EXECUTOR)
618 #include <math.h>
619
620 opts_cmd_t opts;
621
622 const char *type_name[TYPE_COUNT] = {
623     "void",
624     "string",
625     "float",
626     "vector",
627     "entity",
628     "field",
629     "function",
630     "pointer",
631 #if 0
632     "integer",
633 #endif
634     "variant"
635 };
636
637 typedef struct {
638     int         vtype;
639     const char *value;
640 } qcvm_parameter;
641
642 qcvm_parameter *main_params = NULL;
643
644 #define CheckArgs(num) do {                                                    \
645     if (prog->argc != (num)) {                                                 \
646         prog->vmerror++;                                                       \
647         printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
648         __FUNCTION__, prog->argc, (num));                                      \
649         return -1;                                                             \
650     }                                                                          \
651 } while (0)
652
653 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
654 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
655 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
656
657 static int qc_print(qc_program *prog)
658 {
659     size_t i;
660     const char *laststr = NULL;
661     for (i = 0; i < (size_t)prog->argc; ++i) {
662         qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
663         printf("%s", (laststr = prog_getstring(prog, str->string)));
664     }
665     if (laststr && (prog->xflags & VMXF_TRACE)) {
666         size_t len = strlen(laststr);
667         if (!len || laststr[len-1] != '\n')
668             printf("\n");
669     }
670     return 0;
671 }
672
673 static int qc_error(qc_program *prog)
674 {
675     printf("*** VM raised an error:\n");
676     qc_print(prog);
677     prog->vmerror++;
678     return -1;
679 }
680
681 static int qc_ftos(qc_program *prog)
682 {
683     char buffer[512];
684     qcany *num;
685     qcany str;
686     CheckArgs(1);
687     num = GetArg(0);
688     snprintf(buffer, sizeof(buffer), "%g", num->_float);
689     str.string = prog_tempstring(prog, buffer);
690     Return(str);
691     return 0;
692 }
693
694 static int qc_stof(qc_program *prog)
695 {
696     qcany *str;
697     qcany num;
698     CheckArgs(1);
699     str = GetArg(0);
700     num._float = strtof(prog_getstring(prog, str->string), NULL);
701     Return(num);
702     return 0;
703 }
704
705 static int qc_vtos(qc_program *prog)
706 {
707     char buffer[512];
708     qcany *num;
709     qcany str;
710     CheckArgs(1);
711     num = GetArg(0);
712     snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
713     str.string = prog_tempstring(prog, buffer);
714     Return(str);
715     return 0;
716 }
717
718 static int qc_etos(qc_program *prog)
719 {
720     char buffer[512];
721     qcany *num;
722     qcany str;
723     CheckArgs(1);
724     num = GetArg(0);
725     snprintf(buffer, sizeof(buffer), "%i", num->_int);
726     str.string = prog_tempstring(prog, buffer);
727     Return(str);
728     return 0;
729 }
730
731 static int qc_spawn(qc_program *prog)
732 {
733     qcany ent;
734     CheckArgs(0);
735     ent.edict = prog_spawn_entity(prog);
736     Return(ent);
737     return (ent.edict ? 0 : -1);
738 }
739
740 static int qc_kill(qc_program *prog)
741 {
742     qcany *ent;
743     CheckArgs(1);
744     ent = GetArg(0);
745     prog_free_entity(prog, ent->edict);
746     return 0;
747 }
748
749 static int qc_vlen(qc_program *prog)
750 {
751     qcany *vec, len;
752     CheckArgs(1);
753     vec = GetArg(0);
754     len._float = sqrt(vec->vector[0] * vec->vector[0] +
755                       vec->vector[1] * vec->vector[1] +
756                       vec->vector[2] * vec->vector[2]);
757     Return(len);
758     return 0;
759 }
760
761 static prog_builtin qc_builtins[] = {
762     NULL,
763     &qc_print, /*   1   */
764     &qc_ftos,  /*   2   */
765     &qc_spawn, /*   3   */
766     &qc_kill,  /*   4   */
767     &qc_vtos,  /*   5   */
768     &qc_error, /*   6   */
769     &qc_vlen,  /*   7   */
770     &qc_etos,  /*   8   */
771     &qc_stof   /*   9   */
772 };
773 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
774
775 static const char *arg0 = NULL;
776
777 static void version() {
778     printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
779            GMQCC_VERSION_MAJOR,
780            GMQCC_VERSION_MINOR,
781            GMQCC_VERSION_PATCH,
782            __DATE__,
783            __TIME__
784     );
785 }
786
787 static void usage()
788 {
789     printf("usage: %s [options] [parameters] file\n", arg0);
790     printf("options:\n");
791     printf("  -h, --help    print this message\n"
792            "  -trace        trace the execution\n"
793            "  -profile      perform profiling during execution\n"
794            "  -info         print information from the prog's header\n"
795            "  -disasm       disassemble and exit\n"
796            "  -printdefs    list the defs section\n"
797            "  -printfields  list the field section\n"
798            "  -printfuns    list functions information\n");
799     printf("parameters:\n");
800     printf("  -vector <V>   pass a vector parameter to main()\n"
801            "  -float  <f>   pass a float parameter to main()\n"
802            "  -string <s>   pass a string parameter to main() \n");
803 }
804
805 static void prog_main_setparams(qc_program *prog)
806 {
807     size_t i;
808     qcany *arg;
809
810     for (i = 0; i < vec_size(main_params); ++i) {
811         arg = GetGlobal(OFS_PARM0 + 3*i);
812         arg->vector[0] = 0;
813         arg->vector[1] = 0;
814         arg->vector[2] = 0;
815         switch (main_params[i].vtype) {
816             case TYPE_VECTOR:
817 #ifdef WIN32
818                 (void)sscanf_s(main_params[i].value, " %f %f %f ",
819                                &arg->vector[0],
820                                &arg->vector[1],
821                                &arg->vector[2]);
822 #else
823                 (void)sscanf(main_params[i].value, " %f %f %f ",
824                              &arg->vector[0],
825                              &arg->vector[1],
826                              &arg->vector[2]);
827 #endif
828                 break;
829             case TYPE_FLOAT:
830                 arg->_float = atof(main_params[i].value);
831                 break;
832             case TYPE_STRING:
833                 arg->string = prog_tempstring(prog, main_params[i].value);
834                 break;
835             default:
836                 printf("error: unhandled parameter type: %i\n", main_params[i].vtype);
837                 break;
838         }
839     }
840 }
841
842 void prog_disasm_function(qc_program *prog, size_t id);
843 int main(int argc, char **argv)
844 {
845     size_t      i;
846     qcint       fnmain = -1;
847     qc_program *prog;
848     size_t      xflags = VMXF_DEFAULT;
849     bool        opts_printfields = false;
850     bool        opts_printdefs   = false;
851     bool        opts_printfuns   = false;
852     bool        opts_disasm      = false;
853     bool        opts_info        = false;
854     bool        noexec           = false;
855     const char *progsfile        = NULL;
856
857     arg0 = argv[0];
858
859     if (argc < 2) {
860         usage();
861         exit(1);
862     }
863
864     while (argc > 1) {
865         if (!strcmp(argv[1], "-h") ||
866             !strcmp(argv[1], "-help") ||
867             !strcmp(argv[1], "--help"))
868         {
869             usage();
870             exit(0);
871         }
872         else if (!strcmp(argv[1], "-v") ||
873                  !strcmp(argv[1], "-version") ||
874                  !strcmp(argv[1], "--version"))
875         {
876             version();
877             exit(0);
878         }
879         else if (!strcmp(argv[1], "-trace")) {
880             --argc;
881             ++argv;
882             xflags |= VMXF_TRACE;
883         }
884         else if (!strcmp(argv[1], "-profile")) {
885             --argc;
886             ++argv;
887             xflags |= VMXF_PROFILE;
888         }
889         else if (!strcmp(argv[1], "-info")) {
890             --argc;
891             ++argv;
892             opts_info = true;
893             noexec = true;
894         }
895         else if (!strcmp(argv[1], "-disasm")) {
896             --argc;
897             ++argv;
898             opts_disasm = true;
899             noexec = true;
900         }
901         else if (!strcmp(argv[1], "-printdefs")) {
902             --argc;
903             ++argv;
904             opts_printdefs = true;
905             noexec = true;
906         }
907         else if (!strcmp(argv[1], "-printfuns")) {
908             --argc;
909             ++argv;
910             opts_printfuns = true;
911             noexec = true;
912         }
913         else if (!strcmp(argv[1], "-printfields")) {
914             --argc;
915             ++argv;
916             opts_printfields = true;
917             noexec = true;
918         }
919         else if (!strcmp(argv[1], "-vector") ||
920                  !strcmp(argv[1], "-string") ||
921                  !strcmp(argv[1], "-float") )
922         {
923             qcvm_parameter p;
924             if (argv[1][1] == 'f')
925                 p.vtype = TYPE_FLOAT;
926             else if (argv[1][1] == 's')
927                 p.vtype = TYPE_STRING;
928             else if (argv[1][1] == 'v')
929                 p.vtype = TYPE_VECTOR;
930
931             --argc;
932             ++argv;
933             if (argc < 3) {
934                 usage();
935                 exit(1);
936             }
937             p.value = argv[1];
938
939             vec_push(main_params, p);
940             --argc;
941             ++argv;
942         }
943         else if (!strcmp(argv[1], "--")) {
944             --argc;
945             ++argv;
946             break;
947         }
948         else if (argv[1][0] != '-') {
949             if (progsfile) {
950                 printf("only 1 program file may be specified\n");
951                 usage();
952                 exit(1);
953             }
954             progsfile = argv[1];
955             --argc;
956             ++argv;
957         }
958         else
959         {
960             usage();
961             exit(1);
962         }
963     }
964
965     if (argc > 2) {
966         usage();
967         exit(1);
968     }
969     if (argc > 1) {
970         if (progsfile) {
971             printf("only 1 program file may be specified\n");
972             usage();
973             exit(1);
974         }
975         progsfile = argv[1];
976         --argc;
977         ++argv;
978     }
979
980     if (!progsfile) {
981         usage();
982         exit(1);
983     }
984
985     prog = prog_load(progsfile);
986     if (!prog) {
987         printf("failed to load program '%s'\n", progsfile);
988         exit(1);
989     }
990
991     prog->builtins       = qc_builtins;
992     prog->builtins_count = qc_builtins_count;
993
994     if (opts_info) {
995         printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
996         printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
997         printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
998     }
999
1000     if (opts_info) {
1001         prog_delete(prog);
1002         return 0;
1003     }
1004     if (opts_disasm) {
1005         for (i = 1; i < vec_size(prog->functions); ++i)
1006             prog_disasm_function(prog, i);
1007         return 0;
1008     }
1009     if (opts_printdefs) {
1010         for (i = 0; i < vec_size(prog->defs); ++i) {
1011             printf("Global: %8s %-16s at %u%s\n",
1012                    type_name[prog->defs[i].type & DEF_TYPEMASK],
1013                    prog_getstring(prog, prog->defs[i].name),
1014                    (unsigned int)prog->defs[i].offset,
1015                    ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1016         }
1017     }
1018     if (opts_printfields) {
1019         for (i = 0; i < vec_size(prog->fields); ++i) {
1020             printf("Field: %8s %-16s at %u%s\n",
1021                    type_name[prog->fields[i].type],
1022                    prog_getstring(prog, prog->fields[i].name),
1023                    (unsigned int)prog->fields[i].offset,
1024                    ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1025         }
1026     }
1027     if (opts_printfuns) {
1028         for (i = 0; i < vec_size(prog->functions); ++i) {
1029             int32_t a;
1030             printf("Function: %-16s taking %i parameters:(",
1031                    prog_getstring(prog, prog->functions[i].name),
1032                    (unsigned int)prog->functions[i].nargs);
1033             for (a = 0; a < prog->functions[i].nargs; ++a) {
1034                 printf(" %i", prog->functions[i].argsize[a]);
1035             }
1036             printf(") locals: %i + %i\n",
1037                    prog->functions[i].firstlocal,
1038                    prog->functions[i].locals);
1039         }
1040     }
1041     if (!noexec) {
1042         for (i = 1; i < vec_size(prog->functions); ++i) {
1043             const char *name = prog_getstring(prog, prog->functions[i].name);
1044             if (!strcmp(name, "main"))
1045                 fnmain = (qcint)i;
1046         }
1047         if (fnmain > 0)
1048         {
1049             prog_main_setparams(prog);
1050             prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1051         }
1052         else
1053             printf("No main function found\n");
1054     }
1055
1056     prog_delete(prog);
1057     return 0;
1058 }
1059
1060 void prog_disasm_function(qc_program *prog, size_t id)
1061 {
1062     prog_section_function *fdef = prog->functions + id;
1063     prog_section_statement *st;
1064
1065     if (fdef->entry < 0) {
1066         printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1067         return;
1068     }
1069     else
1070         printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1071
1072     st = prog->code + fdef->entry;
1073     while (st->opcode != INSTR_DONE) {
1074         prog_print_statement(prog, st);
1075         ++st;
1076     }
1077 }
1078 #endif
1079 #else /* !QCVM_LOOP */
1080 /*
1081  * Everything from here on is not including into the compilation of the
1082  * executor.  This is simply code that is #included via #include __FILE__
1083  * see when QCVM_LOOP is defined, the rest of the code above do not get
1084  * re-included.  So this really just acts like one large macro, but it
1085  * sort of isn't, which makes it nicer looking.
1086  */
1087
1088 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1089 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1090 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1091
1092 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1093
1094 /* to be consistent with current darkplaces behaviour */
1095 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1096 #   define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1097 #endif
1098
1099 while (1) {
1100     prog_section_function  *newf;
1101     qcany          *ed;
1102     qcany          *ptr;
1103
1104     ++st;
1105
1106 #if QCVM_PROFILE
1107     prog->profile[st - prog->code]++;
1108 #endif
1109
1110 #if QCVM_TRACE
1111     prog_print_statement(prog, st);
1112 #endif
1113
1114     switch (st->opcode)
1115     {
1116         default:
1117             qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1118             goto cleanup;
1119
1120         case INSTR_DONE:
1121         case INSTR_RETURN:
1122             /* TODO: add instruction count to function profile count */
1123             GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1124             GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1125             GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1126
1127             st = prog->code + prog_leavefunction(prog);
1128             if (!vec_size(prog->stack))
1129                 goto cleanup;
1130
1131             break;
1132
1133         case INSTR_MUL_F:
1134             OPC->_float = OPA->_float * OPB->_float;
1135             break;
1136         case INSTR_MUL_V:
1137             OPC->_float = OPA->vector[0]*OPB->vector[0] +
1138                           OPA->vector[1]*OPB->vector[1] +
1139                           OPA->vector[2]*OPB->vector[2];
1140             break;
1141         case INSTR_MUL_FV:
1142             OPC->vector[0] = OPA->_float * OPB->vector[0];
1143             OPC->vector[1] = OPA->_float * OPB->vector[1];
1144             OPC->vector[2] = OPA->_float * OPB->vector[2];
1145             break;
1146         case INSTR_MUL_VF:
1147             OPC->vector[0] = OPB->_float * OPA->vector[0];
1148             OPC->vector[1] = OPB->_float * OPA->vector[1];
1149             OPC->vector[2] = OPB->_float * OPA->vector[2];
1150             break;
1151         case INSTR_DIV_F:
1152             if (OPB->_float != 0.0f)
1153                 OPC->_float = OPA->_float / OPB->_float;
1154             else
1155                 OPC->_float = 0;
1156             break;
1157
1158         case INSTR_ADD_F:
1159             OPC->_float = OPA->_float + OPB->_float;
1160             break;
1161         case INSTR_ADD_V:
1162             OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1163             OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1164             OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1165             break;
1166         case INSTR_SUB_F:
1167             OPC->_float = OPA->_float - OPB->_float;
1168             break;
1169         case INSTR_SUB_V:
1170             OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1171             OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1172             OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1173             break;
1174
1175         case INSTR_EQ_F:
1176             OPC->_float = (OPA->_float == OPB->_float);
1177             break;
1178         case INSTR_EQ_V:
1179             OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1180                            (OPA->vector[1] == OPB->vector[1]) &&
1181                            (OPA->vector[2] == OPB->vector[2]) );
1182             break;
1183         case INSTR_EQ_S:
1184             OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1185                                   prog_getstring(prog, OPB->string));
1186             break;
1187         case INSTR_EQ_E:
1188             OPC->_float = (OPA->_int == OPB->_int);
1189             break;
1190         case INSTR_EQ_FNC:
1191             OPC->_float = (OPA->function == OPB->function);
1192             break;
1193         case INSTR_NE_F:
1194             OPC->_float = (OPA->_float != OPB->_float);
1195             break;
1196         case INSTR_NE_V:
1197             OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1198                            (OPA->vector[1] != OPB->vector[1]) ||
1199                            (OPA->vector[2] != OPB->vector[2]) );
1200             break;
1201         case INSTR_NE_S:
1202             OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1203                                    prog_getstring(prog, OPB->string));
1204             break;
1205         case INSTR_NE_E:
1206             OPC->_float = (OPA->_int != OPB->_int);
1207             break;
1208         case INSTR_NE_FNC:
1209             OPC->_float = (OPA->function != OPB->function);
1210             break;
1211
1212         case INSTR_LE:
1213             OPC->_float = (OPA->_float <= OPB->_float);
1214             break;
1215         case INSTR_GE:
1216             OPC->_float = (OPA->_float >= OPB->_float);
1217             break;
1218         case INSTR_LT:
1219             OPC->_float = (OPA->_float < OPB->_float);
1220             break;
1221         case INSTR_GT:
1222             OPC->_float = (OPA->_float > OPB->_float);
1223             break;
1224
1225         case INSTR_LOAD_F:
1226         case INSTR_LOAD_S:
1227         case INSTR_LOAD_FLD:
1228         case INSTR_LOAD_ENT:
1229         case INSTR_LOAD_FNC:
1230             if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1231                 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1232                 goto cleanup;
1233             }
1234             if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1235                 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1236                           prog->filename,
1237                           OPB->_int);
1238                 goto cleanup;
1239             }
1240             ed = prog_getedict(prog, OPA->edict);
1241             OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1242             break;
1243         case INSTR_LOAD_V:
1244             if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1245                 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1246                 goto cleanup;
1247             }
1248             if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1249             {
1250                 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1251                           prog->filename,
1252                           OPB->_int + 2);
1253                 goto cleanup;
1254             }
1255             ed = prog_getedict(prog, OPA->edict);
1256             OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1257             OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1258             OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1259             break;
1260
1261         case INSTR_ADDRESS:
1262             if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1263                 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1264                 goto cleanup;
1265             }
1266             if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1267             {
1268                 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1269                           prog->filename,
1270                           OPB->_int);
1271                 goto cleanup;
1272             }
1273
1274             ed = prog_getedict(prog, OPA->edict);
1275             OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1276             break;
1277
1278         case INSTR_STORE_F:
1279         case INSTR_STORE_S:
1280         case INSTR_STORE_ENT:
1281         case INSTR_STORE_FLD:
1282         case INSTR_STORE_FNC:
1283             OPB->_int = OPA->_int;
1284             break;
1285         case INSTR_STORE_V:
1286             OPB->ivector[0] = OPA->ivector[0];
1287             OPB->ivector[1] = OPA->ivector[1];
1288             OPB->ivector[2] = OPA->ivector[2];
1289             break;
1290
1291         case INSTR_STOREP_F:
1292         case INSTR_STOREP_S:
1293         case INSTR_STOREP_ENT:
1294         case INSTR_STOREP_FLD:
1295         case INSTR_STOREP_FNC:
1296             if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1297                 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1298                 goto cleanup;
1299             }
1300             if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1301                 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1302                           prog->filename,
1303                           prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1304                           OPB->_int);
1305             ptr = (qcany*)(prog->entitydata + OPB->_int);
1306             ptr->_int = OPA->_int;
1307             break;
1308         case INSTR_STOREP_V:
1309             if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1310                 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1311                 goto cleanup;
1312             }
1313             if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1314                 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1315                           prog->filename,
1316                           prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1317                           OPB->_int);
1318             ptr = (qcany*)(prog->entitydata + OPB->_int);
1319             ptr->ivector[0] = OPA->ivector[0];
1320             ptr->ivector[1] = OPA->ivector[1];
1321             ptr->ivector[2] = OPA->ivector[2];
1322             break;
1323
1324         case INSTR_NOT_F:
1325             OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1326             break;
1327         case INSTR_NOT_V:
1328             OPC->_float = !OPA->vector[0] &&
1329                           !OPA->vector[1] &&
1330                           !OPA->vector[2];
1331             break;
1332         case INSTR_NOT_S:
1333             OPC->_float = !OPA->string ||
1334                           !*prog_getstring(prog, OPA->string);
1335             break;
1336         case INSTR_NOT_ENT:
1337             OPC->_float = (OPA->edict == 0);
1338             break;
1339         case INSTR_NOT_FNC:
1340             OPC->_float = !OPA->function;
1341             break;
1342
1343         case INSTR_IF:
1344             /* this is consistent with darkplaces' behaviour */
1345             if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1346             {
1347                 st += st->o2.s1 - 1;    /* offset the s++ */
1348                 if (++jumpcount >= maxjumps)
1349                     qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1350             }
1351             break;
1352         case INSTR_IFNOT:
1353             if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1354             {
1355                 st += st->o2.s1 - 1;    /* offset the s++ */
1356                 if (++jumpcount >= maxjumps)
1357                     qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1358             }
1359             break;
1360
1361         case INSTR_CALL0:
1362         case INSTR_CALL1:
1363         case INSTR_CALL2:
1364         case INSTR_CALL3:
1365         case INSTR_CALL4:
1366         case INSTR_CALL5:
1367         case INSTR_CALL6:
1368         case INSTR_CALL7:
1369         case INSTR_CALL8:
1370             prog->argc = st->opcode - INSTR_CALL0;
1371             if (!OPA->function)
1372                 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1373
1374             if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1375             {
1376                 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1377                 goto cleanup;
1378             }
1379
1380             newf = &prog->functions[OPA->function];
1381             newf->profile++;
1382
1383             prog->statement = (st - prog->code) + 1;
1384
1385             if (newf->entry < 0)
1386             {
1387                 /* negative statements are built in functions */
1388                 qcint builtinnumber = -newf->entry;
1389                 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1390                     prog->builtins[builtinnumber](prog);
1391                 else
1392                     qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1393                               builtinnumber, prog->filename);
1394             }
1395             else
1396                 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1397             if (prog->vmerror)
1398                 goto cleanup;
1399             break;
1400
1401         case INSTR_STATE:
1402             qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1403             break;
1404
1405         case INSTR_GOTO:
1406             st += st->o1.s1 - 1;    /* offset the s++ */
1407             if (++jumpcount == 10000000)
1408                 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1409             break;
1410
1411         case INSTR_AND:
1412             OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1413                           FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1414             break;
1415         case INSTR_OR:
1416             OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1417                           FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1418             break;
1419
1420         case INSTR_BITAND:
1421             OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1422             break;
1423         case INSTR_BITOR:
1424             OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1425             break;
1426     }
1427 }
1428
1429 #undef QCVM_PROFILE
1430 #undef QCVM_TRACE
1431 #endif /* !QCVM_LOOP */