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", 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 char* prog_getstring(qc_program *prog, qcint str)
172 /* cast for return required for C++ */
173 if (str < 0 || str >= (qcint)vec_size(prog->strings))
174 return (char*)"<<<invalid string>>>";
175 return prog->strings + str;
178 prog_section_def* prog_entfield(qc_program *prog, qcint off)
181 for (i = 0; i < vec_size(prog->fields); ++i) {
182 if (prog->fields[i].offset == off)
183 return (prog->fields + i);
188 prog_section_def* prog_getdef(qc_program *prog, qcint off)
191 for (i = 0; i < vec_size(prog->defs); ++i) {
192 if (prog->defs[i].offset == off)
193 return (prog->defs + i);
198 qcany* prog_getedict(qc_program *prog, qcint e)
200 if (e >= (qcint)vec_size(prog->entitypool)) {
202 fprintf(stderr, "Accessing out of bounds edict %i\n", (int)e);
205 return (qcany*)(prog->entitydata + (prog->entityfields * e));
208 qcint prog_spawn_entity(qc_program *prog)
212 for (e = 0; e < (qcint)vec_size(prog->entitypool); ++e) {
213 if (!prog->entitypool[e]) {
214 data = (char*)(prog->entitydata + (prog->entityfields * e));
215 memset(data, 0, prog->entityfields * sizeof(qcint));
219 vec_push(prog->entitypool, true);
221 data = (char*)vec_add(prog->entitydata, prog->entityfields);
222 memset(data, 0, prog->entityfields * sizeof(qcint));
226 void prog_free_entity(qc_program *prog, qcint e)
230 fprintf(stderr, "Trying to free world entity\n");
233 if (e >= (qcint)vec_size(prog->entitypool)) {
235 fprintf(stderr, "Trying to free out of bounds entity\n");
238 if (!prog->entitypool[e]) {
240 fprintf(stderr, "Double free on entity\n");
243 prog->entitypool[e] = false;
246 qcint prog_tempstring(qc_program *prog, const char *str)
248 size_t len = strlen(str);
249 size_t at = prog->tempstring_at;
251 /* when we reach the end we start over */
252 if (at + len >= vec_size(prog->strings))
253 at = prog->tempstring_start;
255 /* when it doesn't fit, reallocate */
256 if (at + len >= vec_size(prog->strings))
258 (void)vec_add(prog->strings, len+1);
259 memcpy(prog->strings + at, str, len+1);
263 /* when it fits, just copy */
264 memcpy(prog->strings + at, str, len+1);
265 prog->tempstring_at += len+1;
269 static size_t print_escaped_string(const char *str, size_t maxlen)
273 --maxlen; /* because we're lazy and have escape sequences */
283 case '\a': len += 2; putchar('\\'); putchar('a'); break;
284 case '\b': len += 2; putchar('\\'); putchar('b'); break;
285 case '\r': len += 2; putchar('\\'); putchar('r'); break;
286 case '\n': len += 2; putchar('\\'); putchar('n'); break;
287 case '\t': len += 2; putchar('\\'); putchar('t'); break;
288 case '\f': len += 2; putchar('\\'); putchar('f'); break;
289 case '\v': len += 2; putchar('\\'); putchar('v'); break;
290 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
291 case '"': len += 2; putchar('\\'); putchar('"'); break;
303 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
305 static char spaces[28+1] = " ";
306 prog_section_def *def;
311 len = printf("<null>,");
315 def = prog_getdef(prog, glob);
316 value = (qcany*)(&prog->globals[glob]);
318 len = printf("[@%u] ", glob);
320 const char *name = prog_getstring(prog, def->name);
324 len += printf("%s ", name);
325 vtype = def->type & DEF_TYPEMASK;
334 len += printf("(%i),", value->_int);
337 len += printf("'%g %g %g',", value->vector[0],
343 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
345 len += printf("(null)");
347 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
351 len += printf("%g,", value->_float);
355 if (len < (int)sizeof(spaces)-1) {
356 spaces[sizeof(spaces)-1-len] = 0;
357 fs_file_puts(stdout, spaces);
358 spaces[sizeof(spaces)-1-len] = ' ';
362 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
364 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
365 printf("<illegal instruction %d>\n", st->opcode);
368 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
370 for (i = 0; i < vec_size(prog->function_stack); ++i)
372 printf("%s:", vec_last(prog->function_stack));
374 printf(" <> %-12s", asm_instr[st->opcode].m);
375 if (st->opcode >= INSTR_IF &&
376 st->opcode <= INSTR_IFNOT)
378 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
379 printf("%d\n", st->o2.s1);
381 else if (st->opcode >= INSTR_CALL0 &&
382 st->opcode <= INSTR_CALL8)
384 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
387 else if (st->opcode == INSTR_GOTO)
389 printf("%i\n", st->o1.s1);
393 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
397 t[1] = t[2] = TYPE_VECTOR;
400 t[0] = t[2] = TYPE_VECTOR;
403 t[0] = t[1] = TYPE_VECTOR;
409 t[0] = t[1] = t[2] = TYPE_VECTOR;
413 t[0] = t[1] = TYPE_STRING;
420 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
423 t[0] = t[1] = TYPE_STRING; t[2] = -1;
425 case INSTR_STORE_ENT:
426 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
428 case INSTR_STORE_FLD:
429 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
431 case INSTR_STORE_FNC:
432 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
435 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
438 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
440 case INSTR_STOREP_ENT:
441 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
443 case INSTR_STOREP_FLD:
444 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
446 case INSTR_STOREP_FNC:
447 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
450 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
451 else printf("(none), ");
452 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
453 else printf("(none), ");
454 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
455 else printf("(none)");
461 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
468 st.localsp = vec_size(prog->localstack);
469 st.stmt = prog->statement;
472 if (prog->xflags & VMXF_TRACE) {
473 const char *str = prog_getstring(prog, func->name);
474 vec_push(prog->function_stack, str);
477 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
478 if (vec_size(prog->stack))
480 prog_section_function *cur;
481 cur = prog->stack[vec_size(prog->stack)-1].function;
484 qcint *globals = prog->globals + cur->firstlocal;
485 vec_append(prog->localstack, cur->locals, globals);
490 qcint *globals = prog->globals + func->firstlocal;
491 vec_append(prog->localstack, func->locals, globals);
495 /* copy parameters */
496 parampos = func->firstlocal;
497 for (p = 0; p < func->nargs; ++p)
500 for (s = 0; s < func->argsize[p]; ++s) {
501 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
506 vec_push(prog->stack, st);
511 static qcint prog_leavefunction(qc_program *prog)
513 prog_section_function *prev = NULL;
516 qc_exec_stack st = vec_last(prog->stack);
518 if (prog->xflags & VMXF_TRACE) {
519 if (vec_size(prog->function_stack))
520 vec_pop(prog->function_stack);
523 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
524 if (vec_size(prog->stack) > 1) {
525 prev = prog->stack[vec_size(prog->stack)-2].function;
526 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
529 prev = prog->stack[vec_size(prog->stack)-1].function;
530 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
533 qcint *globals = prog->globals + prev->firstlocal;
534 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
535 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
536 vec_shrinkto(prog->localstack, oldsp);
539 vec_pop(prog->stack);
541 return st.stmt - 1; /* offset the ++st */
544 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
547 size_t oldxflags = prog->xflags;
548 prog_section_statement *st;
551 prog->xflags = flags;
553 st = prog->code + prog_enterfunction(prog, func);
561 #define QCVM_PROFILE 0
567 #define QCVM_PROFILE 0
573 #define QCVM_PROFILE 1
577 case (VMXF_TRACE|VMXF_PROFILE):
579 #define QCVM_PROFILE 1
586 prog->xflags = oldxflags;
587 vec_free(prog->localstack);
588 vec_free(prog->stack);
594 /***********************************************************************
595 * main for when building the standalone executor
598 #if defined(QCVM_EXECUTOR)
603 const char *type_name[TYPE_COUNT] = {
629 qcvm_parameter *main_params = NULL;
631 #define CheckArgs(num) do { \
632 if (prog->argc != (num)) { \
634 fprintf(stderr, "ERROR: invalid number of arguments for %s: %i, expected %i\n", \
635 __FUNCTION__, prog->argc, (num)); \
640 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
641 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
642 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
644 static int qc_print(qc_program *prog)
647 const char *laststr = NULL;
648 for (i = 0; i < (size_t)prog->argc; ++i) {
649 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
650 laststr = prog_getstring(prog, str->string);
651 printf("%s", laststr);
653 if (laststr && (prog->xflags & VMXF_TRACE)) {
654 size_t len = strlen(laststr);
655 if (!len || laststr[len-1] != '\n')
661 static int qc_error(qc_program *prog)
663 fprintf(stderr, "*** VM raised an error:\n");
669 static int qc_ftos(qc_program *prog)
676 snprintf(buffer, sizeof(buffer), "%g", num->_float);
677 str.string = prog_tempstring(prog, buffer);
682 static int qc_stof(qc_program *prog)
688 num._float = strtof(prog_getstring(prog, str->string), NULL);
693 static int qc_vtos(qc_program *prog)
700 snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
701 str.string = prog_tempstring(prog, buffer);
706 static int qc_etos(qc_program *prog)
713 snprintf(buffer, sizeof(buffer), "%i", num->_int);
714 str.string = prog_tempstring(prog, buffer);
719 static int qc_spawn(qc_program *prog)
723 ent.edict = prog_spawn_entity(prog);
725 return (ent.edict ? 0 : -1);
728 static int qc_kill(qc_program *prog)
733 prog_free_entity(prog, ent->edict);
737 static int qc_sqrt(qc_program *prog)
742 out._float = sqrt(num->_float);
747 static int qc_vlen(qc_program *prog)
752 len._float = sqrt(vec->vector[0] * vec->vector[0] +
753 vec->vector[1] * vec->vector[1] +
754 vec->vector[2] * vec->vector[2]);
759 static int qc_normalize(qc_program *prog)
766 len = sqrt(vec->vector[0] * vec->vector[0] +
767 vec->vector[1] * vec->vector[1] +
768 vec->vector[2] * vec->vector[2]);
773 out.vector[0] = len * vec->vector[0];
774 out.vector[1] = len * vec->vector[1];
775 out.vector[2] = len * vec->vector[2];
780 static int qc_strcat(qc_program *prog)
791 cstr1 = prog_getstring(prog, str1->string);
792 cstr2 = prog_getstring(prog, str2->string);
793 len1 = strlen(cstr1);
794 len2 = strlen(cstr2);
795 buffer = (char*)mem_a(len1 + len2 + 1);
796 memcpy(buffer, cstr1, len1);
797 memcpy(buffer+len1, cstr2, len2+1);
798 out.string = prog_tempstring(prog, buffer);
804 static int qc_strcmp(qc_program *prog)
810 if (prog->argc != 2 && prog->argc != 3) {
811 fprintf(stderr, "ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
818 cstr1 = prog_getstring(prog, str1->string);
819 cstr2 = prog_getstring(prog, str2->string);
821 out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
823 out._float = strcmp(cstr1, cstr2);
828 static int qc_floor(qc_program *prog)
833 out._float = floor(num->_float);
838 static prog_builtin qc_builtins[] = {
851 &qc_normalize, /* 12 */
855 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
857 static const char *arg0 = NULL;
859 static void version() {
860 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
871 printf("usage: %s [options] [parameters] file\n", arg0);
872 printf("options:\n");
873 printf(" -h, --help print this message\n"
874 " -trace trace the execution\n"
875 " -profile perform profiling during execution\n"
876 " -info print information from the prog's header\n"
877 " -disasm disassemble and exit\n"
878 " -disasm-func func disassemble and exit\n"
879 " -printdefs list the defs section\n"
880 " -printfields list the field section\n"
881 " -printfuns list functions information\n"
883 " -vv be even more verbose\n");
884 printf("parameters:\n");
885 printf(" -vector <V> pass a vector parameter to main()\n"
886 " -float <f> pass a float parameter to main()\n"
887 " -string <s> pass a string parameter to main() \n");
890 static void prog_main_setparams(qc_program *prog)
895 for (i = 0; i < vec_size(main_params); ++i) {
896 arg = GetGlobal(OFS_PARM0 + 3*i);
900 switch (main_params[i].vtype) {
903 (void)sscanf_s(main_params[i].value, " %f %f %f ",
908 (void)sscanf(main_params[i].value, " %f %f %f ",
915 arg->_float = atof(main_params[i].value);
918 arg->string = prog_tempstring(prog, main_params[i].value);
921 fprintf(stderr, "error: unhandled parameter type: %i\n", main_params[i].vtype);
927 void prog_disasm_function(qc_program *prog, size_t id);
928 int main(int argc, char **argv)
933 size_t xflags = VMXF_DEFAULT;
934 bool opts_printfields = false;
935 bool opts_printdefs = false;
936 bool opts_printfuns = false;
937 bool opts_disasm = false;
938 bool opts_info = false;
940 const char *progsfile = NULL;
941 const char **dis_list = NULL;
952 if (!strcmp(argv[1], "-h") ||
953 !strcmp(argv[1], "-help") ||
954 !strcmp(argv[1], "--help"))
959 else if (!strcmp(argv[1], "-v")) {
964 else if (!strncmp(argv[1], "-vv", 3)) {
965 const char *av = argv[1]+1;
977 else if (!strcmp(argv[1], "-version") ||
978 !strcmp(argv[1], "--version"))
983 else if (!strcmp(argv[1], "-trace")) {
986 xflags |= VMXF_TRACE;
988 else if (!strcmp(argv[1], "-profile")) {
991 xflags |= VMXF_PROFILE;
993 else if (!strcmp(argv[1], "-info")) {
999 else if (!strcmp(argv[1], "-disasm")) {
1005 else if (!strcmp(argv[1], "-disasm-func")) {
1012 vec_push(dis_list, argv[1]);
1017 else if (!strcmp(argv[1], "-printdefs")) {
1020 opts_printdefs = true;
1023 else if (!strcmp(argv[1], "-printfuns")) {
1026 opts_printfuns = true;
1029 else if (!strcmp(argv[1], "-printfields")) {
1032 opts_printfields = true;
1035 else if (!strcmp(argv[1], "-vector") ||
1036 !strcmp(argv[1], "-string") ||
1037 !strcmp(argv[1], "-float") )
1040 if (argv[1][1] == 'f')
1041 p.vtype = TYPE_FLOAT;
1042 else if (argv[1][1] == 's')
1043 p.vtype = TYPE_STRING;
1044 else if (argv[1][1] == 'v')
1045 p.vtype = TYPE_VECTOR;
1055 vec_push(main_params, p);
1059 else if (!strcmp(argv[1], "--")) {
1064 else if (argv[1][0] != '-') {
1066 fprintf(stderr, "only 1 program file may be specified\n");
1070 progsfile = argv[1];
1076 fprintf(stderr, "unknown parameter: %s\n", argv[1]);
1082 if (argc == 2 && !progsfile) {
1083 progsfile = argv[1];
1089 fprintf(stderr, "must specify a program to execute\n");
1094 prog = prog_load(progsfile, noexec);
1096 fprintf(stderr, "failed to load program '%s'\n", progsfile);
1100 prog->builtins = qc_builtins;
1101 prog->builtins_count = qc_builtins_count;
1104 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
1105 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
1106 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
1113 (unsigned long)vec_size(prog->code),
1114 (unsigned long)vec_size(prog->defs),
1115 (unsigned long)vec_size(prog->fields),
1116 (unsigned long)vec_size(prog->functions),
1117 (unsigned long)vec_size(prog->strings));
1124 for (i = 0; i < vec_size(dis_list); ++i) {
1126 printf("Looking for `%s`\n", dis_list[i]);
1127 for (k = 1; k < vec_size(prog->functions); ++k) {
1128 const char *name = prog_getstring(prog, prog->functions[k].name);
1129 if (!strcmp(name, dis_list[i])) {
1130 prog_disasm_function(prog, k);
1136 for (i = 1; i < vec_size(prog->functions); ++i)
1137 prog_disasm_function(prog, i);
1140 if (opts_printdefs) {
1141 for (i = 0; i < vec_size(prog->defs); ++i) {
1142 printf("Global: %8s %-16s at %u%s",
1143 type_name[prog->defs[i].type & DEF_TYPEMASK],
1144 prog_getstring(prog, prog->defs[i].name),
1145 (unsigned int)prog->defs[i].offset,
1146 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1148 switch (prog->defs[i].type & DEF_TYPEMASK) {
1150 printf(" [init: %g]", ((qcany*)(prog->globals + prog->defs[i].offset))->_float);
1153 printf(" [init: %i]", (int)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1159 printf(" [init: %u]", (unsigned)( ((qcany*)(prog->globals + prog->defs[i].offset))->_int ));
1162 printf(" [init: `%s`]", prog_getstring(prog, ((qcany*)(prog->globals + prog->defs[i].offset))->string ));
1171 if (opts_printfields) {
1172 for (i = 0; i < vec_size(prog->fields); ++i) {
1173 printf("Field: %8s %-16s at %u%s\n",
1174 type_name[prog->fields[i].type],
1175 prog_getstring(prog, prog->fields[i].name),
1176 (unsigned int)prog->fields[i].offset,
1177 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1180 if (opts_printfuns) {
1181 for (i = 0; i < vec_size(prog->functions); ++i) {
1183 printf("Function: %-16s taking %i parameters:(",
1184 prog_getstring(prog, prog->functions[i].name),
1185 (unsigned int)prog->functions[i].nargs);
1186 for (a = 0; a < prog->functions[i].nargs; ++a) {
1187 printf(" %i", prog->functions[i].argsize[a]);
1190 int32_t start = prog->functions[i].entry;
1192 printf(") builtin %i\n", (int)-start);
1195 prog_section_statement *st = prog->code + start;
1196 for (;st->opcode != INSTR_DONE; ++st)
1198 printf(") - %lu instructions", (unsigned long)funsize);
1200 printf(" - locals: %i + %i\n",
1201 prog->functions[i].firstlocal,
1202 prog->functions[i].locals);
1209 printf(") locals: %i + %i\n",
1210 prog->functions[i].firstlocal,
1211 prog->functions[i].locals);
1218 for (i = 1; i < vec_size(prog->functions); ++i) {
1219 const char *name = prog_getstring(prog, prog->functions[i].name);
1220 if (!strcmp(name, "main"))
1225 prog_main_setparams(prog);
1226 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1229 fprintf(stderr, "No main function found\n");
1236 void prog_disasm_function(qc_program *prog, size_t id)
1238 prog_section_function *fdef = prog->functions + id;
1239 prog_section_statement *st;
1241 if (fdef->entry < 0) {
1242 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1246 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1248 st = prog->code + fdef->entry;
1249 while (st->opcode != INSTR_DONE) {
1250 prog_print_statement(prog, st);
1255 #else /* !QCVM_LOOP */
1257 * Everything from here on is not including into the compilation of the
1258 * executor. This is simply code that is #included via #include __FILE__
1259 * see when QCVM_LOOP is defined, the rest of the code above do not get
1260 * re-included. So this really just acts like one large macro, but it
1261 * sort of isn't, which makes it nicer looking.
1264 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1265 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1266 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1268 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1270 /* to be consistent with current darkplaces behaviour */
1271 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1272 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1276 prog_section_function *newf;
1283 prog->profile[st - prog->code]++;
1287 prog_print_statement(prog, st);
1293 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1298 /* TODO: add instruction count to function profile count */
1299 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1300 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1301 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1303 st = prog->code + prog_leavefunction(prog);
1304 if (!vec_size(prog->stack))
1310 OPC->_float = OPA->_float * OPB->_float;
1313 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1314 OPA->vector[1]*OPB->vector[1] +
1315 OPA->vector[2]*OPB->vector[2];
1319 qcfloat f = OPA->_float;
1320 OPC->vector[0] = f * OPB->vector[0];
1321 OPC->vector[1] = f * OPB->vector[1];
1322 OPC->vector[2] = f * OPB->vector[2];
1327 qcfloat f = OPB->_float;
1328 OPC->vector[0] = f * OPA->vector[0];
1329 OPC->vector[1] = f * OPA->vector[1];
1330 OPC->vector[2] = f * OPA->vector[2];
1334 if (OPB->_float != 0.0f)
1335 OPC->_float = OPA->_float / OPB->_float;
1341 OPC->_float = OPA->_float + OPB->_float;
1344 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1345 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1346 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1349 OPC->_float = OPA->_float - OPB->_float;
1352 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1353 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1354 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1358 OPC->_float = (OPA->_float == OPB->_float);
1361 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1362 (OPA->vector[1] == OPB->vector[1]) &&
1363 (OPA->vector[2] == OPB->vector[2]) );
1366 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1367 prog_getstring(prog, OPB->string));
1370 OPC->_float = (OPA->_int == OPB->_int);
1373 OPC->_float = (OPA->function == OPB->function);
1376 OPC->_float = (OPA->_float != OPB->_float);
1379 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1380 (OPA->vector[1] != OPB->vector[1]) ||
1381 (OPA->vector[2] != OPB->vector[2]) );
1384 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1385 prog_getstring(prog, OPB->string));
1388 OPC->_float = (OPA->_int != OPB->_int);
1391 OPC->_float = (OPA->function != OPB->function);
1395 OPC->_float = (OPA->_float <= OPB->_float);
1398 OPC->_float = (OPA->_float >= OPB->_float);
1401 OPC->_float = (OPA->_float < OPB->_float);
1404 OPC->_float = (OPA->_float > OPB->_float);
1409 case INSTR_LOAD_FLD:
1410 case INSTR_LOAD_ENT:
1411 case INSTR_LOAD_FNC:
1412 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1413 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1416 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1417 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1422 ed = prog_getedict(prog, OPA->edict);
1423 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1426 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1427 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1430 if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1432 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1437 ed = prog_getedict(prog, OPA->edict);
1438 ptr = (qcany*)( ((qcint*)ed) + OPB->_int );
1439 OPC->ivector[0] = ptr->ivector[0];
1440 OPC->ivector[1] = ptr->ivector[1];
1441 OPC->ivector[2] = ptr->ivector[2];
1445 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1446 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1449 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1451 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1457 ed = prog_getedict(prog, OPA->edict);
1458 OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1463 case INSTR_STORE_ENT:
1464 case INSTR_STORE_FLD:
1465 case INSTR_STORE_FNC:
1466 OPB->_int = OPA->_int;
1469 OPB->ivector[0] = OPA->ivector[0];
1470 OPB->ivector[1] = OPA->ivector[1];
1471 OPB->ivector[2] = OPA->ivector[2];
1474 case INSTR_STOREP_F:
1475 case INSTR_STOREP_S:
1476 case INSTR_STOREP_ENT:
1477 case INSTR_STOREP_FLD:
1478 case INSTR_STOREP_FNC:
1479 if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1480 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1483 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1484 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1486 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1488 ptr = (qcany*)(prog->entitydata + OPB->_int);
1489 ptr->_int = OPA->_int;
1491 case INSTR_STOREP_V:
1492 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1493 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1496 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1497 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1499 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1501 ptr = (qcany*)(prog->entitydata + OPB->_int);
1502 ptr->ivector[0] = OPA->ivector[0];
1503 ptr->ivector[1] = OPA->ivector[1];
1504 ptr->ivector[2] = OPA->ivector[2];
1508 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1511 OPC->_float = !OPA->vector[0] &&
1516 OPC->_float = !OPA->string ||
1517 !*prog_getstring(prog, OPA->string);
1520 OPC->_float = (OPA->edict == 0);
1523 OPC->_float = !OPA->function;
1527 /* this is consistent with darkplaces' behaviour */
1528 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1530 st += st->o2.s1 - 1; /* offset the s++ */
1531 if (++jumpcount >= maxjumps)
1532 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1536 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1538 st += st->o2.s1 - 1; /* offset the s++ */
1539 if (++jumpcount >= maxjumps)
1540 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1553 prog->argc = st->opcode - INSTR_CALL0;
1555 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1557 if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1559 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1563 newf = &prog->functions[OPA->function];
1566 prog->statement = (st - prog->code) + 1;
1568 if (newf->entry < 0)
1570 /* negative statements are built in functions */
1571 qcint builtinnumber = -newf->entry;
1572 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1573 prog->builtins[builtinnumber](prog);
1575 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1576 builtinnumber, prog->filename);
1579 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1585 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1589 st += st->o1.s1 - 1; /* offset the s++ */
1590 if (++jumpcount == 10000000)
1591 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1595 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1596 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1599 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1600 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1604 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1607 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1614 #endif /* !QCVM_LOOP */