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 opts_cmd_t opts; /* command line options */
34 static void loaderror(const char *fmt, ...)
41 printf(": %s\n", util_strerror(err));
44 static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
56 qc_program_t* prog_load(const char *filename, bool skipversion)
60 FILE *file = fs_file_open(filename, "rb");
65 if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
66 loaderror("failed to read header from '%s'", filename);
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 */
121 /* profile counters */
122 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
124 /* Add tempstring area */
125 prog->tempstring_start = vec_size(prog->strings);
126 prog->tempstring_at = vec_size(prog->strings);
127 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
129 /* spawn the world entity */
130 vec_push(prog->entitypool, true);
131 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
138 mem_d(prog->filename);
139 vec_free(prog->code);
140 vec_free(prog->defs);
141 vec_free(prog->fields);
142 vec_free(prog->functions);
143 vec_free(prog->strings);
144 vec_free(prog->globals);
145 vec_free(prog->entitydata);
146 vec_free(prog->entitypool);
153 void prog_delete(qc_program_t *prog)
155 if (prog->filename) mem_d(prog->filename);
156 vec_free(prog->code);
157 vec_free(prog->defs);
158 vec_free(prog->fields);
159 vec_free(prog->functions);
160 vec_free(prog->strings);
161 vec_free(prog->globals);
162 vec_free(prog->entitydata);
163 vec_free(prog->entitypool);
164 vec_free(prog->localstack);
165 vec_free(prog->stack);
166 vec_free(prog->profile);
170 /***********************************************************************
174 const char* prog_getstring(qc_program_t *prog, qcint_t str) {
175 /* cast for return required for C++ */
176 if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
177 return "<<<invalid string>>>";
179 return prog->strings + str;
182 prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
184 for (i = 0; i < vec_size(prog->fields); ++i) {
185 if (prog->fields[i].offset == off)
186 return (prog->fields + i);
191 prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
194 for (i = 0; i < vec_size(prog->defs); ++i) {
195 if (prog->defs[i].offset == off)
196 return (prog->defs + i);
201 qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
202 if (e >= (qcint_t)vec_size(prog->entitypool)) {
204 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
207 return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
210 static qcint_t prog_spawn_entity(qc_program_t *prog) {
213 for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
214 if (!prog->entitypool[e]) {
215 data = (char*)(prog->entitydata + (prog->entityfields * e));
216 memset(data, 0, prog->entityfields * sizeof(qcint_t));
220 vec_push(prog->entitypool, true);
222 data = (char*)vec_add(prog->entitydata, prog->entityfields);
223 memset(data, 0, prog->entityfields * sizeof(qcint_t));
227 static void prog_free_entity(qc_program_t *prog, qcint_t e) {
230 fprintf(stderr, "Trying to free world entity\n");
233 if (e >= (qcint_t)vec_size(prog->entitypool)) {
235 fprintf(stderr, "Trying to free out of bounds entity\n");
238 if (!prog->entitypool[e]) {
240 fprintf(stderr, "Double free on entity\n");
243 prog->entitypool[e] = false;
246 qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
247 size_t len = strlen(str);
248 size_t at = prog->tempstring_at;
250 /* when we reach the end we start over */
251 if (at + len >= vec_size(prog->strings))
252 at = prog->tempstring_start;
254 /* when it doesn't fit, reallocate */
255 if (at + len >= vec_size(prog->strings))
257 (void)vec_add(prog->strings, len+1);
258 memcpy(prog->strings + at, str, len+1);
262 /* when it fits, just copy */
263 memcpy(prog->strings + at, str, len+1);
264 prog->tempstring_at += len+1;
268 static size_t print_escaped_string(const char *str, size_t maxlen) {
271 --maxlen; /* because we're lazy and have escape sequences */
281 case '\a': len += 2; putchar('\\'); putchar('a'); break;
282 case '\b': len += 2; putchar('\\'); putchar('b'); break;
283 case '\r': len += 2; putchar('\\'); putchar('r'); break;
284 case '\n': len += 2; putchar('\\'); putchar('n'); break;
285 case '\t': len += 2; putchar('\\'); putchar('t'); break;
286 case '\f': len += 2; putchar('\\'); putchar('f'); break;
287 case '\v': len += 2; putchar('\\'); putchar('v'); break;
288 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
289 case '"': len += 2; putchar('\\'); putchar('"'); break;
301 static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
302 static char spaces[28+1] = " ";
303 prog_section_def_t *def;
308 if ((len = printf("<null>,")) == -1)
314 def = prog_getdef(prog, glob);
315 value = (qcany_t*)(&prog->globals[glob]);
317 len = printf("[@%u] ", glob);
319 const char *name = prog_getstring(prog, def->name);
323 len += printf("%s ", name);
324 vtype = def->type & DEF_TYPEMASK;
333 len += printf("(%i),", value->_int);
336 len += printf("'%g %g %g',", value->vector[0],
342 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
344 len += printf("(null)");
346 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
350 len += printf("%g,", value->_float);
354 if (len < (int)sizeof(spaces)-1) {
355 spaces[sizeof(spaces)-1-len] = 0;
356 fs_file_puts(stdout, spaces);
357 spaces[sizeof(spaces)-1-len] = ' ';
361 static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
362 if (st->opcode >= VINSTR_END) {
363 printf("<illegal instruction %d>\n", st->opcode);
366 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
368 for (i = 0; i < vec_size(prog->function_stack); ++i)
370 printf("%s:", vec_last(prog->function_stack));
372 printf(" <> %-12s", util_instr_str[st->opcode]);
373 if (st->opcode >= INSTR_IF &&
374 st->opcode <= INSTR_IFNOT)
376 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
377 printf("%d\n", st->o2.s1);
379 else if (st->opcode >= INSTR_CALL0 &&
380 st->opcode <= INSTR_CALL8)
382 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
385 else if (st->opcode == INSTR_GOTO)
387 printf("%i\n", st->o1.s1);
391 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
395 t[1] = t[2] = TYPE_VECTOR;
398 t[0] = t[2] = TYPE_VECTOR;
401 t[0] = t[1] = TYPE_VECTOR;
407 t[0] = t[1] = t[2] = TYPE_VECTOR;
411 t[0] = t[1] = TYPE_STRING;
418 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
421 t[0] = t[1] = TYPE_STRING; t[2] = -1;
423 case INSTR_STORE_ENT:
424 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
426 case INSTR_STORE_FLD:
427 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
429 case INSTR_STORE_FNC:
430 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
433 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
436 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
438 case INSTR_STOREP_ENT:
439 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
441 case INSTR_STOREP_FLD:
442 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
444 case INSTR_STOREP_FNC:
445 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
448 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
449 else printf("(none), ");
450 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
451 else printf("(none), ");
452 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
453 else printf("(none)");
458 static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
464 st.localsp = vec_size(prog->localstack);
465 st.stmt = prog->statement;
468 if (prog->xflags & VMXF_TRACE) {
469 const char *str = prog_getstring(prog, func->name);
470 vec_push(prog->function_stack, str);
473 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
474 if (vec_size(prog->stack))
476 prog_section_function_t *cur;
477 cur = prog->stack[vec_size(prog->stack)-1].function;
480 qcint_t *globals = prog->globals + cur->firstlocal;
481 vec_append(prog->localstack, cur->locals, globals);
486 qcint_t *globals = prog->globals + func->firstlocal;
487 vec_append(prog->localstack, func->locals, globals);
491 /* copy parameters */
492 parampos = func->firstlocal;
493 for (p = 0; p < func->nargs; ++p)
496 for (s = 0; s < func->argsize[p]; ++s) {
497 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
502 vec_push(prog->stack, st);
507 static qcint_t prog_leavefunction(qc_program_t *prog) {
508 prog_section_function_t *prev = NULL;
511 qc_exec_stack_t st = vec_last(prog->stack);
513 if (prog->xflags & VMXF_TRACE) {
514 if (vec_size(prog->function_stack))
515 vec_pop(prog->function_stack);
518 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
519 if (vec_size(prog->stack) > 1) {
520 prev = prog->stack[vec_size(prog->stack)-2].function;
521 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
524 prev = prog->stack[vec_size(prog->stack)-1].function;
525 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
528 qcint_t *globals = prog->globals + prev->firstlocal;
529 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
530 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
531 vec_shrinkto(prog->localstack, oldsp);
534 vec_pop(prog->stack);
536 return st.stmt - 1; /* offset the ++st */
539 bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
541 size_t oldxflags = prog->xflags;
542 prog_section_statement_t *st;
545 prog->xflags = flags;
547 st = prog->code + prog_enterfunction(prog, func);
555 #define QCVM_PROFILE 0
561 #define QCVM_PROFILE 0
567 #define QCVM_PROFILE 1
571 case (VMXF_TRACE|VMXF_PROFILE):
573 #define QCVM_PROFILE 1
580 prog->xflags = oldxflags;
581 vec_free(prog->localstack);
582 vec_free(prog->stack);
588 /***********************************************************************
589 * main for when building the standalone executor
592 #if defined(QCVM_EXECUTOR)
595 const char *type_name[TYPE_COUNT] = {
621 static qcvm_parameter *main_params = NULL;
623 #define CheckArgs(num) do { \
624 if (prog->argc != (num)) { \
626 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
627 __FUNCTION__, prog->argc, (num)); \
632 #define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
633 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
634 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
636 static int qc_print(qc_program_t *prog) {
638 const char *laststr = NULL;
639 for (i = 0; i < (size_t)prog->argc; ++i) {
640 qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
641 laststr = prog_getstring(prog, str->string);
642 printf("%s", laststr);
644 if (laststr && (prog->xflags & VMXF_TRACE)) {
645 size_t len = strlen(laststr);
646 if (!len || laststr[len-1] != '\n')
652 static int qc_error(qc_program_t *prog) {
653 fprintf(stderr, "*** VM raised an error:\n");
659 static int qc_ftos(qc_program_t *prog) {
665 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
666 str.string = prog_tempstring(prog, buffer);
671 static int qc_stof(qc_program_t *prog) {
676 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
681 static int qc_vtos(qc_program_t *prog) {
687 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
688 str.string = prog_tempstring(prog, buffer);
693 static int qc_etos(qc_program_t *prog) {
699 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
700 str.string = prog_tempstring(prog, buffer);
705 static int qc_spawn(qc_program_t *prog) {
708 ent.edict = prog_spawn_entity(prog);
710 return (ent.edict ? 0 : -1);
713 static int qc_kill(qc_program_t *prog) {
717 prog_free_entity(prog, ent->edict);
721 static int qc_sqrt(qc_program_t *prog) {
725 out._float = sqrt(num->_float);
730 static int qc_vlen(qc_program_t *prog) {
734 len._float = sqrt(vec->vector[0] * vec->vector[0] +
735 vec->vector[1] * vec->vector[1] +
736 vec->vector[2] * vec->vector[2]);
741 static int qc_normalize(qc_program_t *prog) {
747 len = sqrt(vec->vector[0] * vec->vector[0] +
748 vec->vector[1] * vec->vector[1] +
749 vec->vector[2] * vec->vector[2]);
754 out.vector[0] = len * vec->vector[0];
755 out.vector[1] = len * vec->vector[1];
756 out.vector[2] = len * vec->vector[2];
761 static int qc_strcat(qc_program_t *prog) {
764 qcany_t *str1, *str2;
773 cstr1 = prog_getstring(prog, str1->string);
774 cstr2 = prog_getstring(prog, str2->string);
775 len1 = strlen(cstr1);
776 len2 = strlen(cstr2);
777 buffer = (char*)mem_a(len1 + len2 + 1);
778 memcpy(buffer, cstr1, len1);
779 memcpy(buffer+len1, cstr2, len2+1);
780 out.string = prog_tempstring(prog, buffer);
786 static int qc_strcmp(qc_program_t *prog) {
787 qcany_t *str1, *str2;
793 if (prog->argc != 2 && prog->argc != 3) {
794 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
801 cstr1 = prog_getstring(prog, str1->string);
802 cstr2 = prog_getstring(prog, str2->string);
804 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
806 out._float = strcmp(cstr1, cstr2);
811 static int qc_floor(qc_program_t *prog) {
815 out._float = floor(num->_float);
820 static prog_builtin_t qc_builtins[] = {
833 &qc_normalize, /* 12 */
838 static const char *arg0 = NULL;
840 static void version(void) {
841 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
850 static void usage(void) {
851 printf("usage: %s [options] [parameters] file\n", arg0);
852 printf("options:\n");
853 printf(" -h, --help print this message\n"
854 " -trace trace the execution\n"
855 " -profile perform profiling during execution\n"
856 " -info print information from the prog's header\n"
857 " -disasm disassemble and exit\n"
858 " -disasm-func func disassemble and exit\n"
859 " -printdefs list the defs section\n"
860 " -printfields list the field section\n"
861 " -printfuns list functions information\n"
863 " -vv be even more verbose\n");
864 printf("parameters:\n");
865 printf(" -vector <V> pass a vector parameter to main()\n"
866 " -float <f> pass a float parameter to main()\n"
867 " -string <s> pass a string parameter to main() \n");
870 static void prog_main_setparams(qc_program_t *prog) {
874 for (i = 0; i < vec_size(main_params); ++i) {
875 arg = GetGlobal(OFS_PARM0 + 3*i);
879 switch (main_params[i].vtype) {
882 (void)sscanf_s(main_params[i].value, " %f %f %f ",
887 (void)sscanf(main_params[i].value, " %f %f %f ",
894 arg->_float = atof(main_params[i].value);
897 arg->string = prog_tempstring(prog, main_params[i].value);
900 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
906 void prog_disasm_function(qc_program_t *prog, size_t id);
908 int main(int argc, char **argv) {
912 size_t xflags = VMXF_DEFAULT;
913 bool opts_printfields = false;
914 bool opts_printdefs = false;
915 bool opts_printfuns = false;
916 bool opts_disasm = false;
917 bool opts_info = false;
919 const char *progsfile = NULL;
920 const char **dis_list = NULL;
931 if (!strcmp(argv[1], "-h") ||
932 !strcmp(argv[1], "-help") ||
933 !strcmp(argv[1], "--help"))
938 else if (!strcmp(argv[1], "-v")) {
943 else if (!strncmp(argv[1], "-vv", 3)) {
944 const char *av = argv[1]+1;
956 else if (!strcmp(argv[1], "-version") ||
957 !strcmp(argv[1], "--version"))
962 else if (!strcmp(argv[1], "-trace")) {
965 xflags |= VMXF_TRACE;
967 else if (!strcmp(argv[1], "-profile")) {
970 xflags |= VMXF_PROFILE;
972 else if (!strcmp(argv[1], "-info")) {
978 else if (!strcmp(argv[1], "-disasm")) {
984 else if (!strcmp(argv[1], "-disasm-func")) {
991 vec_push(dis_list, argv[1]);
996 else if (!strcmp(argv[1], "-printdefs")) {
999 opts_printdefs = true;
1002 else if (!strcmp(argv[1], "-printfuns")) {
1005 opts_printfuns = true;
1008 else if (!strcmp(argv[1], "-printfields")) {
1011 opts_printfields = true;
1014 else if (!strcmp(argv[1], "-vector") ||
1015 !strcmp(argv[1], "-string") ||
1016 !strcmp(argv[1], "-float") )
1019 if (argv[1][1] == 'f')
1020 p.vtype = TYPE_FLOAT;
1021 else if (argv[1][1] == 's')
1022 p.vtype = TYPE_STRING;
1023 else if (argv[1][1] == 'v')
1024 p.vtype = TYPE_VECTOR;
1026 p.vtype = TYPE_VOID;
1036 vec_push(main_params, p);
1040 else if (!strcmp(argv[1], "--")) {
1045 else if (argv[1][0] != '-') {
1047 fprintf(stderr, "only 1 program file may be specified\n");
1051 progsfile = argv[1];
1057 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1063 if (argc == 2 && !progsfile) {
1064 progsfile = argv[1];
1070 fprintf(stderr, "must specify a program to execute\n");
1075 prog = prog_load(progsfile, noexec);
1077 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1081 prog->builtins = qc_builtins;
1082 prog->builtins_count = GMQCC_ARRAY_COUNT(qc_builtins);
1085 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1086 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1087 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1094 (unsigned long)vec_size(prog->code),
1095 (unsigned long)vec_size(prog->defs),
1096 (unsigned long)vec_size(prog->fields),
1097 (unsigned long)vec_size(prog->functions),
1098 (unsigned long)vec_size(prog->strings));
1105 for (i = 0; i < vec_size(dis_list); ++i) {
1107 printf("Looking for `%s`\n", dis_list[i]);
1108 for (k = 1; k < vec_size(prog->functions); ++k) {
1109 const char *name = prog_getstring(prog, prog->functions[k].name);
1110 if (!strcmp(name, dis_list[i])) {
1111 prog_disasm_function(prog, k);
1117 for (i = 1; i < vec_size(prog->functions); ++i)
1118 prog_disasm_function(prog, i);
1121 if (opts_printdefs) {
1122 const char *getstring = NULL;
1123 for (i = 0; i < vec_size(prog->defs); ++i) {
1124 printf("Global: %8s %-16s at %u%s",
1125 type_name[prog->defs[i].type & DEF_TYPEMASK],
1126 prog_getstring(prog, prog->defs[i].name),
1127 (unsigned int)prog->defs[i].offset,
1128 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1130 switch (prog->defs[i].type & DEF_TYPEMASK) {
1132 printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
1135 printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1141 printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1144 getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
1145 printf(" [init: `");
1146 print_escaped_string(getstring, strlen(getstring));
1156 if (opts_printfields) {
1157 for (i = 0; i < vec_size(prog->fields); ++i) {
1158 printf("Field: %8s %-16s at %u%s\n",
1159 type_name[prog->fields[i].type],
1160 prog_getstring(prog, prog->fields[i].name),
1161 (unsigned int)prog->fields[i].offset,
1162 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1165 if (opts_printfuns) {
1166 for (i = 0; i < vec_size(prog->functions); ++i) {
1168 printf("Function: %-16s taking %i parameters:(",
1169 prog_getstring(prog, prog->functions[i].name),
1170 (unsigned int)prog->functions[i].nargs);
1171 for (a = 0; a < prog->functions[i].nargs; ++a) {
1172 printf(" %i", prog->functions[i].argsize[a]);
1175 int32_t start = prog->functions[i].entry;
1177 printf(") builtin %i\n", (int)-start);
1180 prog_section_statement_t *st = prog->code + start;
1181 for (;st->opcode != INSTR_DONE; ++st)
1183 printf(") - %lu instructions", (unsigned long)funsize);
1185 printf(" - locals: %i + %i\n",
1186 prog->functions[i].firstlocal,
1187 prog->functions[i].locals);
1194 printf(") locals: %i + %i\n",
1195 prog->functions[i].firstlocal,
1196 prog->functions[i].locals);
1203 for (i = 1; i < vec_size(prog->functions); ++i) {
1204 const char *name = prog_getstring(prog, prog->functions[i].name);
1205 if (!strcmp(name, "main"))
1206 fnmain = (qcint_t)i;
1210 prog_main_setparams(prog);
1211 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1214 fprintf(stderr, "No main function found\n");
1221 void prog_disasm_function(qc_program_t *prog, size_t id) {
1222 prog_section_function_t *fdef = prog->functions + id;
1223 prog_section_statement_t *st;
1225 if (fdef->entry < 0) {
1226 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1230 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1232 st = prog->code + fdef->entry;
1233 while (st->opcode != INSTR_DONE) {
1234 prog_print_statement(prog, st);
1239 #else /* !QCVM_LOOP */
1241 * Everything from here on is not including into the compilation of the
1242 * executor. This is simply code that is #included via #include __FILE__
1243 * see when QCVM_LOOP is defined, the rest of the code above do not get
1244 * re-included. So this really just acts like one large macro, but it
1245 * sort of isn't, which makes it nicer looking.
1248 #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
1249 #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
1250 #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
1252 #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
1254 /* to be consistent with current darkplaces behaviour */
1255 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1256 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1260 prog_section_function_t *newf;
1267 prog->profile[st - prog->code]++;
1271 prog_print_statement(prog, st);
1277 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1282 /* TODO: add instruction count to function profile count */
1283 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1284 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1285 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1287 st = prog->code + prog_leavefunction(prog);
1288 if (!vec_size(prog->stack))
1294 OPC->_float = OPA->_float * OPB->_float;
1297 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1298 OPA->vector[1]*OPB->vector[1] +
1299 OPA->vector[2]*OPB->vector[2];
1303 qcfloat_t f = OPA->_float;
1304 OPC->vector[0] = f * OPB->vector[0];
1305 OPC->vector[1] = f * OPB->vector[1];
1306 OPC->vector[2] = f * OPB->vector[2];
1311 qcfloat_t f = OPB->_float;
1312 OPC->vector[0] = f * OPA->vector[0];
1313 OPC->vector[1] = f * OPA->vector[1];
1314 OPC->vector[2] = f * OPA->vector[2];
1318 if (OPB->_float != 0.0f)
1319 OPC->_float = OPA->_float / OPB->_float;
1325 OPC->_float = OPA->_float + OPB->_float;
1328 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1329 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1330 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
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];
1342 OPC->_float = (OPA->_float == OPB->_float);
1345 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1346 (OPA->vector[1] == OPB->vector[1]) &&
1347 (OPA->vector[2] == OPB->vector[2]) );
1350 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1351 prog_getstring(prog, OPB->string));
1354 OPC->_float = (OPA->_int == OPB->_int);
1357 OPC->_float = (OPA->function == OPB->function);
1360 OPC->_float = (OPA->_float != OPB->_float);
1363 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1364 (OPA->vector[1] != OPB->vector[1]) ||
1365 (OPA->vector[2] != OPB->vector[2]) );
1368 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1369 prog_getstring(prog, OPB->string));
1372 OPC->_float = (OPA->_int != OPB->_int);
1375 OPC->_float = (OPA->function != OPB->function);
1379 OPC->_float = (OPA->_float <= OPB->_float);
1382 OPC->_float = (OPA->_float >= OPB->_float);
1385 OPC->_float = (OPA->_float < OPB->_float);
1388 OPC->_float = (OPA->_float > OPB->_float);
1393 case INSTR_LOAD_FLD:
1394 case INSTR_LOAD_ENT:
1395 case INSTR_LOAD_FNC:
1396 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1397 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1400 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1401 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1406 ed = prog_getedict(prog, OPA->edict);
1407 OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
1410 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1411 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1414 if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
1416 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1421 ed = prog_getedict(prog, OPA->edict);
1422 ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
1423 OPC->ivector[0] = ptr->ivector[0];
1424 OPC->ivector[1] = ptr->ivector[1];
1425 OPC->ivector[2] = ptr->ivector[2];
1429 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1430 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1433 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1435 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1441 ed = prog_getedict(prog, OPA->edict);
1442 OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
1447 case INSTR_STORE_ENT:
1448 case INSTR_STORE_FLD:
1449 case INSTR_STORE_FNC:
1450 OPB->_int = OPA->_int;
1453 OPB->ivector[0] = OPA->ivector[0];
1454 OPB->ivector[1] = OPA->ivector[1];
1455 OPB->ivector[2] = OPA->ivector[2];
1458 case INSTR_STOREP_F:
1459 case INSTR_STOREP_S:
1460 case INSTR_STOREP_ENT:
1461 case INSTR_STOREP_FLD:
1462 case INSTR_STOREP_FNC:
1463 if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
1464 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1467 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1468 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1470 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1472 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1473 ptr->_int = OPA->_int;
1475 case INSTR_STOREP_V:
1476 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
1477 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1480 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1481 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1483 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1485 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1486 ptr->ivector[0] = OPA->ivector[0];
1487 ptr->ivector[1] = OPA->ivector[1];
1488 ptr->ivector[2] = OPA->ivector[2];
1492 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1495 OPC->_float = !OPA->vector[0] &&
1500 OPC->_float = !OPA->string ||
1501 !*prog_getstring(prog, OPA->string);
1504 OPC->_float = (OPA->edict == 0);
1507 OPC->_float = !OPA->function;
1511 /* this is consistent with darkplaces' behaviour */
1512 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1514 st += st->o2.s1 - 1; /* offset the s++ */
1515 if (++jumpcount >= maxjumps)
1516 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
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);
1537 prog->argc = st->opcode - INSTR_CALL0;
1539 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1541 if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
1543 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1547 newf = &prog->functions[OPA->function];
1550 prog->statement = (st - prog->code) + 1;
1552 if (newf->entry < 0)
1554 /* negative statements are built in functions */
1555 qcint_t builtinnumber = -newf->entry;
1556 if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
1557 prog->builtins[builtinnumber](prog);
1559 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1560 builtinnumber, prog->filename);
1563 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1569 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1573 st += st->o1.s1 - 1; /* offset the s++ */
1574 if (++jumpcount == 10000000)
1575 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1579 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1580 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1583 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1584 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1588 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1591 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1598 #endif /* !QCVM_LOOP */