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_t *prog, const char *fmt, ...)
55 qc_program_t* 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_t*)mem_a(sizeof(qc_program_t));
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_t *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_t *prog, qcint_t str) {
174 /* cast for return required for C++ */
175 if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
176 return "<<<invalid string>>>";
178 return prog->strings + str;
181 prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t 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_t* prog_getdef(qc_program_t *prog, qcint_t 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_t* prog_getedict(qc_program_t *prog, qcint_t e) {
201 if (e >= (qcint_t)vec_size(prog->entitypool)) {
203 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
206 return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
209 qcint_t prog_spawn_entity(qc_program_t *prog) {
212 for (e = 0; e < (qcint_t)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_t));
219 vec_push(prog->entitypool, true);
221 data = (char*)vec_add(prog->entitydata, prog->entityfields);
222 memset(data, 0, prog->entityfields * sizeof(qcint_t));
226 void prog_free_entity(qc_program_t *prog, qcint_t e) {
229 fprintf(stderr, "Trying to free world entity\n");
232 if (e >= (qcint_t)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_t prog_tempstring(qc_program_t *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_t *prog, unsigned int glob, int vtype) {
301 static char spaces[28+1] = " ";
302 prog_section_def_t *def;
307 if ((len = printf("<null>,")) == -1)
313 def = prog_getdef(prog, glob);
314 value = (qcany_t*)(&prog->globals[glob]);
316 len = printf("[@%u] ", glob);
318 const char *name = prog_getstring(prog, def->name);
322 len += printf("%s ", name);
323 vtype = def->type & DEF_TYPEMASK;
332 len += printf("(%i),", value->_int);
335 len += printf("'%g %g %g',", value->vector[0],
341 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
343 len += printf("(null)");
345 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
349 len += printf("%g,", value->_float);
353 if (len < (int)sizeof(spaces)-1) {
354 spaces[sizeof(spaces)-1-len] = 0;
355 fs_file_puts(stdout, spaces);
356 spaces[sizeof(spaces)-1-len] = ' ';
360 static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
361 if (st->opcode >= VINSTR_END) {
362 printf("<illegal instruction %d>\n", st->opcode);
365 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
367 for (i = 0; i < vec_size(prog->function_stack); ++i)
369 printf("%s:", vec_last(prog->function_stack));
371 printf(" <> %-12s", util_instr_str[st->opcode]);
372 if (st->opcode >= INSTR_IF &&
373 st->opcode <= INSTR_IFNOT)
375 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
376 printf("%d\n", st->o2.s1);
378 else if (st->opcode >= INSTR_CALL0 &&
379 st->opcode <= INSTR_CALL8)
381 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
384 else if (st->opcode == INSTR_GOTO)
386 printf("%i\n", st->o1.s1);
390 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
394 t[1] = t[2] = TYPE_VECTOR;
397 t[0] = t[2] = TYPE_VECTOR;
400 t[0] = t[1] = TYPE_VECTOR;
406 t[0] = t[1] = t[2] = TYPE_VECTOR;
410 t[0] = t[1] = TYPE_STRING;
417 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
420 t[0] = t[1] = TYPE_STRING; t[2] = -1;
422 case INSTR_STORE_ENT:
423 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
425 case INSTR_STORE_FLD:
426 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
428 case INSTR_STORE_FNC:
429 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
432 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
435 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
437 case INSTR_STOREP_ENT:
438 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
440 case INSTR_STOREP_FLD:
441 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
443 case INSTR_STOREP_FNC:
444 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
447 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
448 else printf("(none), ");
449 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
450 else printf("(none), ");
451 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
452 else printf("(none)");
457 static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
463 st.localsp = vec_size(prog->localstack);
464 st.stmt = prog->statement;
467 if (prog->xflags & VMXF_TRACE) {
468 const char *str = prog_getstring(prog, func->name);
469 vec_push(prog->function_stack, str);
472 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
473 if (vec_size(prog->stack))
475 prog_section_function_t *cur;
476 cur = prog->stack[vec_size(prog->stack)-1].function;
479 qcint_t *globals = prog->globals + cur->firstlocal;
480 vec_append(prog->localstack, cur->locals, globals);
485 qcint_t *globals = prog->globals + func->firstlocal;
486 vec_append(prog->localstack, func->locals, globals);
490 /* copy parameters */
491 parampos = func->firstlocal;
492 for (p = 0; p < func->nargs; ++p)
495 for (s = 0; s < func->argsize[p]; ++s) {
496 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
501 vec_push(prog->stack, st);
506 static qcint_t prog_leavefunction(qc_program_t *prog) {
507 prog_section_function_t *prev = NULL;
510 qc_exec_stack_t st = vec_last(prog->stack);
512 if (prog->xflags & VMXF_TRACE) {
513 if (vec_size(prog->function_stack))
514 vec_pop(prog->function_stack);
517 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
518 if (vec_size(prog->stack) > 1) {
519 prev = prog->stack[vec_size(prog->stack)-2].function;
520 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
523 prev = prog->stack[vec_size(prog->stack)-1].function;
524 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
527 qcint_t *globals = prog->globals + prev->firstlocal;
528 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
529 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
530 vec_shrinkto(prog->localstack, oldsp);
533 vec_pop(prog->stack);
535 return st.stmt - 1; /* offset the ++st */
538 bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
540 size_t oldxflags = prog->xflags;
541 prog_section_statement_t *st;
544 prog->xflags = flags;
546 st = prog->code + prog_enterfunction(prog, func);
554 #define QCVM_PROFILE 0
560 #define QCVM_PROFILE 0
566 #define QCVM_PROFILE 1
570 case (VMXF_TRACE|VMXF_PROFILE):
572 #define QCVM_PROFILE 1
579 prog->xflags = oldxflags;
580 vec_free(prog->localstack);
581 vec_free(prog->stack);
587 /***********************************************************************
588 * main for when building the standalone executor
591 #if defined(QCVM_EXECUTOR)
596 const char *type_name[TYPE_COUNT] = {
622 static qcvm_parameter *main_params = NULL;
624 #define CheckArgs(num) do { \
625 if (prog->argc != (num)) { \
627 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
628 __FUNCTION__, prog->argc, (num)); \
633 #define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
634 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
635 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
637 static int qc_print(qc_program_t *prog) {
639 const char *laststr = NULL;
640 for (i = 0; i < (size_t)prog->argc; ++i) {
641 qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
642 laststr = prog_getstring(prog, str->string);
643 printf("%s", laststr);
645 if (laststr && (prog->xflags & VMXF_TRACE)) {
646 size_t len = strlen(laststr);
647 if (!len || laststr[len-1] != '\n')
653 static int qc_error(qc_program_t *prog) {
654 fprintf(stderr, "*** VM raised an error:\n");
660 static int qc_ftos(qc_program_t *prog) {
666 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
667 str.string = prog_tempstring(prog, buffer);
672 static int qc_stof(qc_program_t *prog) {
677 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
682 static int qc_vtos(qc_program_t *prog) {
688 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
689 str.string = prog_tempstring(prog, buffer);
694 static int qc_etos(qc_program_t *prog) {
700 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
701 str.string = prog_tempstring(prog, buffer);
706 static int qc_spawn(qc_program_t *prog) {
709 ent.edict = prog_spawn_entity(prog);
711 return (ent.edict ? 0 : -1);
714 static int qc_kill(qc_program_t *prog) {
718 prog_free_entity(prog, ent->edict);
722 static int qc_sqrt(qc_program_t *prog) {
726 out._float = sqrt(num->_float);
731 static int qc_vlen(qc_program_t *prog) {
735 len._float = sqrt(vec->vector[0] * vec->vector[0] +
736 vec->vector[1] * vec->vector[1] +
737 vec->vector[2] * vec->vector[2]);
742 static int qc_normalize(qc_program_t *prog) {
748 len = sqrt(vec->vector[0] * vec->vector[0] +
749 vec->vector[1] * vec->vector[1] +
750 vec->vector[2] * vec->vector[2]);
755 out.vector[0] = len * vec->vector[0];
756 out.vector[1] = len * vec->vector[1];
757 out.vector[2] = len * vec->vector[2];
762 static int qc_strcat(qc_program_t *prog) {
765 qcany_t *str1, *str2;
774 cstr1 = prog_getstring(prog, str1->string);
775 cstr2 = prog_getstring(prog, str2->string);
776 len1 = strlen(cstr1);
777 len2 = strlen(cstr2);
778 buffer = (char*)mem_a(len1 + len2 + 1);
779 memcpy(buffer, cstr1, len1);
780 memcpy(buffer+len1, cstr2, len2+1);
781 out.string = prog_tempstring(prog, buffer);
787 static int qc_strcmp(qc_program_t *prog) {
788 qcany_t *str1, *str2;
794 if (prog->argc != 2 && prog->argc != 3) {
795 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
802 cstr1 = prog_getstring(prog, str1->string);
803 cstr2 = prog_getstring(prog, str2->string);
805 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
807 out._float = strcmp(cstr1, cstr2);
812 static int qc_floor(qc_program_t *prog) {
816 out._float = floor(num->_float);
821 static prog_builtin_t qc_builtins[] = {
834 &qc_normalize, /* 12 */
838 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
840 static const char *arg0 = NULL;
842 static void version(void) {
843 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
852 static void usage(void) {
853 printf("usage: %s [options] [parameters] file\n", arg0);
854 printf("options:\n");
855 printf(" -h, --help print this message\n"
856 " -trace trace the execution\n"
857 " -profile perform profiling during execution\n"
858 " -info print information from the prog's header\n"
859 " -disasm disassemble and exit\n"
860 " -disasm-func func disassemble and exit\n"
861 " -printdefs list the defs section\n"
862 " -printfields list the field section\n"
863 " -printfuns list functions information\n"
865 " -vv be even more verbose\n");
866 printf("parameters:\n");
867 printf(" -vector <V> pass a vector parameter to main()\n"
868 " -float <f> pass a float parameter to main()\n"
869 " -string <s> pass a string parameter to main() \n");
872 static void prog_main_setparams(qc_program_t *prog) {
876 for (i = 0; i < vec_size(main_params); ++i) {
877 arg = GetGlobal(OFS_PARM0 + 3*i);
881 switch (main_params[i].vtype) {
884 (void)sscanf_s(main_params[i].value, " %f %f %f ",
889 (void)sscanf(main_params[i].value, " %f %f %f ",
896 arg->_float = atof(main_params[i].value);
899 arg->string = prog_tempstring(prog, main_params[i].value);
902 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
908 void escapestring(char* dest, const char* src) {
910 while ((c = *(src++))) {
913 *(dest++) = '\\', *(dest++) = 't';
916 *(dest++) = '\\', *(dest++) = 'n';
919 *(dest++) = '\\', *(dest++) = 'r';
922 *(dest++) = '\\', *(dest++) = '\\';
925 *(dest++) = '\\', *(dest++) = '\"';
934 void prog_disasm_function(qc_program_t *prog, size_t id);
936 int main(int argc, char **argv) {
940 size_t xflags = VMXF_DEFAULT;
941 bool opts_printfields = false;
942 bool opts_printdefs = false;
943 bool opts_printfuns = false;
944 bool opts_disasm = false;
945 bool opts_info = false;
947 const char *progsfile = NULL;
948 const char **dis_list = NULL;
959 if (!strcmp(argv[1], "-h") ||
960 !strcmp(argv[1], "-help") ||
961 !strcmp(argv[1], "--help"))
966 else if (!strcmp(argv[1], "-v")) {
971 else if (!strncmp(argv[1], "-vv", 3)) {
972 const char *av = argv[1]+1;
984 else if (!strcmp(argv[1], "-version") ||
985 !strcmp(argv[1], "--version"))
990 else if (!strcmp(argv[1], "-trace")) {
993 xflags |= VMXF_TRACE;
995 else if (!strcmp(argv[1], "-profile")) {
998 xflags |= VMXF_PROFILE;
1000 else if (!strcmp(argv[1], "-info")) {
1006 else if (!strcmp(argv[1], "-disasm")) {
1012 else if (!strcmp(argv[1], "-disasm-func")) {
1019 vec_push(dis_list, argv[1]);
1024 else if (!strcmp(argv[1], "-printdefs")) {
1027 opts_printdefs = true;
1030 else if (!strcmp(argv[1], "-printfuns")) {
1033 opts_printfuns = true;
1036 else if (!strcmp(argv[1], "-printfields")) {
1039 opts_printfields = true;
1042 else if (!strcmp(argv[1], "-vector") ||
1043 !strcmp(argv[1], "-string") ||
1044 !strcmp(argv[1], "-float") )
1047 if (argv[1][1] == 'f')
1048 p.vtype = TYPE_FLOAT;
1049 else if (argv[1][1] == 's')
1050 p.vtype = TYPE_STRING;
1051 else if (argv[1][1] == 'v')
1052 p.vtype = TYPE_VECTOR;
1054 p.vtype = TYPE_VOID;
1064 vec_push(main_params, p);
1068 else if (!strcmp(argv[1], "--")) {
1073 else if (argv[1][0] != '-') {
1075 fprintf(stderr, "only 1 program file may be specified\n");
1079 progsfile = argv[1];
1085 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1091 if (argc == 2 && !progsfile) {
1092 progsfile = argv[1];
1098 fprintf(stderr, "must specify a program to execute\n");
1103 prog = prog_load(progsfile, noexec);
1105 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1109 prog->builtins = qc_builtins;
1110 prog->builtins_count = qc_builtins_count;
1113 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1114 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1115 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1122 (unsigned long)vec_size(prog->code),
1123 (unsigned long)vec_size(prog->defs),
1124 (unsigned long)vec_size(prog->fields),
1125 (unsigned long)vec_size(prog->functions),
1126 (unsigned long)vec_size(prog->strings));
1133 for (i = 0; i < vec_size(dis_list); ++i) {
1135 printf("Looking for `%s`\n", dis_list[i]);
1136 for (k = 1; k < vec_size(prog->functions); ++k) {
1137 const char *name = prog_getstring(prog, prog->functions[k].name);
1138 if (!strcmp(name, dis_list[i])) {
1139 prog_disasm_function(prog, k);
1145 for (i = 1; i < vec_size(prog->functions); ++i)
1146 prog_disasm_function(prog, i);
1149 if (opts_printdefs) {
1150 char *escape = NULL;
1151 const char *getstring = NULL;
1153 for (i = 0; i < vec_size(prog->defs); ++i) {
1154 printf("Global: %8s %-16s at %u%s",
1155 type_name[prog->defs[i].type & DEF_TYPEMASK],
1156 prog_getstring(prog, prog->defs[i].name),
1157 (unsigned int)prog->defs[i].offset,
1158 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1160 switch (prog->defs[i].type & DEF_TYPEMASK) {
1162 printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
1165 printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1171 printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1174 getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
1175 escape = (char*)mem_a(strlen(getstring) * 2 + 1); /* will be enough */
1176 escapestring(escape, getstring);
1177 printf(" [init: `%s`]", escape);
1179 mem_d(escape); /* free */
1188 if (opts_printfields) {
1189 for (i = 0; i < vec_size(prog->fields); ++i) {
1190 printf("Field: %8s %-16s at %u%s\n",
1191 type_name[prog->fields[i].type],
1192 prog_getstring(prog, prog->fields[i].name),
1193 (unsigned int)prog->fields[i].offset,
1194 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1197 if (opts_printfuns) {
1198 for (i = 0; i < vec_size(prog->functions); ++i) {
1200 printf("Function: %-16s taking %i parameters:(",
1201 prog_getstring(prog, prog->functions[i].name),
1202 (unsigned int)prog->functions[i].nargs);
1203 for (a = 0; a < prog->functions[i].nargs; ++a) {
1204 printf(" %i", prog->functions[i].argsize[a]);
1207 int32_t start = prog->functions[i].entry;
1209 printf(") builtin %i\n", (int)-start);
1212 prog_section_statement_t *st = prog->code + start;
1213 for (;st->opcode != INSTR_DONE; ++st)
1215 printf(") - %lu instructions", (unsigned long)funsize);
1217 printf(" - locals: %i + %i\n",
1218 prog->functions[i].firstlocal,
1219 prog->functions[i].locals);
1226 printf(") locals: %i + %i\n",
1227 prog->functions[i].firstlocal,
1228 prog->functions[i].locals);
1235 for (i = 1; i < vec_size(prog->functions); ++i) {
1236 const char *name = prog_getstring(prog, prog->functions[i].name);
1237 if (!strcmp(name, "main"))
1238 fnmain = (qcint_t)i;
1242 prog_main_setparams(prog);
1243 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1246 fprintf(stderr, "No main function found\n");
1253 void prog_disasm_function(qc_program_t *prog, size_t id) {
1254 prog_section_function_t *fdef = prog->functions + id;
1255 prog_section_statement_t *st;
1257 if (fdef->entry < 0) {
1258 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1262 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1264 st = prog->code + fdef->entry;
1265 while (st->opcode != INSTR_DONE) {
1266 prog_print_statement(prog, st);
1271 #else /* !QCVM_LOOP */
1273 * Everything from here on is not including into the compilation of the
1274 * executor. This is simply code that is #included via #include __FILE__
1275 * see when QCVM_LOOP is defined, the rest of the code above do not get
1276 * re-included. So this really just acts like one large macro, but it
1277 * sort of isn't, which makes it nicer looking.
1280 #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
1281 #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
1282 #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
1284 #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
1286 /* to be consistent with current darkplaces behaviour */
1287 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1288 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1292 prog_section_function_t *newf;
1299 prog->profile[st - prog->code]++;
1303 prog_print_statement(prog, st);
1309 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1314 /* TODO: add instruction count to function profile count */
1315 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1316 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1317 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1319 st = prog->code + prog_leavefunction(prog);
1320 if (!vec_size(prog->stack))
1326 OPC->_float = OPA->_float * OPB->_float;
1329 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1330 OPA->vector[1]*OPB->vector[1] +
1331 OPA->vector[2]*OPB->vector[2];
1335 qcfloat_t f = OPA->_float;
1336 OPC->vector[0] = f * OPB->vector[0];
1337 OPC->vector[1] = f * OPB->vector[1];
1338 OPC->vector[2] = f * OPB->vector[2];
1343 qcfloat_t f = OPB->_float;
1344 OPC->vector[0] = f * OPA->vector[0];
1345 OPC->vector[1] = f * OPA->vector[1];
1346 OPC->vector[2] = f * OPA->vector[2];
1350 if (OPB->_float != 0.0f)
1351 OPC->_float = OPA->_float / OPB->_float;
1357 OPC->_float = OPA->_float + OPB->_float;
1360 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1361 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1362 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1365 OPC->_float = OPA->_float - OPB->_float;
1368 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1369 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1370 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1374 OPC->_float = (OPA->_float == OPB->_float);
1377 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1378 (OPA->vector[1] == OPB->vector[1]) &&
1379 (OPA->vector[2] == OPB->vector[2]) );
1382 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1383 prog_getstring(prog, OPB->string));
1386 OPC->_float = (OPA->_int == OPB->_int);
1389 OPC->_float = (OPA->function == OPB->function);
1392 OPC->_float = (OPA->_float != OPB->_float);
1395 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1396 (OPA->vector[1] != OPB->vector[1]) ||
1397 (OPA->vector[2] != OPB->vector[2]) );
1400 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1401 prog_getstring(prog, OPB->string));
1404 OPC->_float = (OPA->_int != OPB->_int);
1407 OPC->_float = (OPA->function != OPB->function);
1411 OPC->_float = (OPA->_float <= OPB->_float);
1414 OPC->_float = (OPA->_float >= OPB->_float);
1417 OPC->_float = (OPA->_float < OPB->_float);
1420 OPC->_float = (OPA->_float > OPB->_float);
1425 case INSTR_LOAD_FLD:
1426 case INSTR_LOAD_ENT:
1427 case INSTR_LOAD_FNC:
1428 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1429 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1432 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1433 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1438 ed = prog_getedict(prog, OPA->edict);
1439 OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
1442 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1443 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1446 if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
1448 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1453 ed = prog_getedict(prog, OPA->edict);
1454 ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
1455 OPC->ivector[0] = ptr->ivector[0];
1456 OPC->ivector[1] = ptr->ivector[1];
1457 OPC->ivector[2] = ptr->ivector[2];
1461 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1462 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1465 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1467 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1473 ed = prog_getedict(prog, OPA->edict);
1474 OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
1479 case INSTR_STORE_ENT:
1480 case INSTR_STORE_FLD:
1481 case INSTR_STORE_FNC:
1482 OPB->_int = OPA->_int;
1485 OPB->ivector[0] = OPA->ivector[0];
1486 OPB->ivector[1] = OPA->ivector[1];
1487 OPB->ivector[2] = OPA->ivector[2];
1490 case INSTR_STOREP_F:
1491 case INSTR_STOREP_S:
1492 case INSTR_STOREP_ENT:
1493 case INSTR_STOREP_FLD:
1494 case INSTR_STOREP_FNC:
1495 if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
1496 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1499 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1500 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1502 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1504 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1505 ptr->_int = OPA->_int;
1507 case INSTR_STOREP_V:
1508 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
1509 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1512 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1513 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1515 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1517 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1518 ptr->ivector[0] = OPA->ivector[0];
1519 ptr->ivector[1] = OPA->ivector[1];
1520 ptr->ivector[2] = OPA->ivector[2];
1524 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1527 OPC->_float = !OPA->vector[0] &&
1532 OPC->_float = !OPA->string ||
1533 !*prog_getstring(prog, OPA->string);
1536 OPC->_float = (OPA->edict == 0);
1539 OPC->_float = !OPA->function;
1543 /* this is consistent with darkplaces' behaviour */
1544 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1546 st += st->o2.s1 - 1; /* offset the s++ */
1547 if (++jumpcount >= maxjumps)
1548 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1552 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1554 st += st->o2.s1 - 1; /* offset the s++ */
1555 if (++jumpcount >= maxjumps)
1556 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1569 prog->argc = st->opcode - INSTR_CALL0;
1571 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1573 if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
1575 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1579 newf = &prog->functions[OPA->function];
1582 prog->statement = (st - prog->code) + 1;
1584 if (newf->entry < 0)
1586 /* negative statements are built in functions */
1587 qcint_t builtinnumber = -newf->entry;
1588 if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
1589 prog->builtins[builtinnumber](prog);
1591 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1592 builtinnumber, prog->filename);
1595 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1601 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1605 st += st->o1.s1 - 1; /* offset the s++ */
1606 if (++jumpcount == 10000000)
1607 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1611 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1612 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1615 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1616 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1620 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1623 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1630 #endif /* !QCVM_LOOP */