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