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