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