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
32 static void loaderror(const char *fmt, ...)
39 printf(": %s\n", util_strerror(err));
42 static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
54 qc_program_t* prog_load(const char *filename, bool skipversion)
58 fs_file_t *file = fs_file_open(filename, "rb");
63 if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
64 loaderror("failed to read header from '%s'", filename);
69 if (!skipversion && header.version != 6) {
70 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
75 prog = (qc_program_t*)mem_a(sizeof(qc_program_t));
78 fprintf(stderr, "failed to allocate program data\n");
81 memset(prog, 0, sizeof(*prog));
83 prog->entityfields = header.entfield;
84 prog->crc16 = header.crc16;
86 prog->filename = util_strdup(filename);
87 if (!prog->filename) {
88 loaderror("failed to store program name");
92 #define read_data(hdrvar, progvar, reserved) \
93 if (fs_file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
94 loaderror("seek failed"); \
98 vec_add(prog->progvar, header.hdrvar.length + reserved), \
99 sizeof(*prog->progvar), \
100 header.hdrvar.length, \
102 )!= header.hdrvar.length \
104 loaderror("read failed"); \
107 #define read_data1(x) read_data(x, x, 0)
108 #define read_data2(x, y) read_data(x, x, y)
110 read_data (statements, code, 0);
113 read_data1(functions);
115 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
119 /* profile counters */
120 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
122 /* Add tempstring area */
123 prog->tempstring_start = vec_size(prog->strings);
124 prog->tempstring_at = vec_size(prog->strings);
125 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
127 /* spawn the world entity */
128 vec_push(prog->entitypool, true);
129 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
136 mem_d(prog->filename);
137 vec_free(prog->code);
138 vec_free(prog->defs);
139 vec_free(prog->fields);
140 vec_free(prog->functions);
141 vec_free(prog->strings);
142 vec_free(prog->globals);
143 vec_free(prog->entitydata);
144 vec_free(prog->entitypool);
151 void prog_delete(qc_program_t *prog)
153 if (prog->filename) mem_d(prog->filename);
154 vec_free(prog->code);
155 vec_free(prog->defs);
156 vec_free(prog->fields);
157 vec_free(prog->functions);
158 vec_free(prog->strings);
159 vec_free(prog->globals);
160 vec_free(prog->entitydata);
161 vec_free(prog->entitypool);
162 vec_free(prog->localstack);
163 vec_free(prog->stack);
164 vec_free(prog->profile);
168 /***********************************************************************
172 const char* prog_getstring(qc_program_t *prog, qcint_t str) {
173 /* cast for return required for C++ */
174 if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
175 return "<<<invalid string>>>";
177 return prog->strings + str;
180 prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
182 for (i = 0; i < vec_size(prog->fields); ++i) {
183 if (prog->fields[i].offset == off)
184 return (prog->fields + i);
189 prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
192 for (i = 0; i < vec_size(prog->defs); ++i) {
193 if (prog->defs[i].offset == off)
194 return (prog->defs + i);
199 qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
200 if (e >= (qcint_t)vec_size(prog->entitypool)) {
202 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
205 return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
208 static qcint_t prog_spawn_entity(qc_program_t *prog) {
211 for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
212 if (!prog->entitypool[e]) {
213 data = (char*)(prog->entitydata + (prog->entityfields * e));
214 memset(data, 0, prog->entityfields * sizeof(qcint_t));
218 vec_push(prog->entitypool, true);
220 data = (char*)vec_add(prog->entitydata, prog->entityfields);
221 memset(data, 0, prog->entityfields * sizeof(qcint_t));
225 static void prog_free_entity(qc_program_t *prog, qcint_t e) {
228 fprintf(stderr, "Trying to free world entity\n");
231 if (e >= (qcint_t)vec_size(prog->entitypool)) {
233 fprintf(stderr, "Trying to free out of bounds entity\n");
236 if (!prog->entitypool[e]) {
238 fprintf(stderr, "Double free on entity\n");
241 prog->entitypool[e] = false;
244 qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
245 size_t len = strlen(str);
246 size_t at = prog->tempstring_at;
248 /* when we reach the end we start over */
249 if (at + len >= vec_size(prog->strings))
250 at = prog->tempstring_start;
252 /* when it doesn't fit, reallocate */
253 if (at + len >= vec_size(prog->strings))
255 (void)vec_add(prog->strings, len+1);
256 memcpy(prog->strings + at, str, len+1);
260 /* when it fits, just copy */
261 memcpy(prog->strings + at, str, len+1);
262 prog->tempstring_at += len+1;
266 static size_t print_escaped_string(const char *str, size_t maxlen) {
269 --maxlen; /* because we're lazy and have escape sequences */
279 case '\a': len += 2; putchar('\\'); putchar('a'); break;
280 case '\b': len += 2; putchar('\\'); putchar('b'); break;
281 case '\r': len += 2; putchar('\\'); putchar('r'); break;
282 case '\n': len += 2; putchar('\\'); putchar('n'); break;
283 case '\t': len += 2; putchar('\\'); putchar('t'); break;
284 case '\f': len += 2; putchar('\\'); putchar('f'); break;
285 case '\v': len += 2; putchar('\\'); putchar('v'); break;
286 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
287 case '"': len += 2; putchar('\\'); putchar('"'); break;
299 static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
300 static char spaces[28+1] = " ";
301 prog_section_def_t *def;
306 if ((len = printf("<null>,")) == -1)
312 def = prog_getdef(prog, glob);
313 value = (qcany_t*)(&prog->globals[glob]);
315 len = printf("[@%u] ", glob);
317 const char *name = prog_getstring(prog, def->name);
321 len += printf("%s ", name);
322 vtype = def->type & DEF_TYPEMASK;
331 len += printf("(%i),", value->_int);
334 len += printf("'%g %g %g',", value->vector[0],
340 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
342 len += printf("(null)");
344 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
348 len += printf("%g,", value->_float);
352 if (len < (int)sizeof(spaces)-1) {
353 spaces[sizeof(spaces)-1-len] = 0;
354 fs_file_puts((fs_file_t*)stdout, spaces);
355 spaces[sizeof(spaces)-1-len] = ' ';
359 static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
360 if (st->opcode >= VINSTR_END) {
361 printf("<illegal instruction %d>\n", st->opcode);
364 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
366 for (i = 0; i < vec_size(prog->function_stack); ++i)
368 printf("%s:", vec_last(prog->function_stack));
370 printf(" <> %-12s", util_instr_str[st->opcode]);
371 if (st->opcode >= INSTR_IF &&
372 st->opcode <= INSTR_IFNOT)
374 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
375 printf("%d\n", st->o2.s1);
377 else if (st->opcode >= INSTR_CALL0 &&
378 st->opcode <= INSTR_CALL8)
380 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
383 else if (st->opcode == INSTR_GOTO)
385 printf("%i\n", st->o1.s1);
389 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
393 t[1] = t[2] = TYPE_VECTOR;
396 t[0] = t[2] = TYPE_VECTOR;
399 t[0] = t[1] = TYPE_VECTOR;
405 t[0] = t[1] = t[2] = TYPE_VECTOR;
409 t[0] = t[1] = TYPE_STRING;
416 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
419 t[0] = t[1] = TYPE_STRING; t[2] = -1;
421 case INSTR_STORE_ENT:
422 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
424 case INSTR_STORE_FLD:
425 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
427 case INSTR_STORE_FNC:
428 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
431 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
434 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
436 case INSTR_STOREP_ENT:
437 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
439 case INSTR_STOREP_FLD:
440 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
442 case INSTR_STOREP_FNC:
443 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
446 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
447 else printf("(none), ");
448 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
449 else printf("(none), ");
450 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
451 else printf("(none)");
456 static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
462 st.localsp = vec_size(prog->localstack);
463 st.stmt = prog->statement;
466 if (prog->xflags & VMXF_TRACE) {
467 const char *str = prog_getstring(prog, func->name);
468 vec_push(prog->function_stack, str);
471 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
472 if (vec_size(prog->stack))
474 prog_section_function_t *cur;
475 cur = prog->stack[vec_size(prog->stack)-1].function;
478 qcint_t *globals = prog->globals + cur->firstlocal;
479 vec_append(prog->localstack, cur->locals, globals);
484 qcint_t *globals = prog->globals + func->firstlocal;
485 vec_append(prog->localstack, func->locals, globals);
489 /* copy parameters */
490 parampos = func->firstlocal;
491 for (p = 0; p < func->nargs; ++p)
494 for (s = 0; s < func->argsize[p]; ++s) {
495 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
500 vec_push(prog->stack, st);
505 static qcint_t prog_leavefunction(qc_program_t *prog) {
506 prog_section_function_t *prev = NULL;
509 qc_exec_stack_t st = vec_last(prog->stack);
511 if (prog->xflags & VMXF_TRACE) {
512 if (vec_size(prog->function_stack))
513 vec_pop(prog->function_stack);
516 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
517 if (vec_size(prog->stack) > 1) {
518 prev = prog->stack[vec_size(prog->stack)-2].function;
519 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
522 prev = prog->stack[vec_size(prog->stack)-1].function;
523 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
526 qcint_t *globals = prog->globals + prev->firstlocal;
527 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
528 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
529 vec_shrinkto(prog->localstack, oldsp);
532 vec_pop(prog->stack);
534 return st.stmt - 1; /* offset the ++st */
537 bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
539 size_t oldxflags = prog->xflags;
540 prog_section_statement_t *st;
543 prog->xflags = flags;
545 st = prog->code + prog_enterfunction(prog, func);
553 #define QCVM_PROFILE 0
559 #define QCVM_PROFILE 0
565 #define QCVM_PROFILE 1
569 case (VMXF_TRACE|VMXF_PROFILE):
571 #define QCVM_PROFILE 1
578 prog->xflags = oldxflags;
579 vec_free(prog->localstack);
580 vec_free(prog->stack);
586 /***********************************************************************
587 * main for when building the standalone 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_t*)(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_t *prog) {
635 const char *laststr = NULL;
636 for (i = 0; i < (size_t)prog->argc; ++i) {
637 qcany_t *str = (qcany_t*)(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_t *prog) {
650 fprintf(stderr, "*** VM raised an error:\n");
656 static int qc_ftos(qc_program_t *prog) {
662 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
663 str.string = prog_tempstring(prog, buffer);
668 static int qc_stof(qc_program_t *prog) {
673 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
678 static int qc_vtos(qc_program_t *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_t *prog) {
696 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
697 str.string = prog_tempstring(prog, buffer);
702 static int qc_spawn(qc_program_t *prog) {
705 ent.edict = prog_spawn_entity(prog);
707 return (ent.edict ? 0 : -1);
710 static int qc_kill(qc_program_t *prog) {
714 prog_free_entity(prog, ent->edict);
718 static int qc_sqrt(qc_program_t *prog) {
722 out._float = sqrt(num->_float);
727 static int qc_vlen(qc_program_t *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_t *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_t *prog) {
761 qcany_t *str1, *str2;
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_t *prog) {
784 qcany_t *str1, *str2;
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_t *prog) {
812 out._float = floor(num->_float);
817 static prog_builtin_t qc_builtins[] = {
830 &qc_normalize, /* 12 */
835 static const char *arg0 = NULL;
837 static void version(void) {
838 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
847 static void usage(void) {
848 printf("usage: %s [options] [parameters] file\n", arg0);
849 printf("options:\n");
850 printf(" -h, --help print this message\n"
851 " -trace trace the execution\n"
852 " -profile perform profiling during execution\n"
853 " -info print information from the prog's header\n"
854 " -disasm disassemble and exit\n"
855 " -disasm-func func disassemble and exit\n"
856 " -printdefs list the defs section\n"
857 " -printfields list the field section\n"
858 " -printfuns list functions information\n"
860 " -vv be even more verbose\n");
861 printf("parameters:\n");
862 printf(" -vector <V> pass a vector parameter to main()\n"
863 " -float <f> pass a float parameter to main()\n"
864 " -string <s> pass a string parameter to main() \n");
867 static void prog_main_setparams(qc_program_t *prog) {
871 for (i = 0; i < vec_size(main_params); ++i) {
872 arg = GetGlobal(OFS_PARM0 + 3*i);
876 switch (main_params[i].vtype) {
878 (void)util_sscanf(main_params[i].value, " %f %f %f ",
884 arg->_float = atof(main_params[i].value);
887 arg->string = prog_tempstring(prog, main_params[i].value);
890 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
896 void prog_disasm_function(qc_program_t *prog, size_t id);
898 int main(int argc, char **argv) {
902 size_t xflags = VMXF_DEFAULT;
903 bool opts_printfields = false;
904 bool opts_printdefs = false;
905 bool opts_printfuns = false;
906 bool opts_disasm = false;
907 bool opts_info = false;
909 const char *progsfile = NULL;
910 const char **dis_list = NULL;
921 if (!strcmp(argv[1], "-h") ||
922 !strcmp(argv[1], "-help") ||
923 !strcmp(argv[1], "--help"))
928 else if (!strcmp(argv[1], "-v")) {
933 else if (!strncmp(argv[1], "-vv", 3)) {
934 const char *av = argv[1]+1;
946 else if (!strcmp(argv[1], "-version") ||
947 !strcmp(argv[1], "--version"))
952 else if (!strcmp(argv[1], "-trace")) {
955 xflags |= VMXF_TRACE;
957 else if (!strcmp(argv[1], "-profile")) {
960 xflags |= VMXF_PROFILE;
962 else if (!strcmp(argv[1], "-info")) {
968 else if (!strcmp(argv[1], "-disasm")) {
974 else if (!strcmp(argv[1], "-disasm-func")) {
981 vec_push(dis_list, argv[1]);
986 else if (!strcmp(argv[1], "-printdefs")) {
989 opts_printdefs = true;
992 else if (!strcmp(argv[1], "-printfuns")) {
995 opts_printfuns = true;
998 else if (!strcmp(argv[1], "-printfields")) {
1001 opts_printfields = true;
1004 else if (!strcmp(argv[1], "-vector") ||
1005 !strcmp(argv[1], "-string") ||
1006 !strcmp(argv[1], "-float") )
1009 if (argv[1][1] == 'f')
1010 p.vtype = TYPE_FLOAT;
1011 else if (argv[1][1] == 's')
1012 p.vtype = TYPE_STRING;
1013 else if (argv[1][1] == 'v')
1014 p.vtype = TYPE_VECTOR;
1016 p.vtype = TYPE_VOID;
1026 vec_push(main_params, p);
1030 else if (!strcmp(argv[1], "--")) {
1035 else if (argv[1][0] != '-') {
1037 fprintf(stderr, "only 1 program file may be specified\n");
1041 progsfile = argv[1];
1047 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1053 if (argc == 2 && !progsfile) {
1054 progsfile = argv[1];
1060 fprintf(stderr, "must specify a program to execute\n");
1065 prog = prog_load(progsfile, noexec);
1067 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1071 prog->builtins = qc_builtins;
1072 prog->builtins_count = GMQCC_ARRAY_COUNT(qc_builtins);
1075 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1076 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1077 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1084 (unsigned long)vec_size(prog->code),
1085 (unsigned long)vec_size(prog->defs),
1086 (unsigned long)vec_size(prog->fields),
1087 (unsigned long)vec_size(prog->functions),
1088 (unsigned long)vec_size(prog->strings));
1095 for (i = 0; i < vec_size(dis_list); ++i) {
1097 printf("Looking for `%s`\n", dis_list[i]);
1098 for (k = 1; k < vec_size(prog->functions); ++k) {
1099 const char *name = prog_getstring(prog, prog->functions[k].name);
1100 if (!strcmp(name, dis_list[i])) {
1101 prog_disasm_function(prog, k);
1107 for (i = 1; i < vec_size(prog->functions); ++i)
1108 prog_disasm_function(prog, i);
1111 if (opts_printdefs) {
1112 const char *getstring = NULL;
1113 for (i = 0; i < vec_size(prog->defs); ++i) {
1114 printf("Global: %8s %-16s at %u%s",
1115 type_name[prog->defs[i].type & DEF_TYPEMASK],
1116 prog_getstring(prog, prog->defs[i].name),
1117 (unsigned int)prog->defs[i].offset,
1118 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1120 switch (prog->defs[i].type & DEF_TYPEMASK) {
1122 printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
1125 printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1131 printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1134 getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
1135 printf(" [init: `");
1136 print_escaped_string(getstring, strlen(getstring));
1146 if (opts_printfields) {
1147 for (i = 0; i < vec_size(prog->fields); ++i) {
1148 printf("Field: %8s %-16s at %u%s\n",
1149 type_name[prog->fields[i].type],
1150 prog_getstring(prog, prog->fields[i].name),
1151 (unsigned int)prog->fields[i].offset,
1152 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1155 if (opts_printfuns) {
1156 for (i = 0; i < vec_size(prog->functions); ++i) {
1158 printf("Function: %-16s taking %u parameters:(",
1159 prog_getstring(prog, prog->functions[i].name),
1160 (unsigned int)prog->functions[i].nargs);
1161 for (a = 0; a < prog->functions[i].nargs; ++a) {
1162 printf(" %i", prog->functions[i].argsize[a]);
1165 int32_t start = prog->functions[i].entry;
1167 printf(") builtin %i\n", (int)-start);
1170 prog_section_statement_t *st = prog->code + start;
1171 for (;st->opcode != INSTR_DONE; ++st)
1173 printf(") - %lu instructions", (unsigned long)funsize);
1175 printf(" - locals: %i + %i\n",
1176 prog->functions[i].firstlocal,
1177 prog->functions[i].locals);
1184 printf(") locals: %i + %i\n",
1185 prog->functions[i].firstlocal,
1186 prog->functions[i].locals);
1193 for (i = 1; i < vec_size(prog->functions); ++i) {
1194 const char *name = prog_getstring(prog, prog->functions[i].name);
1195 if (!strcmp(name, "main"))
1196 fnmain = (qcint_t)i;
1200 prog_main_setparams(prog);
1201 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1204 fprintf(stderr, "No main function found\n");
1211 void prog_disasm_function(qc_program_t *prog, size_t id) {
1212 prog_section_function_t *fdef = prog->functions + id;
1213 prog_section_statement_t *st;
1215 if (fdef->entry < 0) {
1216 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1220 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1222 st = prog->code + fdef->entry;
1223 while (st->opcode != INSTR_DONE) {
1224 prog_print_statement(prog, st);
1228 #else /* !QCVM_LOOP */
1230 * Everything from here on is not including into the compilation of the
1231 * executor. This is simply code that is #included via #include __FILE__
1232 * see when QCVM_LOOP is defined, the rest of the code above do not get
1233 * re-included. So this really just acts like one large macro, but it
1234 * sort of isn't, which makes it nicer looking.
1237 #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
1238 #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
1239 #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
1241 #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
1243 /* to be consistent with current darkplaces behaviour */
1244 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1245 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1248 while (prog->vmerror == 0) {
1249 prog_section_function_t *newf;
1256 prog->profile[st - prog->code]++;
1260 prog_print_statement(prog, st);
1266 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1271 /* TODO: add instruction count to function profile count */
1272 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1273 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1274 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1276 st = prog->code + prog_leavefunction(prog);
1277 if (!vec_size(prog->stack))
1283 OPC->_float = OPA->_float * OPB->_float;
1286 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1287 OPA->vector[1]*OPB->vector[1] +
1288 OPA->vector[2]*OPB->vector[2];
1292 qcfloat_t f = OPA->_float;
1293 OPC->vector[0] = f * OPB->vector[0];
1294 OPC->vector[1] = f * OPB->vector[1];
1295 OPC->vector[2] = f * OPB->vector[2];
1300 qcfloat_t f = OPB->_float;
1301 OPC->vector[0] = f * OPA->vector[0];
1302 OPC->vector[1] = f * OPA->vector[1];
1303 OPC->vector[2] = f * OPA->vector[2];
1307 if (OPB->_float != 0.0f)
1308 OPC->_float = OPA->_float / OPB->_float;
1314 OPC->_float = OPA->_float + OPB->_float;
1317 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1318 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1319 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1322 OPC->_float = OPA->_float - OPB->_float;
1325 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1326 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1327 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1331 OPC->_float = (OPA->_float == OPB->_float);
1334 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1335 (OPA->vector[1] == OPB->vector[1]) &&
1336 (OPA->vector[2] == OPB->vector[2]) );
1339 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1340 prog_getstring(prog, OPB->string));
1343 OPC->_float = (OPA->_int == OPB->_int);
1346 OPC->_float = (OPA->function == OPB->function);
1349 OPC->_float = (OPA->_float != OPB->_float);
1352 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1353 (OPA->vector[1] != OPB->vector[1]) ||
1354 (OPA->vector[2] != OPB->vector[2]) );
1357 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1358 prog_getstring(prog, OPB->string));
1361 OPC->_float = (OPA->_int != OPB->_int);
1364 OPC->_float = (OPA->function != OPB->function);
1368 OPC->_float = (OPA->_float <= OPB->_float);
1371 OPC->_float = (OPA->_float >= OPB->_float);
1374 OPC->_float = (OPA->_float < OPB->_float);
1377 OPC->_float = (OPA->_float > OPB->_float);
1382 case INSTR_LOAD_FLD:
1383 case INSTR_LOAD_ENT:
1384 case INSTR_LOAD_FNC:
1385 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1386 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1389 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1390 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1395 ed = prog_getedict(prog, OPA->edict);
1396 OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
1399 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1400 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1403 if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
1405 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1410 ed = prog_getedict(prog, OPA->edict);
1411 ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
1412 OPC->ivector[0] = ptr->ivector[0];
1413 OPC->ivector[1] = ptr->ivector[1];
1414 OPC->ivector[2] = ptr->ivector[2];
1418 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1419 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1422 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1424 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1430 ed = prog_getedict(prog, OPA->edict);
1431 OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
1436 case INSTR_STORE_ENT:
1437 case INSTR_STORE_FLD:
1438 case INSTR_STORE_FNC:
1439 OPB->_int = OPA->_int;
1442 OPB->ivector[0] = OPA->ivector[0];
1443 OPB->ivector[1] = OPA->ivector[1];
1444 OPB->ivector[2] = OPA->ivector[2];
1447 case INSTR_STOREP_F:
1448 case INSTR_STOREP_S:
1449 case INSTR_STOREP_ENT:
1450 case INSTR_STOREP_FLD:
1451 case INSTR_STOREP_FNC:
1452 if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
1453 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1456 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1457 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1459 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1461 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1462 ptr->_int = OPA->_int;
1464 case INSTR_STOREP_V:
1465 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
1466 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1469 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1470 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1472 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1474 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1475 ptr->ivector[0] = OPA->ivector[0];
1476 ptr->ivector[1] = OPA->ivector[1];
1477 ptr->ivector[2] = OPA->ivector[2];
1481 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1484 OPC->_float = !OPA->vector[0] &&
1489 OPC->_float = !OPA->string ||
1490 !*prog_getstring(prog, OPA->string);
1493 OPC->_float = (OPA->edict == 0);
1496 OPC->_float = !OPA->function;
1500 /* this is consistent with darkplaces' behaviour */
1501 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1503 st += st->o2.s1 - 1; /* offset the s++ */
1504 if (++jumpcount >= maxjumps)
1505 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1509 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1511 st += st->o2.s1 - 1; /* offset the s++ */
1512 if (++jumpcount >= maxjumps)
1513 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1526 prog->argc = st->opcode - INSTR_CALL0;
1528 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1530 if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
1532 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1536 newf = &prog->functions[OPA->function];
1539 prog->statement = (st - prog->code) + 1;
1541 if (newf->entry < 0)
1543 /* negative statements are built in functions */
1544 qcint_t builtinnumber = -newf->entry;
1545 if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
1546 prog->builtins[builtinnumber](prog);
1548 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1549 builtinnumber, prog->filename);
1552 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1558 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1562 st += st->o1.s1 - 1; /* offset the s++ */
1563 if (++jumpcount == 10000000)
1564 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1568 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1569 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1572 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1573 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1577 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1580 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1587 #endif /* !QCVM_LOOP */