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 *prog, const char *fmt, ...)
54 qc_program* prog_load(const char *filename, bool skipversion)
58 FILE *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*)mem_a(sizeof(qc_program));
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);
149 void prog_delete(qc_program *prog)
151 if (prog->filename) mem_d(prog->filename);
152 vec_free(prog->code);
153 vec_free(prog->defs);
154 vec_free(prog->fields);
155 vec_free(prog->functions);
156 vec_free(prog->strings);
157 vec_free(prog->globals);
158 vec_free(prog->entitydata);
159 vec_free(prog->entitypool);
160 vec_free(prog->localstack);
161 vec_free(prog->stack);
162 vec_free(prog->profile);
166 /***********************************************************************
170 const char* prog_getstring(qc_program *prog, qcint str) {
171 /* cast for return required for C++ */
172 if (str < 0 || str >= (qcint)vec_size(prog->strings))
173 return "<<<invalid string>>>";
175 return prog->strings + str;
178 prog_section_def* prog_entfield(qc_program *prog, qcint off) {
180 for (i = 0; i < vec_size(prog->fields); ++i) {
181 if (prog->fields[i].offset == off)
182 return (prog->fields + i);
187 prog_section_def* prog_getdef(qc_program *prog, qcint off)
190 for (i = 0; i < vec_size(prog->defs); ++i) {
191 if (prog->defs[i].offset == off)
192 return (prog->defs + i);
197 qcany* prog_getedict(qc_program *prog, qcint e) {
198 if (e >= (qcint)vec_size(prog->entitypool)) {
200 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
203 return (qcany*)(prog->entitydata + (prog->entityfields * e));
206 qcint prog_spawn_entity(qc_program *prog) {
209 for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) {
210 if (!prog->entitypool[e]) {
211 data = (char*)(prog->entitydata + (prog->entityfields * e));
212 memset(data, 0, prog->entityfields * sizeof(qcint));
216 vec_push(prog->entitypool, true);
218 data = (char*)vec_add(prog->entitydata, prog->entityfields);
219 memset(data, 0, prog->entityfields * sizeof(qcint));
223 void prog_free_entity(qc_program *prog, qcint e) {
226 fprintf(stderr, "Trying to free world entity\n");
229 if (e >= (qcint)vec_size(prog->entitypool)) {
231 fprintf(stderr, "Trying to free out of bounds entity\n");
234 if (!prog->entitypool[e]) {
236 fprintf(stderr, "Double free on entity\n");
239 prog->entitypool[e] = false;
242 qcint prog_tempstring(qc_program *prog, const char *str) {
243 size_t len = strlen(str);
244 size_t at = prog->tempstring_at;
246 /* when we reach the end we start over */
247 if (at + len >= vec_size(prog->strings))
248 at = prog->tempstring_start;
250 /* when it doesn't fit, reallocate */
251 if (at + len >= vec_size(prog->strings))
253 (void)vec_add(prog->strings, len+1);
254 memcpy(prog->strings + at, str, len+1);
258 /* when it fits, just copy */
259 memcpy(prog->strings + at, str, len+1);
260 prog->tempstring_at += len+1;
264 static size_t print_escaped_string(const char *str, size_t maxlen) {
267 --maxlen; /* because we're lazy and have escape sequences */
277 case '\a': len += 2; putchar('\\'); putchar('a'); break;
278 case '\b': len += 2; putchar('\\'); putchar('b'); break;
279 case '\r': len += 2; putchar('\\'); putchar('r'); break;
280 case '\n': len += 2; putchar('\\'); putchar('n'); break;
281 case '\t': len += 2; putchar('\\'); putchar('t'); break;
282 case '\f': len += 2; putchar('\\'); putchar('f'); break;
283 case '\v': len += 2; putchar('\\'); putchar('v'); break;
284 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
285 case '"': len += 2; putchar('\\'); putchar('"'); break;
297 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype) {
298 static char spaces[28+1] = " ";
299 prog_section_def *def;
304 len = printf("<null>,");
308 def = prog_getdef(prog, glob);
309 value = (qcany*)(&prog->globals[glob]);
311 len = printf("[@%u] ", glob);
313 const char *name = prog_getstring(prog, def->name);
317 len += printf("%s ", name);
318 vtype = def->type & DEF_TYPEMASK;
327 len += printf("(%i),", value->_int);
330 len += printf("'%g %g %g',", value->vector[0],
336 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
338 len += printf("(null)");
340 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
344 len += printf("%g,", value->_float);
348 if (len < (int)sizeof(spaces)-1) {
349 spaces[sizeof(spaces)-1-len] = 0;
350 fs_file_puts(stdout, spaces);
351 spaces[sizeof(spaces)-1-len] = ' ';
355 static void prog_print_statement(qc_program *prog, prog_section_statement *st) {
356 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
357 printf("<illegal instruction %d>\n", st->opcode);
360 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
362 for (i = 0; i < vec_size(prog->function_stack); ++i)
364 printf("%s:", vec_last(prog->function_stack));
366 printf(" <> %-12s", asm_instr[st->opcode].m);
367 if (st->opcode >= INSTR_IF &&
368 st->opcode <= INSTR_IFNOT)
370 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
371 printf("%d\n", st->o2.s1);
373 else if (st->opcode >= INSTR_CALL0 &&
374 st->opcode <= INSTR_CALL8)
376 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
379 else if (st->opcode == INSTR_GOTO)
381 printf("%i\n", st->o1.s1);
385 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
389 t[1] = t[2] = TYPE_VECTOR;
392 t[0] = t[2] = TYPE_VECTOR;
395 t[0] = t[1] = TYPE_VECTOR;
401 t[0] = t[1] = t[2] = TYPE_VECTOR;
405 t[0] = t[1] = TYPE_STRING;
412 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
415 t[0] = t[1] = TYPE_STRING; t[2] = -1;
417 case INSTR_STORE_ENT:
418 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
420 case INSTR_STORE_FLD:
421 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
423 case INSTR_STORE_FNC:
424 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
427 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
430 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
432 case INSTR_STOREP_ENT:
433 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
435 case INSTR_STOREP_FLD:
436 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
438 case INSTR_STOREP_FNC:
439 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
442 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
443 else printf("(none), ");
444 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
445 else printf("(none), ");
446 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
447 else printf("(none)");
452 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func) {
458 st.localsp = vec_size(prog->localstack);
459 st.stmt = prog->statement;
462 if (prog->xflags & VMXF_TRACE) {
463 const char *str = prog_getstring(prog, func->name);
464 vec_push(prog->function_stack, str);
467 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
468 if (vec_size(prog->stack))
470 prog_section_function *cur;
471 cur = prog->stack[vec_size(prog->stack)-1].function;
474 qcint *globals = prog->globals + cur->firstlocal;
475 vec_append(prog->localstack, cur->locals, globals);
480 qcint *globals = prog->globals + func->firstlocal;
481 vec_append(prog->localstack, func->locals, globals);
485 /* copy parameters */
486 parampos = func->firstlocal;
487 for (p = 0; p < func->nargs; ++p)
490 for (s = 0; s < func->argsize[p]; ++s) {
491 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
496 vec_push(prog->stack, st);
501 static qcint prog_leavefunction(qc_program *prog) {
502 prog_section_function *prev = NULL;
505 qc_exec_stack st = vec_last(prog->stack);
507 if (prog->xflags & VMXF_TRACE) {
508 if (vec_size(prog->function_stack))
509 vec_pop(prog->function_stack);
512 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
513 if (vec_size(prog->stack) > 1) {
514 prev = prog->stack[vec_size(prog->stack)-2].function;
515 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
518 prev = prog->stack[vec_size(prog->stack)-1].function;
519 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
522 qcint *globals = prog->globals + prev->firstlocal;
523 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
524 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
525 vec_shrinkto(prog->localstack, oldsp);
528 vec_pop(prog->stack);
530 return st.stmt - 1; /* offset the ++st */
533 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps) {
535 size_t oldxflags = prog->xflags;
536 prog_section_statement *st;
539 prog->xflags = flags;
541 st = prog->code + prog_enterfunction(prog, func);
549 #define QCVM_PROFILE 0
555 #define QCVM_PROFILE 0
561 #define QCVM_PROFILE 1
565 case (VMXF_TRACE|VMXF_PROFILE):
567 #define QCVM_PROFILE 1
574 prog->xflags = oldxflags;
575 vec_free(prog->localstack);
576 vec_free(prog->stack);
582 /***********************************************************************
583 * main for when building the standalone executor
586 #if defined(QCVM_EXECUTOR)
591 const char *type_name[TYPE_COUNT] = {
617 static qcvm_parameter *main_params = NULL;
619 #define CheckArgs(num) do { \
620 if (prog->argc != (num)) { \
622 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
623 __FUNCTION__, prog->argc, (num)); \
628 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
629 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
630 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
632 static int qc_print(qc_program *prog) {
634 const char *laststr = NULL;
635 for (i = 0; i < (size_t)prog->argc; ++i) {
636 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
637 laststr = prog_getstring(prog, str->string);
638 printf("%s", laststr);
640 if (laststr && (prog->xflags & VMXF_TRACE)) {
641 size_t len = strlen(laststr);
642 if (!len || laststr[len-1] != '\n')
648 static int qc_error(qc_program *prog) {
649 fprintf(stderr, "*** VM raised an error:\n");
655 static int qc_ftos(qc_program *prog) {
661 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
662 str.string = prog_tempstring(prog, buffer);
667 static int qc_stof(qc_program *prog) {
672 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
677 static int qc_vtos(qc_program *prog) {
683 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
684 str.string = prog_tempstring(prog, buffer);
689 static int qc_etos(qc_program *prog) {
695 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
696 str.string = prog_tempstring(prog, buffer);
701 static int qc_spawn(qc_program *prog) {
704 ent.edict = prog_spawn_entity(prog);
706 return (ent.edict ? 0 : -1);
709 static int qc_kill(qc_program *prog) {
713 prog_free_entity(prog, ent->edict);
717 static int qc_sqrt(qc_program *prog) {
721 out._float = sqrt(num->_float);
726 static int qc_vlen(qc_program *prog) {
730 len._float = sqrt(vec->vector[0] * vec->vector[0] +
731 vec->vector[1] * vec->vector[1] +
732 vec->vector[2] * vec->vector[2]);
737 static int qc_normalize(qc_program *prog) {
743 len = sqrt(vec->vector[0] * vec->vector[0] +
744 vec->vector[1] * vec->vector[1] +
745 vec->vector[2] * vec->vector[2]);
750 out.vector[0] = len * vec->vector[0];
751 out.vector[1] = len * vec->vector[1];
752 out.vector[2] = len * vec->vector[2];
757 static int qc_strcat(qc_program *prog) {
769 cstr1 = prog_getstring(prog, str1->string);
770 cstr2 = prog_getstring(prog, str2->string);
771 len1 = strlen(cstr1);
772 len2 = strlen(cstr2);
773 buffer = (char*)mem_a(len1 + len2 + 1);
774 memcpy(buffer, cstr1, len1);
775 memcpy(buffer+len1, cstr2, len2+1);
776 out.string = prog_tempstring(prog, buffer);
782 static int qc_strcmp(qc_program *prog) {
789 if (prog->argc != 2 && prog->argc != 3) {
790 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
797 cstr1 = prog_getstring(prog, str1->string);
798 cstr2 = prog_getstring(prog, str2->string);
800 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
802 out._float = strcmp(cstr1, cstr2);
807 static int qc_floor(qc_program *prog) {
811 out._float = floor(num->_float);
816 static prog_builtin qc_builtins[] = {
829 &qc_normalize, /* 12 */
833 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
835 static const char *arg0 = NULL;
837 static void version() {
838 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
847 static void usage() {
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 *prog) {
871 for (i = 0; i < vec_size(main_params); ++i) {
872 arg = GetGlobal(OFS_PARM0 + 3*i);
876 switch (main_params[i].vtype) {
879 (void)sscanf_s(main_params[i].value, " %f %f %f ",
884 (void)sscanf(main_params[i].value, " %f %f %f ",
891 arg->_float = atof(main_params[i].value);
894 arg->string = prog_tempstring(prog, main_params[i].value);
897 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
903 void escapestring(char* dest, const char* src) {
905 while ((c = *(src++))) {
908 *(dest++) = '\\', *(dest++) = 't';
911 *(dest++) = '\\', *(dest++) = 'n';
914 *(dest++) = '\\', *(dest++) = 'r';
917 *(dest++) = '\\', *(dest++) = '\\';
920 *(dest++) = '\\', *(dest++) = '\"';
929 void prog_disasm_function(qc_program *prog, size_t id);
931 int main(int argc, char **argv) {
935 size_t xflags = VMXF_DEFAULT;
936 bool opts_printfields = false;
937 bool opts_printdefs = false;
938 bool opts_printfuns = false;
939 bool opts_disasm = false;
940 bool opts_info = false;
942 const char *progsfile = NULL;
943 const char **dis_list = NULL;
954 if (!strcmp(argv[1], "-h") ||
955 !strcmp(argv[1], "-help") ||
956 !strcmp(argv[1], "--help"))
961 else if (!strcmp(argv[1], "-v")) {
966 else if (!strncmp(argv[1], "-vv", 3)) {
967 const char *av = argv[1]+1;
979 else if (!strcmp(argv[1], "-version") ||
980 !strcmp(argv[1], "--version"))
985 else if (!strcmp(argv[1], "-trace")) {
988 xflags |= VMXF_TRACE;
990 else if (!strcmp(argv[1], "-profile")) {
993 xflags |= VMXF_PROFILE;
995 else if (!strcmp(argv[1], "-info")) {
1001 else if (!strcmp(argv[1], "-disasm")) {
1007 else if (!strcmp(argv[1], "-disasm-func")) {
1014 vec_push(dis_list, argv[1]);
1019 else if (!strcmp(argv[1], "-printdefs")) {
1022 opts_printdefs = true;
1025 else if (!strcmp(argv[1], "-printfuns")) {
1028 opts_printfuns = true;
1031 else if (!strcmp(argv[1], "-printfields")) {
1034 opts_printfields = true;
1037 else if (!strcmp(argv[1], "-vector") ||
1038 !strcmp(argv[1], "-string") ||
1039 !strcmp(argv[1], "-float") )
1042 if (argv[1][1] == 'f')
1043 p.vtype = TYPE_FLOAT;
1044 else if (argv[1][1] == 's')
1045 p.vtype = TYPE_STRING;
1046 else if (argv[1][1] == 'v')
1047 p.vtype = TYPE_VECTOR;
1057 vec_push(main_params, p);
1061 else if (!strcmp(argv[1], "--")) {
1066 else if (argv[1][0] != '-') {
1068 fprintf(stderr, "only 1 program file may be specified\n");
1072 progsfile = argv[1];
1078 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1084 if (argc == 2 && !progsfile) {
1085 progsfile = argv[1];
1091 fprintf(stderr, "must specify a program to execute\n");
1096 prog = prog_load(progsfile, noexec);
1098 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1102 prog->builtins = qc_builtins;
1103 prog->builtins_count = qc_builtins_count;
1106 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1107 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1108 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1115 (unsigned long)vec_size(prog->code),
1116 (unsigned long)vec_size(prog->defs),
1117 (unsigned long)vec_size(prog->fields),
1118 (unsigned long)vec_size(prog->functions),
1119 (unsigned long)vec_size(prog->strings));
1126 for (i = 0; i < vec_size(dis_list); ++i) {
1128 printf("Looking for `%s`\n", dis_list[i]);
1129 for (k = 1; k < vec_size(prog->functions); ++k) {
1130 const char *name = prog_getstring(prog, prog->functions[k].name);
1131 if (!strcmp(name, dis_list[i])) {
1132 prog_disasm_function(prog, k);
1138 for (i = 1; i < vec_size(prog->functions); ++i)
1139 prog_disasm_function(prog, i);
1142 if (opts_printdefs) {
1143 char *escape = NULL;
1144 const char *getstring = NULL;
1146 for (i = 0; i < vec_size(prog->defs); ++i) {
1147 printf("Global: %8s %-16s at %u%s",
1148 type_name[prog->defs[i].type & DEF_TYPEMASK],
1149 prog_getstring(prog, prog->defs[i].name),
1150 (unsigned int)prog->defs[i].offset,
1151 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1153 switch (prog->defs[i].type & DEF_TYPEMASK) {
1155 printf(" [init: %g]", ((qcany*)(prog->globals + prog->defs[i].offset))->_float);
1158 printf(" [init: %i]", (int)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1164 printf(" [init: %u]", (unsigned)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1167 getstring = prog_getstring(prog, ((qcany*)(prog->globals + prog->defs[i].offset))->string);
1168 escape = (char*)mem_a(strlen(getstring) * 2 + 1); /* will be enough */
1169 escapestring(escape, getstring);
1170 printf(" [init: `%s`]", escape);
1172 mem_d(escape); /* free */
1181 if (opts_printfields) {
1182 for (i = 0; i < vec_size(prog->fields); ++i) {
1183 printf("Field: %8s %-16s at %u%s\n",
1184 type_name[prog->fields[i].type],
1185 prog_getstring(prog, prog->fields[i].name),
1186 (unsigned int)prog->fields[i].offset,
1187 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1190 if (opts_printfuns) {
1191 for (i = 0; i < vec_size(prog->functions); ++i) {
1193 printf("Function: %-16s taking %i parameters:(",
1194 prog_getstring(prog, prog->functions[i].name),
1195 (unsigned int)prog->functions[i].nargs);
1196 for (a = 0; a < prog->functions[i].nargs; ++a) {
1197 printf(" %i", prog->functions[i].argsize[a]);
1200 int32_t start = prog->functions[i].entry;
1202 printf(") builtin %i\n", (int)-start);
1205 prog_section_statement *st = prog->code + start;
1206 for (;st->opcode != INSTR_DONE; ++st)
1208 printf(") - %lu instructions", (unsigned long)funsize);
1210 printf(" - locals: %i + %i\n",
1211 prog->functions[i].firstlocal,
1212 prog->functions[i].locals);
1219 printf(") locals: %i + %i\n",
1220 prog->functions[i].firstlocal,
1221 prog->functions[i].locals);
1228 for (i = 1; i < vec_size(prog->functions); ++i) {
1229 const char *name = prog_getstring(prog, prog->functions[i].name);
1230 if (!strcmp(name, "main"))
1235 prog_main_setparams(prog);
1236 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1239 fprintf(stderr, "No main function found\n");
1246 void prog_disasm_function(qc_program *prog, size_t id) {
1247 prog_section_function *fdef = prog->functions + id;
1248 prog_section_statement *st;
1250 if (fdef->entry < 0) {
1251 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1255 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1257 st = prog->code + fdef->entry;
1258 while (st->opcode != INSTR_DONE) {
1259 prog_print_statement(prog, st);
1264 #else /* !QCVM_LOOP */
1266 * Everything from here on is not including into the compilation of the
1267 * executor. This is simply code that is #included via #include __FILE__
1268 * see when QCVM_LOOP is defined, the rest of the code above do not get
1269 * re-included. So this really just acts like one large macro, but it
1270 * sort of isn't, which makes it nicer looking.
1273 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1274 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1275 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1277 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1279 /* to be consistent with current darkplaces behaviour */
1280 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1281 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1285 prog_section_function *newf;
1292 prog->profile[st - prog->code]++;
1296 prog_print_statement(prog, st);
1302 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1307 /* TODO: add instruction count to function profile count */
1308 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1309 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1310 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1312 st = prog->code + prog_leavefunction(prog);
1313 if (!vec_size(prog->stack))
1319 OPC->_float = OPA->_float * OPB->_float;
1322 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1323 OPA->vector[1]*OPB->vector[1] +
1324 OPA->vector[2]*OPB->vector[2];
1328 qcfloat f = OPA->_float;
1329 OPC->vector[0] = f * OPB->vector[0];
1330 OPC->vector[1] = f * OPB->vector[1];
1331 OPC->vector[2] = f * OPB->vector[2];
1336 qcfloat f = OPB->_float;
1337 OPC->vector[0] = f * OPA->vector[0];
1338 OPC->vector[1] = f * OPA->vector[1];
1339 OPC->vector[2] = f * OPA->vector[2];
1343 if (OPB->_float != 0.0f)
1344 OPC->_float = OPA->_float / OPB->_float;
1350 OPC->_float = OPA->_float + OPB->_float;
1353 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1354 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1355 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1358 OPC->_float = OPA->_float - OPB->_float;
1361 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1362 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1363 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1367 OPC->_float = (OPA->_float == OPB->_float);
1370 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1371 (OPA->vector[1] == OPB->vector[1]) &&
1372 (OPA->vector[2] == OPB->vector[2]) );
1375 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1376 prog_getstring(prog, OPB->string));
1379 OPC->_float = (OPA->_int == OPB->_int);
1382 OPC->_float = (OPA->function == OPB->function);
1385 OPC->_float = (OPA->_float != OPB->_float);
1388 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1389 (OPA->vector[1] != OPB->vector[1]) ||
1390 (OPA->vector[2] != OPB->vector[2]) );
1393 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1394 prog_getstring(prog, OPB->string));
1397 OPC->_float = (OPA->_int != OPB->_int);
1400 OPC->_float = (OPA->function != OPB->function);
1404 OPC->_float = (OPA->_float <= OPB->_float);
1407 OPC->_float = (OPA->_float >= OPB->_float);
1410 OPC->_float = (OPA->_float < OPB->_float);
1413 OPC->_float = (OPA->_float > OPB->_float);
1418 case INSTR_LOAD_FLD:
1419 case INSTR_LOAD_ENT:
1420 case INSTR_LOAD_FNC:
1421 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1422 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1425 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1426 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1431 ed = prog_getedict(prog, OPA->edict);
1432 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1435 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1436 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1439 if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1441 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1446 ed = prog_getedict(prog, OPA->edict);
1447 ptr = (qcany*)( ((qcint*)ed) + OPB->_int );
1448 OPC->ivector[0] = ptr->ivector[0];
1449 OPC->ivector[1] = ptr->ivector[1];
1450 OPC->ivector[2] = ptr->ivector[2];
1454 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1455 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1458 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1460 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1466 ed = prog_getedict(prog, OPA->edict);
1467 OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1472 case INSTR_STORE_ENT:
1473 case INSTR_STORE_FLD:
1474 case INSTR_STORE_FNC:
1475 OPB->_int = OPA->_int;
1478 OPB->ivector[0] = OPA->ivector[0];
1479 OPB->ivector[1] = OPA->ivector[1];
1480 OPB->ivector[2] = OPA->ivector[2];
1483 case INSTR_STOREP_F:
1484 case INSTR_STOREP_S:
1485 case INSTR_STOREP_ENT:
1486 case INSTR_STOREP_FLD:
1487 case INSTR_STOREP_FNC:
1488 if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1489 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1492 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1493 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1495 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1497 ptr = (qcany*)(prog->entitydata + OPB->_int);
1498 ptr->_int = OPA->_int;
1500 case INSTR_STOREP_V:
1501 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1502 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1505 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1506 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1508 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1510 ptr = (qcany*)(prog->entitydata + OPB->_int);
1511 ptr->ivector[0] = OPA->ivector[0];
1512 ptr->ivector[1] = OPA->ivector[1];
1513 ptr->ivector[2] = OPA->ivector[2];
1517 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1520 OPC->_float = !OPA->vector[0] &&
1525 OPC->_float = !OPA->string ||
1526 !*prog_getstring(prog, OPA->string);
1529 OPC->_float = (OPA->edict == 0);
1532 OPC->_float = !OPA->function;
1536 /* this is consistent with darkplaces' behaviour */
1537 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1539 st += st->o2.s1 - 1; /* offset the s++ */
1540 if (++jumpcount >= maxjumps)
1541 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1545 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1547 st += st->o2.s1 - 1; /* offset the s++ */
1548 if (++jumpcount >= maxjumps)
1549 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1562 prog->argc = st->opcode - INSTR_CALL0;
1564 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1566 if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1568 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1572 newf = &prog->functions[OPA->function];
1575 prog->statement = (st - prog->code) + 1;
1577 if (newf->entry < 0)
1579 /* negative statements are built in functions */
1580 qcint builtinnumber = -newf->entry;
1581 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1582 prog->builtins[builtinnumber](prog);
1584 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1585 builtinnumber, prog->filename);
1588 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1594 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1598 st += st->o1.s1 - 1; /* offset the s++ */
1599 if (++jumpcount == 10000000)
1600 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1604 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1605 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1608 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1609 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1613 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1616 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1623 #endif /* !QCVM_LOOP */