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