2 * Copyright (C) 2012, 2013, 2014
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:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
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
32 static void loaderror(const char *fmt, ...)
39 printf(": %s\n", util_strerror(err));
42 static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
54 qc_program_t* prog_load(const char *filename, bool skipversion)
58 fs_file_t *file = fs_file_open(filename, "rb");
63 if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
64 loaderror("failed to read header from '%s'", filename);
69 util_swap_header(&header);
71 if (!skipversion && header.version != 6) {
72 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
77 prog = (qc_program_t*)mem_a(sizeof(qc_program_t));
80 fprintf(stderr, "failed to allocate program data\n");
83 memset(prog, 0, sizeof(*prog));
85 prog->entityfields = header.entfield;
86 prog->crc16 = header.crc16;
88 prog->filename = util_strdup(filename);
89 if (!prog->filename) {
90 loaderror("failed to store program name");
94 #define read_data(hdrvar, progvar, reserved) \
95 if (fs_file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
96 loaderror("seek failed"); \
100 vec_add(prog->progvar, header.hdrvar.length + reserved), \
101 sizeof(*prog->progvar), \
102 header.hdrvar.length, \
104 )!= header.hdrvar.length \
106 loaderror("read failed"); \
109 #define read_data1(x) read_data(x, x, 0)
110 #define read_data2(x, y) read_data(x, x, y)
112 read_data (statements, code, 0);
115 read_data1(functions);
117 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
119 util_swap_statements (prog->code);
120 util_swap_defs_fields(prog->defs);
121 util_swap_defs_fields(prog->fields);
122 util_swap_functions (prog->functions);
123 util_swap_globals (prog->globals);
127 /* profile counters */
128 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
130 /* Add tempstring area */
131 prog->tempstring_start = vec_size(prog->strings);
132 prog->tempstring_at = vec_size(prog->strings);
133 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
135 /* spawn the world entity */
136 vec_push(prog->entitypool, true);
137 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
144 mem_d(prog->filename);
145 vec_free(prog->code);
146 vec_free(prog->defs);
147 vec_free(prog->fields);
148 vec_free(prog->functions);
149 vec_free(prog->strings);
150 vec_free(prog->globals);
151 vec_free(prog->entitydata);
152 vec_free(prog->entitypool);
159 void prog_delete(qc_program_t *prog)
161 if (prog->filename) mem_d(prog->filename);
162 vec_free(prog->code);
163 vec_free(prog->defs);
164 vec_free(prog->fields);
165 vec_free(prog->functions);
166 vec_free(prog->strings);
167 vec_free(prog->globals);
168 vec_free(prog->entitydata);
169 vec_free(prog->entitypool);
170 vec_free(prog->localstack);
171 vec_free(prog->stack);
172 vec_free(prog->profile);
176 /***********************************************************************
180 const char* prog_getstring(qc_program_t *prog, qcint_t str) {
181 /* cast for return required for C++ */
182 if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
183 return "<<<invalid string>>>";
185 return prog->strings + str;
188 prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
190 for (i = 0; i < vec_size(prog->fields); ++i) {
191 if (prog->fields[i].offset == off)
192 return (prog->fields + i);
197 prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
200 for (i = 0; i < vec_size(prog->defs); ++i) {
201 if (prog->defs[i].offset == off)
202 return (prog->defs + i);
207 qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
208 if (e >= (qcint_t)vec_size(prog->entitypool)) {
210 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
213 return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
216 static qcint_t prog_spawn_entity(qc_program_t *prog) {
219 for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
220 if (!prog->entitypool[e]) {
221 data = (char*)(prog->entitydata + (prog->entityfields * e));
222 memset(data, 0, prog->entityfields * sizeof(qcint_t));
226 vec_push(prog->entitypool, true);
228 data = (char*)vec_add(prog->entitydata, prog->entityfields);
229 memset(data, 0, prog->entityfields * sizeof(qcint_t));
233 static void prog_free_entity(qc_program_t *prog, qcint_t e) {
236 fprintf(stderr, "Trying to free world entity\n");
239 if (e >= (qcint_t)vec_size(prog->entitypool)) {
241 fprintf(stderr, "Trying to free out of bounds entity\n");
244 if (!prog->entitypool[e]) {
246 fprintf(stderr, "Double free on entity\n");
249 prog->entitypool[e] = false;
252 qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
253 size_t len = strlen(str);
254 size_t at = prog->tempstring_at;
256 /* when we reach the end we start over */
257 if (at + len >= vec_size(prog->strings))
258 at = prog->tempstring_start;
260 /* when it doesn't fit, reallocate */
261 if (at + len >= vec_size(prog->strings))
263 (void)vec_add(prog->strings, len+1);
264 memcpy(prog->strings + at, str, len+1);
268 /* when it fits, just copy */
269 memcpy(prog->strings + at, str, len+1);
270 prog->tempstring_at += len+1;
274 static size_t print_escaped_string(const char *str, size_t maxlen) {
277 --maxlen; /* because we're lazy and have escape sequences */
287 case '\a': len += 2; putchar('\\'); putchar('a'); break;
288 case '\b': len += 2; putchar('\\'); putchar('b'); break;
289 case '\r': len += 2; putchar('\\'); putchar('r'); break;
290 case '\n': len += 2; putchar('\\'); putchar('n'); break;
291 case '\t': len += 2; putchar('\\'); putchar('t'); break;
292 case '\f': len += 2; putchar('\\'); putchar('f'); break;
293 case '\v': len += 2; putchar('\\'); putchar('v'); break;
294 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
295 case '"': len += 2; putchar('\\'); putchar('"'); break;
307 static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
308 static char spaces[28+1] = " ";
309 prog_section_def_t *def;
314 if ((len = printf("<null>,")) == -1)
320 def = prog_getdef(prog, glob);
321 value = (qcany_t*)(&prog->globals[glob]);
323 len = printf("[@%u] ", glob);
325 const char *name = prog_getstring(prog, def->name);
329 len += printf("%s ", name);
330 vtype = def->type & DEF_TYPEMASK;
339 len += printf("(%i),", value->_int);
342 len += printf("'%g %g %g',", value->vector[0],
348 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
350 len += printf("(null)");
352 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
356 len += printf("%g,", value->_float);
360 if (len < (int)sizeof(spaces)-1) {
361 spaces[sizeof(spaces)-1-len] = 0;
362 fs_file_puts((fs_file_t*)stdout, spaces);
363 spaces[sizeof(spaces)-1-len] = ' ';
367 static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
368 if (st->opcode >= VINSTR_END) {
369 printf("<illegal instruction %d>\n", st->opcode);
372 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
374 for (i = 0; i < vec_size(prog->function_stack); ++i)
376 printf("%s:", vec_last(prog->function_stack));
378 printf(" <> %-12s", util_instr_str[st->opcode]);
379 if (st->opcode >= INSTR_IF &&
380 st->opcode <= INSTR_IFNOT)
382 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
383 printf("%d\n", st->o2.s1);
385 else if (st->opcode >= INSTR_CALL0 &&
386 st->opcode <= INSTR_CALL8)
388 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
391 else if (st->opcode == INSTR_GOTO)
393 printf("%i\n", st->o1.s1);
397 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
401 t[1] = t[2] = TYPE_VECTOR;
404 t[0] = t[2] = TYPE_VECTOR;
407 t[0] = t[1] = TYPE_VECTOR;
413 t[0] = t[1] = t[2] = TYPE_VECTOR;
417 t[0] = t[1] = TYPE_STRING;
424 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
427 t[0] = t[1] = TYPE_STRING; t[2] = -1;
429 case INSTR_STORE_ENT:
430 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
432 case INSTR_STORE_FLD:
433 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
435 case INSTR_STORE_FNC:
436 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
439 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
442 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
444 case INSTR_STOREP_ENT:
445 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
447 case INSTR_STOREP_FLD:
448 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
450 case INSTR_STOREP_FNC:
451 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
454 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
455 else printf("(none), ");
456 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
457 else printf("(none), ");
458 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
459 else printf("(none)");
464 static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
470 st.localsp = vec_size(prog->localstack);
471 st.stmt = prog->statement;
474 if (prog->xflags & VMXF_TRACE) {
475 const char *str = prog_getstring(prog, func->name);
476 vec_push(prog->function_stack, str);
479 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
480 if (vec_size(prog->stack))
482 prog_section_function_t *cur;
483 cur = prog->stack[vec_size(prog->stack)-1].function;
486 qcint_t *globals = prog->globals + cur->firstlocal;
487 vec_append(prog->localstack, cur->locals, globals);
492 qcint_t *globals = prog->globals + func->firstlocal;
493 vec_append(prog->localstack, func->locals, globals);
497 /* copy parameters */
498 parampos = func->firstlocal;
499 for (p = 0; p < func->nargs; ++p)
502 for (s = 0; s < func->argsize[p]; ++s) {
503 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
508 vec_push(prog->stack, st);
513 static qcint_t prog_leavefunction(qc_program_t *prog) {
514 prog_section_function_t *prev = NULL;
517 qc_exec_stack_t st = vec_last(prog->stack);
519 if (prog->xflags & VMXF_TRACE) {
520 if (vec_size(prog->function_stack))
521 vec_pop(prog->function_stack);
524 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
525 if (vec_size(prog->stack) > 1) {
526 prev = prog->stack[vec_size(prog->stack)-2].function;
527 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
530 prev = prog->stack[vec_size(prog->stack)-1].function;
531 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
534 qcint_t *globals = prog->globals + prev->firstlocal;
535 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
536 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
537 vec_shrinkto(prog->localstack, oldsp);
540 vec_pop(prog->stack);
542 return st.stmt - 1; /* offset the ++st */
545 bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
547 size_t oldxflags = prog->xflags;
548 prog_section_statement_t *st;
551 prog->xflags = flags;
553 st = prog->code + prog_enterfunction(prog, func);
561 #define QCVM_PROFILE 0
567 #define QCVM_PROFILE 0
573 #define QCVM_PROFILE 1
577 case (VMXF_TRACE|VMXF_PROFILE):
579 #define QCVM_PROFILE 1
586 prog->xflags = oldxflags;
587 vec_free(prog->localstack);
588 vec_free(prog->stack);
594 /***********************************************************************
595 * main for when building the standalone executor
600 const char *type_name[TYPE_COUNT] = {
626 static qcvm_parameter *main_params = NULL;
628 #define CheckArgs(num) do { \
629 if (prog->argc != (num)) { \
631 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
632 __FUNCTION__, prog->argc, (num)); \
637 #define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
638 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
639 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
641 static int qc_print(qc_program_t *prog) {
643 const char *laststr = NULL;
644 for (i = 0; i < (size_t)prog->argc; ++i) {
645 qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
646 laststr = prog_getstring(prog, str->string);
647 printf("%s", laststr);
649 if (laststr && (prog->xflags & VMXF_TRACE)) {
650 size_t len = strlen(laststr);
651 if (!len || laststr[len-1] != '\n')
657 static int qc_error(qc_program_t *prog) {
658 fprintf(stderr, "*** VM raised an error:\n");
664 static int qc_ftos(qc_program_t *prog) {
670 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
671 str.string = prog_tempstring(prog, buffer);
676 static int qc_stof(qc_program_t *prog) {
681 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
686 static int qc_vtos(qc_program_t *prog) {
692 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
693 str.string = prog_tempstring(prog, buffer);
698 static int qc_etos(qc_program_t *prog) {
704 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
705 str.string = prog_tempstring(prog, buffer);
710 static int qc_spawn(qc_program_t *prog) {
713 ent.edict = prog_spawn_entity(prog);
715 return (ent.edict ? 0 : -1);
718 static int qc_kill(qc_program_t *prog) {
722 prog_free_entity(prog, ent->edict);
726 static int qc_sqrt(qc_program_t *prog) {
730 out._float = sqrt(num->_float);
735 static int qc_vlen(qc_program_t *prog) {
739 len._float = sqrt(vec->vector[0] * vec->vector[0] +
740 vec->vector[1] * vec->vector[1] +
741 vec->vector[2] * vec->vector[2]);
746 static int qc_normalize(qc_program_t *prog) {
752 len = sqrt(vec->vector[0] * vec->vector[0] +
753 vec->vector[1] * vec->vector[1] +
754 vec->vector[2] * vec->vector[2]);
759 out.vector[0] = len * vec->vector[0];
760 out.vector[1] = len * vec->vector[1];
761 out.vector[2] = len * vec->vector[2];
766 static int qc_strcat(qc_program_t *prog) {
769 qcany_t *str1, *str2;
778 cstr1 = prog_getstring(prog, str1->string);
779 cstr2 = prog_getstring(prog, str2->string);
780 len1 = strlen(cstr1);
781 len2 = strlen(cstr2);
782 buffer = (char*)mem_a(len1 + len2 + 1);
783 memcpy(buffer, cstr1, len1);
784 memcpy(buffer+len1, cstr2, len2+1);
785 out.string = prog_tempstring(prog, buffer);
791 static int qc_strcmp(qc_program_t *prog) {
792 qcany_t *str1, *str2;
798 if (prog->argc != 2 && prog->argc != 3) {
799 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
806 cstr1 = prog_getstring(prog, str1->string);
807 cstr2 = prog_getstring(prog, str2->string);
809 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
811 out._float = strcmp(cstr1, cstr2);
816 static int qc_floor(qc_program_t *prog) {
820 out._float = floor(num->_float);
825 static prog_builtin_t qc_builtins[] = {
838 &qc_normalize, /* 12 */
843 static const char *arg0 = NULL;
845 static void version(void) {
846 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
855 static void usage(void) {
856 printf("usage: %s [options] [parameters] file\n", arg0);
857 printf("options:\n");
858 printf(" -h, --help print this message\n"
859 " -trace trace the execution\n"
860 " -profile perform profiling during execution\n"
861 " -info print information from the prog's header\n"
862 " -disasm disassemble and exit\n"
863 " -disasm-func func disassemble and exit\n"
864 " -printdefs list the defs section\n"
865 " -printfields list the field section\n"
866 " -printfuns list functions information\n"
868 " -vv be even more verbose\n");
869 printf("parameters:\n");
870 printf(" -vector <V> pass a vector parameter to main()\n"
871 " -float <f> pass a float parameter to main()\n"
872 " -string <s> pass a string parameter to main() \n");
875 static void prog_main_setparams(qc_program_t *prog) {
879 for (i = 0; i < vec_size(main_params); ++i) {
880 arg = GetGlobal(OFS_PARM0 + 3*i);
884 switch (main_params[i].vtype) {
886 (void)util_sscanf(main_params[i].value, " %f %f %f ",
892 arg->_float = atof(main_params[i].value);
895 arg->string = prog_tempstring(prog, main_params[i].value);
898 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
904 void prog_disasm_function(qc_program_t *prog, size_t id);
906 int main(int argc, char **argv) {
910 size_t xflags = VMXF_DEFAULT;
911 bool opts_printfields = false;
912 bool opts_printdefs = false;
913 bool opts_printfuns = false;
914 bool opts_disasm = false;
915 bool opts_info = false;
917 const char *progsfile = NULL;
918 const char **dis_list = NULL;
929 if (!strcmp(argv[1], "-h") ||
930 !strcmp(argv[1], "-help") ||
931 !strcmp(argv[1], "--help"))
936 else if (!strcmp(argv[1], "-v")) {
941 else if (!strncmp(argv[1], "-vv", 3)) {
942 const char *av = argv[1]+1;
954 else if (!strcmp(argv[1], "-version") ||
955 !strcmp(argv[1], "--version"))
960 else if (!strcmp(argv[1], "-trace")) {
963 xflags |= VMXF_TRACE;
965 else if (!strcmp(argv[1], "-profile")) {
968 xflags |= VMXF_PROFILE;
970 else if (!strcmp(argv[1], "-info")) {
976 else if (!strcmp(argv[1], "-disasm")) {
982 else if (!strcmp(argv[1], "-disasm-func")) {
989 vec_push(dis_list, argv[1]);
994 else if (!strcmp(argv[1], "-printdefs")) {
997 opts_printdefs = true;
1000 else if (!strcmp(argv[1], "-printfuns")) {
1003 opts_printfuns = true;
1006 else if (!strcmp(argv[1], "-printfields")) {
1009 opts_printfields = true;
1012 else if (!strcmp(argv[1], "-vector") ||
1013 !strcmp(argv[1], "-string") ||
1014 !strcmp(argv[1], "-float") )
1017 if (argv[1][1] == 'f')
1018 p.vtype = TYPE_FLOAT;
1019 else if (argv[1][1] == 's')
1020 p.vtype = TYPE_STRING;
1021 else if (argv[1][1] == 'v')
1022 p.vtype = TYPE_VECTOR;
1024 p.vtype = TYPE_VOID;
1034 vec_push(main_params, p);
1038 else if (!strcmp(argv[1], "--")) {
1043 else if (argv[1][0] != '-') {
1045 fprintf(stderr, "only 1 program file may be specified\n");
1049 progsfile = argv[1];
1055 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1061 if (argc == 2 && !progsfile) {
1062 progsfile = argv[1];
1068 fprintf(stderr, "must specify a program to execute\n");
1073 prog = prog_load(progsfile, noexec);
1075 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1079 prog->builtins = qc_builtins;
1080 prog->builtins_count = GMQCC_ARRAY_COUNT(qc_builtins);
1083 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1084 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1085 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1092 (unsigned long)vec_size(prog->code),
1093 (unsigned long)vec_size(prog->defs),
1094 (unsigned long)vec_size(prog->fields),
1095 (unsigned long)vec_size(prog->functions),
1096 (unsigned long)vec_size(prog->strings));
1103 for (i = 0; i < vec_size(dis_list); ++i) {
1105 printf("Looking for `%s`\n", dis_list[i]);
1106 for (k = 1; k < vec_size(prog->functions); ++k) {
1107 const char *name = prog_getstring(prog, prog->functions[k].name);
1108 if (!strcmp(name, dis_list[i])) {
1109 prog_disasm_function(prog, k);
1115 for (i = 1; i < vec_size(prog->functions); ++i)
1116 prog_disasm_function(prog, i);
1119 if (opts_printdefs) {
1120 const char *getstring = NULL;
1121 for (i = 0; i < vec_size(prog->defs); ++i) {
1122 printf("Global: %8s %-16s at %u%s",
1123 type_name[prog->defs[i].type & DEF_TYPEMASK],
1124 prog_getstring(prog, prog->defs[i].name),
1125 (unsigned int)prog->defs[i].offset,
1126 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1128 switch (prog->defs[i].type & DEF_TYPEMASK) {
1130 printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
1133 printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1139 printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1142 getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
1143 printf(" [init: `");
1144 print_escaped_string(getstring, strlen(getstring));
1154 if (opts_printfields) {
1155 for (i = 0; i < vec_size(prog->fields); ++i) {
1156 printf("Field: %8s %-16s at %u%s\n",
1157 type_name[prog->fields[i].type],
1158 prog_getstring(prog, prog->fields[i].name),
1159 (unsigned int)prog->fields[i].offset,
1160 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1163 if (opts_printfuns) {
1164 for (i = 0; i < vec_size(prog->functions); ++i) {
1166 printf("Function: %-16s taking %u parameters:(",
1167 prog_getstring(prog, prog->functions[i].name),
1168 (unsigned int)prog->functions[i].nargs);
1169 for (a = 0; a < prog->functions[i].nargs; ++a) {
1170 printf(" %i", prog->functions[i].argsize[a]);
1173 int32_t start = prog->functions[i].entry;
1175 printf(") builtin %i\n", (int)-start);
1178 prog_section_statement_t *st = prog->code + start;
1179 for (;st->opcode != INSTR_DONE; ++st)
1181 printf(") - %lu instructions", (unsigned long)funsize);
1183 printf(" - locals: %i + %i\n",
1184 prog->functions[i].firstlocal,
1185 prog->functions[i].locals);
1192 printf(") locals: %i + %i\n",
1193 prog->functions[i].firstlocal,
1194 prog->functions[i].locals);
1201 for (i = 1; i < vec_size(prog->functions); ++i) {
1202 const char *name = prog_getstring(prog, prog->functions[i].name);
1203 if (!strcmp(name, "main"))
1204 fnmain = (qcint_t)i;
1208 prog_main_setparams(prog);
1209 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1212 fprintf(stderr, "No main function found\n");
1219 void prog_disasm_function(qc_program_t *prog, size_t id) {
1220 prog_section_function_t *fdef = prog->functions + id;
1221 prog_section_statement_t *st;
1223 if (fdef->entry < 0) {
1224 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1228 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1230 st = prog->code + fdef->entry;
1231 while (st->opcode != INSTR_DONE) {
1232 prog_print_statement(prog, st);
1236 #else /* !QCVM_LOOP */
1238 * Everything from here on is not including into the compilation of the
1239 * executor. This is simply code that is #included via #include __FILE__
1240 * see when QCVM_LOOP is defined, the rest of the code above do not get
1241 * re-included. So this really just acts like one large macro, but it
1242 * sort of isn't, which makes it nicer looking.
1245 #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
1246 #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
1247 #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
1249 #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
1251 /* to be consistent with current darkplaces behaviour */
1252 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1253 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1256 while (prog->vmerror == 0) {
1257 prog_section_function_t *newf;
1264 prog->profile[st - prog->code]++;
1268 prog_print_statement(prog, st);
1274 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1279 /* TODO: add instruction count to function profile count */
1280 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1281 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1282 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1284 st = prog->code + prog_leavefunction(prog);
1285 if (!vec_size(prog->stack))
1291 OPC->_float = OPA->_float * OPB->_float;
1294 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1295 OPA->vector[1]*OPB->vector[1] +
1296 OPA->vector[2]*OPB->vector[2];
1300 qcfloat_t f = OPA->_float;
1301 OPC->vector[0] = f * OPB->vector[0];
1302 OPC->vector[1] = f * OPB->vector[1];
1303 OPC->vector[2] = f * OPB->vector[2];
1308 qcfloat_t f = OPB->_float;
1309 OPC->vector[0] = f * OPA->vector[0];
1310 OPC->vector[1] = f * OPA->vector[1];
1311 OPC->vector[2] = f * OPA->vector[2];
1315 if (OPB->_float != 0.0f)
1316 OPC->_float = OPA->_float / OPB->_float;
1322 OPC->_float = OPA->_float + OPB->_float;
1325 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1326 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1327 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1330 OPC->_float = OPA->_float - OPB->_float;
1333 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1334 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1335 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1339 OPC->_float = (OPA->_float == OPB->_float);
1342 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1343 (OPA->vector[1] == OPB->vector[1]) &&
1344 (OPA->vector[2] == OPB->vector[2]) );
1347 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1348 prog_getstring(prog, OPB->string));
1351 OPC->_float = (OPA->_int == OPB->_int);
1354 OPC->_float = (OPA->function == OPB->function);
1357 OPC->_float = (OPA->_float != OPB->_float);
1360 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1361 (OPA->vector[1] != OPB->vector[1]) ||
1362 (OPA->vector[2] != OPB->vector[2]) );
1365 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1366 prog_getstring(prog, OPB->string));
1369 OPC->_float = (OPA->_int != OPB->_int);
1372 OPC->_float = (OPA->function != OPB->function);
1376 OPC->_float = (OPA->_float <= OPB->_float);
1379 OPC->_float = (OPA->_float >= OPB->_float);
1382 OPC->_float = (OPA->_float < OPB->_float);
1385 OPC->_float = (OPA->_float > OPB->_float);
1390 case INSTR_LOAD_FLD:
1391 case INSTR_LOAD_ENT:
1392 case INSTR_LOAD_FNC:
1393 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1394 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1397 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1398 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1403 ed = prog_getedict(prog, OPA->edict);
1404 OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
1407 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1408 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1411 if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
1413 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1418 ed = prog_getedict(prog, OPA->edict);
1419 ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
1420 OPC->ivector[0] = ptr->ivector[0];
1421 OPC->ivector[1] = ptr->ivector[1];
1422 OPC->ivector[2] = ptr->ivector[2];
1426 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1427 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1430 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1432 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1438 ed = prog_getedict(prog, OPA->edict);
1439 OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
1444 case INSTR_STORE_ENT:
1445 case INSTR_STORE_FLD:
1446 case INSTR_STORE_FNC:
1447 OPB->_int = OPA->_int;
1450 OPB->ivector[0] = OPA->ivector[0];
1451 OPB->ivector[1] = OPA->ivector[1];
1452 OPB->ivector[2] = OPA->ivector[2];
1455 case INSTR_STOREP_F:
1456 case INSTR_STOREP_S:
1457 case INSTR_STOREP_ENT:
1458 case INSTR_STOREP_FLD:
1459 case INSTR_STOREP_FNC:
1460 if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
1461 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1464 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1465 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1467 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1469 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1470 ptr->_int = OPA->_int;
1472 case INSTR_STOREP_V:
1473 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
1474 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1477 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1478 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1480 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1482 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1483 ptr->ivector[0] = OPA->ivector[0];
1484 ptr->ivector[1] = OPA->ivector[1];
1485 ptr->ivector[2] = OPA->ivector[2];
1489 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1492 OPC->_float = !OPA->vector[0] &&
1497 OPC->_float = !OPA->string ||
1498 !*prog_getstring(prog, OPA->string);
1501 OPC->_float = (OPA->edict == 0);
1504 OPC->_float = !OPA->function;
1508 /* this is consistent with darkplaces' behaviour */
1509 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1511 st += st->o2.s1 - 1; /* offset the s++ */
1512 if (++jumpcount >= maxjumps)
1513 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1517 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1519 st += st->o2.s1 - 1; /* offset the s++ */
1520 if (++jumpcount >= maxjumps)
1521 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1534 prog->argc = st->opcode - INSTR_CALL0;
1536 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1538 if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
1540 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1544 newf = &prog->functions[OPA->function];
1547 prog->statement = (st - prog->code) + 1;
1549 if (newf->entry < 0)
1551 /* negative statements are built in functions */
1552 qcint_t builtinnumber = -newf->entry;
1553 if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
1554 prog->builtins[builtinnumber](prog);
1556 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1557 builtinnumber, prog->filename);
1560 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1566 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1570 st += st->o1.s1 - 1; /* offset the s++ */
1571 if (++jumpcount == 10000000)
1572 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1576 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1577 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1580 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1581 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1585 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1588 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1595 #endif /* !QCVM_LOOP */