2 * Copyright (C) 2012, 2013, 2014
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 static void loaderror(const char *fmt, ...)
39 printf(": %s\n", util_strerror(err));
42 static void qcvmerror(qc_program_t *prog, const char *fmt, ...)
54 qc_program_t* prog_load(const char *filename, bool skipversion)
59 fs_file_t *file = fs_file_open(filename, "rb");
61 /* we need all those in order to support INSTR_STATE: */
62 bool has_self = false,
65 has_nextthink = false,
71 if (fs_file_read(&header, sizeof(header), 1, file) != 1) {
72 loaderror("failed to read header from '%s'", filename);
77 util_swap_header(&header);
79 if (!skipversion && header.version != 6) {
80 loaderror("header says this is a version %i progs, we need version 6\n", header.version);
85 prog = (qc_program_t*)mem_a(sizeof(qc_program_t));
88 fprintf(stderr, "failed to allocate program data\n");
91 memset(prog, 0, sizeof(*prog));
93 prog->entityfields = header.entfield;
94 prog->crc16 = header.crc16;
96 prog->filename = util_strdup(filename);
97 if (!prog->filename) {
98 loaderror("failed to store program name");
102 #define read_data(hdrvar, progvar, reserved) \
103 if (fs_file_seek(file, header.hdrvar.offset, SEEK_SET) != 0) { \
104 loaderror("seek failed"); \
108 vec_add(prog->progvar, header.hdrvar.length + reserved), \
109 sizeof(*prog->progvar), \
110 header.hdrvar.length, \
112 )!= header.hdrvar.length \
114 loaderror("read failed"); \
117 #define read_data1(x) read_data(x, x, 0)
118 #define read_data2(x, y) read_data(x, x, y)
120 read_data (statements, code, 0);
123 read_data1(functions);
125 read_data2(globals, 2); /* reserve more in case a RETURN using with the global at "the end" exists */
127 util_swap_statements (prog->code);
128 util_swap_defs_fields(prog->defs);
129 util_swap_defs_fields(prog->fields);
130 util_swap_functions (prog->functions);
131 util_swap_globals (prog->globals);
135 /* profile counters */
136 memset(vec_add(prog->profile, vec_size(prog->code)), 0, sizeof(prog->profile[0]) * vec_size(prog->code));
138 /* Add tempstring area */
139 prog->tempstring_start = vec_size(prog->strings);
140 prog->tempstring_at = vec_size(prog->strings);
141 memset(vec_add(prog->strings, 16*1024), 0, 16*1024);
143 /* spawn the world entity */
144 vec_push(prog->entitypool, true);
145 memset(vec_add(prog->entitydata, prog->entityfields), 0, prog->entityfields * sizeof(prog->entitydata[0]));
148 /* cache some globals and fields from names */
149 for (i = 0; i < vec_size(prog->defs); ++i) {
150 const char *name = prog_getstring(prog, prog->defs[i].name);
151 if (!strcmp(name, "self")) {
152 prog->cached_globals.self = prog->defs[i].offset;
155 else if (!strcmp(name, "time")) {
156 prog->cached_globals.time = prog->defs[i].offset;
160 for (i = 0; i < vec_size(prog->fields); ++i) {
161 const char *name = prog_getstring(prog, prog->fields[i].name);
162 if (!strcmp(name, "think")) {
163 prog->cached_fields.think = prog->fields[i].offset;
166 else if (!strcmp(name, "nextthink")) {
167 prog->cached_fields.nextthink = prog->fields[i].offset;
168 has_nextthink = true;
170 else if (!strcmp(name, "frame")) {
171 prog->cached_fields.frame = prog->fields[i].offset;
175 if (has_self && has_time && has_think && has_nextthink && has_frame)
176 prog->supports_state = true;
182 mem_d(prog->filename);
183 vec_free(prog->code);
184 vec_free(prog->defs);
185 vec_free(prog->fields);
186 vec_free(prog->functions);
187 vec_free(prog->strings);
188 vec_free(prog->globals);
189 vec_free(prog->entitydata);
190 vec_free(prog->entitypool);
197 void prog_delete(qc_program_t *prog)
199 if (prog->filename) mem_d(prog->filename);
200 vec_free(prog->code);
201 vec_free(prog->defs);
202 vec_free(prog->fields);
203 vec_free(prog->functions);
204 vec_free(prog->strings);
205 vec_free(prog->globals);
206 vec_free(prog->entitydata);
207 vec_free(prog->entitypool);
208 vec_free(prog->localstack);
209 vec_free(prog->stack);
210 vec_free(prog->profile);
214 /***********************************************************************
218 const char* prog_getstring(qc_program_t *prog, qcint_t str) {
219 /* cast for return required for C++ */
220 if (str < 0 || str >= (qcint_t)vec_size(prog->strings))
221 return "<<<invalid string>>>";
223 return prog->strings + str;
226 prog_section_def_t* prog_entfield(qc_program_t *prog, qcint_t off) {
228 for (i = 0; i < vec_size(prog->fields); ++i) {
229 if (prog->fields[i].offset == off)
230 return (prog->fields + i);
235 prog_section_def_t* prog_getdef(qc_program_t *prog, qcint_t off)
238 for (i = 0; i < vec_size(prog->defs); ++i) {
239 if (prog->defs[i].offset == off)
240 return (prog->defs + i);
245 qcany_t* prog_getedict(qc_program_t *prog, qcint_t e) {
246 if (e >= (qcint_t)vec_size(prog->entitypool)) {
248 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
251 return (qcany_t*)(prog->entitydata + (prog->entityfields * e));
254 static qcint_t prog_spawn_entity(qc_program_t *prog) {
257 for (e = 0; e < (qcint_t)vec_size(prog->entitypool); ++e) {
258 if (!prog->entitypool[e]) {
259 data = (char*)(prog->entitydata + (prog->entityfields * e));
260 memset(data, 0, prog->entityfields * sizeof(qcint_t));
264 vec_push(prog->entitypool, true);
266 data = (char*)vec_add(prog->entitydata, prog->entityfields);
267 memset(data, 0, prog->entityfields * sizeof(qcint_t));
271 static void prog_free_entity(qc_program_t *prog, qcint_t e) {
274 fprintf(stderr, "Trying to free world entity\n");
277 if (e >= (qcint_t)vec_size(prog->entitypool)) {
279 fprintf(stderr, "Trying to free out of bounds entity\n");
282 if (!prog->entitypool[e]) {
284 fprintf(stderr, "Double free on entity\n");
287 prog->entitypool[e] = false;
290 qcint_t prog_tempstring(qc_program_t *prog, const char *str) {
291 size_t len = strlen(str);
292 size_t at = prog->tempstring_at;
294 /* when we reach the end we start over */
295 if (at + len >= vec_size(prog->strings))
296 at = prog->tempstring_start;
298 /* when it doesn't fit, reallocate */
299 if (at + len >= vec_size(prog->strings))
301 (void)vec_add(prog->strings, len+1);
302 memcpy(prog->strings + at, str, len+1);
306 /* when it fits, just copy */
307 memcpy(prog->strings + at, str, len+1);
308 prog->tempstring_at += len+1;
312 static size_t print_escaped_string(const char *str, size_t maxlen) {
315 --maxlen; /* because we're lazy and have escape sequences */
325 case '\a': len += 2; putchar('\\'); putchar('a'); break;
326 case '\b': len += 2; putchar('\\'); putchar('b'); break;
327 case '\r': len += 2; putchar('\\'); putchar('r'); break;
328 case '\n': len += 2; putchar('\\'); putchar('n'); break;
329 case '\t': len += 2; putchar('\\'); putchar('t'); break;
330 case '\f': len += 2; putchar('\\'); putchar('f'); break;
331 case '\v': len += 2; putchar('\\'); putchar('v'); break;
332 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
333 case '"': len += 2; putchar('\\'); putchar('"'); break;
345 static void trace_print_global(qc_program_t *prog, unsigned int glob, int vtype) {
346 static char spaces[28+1] = " ";
347 prog_section_def_t *def;
352 if ((len = printf("<null>,")) == -1)
358 def = prog_getdef(prog, glob);
359 value = (qcany_t*)(&prog->globals[glob]);
361 len = printf("[@%u] ", glob);
363 const char *name = prog_getstring(prog, def->name);
367 len += printf("%s ", name);
368 vtype = def->type & DEF_TYPEMASK;
377 len += printf("(%i),", value->_int);
380 len += printf("'%g %g %g',", value->vector[0],
386 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
388 len += printf("(null)");
390 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
394 len += printf("%g,", value->_float);
398 if (len < (int)sizeof(spaces)-1) {
399 spaces[sizeof(spaces)-1-len] = 0;
400 fs_file_puts((fs_file_t*)stdout, spaces);
401 spaces[sizeof(spaces)-1-len] = ' ';
405 static void prog_print_statement(qc_program_t *prog, prog_section_statement_t *st) {
406 if (st->opcode >= VINSTR_END) {
407 printf("<illegal instruction %d>\n", st->opcode);
410 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
412 for (i = 0; i < vec_size(prog->function_stack); ++i)
414 printf("%s:", vec_last(prog->function_stack));
416 printf(" <> %-12s", util_instr_str[st->opcode]);
417 if (st->opcode >= INSTR_IF &&
418 st->opcode <= INSTR_IFNOT)
420 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
421 printf("%d\n", st->o2.s1);
423 else if (st->opcode >= INSTR_CALL0 &&
424 st->opcode <= INSTR_CALL8)
426 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
429 else if (st->opcode == INSTR_GOTO)
431 printf("%i\n", st->o1.s1);
435 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
439 t[1] = t[2] = TYPE_VECTOR;
442 t[0] = t[2] = TYPE_VECTOR;
445 t[0] = t[1] = TYPE_VECTOR;
451 t[0] = t[1] = t[2] = TYPE_VECTOR;
455 t[0] = t[1] = TYPE_STRING;
462 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
465 t[0] = t[1] = TYPE_STRING; t[2] = -1;
467 case INSTR_STORE_ENT:
468 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
470 case INSTR_STORE_FLD:
471 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
473 case INSTR_STORE_FNC:
474 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
477 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
480 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
482 case INSTR_STOREP_ENT:
483 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
485 case INSTR_STOREP_FLD:
486 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
488 case INSTR_STOREP_FNC:
489 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
492 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
493 else printf("(none), ");
494 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
495 else printf("(none), ");
496 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
497 else printf("(none)");
502 static qcint_t prog_enterfunction(qc_program_t *prog, prog_section_function_t *func) {
508 st.localsp = vec_size(prog->localstack);
509 st.stmt = prog->statement;
512 if (prog->xflags & VMXF_TRACE) {
513 const char *str = prog_getstring(prog, func->name);
514 vec_push(prog->function_stack, str);
517 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
518 if (vec_size(prog->stack))
520 prog_section_function_t *cur;
521 cur = prog->stack[vec_size(prog->stack)-1].function;
524 qcint_t *globals = prog->globals + cur->firstlocal;
525 vec_append(prog->localstack, cur->locals, globals);
530 qcint_t *globals = prog->globals + func->firstlocal;
531 vec_append(prog->localstack, func->locals, globals);
535 /* copy parameters */
536 parampos = func->firstlocal;
537 for (p = 0; p < func->nargs; ++p)
540 for (s = 0; s < func->argsize[p]; ++s) {
541 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
546 vec_push(prog->stack, st);
551 static qcint_t prog_leavefunction(qc_program_t *prog) {
552 prog_section_function_t *prev = NULL;
555 qc_exec_stack_t st = vec_last(prog->stack);
557 if (prog->xflags & VMXF_TRACE) {
558 if (vec_size(prog->function_stack))
559 vec_pop(prog->function_stack);
562 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
563 if (vec_size(prog->stack) > 1) {
564 prev = prog->stack[vec_size(prog->stack)-2].function;
565 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
568 prev = prog->stack[vec_size(prog->stack)-1].function;
569 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
572 qcint_t *globals = prog->globals + prev->firstlocal;
573 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
574 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
575 vec_shrinkto(prog->localstack, oldsp);
578 vec_pop(prog->stack);
580 return st.stmt - 1; /* offset the ++st */
583 bool prog_exec(qc_program_t *prog, prog_section_function_t *func, size_t flags, long maxjumps) {
585 size_t oldxflags = prog->xflags;
586 prog_section_statement_t *st;
589 prog->xflags = flags;
591 st = prog->code + prog_enterfunction(prog, func);
599 #define QCVM_PROFILE 0
605 #define QCVM_PROFILE 0
611 #define QCVM_PROFILE 1
615 case (VMXF_TRACE|VMXF_PROFILE):
617 #define QCVM_PROFILE 1
624 prog->xflags = oldxflags;
625 vec_free(prog->localstack);
626 vec_free(prog->stack);
632 /***********************************************************************
633 * main for when building the standalone executor
638 const char *type_name[TYPE_COUNT] = {
664 static qcvm_parameter *main_params = NULL;
666 #define CheckArgs(num) do { \
667 if (prog->argc != (num)) { \
669 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
670 __FUNCTION__, prog->argc, (num)); \
675 #define GetGlobal(idx) ((qcany_t*)(prog->globals + (idx)))
676 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
677 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
679 static int qc_print(qc_program_t *prog) {
681 const char *laststr = NULL;
682 for (i = 0; i < (size_t)prog->argc; ++i) {
683 qcany_t *str = (qcany_t*)(prog->globals + OFS_PARM0 + 3*i);
684 laststr = prog_getstring(prog, str->string);
685 printf("%s", laststr);
687 if (laststr && (prog->xflags & VMXF_TRACE)) {
688 size_t len = strlen(laststr);
689 if (!len || laststr[len-1] != '\n')
695 static int qc_error(qc_program_t *prog) {
696 fprintf(stderr, "*** VM raised an error:\n");
702 static int qc_ftos(qc_program_t *prog) {
708 util_snprintf(buffer, sizeof(buffer), "%g", num->_float);
709 str.string = prog_tempstring(prog, buffer);
714 static int qc_stof(qc_program_t *prog) {
719 num._float = (float)strtod(prog_getstring(prog, str->string), NULL);
724 static int qc_vtos(qc_program_t *prog) {
730 util_snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
731 str.string = prog_tempstring(prog, buffer);
736 static int qc_etos(qc_program_t *prog) {
742 util_snprintf(buffer, sizeof(buffer), "%i", num->_int);
743 str.string = prog_tempstring(prog, buffer);
748 static int qc_spawn(qc_program_t *prog) {
751 ent.edict = prog_spawn_entity(prog);
753 return (ent.edict ? 0 : -1);
756 static int qc_kill(qc_program_t *prog) {
760 prog_free_entity(prog, ent->edict);
764 static int qc_sqrt(qc_program_t *prog) {
768 out._float = sqrt(num->_float);
773 static int qc_vlen(qc_program_t *prog) {
777 len._float = sqrt(vec->vector[0] * vec->vector[0] +
778 vec->vector[1] * vec->vector[1] +
779 vec->vector[2] * vec->vector[2]);
784 static int qc_normalize(qc_program_t *prog) {
790 len = sqrt(vec->vector[0] * vec->vector[0] +
791 vec->vector[1] * vec->vector[1] +
792 vec->vector[2] * vec->vector[2]);
797 out.vector[0] = len * vec->vector[0];
798 out.vector[1] = len * vec->vector[1];
799 out.vector[2] = len * vec->vector[2];
804 static int qc_strcat(qc_program_t *prog) {
807 qcany_t *str1, *str2;
816 cstr1 = prog_getstring(prog, str1->string);
817 cstr2 = prog_getstring(prog, str2->string);
818 len1 = strlen(cstr1);
819 len2 = strlen(cstr2);
820 buffer = (char*)mem_a(len1 + len2 + 1);
821 memcpy(buffer, cstr1, len1);
822 memcpy(buffer+len1, cstr2, len2+1);
823 out.string = prog_tempstring(prog, buffer);
829 static int qc_strcmp(qc_program_t *prog) {
830 qcany_t *str1, *str2;
836 if (prog->argc != 2 && prog->argc != 3) {
837 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
844 cstr1 = prog_getstring(prog, str1->string);
845 cstr2 = prog_getstring(prog, str2->string);
847 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
849 out._float = strcmp(cstr1, cstr2);
854 static int qc_floor(qc_program_t *prog) {
858 out._float = floor(num->_float);
863 static int qc_pow(qc_program_t *prog) {
864 qcany_t *base, *exp, out;
868 out._float = powf(base->_float, exp->_float);
873 static prog_builtin_t qc_builtins[] = {
886 &qc_normalize, /* 12 */
892 static const char *arg0 = NULL;
894 static void version(void) {
895 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
904 static void usage(void) {
905 printf("usage: %s [options] [parameters] file\n", arg0);
906 printf("options:\n");
907 printf(" -h, --help print this message\n"
908 " -trace trace the execution\n"
909 " -profile perform profiling during execution\n"
910 " -info print information from the prog's header\n"
911 " -disasm disassemble and exit\n"
912 " -disasm-func func disassemble and exit\n"
913 " -printdefs list the defs section\n"
914 " -printfields list the field section\n"
915 " -printfuns list functions information\n"
917 " -vv be even more verbose\n");
918 printf("parameters:\n");
919 printf(" -vector <V> pass a vector parameter to main()\n"
920 " -float <f> pass a float parameter to main()\n"
921 " -string <s> pass a string parameter to main() \n");
924 static void prog_main_setparams(qc_program_t *prog) {
928 for (i = 0; i < vec_size(main_params); ++i) {
929 arg = GetGlobal(OFS_PARM0 + 3*i);
933 switch (main_params[i].vtype) {
935 (void)util_sscanf(main_params[i].value, " %f %f %f ",
941 arg->_float = atof(main_params[i].value);
944 arg->string = prog_tempstring(prog, main_params[i].value);
947 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
953 static void prog_disasm_function(qc_program_t *prog, size_t id);
955 int main(int argc, char **argv) {
959 size_t xflags = VMXF_DEFAULT;
960 bool opts_printfields = false;
961 bool opts_printdefs = false;
962 bool opts_printfuns = false;
963 bool opts_disasm = false;
964 bool opts_info = false;
966 const char *progsfile = NULL;
967 const char **dis_list = NULL;
978 if (!strcmp(argv[1], "-h") ||
979 !strcmp(argv[1], "-help") ||
980 !strcmp(argv[1], "--help"))
985 else if (!strcmp(argv[1], "-v")) {
990 else if (!strncmp(argv[1], "-vv", 3)) {
991 const char *av = argv[1]+1;
1003 else if (!strcmp(argv[1], "-version") ||
1004 !strcmp(argv[1], "--version"))
1009 else if (!strcmp(argv[1], "-trace")) {
1012 xflags |= VMXF_TRACE;
1014 else if (!strcmp(argv[1], "-profile")) {
1017 xflags |= VMXF_PROFILE;
1019 else if (!strcmp(argv[1], "-info")) {
1025 else if (!strcmp(argv[1], "-disasm")) {
1031 else if (!strcmp(argv[1], "-disasm-func")) {
1038 vec_push(dis_list, argv[1]);
1043 else if (!strcmp(argv[1], "-printdefs")) {
1046 opts_printdefs = true;
1049 else if (!strcmp(argv[1], "-printfuns")) {
1052 opts_printfuns = true;
1055 else if (!strcmp(argv[1], "-printfields")) {
1058 opts_printfields = true;
1061 else if (!strcmp(argv[1], "-vector") ||
1062 !strcmp(argv[1], "-string") ||
1063 !strcmp(argv[1], "-float") )
1066 if (argv[1][1] == 'f')
1067 p.vtype = TYPE_FLOAT;
1068 else if (argv[1][1] == 's')
1069 p.vtype = TYPE_STRING;
1070 else if (argv[1][1] == 'v')
1071 p.vtype = TYPE_VECTOR;
1073 p.vtype = TYPE_VOID;
1083 vec_push(main_params, p);
1087 else if (!strcmp(argv[1], "--")) {
1092 else if (argv[1][0] != '-') {
1094 fprintf(stderr, "only 1 program file may be specified\n");
1098 progsfile = argv[1];
1104 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1110 if (argc == 2 && !progsfile) {
1111 progsfile = argv[1];
1117 fprintf(stderr, "must specify a program to execute\n");
1122 prog = prog_load(progsfile, noexec);
1124 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1128 prog->builtins = qc_builtins;
1129 prog->builtins_count = GMQCC_ARRAY_COUNT(qc_builtins);
1132 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1133 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1134 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1141 (unsigned long)vec_size(prog->code),
1142 (unsigned long)vec_size(prog->defs),
1143 (unsigned long)vec_size(prog->fields),
1144 (unsigned long)vec_size(prog->functions),
1145 (unsigned long)vec_size(prog->strings));
1152 for (i = 0; i < vec_size(dis_list); ++i) {
1154 printf("Looking for `%s`\n", dis_list[i]);
1155 for (k = 1; k < vec_size(prog->functions); ++k) {
1156 const char *name = prog_getstring(prog, prog->functions[k].name);
1157 if (!strcmp(name, dis_list[i])) {
1158 prog_disasm_function(prog, k);
1164 for (i = 1; i < vec_size(prog->functions); ++i)
1165 prog_disasm_function(prog, i);
1168 if (opts_printdefs) {
1169 const char *getstring = NULL;
1170 for (i = 0; i < vec_size(prog->defs); ++i) {
1171 printf("Global: %8s %-16s at %u%s",
1172 type_name[prog->defs[i].type & DEF_TYPEMASK],
1173 prog_getstring(prog, prog->defs[i].name),
1174 (unsigned int)prog->defs[i].offset,
1175 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1177 switch (prog->defs[i].type & DEF_TYPEMASK) {
1179 printf(" [init: %g]", ((qcany_t*)(prog->globals + prog->defs[i].offset))->_float);
1182 printf(" [init: %i]", (int)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1188 printf(" [init: %u]", (unsigned)( ((qcany_t*)(prog->globals + prog->defs[i].offset))->_int ));
1191 getstring = prog_getstring(prog, ((qcany_t*)(prog->globals + prog->defs[i].offset))->string);
1192 printf(" [init: `");
1193 print_escaped_string(getstring, strlen(getstring));
1203 if (opts_printfields) {
1204 for (i = 0; i < vec_size(prog->fields); ++i) {
1205 printf("Field: %8s %-16s at %u%s\n",
1206 type_name[prog->fields[i].type],
1207 prog_getstring(prog, prog->fields[i].name),
1208 (unsigned int)prog->fields[i].offset,
1209 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1212 if (opts_printfuns) {
1213 for (i = 0; i < vec_size(prog->functions); ++i) {
1215 printf("Function: %-16s taking %u parameters:(",
1216 prog_getstring(prog, prog->functions[i].name),
1217 (unsigned int)prog->functions[i].nargs);
1218 for (a = 0; a < prog->functions[i].nargs; ++a) {
1219 printf(" %i", prog->functions[i].argsize[a]);
1222 int32_t start = prog->functions[i].entry;
1224 printf(") builtin %i\n", (int)-start);
1227 prog_section_statement_t *st = prog->code + start;
1228 for (;st->opcode != INSTR_DONE; ++st)
1230 printf(") - %lu instructions", (unsigned long)funsize);
1232 printf(" - locals: %i + %i\n",
1233 prog->functions[i].firstlocal,
1234 prog->functions[i].locals);
1241 printf(") locals: %i + %i\n",
1242 prog->functions[i].firstlocal,
1243 prog->functions[i].locals);
1250 for (i = 1; i < vec_size(prog->functions); ++i) {
1251 const char *name = prog_getstring(prog, prog->functions[i].name);
1252 if (!strcmp(name, "main"))
1253 fnmain = (qcint_t)i;
1257 prog_main_setparams(prog);
1258 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1261 fprintf(stderr, "No main function found\n");
1268 static void prog_disasm_function(qc_program_t *prog, size_t id) {
1269 prog_section_function_t *fdef = prog->functions + id;
1270 prog_section_statement_t *st;
1272 if (fdef->entry < 0) {
1273 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1277 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1279 st = prog->code + fdef->entry;
1280 while (st->opcode != INSTR_DONE) {
1281 prog_print_statement(prog, st);
1285 #else /* !QCVM_LOOP */
1287 * Everything from here on is not including into the compilation of the
1288 * executor. This is simply code that is #included via #include __FILE__
1289 * see when QCVM_LOOP is defined, the rest of the code above do not get
1290 * re-included. So this really just acts like one large macro, but it
1291 * sort of isn't, which makes it nicer looking.
1294 #define OPA ( (qcany_t*) (prog->globals + st->o1.u1) )
1295 #define OPB ( (qcany_t*) (prog->globals + st->o2.u1) )
1296 #define OPC ( (qcany_t*) (prog->globals + st->o3.u1) )
1298 #define GLOBAL(x) ( (qcany_t*) (prog->globals + (x)) )
1300 /* to be consistent with current darkplaces behaviour */
1301 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1302 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1305 while (prog->vmerror == 0) {
1306 prog_section_function_t *newf;
1313 prog->profile[st - prog->code]++;
1317 prog_print_statement(prog, st);
1323 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1328 /* TODO: add instruction count to function profile count */
1329 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1330 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1331 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1333 st = prog->code + prog_leavefunction(prog);
1334 if (!vec_size(prog->stack))
1340 OPC->_float = OPA->_float * OPB->_float;
1343 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1344 OPA->vector[1]*OPB->vector[1] +
1345 OPA->vector[2]*OPB->vector[2];
1349 qcfloat_t f = OPA->_float;
1350 OPC->vector[0] = f * OPB->vector[0];
1351 OPC->vector[1] = f * OPB->vector[1];
1352 OPC->vector[2] = f * OPB->vector[2];
1357 qcfloat_t f = OPB->_float;
1358 OPC->vector[0] = f * OPA->vector[0];
1359 OPC->vector[1] = f * OPA->vector[1];
1360 OPC->vector[2] = f * OPA->vector[2];
1364 if (OPB->_float != 0.0f)
1365 OPC->_float = OPA->_float / OPB->_float;
1371 OPC->_float = OPA->_float + OPB->_float;
1374 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1375 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1376 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1379 OPC->_float = OPA->_float - OPB->_float;
1382 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1383 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1384 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1388 OPC->_float = (OPA->_float == OPB->_float);
1391 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1392 (OPA->vector[1] == OPB->vector[1]) &&
1393 (OPA->vector[2] == OPB->vector[2]) );
1396 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1397 prog_getstring(prog, OPB->string));
1400 OPC->_float = (OPA->_int == OPB->_int);
1403 OPC->_float = (OPA->function == OPB->function);
1406 OPC->_float = (OPA->_float != OPB->_float);
1409 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1410 (OPA->vector[1] != OPB->vector[1]) ||
1411 (OPA->vector[2] != OPB->vector[2]) );
1414 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1415 prog_getstring(prog, OPB->string));
1418 OPC->_float = (OPA->_int != OPB->_int);
1421 OPC->_float = (OPA->function != OPB->function);
1425 OPC->_float = (OPA->_float <= OPB->_float);
1428 OPC->_float = (OPA->_float >= OPB->_float);
1431 OPC->_float = (OPA->_float < OPB->_float);
1434 OPC->_float = (OPA->_float > OPB->_float);
1439 case INSTR_LOAD_FLD:
1440 case INSTR_LOAD_ENT:
1441 case INSTR_LOAD_FNC:
1442 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1443 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1446 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1447 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1452 ed = prog_getedict(prog, OPA->edict);
1453 OPC->_int = ((qcany_t*)( ((qcint_t*)ed) + OPB->_int ))->_int;
1456 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1457 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1460 if (OPB->_int < 0 || OPB->_int + 3 > (qcint_t)prog->entityfields)
1462 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1467 ed = prog_getedict(prog, OPA->edict);
1468 ptr = (qcany_t*)( ((qcint_t*)ed) + OPB->_int );
1469 OPC->ivector[0] = ptr->ivector[0];
1470 OPC->ivector[1] = ptr->ivector[1];
1471 OPC->ivector[2] = ptr->ivector[2];
1475 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1476 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1479 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1481 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1487 ed = prog_getedict(prog, OPA->edict);
1488 OPC->_int = ((qcint_t*)ed) - prog->entitydata + OPB->_int;
1493 case INSTR_STORE_ENT:
1494 case INSTR_STORE_FLD:
1495 case INSTR_STORE_FNC:
1496 OPB->_int = OPA->_int;
1499 OPB->ivector[0] = OPA->ivector[0];
1500 OPB->ivector[1] = OPA->ivector[1];
1501 OPB->ivector[2] = OPA->ivector[2];
1504 case INSTR_STOREP_F:
1505 case INSTR_STOREP_S:
1506 case INSTR_STOREP_ENT:
1507 case INSTR_STOREP_FLD:
1508 case INSTR_STOREP_FNC:
1509 if (OPB->_int < 0 || OPB->_int >= (qcint_t)vec_size(prog->entitydata)) {
1510 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1513 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1514 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1516 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1518 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1519 ptr->_int = OPA->_int;
1521 case INSTR_STOREP_V:
1522 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint_t)vec_size(prog->entitydata)) {
1523 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1526 if (OPB->_int < (qcint_t)prog->entityfields && !prog->allowworldwrites)
1527 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1529 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1531 ptr = (qcany_t*)(prog->entitydata + OPB->_int);
1532 ptr->ivector[0] = OPA->ivector[0];
1533 ptr->ivector[1] = OPA->ivector[1];
1534 ptr->ivector[2] = OPA->ivector[2];
1538 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1541 OPC->_float = !OPA->vector[0] &&
1546 OPC->_float = !OPA->string ||
1547 !*prog_getstring(prog, OPA->string);
1550 OPC->_float = (OPA->edict == 0);
1553 OPC->_float = !OPA->function;
1557 /* this is consistent with darkplaces' behaviour */
1558 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1560 st += st->o2.s1 - 1; /* offset the s++ */
1561 if (++jumpcount >= maxjumps)
1562 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1566 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1568 st += st->o2.s1 - 1; /* offset the s++ */
1569 if (++jumpcount >= maxjumps)
1570 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1583 prog->argc = st->opcode - INSTR_CALL0;
1585 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1587 if(!OPA->function || OPA->function >= (qcint_t)vec_size(prog->functions))
1589 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1593 newf = &prog->functions[OPA->function];
1596 prog->statement = (st - prog->code) + 1;
1598 if (newf->entry < 0)
1600 /* negative statements are built in functions */
1601 qcint_t builtinnumber = -newf->entry;
1602 if (builtinnumber < (qcint_t)prog->builtins_count && prog->builtins[builtinnumber])
1603 prog->builtins[builtinnumber](prog);
1605 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1606 builtinnumber, prog->filename);
1609 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1616 qcfloat_t *nextthink;
1619 if (!prog->supports_state) {
1620 qcvmerror(prog, "`%s` tried to execute a STATE operation but misses its defs!", prog->filename);
1623 ed = prog_getedict(prog, prog->globals[prog->cached_globals.self]);
1624 ((qcint_t*)ed)[prog->cached_fields.think] = OPB->function;
1626 frame = (qcfloat_t*)&((qcint_t*)ed)[prog->cached_fields.frame];
1627 *frame = OPA->_float;
1628 nextthink = (qcfloat_t*)&((qcint_t*)ed)[prog->cached_fields.nextthink];
1629 time = (qcfloat_t*)(prog->globals + prog->cached_globals.time);
1630 *nextthink = *time + 0.1;
1635 st += st->o1.s1 - 1; /* offset the s++ */
1636 if (++jumpcount == 10000000)
1637 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1641 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1642 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1645 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1646 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1650 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1653 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1660 #endif /* !QCVM_LOOP */