2 * Copyright (C) 2012, 2013
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
33 static void loaderror(const char *fmt, ...)
40 printf(": %s\n", util_strerror(err));
43 static void qcvmerror(qc_program *prog, const char *fmt, ...)
55 qc_program* prog_load(const char *filename, bool skipversion)
59 FILE *file = fs_file_open(filename, "rb");
64 if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
65 loaderror("failed to read header from '%s'", filename);
70 if (!skipversion && header.version != 6) {
71 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
76 prog = (qc_program*)mem_a(sizeof(qc_program));
79 fprintf(stderr, "failed to allocate program data\n");
82 memset(prog, 0, sizeof(*prog));
84 prog->entityfields = header.entfield;
85 prog->crc16 = header.crc16;
87 prog->filename = util_strdup(filename);
88 if (!prog->filename) {
89 loaderror("failed to store program name");
93 #define read_data(hdrvar, progvar, reserved) \
94 if (fs_file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
95 loaderror("seek failed"); \
99 vec_add(prog->progvar, header.hdrvar.length + reserved), \
100 sizeof(*prog->progvar), \
101 header.hdrvar.length, \
103 )!= header.hdrvar.length \
105 loaderror("read failed"); \
108 #define read_data1(x) read_data(x, x, 0)
109 #define read_data2(x, y) read_data(x, x, y)
111 read_data (statements, code, 0);
114 read_data1(functions);
116 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
120 /* profile counters */
121 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
123 /* Add tempstring area */
124 prog->tempstring_start = vec_size(prog->strings);
125 prog->tempstring_at = vec_size(prog->strings);
126 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
128 /* spawn the world entity */
129 vec_push(prog->entitypool, true);
130 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
137 mem_d(prog->filename);
138 vec_free(prog->code);
139 vec_free(prog->defs);
140 vec_free(prog->fields);
141 vec_free(prog->functions);
142 vec_free(prog->strings);
143 vec_free(prog->globals);
144 vec_free(prog->entitydata);
145 vec_free(prog->entitypool);
150 void prog_delete(qc_program *prog)
152 if (prog->filename) mem_d(prog->filename);
153 vec_free(prog->code);
154 vec_free(prog->defs);
155 vec_free(prog->fields);
156 vec_free(prog->functions);
157 vec_free(prog->strings);
158 vec_free(prog->globals);
159 vec_free(prog->entitydata);
160 vec_free(prog->entitypool);
161 vec_free(prog->localstack);
162 vec_free(prog->stack);
163 vec_free(prog->profile);
167 /***********************************************************************
171 const char* prog_getstring(qc_program *prog, qcint str) {
172 /* cast for return required for C++ */
173 if (str < 0 || str >= (qcint)vec_size(prog->strings))
174 return "<<<invalid string>>>";
176 return prog->strings + str;
179 prog_section_def* prog_entfield(qc_program *prog, qcint off) {
181 for (i = 0; i < vec_size(prog->fields); ++i) {
182 if (prog->fields[i].offset == off)
183 return (prog->fields + i);
188 prog_section_def* prog_getdef(qc_program *prog, qcint off)
191 for (i = 0; i < vec_size(prog->defs); ++i) {
192 if (prog->defs[i].offset == off)
193 return (prog->defs + i);
198 qcany* prog_getedict(qc_program *prog, qcint e) {
199 if (e >= (qcint)vec_size(prog->entitypool)) {
201 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
204 return (qcany*)(prog->entitydata + (prog->entityfields * e));
207 qcint prog_spawn_entity(qc_program *prog) {
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));
217 vec_push(prog->entitypool, true);
219 data = (char*)vec_add(prog->entitydata, prog->entityfields);
220 memset(data, 0, prog->entityfields * sizeof(qcint));
224 void prog_free_entity(qc_program *prog, qcint e) {
227 fprintf(stderr, "Trying to free world entity\n");
230 if (e >= (qcint)vec_size(prog->entitypool)) {
232 fprintf(stderr, "Trying to free out of bounds entity\n");
235 if (!prog->entitypool[e]) {
237 fprintf(stderr, "Double free on entity\n");
240 prog->entitypool[e] = false;
243 qcint prog_tempstring(qc_program *prog, const char *str) {
244 size_t len = strlen(str);
245 size_t at = prog->tempstring_at;
247 /* when we reach the end we start over */
248 if (at + len >= vec_size(prog->strings))
249 at = prog->tempstring_start;
251 /* when it doesn't fit, reallocate */
252 if (at + len >= vec_size(prog->strings))
254 (void)vec_add(prog->strings, len+1);
255 memcpy(prog->strings + at, str, len+1);
259 /* when it fits, just copy */
260 memcpy(prog->strings + at, str, len+1);
261 prog->tempstring_at += len+1;
265 static size_t print_escaped_string(const char *str, size_t maxlen) {
268 --maxlen; /* because we're lazy and have escape sequences */
278 case '\a': len += 2; putchar('\\'); putchar('a'); break;
279 case '\b': len += 2; putchar('\\'); putchar('b'); break;
280 case '\r': len += 2; putchar('\\'); putchar('r'); break;
281 case '\n': len += 2; putchar('\\'); putchar('n'); break;
282 case '\t': len += 2; putchar('\\'); putchar('t'); break;
283 case '\f': len += 2; putchar('\\'); putchar('f'); break;
284 case '\v': len += 2; putchar('\\'); putchar('v'); break;
285 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
286 case '"': len += 2; putchar('\\'); putchar('"'); break;
298 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) {
299 static char spaces[28+1] = " ";
300 prog_section_def *def;
305 len = printf("<null>,");
309 def = prog_getdef(prog, glob);
310 value = (qcany*)(&prog->globals[glob]);
312 len = printf("[@%u] ", glob);
314 const char *name = prog_getstring(prog, def->name);
318 len += printf("%s ", name);
319 vtype = def->type & DEF_TYPEMASK;
328 len += printf("(%i),", value->_int);
331 len += printf("'%g %g %g',", value->vector[0],
337 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
339 len += printf("(null)");
341 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
345 len += printf("%g,", value->_float);
349 if (len < (int)sizeof(spaces)-1) {
350 spaces[sizeof(spaces)-1-len] = 0;
351 fs_file_puts(stdout, spaces);
352 spaces[sizeof(spaces)-1-len] = ' ';
356 static void prog_print_statement(qc_program *prog, prog_section_statement *st) {
357 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
358 printf("<illegal instruction %d>\n", st->opcode);
361 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
363 for (i = 0; i < vec_size(prog->function_stack); ++i)
365 printf("%s:", vec_last(prog->function_stack));
367 printf(" <> %-12s", asm_instr[st->opcode].m);
368 if (st->opcode >= INSTR_IF &&
369 st->opcode <= INSTR_IFNOT)
371 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
372 printf("%d\n", st->o2.s1);
374 else if (st->opcode >= INSTR_CALL0 &&
375 st->opcode <= INSTR_CALL8)
377 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
380 else if (st->opcode == INSTR_GOTO)
382 printf("%i\n", st->o1.s1);
386 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
390 t[1] = t[2] = TYPE_VECTOR;
393 t[0] = t[2] = TYPE_VECTOR;
396 t[0] = t[1] = TYPE_VECTOR;
402 t[0] = t[1] = t[2] = TYPE_VECTOR;
406 t[0] = t[1] = TYPE_STRING;
413 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
416 t[0] = t[1] = TYPE_STRING; t[2] = -1;
418 case INSTR_STORE_ENT:
419 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
421 case INSTR_STORE_FLD:
422 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
424 case INSTR_STORE_FNC:
425 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
428 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
431 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
433 case INSTR_STOREP_ENT:
434 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
436 case INSTR_STOREP_FLD:
437 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
439 case INSTR_STOREP_FNC:
440 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
443 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
444 else printf("(none), ");
445 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
446 else printf("(none), ");
447 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
448 else printf("(none)");
453 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) {
459 st.localsp = vec_size(prog->localstack);
460 st.stmt = prog->statement;
463 if (prog->xflags & VMXF_TRACE) {
464 const char *str = prog_getstring(prog, func->name);
465 vec_push(prog->function_stack, str);
468 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
469 if (vec_size(prog->stack))
471 prog_section_function *cur;
472 cur = prog->stack[vec_size(prog->stack)-1].function;
475 qcint *globals = prog->globals + cur->firstlocal;
476 vec_append(prog->localstack, cur->locals, globals);
481 qcint *globals = prog->globals + func->firstlocal;
482 vec_append(prog->localstack, func->locals, globals);
486 /* copy parameters */
487 parampos = func->firstlocal;
488 for (p = 0; p < func->nargs; ++p)
491 for (s = 0; s < func->argsize[p]; ++s) {
492 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
497 vec_push(prog->stack, st);
502 static qcint prog_leavefunction(qc_program *prog) {
503 prog_section_function *prev = NULL;
506 qc_exec_stack st = vec_last(prog->stack);
508 if (prog->xflags & VMXF_TRACE) {
509 if (vec_size(prog->function_stack))
510 vec_pop(prog->function_stack);
513 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
514 if (vec_size(prog->stack) > 1) {
515 prev = prog->stack[vec_size(prog->stack)-2].function;
516 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
519 prev = prog->stack[vec_size(prog->stack)-1].function;
520 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
523 qcint *globals = prog->globals + prev->firstlocal;
524 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
525 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
526 vec_shrinkto(prog->localstack, oldsp);
529 vec_pop(prog->stack);
531 return st.stmt - 1; /* offset the ++st */
534 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps) {
536 size_t oldxflags = prog->xflags;
537 prog_section_statement *st;
540 prog->xflags = flags;
542 st = prog->code + prog_enterfunction(prog, func);
550 #define QCVM_PROFILE 0
556 #define QCVM_PROFILE 0
562 #define QCVM_PROFILE 1
566 case (VMXF_TRACE|VMXF_PROFILE):
568 #define QCVM_PROFILE 1
575 prog->xflags = oldxflags;
576 vec_free(prog->localstack);
577 vec_free(prog->stack);
583 /***********************************************************************
584 * main for when building the standalone executor
587 #if defined(QCVM_EXECUTOR)
592 const char *type_name[TYPE_COUNT] = {
618 static qcvm_parameter *main_params = NULL;
620 #define CheckArgs(num) do { \
621 if (prog->argc != (num)) { \
623 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
624 __FUNCTION__, prog->argc, (num)); \
629 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
630 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
631 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
633 static int qc_print(qc_program *prog) {
635 const char *laststr = NULL;
636 for (i = 0; i < (size_t)prog->argc; ++i) {
637 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
638 laststr = prog_getstring(prog, str->string);
639 printf("%s", laststr);
641 if (laststr && (prog->xflags & VMXF_TRACE)) {
642 size_t len = strlen(laststr);
643 if (!len || laststr[len-1] != '\n')
649 static int qc_error(qc_program *prog) {
650 fprintf(stderr, "*** VM raised an error:\n");
656 static int qc_ftos(qc_program *prog) {
662 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
663 str.string = prog_tempstring(prog, buffer);
668 static int qc_stof(qc_program *prog) {
673 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
678 static int qc_vtos(qc_program *prog) {
684 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
685 str.string = prog_tempstring(prog, buffer);
690 static int qc_etos(qc_program *prog) {
696 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
697 str.string = prog_tempstring(prog, buffer);
702 static int qc_spawn(qc_program *prog) {
705 ent.edict = prog_spawn_entity(prog);
707 return (ent.edict ? 0 : -1);
710 static int qc_kill(qc_program *prog) {
714 prog_free_entity(prog, ent->edict);
718 static int qc_sqrt(qc_program *prog) {
722 out._float = sqrt(num->_float);
727 static int qc_vlen(qc_program *prog) {
731 len._float = sqrt(vec->vector[0] * vec->vector[0] +
732 vec->vector[1] * vec->vector[1] +
733 vec->vector[2] * vec->vector[2]);
738 static int qc_normalize(qc_program *prog) {
744 len = sqrt(vec->vector[0] * vec->vector[0] +
745 vec->vector[1] * vec->vector[1] +
746 vec->vector[2] * vec->vector[2]);
751 out.vector[0] = len * vec->vector[0];
752 out.vector[1] = len * vec->vector[1];
753 out.vector[2] = len * vec->vector[2];
758 static int qc_strcat(qc_program *prog) {
770 cstr1 = prog_getstring(prog, str1->string);
771 cstr2 = prog_getstring(prog, str2->string);
772 len1 = strlen(cstr1);
773 len2 = strlen(cstr2);
774 buffer = (char*)mem_a(len1 + len2 + 1);
775 memcpy(buffer, cstr1, len1);
776 memcpy(buffer+len1, cstr2, len2+1);
777 out.string = prog_tempstring(prog, buffer);
783 static int qc_strcmp(qc_program *prog) {
790 if (prog->argc != 2 && prog->argc != 3) {
791 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
798 cstr1 = prog_getstring(prog, str1->string);
799 cstr2 = prog_getstring(prog, str2->string);
801 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
803 out._float = strcmp(cstr1, cstr2);
808 static int qc_floor(qc_program *prog) {
812 out._float = floor(num->_float);
817 static prog_builtin qc_builtins[] = {
830 &qc_normalize, /* 12 */
834 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
836 static const char *arg0 = NULL;
838 static void version(void) {
839 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
848 static void usage(void) {
849 printf("usage: %s [options] [parameters] file\n", arg0);
850 printf("options:\n");
851 printf(" -h, --help print this message\n"
852 " -trace trace the execution\n"
853 " -profile perform profiling during execution\n"
854 " -info print information from the prog's header\n"
855 " -disasm disassemble and exit\n"
856 " -disasm-func func disassemble and exit\n"
857 " -printdefs list the defs section\n"
858 " -printfields list the field section\n"
859 " -printfuns list functions information\n"
861 " -vv be even more verbose\n");
862 printf("parameters:\n");
863 printf(" -vector <V> pass a vector parameter to main()\n"
864 " -float <f> pass a float parameter to main()\n"
865 " -string <s> pass a string parameter to main() \n");
868 static void prog_main_setparams(qc_program *prog) {
872 for (i = 0; i < vec_size(main_params); ++i) {
873 arg = GetGlobal(OFS_PARM0 + 3*i);
877 switch (main_params[i].vtype) {
880 (void)sscanf_s(main_params[i].value, " %f %f %f ",
885 (void)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 escapestring(char* dest, const char* src) {
906 while ((c = *(src++))) {
909 *(dest++) = '\\', *(dest++) = 't';
912 *(dest++) = '\\', *(dest++) = 'n';
915 *(dest++) = '\\', *(dest++) = 'r';
918 *(dest++) = '\\', *(dest++) = '\\';
921 *(dest++) = '\\', *(dest++) = '\"';
930 void prog_disasm_function(qc_program *prog, size_t id);
932 int main(int argc, char **argv) {
936 size_t xflags = VMXF_DEFAULT;
937 bool opts_printfields = false;
938 bool opts_printdefs = false;
939 bool opts_printfuns = false;
940 bool opts_disasm = false;
941 bool opts_info = false;
943 const char *progsfile = NULL;
944 const char **dis_list = NULL;
955 if (!strcmp(argv[1], "-h") ||
956 !strcmp(argv[1], "-help") ||
957 !strcmp(argv[1], "--help"))
962 else if (!strcmp(argv[1], "-v")) {
967 else if (!strncmp(argv[1], "-vv", 3)) {
968 const char *av = argv[1]+1;
980 else if (!strcmp(argv[1], "-version") ||
981 !strcmp(argv[1], "--version"))
986 else if (!strcmp(argv[1], "-trace")) {
989 xflags |= VMXF_TRACE;
991 else if (!strcmp(argv[1], "-profile")) {
994 xflags |= VMXF_PROFILE;
996 else if (!strcmp(argv[1], "-info")) {
1002 else if (!strcmp(argv[1], "-disasm")) {
1008 else if (!strcmp(argv[1], "-disasm-func")) {
1015 vec_push(dis_list, argv[1]);
1020 else if (!strcmp(argv[1], "-printdefs")) {
1023 opts_printdefs = true;
1026 else if (!strcmp(argv[1], "-printfuns")) {
1029 opts_printfuns = true;
1032 else if (!strcmp(argv[1], "-printfields")) {
1035 opts_printfields = true;
1038 else if (!strcmp(argv[1], "-vector") ||
1039 !strcmp(argv[1], "-string") ||
1040 !strcmp(argv[1], "-float") )
1043 if (argv[1][1] == 'f')
1044 p.vtype = TYPE_FLOAT;
1045 else if (argv[1][1] == 's')
1046 p.vtype = TYPE_STRING;
1047 else if (argv[1][1] == 'v')
1048 p.vtype = TYPE_VECTOR;
1058 vec_push(main_params, p);
1062 else if (!strcmp(argv[1], "--")) {
1067 else if (argv[1][0] != '-') {
1069 fprintf(stderr, "only 1 program file may be specified\n");
1073 progsfile = argv[1];
1079 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1085 if (argc == 2 && !progsfile) {
1086 progsfile = argv[1];
1092 fprintf(stderr, "must specify a program to execute\n");
1097 prog = prog_load(progsfile, noexec);
1099 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1103 prog->builtins = qc_builtins;
1104 prog->builtins_count = qc_builtins_count;
1107 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1108 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1109 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1116 (unsigned long)vec_size(prog->code),
1117 (unsigned long)vec_size(prog->defs),
1118 (unsigned long)vec_size(prog->fields),
1119 (unsigned long)vec_size(prog->functions),
1120 (unsigned long)vec_size(prog->strings));
1127 for (i = 0; i < vec_size(dis_list); ++i) {
1129 printf("Looking for `%s`\n", dis_list[i]);
1130 for (k = 1; k < vec_size(prog->functions); ++k) {
1131 const char *name = prog_getstring(prog, prog->functions[k].name);
1132 if (!strcmp(name, dis_list[i])) {
1133 prog_disasm_function(prog, k);
1139 for (i = 1; i < vec_size(prog->functions); ++i)
1140 prog_disasm_function(prog, i);
1143 if (opts_printdefs) {
1144 char *escape = NULL;
1145 const char *getstring = NULL;
1147 for (i = 0; i < vec_size(prog->defs); ++i) {
1148 printf("Global: %8s %-16s at %u%s",
1149 type_name[prog->defs[i].type & DEF_TYPEMASK],
1150 prog_getstring(prog, prog->defs[i].name),
1151 (unsigned int)prog->defs[i].offset,
1152 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1154 switch (prog->defs[i].type & DEF_TYPEMASK) {
1156 printf(" [init: %g]", ((qcany*)(prog->globals + prog->defs[i].offset))->_float);
1159 printf(" [init: %i]", (int)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1165 printf(" [init: %u]", (unsigned)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1168 getstring = prog_getstring(prog, ((qcany*)(prog->globals + prog->defs[i].offset))->string);
1169 escape = (char*)mem_a(strlen(getstring) * 2 + 1); /* will be enough */
1170 escapestring(escape, getstring);
1171 printf(" [init: `%s`]", escape);
1173 mem_d(escape); /* free */
1182 if (opts_printfields) {
1183 for (i = 0; i < vec_size(prog->fields); ++i) {
1184 printf("Field: %8s %-16s at %u%s\n",
1185 type_name[prog->fields[i].type],
1186 prog_getstring(prog, prog->fields[i].name),
1187 (unsigned int)prog->fields[i].offset,
1188 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1191 if (opts_printfuns) {
1192 for (i = 0; i < vec_size(prog->functions); ++i) {
1194 printf("Function: %-16s taking %i parameters:(",
1195 prog_getstring(prog, prog->functions[i].name),
1196 (unsigned int)prog->functions[i].nargs);
1197 for (a = 0; a < prog->functions[i].nargs; ++a) {
1198 printf(" %i", prog->functions[i].argsize[a]);
1201 int32_t start = prog->functions[i].entry;
1203 printf(") builtin %i\n", (int)-start);
1206 prog_section_statement *st = prog->code + start;
1207 for (;st->opcode != INSTR_DONE; ++st)
1209 printf(") - %lu instructions", (unsigned long)funsize);
1211 printf(" - locals: %i + %i\n",
1212 prog->functions[i].firstlocal,
1213 prog->functions[i].locals);
1220 printf(") locals: %i + %i\n",
1221 prog->functions[i].firstlocal,
1222 prog->functions[i].locals);
1229 for (i = 1; i < vec_size(prog->functions); ++i) {
1230 const char *name = prog_getstring(prog, prog->functions[i].name);
1231 if (!strcmp(name, "main"))
1236 prog_main_setparams(prog);
1237 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1240 fprintf(stderr, "No main function found\n");
1247 void prog_disasm_function(qc_program *prog, size_t id) {
1248 prog_section_function *fdef = prog->functions + id;
1249 prog_section_statement *st;
1251 if (fdef->entry < 0) {
1252 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1256 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1258 st = prog->code + fdef->entry;
1259 while (st->opcode != INSTR_DONE) {
1260 prog_print_statement(prog, st);
1265 #else /* !QCVM_LOOP */
1267 * Everything from here on is not including into the compilation of the
1268 * executor. This is simply code that is #included via #include __FILE__
1269 * see when QCVM_LOOP is defined, the rest of the code above do not get
1270 * re-included. So this really just acts like one large macro, but it
1271 * sort of isn't, which makes it nicer looking.
1274 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1275 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1276 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1278 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1280 /* to be consistent with current darkplaces behaviour */
1281 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1282 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1286 prog_section_function *newf;
1293 prog->profile[st - prog->code]++;
1297 prog_print_statement(prog, st);
1303 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1308 /* TODO: add instruction count to function profile count */
1309 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1310 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1311 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1313 st = prog->code + prog_leavefunction(prog);
1314 if (!vec_size(prog->stack))
1320 OPC->_float = OPA->_float * OPB->_float;
1323 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1324 OPA->vector[1]*OPB->vector[1] +
1325 OPA->vector[2]*OPB->vector[2];
1329 qcfloat f = OPA->_float;
1330 OPC->vector[0] = f * OPB->vector[0];
1331 OPC->vector[1] = f * OPB->vector[1];
1332 OPC->vector[2] = f * OPB->vector[2];
1337 qcfloat f = OPB->_float;
1338 OPC->vector[0] = f * OPA->vector[0];
1339 OPC->vector[1] = f * OPA->vector[1];
1340 OPC->vector[2] = f * OPA->vector[2];
1344 if (OPB->_float != 0.0f)
1345 OPC->_float = OPA->_float / OPB->_float;
1351 OPC->_float = OPA->_float + OPB->_float;
1354 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1355 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1356 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1359 OPC->_float = OPA->_float - OPB->_float;
1362 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1363 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1364 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1368 OPC->_float = (OPA->_float == OPB->_float);
1371 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1372 (OPA->vector[1] == OPB->vector[1]) &&
1373 (OPA->vector[2] == OPB->vector[2]) );
1376 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1377 prog_getstring(prog, OPB->string));
1380 OPC->_float = (OPA->_int == OPB->_int);
1383 OPC->_float = (OPA->function == OPB->function);
1386 OPC->_float = (OPA->_float != OPB->_float);
1389 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1390 (OPA->vector[1] != OPB->vector[1]) ||
1391 (OPA->vector[2] != OPB->vector[2]) );
1394 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1395 prog_getstring(prog, OPB->string));
1398 OPC->_float = (OPA->_int != OPB->_int);
1401 OPC->_float = (OPA->function != OPB->function);
1405 OPC->_float = (OPA->_float <= OPB->_float);
1408 OPC->_float = (OPA->_float >= OPB->_float);
1411 OPC->_float = (OPA->_float < OPB->_float);
1414 OPC->_float = (OPA->_float > OPB->_float);
1419 case INSTR_LOAD_FLD:
1420 case INSTR_LOAD_ENT:
1421 case INSTR_LOAD_FNC:
1422 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1423 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1426 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1427 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1432 ed = prog_getedict(prog, OPA->edict);
1433 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1436 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1437 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1440 if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1442 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1447 ed = prog_getedict(prog, OPA->edict);
1448 ptr = (qcany*)( ((qcint*)ed) + OPB->_int );
1449 OPC->ivector[0] = ptr->ivector[0];
1450 OPC->ivector[1] = ptr->ivector[1];
1451 OPC->ivector[2] = ptr->ivector[2];
1455 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1456 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1459 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1461 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1467 ed = prog_getedict(prog, OPA->edict);
1468 OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1473 case INSTR_STORE_ENT:
1474 case INSTR_STORE_FLD:
1475 case INSTR_STORE_FNC:
1476 OPB->_int = OPA->_int;
1479 OPB->ivector[0] = OPA->ivector[0];
1480 OPB->ivector[1] = OPA->ivector[1];
1481 OPB->ivector[2] = OPA->ivector[2];
1484 case INSTR_STOREP_F:
1485 case INSTR_STOREP_S:
1486 case INSTR_STOREP_ENT:
1487 case INSTR_STOREP_FLD:
1488 case INSTR_STOREP_FNC:
1489 if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1490 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1493 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1494 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1496 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1498 ptr = (qcany*)(prog->entitydata + OPB->_int);
1499 ptr->_int = OPA->_int;
1501 case INSTR_STOREP_V:
1502 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1503 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1506 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1507 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1509 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1511 ptr = (qcany*)(prog->entitydata + OPB->_int);
1512 ptr->ivector[0] = OPA->ivector[0];
1513 ptr->ivector[1] = OPA->ivector[1];
1514 ptr->ivector[2] = OPA->ivector[2];
1518 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1521 OPC->_float = !OPA->vector[0] &&
1526 OPC->_float = !OPA->string ||
1527 !*prog_getstring(prog, OPA->string);
1530 OPC->_float = (OPA->edict == 0);
1533 OPC->_float = !OPA->function;
1537 /* this is consistent with darkplaces' behaviour */
1538 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1540 st += st->o2.s1 - 1; /* offset the s++ */
1541 if (++jumpcount >= maxjumps)
1542 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1546 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1548 st += st->o2.s1 - 1; /* offset the s++ */
1549 if (++jumpcount >= maxjumps)
1550 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1563 prog->argc = st->opcode - INSTR_CALL0;
1565 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1567 if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1569 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1573 newf = &prog->functions[OPA->function];
1576 prog->statement = (st - prog->code) + 1;
1578 if (newf->entry < 0)
1580 /* negative statements are built in functions */
1581 qcint builtinnumber = -newf->entry;
1582 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1583 prog->builtins[builtinnumber](prog);
1585 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1586 builtinnumber, prog->filename);
1589 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1595 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1599 st += st->o1.s1 - 1; /* offset the s++ */
1600 if (++jumpcount == 10000000)
1601 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1605 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1606 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1609 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1610 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1614 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1617 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1624 #endif /* !QCVM_LOOP */