]> git.xonotic.org Git - xonotic/gmqcc.git/blob - exec.c
c1aa695f23c43f67813f69e775d51e9e69a14e6a
[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         printf("Counts:\n"
981                "      code: %lu\n"
982                "      defs: %lu\n"
983                "    fields: %lu\n"
984                " functions: %lu\n"
985                "   strings: %lu\n",
986                (unsigned long)vec_size(prog->code),
987                (unsigned long)vec_size(prog->defs),
988                (unsigned long)vec_size(prog->fields),
989                (unsigned long)vec_size(prog->functions),
990                (unsigned long)vec_size(prog->strings));
991     }
992
993     if (opts_info) {
994         prog_delete(prog);
995         return 0;
996     }
997     if (opts_disasm) {
998         for (i = 1; i < vec_size(prog->functions); ++i)
999             prog_disasm_function(prog, i);
1000         return 0;
1001     }
1002     if (opts_printdefs) {
1003         for (i = 0; i < vec_size(prog->defs); ++i) {
1004             printf("Global: %8s %-16s at %u%s\n",
1005                    type_name[prog->defs[i].type & DEF_TYPEMASK],
1006                    prog_getstring(prog, prog->defs[i].name),
1007                    (unsigned int)prog->defs[i].offset,
1008                    ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1009         }
1010     }
1011     if (opts_printfields) {
1012         for (i = 0; i < vec_size(prog->fields); ++i) {
1013             printf("Field: %8s %-16s at %u%s\n",
1014                    type_name[prog->fields[i].type],
1015                    prog_getstring(prog, prog->fields[i].name),
1016                    (unsigned int)prog->fields[i].offset,
1017                    ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1018         }
1019     }
1020     if (opts_printfuns) {
1021         for (i = 0; i < vec_size(prog->functions); ++i) {
1022             int32_t a;
1023             printf("Function: %-16s taking %i parameters:(",
1024                    prog_getstring(prog, prog->functions[i].name),
1025                    (unsigned int)prog->functions[i].nargs);
1026             for (a = 0; a < prog->functions[i].nargs; ++a) {
1027                 printf(" %i", prog->functions[i].argsize[a]);
1028             }
1029             printf(") locals: %i + %i\n",
1030                    prog->functions[i].firstlocal,
1031                    prog->functions[i].locals);
1032         }
1033     }
1034     if (!noexec) {
1035         for (i = 1; i < vec_size(prog->functions); ++i) {
1036             const char *name = prog_getstring(prog, prog->functions[i].name);
1037             if (!strcmp(name, "main"))
1038                 fnmain = (qcint)i;
1039         }
1040         if (fnmain > 0)
1041         {
1042             prog_main_setparams(prog);
1043             prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1044         }
1045         else
1046             printf("No main function found\n");
1047     }
1048
1049     prog_delete(prog);
1050     return 0;
1051 }
1052
1053 void prog_disasm_function(qc_program *prog, size_t id)
1054 {
1055     prog_section_function *fdef = prog->functions + id;
1056     prog_section_statement *st;
1057
1058     if (fdef->entry < 0) {
1059         printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1060         return;
1061     }
1062     else
1063         printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1064
1065     st = prog->code + fdef->entry;
1066     while (st->opcode != INSTR_DONE) {
1067         prog_print_statement(prog, st);
1068         ++st;
1069     }
1070 }
1071 #endif
1072 #else /* !QCVM_LOOP */
1073 /*
1074  * Everything from here on is not including into the compilation of the
1075  * executor.  This is simply code that is #included via #include __FILE__
1076  * see when QCVM_LOOP is defined, the rest of the code above do not get
1077  * re-included.  So this really just acts like one large macro, but it
1078  * sort of isn't, which makes it nicer looking.
1079  */
1080
1081 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1082 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1083 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1084
1085 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1086
1087 /* to be consistent with current darkplaces behaviour */
1088 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1089 #   define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1090 #endif
1091
1092 while (1) {
1093     prog_section_function  *newf;
1094     qcany          *ed;
1095     qcany          *ptr;
1096
1097     ++st;
1098
1099 #if QCVM_PROFILE
1100     prog->profile[st - prog->code]++;
1101 #endif
1102
1103 #if QCVM_TRACE
1104     prog_print_statement(prog, st);
1105 #endif
1106
1107     switch (st->opcode)
1108     {
1109         default:
1110             qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1111             goto cleanup;
1112
1113         case INSTR_DONE:
1114         case INSTR_RETURN:
1115             /* TODO: add instruction count to function profile count */
1116             GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1117             GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1118             GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1119
1120             st = prog->code + prog_leavefunction(prog);
1121             if (!vec_size(prog->stack))
1122                 goto cleanup;
1123
1124             break;
1125
1126         case INSTR_MUL_F:
1127             OPC->_float = OPA->_float * OPB->_float;
1128             break;
1129         case INSTR_MUL_V:
1130             OPC->_float = OPA->vector[0]*OPB->vector[0] +
1131                           OPA->vector[1]*OPB->vector[1] +
1132                           OPA->vector[2]*OPB->vector[2];
1133             break;
1134         case INSTR_MUL_FV:
1135             OPC->vector[0] = OPA->_float * OPB->vector[0];
1136             OPC->vector[1] = OPA->_float * OPB->vector[1];
1137             OPC->vector[2] = OPA->_float * OPB->vector[2];
1138             break;
1139         case INSTR_MUL_VF:
1140             OPC->vector[0] = OPB->_float * OPA->vector[0];
1141             OPC->vector[1] = OPB->_float * OPA->vector[1];
1142             OPC->vector[2] = OPB->_float * OPA->vector[2];
1143             break;
1144         case INSTR_DIV_F:
1145             if (OPB->_float != 0.0f)
1146                 OPC->_float = OPA->_float / OPB->_float;
1147             else
1148                 OPC->_float = 0;
1149             break;
1150
1151         case INSTR_ADD_F:
1152             OPC->_float = OPA->_float + OPB->_float;
1153             break;
1154         case INSTR_ADD_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         case INSTR_SUB_F:
1160             OPC->_float = OPA->_float - OPB->_float;
1161             break;
1162         case INSTR_SUB_V:
1163             OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1164             OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1165             OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1166             break;
1167
1168         case INSTR_EQ_F:
1169             OPC->_float = (OPA->_float == OPB->_float);
1170             break;
1171         case INSTR_EQ_V:
1172             OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1173                            (OPA->vector[1] == OPB->vector[1]) &&
1174                            (OPA->vector[2] == OPB->vector[2]) );
1175             break;
1176         case INSTR_EQ_S:
1177             OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1178                                   prog_getstring(prog, OPB->string));
1179             break;
1180         case INSTR_EQ_E:
1181             OPC->_float = (OPA->_int == OPB->_int);
1182             break;
1183         case INSTR_EQ_FNC:
1184             OPC->_float = (OPA->function == OPB->function);
1185             break;
1186         case INSTR_NE_F:
1187             OPC->_float = (OPA->_float != OPB->_float);
1188             break;
1189         case INSTR_NE_V:
1190             OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1191                            (OPA->vector[1] != OPB->vector[1]) ||
1192                            (OPA->vector[2] != OPB->vector[2]) );
1193             break;
1194         case INSTR_NE_S:
1195             OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1196                                    prog_getstring(prog, OPB->string));
1197             break;
1198         case INSTR_NE_E:
1199             OPC->_float = (OPA->_int != OPB->_int);
1200             break;
1201         case INSTR_NE_FNC:
1202             OPC->_float = (OPA->function != OPB->function);
1203             break;
1204
1205         case INSTR_LE:
1206             OPC->_float = (OPA->_float <= OPB->_float);
1207             break;
1208         case INSTR_GE:
1209             OPC->_float = (OPA->_float >= OPB->_float);
1210             break;
1211         case INSTR_LT:
1212             OPC->_float = (OPA->_float < OPB->_float);
1213             break;
1214         case INSTR_GT:
1215             OPC->_float = (OPA->_float > OPB->_float);
1216             break;
1217
1218         case INSTR_LOAD_F:
1219         case INSTR_LOAD_S:
1220         case INSTR_LOAD_FLD:
1221         case INSTR_LOAD_ENT:
1222         case INSTR_LOAD_FNC:
1223             if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1224                 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1225                 goto cleanup;
1226             }
1227             if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1228                 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1229                           prog->filename,
1230                           OPB->_int);
1231                 goto cleanup;
1232             }
1233             ed = prog_getedict(prog, OPA->edict);
1234             OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1235             break;
1236         case INSTR_LOAD_V:
1237             if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1238                 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1239                 goto cleanup;
1240             }
1241             if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1242             {
1243                 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1244                           prog->filename,
1245                           OPB->_int + 2);
1246                 goto cleanup;
1247             }
1248             ed = prog_getedict(prog, OPA->edict);
1249             OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1250             OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1251             OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1252             break;
1253
1254         case INSTR_ADDRESS:
1255             if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1256                 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1257                 goto cleanup;
1258             }
1259             if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1260             {
1261                 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1262                           prog->filename,
1263                           OPB->_int);
1264                 goto cleanup;
1265             }
1266
1267             ed = prog_getedict(prog, OPA->edict);
1268             OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1269             break;
1270
1271         case INSTR_STORE_F:
1272         case INSTR_STORE_S:
1273         case INSTR_STORE_ENT:
1274         case INSTR_STORE_FLD:
1275         case INSTR_STORE_FNC:
1276             OPB->_int = OPA->_int;
1277             break;
1278         case INSTR_STORE_V:
1279             OPB->ivector[0] = OPA->ivector[0];
1280             OPB->ivector[1] = OPA->ivector[1];
1281             OPB->ivector[2] = OPA->ivector[2];
1282             break;
1283
1284         case INSTR_STOREP_F:
1285         case INSTR_STOREP_S:
1286         case INSTR_STOREP_ENT:
1287         case INSTR_STOREP_FLD:
1288         case INSTR_STOREP_FNC:
1289             if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1290                 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1291                 goto cleanup;
1292             }
1293             if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1294                 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1295                           prog->filename,
1296                           prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1297                           OPB->_int);
1298             ptr = (qcany*)(prog->entitydata + OPB->_int);
1299             ptr->_int = OPA->_int;
1300             break;
1301         case INSTR_STOREP_V:
1302             if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1303                 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1304                 goto cleanup;
1305             }
1306             if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1307                 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1308                           prog->filename,
1309                           prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1310                           OPB->_int);
1311             ptr = (qcany*)(prog->entitydata + OPB->_int);
1312             ptr->ivector[0] = OPA->ivector[0];
1313             ptr->ivector[1] = OPA->ivector[1];
1314             ptr->ivector[2] = OPA->ivector[2];
1315             break;
1316
1317         case INSTR_NOT_F:
1318             OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1319             break;
1320         case INSTR_NOT_V:
1321             OPC->_float = !OPA->vector[0] &&
1322                           !OPA->vector[1] &&
1323                           !OPA->vector[2];
1324             break;
1325         case INSTR_NOT_S:
1326             OPC->_float = !OPA->string ||
1327                           !*prog_getstring(prog, OPA->string);
1328             break;
1329         case INSTR_NOT_ENT:
1330             OPC->_float = (OPA->edict == 0);
1331             break;
1332         case INSTR_NOT_FNC:
1333             OPC->_float = !OPA->function;
1334             break;
1335
1336         case INSTR_IF:
1337             /* this is consistent with darkplaces' behaviour */
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         case INSTR_IFNOT:
1346             if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1347             {
1348                 st += st->o2.s1 - 1;    /* offset the s++ */
1349                 if (++jumpcount >= maxjumps)
1350                     qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1351             }
1352             break;
1353
1354         case INSTR_CALL0:
1355         case INSTR_CALL1:
1356         case INSTR_CALL2:
1357         case INSTR_CALL3:
1358         case INSTR_CALL4:
1359         case INSTR_CALL5:
1360         case INSTR_CALL6:
1361         case INSTR_CALL7:
1362         case INSTR_CALL8:
1363             prog->argc = st->opcode - INSTR_CALL0;
1364             if (!OPA->function)
1365                 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1366
1367             if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1368             {
1369                 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1370                 goto cleanup;
1371             }
1372
1373             newf = &prog->functions[OPA->function];
1374             newf->profile++;
1375
1376             prog->statement = (st - prog->code) + 1;
1377
1378             if (newf->entry < 0)
1379             {
1380                 /* negative statements are built in functions */
1381                 qcint builtinnumber = -newf->entry;
1382                 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1383                     prog->builtins[builtinnumber](prog);
1384                 else
1385                     qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1386                               builtinnumber, prog->filename);
1387             }
1388             else
1389                 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1390             if (prog->vmerror)
1391                 goto cleanup;
1392             break;
1393
1394         case INSTR_STATE:
1395             qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1396             break;
1397
1398         case INSTR_GOTO:
1399             st += st->o1.s1 - 1;    /* offset the s++ */
1400             if (++jumpcount == 10000000)
1401                 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1402             break;
1403
1404         case INSTR_AND:
1405             OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1406                           FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1407             break;
1408         case INSTR_OR:
1409             OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1410                           FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1411             break;
1412
1413         case INSTR_BITAND:
1414             OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1415             break;
1416         case INSTR_BITOR:
1417             OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1418             break;
1419     }
1420 }
1421
1422 #undef QCVM_PROFILE
1423 #undef QCVM_TRACE
1424 #endif /* !QCVM_LOOP */