2 * Copyright (C) 2012, 2013, 2014
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 static void loaderror(const char *fmt, ...)
39 printf(": %s\n", util_strerror(err));
42 static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
54 qc_program_t* prog_load(const char *filename, bool skipversion)
58 fs_file_t *file = fs_file_open(filename, "rb");
63 if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
64 loaderror("failed to read header from '%s'", filename);
69 util_swap_header(&header);
71 if (!skipversion && header.version != 6) {
72 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
77 prog = (qc_program_t*)mem_a(sizeof(qc_program_t));
80 fprintf(stderr, "failed to allocate program data\n");
83 memset(prog, 0, sizeof(*prog));
85 prog->entityfields = header.entfield;
86 prog->crc16 = header.crc16;
88 prog->filename = util_strdup(filename);
89 if (!prog->filename) {
90 loaderror("failed to store program name");
94 #define read_data(hdrvar, progvar, reserved) \
95 if (fs_file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
96 loaderror("seek failed"); \
100 vec_add(prog->progvar, header.hdrvar.length + reserved), \
101 sizeof(*prog->progvar), \
102 header.hdrvar.length, \
104 )!= header.hdrvar.length \
106 loaderror("read failed"); \
109 #define read_data1(x) read_data(x, x, 0)
110 #define read_data2(x, y) read_data(x, x, y)
112 read_data (statements, code, 0);
115 read_data1(functions);
117 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
119 util_swap_statements (prog->code);
120 util_swap_defs_fields(prog->defs);
121 util_swap_defs_fields(prog->fields);
122 util_swap_functions (prog->functions);
123 util_swap_globals (prog->globals);
127 /* profile counters */
128 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
130 /* Add tempstring area */
131 prog->tempstring_start = vec_size(prog->strings);
132 prog->tempstring_at = vec_size(prog->strings);
133 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
135 /* spawn the world entity */
136 vec_push(prog->entitypool, true);
137 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
144 mem_d(prog->filename);
145 vec_free(prog->code);
146 vec_free(prog->defs);
147 vec_free(prog->fields);
148 vec_free(prog->functions);
149 vec_free(prog->strings);
150 vec_free(prog->globals);
151 vec_free(prog->entitydata);
152 vec_free(prog->entitypool);
159 void prog_delete(qc_program_t *prog)
161 if (prog->filename) mem_d(prog->filename);
162 vec_free(prog->code);
163 vec_free(prog->defs);
164 vec_free(prog->fields);
165 vec_free(prog->functions);
166 vec_free(prog->strings);
167 vec_free(prog->globals);
168 vec_free(prog->entitydata);
169 vec_free(prog->entitypool);
170 vec_free(prog->localstack);
171 vec_free(prog->stack);
172 vec_free(prog->profile);
176 /***********************************************************************
180 const char* prog_getstring(qc_program_t *prog, qcint_t str) {
181 /* cast for return required for C++ */
182 if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
183 return "<<<invalid string>>>";
185 return prog->strings + str;
188 prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
190 for (i = 0; i < vec_size(prog->fields); ++i) {
191 if (prog->fields[i].offset == off)
192 return (prog->fields + i);
197 prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
200 for (i = 0; i < vec_size(prog->defs); ++i) {
201 if (prog->defs[i].offset == off)
202 return (prog->defs + i);
207 qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
208 if (e >= (qcint_t)vec_size(prog->entitypool)) {
210 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
213 return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
216 static qcint_t prog_spawn_entity(qc_program_t *prog) {
219 for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
220 if (!prog->entitypool[e]) {
221 data = (char*)(prog->entitydata + (prog->entityfields * e));
222 memset(data, 0, prog->entityfields * sizeof(qcint_t));
226 vec_push(prog->entitypool, true);
228 data = (char*)vec_add(prog->entitydata, prog->entityfields);
229 memset(data, 0, prog->entityfields * sizeof(qcint_t));
233 static void prog_free_entity(qc_program_t *prog, qcint_t e) {
236 fprintf(stderr, "Trying to free world entity\n");
239 if (e >= (qcint_t)vec_size(prog->entitypool)) {
241 fprintf(stderr, "Trying to free out of bounds entity\n");
244 if (!prog->entitypool[e]) {
246 fprintf(stderr, "Double free on entity\n");
249 prog->entitypool[e] = false;
252 qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
253 size_t len = strlen(str);
254 size_t at = prog->tempstring_at;
256 /* when we reach the end we start over */
257 if (at + len >= vec_size(prog->strings))
258 at = prog->tempstring_start;
260 /* when it doesn't fit, reallocate */
261 if (at + len >= vec_size(prog->strings))
263 (void)vec_add(prog->strings, len+1);
264 memcpy(prog->strings + at, str, len+1);
268 /* when it fits, just copy */
269 memcpy(prog->strings + at, str, len+1);
270 prog->tempstring_at += len+1;
274 static size_t print_escaped_string(const char *str, size_t maxlen) {
277 --maxlen; /* because we're lazy and have escape sequences */
287 case '\a': len += 2; putchar('\\'); putchar('a'); break;
288 case '\b': len += 2; putchar('\\'); putchar('b'); break;
289 case '\r': len += 2; putchar('\\'); putchar('r'); break;
290 case '\n': len += 2; putchar('\\'); putchar('n'); break;
291 case '\t': len += 2; putchar('\\'); putchar('t'); break;
292 case '\f': len += 2; putchar('\\'); putchar('f'); break;
293 case '\v': len += 2; putchar('\\'); putchar('v'); break;
294 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
295 case '"': len += 2; putchar('\\'); putchar('"'); break;
307 static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
308 static char spaces[28+1] = " ";
309 prog_section_def_t *def;
314 if ((len = printf("<null>,")) == -1)
320 def = prog_getdef(prog, glob);
321 value = (qcany_t*)(&prog->globals[glob]);
323 len = printf("[@%u] ", glob);
325 const char *name = prog_getstring(prog, def->name);
329 len += printf("%s ", name);
330 vtype = def->type & DEF_TYPEMASK;
339 len += printf("(%i),", value->_int);
342 len += printf("'%g %g %g',", value->vector[0],
348 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
350 len += printf("(null)");
352 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
356 len += printf("%g,", value->_float);
360 if (len < (int)sizeof(spaces)-1) {
361 spaces[sizeof(spaces)-1-len] = 0;
362 fs_file_puts((fs_file_t*)stdout, spaces);
363 spaces[sizeof(spaces)-1-len] = ' ';
367 static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
368 if (st->opcode >= VINSTR_END) {
369 printf("<illegal instruction %d>\n", st->opcode);
372 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
374 for (i = 0; i < vec_size(prog->function_stack); ++i)
376 printf("%s:", vec_last(prog->function_stack));
378 printf(" <> %-12s", util_instr_str[st->opcode]);
379 if (st->opcode >= INSTR_IF &&
380 st->opcode <= INSTR_IFNOT)
382 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
383 printf("%d\n", st->o2.s1);
385 else if (st->opcode >= INSTR_CALL0 &&
386 st->opcode <= INSTR_CALL8)
388 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
391 else if (st->opcode == INSTR_GOTO)
393 printf("%i\n", st->o1.s1);
397 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
401 t[1] = t[2] = TYPE_VECTOR;
404 t[0] = t[2] = TYPE_VECTOR;
407 t[0] = t[1] = TYPE_VECTOR;
413 t[0] = t[1] = t[2] = TYPE_VECTOR;
417 t[0] = t[1] = TYPE_STRING;
424 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
427 t[0] = t[1] = TYPE_STRING; t[2] = -1;
429 case INSTR_STORE_ENT:
430 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
432 case INSTR_STORE_FLD:
433 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
435 case INSTR_STORE_FNC:
436 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
439 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
442 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
444 case INSTR_STOREP_ENT:
445 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
447 case INSTR_STOREP_FLD:
448 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
450 case INSTR_STOREP_FNC:
451 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
454 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
455 else printf("(none), ");
456 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
457 else printf("(none), ");
458 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
459 else printf("(none)");
464 static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
470 st.localsp = vec_size(prog->localstack);
471 st.stmt = prog->statement;
474 if (prog->xflags & VMXF_TRACE) {
475 const char *str = prog_getstring(prog, func->name);
476 vec_push(prog->function_stack, str);
479 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
480 if (vec_size(prog->stack))
482 prog_section_function_t *cur;
483 cur = prog->stack[vec_size(prog->stack)-1].function;
486 qcint_t *globals = prog->globals + cur->firstlocal;
487 vec_append(prog->localstack, cur->locals, globals);
492 qcint_t *globals = prog->globals + func->firstlocal;
493 vec_append(prog->localstack, func->locals, globals);
497 /* copy parameters */
498 parampos = func->firstlocal;
499 for (p = 0; p < func->nargs; ++p)
502 for (s = 0; s < func->argsize[p]; ++s) {
503 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
508 vec_push(prog->stack, st);
513 static qcint_t prog_leavefunction(qc_program_t *prog) {
514 prog_section_function_t *prev = NULL;
517 qc_exec_stack_t st = vec_last(prog->stack);
519 if (prog->xflags & VMXF_TRACE) {
520 if (vec_size(prog->function_stack))
521 vec_pop(prog->function_stack);
524 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
525 if (vec_size(prog->stack) > 1) {
526 prev = prog->stack[vec_size(prog->stack)-2].function;
527 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
530 prev = prog->stack[vec_size(prog->stack)-1].function;
531 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
534 qcint_t *globals = prog->globals + prev->firstlocal;
535 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
536 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
537 vec_shrinkto(prog->localstack, oldsp);
540 vec_pop(prog->stack);
542 return st.stmt - 1; /* offset the ++st */
545 bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
547 size_t oldxflags = prog->xflags;
548 prog_section_statement_t *st;
551 prog->xflags = flags;
553 st = prog->code + prog_enterfunction(prog, func);
561 #define QCVM_PROFILE 0
567 #define QCVM_PROFILE 0
573 #define QCVM_PROFILE 1
577 case (VMXF_TRACE|VMXF_PROFILE):
579 #define QCVM_PROFILE 1
586 prog->xflags = oldxflags;
587 vec_free(prog->localstack);
588 vec_free(prog->stack);
594 /***********************************************************************
595 * main for when building the standalone executor
600 const char *type_name[TYPE_COUNT] = {
626 static qcvm_parameter *main_params = NULL;
628 #define CheckArgs(num) do { \
629 if (prog->argc != (num)) { \
631 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
632 __FUNCTION__, prog->argc, (num)); \
637 #define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
638 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
639 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
641 static int qc_print(qc_program_t *prog) {
643 const char *laststr = NULL;
644 for (i = 0; i < (size_t)prog->argc; ++i) {
645 qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
646 laststr = prog_getstring(prog, str->string);
647 printf("%s", laststr);
649 if (laststr && (prog->xflags & VMXF_TRACE)) {
650 size_t len = strlen(laststr);
651 if (!len || laststr[len-1] != '\n')
657 static int qc_error(qc_program_t *prog) {
658 fprintf(stderr, "*** VM raised an error:\n");
664 static int qc_ftos(qc_program_t *prog) {
670 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
671 str.string = prog_tempstring(prog, buffer);
676 static int qc_stof(qc_program_t *prog) {
681 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
686 static int qc_vtos(qc_program_t *prog) {
692 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
693 str.string = prog_tempstring(prog, buffer);
698 static int qc_etos(qc_program_t *prog) {
704 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
705 str.string = prog_tempstring(prog, buffer);
710 static int qc_spawn(qc_program_t *prog) {
713 ent.edict = prog_spawn_entity(prog);
715 return (ent.edict ? 0 : -1);
718 static int qc_kill(qc_program_t *prog) {
722 prog_free_entity(prog, ent->edict);
726 static int qc_sqrt(qc_program_t *prog) {
730 out._float = sqrt(num->_float);
735 static int qc_vlen(qc_program_t *prog) {
739 len._float = sqrt(vec->vector[0] * vec->vector[0] +
740 vec->vector[1] * vec->vector[1] +
741 vec->vector[2] * vec->vector[2]);
746 static int qc_normalize(qc_program_t *prog) {
752 len = sqrt(vec->vector[0] * vec->vector[0] +
753 vec->vector[1] * vec->vector[1] +
754 vec->vector[2] * vec->vector[2]);
759 out.vector[0] = len * vec->vector[0];
760 out.vector[1] = len * vec->vector[1];
761 out.vector[2] = len * vec->vector[2];
766 static int qc_strcat(qc_program_t *prog) {
769 qcany_t *str1, *str2;
778 cstr1 = prog_getstring(prog, str1->string);
779 cstr2 = prog_getstring(prog, str2->string);
780 len1 = strlen(cstr1);
781 len2 = strlen(cstr2);
782 buffer = (char*)mem_a(len1 + len2 + 1);
783 memcpy(buffer, cstr1, len1);
784 memcpy(buffer+len1, cstr2, len2+1);
785 out.string = prog_tempstring(prog, buffer);
791 static int qc_strcmp(qc_program_t *prog) {
792 qcany_t *str1, *str2;
798 if (prog->argc != 2 && prog->argc != 3) {
799 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
806 cstr1 = prog_getstring(prog, str1->string);
807 cstr2 = prog_getstring(prog, str2->string);
809 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
811 out._float = strcmp(cstr1, cstr2);
816 static int qc_floor(qc_program_t *prog) {
820 out._float = floor(num->_float);
825 static int qc_pow(qc_program_t *prog) {
826 qcany_t *base, *exp, out;
830 out._float = powf(base->_float, exp->_float);
835 static prog_builtin_t qc_builtins[] = {
848 &qc_normalize, /* 12 */
854 static const char *arg0 = NULL;
856 static void version(void) {
857 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
866 static void usage(void) {
867 printf("usage: %s [options] [parameters] file\n", arg0);
868 printf("options:\n");
869 printf(" -h, --help print this message\n"
870 " -trace trace the execution\n"
871 " -profile perform profiling during execution\n"
872 " -info print information from the prog's header\n"
873 " -disasm disassemble and exit\n"
874 " -disasm-func func disassemble and exit\n"
875 " -printdefs list the defs section\n"
876 " -printfields list the field section\n"
877 " -printfuns list functions information\n"
879 " -vv be even more verbose\n");
880 printf("parameters:\n");
881 printf(" -vector <V> pass a vector parameter to main()\n"
882 " -float <f> pass a float parameter to main()\n"
883 " -string <s> pass a string parameter to main() \n");
886 static void prog_main_setparams(qc_program_t *prog) {
890 for (i = 0; i < vec_size(main_params); ++i) {
891 arg = GetGlobal(OFS_PARM0 + 3*i);
895 switch (main_params[i].vtype) {
897 (void)util_sscanf(main_params[i].value, " %f %f %f ",
903 arg->_float = atof(main_params[i].value);
906 arg->string = prog_tempstring(prog, main_params[i].value);
909 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
915 void prog_disasm_function(qc_program_t *prog, size_t id);
917 int main(int argc, char **argv) {
921 size_t xflags = VMXF_DEFAULT;
922 bool opts_printfields = false;
923 bool opts_printdefs = false;
924 bool opts_printfuns = false;
925 bool opts_disasm = false;
926 bool opts_info = false;
928 const char *progsfile = NULL;
929 const char **dis_list = NULL;
940 if (!strcmp(argv[1], "-h") ||
941 !strcmp(argv[1], "-help") ||
942 !strcmp(argv[1], "--help"))
947 else if (!strcmp(argv[1], "-v")) {
952 else if (!strncmp(argv[1], "-vv", 3)) {
953 const char *av = argv[1]+1;
965 else if (!strcmp(argv[1], "-version") ||
966 !strcmp(argv[1], "--version"))
971 else if (!strcmp(argv[1], "-trace")) {
974 xflags |= VMXF_TRACE;
976 else if (!strcmp(argv[1], "-profile")) {
979 xflags |= VMXF_PROFILE;
981 else if (!strcmp(argv[1], "-info")) {
987 else if (!strcmp(argv[1], "-disasm")) {
993 else if (!strcmp(argv[1], "-disasm-func")) {
1000 vec_push(dis_list, argv[1]);
1005 else if (!strcmp(argv[1], "-printdefs")) {
1008 opts_printdefs = true;
1011 else if (!strcmp(argv[1], "-printfuns")) {
1014 opts_printfuns = true;
1017 else if (!strcmp(argv[1], "-printfields")) {
1020 opts_printfields = true;
1023 else if (!strcmp(argv[1], "-vector") ||
1024 !strcmp(argv[1], "-string") ||
1025 !strcmp(argv[1], "-float") )
1028 if (argv[1][1] == 'f')
1029 p.vtype = TYPE_FLOAT;
1030 else if (argv[1][1] == 's')
1031 p.vtype = TYPE_STRING;
1032 else if (argv[1][1] == 'v')
1033 p.vtype = TYPE_VECTOR;
1035 p.vtype = TYPE_VOID;
1045 vec_push(main_params, p);
1049 else if (!strcmp(argv[1], "--")) {
1054 else if (argv[1][0] != '-') {
1056 fprintf(stderr, "only 1 program file may be specified\n");
1060 progsfile = argv[1];
1066 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1072 if (argc == 2 && !progsfile) {
1073 progsfile = argv[1];
1079 fprintf(stderr, "must specify a program to execute\n");
1084 prog = prog_load(progsfile, noexec);
1086 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1090 prog->builtins = qc_builtins;
1091 prog->builtins_count = GMQCC_ARRAY_COUNT(qc_builtins);
1094 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1095 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1096 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1103 (unsigned long)vec_size(prog->code),
1104 (unsigned long)vec_size(prog->defs),
1105 (unsigned long)vec_size(prog->fields),
1106 (unsigned long)vec_size(prog->functions),
1107 (unsigned long)vec_size(prog->strings));
1114 for (i = 0; i < vec_size(dis_list); ++i) {
1116 printf("Looking for `%s`\n", dis_list[i]);
1117 for (k = 1; k < vec_size(prog->functions); ++k) {
1118 const char *name = prog_getstring(prog, prog->functions[k].name);
1119 if (!strcmp(name, dis_list[i])) {
1120 prog_disasm_function(prog, k);
1126 for (i = 1; i < vec_size(prog->functions); ++i)
1127 prog_disasm_function(prog, i);
1130 if (opts_printdefs) {
1131 const char *getstring = NULL;
1132 for (i = 0; i < vec_size(prog->defs); ++i) {
1133 printf("Global: %8s %-16s at %u%s",
1134 type_name[prog->defs[i].type & DEF_TYPEMASK],
1135 prog_getstring(prog, prog->defs[i].name),
1136 (unsigned int)prog->defs[i].offset,
1137 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1139 switch (prog->defs[i].type & DEF_TYPEMASK) {
1141 printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
1144 printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1150 printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1153 getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
1154 printf(" [init: `");
1155 print_escaped_string(getstring, strlen(getstring));
1165 if (opts_printfields) {
1166 for (i = 0; i < vec_size(prog->fields); ++i) {
1167 printf("Field: %8s %-16s at %u%s\n",
1168 type_name[prog->fields[i].type],
1169 prog_getstring(prog, prog->fields[i].name),
1170 (unsigned int)prog->fields[i].offset,
1171 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1174 if (opts_printfuns) {
1175 for (i = 0; i < vec_size(prog->functions); ++i) {
1177 printf("Function: %-16s taking %u parameters:(",
1178 prog_getstring(prog, prog->functions[i].name),
1179 (unsigned int)prog->functions[i].nargs);
1180 for (a = 0; a < prog->functions[i].nargs; ++a) {
1181 printf(" %i", prog->functions[i].argsize[a]);
1184 int32_t start = prog->functions[i].entry;
1186 printf(") builtin %i\n", (int)-start);
1189 prog_section_statement_t *st = prog->code + start;
1190 for (;st->opcode != INSTR_DONE; ++st)
1192 printf(") - %lu instructions", (unsigned long)funsize);
1194 printf(" - locals: %i + %i\n",
1195 prog->functions[i].firstlocal,
1196 prog->functions[i].locals);
1203 printf(") locals: %i + %i\n",
1204 prog->functions[i].firstlocal,
1205 prog->functions[i].locals);
1212 for (i = 1; i < vec_size(prog->functions); ++i) {
1213 const char *name = prog_getstring(prog, prog->functions[i].name);
1214 if (!strcmp(name, "main"))
1215 fnmain = (qcint_t)i;
1219 prog_main_setparams(prog);
1220 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1223 fprintf(stderr, "No main function found\n");
1230 void prog_disasm_function(qc_program_t *prog, size_t id) {
1231 prog_section_function_t *fdef = prog->functions + id;
1232 prog_section_statement_t *st;
1234 if (fdef->entry < 0) {
1235 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1239 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1241 st = prog->code + fdef->entry;
1242 while (st->opcode != INSTR_DONE) {
1243 prog_print_statement(prog, st);
1247 #else /* !QCVM_LOOP */
1249 * Everything from here on is not including into the compilation of the
1250 * executor. This is simply code that is #included via #include __FILE__
1251 * see when QCVM_LOOP is defined, the rest of the code above do not get
1252 * re-included. So this really just acts like one large macro, but it
1253 * sort of isn't, which makes it nicer looking.
1256 #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
1257 #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
1258 #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
1260 #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
1262 /* to be consistent with current darkplaces behaviour */
1263 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1264 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1267 while (prog->vmerror == 0) {
1268 prog_section_function_t *newf;
1275 prog->profile[st - prog->code]++;
1279 prog_print_statement(prog, st);
1285 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1290 /* TODO: add instruction count to function profile count */
1291 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1292 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1293 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1295 st = prog->code + prog_leavefunction(prog);
1296 if (!vec_size(prog->stack))
1302 OPC->_float = OPA->_float * OPB->_float;
1305 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1306 OPA->vector[1]*OPB->vector[1] +
1307 OPA->vector[2]*OPB->vector[2];
1311 qcfloat_t f = OPA->_float;
1312 OPC->vector[0] = f * OPB->vector[0];
1313 OPC->vector[1] = f * OPB->vector[1];
1314 OPC->vector[2] = f * OPB->vector[2];
1319 qcfloat_t f = OPB->_float;
1320 OPC->vector[0] = f * OPA->vector[0];
1321 OPC->vector[1] = f * OPA->vector[1];
1322 OPC->vector[2] = f * OPA->vector[2];
1326 if (OPB->_float != 0.0f)
1327 OPC->_float = OPA->_float / OPB->_float;
1333 OPC->_float = OPA->_float + OPB->_float;
1336 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1337 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1338 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1341 OPC->_float = OPA->_float - OPB->_float;
1344 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1345 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1346 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1350 OPC->_float = (OPA->_float == OPB->_float);
1353 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1354 (OPA->vector[1] == OPB->vector[1]) &&
1355 (OPA->vector[2] == OPB->vector[2]) );
1358 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1359 prog_getstring(prog, OPB->string));
1362 OPC->_float = (OPA->_int == OPB->_int);
1365 OPC->_float = (OPA->function == OPB->function);
1368 OPC->_float = (OPA->_float != OPB->_float);
1371 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1372 (OPA->vector[1] != OPB->vector[1]) ||
1373 (OPA->vector[2] != OPB->vector[2]) );
1376 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1377 prog_getstring(prog, OPB->string));
1380 OPC->_float = (OPA->_int != OPB->_int);
1383 OPC->_float = (OPA->function != OPB->function);
1387 OPC->_float = (OPA->_float <= OPB->_float);
1390 OPC->_float = (OPA->_float >= OPB->_float);
1393 OPC->_float = (OPA->_float < OPB->_float);
1396 OPC->_float = (OPA->_float > OPB->_float);
1401 case INSTR_LOAD_FLD:
1402 case INSTR_LOAD_ENT:
1403 case INSTR_LOAD_FNC:
1404 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1405 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1408 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1409 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1414 ed = prog_getedict(prog, OPA->edict);
1415 OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
1418 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1419 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1422 if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
1424 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1429 ed = prog_getedict(prog, OPA->edict);
1430 ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
1431 OPC->ivector[0] = ptr->ivector[0];
1432 OPC->ivector[1] = ptr->ivector[1];
1433 OPC->ivector[2] = ptr->ivector[2];
1437 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1438 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1441 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1443 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1449 ed = prog_getedict(prog, OPA->edict);
1450 OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
1455 case INSTR_STORE_ENT:
1456 case INSTR_STORE_FLD:
1457 case INSTR_STORE_FNC:
1458 OPB->_int = OPA->_int;
1461 OPB->ivector[0] = OPA->ivector[0];
1462 OPB->ivector[1] = OPA->ivector[1];
1463 OPB->ivector[2] = OPA->ivector[2];
1466 case INSTR_STOREP_F:
1467 case INSTR_STOREP_S:
1468 case INSTR_STOREP_ENT:
1469 case INSTR_STOREP_FLD:
1470 case INSTR_STOREP_FNC:
1471 if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
1472 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1475 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1476 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1478 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1480 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1481 ptr->_int = OPA->_int;
1483 case INSTR_STOREP_V:
1484 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
1485 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1488 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1489 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1491 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1493 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1494 ptr->ivector[0] = OPA->ivector[0];
1495 ptr->ivector[1] = OPA->ivector[1];
1496 ptr->ivector[2] = OPA->ivector[2];
1500 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1503 OPC->_float = !OPA->vector[0] &&
1508 OPC->_float = !OPA->string ||
1509 !*prog_getstring(prog, OPA->string);
1512 OPC->_float = (OPA->edict == 0);
1515 OPC->_float = !OPA->function;
1519 /* this is consistent with darkplaces' behaviour */
1520 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1522 st += st->o2.s1 - 1; /* offset the s++ */
1523 if (++jumpcount >= maxjumps)
1524 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1528 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1530 st += st->o2.s1 - 1; /* offset the s++ */
1531 if (++jumpcount >= maxjumps)
1532 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1545 prog->argc = st->opcode - INSTR_CALL0;
1547 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1549 if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
1551 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1555 newf = &prog->functions[OPA->function];
1558 prog->statement = (st - prog->code) + 1;
1560 if (newf->entry < 0)
1562 /* negative statements are built in functions */
1563 qcint_t builtinnumber = -newf->entry;
1564 if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
1565 prog->builtins[builtinnumber](prog);
1567 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1568 builtinnumber, prog->filename);
1571 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1577 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1581 st += st->o1.s1 - 1; /* offset the s++ */
1582 if (++jumpcount == 10000000)
1583 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1587 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1588 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1591 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1592 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1596 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1599 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1606 #endif /* !QCVM_LOOP */