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);
152 void prog_delete(qc_program *prog)
154 if (prog->filename) mem_d(prog->filename);
155 vec_free(prog->code);
156 vec_free(prog->defs);
157 vec_free(prog->fields);
158 vec_free(prog->functions);
159 vec_free(prog->strings);
160 vec_free(prog->globals);
161 vec_free(prog->entitydata);
162 vec_free(prog->entitypool);
163 vec_free(prog->localstack);
164 vec_free(prog->stack);
165 vec_free(prog->profile);
169 /***********************************************************************
173 const char* prog_getstring(qc_program *prog, qcint str) {
174 /* cast for return required for C++ */
175 if (str < 0 || str >= (qcint)vec_size(prog->strings))
176 return "<<<invalid string>>>";
178 return prog->strings + str;
181 prog_section_def* prog_entfield(qc_program *prog, qcint off) {
183 for (i = 0; i < vec_size(prog->fields); ++i) {
184 if (prog->fields[i].offset == off)
185 return (prog->fields + i);
190 prog_section_def* prog_getdef(qc_program *prog, qcint off)
193 for (i = 0; i < vec_size(prog->defs); ++i) {
194 if (prog->defs[i].offset == off)
195 return (prog->defs + i);
200 qcany* prog_getedict(qc_program *prog, qcint e) {
201 if (e >= (qcint)vec_size(prog->entitypool)) {
203 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
206 return (qcany*)(prog->entitydata + (prog->entityfields * e));
209 qcint prog_spawn_entity(qc_program *prog) {
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));
219 vec_push(prog->entitypool, true);
221 data = (char*)vec_add(prog->entitydata, prog->entityfields);
222 memset(data, 0, prog->entityfields * sizeof(qcint));
226 void prog_free_entity(qc_program *prog, qcint e) {
229 fprintf(stderr, "Trying to free world entity\n");
232 if (e >= (qcint)vec_size(prog->entitypool)) {
234 fprintf(stderr, "Trying to free out of bounds entity\n");
237 if (!prog->entitypool[e]) {
239 fprintf(stderr, "Double free on entity\n");
242 prog->entitypool[e] = false;
245 qcint prog_tempstring(qc_program *prog, const char *str) {
246 size_t len = strlen(str);
247 size_t at = prog->tempstring_at;
249 /* when we reach the end we start over */
250 if (at + len >= vec_size(prog->strings))
251 at = prog->tempstring_start;
253 /* when it doesn't fit, reallocate */
254 if (at + len >= vec_size(prog->strings))
256 (void)vec_add(prog->strings, len+1);
257 memcpy(prog->strings + at, str, len+1);
261 /* when it fits, just copy */
262 memcpy(prog->strings + at, str, len+1);
263 prog->tempstring_at += len+1;
267 static size_t print_escaped_string(const char *str, size_t maxlen) {
270 --maxlen; /* because we're lazy and have escape sequences */
280 case '\a': len += 2; putchar('\\'); putchar('a'); break;
281 case '\b': len += 2; putchar('\\'); putchar('b'); break;
282 case '\r': len += 2; putchar('\\'); putchar('r'); break;
283 case '\n': len += 2; putchar('\\'); putchar('n'); break;
284 case '\t': len += 2; putchar('\\'); putchar('t'); break;
285 case '\f': len += 2; putchar('\\'); putchar('f'); break;
286 case '\v': len += 2; putchar('\\'); putchar('v'); break;
287 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
288 case '"': len += 2; putchar('\\'); putchar('"'); break;
300 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) {
301 static char spaces[28+1] = " ";
302 prog_section_def *def;
307 len = printf("<null>,");
311 def = prog_getdef(prog, glob);
312 value = (qcany*)(&prog->globals[glob]);
314 len = printf("[@%u] ", glob);
316 const char *name = prog_getstring(prog, def->name);
320 len += printf("%s ", name);
321 vtype = def->type & DEF_TYPEMASK;
330 len += printf("(%i),", value->_int);
333 len += printf("'%g %g %g',", value->vector[0],
339 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
341 len += printf("(null)");
343 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
347 len += printf("%g,", value->_float);
351 if (len < (int)sizeof(spaces)-1) {
352 spaces[sizeof(spaces)-1-len] = 0;
353 fs_file_puts(stdout, spaces);
354 spaces[sizeof(spaces)-1-len] = ' ';
358 static void prog_print_statement(qc_program *prog, prog_section_statement *st) {
359 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
360 printf("<illegal instruction %d>\n", st->opcode);
363 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
365 for (i = 0; i < vec_size(prog->function_stack); ++i)
367 printf("%s:", vec_last(prog->function_stack));
369 printf(" <> %-12s", asm_instr[st->opcode].m);
370 if (st->opcode >= INSTR_IF &&
371 st->opcode <= INSTR_IFNOT)
373 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
374 printf("%d\n", st->o2.s1);
376 else if (st->opcode >= INSTR_CALL0 &&
377 st->opcode <= INSTR_CALL8)
379 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
382 else if (st->opcode == INSTR_GOTO)
384 printf("%i\n", st->o1.s1);
388 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
392 t[1] = t[2] = TYPE_VECTOR;
395 t[0] = t[2] = TYPE_VECTOR;
398 t[0] = t[1] = TYPE_VECTOR;
404 t[0] = t[1] = t[2] = TYPE_VECTOR;
408 t[0] = t[1] = TYPE_STRING;
415 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
418 t[0] = t[1] = TYPE_STRING; t[2] = -1;
420 case INSTR_STORE_ENT:
421 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
423 case INSTR_STORE_FLD:
424 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
426 case INSTR_STORE_FNC:
427 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
430 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
433 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
435 case INSTR_STOREP_ENT:
436 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
438 case INSTR_STOREP_FLD:
439 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
441 case INSTR_STOREP_FNC:
442 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
445 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
446 else printf("(none), ");
447 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
448 else printf("(none), ");
449 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
450 else printf("(none)");
455 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) {
461 st.localsp = vec_size(prog->localstack);
462 st.stmt = prog->statement;
465 if (prog->xflags & VMXF_TRACE) {
466 const char *str = prog_getstring(prog, func->name);
467 vec_push(prog->function_stack, str);
470 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
471 if (vec_size(prog->stack))
473 prog_section_function *cur;
474 cur = prog->stack[vec_size(prog->stack)-1].function;
477 qcint *globals = prog->globals + cur->firstlocal;
478 vec_append(prog->localstack, cur->locals, globals);
483 qcint *globals = prog->globals + func->firstlocal;
484 vec_append(prog->localstack, func->locals, globals);
488 /* copy parameters */
489 parampos = func->firstlocal;
490 for (p = 0; p < func->nargs; ++p)
493 for (s = 0; s < func->argsize[p]; ++s) {
494 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
499 vec_push(prog->stack, st);
504 static qcint prog_leavefunction(qc_program *prog) {
505 prog_section_function *prev = NULL;
508 qc_exec_stack st = vec_last(prog->stack);
510 if (prog->xflags & VMXF_TRACE) {
511 if (vec_size(prog->function_stack))
512 vec_pop(prog->function_stack);
515 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
516 if (vec_size(prog->stack) > 1) {
517 prev = prog->stack[vec_size(prog->stack)-2].function;
518 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
521 prev = prog->stack[vec_size(prog->stack)-1].function;
522 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
525 qcint *globals = prog->globals + prev->firstlocal;
526 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
527 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
528 vec_shrinkto(prog->localstack, oldsp);
531 vec_pop(prog->stack);
533 return st.stmt - 1; /* offset the ++st */
536 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps) {
538 size_t oldxflags = prog->xflags;
539 prog_section_statement *st;
542 prog->xflags = flags;
544 st = prog->code + prog_enterfunction(prog, func);
552 #define QCVM_PROFILE 0
558 #define QCVM_PROFILE 0
564 #define QCVM_PROFILE 1
568 case (VMXF_TRACE|VMXF_PROFILE):
570 #define QCVM_PROFILE 1
577 prog->xflags = oldxflags;
578 vec_free(prog->localstack);
579 vec_free(prog->stack);
585 /***********************************************************************
586 * main for when building the standalone executor
589 #if defined(QCVM_EXECUTOR)
594 const char *type_name[TYPE_COUNT] = {
620 static qcvm_parameter *main_params = NULL;
622 #define CheckArgs(num) do { \
623 if (prog->argc != (num)) { \
625 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
626 __FUNCTION__, prog->argc, (num)); \
631 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
632 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
633 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
635 static int qc_print(qc_program *prog) {
637 const char *laststr = NULL;
638 for (i = 0; i < (size_t)prog->argc; ++i) {
639 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
640 laststr = prog_getstring(prog, str->string);
641 printf("%s", laststr);
643 if (laststr && (prog->xflags & VMXF_TRACE)) {
644 size_t len = strlen(laststr);
645 if (!len || laststr[len-1] != '\n')
651 static int qc_error(qc_program *prog) {
652 fprintf(stderr, "*** VM raised an error:\n");
658 static int qc_ftos(qc_program *prog) {
664 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
665 str.string = prog_tempstring(prog, buffer);
670 static int qc_stof(qc_program *prog) {
675 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
680 static int qc_vtos(qc_program *prog) {
686 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
687 str.string = prog_tempstring(prog, buffer);
692 static int qc_etos(qc_program *prog) {
698 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
699 str.string = prog_tempstring(prog, buffer);
704 static int qc_spawn(qc_program *prog) {
707 ent.edict = prog_spawn_entity(prog);
709 return (ent.edict ? 0 : -1);
712 static int qc_kill(qc_program *prog) {
716 prog_free_entity(prog, ent->edict);
720 static int qc_sqrt(qc_program *prog) {
724 out._float = sqrt(num->_float);
729 static int qc_vlen(qc_program *prog) {
733 len._float = sqrt(vec->vector[0] * vec->vector[0] +
734 vec->vector[1] * vec->vector[1] +
735 vec->vector[2] * vec->vector[2]);
740 static int qc_normalize(qc_program *prog) {
746 len = sqrt(vec->vector[0] * vec->vector[0] +
747 vec->vector[1] * vec->vector[1] +
748 vec->vector[2] * vec->vector[2]);
753 out.vector[0] = len * vec->vector[0];
754 out.vector[1] = len * vec->vector[1];
755 out.vector[2] = len * vec->vector[2];
760 static int qc_strcat(qc_program *prog) {
772 cstr1 = prog_getstring(prog, str1->string);
773 cstr2 = prog_getstring(prog, str2->string);
774 len1 = strlen(cstr1);
775 len2 = strlen(cstr2);
776 buffer = (char*)mem_a(len1 + len2 + 1);
777 memcpy(buffer, cstr1, len1);
778 memcpy(buffer+len1, cstr2, len2+1);
779 out.string = prog_tempstring(prog, buffer);
785 static int qc_strcmp(qc_program *prog) {
792 if (prog->argc != 2 && prog->argc != 3) {
793 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
800 cstr1 = prog_getstring(prog, str1->string);
801 cstr2 = prog_getstring(prog, str2->string);
803 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
805 out._float = strcmp(cstr1, cstr2);
810 static int qc_floor(qc_program *prog) {
814 out._float = floor(num->_float);
819 static prog_builtin qc_builtins[] = {
832 &qc_normalize, /* 12 */
836 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
838 static const char *arg0 = NULL;
840 static void version(void) {
841 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
850 static void usage(void) {
851 printf("usage: %s [options] [parameters] file\n", arg0);
852 printf("options:\n");
853 printf(" -h, --help print this message\n"
854 " -trace trace the execution\n"
855 " -profile perform profiling during execution\n"
856 " -info print information from the prog's header\n"
857 " -disasm disassemble and exit\n"
858 " -disasm-func func disassemble and exit\n"
859 " -printdefs list the defs section\n"
860 " -printfields list the field section\n"
861 " -printfuns list functions information\n"
863 " -vv be even more verbose\n");
864 printf("parameters:\n");
865 printf(" -vector <V> pass a vector parameter to main()\n"
866 " -float <f> pass a float parameter to main()\n"
867 " -string <s> pass a string parameter to main() \n");
870 static void prog_main_setparams(qc_program *prog) {
874 for (i = 0; i < vec_size(main_params); ++i) {
875 arg = GetGlobal(OFS_PARM0 + 3*i);
879 switch (main_params[i].vtype) {
882 (void)sscanf_s(main_params[i].value, " %f %f %f ",
887 (void)sscanf(main_params[i].value, " %f %f %f ",
894 arg->_float = atof(main_params[i].value);
897 arg->string = prog_tempstring(prog, main_params[i].value);
900 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
906 void escapestring(char* dest, const char* src) {
908 while ((c = *(src++))) {
911 *(dest++) = '\\', *(dest++) = 't';
914 *(dest++) = '\\', *(dest++) = 'n';
917 *(dest++) = '\\', *(dest++) = 'r';
920 *(dest++) = '\\', *(dest++) = '\\';
923 *(dest++) = '\\', *(dest++) = '\"';
932 void prog_disasm_function(qc_program *prog, size_t id);
934 int main(int argc, char **argv) {
938 size_t xflags = VMXF_DEFAULT;
939 bool opts_printfields = false;
940 bool opts_printdefs = false;
941 bool opts_printfuns = false;
942 bool opts_disasm = false;
943 bool opts_info = false;
945 const char *progsfile = NULL;
946 const char **dis_list = NULL;
957 if (!strcmp(argv[1], "-h") ||
958 !strcmp(argv[1], "-help") ||
959 !strcmp(argv[1], "--help"))
964 else if (!strcmp(argv[1], "-v")) {
969 else if (!strncmp(argv[1], "-vv", 3)) {
970 const char *av = argv[1]+1;
982 else if (!strcmp(argv[1], "-version") ||
983 !strcmp(argv[1], "--version"))
988 else if (!strcmp(argv[1], "-trace")) {
991 xflags |= VMXF_TRACE;
993 else if (!strcmp(argv[1], "-profile")) {
996 xflags |= VMXF_PROFILE;
998 else if (!strcmp(argv[1], "-info")) {
1004 else if (!strcmp(argv[1], "-disasm")) {
1010 else if (!strcmp(argv[1], "-disasm-func")) {
1017 vec_push(dis_list, argv[1]);
1022 else if (!strcmp(argv[1], "-printdefs")) {
1025 opts_printdefs = true;
1028 else if (!strcmp(argv[1], "-printfuns")) {
1031 opts_printfuns = true;
1034 else if (!strcmp(argv[1], "-printfields")) {
1037 opts_printfields = true;
1040 else if (!strcmp(argv[1], "-vector") ||
1041 !strcmp(argv[1], "-string") ||
1042 !strcmp(argv[1], "-float") )
1045 if (argv[1][1] == 'f')
1046 p.vtype = TYPE_FLOAT;
1047 else if (argv[1][1] == 's')
1048 p.vtype = TYPE_STRING;
1049 else if (argv[1][1] == 'v')
1050 p.vtype = TYPE_VECTOR;
1060 vec_push(main_params, p);
1064 else if (!strcmp(argv[1], "--")) {
1069 else if (argv[1][0] != '-') {
1071 fprintf(stderr, "only 1 program file may be specified\n");
1075 progsfile = argv[1];
1081 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1087 if (argc == 2 && !progsfile) {
1088 progsfile = argv[1];
1094 fprintf(stderr, "must specify a program to execute\n");
1099 prog = prog_load(progsfile, noexec);
1101 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1105 prog->builtins = qc_builtins;
1106 prog->builtins_count = qc_builtins_count;
1109 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1110 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1111 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1118 (unsigned long)vec_size(prog->code),
1119 (unsigned long)vec_size(prog->defs),
1120 (unsigned long)vec_size(prog->fields),
1121 (unsigned long)vec_size(prog->functions),
1122 (unsigned long)vec_size(prog->strings));
1129 for (i = 0; i < vec_size(dis_list); ++i) {
1131 printf("Looking for `%s`\n", dis_list[i]);
1132 for (k = 1; k < vec_size(prog->functions); ++k) {
1133 const char *name = prog_getstring(prog, prog->functions[k].name);
1134 if (!strcmp(name, dis_list[i])) {
1135 prog_disasm_function(prog, k);
1141 for (i = 1; i < vec_size(prog->functions); ++i)
1142 prog_disasm_function(prog, i);
1145 if (opts_printdefs) {
1146 char *escape = NULL;
1147 const char *getstring = NULL;
1149 for (i = 0; i < vec_size(prog->defs); ++i) {
1150 printf("Global: %8s %-16s at %u%s",
1151 type_name[prog->defs[i].type & DEF_TYPEMASK],
1152 prog_getstring(prog, prog->defs[i].name),
1153 (unsigned int)prog->defs[i].offset,
1154 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1156 switch (prog->defs[i].type & DEF_TYPEMASK) {
1158 printf(" [init: %g]", ((qcany*)(prog->globals + prog->defs[i].offset))->_float);
1161 printf(" [init: %i]", (int)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1167 printf(" [init: %u]", (unsigned)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1170 getstring = prog_getstring(prog, ((qcany*)(prog->globals + prog->defs[i].offset))->string);
1171 escape = (char*)mem_a(strlen(getstring) * 2 + 1); /* will be enough */
1172 escapestring(escape, getstring);
1173 printf(" [init: `%s`]", escape);
1175 mem_d(escape); /* free */
1184 if (opts_printfields) {
1185 for (i = 0; i < vec_size(prog->fields); ++i) {
1186 printf("Field: %8s %-16s at %u%s\n",
1187 type_name[prog->fields[i].type],
1188 prog_getstring(prog, prog->fields[i].name),
1189 (unsigned int)prog->fields[i].offset,
1190 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1193 if (opts_printfuns) {
1194 for (i = 0; i < vec_size(prog->functions); ++i) {
1196 printf("Function: %-16s taking %i parameters:(",
1197 prog_getstring(prog, prog->functions[i].name),
1198 (unsigned int)prog->functions[i].nargs);
1199 for (a = 0; a < prog->functions[i].nargs; ++a) {
1200 printf(" %i", prog->functions[i].argsize[a]);
1203 int32_t start = prog->functions[i].entry;
1205 printf(") builtin %i\n", (int)-start);
1208 prog_section_statement *st = prog->code + start;
1209 for (;st->opcode != INSTR_DONE; ++st)
1211 printf(") - %lu instructions", (unsigned long)funsize);
1213 printf(" - locals: %i + %i\n",
1214 prog->functions[i].firstlocal,
1215 prog->functions[i].locals);
1222 printf(") locals: %i + %i\n",
1223 prog->functions[i].firstlocal,
1224 prog->functions[i].locals);
1231 for (i = 1; i < vec_size(prog->functions); ++i) {
1232 const char *name = prog_getstring(prog, prog->functions[i].name);
1233 if (!strcmp(name, "main"))
1238 prog_main_setparams(prog);
1239 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1242 fprintf(stderr, "No main function found\n");
1249 void prog_disasm_function(qc_program *prog, size_t id) {
1250 prog_section_function *fdef = prog->functions + id;
1251 prog_section_statement *st;
1253 if (fdef->entry < 0) {
1254 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1258 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1260 st = prog->code + fdef->entry;
1261 while (st->opcode != INSTR_DONE) {
1262 prog_print_statement(prog, st);
1267 #else /* !QCVM_LOOP */
1269 * Everything from here on is not including into the compilation of the
1270 * executor. This is simply code that is #included via #include __FILE__
1271 * see when QCVM_LOOP is defined, the rest of the code above do not get
1272 * re-included. So this really just acts like one large macro, but it
1273 * sort of isn't, which makes it nicer looking.
1276 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1277 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1278 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1280 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1282 /* to be consistent with current darkplaces behaviour */
1283 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1284 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1288 prog_section_function *newf;
1295 prog->profile[st - prog->code]++;
1299 prog_print_statement(prog, st);
1305 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1310 /* TODO: add instruction count to function profile count */
1311 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1312 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1313 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1315 st = prog->code + prog_leavefunction(prog);
1316 if (!vec_size(prog->stack))
1322 OPC->_float = OPA->_float * OPB->_float;
1325 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1326 OPA->vector[1]*OPB->vector[1] +
1327 OPA->vector[2]*OPB->vector[2];
1331 qcfloat f = OPA->_float;
1332 OPC->vector[0] = f * OPB->vector[0];
1333 OPC->vector[1] = f * OPB->vector[1];
1334 OPC->vector[2] = f * OPB->vector[2];
1339 qcfloat f = OPB->_float;
1340 OPC->vector[0] = f * OPA->vector[0];
1341 OPC->vector[1] = f * OPA->vector[1];
1342 OPC->vector[2] = f * OPA->vector[2];
1346 if (OPB->_float != 0.0f)
1347 OPC->_float = OPA->_float / OPB->_float;
1353 OPC->_float = OPA->_float + OPB->_float;
1356 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1357 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1358 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1361 OPC->_float = OPA->_float - OPB->_float;
1364 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1365 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1366 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1370 OPC->_float = (OPA->_float == OPB->_float);
1373 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1374 (OPA->vector[1] == OPB->vector[1]) &&
1375 (OPA->vector[2] == OPB->vector[2]) );
1378 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1379 prog_getstring(prog, OPB->string));
1382 OPC->_float = (OPA->_int == OPB->_int);
1385 OPC->_float = (OPA->function == OPB->function);
1388 OPC->_float = (OPA->_float != OPB->_float);
1391 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1392 (OPA->vector[1] != OPB->vector[1]) ||
1393 (OPA->vector[2] != OPB->vector[2]) );
1396 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1397 prog_getstring(prog, OPB->string));
1400 OPC->_float = (OPA->_int != OPB->_int);
1403 OPC->_float = (OPA->function != OPB->function);
1407 OPC->_float = (OPA->_float <= OPB->_float);
1410 OPC->_float = (OPA->_float >= OPB->_float);
1413 OPC->_float = (OPA->_float < OPB->_float);
1416 OPC->_float = (OPA->_float > OPB->_float);
1421 case INSTR_LOAD_FLD:
1422 case INSTR_LOAD_ENT:
1423 case INSTR_LOAD_FNC:
1424 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1425 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1428 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1429 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1434 ed = prog_getedict(prog, OPA->edict);
1435 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1438 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1439 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1442 if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1444 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1449 ed = prog_getedict(prog, OPA->edict);
1450 ptr = (qcany*)( ((qcint*)ed) + OPB->_int );
1451 OPC->ivector[0] = ptr->ivector[0];
1452 OPC->ivector[1] = ptr->ivector[1];
1453 OPC->ivector[2] = ptr->ivector[2];
1457 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1458 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1461 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1463 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1469 ed = prog_getedict(prog, OPA->edict);
1470 OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1475 case INSTR_STORE_ENT:
1476 case INSTR_STORE_FLD:
1477 case INSTR_STORE_FNC:
1478 OPB->_int = OPA->_int;
1481 OPB->ivector[0] = OPA->ivector[0];
1482 OPB->ivector[1] = OPA->ivector[1];
1483 OPB->ivector[2] = OPA->ivector[2];
1486 case INSTR_STOREP_F:
1487 case INSTR_STOREP_S:
1488 case INSTR_STOREP_ENT:
1489 case INSTR_STOREP_FLD:
1490 case INSTR_STOREP_FNC:
1491 if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1492 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1495 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1496 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1498 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1500 ptr = (qcany*)(prog->entitydata + OPB->_int);
1501 ptr->_int = OPA->_int;
1503 case INSTR_STOREP_V:
1504 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1505 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1508 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1509 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1511 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1513 ptr = (qcany*)(prog->entitydata + OPB->_int);
1514 ptr->ivector[0] = OPA->ivector[0];
1515 ptr->ivector[1] = OPA->ivector[1];
1516 ptr->ivector[2] = OPA->ivector[2];
1520 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1523 OPC->_float = !OPA->vector[0] &&
1528 OPC->_float = !OPA->string ||
1529 !*prog_getstring(prog, OPA->string);
1532 OPC->_float = (OPA->edict == 0);
1535 OPC->_float = !OPA->function;
1539 /* this is consistent with darkplaces' behaviour */
1540 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1542 st += st->o2.s1 - 1; /* offset the s++ */
1543 if (++jumpcount >= maxjumps)
1544 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1548 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1550 st += st->o2.s1 - 1; /* offset the s++ */
1551 if (++jumpcount >= maxjumps)
1552 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1565 prog->argc = st->opcode - INSTR_CALL0;
1567 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1569 if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1571 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1575 newf = &prog->functions[OPA->function];
1578 prog->statement = (st - prog->code) + 1;
1580 if (newf->entry < 0)
1582 /* negative statements are built in functions */
1583 qcint builtinnumber = -newf->entry;
1584 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1585 prog->builtins[builtinnumber](prog);
1587 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1588 builtinnumber, prog->filename);
1591 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1597 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1601 st += st->o1.s1 - 1; /* offset the s++ */
1602 if (++jumpcount == 10000000)
1603 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1607 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1608 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1611 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1612 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1616 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1619 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1626 #endif /* !QCVM_LOOP */