9 static void loaderror(const char *fmt, ...)
16 printf(": %s\n", util_strerror(err));
19 static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
31 qc_program_t* prog_load(const char *filename, bool skipversion)
36 FILE *file = fopen(filename, "rb");
38 /* we need all those in order to support INSTR_STATE: */
39 bool has_self = false,
42 has_nextthink = false,
48 if (fread(&header, sizeof(header), 1, file) != 1) {
49 loaderror("failed to read header from '%s'", filename);
54 util_swap_header(&header);
56 if (!skipversion && header.version != 6) {
57 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
62 prog = (qc_program_t*)mem_a(sizeof(qc_program_t));
65 fprintf(stderr, "failed to allocate program data\n");
68 memset(prog, 0, sizeof(*prog));
70 prog->entityfields = header.entfield;
71 prog->crc16 = header.crc16;
73 prog->filename = util_strdup(filename);
74 if (!prog->filename) {
75 loaderror("failed to store program name");
79 #define read_data(hdrvar, progvar, reserved) \
80 if (fseek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
81 loaderror("seek failed"); \
85 vec_add(prog->progvar, header.hdrvar.length + reserved), \
86 sizeof(*prog->progvar), \
87 header.hdrvar.length, \
89 )!= header.hdrvar.length \
91 loaderror("read failed"); \
94 #define read_data1(x) read_data(x, x, 0)
95 #define read_data2(x, y) read_data(x, x, y)
97 read_data (statements, code, 0);
100 read_data1(functions);
102 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
104 util_swap_statements (prog->code);
105 util_swap_defs_fields(prog->defs);
106 util_swap_defs_fields(prog->fields);
107 util_swap_functions (prog->functions);
108 util_swap_globals (prog->globals);
112 /* profile counters */
113 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
115 /* Add tempstring area */
116 prog->tempstring_start = vec_size(prog->strings);
117 prog->tempstring_at = vec_size(prog->strings);
118 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
120 /* spawn the world entity */
121 vec_push(prog->entitypool, true);
122 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
125 /* cache some globals and fields from names */
126 for (i = 0; i < vec_size(prog->defs); ++i) {
127 const char *name = prog_getstring(prog, prog->defs[i].name);
128 if (!strcmp(name, "self")) {
129 prog->cached_globals.self = prog->defs[i].offset;
132 else if (!strcmp(name, "time")) {
133 prog->cached_globals.time = prog->defs[i].offset;
137 for (i = 0; i < vec_size(prog->fields); ++i) {
138 const char *name = prog_getstring(prog, prog->fields[i].name);
139 if (!strcmp(name, "think")) {
140 prog->cached_fields.think = prog->fields[i].offset;
143 else if (!strcmp(name, "nextthink")) {
144 prog->cached_fields.nextthink = prog->fields[i].offset;
145 has_nextthink = true;
147 else if (!strcmp(name, "frame")) {
148 prog->cached_fields.frame = prog->fields[i].offset;
152 if (has_self && has_time && has_think && has_nextthink && has_frame)
153 prog->supports_state = true;
159 mem_d(prog->filename);
160 vec_free(prog->code);
161 vec_free(prog->defs);
162 vec_free(prog->fields);
163 vec_free(prog->functions);
164 vec_free(prog->strings);
165 vec_free(prog->globals);
166 vec_free(prog->entitydata);
167 vec_free(prog->entitypool);
174 void prog_delete(qc_program_t *prog)
176 if (prog->filename) mem_d(prog->filename);
177 vec_free(prog->code);
178 vec_free(prog->defs);
179 vec_free(prog->fields);
180 vec_free(prog->functions);
181 vec_free(prog->strings);
182 vec_free(prog->globals);
183 vec_free(prog->entitydata);
184 vec_free(prog->entitypool);
185 vec_free(prog->localstack);
186 vec_free(prog->stack);
187 vec_free(prog->profile);
191 /***********************************************************************
195 const char* prog_getstring(qc_program_t *prog, qcint_t str) {
196 /* cast for return required for C++ */
197 if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
198 return "<<<invalid string>>>";
200 return prog->strings + str;
203 prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
205 for (i = 0; i < vec_size(prog->fields); ++i) {
206 if (prog->fields[i].offset == off)
207 return (prog->fields + i);
212 prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
215 for (i = 0; i < vec_size(prog->defs); ++i) {
216 if (prog->defs[i].offset == off)
217 return (prog->defs + i);
222 qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
223 if (e >= (qcint_t)vec_size(prog->entitypool)) {
225 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
228 return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
231 static qcint_t prog_spawn_entity(qc_program_t *prog) {
234 for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
235 if (!prog->entitypool[e]) {
236 data = (char*)(prog->entitydata + (prog->entityfields * e));
237 memset(data, 0, prog->entityfields * sizeof(qcint_t));
241 vec_push(prog->entitypool, true);
243 data = (char*)vec_add(prog->entitydata, prog->entityfields);
244 memset(data, 0, prog->entityfields * sizeof(qcint_t));
248 static void prog_free_entity(qc_program_t *prog, qcint_t e) {
251 fprintf(stderr, "Trying to free world entity\n");
254 if (e >= (qcint_t)vec_size(prog->entitypool)) {
256 fprintf(stderr, "Trying to free out of bounds entity\n");
259 if (!prog->entitypool[e]) {
261 fprintf(stderr, "Double free on entity\n");
264 prog->entitypool[e] = false;
267 qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
268 size_t len = strlen(str);
269 size_t at = prog->tempstring_at;
271 /* when we reach the end we start over */
272 if (at + len >= vec_size(prog->strings))
273 at = prog->tempstring_start;
275 /* when it doesn't fit, reallocate */
276 if (at + len >= vec_size(prog->strings))
278 (void)vec_add(prog->strings, len+1);
279 memcpy(prog->strings + at, str, len+1);
283 /* when it fits, just copy */
284 memcpy(prog->strings + at, str, len+1);
285 prog->tempstring_at += len+1;
289 static size_t print_escaped_string(const char *str, size_t maxlen) {
292 --maxlen; /* because we're lazy and have escape sequences */
302 case '\a': len += 2; putchar('\\'); putchar('a'); break;
303 case '\b': len += 2; putchar('\\'); putchar('b'); break;
304 case '\r': len += 2; putchar('\\'); putchar('r'); break;
305 case '\n': len += 2; putchar('\\'); putchar('n'); break;
306 case '\t': len += 2; putchar('\\'); putchar('t'); break;
307 case '\f': len += 2; putchar('\\'); putchar('f'); break;
308 case '\v': len += 2; putchar('\\'); putchar('v'); break;
309 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
310 case '"': len += 2; putchar('\\'); putchar('"'); break;
322 static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
323 static char spaces[28+1] = " ";
324 prog_section_def_t *def;
329 if ((len = printf("<null>,")) == -1)
335 def = prog_getdef(prog, glob);
336 value = (qcany_t*)(&prog->globals[glob]);
338 len = printf("[@%u] ", glob);
340 const char *name = prog_getstring(prog, def->name);
344 len += printf("%s ", name);
345 vtype = def->type & DEF_TYPEMASK;
354 len += printf("(%i),", value->_int);
357 len += printf("'%g %g %g',", value->vector[0],
363 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
365 len += printf("(null)");
367 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
371 len += printf("%g,", value->_float);
375 if (len < (int)sizeof(spaces)-1) {
376 spaces[sizeof(spaces)-1-len] = 0;
377 fputs(spaces, stdout);
378 spaces[sizeof(spaces)-1-len] = ' ';
382 static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
383 if (st->opcode >= VINSTR_END) {
384 printf("<illegal instruction %d>\n", st->opcode);
387 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
389 for (i = 0; i < vec_size(prog->function_stack); ++i)
391 printf("%s:", vec_last(prog->function_stack));
393 printf(" <> %-12s", util_instr_str[st->opcode]);
394 if (st->opcode >= INSTR_IF &&
395 st->opcode <= INSTR_IFNOT)
397 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
398 printf("%d\n", st->o2.s1);
400 else if (st->opcode >= INSTR_CALL0 &&
401 st->opcode <= INSTR_CALL8)
403 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
406 else if (st->opcode == INSTR_GOTO)
408 printf("%i\n", st->o1.s1);
412 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
416 t[1] = t[2] = TYPE_VECTOR;
419 t[0] = t[2] = TYPE_VECTOR;
422 t[0] = t[1] = TYPE_VECTOR;
428 t[0] = t[1] = t[2] = TYPE_VECTOR;
432 t[0] = t[1] = TYPE_STRING;
439 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
442 t[0] = t[1] = TYPE_STRING; t[2] = -1;
444 case INSTR_STORE_ENT:
445 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
447 case INSTR_STORE_FLD:
448 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
450 case INSTR_STORE_FNC:
451 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
454 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
457 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
459 case INSTR_STOREP_ENT:
460 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
462 case INSTR_STOREP_FLD:
463 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
465 case INSTR_STOREP_FNC:
466 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
469 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
470 else printf("(none), ");
471 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
472 else printf("(none), ");
473 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
474 else printf("(none)");
479 static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
485 st.localsp = vec_size(prog->localstack);
486 st.stmt = prog->statement;
489 if (prog->xflags & VMXF_TRACE) {
490 const char *str = prog_getstring(prog, func->name);
491 vec_push(prog->function_stack, str);
494 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
495 if (vec_size(prog->stack))
497 prog_section_function_t *cur;
498 cur = prog->stack[vec_size(prog->stack)-1].function;
501 qcint_t *globals = prog->globals + cur->firstlocal;
502 vec_append(prog->localstack, cur->locals, globals);
507 qcint_t *globals = prog->globals + func->firstlocal;
508 vec_append(prog->localstack, func->locals, globals);
512 /* copy parameters */
513 parampos = func->firstlocal;
514 for (p = 0; p < func->nargs; ++p)
517 for (s = 0; s < func->argsize[p]; ++s) {
518 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
523 vec_push(prog->stack, st);
528 static qcint_t prog_leavefunction(qc_program_t *prog) {
529 prog_section_function_t *prev = NULL;
532 qc_exec_stack_t st = vec_last(prog->stack);
534 if (prog->xflags & VMXF_TRACE) {
535 if (vec_size(prog->function_stack))
536 vec_pop(prog->function_stack);
539 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
540 if (vec_size(prog->stack) > 1) {
541 prev = prog->stack[vec_size(prog->stack)-2].function;
542 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
545 prev = prog->stack[vec_size(prog->stack)-1].function;
546 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
549 qcint_t *globals = prog->globals + prev->firstlocal;
550 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
551 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
552 vec_shrinkto(prog->localstack, oldsp);
555 vec_pop(prog->stack);
557 return st.stmt - 1; /* offset the ++st */
560 bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
562 size_t oldxflags = prog->xflags;
563 prog_section_statement_t *st;
566 prog->xflags = flags;
568 st = prog->code + prog_enterfunction(prog, func);
576 #define QCVM_PROFILE 0
582 #define QCVM_PROFILE 0
588 #define QCVM_PROFILE 1
592 case (VMXF_TRACE|VMXF_PROFILE):
594 #define QCVM_PROFILE 1
601 prog->xflags = oldxflags;
602 vec_free(prog->localstack);
603 vec_free(prog->stack);
609 /***********************************************************************
610 * main for when building the standalone executor
615 const char *type_name[TYPE_COUNT] = {
636 struct qcvm_parameter {
641 static qcvm_parameter *main_params = NULL;
643 #define CheckArgs(num) do { \
644 if (prog->argc != (num)) { \
646 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
647 __FUNCTION__, prog->argc, (num)); \
652 #define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
653 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
654 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
656 static int qc_print(qc_program_t *prog) {
658 const char *laststr = NULL;
659 for (i = 0; i < (size_t)prog->argc; ++i) {
660 qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
661 laststr = prog_getstring(prog, str->string);
662 printf("%s", laststr);
664 if (laststr && (prog->xflags & VMXF_TRACE)) {
665 size_t len = strlen(laststr);
666 if (!len || laststr[len-1] != '\n')
672 static int qc_error(qc_program_t *prog) {
673 fprintf(stderr, "*** VM raised an error:\n");
679 static int qc_ftos(qc_program_t *prog) {
685 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
686 str.string = prog_tempstring(prog, buffer);
691 static int qc_stof(qc_program_t *prog) {
696 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
701 static int qc_vtos(qc_program_t *prog) {
707 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
708 str.string = prog_tempstring(prog, buffer);
713 static int qc_etos(qc_program_t *prog) {
719 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
720 str.string = prog_tempstring(prog, buffer);
725 static int qc_spawn(qc_program_t *prog) {
728 ent.edict = prog_spawn_entity(prog);
730 return (ent.edict ? 0 : -1);
733 static int qc_kill(qc_program_t *prog) {
737 prog_free_entity(prog, ent->edict);
741 static int qc_sqrt(qc_program_t *prog) {
745 out._float = sqrt(num->_float);
750 static int qc_vlen(qc_program_t *prog) {
754 len._float = sqrt(vec->vector[0] * vec->vector[0] +
755 vec->vector[1] * vec->vector[1] +
756 vec->vector[2] * vec->vector[2]);
761 static int qc_normalize(qc_program_t *prog) {
767 len = sqrt(vec->vector[0] * vec->vector[0] +
768 vec->vector[1] * vec->vector[1] +
769 vec->vector[2] * vec->vector[2]);
774 out.vector[0] = len * vec->vector[0];
775 out.vector[1] = len * vec->vector[1];
776 out.vector[2] = len * vec->vector[2];
781 static int qc_strcat(qc_program_t *prog) {
784 qcany_t *str1, *str2;
793 cstr1 = prog_getstring(prog, str1->string);
794 cstr2 = prog_getstring(prog, str2->string);
795 len1 = strlen(cstr1);
796 len2 = strlen(cstr2);
797 buffer = (char*)mem_a(len1 + len2 + 1);
798 memcpy(buffer, cstr1, len1);
799 memcpy(buffer+len1, cstr2, len2+1);
800 out.string = prog_tempstring(prog, buffer);
806 static int qc_strcmp(qc_program_t *prog) {
807 qcany_t *str1, *str2;
813 if (prog->argc != 2 && prog->argc != 3) {
814 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
821 cstr1 = prog_getstring(prog, str1->string);
822 cstr2 = prog_getstring(prog, str2->string);
824 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
826 out._float = strcmp(cstr1, cstr2);
831 static int qc_floor(qc_program_t *prog) {
835 out._float = floor(num->_float);
840 static int qc_pow(qc_program_t *prog) {
841 qcany_t *base, *exp, out;
845 out._float = powf(base->_float, exp->_float);
850 static prog_builtin_t qc_builtins[] = {
863 &qc_normalize, /* 12 */
869 static const char *arg0 = NULL;
871 static void version(void) {
872 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
881 static void usage(void) {
882 printf("usage: %s [options] [parameters] file\n", arg0);
883 printf("options:\n");
884 printf(" -h, --help print this message\n"
885 " -trace trace the execution\n"
886 " -profile perform profiling during execution\n"
887 " -info print information from the prog's header\n"
888 " -disasm disassemble and exit\n"
889 " -disasm-func func disassemble and exit\n"
890 " -printdefs list the defs section\n"
891 " -printfields list the field section\n"
892 " -printfuns list functions information\n"
894 " -vv be even more verbose\n");
895 printf("parameters:\n");
896 printf(" -vector <V> pass a vector parameter to main()\n"
897 " -float <f> pass a float parameter to main()\n"
898 " -string <s> pass a string parameter to main() \n");
901 static void prog_main_setparams(qc_program_t *prog) {
905 for (i = 0; i < vec_size(main_params); ++i) {
906 arg = GetGlobal(OFS_PARM0 + 3*i);
910 switch (main_params[i].vtype) {
912 (void)util_sscanf(main_params[i].value, " %f %f %f ",
918 arg->_float = atof(main_params[i].value);
921 arg->string = prog_tempstring(prog, main_params[i].value);
924 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
930 static void prog_disasm_function(qc_program_t *prog, size_t id);
932 int main(int argc, char **argv) {
936 size_t xflags = VMXF_DEFAULT;
937 bool opts_printfields = false;
938 bool opts_printdefs = false;
939 bool opts_printfuns = false;
940 bool opts_disasm = false;
941 bool opts_info = false;
943 const char *progsfile = NULL;
944 const char **dis_list = NULL;
955 if (!strcmp(argv[1], "-h") ||
956 !strcmp(argv[1], "-help") ||
957 !strcmp(argv[1], "--help"))
962 else if (!strcmp(argv[1], "-v")) {
967 else if (!strncmp(argv[1], "-vv", 3)) {
968 const char *av = argv[1]+1;
980 else if (!strcmp(argv[1], "-version") ||
981 !strcmp(argv[1], "--version"))
986 else if (!strcmp(argv[1], "-trace")) {
989 xflags |= VMXF_TRACE;
991 else if (!strcmp(argv[1], "-profile")) {
994 xflags |= VMXF_PROFILE;
996 else if (!strcmp(argv[1], "-info")) {
1002 else if (!strcmp(argv[1], "-disasm")) {
1008 else if (!strcmp(argv[1], "-disasm-func")) {
1015 vec_push(dis_list, argv[1]);
1020 else if (!strcmp(argv[1], "-printdefs")) {
1023 opts_printdefs = true;
1026 else if (!strcmp(argv[1], "-printfuns")) {
1029 opts_printfuns = true;
1032 else if (!strcmp(argv[1], "-printfields")) {
1035 opts_printfields = true;
1038 else if (!strcmp(argv[1], "-vector") ||
1039 !strcmp(argv[1], "-string") ||
1040 !strcmp(argv[1], "-float") )
1043 if (argv[1][1] == 'f')
1044 p.vtype = TYPE_FLOAT;
1045 else if (argv[1][1] == 's')
1046 p.vtype = TYPE_STRING;
1047 else if (argv[1][1] == 'v')
1048 p.vtype = TYPE_VECTOR;
1050 p.vtype = TYPE_VOID;
1060 vec_push(main_params, p);
1064 else if (!strcmp(argv[1], "--")) {
1069 else if (argv[1][0] != '-') {
1071 fprintf(stderr, "only 1 program file may be specified\n");
1075 progsfile = argv[1];
1081 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1087 if (argc == 2 && !progsfile) {
1088 progsfile = argv[1];
1094 fprintf(stderr, "must specify a program to execute\n");
1099 prog = prog_load(progsfile, noexec);
1101 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1105 prog->builtins = qc_builtins;
1106 prog->builtins_count = GMQCC_ARRAY_COUNT(qc_builtins);
1109 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1110 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1111 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1118 (unsigned long)vec_size(prog->code),
1119 (unsigned long)vec_size(prog->defs),
1120 (unsigned long)vec_size(prog->fields),
1121 (unsigned long)vec_size(prog->functions),
1122 (unsigned long)vec_size(prog->strings));
1129 for (i = 0; i < vec_size(dis_list); ++i) {
1131 printf("Looking for `%s`\n", dis_list[i]);
1132 for (k = 1; k < vec_size(prog->functions); ++k) {
1133 const char *name = prog_getstring(prog, prog->functions[k].name);
1134 if (!strcmp(name, dis_list[i])) {
1135 prog_disasm_function(prog, k);
1141 for (i = 1; i < vec_size(prog->functions); ++i)
1142 prog_disasm_function(prog, i);
1145 if (opts_printdefs) {
1146 const char *getstring = NULL;
1147 for (i = 0; i < vec_size(prog->defs); ++i) {
1148 printf("Global: %8s %-16s at %u%s",
1149 type_name[prog->defs[i].type & DEF_TYPEMASK],
1150 prog_getstring(prog, prog->defs[i].name),
1151 (unsigned int)prog->defs[i].offset,
1152 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1154 switch (prog->defs[i].type & DEF_TYPEMASK) {
1156 printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
1159 printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1165 printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1168 getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
1169 printf(" [init: `");
1170 print_escaped_string(getstring, strlen(getstring));
1180 if (opts_printfields) {
1181 for (i = 0; i < vec_size(prog->fields); ++i) {
1182 printf("Field: %8s %-16s at %u%s\n",
1183 type_name[prog->fields[i].type],
1184 prog_getstring(prog, prog->fields[i].name),
1185 (unsigned int)prog->fields[i].offset,
1186 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1189 if (opts_printfuns) {
1190 for (i = 0; i < vec_size(prog->functions); ++i) {
1192 printf("Function: %-16s taking %u parameters:(",
1193 prog_getstring(prog, prog->functions[i].name),
1194 (unsigned int)prog->functions[i].nargs);
1195 for (a = 0; a < prog->functions[i].nargs; ++a) {
1196 printf(" %i", prog->functions[i].argsize[a]);
1199 int32_t start = prog->functions[i].entry;
1201 printf(") builtin %i\n", (int)-start);
1204 prog_section_statement_t *st = prog->code + start;
1205 for (;st->opcode != INSTR_DONE; ++st)
1207 printf(") - %lu instructions", (unsigned long)funsize);
1209 printf(" - locals: %i + %i\n",
1210 prog->functions[i].firstlocal,
1211 prog->functions[i].locals);
1218 printf(") locals: %i + %i\n",
1219 prog->functions[i].firstlocal,
1220 prog->functions[i].locals);
1227 for (i = 1; i < vec_size(prog->functions); ++i) {
1228 const char *name = prog_getstring(prog, prog->functions[i].name);
1229 if (!strcmp(name, "main"))
1230 fnmain = (qcint_t)i;
1234 prog_main_setparams(prog);
1235 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1238 fprintf(stderr, "No main function found\n");
1245 static void prog_disasm_function(qc_program_t *prog, size_t id) {
1246 prog_section_function_t *fdef = prog->functions + id;
1247 prog_section_statement_t *st;
1249 if (fdef->entry < 0) {
1250 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1254 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1256 st = prog->code + fdef->entry;
1257 while (st->opcode != INSTR_DONE) {
1258 prog_print_statement(prog, st);
1262 #else /* !QCVM_LOOP */
1264 * Everything from here on is not including into the compilation of the
1265 * executor. This is simply code that is #included via #include __FILE__
1266 * see when QCVM_LOOP is defined, the rest of the code above do not get
1267 * re-included. So this really just acts like one large macro, but it
1268 * sort of isn't, which makes it nicer looking.
1271 #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
1272 #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
1273 #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
1275 #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
1277 /* to be consistent with current darkplaces behaviour */
1278 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1279 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1282 while (prog->vmerror == 0) {
1283 prog_section_function_t *newf;
1290 prog->profile[st - prog->code]++;
1294 prog_print_statement(prog, st);
1300 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1305 /* TODO: add instruction count to function profile count */
1306 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1307 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1308 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1310 st = prog->code + prog_leavefunction(prog);
1311 if (!vec_size(prog->stack))
1317 OPC->_float = OPA->_float * OPB->_float;
1320 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1321 OPA->vector[1]*OPB->vector[1] +
1322 OPA->vector[2]*OPB->vector[2];
1326 qcfloat_t f = OPA->_float;
1327 OPC->vector[0] = f * OPB->vector[0];
1328 OPC->vector[1] = f * OPB->vector[1];
1329 OPC->vector[2] = f * OPB->vector[2];
1334 qcfloat_t f = OPB->_float;
1335 OPC->vector[0] = f * OPA->vector[0];
1336 OPC->vector[1] = f * OPA->vector[1];
1337 OPC->vector[2] = f * OPA->vector[2];
1341 if (OPB->_float != 0.0f)
1342 OPC->_float = OPA->_float / OPB->_float;
1348 OPC->_float = OPA->_float + OPB->_float;
1351 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1352 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1353 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1356 OPC->_float = OPA->_float - OPB->_float;
1359 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1360 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1361 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1365 OPC->_float = (OPA->_float == OPB->_float);
1368 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1369 (OPA->vector[1] == OPB->vector[1]) &&
1370 (OPA->vector[2] == OPB->vector[2]) );
1373 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1374 prog_getstring(prog, OPB->string));
1377 OPC->_float = (OPA->_int == OPB->_int);
1380 OPC->_float = (OPA->function == OPB->function);
1383 OPC->_float = (OPA->_float != OPB->_float);
1386 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1387 (OPA->vector[1] != OPB->vector[1]) ||
1388 (OPA->vector[2] != OPB->vector[2]) );
1391 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1392 prog_getstring(prog, OPB->string));
1395 OPC->_float = (OPA->_int != OPB->_int);
1398 OPC->_float = (OPA->function != OPB->function);
1402 OPC->_float = (OPA->_float <= OPB->_float);
1405 OPC->_float = (OPA->_float >= OPB->_float);
1408 OPC->_float = (OPA->_float < OPB->_float);
1411 OPC->_float = (OPA->_float > OPB->_float);
1416 case INSTR_LOAD_FLD:
1417 case INSTR_LOAD_ENT:
1418 case INSTR_LOAD_FNC:
1419 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1420 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1423 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1424 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1429 ed = prog_getedict(prog, OPA->edict);
1430 OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
1433 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1434 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1437 if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
1439 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1444 ed = prog_getedict(prog, OPA->edict);
1445 ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
1446 OPC->ivector[0] = ptr->ivector[0];
1447 OPC->ivector[1] = ptr->ivector[1];
1448 OPC->ivector[2] = ptr->ivector[2];
1452 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1453 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1456 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1458 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1464 ed = prog_getedict(prog, OPA->edict);
1465 OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
1470 case INSTR_STORE_ENT:
1471 case INSTR_STORE_FLD:
1472 case INSTR_STORE_FNC:
1473 OPB->_int = OPA->_int;
1476 OPB->ivector[0] = OPA->ivector[0];
1477 OPB->ivector[1] = OPA->ivector[1];
1478 OPB->ivector[2] = OPA->ivector[2];
1481 case INSTR_STOREP_F:
1482 case INSTR_STOREP_S:
1483 case INSTR_STOREP_ENT:
1484 case INSTR_STOREP_FLD:
1485 case INSTR_STOREP_FNC:
1486 if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
1487 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1490 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1491 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1493 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1495 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1496 ptr->_int = OPA->_int;
1498 case INSTR_STOREP_V:
1499 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
1500 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1503 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1504 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1506 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1508 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1509 ptr->ivector[0] = OPA->ivector[0];
1510 ptr->ivector[1] = OPA->ivector[1];
1511 ptr->ivector[2] = OPA->ivector[2];
1515 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1518 OPC->_float = !OPA->vector[0] &&
1523 OPC->_float = !OPA->string ||
1524 !*prog_getstring(prog, OPA->string);
1527 OPC->_float = (OPA->edict == 0);
1530 OPC->_float = !OPA->function;
1534 /* this is consistent with darkplaces' behaviour */
1535 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1537 st += st->o2.s1 - 1; /* offset the s++ */
1538 if (++jumpcount >= maxjumps)
1539 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1543 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1545 st += st->o2.s1 - 1; /* offset the s++ */
1546 if (++jumpcount >= maxjumps)
1547 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1560 prog->argc = st->opcode - INSTR_CALL0;
1562 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1564 if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
1566 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1570 newf = &prog->functions[OPA->function];
1573 prog->statement = (st - prog->code) + 1;
1575 if (newf->entry < 0)
1577 /* negative statements are built in functions */
1578 qcint_t builtinnumber = -newf->entry;
1579 if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
1580 prog->builtins[builtinnumber](prog);
1582 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1583 builtinnumber, prog->filename);
1586 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1593 qcfloat_t *nextthink;
1596 if (!prog->supports_state) {
1597 qcvmerror(prog, "`%s` tried to execute a STATE operation but misses its defs!", prog->filename);
1600 ed = prog_getedict(prog, prog->globals[prog->cached_globals.self]);
1601 ((qcint_t*)ed)[prog->cached_fields.think] = OPB->function;
1603 frame = (qcfloat_t*)&((qcint_t*)ed)[prog->cached_fields.frame];
1604 *frame = OPA->_float;
1605 nextthink = (qcfloat_t*)&((qcint_t*)ed)[prog->cached_fields.nextthink];
1606 time = (qcfloat_t*)(prog->globals + prog->cached_globals.time);
1607 *nextthink = *time + 0.1;
1612 st += st->o1.s1 - 1; /* offset the s++ */
1613 if (++jumpcount == 10000000)
1614 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1618 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1619 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1622 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1623 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1627 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1630 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1637 #endif /* !QCVM_LOOP */