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