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)
58 FILE *file = file_open(filename, "rb");
63 if (file_read(&header, sizeof(header), 1, file) != 1) {
64 loaderror("failed to read header from '%s'", filename);
69 if (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 printf("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 (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 printf("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 printf("Trying to free world entity\n");
233 if (e >= (qcint)vec_size(prog->entitypool)) {
235 printf("Trying to free out of bounds entity\n");
238 if (!prog->entitypool[e]) {
240 printf("Double free on entity\n");
243 prog->entitypool[e] = false;
246 qcint prog_tempstring(qc_program *prog, const char *_str)
248 /* we don't access it, but the macro-generated functions don't use
251 char *str = (char*)_str;
253 size_t len = strlen(str);
254 size_t at = prog->tempstring_at;
256 /* when we reach the end we start over */
257 if (at + len >= vec_size(prog->strings))
258 at = prog->tempstring_start;
260 /* when it doesn't fit, reallocate */
261 if (at + len >= vec_size(prog->strings))
263 (void)vec_add(prog->strings, len+1);
264 memcpy(prog->strings + at, str, len+1);
268 /* when it fits, just copy */
269 memcpy(prog->strings + at, str, len+1);
270 prog->tempstring_at += len+1;
274 static size_t print_escaped_string(const char *str, size_t maxlen)
278 --maxlen; /* because we're lazy and have escape sequences */
288 case '\a': len += 2; putchar('\\'); putchar('a'); break;
289 case '\b': len += 2; putchar('\\'); putchar('b'); break;
290 case '\r': len += 2; putchar('\\'); putchar('r'); break;
291 case '\n': len += 2; putchar('\\'); putchar('n'); break;
292 case '\t': len += 2; putchar('\\'); putchar('t'); break;
293 case '\f': len += 2; putchar('\\'); putchar('f'); break;
294 case '\v': len += 2; putchar('\\'); putchar('v'); break;
295 case '\\': len += 2; putchar('\\'); putchar('\\'); break;
296 case '"': len += 2; putchar('\\'); putchar('"'); break;
308 static void trace_print_global(qc_program *prog, unsigned int glob, int vtype)
310 static char spaces[28+1] = " ";
311 prog_section_def *def;
316 len = printf("<null>,");
320 def = prog_getdef(prog, glob);
321 value = (qcany*)(&prog->globals[glob]);
323 len = printf("[@%u] ", glob);
325 const char *name = prog_getstring(prog, def->name);
329 len += printf("%s ", name);
330 vtype = def->type & DEF_TYPEMASK;
339 len += printf("(%i),", value->_int);
342 len += printf("'%g %g %g',", value->vector[0],
348 len += print_escaped_string(prog_getstring(prog, value->string), sizeof(spaces)-len-5);
350 len += printf("(null)");
352 /* len += printf("\"%s\",", prog_getstring(prog, value->string)); */
356 len += printf("%g,", value->_float);
360 if (len < (int)sizeof(spaces)-1) {
361 spaces[sizeof(spaces)-1-len] = 0;
363 spaces[sizeof(spaces)-1-len] = ' ';
367 static void prog_print_statement(qc_program *prog, prog_section_statement *st)
369 if (st->opcode >= (sizeof(asm_instr)/sizeof(asm_instr[0]))) {
370 printf("<illegal instruction %d>\n", st->opcode);
373 if ((prog->xflags & VMXF_TRACE) && vec_size(prog->function_stack)) {
375 for (i = 0; i < vec_size(prog->function_stack); ++i)
377 printf("%s:", vec_last(prog->function_stack));
379 printf(" <> %-12s", asm_instr[st->opcode].m);
380 if (st->opcode >= INSTR_IF &&
381 st->opcode <= INSTR_IFNOT)
383 trace_print_global(prog, st->o1.u1, TYPE_FLOAT);
384 printf("%d\n", st->o2.s1);
386 else if (st->opcode >= INSTR_CALL0 &&
387 st->opcode <= INSTR_CALL8)
389 trace_print_global(prog, st->o1.u1, TYPE_FUNCTION);
392 else if (st->opcode == INSTR_GOTO)
394 printf("%i\n", st->o1.s1);
398 int t[3] = { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT };
402 t[1] = t[2] = TYPE_VECTOR;
405 t[0] = t[2] = TYPE_VECTOR;
408 t[0] = t[1] = TYPE_VECTOR;
414 t[0] = t[1] = t[2] = TYPE_VECTOR;
418 t[0] = t[1] = TYPE_STRING;
425 t[0] = t[1] = TYPE_VECTOR; t[2] = -1;
428 t[0] = t[1] = TYPE_STRING; t[2] = -1;
430 case INSTR_STORE_ENT:
431 t[0] = t[1] = TYPE_ENTITY; t[2] = -1;
433 case INSTR_STORE_FLD:
434 t[0] = t[1] = TYPE_FIELD; t[2] = -1;
436 case INSTR_STORE_FNC:
437 t[0] = t[1] = TYPE_FUNCTION; t[2] = -1;
440 t[0] = TYPE_VECTOR; t[1] = TYPE_ENTITY; t[2] = -1;
443 t[0] = TYPE_STRING; t[1] = TYPE_ENTITY; t[2] = -1;
445 case INSTR_STOREP_ENT:
446 t[0] = TYPE_ENTITY; t[1] = TYPE_ENTITY; t[2] = -1;
448 case INSTR_STOREP_FLD:
449 t[0] = TYPE_FIELD; t[1] = TYPE_ENTITY; t[2] = -1;
451 case INSTR_STOREP_FNC:
452 t[0] = TYPE_FUNCTION; t[1] = TYPE_ENTITY; t[2] = -1;
455 if (t[0] >= 0) trace_print_global(prog, st->o1.u1, t[0]);
456 else printf("(none), ");
457 if (t[1] >= 0) trace_print_global(prog, st->o2.u1, t[1]);
458 else printf("(none), ");
459 if (t[2] >= 0) trace_print_global(prog, st->o3.u1, t[2]);
460 else printf("(none)");
466 static qcint prog_enterfunction(qc_program *prog, prog_section_function *func)
473 st.localsp = vec_size(prog->localstack);
474 st.stmt = prog->statement;
477 if (prog->xflags & VMXF_TRACE) {
478 vec_push(prog->function_stack, prog_getstring(prog, func->name));
481 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
482 if (vec_size(prog->stack))
484 prog_section_function *cur;
485 cur = prog->stack[vec_size(prog->stack)-1].function;
488 qcint *globals = prog->globals + cur->firstlocal;
489 vec_append(prog->localstack, cur->locals, globals);
494 qcint *globals = prog->globals + func->firstlocal;
495 vec_append(prog->localstack, func->locals, globals);
499 /* copy parameters */
500 parampos = func->firstlocal;
501 for (p = 0; p < func->nargs; ++p)
504 for (s = 0; s < func->argsize[p]; ++s) {
505 prog->globals[parampos] = prog->globals[OFS_PARM0 + 3*p + s];
510 vec_push(prog->stack, st);
515 static qcint prog_leavefunction(qc_program *prog)
517 prog_section_function *prev = NULL;
520 qc_exec_stack st = vec_last(prog->stack);
522 if (prog->xflags & VMXF_TRACE) {
523 if (vec_size(prog->function_stack))
524 vec_pop(prog->function_stack);
527 #ifdef QCVM_BACKUP_STRATEGY_CALLER_VARS
528 if (vec_size(prog->stack) > 1) {
529 prev = prog->stack[vec_size(prog->stack)-2].function;
530 oldsp = prog->stack[vec_size(prog->stack)-2].localsp;
533 prev = prog->stack[vec_size(prog->stack)-1].function;
534 oldsp = prog->stack[vec_size(prog->stack)-1].localsp;
537 qcint *globals = prog->globals + prev->firstlocal;
538 memcpy(globals, prog->localstack + oldsp, prev->locals * sizeof(prog->localstack[0]));
539 /* vec_remove(prog->localstack, oldsp, vec_size(prog->localstack)-oldsp); */
540 vec_shrinkto(prog->localstack, oldsp);
543 vec_pop(prog->stack);
545 return st.stmt - 1; /* offset the ++st */
548 bool prog_exec(qc_program *prog, prog_section_function *func, size_t flags, long maxjumps)
551 size_t oldxflags = prog->xflags;
552 prog_section_statement *st;
555 prog->xflags = flags;
557 st = prog->code + prog_enterfunction(prog, func);
565 #define QCVM_PROFILE 0
571 #define QCVM_PROFILE 0
577 #define QCVM_PROFILE 1
581 case (VMXF_TRACE|VMXF_PROFILE):
583 #define QCVM_PROFILE 1
590 prog->xflags = oldxflags;
591 vec_free(prog->localstack);
592 vec_free(prog->stack);
598 /***********************************************************************
599 * main for when building the standalone executor
602 #if defined(QCVM_EXECUTOR)
607 const char *type_name[TYPE_COUNT] = {
627 qcvm_parameter *main_params = NULL;
629 #define CheckArgs(num) do { \
630 if (prog->argc != (num)) { \
632 printf("ERROR: invalid number of arguments for %s: %i, expected %i\n", \
633 __FUNCTION__, prog->argc, (num)); \
638 #define GetGlobal(idx) ((qcany*)(prog->globals + (idx)))
639 #define GetArg(num) GetGlobal(OFS_PARM0 + 3*(num))
640 #define Return(any) *(GetGlobal(OFS_RETURN)) = (any)
642 static int qc_print(qc_program *prog)
645 const char *laststr = NULL;
646 for (i = 0; i < (size_t)prog->argc; ++i) {
647 qcany *str = (qcany*)(prog->globals + OFS_PARM0 + 3*i);
648 laststr = prog_getstring(prog, str->string);
649 printf("%s", laststr);
651 if (laststr && (prog->xflags & VMXF_TRACE)) {
652 size_t len = strlen(laststr);
653 if (!len || laststr[len-1] != '\n')
659 static int qc_error(qc_program *prog)
661 printf("*** VM raised an error:\n");
667 static int qc_ftos(qc_program *prog)
674 snprintf(buffer, sizeof(buffer), "%g", num->_float);
675 str.string = prog_tempstring(prog, buffer);
680 static int qc_stof(qc_program *prog)
686 num._float = strtof(prog_getstring(prog, str->string), NULL);
691 static int qc_vtos(qc_program *prog)
698 snprintf(buffer, sizeof(buffer), "'%g %g %g'", num->vector[0], num->vector[1], num->vector[2]);
699 str.string = prog_tempstring(prog, buffer);
704 static int qc_etos(qc_program *prog)
711 snprintf(buffer, sizeof(buffer), "%i", num->_int);
712 str.string = prog_tempstring(prog, buffer);
717 static int qc_spawn(qc_program *prog)
721 ent.edict = prog_spawn_entity(prog);
723 return (ent.edict ? 0 : -1);
726 static int qc_kill(qc_program *prog)
731 prog_free_entity(prog, ent->edict);
735 static int qc_vlen(qc_program *prog)
740 len._float = sqrt(vec->vector[0] * vec->vector[0] +
741 vec->vector[1] * vec->vector[1] +
742 vec->vector[2] * vec->vector[2]);
747 static prog_builtin qc_builtins[] = {
759 static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);
761 static const char *arg0 = NULL;
763 static void version() {
764 printf("GMQCC-QCVM %d.%d.%d Built %s %s\n",
775 printf("usage: %s [options] [parameters] file\n", arg0);
776 printf("options:\n");
777 printf(" -h, --help print this message\n"
778 " -trace trace the execution\n"
779 " -profile perform profiling during execution\n"
780 " -info print information from the prog's header\n"
781 " -disasm disassemble and exit\n"
782 " -printdefs list the defs section\n"
783 " -printfields list the field section\n"
784 " -printfuns list functions information\n");
785 printf("parameters:\n");
786 printf(" -vector <V> pass a vector parameter to main()\n"
787 " -float <f> pass a float parameter to main()\n"
788 " -string <s> pass a string parameter to main() \n");
791 static void prog_main_setparams(qc_program *prog)
796 for (i = 0; i < vec_size(main_params); ++i) {
797 arg = GetGlobal(OFS_PARM0 + 3*i);
801 switch (main_params[i].vtype) {
804 (void)sscanf_s(main_params[i].value, " %f %f %f ",
809 (void)sscanf(main_params[i].value, " %f %f %f ",
816 arg->_float = atof(main_params[i].value);
819 arg->string = prog_tempstring(prog, main_params[i].value);
822 printf("error: unhandled parameter type: %i\n", main_params[i].vtype);
828 void prog_disasm_function(qc_program *prog, size_t id);
829 int main(int argc, char **argv)
834 size_t xflags = VMXF_DEFAULT;
835 bool opts_printfields = false;
836 bool opts_printdefs = false;
837 bool opts_printfuns = false;
838 bool opts_disasm = false;
839 bool opts_info = false;
841 const char *progsfile = NULL;
851 if (!strcmp(argv[1], "-h") ||
852 !strcmp(argv[1], "-help") ||
853 !strcmp(argv[1], "--help"))
858 else if (!strcmp(argv[1], "-v") ||
859 !strcmp(argv[1], "-version") ||
860 !strcmp(argv[1], "--version"))
865 else if (!strcmp(argv[1], "-trace")) {
868 xflags |= VMXF_TRACE;
870 else if (!strcmp(argv[1], "-profile")) {
873 xflags |= VMXF_PROFILE;
875 else if (!strcmp(argv[1], "-info")) {
881 else if (!strcmp(argv[1], "-disasm")) {
887 else if (!strcmp(argv[1], "-printdefs")) {
890 opts_printdefs = true;
893 else if (!strcmp(argv[1], "-printfuns")) {
896 opts_printfuns = true;
899 else if (!strcmp(argv[1], "-printfields")) {
902 opts_printfields = true;
905 else if (!strcmp(argv[1], "-vector") ||
906 !strcmp(argv[1], "-string") ||
907 !strcmp(argv[1], "-float") )
910 if (argv[1][1] == 'f')
911 p.vtype = TYPE_FLOAT;
912 else if (argv[1][1] == 's')
913 p.vtype = TYPE_STRING;
914 else if (argv[1][1] == 'v')
915 p.vtype = TYPE_VECTOR;
925 vec_push(main_params, p);
929 else if (!strcmp(argv[1], "--")) {
934 else if (argv[1][0] != '-') {
936 printf("only 1 program file may be specified\n");
957 printf("only 1 program file may be specified\n");
971 prog = prog_load(progsfile);
973 printf("failed to load program '%s'\n", progsfile);
977 prog->builtins = qc_builtins;
978 prog->builtins_count = qc_builtins_count;
981 printf("Program's system-checksum = 0x%04x\n", (unsigned int)prog->crc16);
982 printf("Entity field space: %u\n", (unsigned int)prog->entityfields);
983 printf("Globals: %u\n", (unsigned int)vec_size(prog->globals));
990 (unsigned long)vec_size(prog->code),
991 (unsigned long)vec_size(prog->defs),
992 (unsigned long)vec_size(prog->fields),
993 (unsigned long)vec_size(prog->functions),
994 (unsigned long)vec_size(prog->strings));
1002 for (i = 1; i < vec_size(prog->functions); ++i)
1003 prog_disasm_function(prog, i);
1006 if (opts_printdefs) {
1007 for (i = 0; i < vec_size(prog->defs); ++i) {
1008 printf("Global: %8s %-16s at %u%s\n",
1009 type_name[prog->defs[i].type & DEF_TYPEMASK],
1010 prog_getstring(prog, prog->defs[i].name),
1011 (unsigned int)prog->defs[i].offset,
1012 ((prog->defs[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1015 if (opts_printfields) {
1016 for (i = 0; i < vec_size(prog->fields); ++i) {
1017 printf("Field: %8s %-16s at %u%s\n",
1018 type_name[prog->fields[i].type],
1019 prog_getstring(prog, prog->fields[i].name),
1020 (unsigned int)prog->fields[i].offset,
1021 ((prog->fields[i].type & DEF_SAVEGLOBAL) ? " [SAVE]" : ""));
1024 if (opts_printfuns) {
1025 for (i = 0; i < vec_size(prog->functions); ++i) {
1027 printf("Function: %-16s taking %i parameters:(",
1028 prog_getstring(prog, prog->functions[i].name),
1029 (unsigned int)prog->functions[i].nargs);
1030 for (a = 0; a < prog->functions[i].nargs; ++a) {
1031 printf(" %i", prog->functions[i].argsize[a]);
1033 printf(") locals: %i + %i\n",
1034 prog->functions[i].firstlocal,
1035 prog->functions[i].locals);
1039 for (i = 1; i < vec_size(prog->functions); ++i) {
1040 const char *name = prog_getstring(prog, prog->functions[i].name);
1041 if (!strcmp(name, "main"))
1046 prog_main_setparams(prog);
1047 prog_exec(prog, &prog->functions[fnmain], xflags, VM_JUMPS_DEFAULT);
1050 printf("No main function found\n");
1057 void prog_disasm_function(qc_program *prog, size_t id)
1059 prog_section_function *fdef = prog->functions + id;
1060 prog_section_statement *st;
1062 if (fdef->entry < 0) {
1063 printf("FUNCTION \"%s\" = builtin #%i\n", prog_getstring(prog, fdef->name), (int)-fdef->entry);
1067 printf("FUNCTION \"%s\"\n", prog_getstring(prog, fdef->name));
1069 st = prog->code + fdef->entry;
1070 while (st->opcode != INSTR_DONE) {
1071 prog_print_statement(prog, st);
1076 #else /* !QCVM_LOOP */
1078 * Everything from here on is not including into the compilation of the
1079 * executor. This is simply code that is #included via #include __FILE__
1080 * see when QCVM_LOOP is defined, the rest of the code above do not get
1081 * re-included. So this really just acts like one large macro, but it
1082 * sort of isn't, which makes it nicer looking.
1085 #define OPA ( (qcany*) (prog->globals + st->o1.u1) )
1086 #define OPB ( (qcany*) (prog->globals + st->o2.u1) )
1087 #define OPC ( (qcany*) (prog->globals + st->o3.u1) )
1089 #define GLOBAL(x) ( (qcany*) (prog->globals + (x)) )
1091 /* to be consistent with current darkplaces behaviour */
1092 #if !defined(FLOAT_IS_TRUE_FOR_INT)
1093 # define FLOAT_IS_TRUE_FOR_INT(x) ( (x) & 0x7FFFFFFF )
1097 prog_section_function *newf;
1104 prog->profile[st - prog->code]++;
1108 prog_print_statement(prog, st);
1114 qcvmerror(prog, "Illegal instruction in %s\n", prog->filename);
1119 /* TODO: add instruction count to function profile count */
1120 GLOBAL(OFS_RETURN)->ivector[0] = OPA->ivector[0];
1121 GLOBAL(OFS_RETURN)->ivector[1] = OPA->ivector[1];
1122 GLOBAL(OFS_RETURN)->ivector[2] = OPA->ivector[2];
1124 st = prog->code + prog_leavefunction(prog);
1125 if (!vec_size(prog->stack))
1131 OPC->_float = OPA->_float * OPB->_float;
1134 OPC->_float = OPA->vector[0]*OPB->vector[0] +
1135 OPA->vector[1]*OPB->vector[1] +
1136 OPA->vector[2]*OPB->vector[2];
1139 OPC->vector[0] = OPA->_float * OPB->vector[0];
1140 OPC->vector[1] = OPA->_float * OPB->vector[1];
1141 OPC->vector[2] = OPA->_float * OPB->vector[2];
1144 OPC->vector[0] = OPB->_float * OPA->vector[0];
1145 OPC->vector[1] = OPB->_float * OPA->vector[1];
1146 OPC->vector[2] = OPB->_float * OPA->vector[2];
1149 if (OPB->_float != 0.0f)
1150 OPC->_float = OPA->_float / OPB->_float;
1156 OPC->_float = OPA->_float + OPB->_float;
1159 OPC->vector[0] = OPA->vector[0] + OPB->vector[0];
1160 OPC->vector[1] = OPA->vector[1] + OPB->vector[1];
1161 OPC->vector[2] = OPA->vector[2] + OPB->vector[2];
1164 OPC->_float = OPA->_float - OPB->_float;
1167 OPC->vector[0] = OPA->vector[0] - OPB->vector[0];
1168 OPC->vector[1] = OPA->vector[1] - OPB->vector[1];
1169 OPC->vector[2] = OPA->vector[2] - OPB->vector[2];
1173 OPC->_float = (OPA->_float == OPB->_float);
1176 OPC->_float = ((OPA->vector[0] == OPB->vector[0]) &&
1177 (OPA->vector[1] == OPB->vector[1]) &&
1178 (OPA->vector[2] == OPB->vector[2]) );
1181 OPC->_float = !strcmp(prog_getstring(prog, OPA->string),
1182 prog_getstring(prog, OPB->string));
1185 OPC->_float = (OPA->_int == OPB->_int);
1188 OPC->_float = (OPA->function == OPB->function);
1191 OPC->_float = (OPA->_float != OPB->_float);
1194 OPC->_float = ((OPA->vector[0] != OPB->vector[0]) ||
1195 (OPA->vector[1] != OPB->vector[1]) ||
1196 (OPA->vector[2] != OPB->vector[2]) );
1199 OPC->_float = !!strcmp(prog_getstring(prog, OPA->string),
1200 prog_getstring(prog, OPB->string));
1203 OPC->_float = (OPA->_int != OPB->_int);
1206 OPC->_float = (OPA->function != OPB->function);
1210 OPC->_float = (OPA->_float <= OPB->_float);
1213 OPC->_float = (OPA->_float >= OPB->_float);
1216 OPC->_float = (OPA->_float < OPB->_float);
1219 OPC->_float = (OPA->_float > OPB->_float);
1224 case INSTR_LOAD_FLD:
1225 case INSTR_LOAD_ENT:
1226 case INSTR_LOAD_FNC:
1227 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1228 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1231 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields)) {
1232 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1237 ed = prog_getedict(prog, OPA->edict);
1238 OPC->_int = ((qcany*)( ((qcint*)ed) + OPB->_int ))->_int;
1241 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1242 qcvmerror(prog, "progs `%s` attempted to read an out of bounds entity", prog->filename);
1245 if (OPB->_int < 0 || OPB->_int + 3 > (qcint)prog->entityfields)
1247 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1252 ed = prog_getedict(prog, OPA->edict);
1253 OPC->ivector[0] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[0];
1254 OPC->ivector[1] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[1];
1255 OPC->ivector[2] = ((qcany*)( ((qcint*)ed) + OPB->_int ))->ivector[2];
1259 if (OPA->edict < 0 || OPA->edict >= prog->entities) {
1260 qcvmerror(prog, "prog `%s` attempted to address an out of bounds entity %i", prog->filename, OPA->edict);
1263 if ((unsigned int)(OPB->_int) >= (unsigned int)(prog->entityfields))
1265 qcvmerror(prog, "prog `%s` attempted to read an invalid field from entity (%i)",
1271 ed = prog_getedict(prog, OPA->edict);
1272 OPC->_int = ((qcint*)ed) - prog->entitydata + OPB->_int;
1277 case INSTR_STORE_ENT:
1278 case INSTR_STORE_FLD:
1279 case INSTR_STORE_FNC:
1280 OPB->_int = OPA->_int;
1283 OPB->ivector[0] = OPA->ivector[0];
1284 OPB->ivector[1] = OPA->ivector[1];
1285 OPB->ivector[2] = OPA->ivector[2];
1288 case INSTR_STOREP_F:
1289 case INSTR_STOREP_S:
1290 case INSTR_STOREP_ENT:
1291 case INSTR_STOREP_FLD:
1292 case INSTR_STOREP_FNC:
1293 if (OPB->_int < 0 || OPB->_int >= (qcint)vec_size(prog->entitydata)) {
1294 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1297 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1298 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1300 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1302 ptr = (qcany*)(prog->entitydata + OPB->_int);
1303 ptr->_int = OPA->_int;
1305 case INSTR_STOREP_V:
1306 if (OPB->_int < 0 || OPB->_int + 2 >= (qcint)vec_size(prog->entitydata)) {
1307 qcvmerror(prog, "`%s` attempted to write to an out of bounds edict (%i)", prog->filename, OPB->_int);
1310 if (OPB->_int < (qcint)prog->entityfields && !prog->allowworldwrites)
1311 qcvmerror(prog, "`%s` tried to assign to world.%s (field %i)\n",
1313 prog_getstring(prog, prog_entfield(prog, OPB->_int)->name),
1315 ptr = (qcany*)(prog->entitydata + OPB->_int);
1316 ptr->ivector[0] = OPA->ivector[0];
1317 ptr->ivector[1] = OPA->ivector[1];
1318 ptr->ivector[2] = OPA->ivector[2];
1322 OPC->_float = !FLOAT_IS_TRUE_FOR_INT(OPA->_int);
1325 OPC->_float = !OPA->vector[0] &&
1330 OPC->_float = !OPA->string ||
1331 !*prog_getstring(prog, OPA->string);
1334 OPC->_float = (OPA->edict == 0);
1337 OPC->_float = !OPA->function;
1341 /* this is consistent with darkplaces' behaviour */
1342 if(FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1344 st += st->o2.s1 - 1; /* offset the s++ */
1345 if (++jumpcount >= maxjumps)
1346 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1350 if(!FLOAT_IS_TRUE_FOR_INT(OPA->_int))
1352 st += st->o2.s1 - 1; /* offset the s++ */
1353 if (++jumpcount >= maxjumps)
1354 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1367 prog->argc = st->opcode - INSTR_CALL0;
1369 qcvmerror(prog, "NULL function in `%s`", prog->filename);
1371 if(!OPA->function || OPA->function >= (qcint)vec_size(prog->functions))
1373 qcvmerror(prog, "CALL outside the program in `%s`", prog->filename);
1377 newf = &prog->functions[OPA->function];
1380 prog->statement = (st - prog->code) + 1;
1382 if (newf->entry < 0)
1384 /* negative statements are built in functions */
1385 qcint builtinnumber = -newf->entry;
1386 if (builtinnumber < (qcint)prog->builtins_count && prog->builtins[builtinnumber])
1387 prog->builtins[builtinnumber](prog);
1389 qcvmerror(prog, "No such builtin #%i in %s! Try updating your gmqcc sources",
1390 builtinnumber, prog->filename);
1393 st = prog->code + prog_enterfunction(prog, newf) - 1; /* offset st++ */
1399 qcvmerror(prog, "`%s` tried to execute a STATE operation", prog->filename);
1403 st += st->o1.s1 - 1; /* offset the s++ */
1404 if (++jumpcount == 10000000)
1405 qcvmerror(prog, "`%s` hit the runaway loop counter limit of %li jumps", prog->filename, jumpcount);
1409 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) &&
1410 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1413 OPC->_float = FLOAT_IS_TRUE_FOR_INT(OPA->_int) ||
1414 FLOAT_IS_TRUE_FOR_INT(OPB->_int);
1418 OPC->_float = ((int)OPA->_float) & ((int)OPB->_float);
1421 OPC->_float = ((int)OPA->_float) | ((int)OPB->_float);
1428 #endif /* !QCVM_LOOP */